[<prev] [next>] [thread-next>] [day] [month] [year] [list]
Message-Id: <20230127223816.never.413-kees@kernel.org>
Date: Fri, 27 Jan 2023 14:38:17 -0800
From: Kees Cook <keescook@...omium.org>
To: Jason Gunthorpe <jgg@...pe.ca>
Cc: Kees Cook <keescook@...omium.org>,
Kevin Tian <kevin.tian@...el.com>,
Joerg Roedel <joro@...tes.org>, Will Deacon <will@...nel.org>,
Robin Murphy <robin.murphy@....com>, iommu@...ts.linux.dev,
linux-kernel@...r.kernel.org, linux-hardening@...r.kernel.org
Subject: [PATCH] iommufd: Add top-level bounds check on kernel buffer size
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@...pe.ca>
Cc: Kevin Tian <kevin.tian@...el.com>
Cc: Joerg Roedel <joro@...tes.org>
Cc: Will Deacon <will@...nel.org>
Cc: Robin Murphy <robin.murphy@....com>
Cc: iommu@...ts.linux.dev
Signed-off-by: Kees Cook <keescook@...omium.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
Powered by blists - more mailing lists