All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] iommufd: Add top-level bounds check on kernel buffer size
@ 2023-01-27 22:38 Kees Cook
  2023-01-28  0:47 ` Jason Gunthorpe
  0 siblings, 1 reply; 5+ messages in thread
From: Kees Cook @ 2023-01-27 22:38 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Kees Cook, Kevin Tian, Joerg Roedel, Will Deacon, Robin Murphy,
	iommu, linux-kernel, linux-hardening

While the op->size assignments are already bounds-checked at static
initializer time, these limits aren't aggregated and tracked when doing
later variable range checking under -Warray-bounds. Help the compiler
see that we know what we're talking about, and we'll never ask to
write more that sizeof(ucmd.cmd) bytes during the memset() inside
copy_struct_from_user(). Seen under GCC 13:

In function 'copy_struct_from_user',
    inlined from 'iommufd_fops_ioctl' at ../drivers/iommu/iommufd/main.c:333:8:
../include/linux/fortify-string.h:59:33: warning: '__builtin_memset' offset [57, 4294967294] is out of the bounds [0, 56] of object 'buf' with type 'union ucmd_buffer' [-Warray-bounds=]
   59 | #define __underlying_memset     __builtin_memset
      |                                 ^
../include/linux/fortify-string.h:453:9: note: in expansion of macro '__underlying_memset'
  453 |         __underlying_memset(p, c, __fortify_size); \
      |         ^~~~~~~~~~~~~~~~~~~
../include/linux/fortify-string.h:461:25: note: in expansion of macro '__fortify_memset_chk'
  461 | #define memset(p, c, s) __fortify_memset_chk(p, c, s, \
      |                         ^~~~~~~~~~~~~~~~~~~~
../include/linux/uaccess.h:334:17: note: in expansion of macro 'memset'
  334 |                 memset(dst + size, 0, rest);
      |                 ^~~~~~
../drivers/iommu/iommufd/main.c: In function 'iommufd_fops_ioctl':
../drivers/iommu/iommufd/main.c:311:27: note: 'buf' declared here
  311 |         union ucmd_buffer buf;
      |                           ^~~

Cc: Jason Gunthorpe <jgg@ziepe.ca>
Cc: Kevin Tian <kevin.tian@intel.com>
Cc: Joerg Roedel <joro@8bytes.org>
Cc: Will Deacon <will@kernel.org>
Cc: Robin Murphy <robin.murphy@arm.com>
Cc: iommu@lists.linux.dev
Signed-off-by: Kees Cook <keescook@chromium.org>
---
 drivers/iommu/iommufd/main.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/drivers/iommu/iommufd/main.c b/drivers/iommu/iommufd/main.c
index 3fbe636c3d8a..34a1785da33a 100644
--- a/drivers/iommu/iommufd/main.c
+++ b/drivers/iommu/iommufd/main.c
@@ -330,8 +330,9 @@ static long iommufd_fops_ioctl(struct file *filp, unsigned int cmd,
 		return -EINVAL;
 
 	ucmd.cmd = &buf;
-	ret = copy_struct_from_user(ucmd.cmd, op->size, ucmd.ubuffer,
-				    ucmd.user_size);
+	ret = copy_struct_from_user(ucmd.cmd,
+				    min_t(size_t, op->size, sizeof(ucmd.cmd)),
+				    ucmd.ubuffer, ucmd.user_size);
 	if (ret)
 		return ret;
 	ret = op->execute(&ucmd);
-- 
2.34.1


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH] iommufd: Add top-level bounds check on kernel buffer size
  2023-01-27 22:38 [PATCH] iommufd: Add top-level bounds check on kernel buffer size Kees Cook
@ 2023-01-28  0:47 ` Jason Gunthorpe
  2023-01-28  0:57   ` Kees Cook
  0 siblings, 1 reply; 5+ messages in thread
From: Jason Gunthorpe @ 2023-01-28  0:47 UTC (permalink / raw)
  To: Kees Cook
  Cc: Kevin Tian, Joerg Roedel, Will Deacon, Robin Murphy, iommu,
	linux-kernel, linux-hardening

On Fri, Jan 27, 2023 at 02:38:17PM -0800, Kees Cook wrote:
> While the op->size assignments are already bounds-checked at static
> initializer time, these limits aren't aggregated and tracked when doing
> later variable range checking under -Warray-bounds. Help the compiler
> see that we know what we're talking about, and we'll never ask to
> write more that sizeof(ucmd.cmd) bytes during the memset() inside
> copy_struct_from_user(). Seen under GCC 13:
> 
> In function 'copy_struct_from_user',
>     inlined from 'iommufd_fops_ioctl' at ../drivers/iommu/iommufd/main.c:333:8:
> ../include/linux/fortify-string.h:59:33: warning: '__builtin_memset' offset [57, 4294967294] is out of the bounds [0, 56] of object 'buf' with type 'union ucmd_buffer' [-Warray-bounds=]
>    59 | #define __underlying_memset     __builtin_memset

This seems strange to me

I thought the way gcc handled this was if it knew the value must be in
a certain range then it would check it

If it couldn't figure out any ranges it would not make a warning.

So why did it decide "rest" was in that really weird range?

Is this just a compiler bug?

Jason

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] iommufd: Add top-level bounds check on kernel buffer size
  2023-01-28  0:47 ` Jason Gunthorpe
@ 2023-01-28  0:57   ` Kees Cook
  2023-01-28  1:13     ` Jason Gunthorpe
  0 siblings, 1 reply; 5+ messages in thread
From: Kees Cook @ 2023-01-28  0:57 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Kevin Tian, Joerg Roedel, Will Deacon, Robin Murphy, iommu,
	linux-kernel, linux-hardening

On Fri, Jan 27, 2023 at 08:47:34PM -0400, Jason Gunthorpe wrote:
> On Fri, Jan 27, 2023 at 02:38:17PM -0800, Kees Cook wrote:
> > While the op->size assignments are already bounds-checked at static
> > initializer time, these limits aren't aggregated and tracked when doing
> > later variable range checking under -Warray-bounds. Help the compiler
> > see that we know what we're talking about, and we'll never ask to
> > write more that sizeof(ucmd.cmd) bytes during the memset() inside
> > copy_struct_from_user(). Seen under GCC 13:
> > 
> > In function 'copy_struct_from_user',
> >     inlined from 'iommufd_fops_ioctl' at ../drivers/iommu/iommufd/main.c:333:8:
> > ../include/linux/fortify-string.h:59:33: warning: '__builtin_memset' offset [57, 4294967294] is out of the bounds [0, 56] of object 'buf' with type 'union ucmd_buffer' [-Warray-bounds=]
> >    59 | #define __underlying_memset     __builtin_memset
> 
> This seems strange to me
> 
> I thought the way gcc handled this was if it knew the value must be in
> a certain range then it would check it
> 
> If it couldn't figure out any ranges it would not make a warning.
> 
> So why did it decide "rest" was in that really weird range?

It's because it got bounds-checked at the lower end (for the minimum
size test).

> 
> Is this just a compiler bug?

I don't think so. This just keeps the bounds within the buffer size now.

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] iommufd: Add top-level bounds check on kernel buffer size
  2023-01-28  0:57   ` Kees Cook
@ 2023-01-28  1:13     ` Jason Gunthorpe
  2023-02-01  1:01       ` Kees Cook
  0 siblings, 1 reply; 5+ messages in thread
From: Jason Gunthorpe @ 2023-01-28  1:13 UTC (permalink / raw)
  To: Kees Cook
  Cc: Kevin Tian, Joerg Roedel, Will Deacon, Robin Murphy, iommu,
	linux-kernel, linux-hardening

On Fri, Jan 27, 2023 at 04:57:26PM -0800, Kees Cook wrote:
> On Fri, Jan 27, 2023 at 08:47:34PM -0400, Jason Gunthorpe wrote:
> > On Fri, Jan 27, 2023 at 02:38:17PM -0800, Kees Cook wrote:
> > > While the op->size assignments are already bounds-checked at static
> > > initializer time, these limits aren't aggregated and tracked when doing
> > > later variable range checking under -Warray-bounds. Help the compiler
> > > see that we know what we're talking about, and we'll never ask to
> > > write more that sizeof(ucmd.cmd) bytes during the memset() inside
> > > copy_struct_from_user(). Seen under GCC 13:
> > > 
> > > In function 'copy_struct_from_user',
> > >     inlined from 'iommufd_fops_ioctl' at ../drivers/iommu/iommufd/main.c:333:8:
> > > ../include/linux/fortify-string.h:59:33: warning: '__builtin_memset' offset [57, 4294967294] is out of the bounds [0, 56] of object 'buf' with type 'union ucmd_buffer' [-Warray-bounds=]
> > >    59 | #define __underlying_memset     __builtin_memset
> > 
> > This seems strange to me
> > 
> > I thought the way gcc handled this was if it knew the value must be in
> > a certain range then it would check it
> > 
> > If it couldn't figure out any ranges it would not make a warning.
> > 
> > So why did it decide "rest" was in that really weird range?
> 
> It's because it got bounds-checked at the lower end (for the minimum
> size test).

Where? There is no sizeof(ucmd.ubuffer) in this code.

There are no statically computable constants at all.

The minimum size test loads from a struct:

	if (ucmd.user_size < op->min_size)
		return -EINVAL;

So, either gcc can't see through that and thus has no idea what the
bound check is

Or, gcc has figured out that struct iommufd_ioctl_op::min_size has a
finite set of values

If the latter, why doesn't it also know that iommufd_ioctl_op::size
has finite set too?

Combined with the weird report that the upper end of that range is -2
(not UINT_MAX), something very strange is going on inside gcc.

Jason

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH] iommufd: Add top-level bounds check on kernel buffer size
  2023-01-28  1:13     ` Jason Gunthorpe
@ 2023-02-01  1:01       ` Kees Cook
  0 siblings, 0 replies; 5+ messages in thread
From: Kees Cook @ 2023-02-01  1:01 UTC (permalink / raw)
  To: Jason Gunthorpe
  Cc: Kevin Tian, Joerg Roedel, Will Deacon, Robin Murphy, iommu,
	linux-kernel, linux-hardening

On Fri, Jan 27, 2023 at 09:13:31PM -0400, Jason Gunthorpe wrote:
> On Fri, Jan 27, 2023 at 04:57:26PM -0800, Kees Cook wrote:
> > On Fri, Jan 27, 2023 at 08:47:34PM -0400, Jason Gunthorpe wrote:
> > > On Fri, Jan 27, 2023 at 02:38:17PM -0800, Kees Cook wrote:
> > > > While the op->size assignments are already bounds-checked at static
> > > > initializer time, these limits aren't aggregated and tracked when doing
> > > > later variable range checking under -Warray-bounds. Help the compiler
> > > > see that we know what we're talking about, and we'll never ask to
> > > > write more that sizeof(ucmd.cmd) bytes during the memset() inside
> > > > copy_struct_from_user(). Seen under GCC 13:
> > > > 
> > > > In function 'copy_struct_from_user',
> > > >     inlined from 'iommufd_fops_ioctl' at ../drivers/iommu/iommufd/main.c:333:8:
> > > > ../include/linux/fortify-string.h:59:33: warning: '__builtin_memset' offset [57, 4294967294] is out of the bounds [0, 56] of object 'buf' with type 'union ucmd_buffer' [-Warray-bounds=]
> > > >    59 | #define __underlying_memset     __builtin_memset
> > > 
> > > This seems strange to me
> > > 
> > > I thought the way gcc handled this was if it knew the value must be in
> > > a certain range then it would check it
> > > 
> > > If it couldn't figure out any ranges it would not make a warning.
> > > 
> > > So why did it decide "rest" was in that really weird range?
> > 
> > It's because it got bounds-checked at the lower end (for the minimum
> > size test).
> 
> Where? There is no sizeof(ucmd.ubuffer) in this code.

memset() is internally doing that via __builtin_object_size(dst + size, 1).

> There are no statically computable constants at all.

I think it's some logic that excludes a range based on ucmd.user_size
internally to the additional checks in copy_struct_from_user().

Regardless, I think the correct fix should be with
copy_struct_from_user(), so please disregard this patch. :)

-- 
Kees Cook

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2023-02-01  1:02 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-01-27 22:38 [PATCH] iommufd: Add top-level bounds check on kernel buffer size Kees Cook
2023-01-28  0:47 ` Jason Gunthorpe
2023-01-28  0:57   ` Kees Cook
2023-01-28  1:13     ` Jason Gunthorpe
2023-02-01  1:01       ` Kees Cook

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.