Skip to content

Commit fa35198

Browse files
committed
fortify: Explicitly check bounds are compile-time constants
In preparation for replacing __builtin_object_size() with __builtin_dynamic_object_size(), all the compile-time size checks need to check that the bounds comparisons are, in fact, known at compile-time. Enforce what was guaranteed with __bos(). In other words, since all uses of __bos() were constant expressions, it was not required to test for this. When these change to __bdos(), they _may_ be constant expressions, and the checks are only valid when the prior condition holds. This results in no binary differences. Cc: [email protected] Link: https://lore.kernel.org/lkml/[email protected] Signed-off-by: Kees Cook <[email protected]>
1 parent 3e17308 commit fa35198

File tree

1 file changed

+30
-19
lines changed

1 file changed

+30
-19
lines changed

include/linux/fortify-string.h

Lines changed: 30 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,11 @@ extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size)
8080
#define POS __pass_object_size(1)
8181
#define POS0 __pass_object_size(0)
8282

83+
#define __compiletime_lessthan(bounds, length) ( \
84+
__builtin_constant_p((bounds) < (length)) && \
85+
(bounds) < (length) \
86+
)
87+
8388
/**
8489
* strncpy - Copy a string to memory with non-guaranteed NUL padding
8590
*
@@ -117,7 +122,7 @@ char *strncpy(char * const POS p, const char *q, __kernel_size_t size)
117122
{
118123
size_t p_size = __builtin_object_size(p, 1);
119124

120-
if (__builtin_constant_p(size) && p_size < size)
125+
if (__compiletime_lessthan(p_size, size))
121126
__write_overflow();
122127
if (p_size < size)
123128
fortify_panic(__func__);
@@ -224,7 +229,7 @@ __FORTIFY_INLINE ssize_t strscpy(char * const POS p, const char * const POS q, s
224229
* If size can be known at compile time and is greater than
225230
* p_size, generate a compile time write overflow error.
226231
*/
227-
if (__builtin_constant_p(size) && size > p_size)
232+
if (__compiletime_lessthan(p_size, size))
228233
__write_overflow();
229234

230235
/*
@@ -281,15 +286,16 @@ __FORTIFY_INLINE void fortify_memset_chk(__kernel_size_t size,
281286
/*
282287
* Length argument is a constant expression, so we
283288
* can perform compile-time bounds checking where
284-
* buffer sizes are known.
289+
* buffer sizes are also known at compile time.
285290
*/
286291

287292
/* Error when size is larger than enclosing struct. */
288-
if (p_size > p_size_field && p_size < size)
293+
if (__compiletime_lessthan(p_size_field, p_size) &&
294+
__compiletime_lessthan(p_size, size))
289295
__write_overflow();
290296

291297
/* Warn when write size is larger than dest field. */
292-
if (p_size_field < size)
298+
if (__compiletime_lessthan(p_size_field, size))
293299
__write_overflow_field(p_size_field, size);
294300
}
295301
/*
@@ -365,25 +371,28 @@ __FORTIFY_INLINE bool fortify_memcpy_chk(__kernel_size_t size,
365371
/*
366372
* Length argument is a constant expression, so we
367373
* can perform compile-time bounds checking where
368-
* buffer sizes are known.
374+
* buffer sizes are also known at compile time.
369375
*/
370376

371377
/* Error when size is larger than enclosing struct. */
372-
if (p_size > p_size_field && p_size < size)
378+
if (__compiletime_lessthan(p_size_field, p_size) &&
379+
__compiletime_lessthan(p_size, size))
373380
__write_overflow();
374-
if (q_size > q_size_field && q_size < size)
381+
if (__compiletime_lessthan(q_size_field, q_size) &&
382+
__compiletime_lessthan(q_size, size))
375383
__read_overflow2();
376384

377385
/* Warn when write size argument larger than dest field. */
378-
if (p_size_field < size)
386+
if (__compiletime_lessthan(p_size_field, size))
379387
__write_overflow_field(p_size_field, size);
380388
/*
381389
* Warn for source field over-read when building with W=1
382390
* or when an over-write happened, so both can be fixed at
383391
* the same time.
384392
*/
385-
if ((IS_ENABLED(KBUILD_EXTRA_WARN1) || p_size_field < size) &&
386-
q_size_field < size)
393+
if ((IS_ENABLED(KBUILD_EXTRA_WARN1) ||
394+
__compiletime_lessthan(p_size_field, size)) &&
395+
__compiletime_lessthan(q_size_field, size))
387396
__read_overflow2_field(q_size_field, size);
388397
}
389398
/*
@@ -494,7 +503,7 @@ __FORTIFY_INLINE void *memscan(void * const POS0 p, int c, __kernel_size_t size)
494503
{
495504
size_t p_size = __builtin_object_size(p, 0);
496505

497-
if (__builtin_constant_p(size) && p_size < size)
506+
if (__compiletime_lessthan(p_size, size))
498507
__read_overflow();
499508
if (p_size < size)
500509
fortify_panic(__func__);
@@ -508,9 +517,9 @@ int memcmp(const void * const POS0 p, const void * const POS0 q, __kernel_size_t
508517
size_t q_size = __builtin_object_size(q, 0);
509518

510519
if (__builtin_constant_p(size)) {
511-
if (p_size < size)
520+
if (__compiletime_lessthan(p_size, size))
512521
__read_overflow();
513-
if (q_size < size)
522+
if (__compiletime_lessthan(q_size, size))
514523
__read_overflow2();
515524
}
516525
if (p_size < size || q_size < size)
@@ -523,7 +532,7 @@ void *memchr(const void * const POS0 p, int c, __kernel_size_t size)
523532
{
524533
size_t p_size = __builtin_object_size(p, 0);
525534

526-
if (__builtin_constant_p(size) && p_size < size)
535+
if (__compiletime_lessthan(p_size, size))
527536
__read_overflow();
528537
if (p_size < size)
529538
fortify_panic(__func__);
@@ -535,7 +544,7 @@ __FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size)
535544
{
536545
size_t p_size = __builtin_object_size(p, 0);
537546

538-
if (__builtin_constant_p(size) && p_size < size)
547+
if (__compiletime_lessthan(p_size, size))
539548
__read_overflow();
540549
if (p_size < size)
541550
fortify_panic(__func__);
@@ -547,7 +556,7 @@ __FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp
547556
{
548557
size_t p_size = __builtin_object_size(p, 0);
549558

550-
if (__builtin_constant_p(size) && p_size < size)
559+
if (__compiletime_lessthan(p_size, size))
551560
__read_overflow();
552561
if (p_size < size)
553562
fortify_panic(__func__);
@@ -563,11 +572,13 @@ char *strcpy(char * const POS p, const char * const POS q)
563572
size_t size;
564573

565574
/* If neither buffer size is known, immediately give up. */
566-
if (p_size == SIZE_MAX && q_size == SIZE_MAX)
575+
if (__builtin_constant_p(p_size) &&
576+
__builtin_constant_p(q_size) &&
577+
p_size == SIZE_MAX && q_size == SIZE_MAX)
567578
return __underlying_strcpy(p, q);
568579
size = strlen(q) + 1;
569580
/* Compile-time check for const size overflow. */
570-
if (__builtin_constant_p(size) && p_size < size)
581+
if (__compiletime_lessthan(p_size, size))
571582
__write_overflow();
572583
/* Run-time check for dynamic size overflow. */
573584
if (p_size < size)

0 commit comments

Comments
 (0)