Skip to content

Commit e524d16

Browse files
committed
Merge tag 'copy-struct-from-user-v5.4-rc2' of git://git.kernel.org/pub/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
2 parents af0622f + 3411158 commit e524d16

File tree

9 files changed

+288
-114
lines changed

9 files changed

+288
-114
lines changed

include/linux/bitops.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
#include <asm/types.h>
55
#include <linux/bits.h>
66

7+
/* Set bits in the first 'n' bytes when loaded from memory */
8+
#ifdef __LITTLE_ENDIAN
9+
# define aligned_byte_mask(n) ((1UL << 8*(n))-1)
10+
#else
11+
# define aligned_byte_mask(n) (~0xffUL << (BITS_PER_LONG - 8 - 8*(n)))
12+
#endif
13+
714
#define BITS_PER_TYPE(type) (sizeof(type) * BITS_PER_BYTE)
815
#define BITS_TO_LONGS(nr) DIV_ROUND_UP(nr, BITS_PER_TYPE(long))
916

include/linux/uaccess.h

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,76 @@ __copy_from_user_inatomic_nocache(void *to, const void __user *from,
231231

232232
#endif /* ARCH_HAS_NOCACHE_UACCESS */
233233

234+
extern __must_check int check_zeroed_user(const void __user *from, size_t size);
235+
236+
/**
237+
* copy_struct_from_user: copy a struct from userspace
238+
* @dst: Destination address, in kernel space. This buffer must be @ksize
239+
* bytes long.
240+
* @ksize: Size of @dst struct.
241+
* @src: Source address, in userspace.
242+
* @usize: (Alleged) size of @src struct.
243+
*
244+
* Copies a struct from userspace to kernel space, in a way that guarantees
245+
* backwards-compatibility for struct syscall arguments (as long as future
246+
* struct extensions are made such that all new fields are *appended* to the
247+
* old struct, and zeroed-out new fields have the same meaning as the old
248+
* struct).
249+
*
250+
* @ksize is just sizeof(*dst), and @usize should've been passed by userspace.
251+
* The recommended usage is something like the following:
252+
*
253+
* SYSCALL_DEFINE2(foobar, const struct foo __user *, uarg, size_t, usize)
254+
* {
255+
* int err;
256+
* struct foo karg = {};
257+
*
258+
* if (usize > PAGE_SIZE)
259+
* return -E2BIG;
260+
* if (usize < FOO_SIZE_VER0)
261+
* return -EINVAL;
262+
*
263+
* err = copy_struct_from_user(&karg, sizeof(karg), uarg, usize);
264+
* if (err)
265+
* return err;
266+
*
267+
* // ...
268+
* }
269+
*
270+
* There are three cases to consider:
271+
* * If @usize == @ksize, then it's copied verbatim.
272+
* * If @usize < @ksize, then the userspace has passed an old struct to a
273+
* newer kernel. The rest of the trailing bytes in @dst (@ksize - @usize)
274+
* are to be zero-filled.
275+
* * If @usize > @ksize, then the userspace has passed a new struct to an
276+
* older kernel. The trailing bytes unknown to the kernel (@usize - @ksize)
277+
* are checked to ensure they are zeroed, otherwise -E2BIG is returned.
278+
*
279+
* Returns (in all cases, some data may have been copied):
280+
* * -E2BIG: (@usize > @ksize) and there are non-zero trailing bytes in @src.
281+
* * -EFAULT: access to userspace failed.
282+
*/
283+
static __always_inline __must_check int
284+
copy_struct_from_user(void *dst, size_t ksize, const void __user *src,
285+
size_t usize)
286+
{
287+
size_t size = min(ksize, usize);
288+
size_t rest = max(ksize, usize) - size;
289+
290+
/* Deal with trailing bytes. */
291+
if (usize < ksize) {
292+
memset(dst + size, 0, rest);
293+
} else if (usize > ksize) {
294+
int ret = check_zeroed_user(src + size, rest);
295+
if (ret <= 0)
296+
return ret ?: -E2BIG;
297+
}
298+
/* Copy the interoperable parts of the struct. */
299+
if (copy_from_user(dst, src, size))
300+
return -EFAULT;
301+
return 0;
302+
}
303+
234304
/*
235305
* probe_kernel_read(): safely attempt to read from a location
236306
* @dst: pointer to the buffer that shall take the data

include/uapi/linux/sched.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,8 @@ struct clone_args {
7171
};
7272
#endif
7373

74+
#define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */
75+
7476
/*
7577
* Scheduling policies
7678
*/

kernel/events/core.c

Lines changed: 9 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -10586,55 +10586,26 @@ static int perf_copy_attr(struct perf_event_attr __user *uattr,
1058610586
u32 size;
1058710587
int ret;
1058810588

10589-
if (!access_ok(uattr, PERF_ATTR_SIZE_VER0))
10590-
return -EFAULT;
10591-
10592-
/*
10593-
* zero the full structure, so that a short copy will be nice.
10594-
*/
10589+
/* Zero the full structure, so that a short copy will be nice. */
1059510590
memset(attr, 0, sizeof(*attr));
1059610591

1059710592
ret = get_user(size, &uattr->size);
1059810593
if (ret)
1059910594
return ret;
1060010595

10601-
if (size > PAGE_SIZE) /* silly large */
10602-
goto err_size;
10603-
10604-
if (!size) /* abi compat */
10596+
/* ABI compatibility quirk: */
10597+
if (!size)
1060510598
size = PERF_ATTR_SIZE_VER0;
10606-
10607-
if (size < PERF_ATTR_SIZE_VER0)
10599+
if (size < PERF_ATTR_SIZE_VER0 || size > PAGE_SIZE)
1060810600
goto err_size;
1060910601

10610-
/*
10611-
* If we're handed a bigger struct than we know of,
10612-
* ensure all the unknown bits are 0 - i.e. new
10613-
* user-space does not rely on any kernel feature
10614-
* extensions we dont know about yet.
10615-
*/
10616-
if (size > sizeof(*attr)) {
10617-
unsigned char __user *addr;
10618-
unsigned char __user *end;
10619-
unsigned char val;
10620-
10621-
addr = (void __user *)uattr + sizeof(*attr);
10622-
end = (void __user *)uattr + size;
10623-
10624-
for (; addr < end; addr++) {
10625-
ret = get_user(val, addr);
10626-
if (ret)
10627-
return ret;
10628-
if (val)
10629-
goto err_size;
10630-
}
10631-
size = sizeof(*attr);
10602+
ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
10603+
if (ret) {
10604+
if (ret == -E2BIG)
10605+
goto err_size;
10606+
return ret;
1063210607
}
1063310608

10634-
ret = copy_from_user(attr, uattr, size);
10635-
if (ret)
10636-
return -EFAULT;
10637-
1063810609
attr->size = size;
1063910610

1064010611
if (attr->__reserved_1)

kernel/fork.c

Lines changed: 7 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -2525,39 +2525,19 @@ SYSCALL_DEFINE5(clone, unsigned long, clone_flags, unsigned long, newsp,
25252525
#ifdef __ARCH_WANT_SYS_CLONE3
25262526
noinline static int copy_clone_args_from_user(struct kernel_clone_args *kargs,
25272527
struct clone_args __user *uargs,
2528-
size_t size)
2528+
size_t usize)
25292529
{
2530+
int err;
25302531
struct clone_args args;
25312532

2532-
if (unlikely(size > PAGE_SIZE))
2533+
if (unlikely(usize > PAGE_SIZE))
25332534
return -E2BIG;
2534-
2535-
if (unlikely(size < sizeof(struct clone_args)))
2535+
if (unlikely(usize < CLONE_ARGS_SIZE_VER0))
25362536
return -EINVAL;
25372537

2538-
if (unlikely(!access_ok(uargs, size)))
2539-
return -EFAULT;
2540-
2541-
if (size > sizeof(struct clone_args)) {
2542-
unsigned char __user *addr;
2543-
unsigned char __user *end;
2544-
unsigned char val;
2545-
2546-
addr = (void __user *)uargs + sizeof(struct clone_args);
2547-
end = (void __user *)uargs + size;
2548-
2549-
for (; addr < end; addr++) {
2550-
if (get_user(val, addr))
2551-
return -EFAULT;
2552-
if (val)
2553-
return -E2BIG;
2554-
}
2555-
2556-
size = sizeof(struct clone_args);
2557-
}
2558-
2559-
if (copy_from_user(&args, uargs, size))
2560-
return -EFAULT;
2538+
err = copy_struct_from_user(&args, sizeof(args), uargs, usize);
2539+
if (err)
2540+
return err;
25612541

25622542
/*
25632543
* Verify that higher 32bits of exit_signal are unset and that

kernel/sched/core.c

Lines changed: 7 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -5106,55 +5106,26 @@ static int sched_copy_attr(struct sched_attr __user *uattr, struct sched_attr *a
51065106
u32 size;
51075107
int ret;
51085108

5109-
if (!access_ok(uattr, SCHED_ATTR_SIZE_VER0))
5110-
return -EFAULT;
5111-
51125109
/* Zero the full structure, so that a short copy will be nice: */
51135110
memset(attr, 0, sizeof(*attr));
51145111

51155112
ret = get_user(size, &uattr->size);
51165113
if (ret)
51175114
return ret;
51185115

5119-
/* Bail out on silly large: */
5120-
if (size > PAGE_SIZE)
5121-
goto err_size;
5122-
51235116
/* ABI compatibility quirk: */
51245117
if (!size)
51255118
size = SCHED_ATTR_SIZE_VER0;
5126-
5127-
if (size < SCHED_ATTR_SIZE_VER0)
5119+
if (size < SCHED_ATTR_SIZE_VER0 || size > PAGE_SIZE)
51285120
goto err_size;
51295121

5130-
/*
5131-
* If we're handed a bigger struct than we know of,
5132-
* ensure all the unknown bits are 0 - i.e. new
5133-
* user-space does not rely on any kernel feature
5134-
* extensions we dont know about yet.
5135-
*/
5136-
if (size > sizeof(*attr)) {
5137-
unsigned char __user *addr;
5138-
unsigned char __user *end;
5139-
unsigned char val;
5140-
5141-
addr = (void __user *)uattr + sizeof(*attr);
5142-
end = (void __user *)uattr + size;
5143-
5144-
for (; addr < end; addr++) {
5145-
ret = get_user(val, addr);
5146-
if (ret)
5147-
return ret;
5148-
if (val)
5149-
goto err_size;
5150-
}
5151-
size = sizeof(*attr);
5122+
ret = copy_struct_from_user(attr, sizeof(*attr), uattr, size);
5123+
if (ret) {
5124+
if (ret == -E2BIG)
5125+
goto err_size;
5126+
return ret;
51525127
}
51535128

5154-
ret = copy_from_user(attr, uattr, size);
5155-
if (ret)
5156-
return -EFAULT;
5157-
51585129
if ((attr->sched_flags & SCHED_FLAG_UTIL_CLAMP) &&
51595130
size < SCHED_ATTR_SIZE_VER1)
51605131
return -EINVAL;
@@ -5354,7 +5325,7 @@ sched_attr_copy_to_user(struct sched_attr __user *uattr,
53545325
* sys_sched_getattr - similar to sched_getparam, but with sched_attr
53555326
* @pid: the pid in question.
53565327
* @uattr: structure containing the extended parameters.
5357-
* @usize: sizeof(attr) that user-space knows about, for forwards and backwards compatibility.
5328+
* @usize: sizeof(attr) for fwd/bwd comp.
53585329
* @flags: for future extension.
53595330
*/
53605331
SYSCALL_DEFINE4(sched_getattr, pid_t, pid, struct sched_attr __user *, uattr,

lib/strnlen_user.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,10 @@
33
#include <linux/export.h>
44
#include <linux/uaccess.h>
55
#include <linux/mm.h>
6+
#include <linux/bitops.h>
67

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

9-
/* Set bits in the first 'n' bytes when loaded from memory */
10-
#ifdef __LITTLE_ENDIAN
11-
# define aligned_byte_mask(n) ((1ul << 8*(n))-1)
12-
#else
13-
# define aligned_byte_mask(n) (~0xfful << (BITS_PER_LONG - 8 - 8*(n)))
14-
#endif
15-
1610
/*
1711
* Do a strnlen, return length of string *with* final '\0'.
1812
* 'count' is the user-supplied count, while 'max' is the

0 commit comments

Comments
 (0)