overflow: Implement size_t saturating arithmetic helpers
Subsystems (4)
v2 cover overflow: Implement size_t saturating arithmetic helpers
Hi,
While doing more array_size() scans on the kernel, and reviewing recent[1] struct_size() work[2], it became clear we needed helpers to perform composed saturating adds and multiplies. This creates those helpers and updates the self tests to check them.
v1: https://lore.kernel.org/lkml/[email protected]/
v2:
- use static inlines instead of macros (Rasmus)
- undef local helpers defs in selftest (Rasmus)
- Make sure to keep saturation for size_sub() arguments
- update selftests for size_sub() behavior
- Add reviews
Thanks,
-Kees
[1] https://lore.kernel.org/linux-hardening/202201241213.82E7D9F598@keescook/
[2] https://lore.kernel.org/lkml/?q=%22open-coded+arithmetic%22
Kees Cook (2):
test_overflow: Regularize test reporting output
overflow: Implement size_t saturating arithmetic helpers
Documentation/process/deprecated.rst | 20 +++- include/linux/overflow.h | 110 +++++++++++-------- lib/test_overflow.c | 152 ++++++++++++++++++++++----- 3 files changed, 214 insertions(+), 68 deletions(-)
Signature · 1 line
2.30.2
PATCH v2 1/2 test_overflow: Regularize test reporting output
Report test run summaries more regularly, so it's easier to understand the output:
- Remove noisy "ok" reports for shift and allocator tests.
- Reorganize per-type output to the end of each type's tests.
- Replace redundant vmalloc tests with __vmalloc so that __GFP_NO_WARN can be used to keep the expected failure warnings out of dmesg, similar to commit 8e060c21ae2c ("lib/test_overflow.c: avoid tainting the kernel and fix wrap size")
Resulting output:
test_overflow: 18 u8 arithmetic tests finished
test_overflow: 19 s8 arithmetic tests finished
test_overflow: 17 u16 arithmetic tests finished
test_overflow: 17 s16 arithmetic tests finished
test_overflow: 17 u32 arithmetic tests finished
test_overflow: 17 s32 arithmetic tests finished
test_overflow: 17 u64 arithmetic tests finished
test_overflow: 21 s64 arithmetic tests finished
test_overflow: 113 shift tests finished
test_overflow: 17 overflow size helper tests finished
test_overflow: 11 allocation overflow tests finished
test_overflow: all tests passed
--- lib/test_overflow.c | 54 +++++++++++++++++++++++++-------------------- 1 file changed, 30 insertions(+), 24 deletions(-)
lib/test_overflow.c +30 -24
@@ -252,10 +252,10 @@ static int __init test_ ## t ## _overflow(void) { \int err =0 ; \unsigned i; \ \ - pr_info("%-3s: %zu arithmetic tests\n", #t, \ - ARRAY_SIZE(t ## _tests)); \ for (i = 0; i < ARRAY_SIZE(t ## _tests); ++i) \ err |= do_test_ ## t(&t ## _tests[i]); \ + pr_info("%zu %s arithmetic tests finished\n", \ + ARRAY_SIZE(t ## _tests), #t); \ return err; \ }
@@ -291,6 +291,7 @@ static int __init test_overflow_calculation(void)static int __init test_overflow_shift (void ) {int err =0 ; +int count =0 ;/* Args are: value, shift, type, expected result, overflow expected */ #define TEST_ONE_SHIFT (a, s, t, expect, of) ({ \
@@ -313,9 +314,7 @@ static int __init test_overflow_shift(void)pr_warn ("got %llu\n" , (u64 )__d); \ __failed =1 ; \ } \ - if (!__failed) \ - pr_info("ok: (%s)(%s << %s) == %s\n", #t, #a, #s, \ - of ? "overflow" : #expect); \ + count++; \ __failed; \ })
@@ -479,6 +478,10 @@ static int __init test_overflow_shift(void) err |=TEST_ONE_SHIFT (0 ,31 , s32,0 , false); err |=TEST_ONE_SHIFT (0 ,63 , s64,0 , false); +pr_info ("%d shift tests finished\n" , count); + +#undef TEST_ONE_SHIFT +return err; }
@@ -530,7 +533,6 @@ static int __init test_ ## func (void *arg) \ free ## want_arg (free_func, arg, ptr); \ return 1; \ } \ - pr_info(#func " detected saturation\n"); \ return 0; \ }
@@ -544,10 +546,7 @@ DEFINE_TEST_ALLOC(kmalloc, kfree, 0, 1, 0);DEFINE_TEST_ALLOC (kmalloc_node, kfree,0 ,1 ,1 );DEFINE_TEST_ALLOC (kzalloc, kfree,0 ,1 ,0 );DEFINE_TEST_ALLOC (kzalloc_node, kfree,0 ,1 ,1 ); -DEFINE_TEST_ALLOC (vmalloc, vfree,0 ,0 ,0 ); -DEFINE_TEST_ALLOC (vmalloc_node, vfree,0 ,0 ,1 ); -DEFINE_TEST_ALLOC (vzalloc, vfree,0 ,0 ,0 ); -DEFINE_TEST_ALLOC (vzalloc_node, vfree,0 ,0 ,1 ); +DEFINE_TEST_ALLOC (__vmalloc, vfree,0 ,1 ,0 );DEFINE_TEST_ALLOC (kvmalloc, kvfree,0 ,1 ,0 );DEFINE_TEST_ALLOC (kvmalloc_node, kvfree,0 ,1 ,1 );DEFINE_TEST_ALLOC (kvzalloc, kvfree,0 ,1 ,0 );
@@ -559,8 +558,14 @@ static int __init test_overflow_allocation(void) {const char device_name[] ="overflow-test" ;struct device *dev; +int count =0 ;int err =0 ; +#define check_allocation_overflow (alloc) ({ \ + count++; \ + test_ ## alloc(dev); \ +}) +/* Create dummy device for devm_kmalloc()-family tests. */ dev =root_device_register (device_name);if (IS_ERR (dev)) {
@@ -568,23 +573,24 @@ static int __init test_overflow_allocation(void)return 1 ; } - err |=test_kmalloc (NULL ); - err |=test_kmalloc_node (NULL ); - err |=test_kzalloc (NULL ); - err |=test_kzalloc_node (NULL ); - err |=test_kvmalloc (NULL ); - err |=test_kvmalloc_node (NULL ); - err |=test_kvzalloc (NULL ); - err |=test_kvzalloc_node (NULL ); - err |=test_vmalloc (NULL ); - err |=test_vmalloc_node (NULL ); - err |=test_vzalloc (NULL ); - err |=test_vzalloc_node (NULL ); - err |=test_devm_kmalloc (dev); - err |=test_devm_kzalloc (dev); + err |=check_allocation_overflow (kmalloc); + err |=check_allocation_overflow (kmalloc_node); + err |=check_allocation_overflow (kzalloc); + err |=check_allocation_overflow (kzalloc_node); + err |=check_allocation_overflow (__vmalloc); + err |=check_allocation_overflow (kvmalloc); + err |=check_allocation_overflow (kvmalloc_node); + err |=check_allocation_overflow (kvzalloc); + err |=check_allocation_overflow (kvzalloc_node); + err |=check_allocation_overflow (devm_kmalloc); + err |=check_allocation_overflow (devm_kzalloc);device_unregister (dev); +pr_info ("%d allocation overflow tests finished\n" , count); + +#undef check_allocation_overflow +return err; }
Signature · 1 line
2.30.2
PATCH v2 2/2 overflow: Implement size_t saturating arithmetic helpers
In reply to Kees CookIn order to perform more open-coded replacements of common allocation size arithmetic, the kernel needs saturating (SIZE_MAX) helpers for multiplication, addition, and subtraction. For example, it is common in allocators, especially on realloc, to add to an existing size:
p = krealloc(map->patch,
sizeof(struct reg_sequence) * (map->patch_regs + num_regs),
GFP_KERNEL);There is no existing saturating replacement for this calculation, and just leaving the addition open coded inside array_size() could potentially overflow as well. For example, an overflow in an expression for a size_t argument might wrap to zero:
array_size(anything, something_at_size_max + 1) == 0
Introduce size_mul(), size_add(), and size_sub() helpers that implicitly promote arguments to size_t and saturated calculations for use in allocations. With these helpers it is also possible to redefine array_size(), array3_size(), flex_array_size(), and struct_size() in terms of the new helpers.
As with the check_*_overflow() helpers, the new helpers use __must_check, though what is really desired is a way to make sure that assignment is only to a size_t lvalue. Without this, it's still possible to introduce overflow/underflow via type conversion (i.e. from size_t to int). Enforcing this will currently need to be left to static analysis or future use of -Wconversion.
--- Documentation/process/deprecated.rst | 20 ++++- include/linux/overflow.h | 110 +++++++++++++++++---------- lib/test_overflow.c | 98 ++++++++++++++++++++++++ 3 files changed, 184 insertions(+), 44 deletions(-)
Documentation/process/deprecated.rst +17 -3
@@ -71,6 +71,9 @@ Instead, the 2-factor form of the allocator should be used:: foo = kmalloc_array(count, size, GFP_KERNEL); +Specifically, kmalloc() can be replaced with kmalloc_array(), and +kzalloc() can be replaced with kcalloc(). + If no 2-factor form is available, the saturate-on-overflow helpers should be used::
@@ -91,9 +94,20 @@ Instead, use the helper:: array usage and switch to a `flexible array member <#zero-length-and-one-element-arrays>`_ instead. -See array_size(), array3_size(), and struct_size(), -for more details as well as the related check_add_overflow() and -check_mul_overflow() family of functions. +For other calculations, please compose the use of the size_mul(), +size_add(), and size_sub() helpers. For example, in the case of:: + + foo = krealloc(current_size + chunk_size * (count - 3), GFP_KERNEL); + +Instead, use the helpers:: + + foo = krealloc(size_add(current_size, + size_mul(chunk_size, + size_sub(count, 3))), GFP_KERNEL); + +For more details, also see array3_size() and flex_array_size(), +as well as the related check_mul_overflow(), check_add_overflow(), +check_sub_overflow(), and check_shl_overflow() family of functions. simple_strtol(), simple_strtoll(), simple_strtoul(), simple_strtoull() ----------------------------------------------------------------------
include/linux/overflow.h +69 -41
157 lines not on this page · raw message
lib/test_overflow.c +98 -0
110 lines not on this page · raw message
Signature · 1 line
2.30.2