Skip to content

Commit 439a1bc

Browse files
committed
fortify: Use __builtin_dynamic_object_size() when available
Since the commits starting with c37495d ("slab: add __alloc_size attributes for better bounds checking"), the compilers have runtime allocation size hints available in some places. This was immediately available to CONFIG_UBSAN_BOUNDS, but CONFIG_FORTIFY_SOURCE needed updating to explicitly make use of the hints via the associated __builtin_dynamic_object_size() helper. Detect and use the builtin when it is available, increasing the accuracy of the mitigation. When runtime sizes are not available, __builtin_dynamic_object_size() falls back to __builtin_object_size(), leaving the existing bounds checking unchanged. Additionally update the VMALLOC_LINEAR_OVERFLOW LKDTM test to make the hint invisible, otherwise the architectural defense is not exercised (the buffer overflow is detected in the memset() rather than when it crosses the edge of the allocation). Cc: Arnd Bergmann <[email protected]> Cc: Greg Kroah-Hartman <[email protected]> Cc: Nick Desaulniers <[email protected]> Cc: Nathan Chancellor <[email protected]> Cc: Tom Rix <[email protected]> Cc: [email protected] Cc: [email protected] Reviewed-by: Miguel Ojeda <[email protected]> # include/linux/compiler_attributes.h Signed-off-by: Kees Cook <[email protected]>
1 parent b2ba00c commit 439a1bc

File tree

3 files changed

+13
-0
lines changed

3 files changed

+13
-0
lines changed

drivers/misc/lkdtm/heap.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ static void lkdtm_VMALLOC_LINEAR_OVERFLOW(void)
3131
char *one, *two;
3232

3333
one = vzalloc(PAGE_SIZE);
34+
OPTIMIZER_HIDE_VAR(one);
3435
two = vzalloc(PAGE_SIZE);
3536

3637
pr_info("Attempting vmalloc linear overflow ...\n");

include/linux/compiler_attributes.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,11 @@
297297
*
298298
* clang: https://clang.llvm.org/docs/AttributeReference.html#pass-object-size-pass-dynamic-object-size
299299
*/
300+
#if __has_attribute(__pass_dynamic_object_size__)
301+
# define __pass_dynamic_object_size(type) __attribute__((__pass_dynamic_object_size__(type)))
302+
#else
303+
# define __pass_dynamic_object_size(type)
304+
#endif
300305
#if __has_attribute(__pass_object_size__)
301306
# define __pass_object_size(type) __attribute__((__pass_object_size__(type)))
302307
#else

include/linux/fortify-string.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -90,10 +90,17 @@ extern char *__underlying_strncpy(char *p, const char *q, __kernel_size_t size)
9090
* size, rather than struct size), but there remain some stragglers using
9191
* type 0 that will be converted in the future.
9292
*/
93+
#if __has_builtin(__builtin_dynamic_object_size)
94+
#define POS __pass_dynamic_object_size(1)
95+
#define POS0 __pass_dynamic_object_size(0)
96+
#define __struct_size(p) __builtin_dynamic_object_size(p, 0)
97+
#define __member_size(p) __builtin_dynamic_object_size(p, 1)
98+
#else
9399
#define POS __pass_object_size(1)
94100
#define POS0 __pass_object_size(0)
95101
#define __struct_size(p) __builtin_object_size(p, 0)
96102
#define __member_size(p) __builtin_object_size(p, 1)
103+
#endif
97104

98105
#define __compiletime_lessthan(bounds, length) ( \
99106
__builtin_constant_p((bounds) < (length)) && \

0 commit comments

Comments
 (0)