Skip to content

Commit 1b4b2f1

Browse files
keesAlex Shi
authored andcommitted
usercopy: fold builtin_const check into inline function
Instead of having each caller of check_object_size() need to remember to check for a const size parameter, move the check into check_object_size() itself. This actually matches the original implementation in PaX, though this commit cleans up the now-redundant builtin_const() calls in the various architectures. Signed-off-by: Kees Cook <keescook@chromium.org> (cherry picked from commit 81409e9e28058811c9ea865345e1753f8f677e44) Signed-off-by: Alex Shi <alex.shi@linaro.org>
1 parent ed67fb8 commit 1b4b2f1

5 files changed

Lines changed: 19 additions & 31 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
@@ -326,14 +326,12 @@ static inline unsigned long copy_from_user(void *to,
326326
unsigned long over;
327327

328328
if (access_ok(VERIFY_READ, from, n)) {
329-
if (!__builtin_constant_p(n))
330-
check_object_size(to, n, false);
329+
check_object_size(to, n, false);
331330
return __copy_tofrom_user((__force void __user *)to, from, n);
332331
}
333332
if ((unsigned long)from < TASK_SIZE) {
334333
over = (unsigned long)from + n - TASK_SIZE;
335-
if (!__builtin_constant_p(n - over))
336-
check_object_size(to, n - over, false);
334+
check_object_size(to, n - over, false);
337335
return __copy_tofrom_user((__force void __user *)to, from,
338336
n - over) + over;
339337
}
@@ -346,14 +344,12 @@ static inline unsigned long copy_to_user(void __user *to,
346344
unsigned long over;
347345

348346
if (access_ok(VERIFY_WRITE, to, n)) {
349-
if (!__builtin_constant_p(n))
350-
check_object_size(from, n, true);
347+
check_object_size(from, n, true);
351348
return __copy_tofrom_user(to, (__force void __user *)from, n);
352349
}
353350
if ((unsigned long)to < TASK_SIZE) {
354351
over = (unsigned long)to + n - TASK_SIZE;
355-
if (!__builtin_constant_p(n))
356-
check_object_size(from, n - over, true);
352+
check_object_size(from, n - over, true);
357353
return __copy_tofrom_user(to, (__force void __user *)from,
358354
n - over) + over;
359355
}
@@ -398,8 +394,7 @@ static inline unsigned long __copy_from_user_inatomic(void *to,
398394
return 0;
399395
}
400396

401-
if (!__builtin_constant_p(n))
402-
check_object_size(to, n, false);
397+
check_object_size(to, n, false);
403398

404399
return __copy_tofrom_user((__force void __user *)to, from, n);
405400
}
@@ -427,8 +422,8 @@ static inline unsigned long __copy_to_user_inatomic(void __user *to,
427422
if (ret == 0)
428423
return 0;
429424
}
430-
if (!__builtin_constant_p(n))
431-
check_object_size(from, n, true);
425+
426+
check_object_size(from, n, true);
432427

433428
return __copy_tofrom_user(to, (__force const void __user *)from, n);
434429
}

arch/sparc/include/asm/uaccess_32.h

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -314,25 +314,22 @@ unsigned long __copy_user(void __user *to, const void __user *from, unsigned lon
314314
static inline unsigned long copy_to_user(void __user *to, const void *from, unsigned long n)
315315
{
316316
if (n && __access_ok((unsigned long) to, n)) {
317-
if (!__builtin_constant_p(n))
318-
check_object_size(from, n, true);
317+
check_object_size(from, n, true);
319318
return __copy_user(to, (__force void __user *) from, n);
320319
} else
321320
return n;
322321
}
323322

324323
static inline unsigned long __copy_to_user(void __user *to, const void *from, unsigned long n)
325324
{
326-
if (!__builtin_constant_p(n))
327-
check_object_size(from, n, true);
325+
check_object_size(from, n, true);
328326
return __copy_user(to, (__force void __user *) from, n);
329327
}
330328

331329
static inline unsigned long copy_from_user(void *to, const void __user *from, unsigned long n)
332330
{
333331
if (n && __access_ok((unsigned long) from, n)) {
334-
if (!__builtin_constant_p(n))
335-
check_object_size(to, n, false);
332+
check_object_size(to, n, false);
336333
return __copy_user((__force void __user *) to, from, n);
337334
} else
338335
return n;

arch/sparc/include/asm/uaccess_64.h

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

255-
if (!__builtin_constant_p(size))
256-
check_object_size(to, size, false);
255+
check_object_size(to, size, false);
257256

258257
ret = ___copy_from_user(to, from, size);
259258
if (unlikely(ret))
@@ -273,8 +272,8 @@ copy_to_user(void __user *to, const void *from, unsigned long size)
273272
{
274273
unsigned long ret;
275274

276-
if (!__builtin_constant_p(size))
277-
check_object_size(from, size, true);
275+
check_object_size(from, size, true);
276+
278277
ret = ___copy_to_user(to, from, size);
279278
if (unlikely(ret))
280279
ret = copy_to_user_fixup(to, from, size);

include/linux/thread_info.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,8 @@ extern void __check_object_size(const void *ptr, unsigned long n,
161161
static inline void check_object_size(const void *ptr, unsigned long n,
162162
bool to_user)
163163
{
164-
__check_object_size(ptr, n, to_user);
164+
if (!__builtin_constant_p(n))
165+
__check_object_size(ptr, n, to_user);
165166
}
166167
#else
167168
static inline void check_object_size(const void *ptr, unsigned long n,

0 commit comments

Comments
 (0)