Skip to content

Commit f5a1a53

Browse files
cypharChristian Brauner
authored andcommitted
lib: introduce copy_struct_from_user() helper
A common pattern for syscall extensions is increasing the size of a struct passed from userspace, such that the zero-value of the new fields result in the old kernel behaviour (allowing for a mix of userspace and kernel vintages to operate on one another in most cases). While this interface exists for communication in both directions, only one interface is straightforward to have reasonable semantics for (userspace passing a struct to the kernel). For kernel returns to userspace, what the correct semantics are (whether there should be an error if userspace is unaware of a new extension) is very syscall-dependent and thus probably cannot be unified between syscalls (a good example of this problem is [1]). Previously there was no common lib/ function that implemented the necessary extension-checking semantics (and different syscalls implemented them slightly differently or incompletely[2]). Future patches replace common uses of this pattern to make use of copy_struct_from_user(). Some in-kernel selftests that insure that the handling of alignment and various byte patterns are all handled identically to memchr_inv() usage. [1]: commit 1251201 ("sched/core: Fix uclamp ABI bug, clean up and robustify sched_read_attr() ABI logic and code") [2]: For instance {sched_setattr,perf_event_open,clone3}(2) all do do similar checks to copy_struct_from_user() while rt_sigprocmask(2) always rejects differently-sized struct arguments. Suggested-by: Rasmus Villemoes <[email protected]> Signed-off-by: Aleksa Sarai <[email protected]> Reviewed-by: Kees Cook <[email protected]> Reviewed-by: Christian Brauner <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Christian Brauner <[email protected]>
1 parent 54ecb8f commit f5a1a53

File tree

5 files changed

+263
-13
lines changed

5 files changed

+263
-13
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

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

lib/test_user_copy.c

Lines changed: 130 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,133 @@
3131
# define TEST_U64
3232
#endif
3333

34-
#define test(condition, msg) \
35-
({ \
36-
int cond = (condition); \
37-
if (cond) \
38-
pr_warn("%s\n", msg); \
39-
cond; \
34+
#define test(condition, msg, ...) \
35+
({ \
36+
int cond = (condition); \
37+
if (cond) \
38+
pr_warn("[%d] " msg "\n", __LINE__, ##__VA_ARGS__); \
39+
cond; \
4040
})
4141

42+
static bool is_zeroed(void *from, size_t size)
43+
{
44+
return memchr_inv(from, 0x0, size) == NULL;
45+
}
46+
47+
static int test_check_nonzero_user(char *kmem, char __user *umem, size_t size)
48+
{
49+
int ret = 0;
50+
size_t start, end, i;
51+
size_t zero_start = size / 4;
52+
size_t zero_end = size - zero_start;
53+
54+
/*
55+
* We conduct a series of check_nonzero_user() tests on a block of memory
56+
* with the following byte-pattern (trying every possible [start,end]
57+
* pair):
58+
*
59+
* [ 00 ff 00 ff ... 00 00 00 00 ... ff 00 ff 00 ]
60+
*
61+
* And we verify that check_nonzero_user() acts identically to memchr_inv().
62+
*/
63+
64+
memset(kmem, 0x0, size);
65+
for (i = 1; i < zero_start; i += 2)
66+
kmem[i] = 0xff;
67+
for (i = zero_end; i < size; i += 2)
68+
kmem[i] = 0xff;
69+
70+
ret |= test(copy_to_user(umem, kmem, size),
71+
"legitimate copy_to_user failed");
72+
73+
for (start = 0; start <= size; start++) {
74+
for (end = start; end <= size; end++) {
75+
size_t len = end - start;
76+
int retval = check_zeroed_user(umem + start, len);
77+
int expected = is_zeroed(kmem + start, len);
78+
79+
ret |= test(retval != expected,
80+
"check_nonzero_user(=%d) != memchr_inv(=%d) mismatch (start=%zu, end=%zu)",
81+
retval, expected, start, end);
82+
}
83+
}
84+
85+
return ret;
86+
}
87+
88+
static int test_copy_struct_from_user(char *kmem, char __user *umem,
89+
size_t size)
90+
{
91+
int ret = 0;
92+
char *umem_src = NULL, *expected = NULL;
93+
size_t ksize, usize;
94+
95+
umem_src = kmalloc(size, GFP_KERNEL);
96+
if (ret |= test(umem_src == NULL, "kmalloc failed"))
97+
goto out_free;
98+
99+
expected = kmalloc(size, GFP_KERNEL);
100+
if (ret |= test(expected == NULL, "kmalloc failed"))
101+
goto out_free;
102+
103+
/* Fill umem with a fixed byte pattern. */
104+
memset(umem_src, 0x3e, size);
105+
ret |= test(copy_to_user(umem, umem_src, size),
106+
"legitimate copy_to_user failed");
107+
108+
/* Check basic case -- (usize == ksize). */
109+
ksize = size;
110+
usize = size;
111+
112+
memcpy(expected, umem_src, ksize);
113+
114+
memset(kmem, 0x0, size);
115+
ret |= test(copy_struct_from_user(kmem, ksize, umem, usize),
116+
"copy_struct_from_user(usize == ksize) failed");
117+
ret |= test(memcmp(kmem, expected, ksize),
118+
"copy_struct_from_user(usize == ksize) gives unexpected copy");
119+
120+
/* Old userspace case -- (usize < ksize). */
121+
ksize = size;
122+
usize = size / 2;
123+
124+
memcpy(expected, umem_src, usize);
125+
memset(expected + usize, 0x0, ksize - usize);
126+
127+
memset(kmem, 0x0, size);
128+
ret |= test(copy_struct_from_user(kmem, ksize, umem, usize),
129+
"copy_struct_from_user(usize < ksize) failed");
130+
ret |= test(memcmp(kmem, expected, ksize),
131+
"copy_struct_from_user(usize < ksize) gives unexpected copy");
132+
133+
/* New userspace (-E2BIG) case -- (usize > ksize). */
134+
ksize = size / 2;
135+
usize = size;
136+
137+
memset(kmem, 0x0, size);
138+
ret |= test(copy_struct_from_user(kmem, ksize, umem, usize) != -E2BIG,
139+
"copy_struct_from_user(usize > ksize) didn't give E2BIG");
140+
141+
/* New userspace (success) case -- (usize > ksize). */
142+
ksize = size / 2;
143+
usize = size;
144+
145+
memcpy(expected, umem_src, ksize);
146+
ret |= test(clear_user(umem + ksize, usize - ksize),
147+
"legitimate clear_user failed");
148+
149+
memset(kmem, 0x0, size);
150+
ret |= test(copy_struct_from_user(kmem, ksize, umem, usize),
151+
"copy_struct_from_user(usize > ksize) failed");
152+
ret |= test(memcmp(kmem, expected, ksize),
153+
"copy_struct_from_user(usize > ksize) gives unexpected copy");
154+
155+
out_free:
156+
kfree(expected);
157+
kfree(umem_src);
158+
return ret;
159+
}
160+
42161
static int __init test_user_copy_init(void)
43162
{
44163
int ret = 0;
@@ -106,6 +225,11 @@ static int __init test_user_copy_init(void)
106225
#endif
107226
#undef test_legit
108227

228+
/* Test usage of check_nonzero_user(). */
229+
ret |= test_check_nonzero_user(kmem, usermem, 2 * PAGE_SIZE);
230+
/* Test usage of copy_struct_from_user(). */
231+
ret |= test_copy_struct_from_user(kmem, usermem, 2 * PAGE_SIZE);
232+
109233
/*
110234
* Invalid usage: none of these copies should succeed.
111235
*/

lib/usercopy.c

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
// SPDX-License-Identifier: GPL-2.0
22
#include <linux/uaccess.h>
3+
#include <linux/bitops.h>
34

45
/* out-of-line parts */
56

@@ -31,3 +32,57 @@ unsigned long _copy_to_user(void __user *to, const void *from, unsigned long n)
3132
}
3233
EXPORT_SYMBOL(_copy_to_user);
3334
#endif
35+
36+
/**
37+
* check_zeroed_user: check if a userspace buffer only contains zero bytes
38+
* @from: Source address, in userspace.
39+
* @size: Size of buffer.
40+
*
41+
* This is effectively shorthand for "memchr_inv(from, 0, size) == NULL" for
42+
* userspace addresses (and is more efficient because we don't care where the
43+
* first non-zero byte is).
44+
*
45+
* Returns:
46+
* * 0: There were non-zero bytes present in the buffer.
47+
* * 1: The buffer was full of zero bytes.
48+
* * -EFAULT: access to userspace failed.
49+
*/
50+
int check_zeroed_user(const void __user *from, size_t size)
51+
{
52+
unsigned long val;
53+
uintptr_t align = (uintptr_t) from % sizeof(unsigned long);
54+
55+
if (unlikely(size == 0))
56+
return 1;
57+
58+
from -= align;
59+
size += align;
60+
61+
if (!user_access_begin(from, size))
62+
return -EFAULT;
63+
64+
unsafe_get_user(val, (unsigned long __user *) from, err_fault);
65+
if (align)
66+
val &= ~aligned_byte_mask(align);
67+
68+
while (size > sizeof(unsigned long)) {
69+
if (unlikely(val))
70+
goto done;
71+
72+
from += sizeof(unsigned long);
73+
size -= sizeof(unsigned long);
74+
75+
unsafe_get_user(val, (unsigned long __user *) from, err_fault);
76+
}
77+
78+
if (size < sizeof(unsigned long))
79+
val &= aligned_byte_mask(size);
80+
81+
done:
82+
user_access_end();
83+
return (val == 0);
84+
err_fault:
85+
user_access_end();
86+
return -EFAULT;
87+
}
88+
EXPORT_SYMBOL(check_zeroed_user);

0 commit comments

Comments
 (0)