Skip to content

Commit 08411a7

Browse files
committed
Merge tag 'usercopy-v4.8-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull hardened usercopy fixes from Kees Cook: - inline copy_*_user() for correct use of __builtin_const_p() for hardened usercopy and the recent compile-time checks. - switch hardened usercopy to only check non-const size arguments to avoid meaningless checks on likely-sane const values. - update lkdtm usercopy tests to compenstate for the const checking. * tag 'usercopy-v4.8-rc6' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: lkdtm: adjust usercopy tests to bypass const checks usercopy: fold builtin_const check into inline function x86/uaccess: force copy_*_user() to be inlined
2 parents d060e0f + 3c17648 commit 08411a7

7 files changed

Lines changed: 38 additions & 41 deletions

File tree

‎arch/ia64/include/asm/uaccess.h‎

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -241,17 +241,15 @@ extern unsigned long __must_check __copy_user (void __user *to, const void __use
241241
static inline unsigned long
242242
__copy_to_user (void __user *to, const void *from, unsigned long count)
243243
{
244-
if (!__builtin_constant_p(count))
245-
check_object_size(from, count, true);
244+
check_object_size(from, count, true);
246245

247246
return __copy_user(to, (__force void __user *) from, count);
248247
}
249248

250249
static inline unsigned long
251250
__copy_from_user (void *to, const void __user *from, unsigned long count)
252251
{
253-
if (!__builtin_constant_p(count))
254-
check_object_size(to, count, false);
252+
check_object_size(to, count, false);
255253

256254
return __copy_user((__force void __user *) to, from, count);
257255
}
@@ -265,8 +263,7 @@ __copy_from_user (void *to, const void __user *from, unsigned long count)
265263
long __cu_len = (n); \
266264
\
267265
if (__access_ok(__cu_to, __cu_len, get_fs())) { \
268-
if (!__builtin_constant_p(n)) \
269-
check_object_size(__cu_from, __cu_len, true); \
266+
check_object_size(__cu_from, __cu_len, true); \
270267
__cu_len = __copy_user(__cu_to, (__force void __user *) __cu_from, __cu_len); \
271268
} \
272269
__cu_len; \
@@ -280,8 +277,7 @@ __copy_from_user (void *to, const void __user *from, unsigned long count)
280277
\
281278
__chk_user_ptr(__cu_from); \
282279
if (__access_ok(__cu_from, __cu_len, get_fs())) { \
283-
if (!__builtin_constant_p(n)) \
284-
check_object_size(__cu_to, __cu_len, false); \
280+
check_object_size(__cu_to, __cu_len, false); \
285281
__cu_len = __copy_user((__force void __user *) __cu_to, __cu_from, __cu_len); \
286282
} \
287283
__cu_len; \

‎arch/powerpc/include/asm/uaccess.h‎

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -311,14 +311,12 @@ static inline unsigned long copy_from_user(void *to,
311311
unsigned long over;
312312

313313
if (access_ok(VERIFY_READ, from, n)) {
314-
if (!__builtin_constant_p(n))
315-
check_object_size(to, n, false);
314+
check_object_size(to, n, false);
316315
return __copy_tofrom_user((__force void __user *)to, from, n);
317316
}
318317
if ((unsigned long)from < TASK_SIZE) {
319318
over = (unsigned long)from + n - TASK_SIZE;
320-
if (!__builtin_constant_p(n - over))
321-
check_object_size(to, n - over, false);
319+
check_object_size(to, n - over, false);
322320
return __copy_tofrom_user((__force void __user *)to, from,
323321
n - over) + over;
324322
}
@@ -331,14 +329,12 @@ static inline unsigned long copy_to_user(void __user *to,
331329
unsigned long over;
332330

333331
if (access_ok(VERIFY_WRITE, to, n)) {
334-
if (!__builtin_constant_p(n))
335-
check_object_size(from, n, true);
332+
check_object_size(from, n, true);
336333
return __copy_tofrom_user(to, (__force void __user *)from, n);
337334
}
338335
if ((unsigned long)to < TASK_SIZE) {
339336
over = (unsigned long)to + n - TASK_SIZE;
340-
if (!__builtin_constant_p(n))
341-
check_object_size(from, n - over, true);
337+
check_object_size(from, n - over, true);
342338
return __copy_tofrom_user(to, (__force void __user *)from,
343339
n - over) + over;
344340
}
@@ -383,8 +379,7 @@ static inline unsigned long __copy_from_user_inatomic(void *to,
383379
return 0;
384380
}
385381

386-
if (!__builtin_constant_p(n))
387-
check_object_size(to, n, false);
382+
check_object_size(to, n, false);
388383

389384
return __copy_tofrom_user((__force void __user *)to, from, n);
390385
}
@@ -412,8 +407,8 @@ static inline unsigned long __copy_to_user_inatomic(void __user *to,
412407
if (ret == 0)
413408
return 0;
414409
}
415-
if (!__builtin_constant_p(n))
416-
check_object_size(from, n, true);
410+
411+
check_object_size(from, n, true);
417412

418413
return __copy_tofrom_user(to, (__force const void __user *)from, n);
419414
}

‎arch/sparc/include/asm/uaccess_32.h‎

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -249,25 +249,22 @@ unsigned long __copy_user(void __user *to, const void __user *from, unsigned lon
249249
static inline unsigned long copy_to_user(void __user *to, const void *from, unsigned long n)
250250
{
251251
if (n && __access_ok((unsigned long) to, n)) {
252-
if (!__builtin_constant_p(n))
253-
check_object_size(from, n, true);
252+
check_object_size(from, n, true);
254253
return __copy_user(to, (__force void __user *) from, n);
255254
} else
256255
return n;
257256
}
258257

259258
static inline unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n)
260259
{
261-
if (!__builtin_constant_p(n))
262-
check_object_size(from, n, true);
260+
check_object_size(from, n, true);
263261
return __copy_user(to, (__force void __user *) from, n);
264262
}
265263

266264
static inline unsigned long copy_from_user(void *to, const void __user *from, unsigned long n)
267265
{
268266
if (n && __access_ok((unsigned long) from, n)) {
269-
if (!__builtin_constant_p(n))
270-
check_object_size(to, n, false);
267+
check_object_size(to, n, false);
271268
return __copy_user((__force void __user *) to, from, n);
272269
} else
273270
return n;

‎arch/sparc/include/asm/uaccess_64.h‎

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -212,8 +212,7 @@ copy_from_user(void *to, const void __user *from, unsigned long size)
212212
{
213213
unsigned long ret;
214214

215-
if (!__builtin_constant_p(size))
216-
check_object_size(to, size, false);
215+
check_object_size(to, size, false);
217216

218217
ret = ___copy_from_user(to, from, size);
219218
if (unlikely(ret))
@@ -233,8 +232,8 @@ copy_to_user(void __user *to, const void *from, unsigned long size)
233232
{
234233
unsigned long ret;
235234

236-
if (!__builtin_constant_p(size))
237-
check_object_size(from, size, true);
235+
check_object_size(from, size, true);
236+
238237
ret = ___copy_to_user(to, from, size);
239238
if (unlikely(ret))
240239
ret = copy_to_user_fixup(to, from, size);

‎arch/x86/include/asm/uaccess.h‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -705,7 +705,7 @@ static inline void copy_user_overflow(int size, unsigned long count)
705705
WARN(1, "Buffer overflow detected (%d < %lu)!\n", size, count);
706706
}
707707

708-
static inline unsigned long __must_check
708+
static __always_inline unsigned long __must_check
709709
copy_from_user(void *to, const void __user *from, unsigned long n)
710710
{
711711
int sz = __compiletime_object_size(to);
@@ -725,7 +725,7 @@ copy_from_user(void *to, const void __user *from, unsigned long n)
725725
return n;
726726
}
727727

728-
static inline unsigned long __must_check
728+
static __always_inline unsigned long __must_check
729729
copy_to_user(void __user *to, const void *from, unsigned long n)
730730
{
731731
int sz = __compiletime_object_size(from);

‎drivers/misc/lkdtm_usercopy.c‎

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,15 @@
99
#include <linux/uaccess.h>
1010
#include <asm/cacheflush.h>
1111

12-
static size_t cache_size = 1024;
12+
/*
13+
* Many of the tests here end up using const sizes, but those would
14+
* normally be ignored by hardened usercopy, so force the compiler
15+
* into choosing the non-const path to make sure we trigger the
16+
* hardened usercopy checks by added "unconst" to all the const copies,
17+
* and making sure "cache_size" isn't optimized into a const.
18+
*/
19+
static volatile size_t unconst = 0;
20+
static volatile size_t cache_size = 1024;
1321
static struct kmem_cache *bad_cache;
1422

1523
static const unsigned char test_text[] = "This is a test.\n";
@@ -67,14 +75,14 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame)
6775
if (to_user) {
6876
pr_info("attempting good copy_to_user of local stack\n");
6977
if (copy_to_user((void __user *)user_addr, good_stack,
70-
sizeof(good_stack))) {
78+
unconst + sizeof(good_stack))) {
7179
pr_warn("copy_to_user failed unexpectedly?!\n");
7280
goto free_user;
7381
}
7482

7583
pr_info("attempting bad copy_to_user of distant stack\n");
7684
if (copy_to_user((void __user *)user_addr, bad_stack,
77-
sizeof(good_stack))) {
85+
unconst + sizeof(good_stack))) {
7886
pr_warn("copy_to_user failed, but lacked Oops\n");
7987
goto free_user;
8088
}
@@ -88,14 +96,14 @@ static noinline void do_usercopy_stack(bool to_user, bool bad_frame)
8896

8997
pr_info("attempting good copy_from_user of local stack\n");
9098
if (copy_from_user(good_stack, (void __user *)user_addr,
91-
sizeof(good_stack))) {
99+
unconst + sizeof(good_stack))) {
92100
pr_warn("copy_from_user failed unexpectedly?!\n");
93101
goto free_user;
94102
}
95103

96104
pr_info("attempting bad copy_from_user of distant stack\n");
97105
if (copy_from_user(bad_stack, (void __user *)user_addr,
98-
sizeof(good_stack))) {
106+
unconst + sizeof(good_stack))) {
99107
pr_warn("copy_from_user failed, but lacked Oops\n");
100108
goto free_user;
101109
}
@@ -109,7 +117,7 @@ static void do_usercopy_heap_size(bool to_user)
109117
{
110118
unsigned long user_addr;
111119
unsigned char *one, *two;
112-
const size_t size = 1024;
120+
size_t size = unconst + 1024;
113121

114122
one = kmalloc(size, GFP_KERNEL);
115123
two = kmalloc(size, GFP_KERNEL);
@@ -285,13 +293,14 @@ void lkdtm_USERCOPY_KERNEL(void)
285293

286294
pr_info("attempting good copy_to_user from kernel rodata\n");
287295
if (copy_to_user((void __user *)user_addr, test_text,
288-
sizeof(test_text))) {
296+
unconst + sizeof(test_text))) {
289297
pr_warn("copy_to_user failed unexpectedly?!\n");
290298
goto free_user;
291299
}
292300

293301
pr_info("attempting bad copy_to_user from kernel text\n");
294-
if (copy_to_user((void __user *)user_addr, vm_mmap, PAGE_SIZE)) {
302+
if (copy_to_user((void __user *)user_addr, vm_mmap,
303+
unconst + PAGE_SIZE)) {
295304
pr_warn("copy_to_user failed, but lacked Oops\n");
296305
goto free_user;
297306
}

‎include/linux/thread_info.h‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,8 @@ extern void __check_object_size(const void *ptr, unsigned long n,
121121
static inline void check_object_size(const void *ptr, unsigned long n,
122122
bool to_user)
123123
{
124-
__check_object_size(ptr, n, to_user);
124+
if (!__builtin_constant_p(n))
125+
__check_object_size(ptr, n, to_user);
125126
}
126127
#else
127128
static inline void check_object_size(const void *ptr, unsigned long n,

0 commit comments

Comments
 (0)