Skip to content

Commit

Permalink
iov_iter: avoid wrap-around instrumentation in copy_compat_iovec_from…
Browse files Browse the repository at this point in the history
…_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

Link: KSPP#26 [1]
Link: https://lkml.kernel.org/r/20240129183729.work.991-kees@kernel.org
Signed-off-by: Kees Cook <keescook@chromium.org>
Cc: Christian Brauner <brauner@kernel.org>
Cc: Alexander Viro <viro@zeniv.linux.org.uk>
Signed-off-by: Andrew Morton <akpm@linux-foundation.org>
  • Loading branch information
kees authored and akpm00 committed Feb 3, 2024
1 parent 8f5f6a4 commit 40bd16c
Showing 1 changed file with 3 additions and 2 deletions.
5 changes: 3 additions & 2 deletions lib/iov_iter.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down

0 comments on commit 40bd16c

Please sign in to comment.