iov_iter: Avoid wrap-around instrumentation in copy_compat_iovec_from_user()
Subsystems (2)
PATCH 1/1 iov_iter: Avoid wrap-around instrumentation in copy_compat_iovec_from_user()
The loop counter "i" in copy_compat_iovec_from_user() is an int, but because the nr_segs argument is unsigned long, the signed overflow sanitizer got worried "i" could wrap around. Instead of making "i" an unsigned long (which may enlarge the type size), switch both nr_segs and i to u32. There is no truncation with nr_segs since it is never larger than UIO_MAXIOV anyway. This keeps sanitizer instrumentation[1] out of a UACCESS path:
vmlinux.o: warning: objtool: copy_compat_iovec_from_user+0xa9: call to __ubsan_handle_add_overflow() with UACCESS enabled
--- lib/iov_iter.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-)
lib/iov_iter.c +3 -2
@@ -1166,11 +1166,12 @@ const void *dup_iter(struct iov_iter *new, struct iov_iter *old, gfp_t flags)EXPORT_SYMBOL (dup_iter);static __noclone int copy_compat_iovec_from_user (struct iovec *iov, -const struct iovec __user *uvec,unsigned long nr_segs) +const struct iovec __user *uvec,u32 nr_segs) {const struct compat_iovec __user *uiov = (const struct compat_iovec __user *)uvec; -int ret = -EFAULT , i; +int ret = -EFAULT ; +u32 i;if (!user_access_begin (uiov, nr_segs *sizeof (*uiov)))return -EFAULT ;
Signature · 1 line
2.34.1
Christian Brauner
AppliedQuoted 9 lines · Kees Cook
The loop counter "i" in
copy_compat_iovec_from_user()is an int, but because thenr_segsargument is unsigned long, the signed overflow sanitizer got worried "i" could wrap around. Instead of making "i" an unsigned long (which may enlarge the type size), switch bothnr_segsand i to u32. There is no truncation withnr_segssince it is never larger thanUIO_MAXIOVanyway. This keeps sanitizer instrumentation[1] out of a UACCESS path:[...]
Applied to the vfs.misc branch of the vfs/vfs.git tree. Patches in the vfs.misc branch should appear in linux-next soon.
Please report any outstanding bugs that were missed during review in a new review to the original patch series allowing us to drop it.
It's encouraged to provide Acked-bys and Reviewed-bys even though the patch has now been applied. If possible patch trailers will be updated.
Note that commit hashes shown below are subject to change due to rebase, trailer updates or similar. If in doubt, please check the listed branch.
tree: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git branch: vfs.misc
[1/1] iov_iter: Avoid wrap-around instrumentation in copy_compat_iovec_from_user()
https://git.kernel.org/vfs/vfs/c/2d5099585c5b