Skip to content

Commit

Permalink
Merge tag 'copy-struct-from-user-v5.4-rc2' of git://git.kernel.org/pu…
Browse files Browse the repository at this point in the history
…b/scm/linux/kernel/git/brauner/linux

Pull copy_struct_from_user() helper from Christian Brauner:
 "This contains the copy_struct_from_user() helper which got split out
  from the openat2() patchset. It is a generic interface designed to
  copy a struct from userspace.

  The helper will be especially useful for structs versioned by size of
  which we have quite a few. This allows for backwards compatibility,
  i.e. an extended struct can be passed to an older kernel, or a legacy
  struct can be passed to a newer kernel. For the first case (extended
  struct, older kernel) the new fields in an extended struct can be set
  to zero and the struct safely passed to an older kernel.

  The most obvious benefit is that this helper lets us get rid of
  duplicate code present in at least sched_setattr(), perf_event_open(),
  and clone3(). More importantly it will also help to ensure that users
  implementing versioning-by-size end up with the same core semantics.

  This point is especially crucial since we have at least one case where
  versioning-by-size is used but with slighly different semantics:
  sched_setattr(), perf_event_open(), and clone3() all do do similar
  checks to copy_struct_from_user() while rt_sigprocmask(2) always
  rejects differently-sized struct arguments.

  With this pull request we also switch over sched_setattr(),
  perf_event_open(), and clone3() to use the new helper"

* tag 'copy-struct-from-user-v5.4-rc2' of git://git.kernel.org/pub/scm/linux/kernel/git/brauner/linux:
  usercopy: Add parentheses around assignment in test_copy_struct_from_user
  perf_event_open: switch to copy_struct_from_user()
  sched_setattr: switch to copy_struct_from_user()
  clone3: switch to copy_struct_from_user()
  lib: introduce copy_struct_from_user() helper
  • Loading branch information
torvalds committed Oct 4, 2019
2 parents af0622f + 3411158 commit e524d16
Show file tree
Hide file tree
Showing 9 changed files with 288 additions and 114 deletions.
7 changes: 7 additions & 0 deletions include/linux/bitops.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,13 @@
#include <asm/types.h>
#include <linux/bits.h>

/* Set bits in the first 'n' bytes when loaded from memory */
#ifdef __LITTLE_ENDIAN
# define aligned_byte_mask(n) ((1UL << 8*(n))-1)
#else
# define aligned_byte_mask(n) (~0xffUL << (BITS_PER_LONG - 8 - 8*(n)))
#endif

#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(long))

Expand Down
70 changes: 70 additions & 0 deletions include/linux/uaccess.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,76 @@ __copy_from_user_inatomic_nocache(void *to, const void __user *from,

#endif /* ARCH_HAS_NOCACHE_UACCESS */

extern __must_check int check_zeroed_user(const void __user *from, size_t size);

/**
* copy_struct_from_user: copy a struct from userspace
* @dst: Destination address, in kernel space. This buffer must be @ksize
* bytes long.
* @ksize: Size of @dst struct.
* @src: Source address, in userspace.
* @usize: (Alleged) size of @src struct.
*
* Copies a struct from userspace to kernel space, in a way that guarantees
* backwards-compatibility for struct syscall arguments (as long as future
* struct extensions are made such that all new fields are *appended* to the
* old struct, and zeroed-out new fields have the same meaning as the old
* struct).
*
* @ksize is just sizeof(*dst), and @usize should've been passed by userspace.
* The recommended usage is something like the following:
*
* SYSCALL_DEFINE2(foobar, const struct foo __user *, uarg, size_t, usize)
* {
* int err;
* struct foo karg = {};
*
* if (usize > PAGE_SIZE)
* return -E2BIG;
* if (usize < FOO_SIZE_VER0)
* return -EINVAL;
*
* err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
* if (err)
* return err;
*
* // ...
* }
*
* There are three cases to consider:
* * If @usize == @ksize, then it's copied verbatim.
* * If @usize < @ksize, then the userspace has passed an old struct to a
* newer kernel. The rest of the trailing bytes in @dst (@ksize - @usize)
* are to be zero-filled.
* * If @usize > @ksize, then the userspace has passed a new struct to an
* older kernel. The trailing bytes unknown to the kernel (@usize - @ksize)
* are checked to ensure they are zeroed, otherwise -E2BIG is returned.
*
* Returns (in all cases, some data may have been copied):
* * -E2BIG: (@usize > @ksize) and there are non-zero trailing bytes in @src.
* * -EFAULT: access to userspace failed.
*/
static __always_inline __must_check int
copy_struct_from_user(void *dst, size_t ksize, const void __user *src,
size_t usize)
{
size_t size = min(ksize, usize);
size_t rest = max(ksize, usize) - size;

/* Deal with trailing bytes. */
if (usize < ksize) {
memset(dst + size, 0, rest);
} else if (usize > ksize) {
int ret = check_zeroed_user(src + size, rest);
if (ret <= 0)
return ret ?: -E2BIG;
}
/* Copy the interoperable parts of the struct. */
if (copy_from_user(dst, src, size))
return -EFAULT;
return 0;
}

/*
* probe_kernel_read(): safely attempt to read from a location
* @dst: pointer to the buffer that shall take the data
Expand Down
2 changes: 2 additions & 0 deletions include/uapi/linux/sched.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ struct clone_args {
};
#endif

#define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */

/*
* Scheduling policies
*/
Expand Down
47 changes: 9 additions & 38 deletions kernel/events/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -10586,55 +10586,26 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
u32 size;
int ret;

if (!access_ok(uattr, PERF_ATTR_SIZE_VER0))
return -EFAULT;

/*
* zero the full structure, so that a short copy will be nice.
*/
/* Zero the full structure, so that a short copy will be nice. */
memset(attr, 0, sizeof(*attr));

ret = get_user(size, &uattr->size);
if (ret)
return ret;

if (size > PAGE_SIZE) /* silly large */
goto err_size;

if (!size) /* abi compat */
/* ABI compatibility quirk: */
if (!size)
size = PERF_ATTR_SIZE_VER0;

if (size < PERF_ATTR_SIZE_VER0)
if (size < PERF_ATTR_SIZE_VER0 || size > PAGE_SIZE)
goto err_size;

/*
* If we're handed a bigger struct than we know of,
* ensure all the unknown bits are 0 - i.e. new
* user-space does not rely on any kernel feature
* extensions we dont know about yet.
*/
if (size > sizeof(*attr)) {
unsigned char __user *addr;
unsigned char __user *end;
unsigned char val;

addr = (void __user *)uattr + sizeof(*attr);
end = (void __user *)uattr + size;

for (; addr < end; addr++) {
ret = get_user(val, addr);
if (ret)
return ret;
if (val)
goto err_size;
}
size = sizeof(*attr);
ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
if (ret) {
if (ret == -E2BIG)
goto err_size;
return ret;
}

ret = copy_from_user(attr, uattr, size);
if (ret)
return -EFAULT;

attr->size = size;

if (attr->__reserved_1)
Expand Down
34 changes: 7 additions & 27 deletions kernel/fork.c
Original file line number Diff line number Diff line change
Expand Up @@ -2525,39 +2525,19 @@ SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
#ifdef __ARCH_WANT_SYS_CLONE3
noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
struct clone_args __user *uargs,
size_t size)
size_t usize)
{
int err;
struct clone_args args;

if (unlikely(size > PAGE_SIZE))
if (unlikely(usize > PAGE_SIZE))
return -E2BIG;

if (unlikely(size < sizeof(struct clone_args)))
if (unlikely(usize < CLONE_ARGS_SIZE_VER0))
return -EINVAL;

if (unlikely(!access_ok(uargs, size)))
return -EFAULT;

if (size > sizeof(struct clone_args)) {
unsigned char __user *addr;
unsigned char __user *end;
unsigned char val;

addr = (void __user *)uargs + sizeof(struct clone_args);
end = (void __user *)uargs + size;

for (; addr < end; addr++) {
if (get_user(val, addr))
return -EFAULT;
if (val)
return -E2BIG;
}

size = sizeof(struct clone_args);
}

if (copy_from_user(&args, uargs, size))
return -EFAULT;
err = copy_struct_from_user(&args, sizeof(args), uargs, usize);
if (err)
return err;

/*
* Verify that higher 32bits of exit_signal are unset and that
Expand Down
43 changes: 7 additions & 36 deletions kernel/sched/core.c
Original file line number Diff line number Diff line change
Expand Up @@ -5106,55 +5106,26 @@ static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *a
u32 size;
int ret;

if (!access_ok(uattr, SCHED_ATTR_SIZE_VER0))
return -EFAULT;

/* Zero the full structure, so that a short copy will be nice: */
memset(attr, 0, sizeof(*attr));

ret = get_user(size, &uattr->size);
if (ret)
return ret;

/* Bail out on silly large: */
if (size > PAGE_SIZE)
goto err_size;

/* ABI compatibility quirk: */
if (!size)
size = SCHED_ATTR_SIZE_VER0;

if (size < SCHED_ATTR_SIZE_VER0)
if (size < SCHED_ATTR_SIZE_VER0 || size > PAGE_SIZE)
goto err_size;

/*
* If we're handed a bigger struct than we know of,
* ensure all the unknown bits are 0 - i.e. new
* user-space does not rely on any kernel feature
* extensions we dont know about yet.
*/
if (size > sizeof(*attr)) {
unsigned char __user *addr;
unsigned char __user *end;
unsigned char val;

addr = (void __user *)uattr + sizeof(*attr);
end = (void __user *)uattr + size;

for (; addr < end; addr++) {
ret = get_user(val, addr);
if (ret)
return ret;
if (val)
goto err_size;
}
size = sizeof(*attr);
ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
if (ret) {
if (ret == -E2BIG)
goto err_size;
return ret;
}

ret = copy_from_user(attr, uattr, size);
if (ret)
return -EFAULT;

if ((attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) &&
size < SCHED_ATTR_SIZE_VER1)
return -EINVAL;
Expand Down Expand Up @@ -5354,7 +5325,7 @@ sched_attr_copy_to_user(struct sched_attr __user *uattr,
* sys_sched_getattr - similar to sched_getparam, but with sched_attr
* @pid: the pid in question.
* @uattr: structure containing the extended parameters.
* @usize: sizeof(attr) that user-space knows about, for forwards and backwards compatibility.
* @usize: sizeof(attr) for fwd/bwd comp.
* @flags: for future extension.
*/
SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,
Expand Down
8 changes: 1 addition & 7 deletions lib/strnlen_user.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,10 @@
#include <linux/export.h>
#include <linux/uaccess.h>
#include <linux/mm.h>
#include <linux/bitops.h>

#include <asm/word-at-a-time.h>

/* Set bits in the first 'n' bytes when loaded from memory */
#ifdef __LITTLE_ENDIAN
# define aligned_byte_mask(n) ((1ul << 8*(n))-1)
#else
# define aligned_byte_mask(n) (~0xfful << (BITS_PER_LONG - 8 - 8*(n)))
#endif

/*
* Do a strnlen, return length of string *with* final '\0'.
* 'count' is the user-supplied count, while 'max' is the
Expand Down
Loading

0 comments on commit e524d16

Please sign in to comment.