Skip to content

Commit d0858cb

Browse files
committed
Merge tag 'overflow-v5.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull overflow updates from Kees Cook: "These changes come in roughly two halves: support of Gustavo A. R. Silva's struct_size() work via additional helpers for catching overflow allocation size calculations, and conversions of selftests to KUnit (which includes some tweaks for UML + Clang): - Convert overflow selftest to KUnit - Convert stackinit selftest to KUnit - Implement size_t saturating arithmetic helpers - Allow struct_size() to be used in initializers" * tag 'overflow-v5.18-rc1' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: lib: stackinit: Convert to KUnit um: Allow builds with Clang lib: overflow: Convert to Kunit overflow: Provide constant expression struct_size overflow: Implement size_t saturating arithmetic helpers test_overflow: Regularize test reporting output
2 parents 2142b7f + 02788eb commit d0858cb

9 files changed

Lines changed: 518 additions & 458 deletions

File tree

Documentation/process/deprecated.rst

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,9 @@ Instead, the 2-factor form of the allocator should be used::
7171

7272
foo = kmalloc_array(count, size, GFP_KERNEL);
7373

74+
Specifically, kmalloc() can be replaced with kmalloc_array(), and
75+
kzalloc() can be replaced with kcalloc().
76+
7477
If no 2-factor form is available, the saturate-on-overflow helpers should
7578
be used::
7679

@@ -91,9 +94,20 @@ Instead, use the helper::
9194
array usage and switch to a `flexible array member
9295
<#zero-length-and-one-element-arrays>`_ instead.
9396

94-
See array_size(), array3_size(), and struct_size(),
95-
for more details as well as the related check_add_overflow() and
96-
check_mul_overflow() family of functions.
97+
For other calculations, please compose the use of the size_mul(),
98+
size_add(), and size_sub() helpers. For example, in the case of::
99+
100+
foo = krealloc(current_size + chunk_size * (count - 3), GFP_KERNEL);
101+
102+
Instead, use the helpers::
103+
104+
foo = krealloc(size_add(current_size,
105+
size_mul(chunk_size,
106+
size_sub(count, 3))), GFP_KERNEL);
107+
108+
For more details, also see array3_size() and flex_array_size(),
109+
as well as the related check_mul_overflow(), check_add_overflow(),
110+
check_sub_overflow(), and check_shl_overflow() family of functions.
97111

98112
simple_strtol(), simple_strtoll(), simple_strtoul(), simple_strtoull()
99113
----------------------------------------------------------------------

arch/um/os-Linux/execvp.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,7 @@ int execvp_noalloc(char *buf, const char *file, char *const argv[])
9393
up finding no executable we can use, we want to diagnose
9494
that we did find one but were denied access. */
9595
got_eacces = 1;
96+
break;
9697
case ENOENT:
9798
case ESTALE:
9899
case ENOTDIR:

arch/x86/um/user-offsets.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,11 @@
88
#define __FRAME_OFFSETS
99
#include <linux/ptrace.h>
1010
#include <asm/types.h>
11+
#include <linux/kbuild.h>
1112

12-
#define DEFINE(sym, val) \
13-
asm volatile("\n->" #sym " %0 " #val : : "i" (val))
14-
15-
#define DEFINE_LONGS(sym, val) \
16-
asm volatile("\n->" #sym " %0 " #val : : "i" (val/sizeof(unsigned long)))
13+
#define DEFINE_LONGS(sym, val) \
14+
COMMENT(#val " / sizeof(unsigned long)"); \
15+
DEFINE(sym, val / sizeof(unsigned long))
1716

1817
void foo(void)
1918
{

include/linux/overflow.h

Lines changed: 73 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include <linux/compiler.h>
66
#include <linux/limits.h>
7+
#include <linux/const.h>
78

89
/*
910
* We need to compute the minimum and maximum values representable in a given
@@ -118,81 +119,94 @@ static inline bool __must_check __must_check_overflow(bool overflow)
118119
}))
119120

120121
/**
121-
* array_size() - Calculate size of 2-dimensional array.
122-
*
123-
* @a: dimension one
124-
* @b: dimension two
122+
* size_mul() - Calculate size_t multiplication with saturation at SIZE_MAX
125123
*
126-
* Calculates size of 2-dimensional array: @a * @b.
124+
* @factor1: first factor
125+
* @factor2: second factor
127126
*
128-
* Returns: number of bytes needed to represent the array or SIZE_MAX on
129-
* overflow.
127+
* Returns: calculate @factor1 * @factor2, both promoted to size_t,
128+
* with any overflow causing the return value to be SIZE_MAX. The
129+
* lvalue must be size_t to avoid implicit type conversion.
130130
*/
131-
static inline __must_check size_t array_size(size_t a, size_t b)
131+
static inline size_t __must_check size_mul(size_t factor1, size_t factor2)
132132
{
133133
size_t bytes;
134134

135-
if (check_mul_overflow(a, b, &bytes))
135+
if (check_mul_overflow(factor1, factor2, &bytes))
136136
return SIZE_MAX;
137137

138138
return bytes;
139139
}
140140

141141
/**
142-
* array3_size() - Calculate size of 3-dimensional array.
142+
* size_add() - Calculate size_t addition with saturation at SIZE_MAX
143143
*
144-
* @a: dimension one
145-
* @b: dimension two
146-
* @c: dimension three
147-
*
148-
* Calculates size of 3-dimensional array: @a * @b * @c.
144+
* @addend1: first addend
145+
* @addend2: second addend
149146
*
150-
* Returns: number of bytes needed to represent the array or SIZE_MAX on
151-
* overflow.
147+
* Returns: calculate @addend1 + @addend2, both promoted to size_t,
148+
* with any overflow causing the return value to be SIZE_MAX. The
149+
* lvalue must be size_t to avoid implicit type conversion.
152150
*/
153-
static inline __must_check size_t array3_size(size_t a, size_t b, size_t c)
151+
static inline size_t __must_check size_add(size_t addend1, size_t addend2)
154152
{
155153
size_t bytes;
156154

157-
if (check_mul_overflow(a, b, &bytes))
158-
return SIZE_MAX;
159-
if (check_mul_overflow(bytes, c, &bytes))
155+
if (check_add_overflow(addend1, addend2, &bytes))
160156
return SIZE_MAX;
161157

162158
return bytes;
163159
}
164160

165-
/*
166-
* Compute a*b+c, returning SIZE_MAX on overflow. Internal helper for
167-
* struct_size() below.
161+
/**
162+
* size_sub() - Calculate size_t subtraction with saturation at SIZE_MAX
163+
*
164+
* @minuend: value to subtract from
165+
* @subtrahend: value to subtract from @minuend
166+
*
167+
* Returns: calculate @minuend - @subtrahend, both promoted to size_t,
168+
* with any overflow causing the return value to be SIZE_MAX. For
169+
* composition with the size_add() and size_mul() helpers, neither
170+
* argument may be SIZE_MAX (or the result with be forced to SIZE_MAX).
171+
* The lvalue must be size_t to avoid implicit type conversion.
168172
*/
169-
static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
173+
static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
170174
{
171175
size_t bytes;
172176

173-
if (check_mul_overflow(a, b, &bytes))
174-
return SIZE_MAX;
175-
if (check_add_overflow(bytes, c, &bytes))
177+
if (minuend == SIZE_MAX || subtrahend == SIZE_MAX ||
178+
check_sub_overflow(minuend, subtrahend, &bytes))
176179
return SIZE_MAX;
177180

178181
return bytes;
179182
}
180183

181184
/**
182-
* struct_size() - Calculate size of structure with trailing array.
183-
* @p: Pointer to the structure.
184-
* @member: Name of the array member.
185-
* @count: Number of elements in the array.
185+
* array_size() - Calculate size of 2-dimensional array.
186186
*
187-
* Calculates size of memory needed for structure @p followed by an
188-
* array of @count number of @member elements.
187+
* @a: dimension one
188+
* @b: dimension two
189189
*
190-
* Return: number of bytes needed or SIZE_MAX on overflow.
190+
* Calculates size of 2-dimensional array: @a * @b.
191+
*
192+
* Returns: number of bytes needed to represent the array or SIZE_MAX on
193+
* overflow.
191194
*/
192-
#define struct_size(p, member, count) \
193-
__ab_c_size(count, \
194-
sizeof(*(p)->member) + __must_be_array((p)->member),\
195-
sizeof(*(p)))
195+
#define array_size(a, b) size_mul(a, b)
196+
197+
/**
198+
* array3_size() - Calculate size of 3-dimensional array.
199+
*
200+
* @a: dimension one
201+
* @b: dimension two
202+
* @c: dimension three
203+
*
204+
* Calculates size of 3-dimensional array: @a * @b * @c.
205+
*
206+
* Returns: number of bytes needed to represent the array or SIZE_MAX on
207+
* overflow.
208+
*/
209+
#define array3_size(a, b, c) size_mul(size_mul(a, b), c)
196210

197211
/**
198212
* flex_array_size() - Calculate size of a flexible array member
@@ -208,7 +222,25 @@ static inline __must_check size_t __ab_c_size(size_t a, size_t b, size_t c)
208222
* Return: number of bytes needed or SIZE_MAX on overflow.
209223
*/
210224
#define flex_array_size(p, member, count) \
211-
array_size(count, \
212-
sizeof(*(p)->member) + __must_be_array((p)->member))
225+
__builtin_choose_expr(__is_constexpr(count), \
226+
(count) * sizeof(*(p)->member) + __must_be_array((p)->member), \
227+
size_mul(count, sizeof(*(p)->member) + __must_be_array((p)->member)))
228+
229+
/**
230+
* struct_size() - Calculate size of structure with trailing flexible array.
231+
*
232+
* @p: Pointer to the structure.
233+
* @member: Name of the array member.
234+
* @count: Number of elements in the array.
235+
*
236+
* Calculates size of memory needed for structure @p followed by an
237+
* array of @count number of @member elements.
238+
*
239+
* Return: number of bytes needed or SIZE_MAX on overflow.
240+
*/
241+
#define struct_size(p, member, count) \
242+
__builtin_choose_expr(__is_constexpr(count), \
243+
sizeof(*(p)) + flex_array_size(p, member, count), \
244+
size_add(sizeof(*(p)), flex_array_size(p, member, count)))
213245

214246
#endif /* __LINUX_OVERFLOW_H */

lib/Kconfig.debug

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -2214,9 +2214,6 @@ config TEST_UUID
22142214
config TEST_XARRAY
22152215
tristate "Test the XArray code at runtime"
22162216

2217-
config TEST_OVERFLOW
2218-
tristate "Test check_*_overflow() functions at runtime"
2219-
22202217
config TEST_RHASHTABLE
22212218
tristate "Perform selftest on resizable hash table"
22222219
help
@@ -2501,6 +2498,30 @@ config MEMCPY_KUNIT_TEST
25012498

25022499
If unsure, say N.
25032500

2501+
config OVERFLOW_KUNIT_TEST
2502+
tristate "Test check_*_overflow() functions at runtime" if !KUNIT_ALL_TESTS
2503+
depends on KUNIT
2504+
default KUNIT_ALL_TESTS
2505+
help
2506+
Builds unit tests for the check_*_overflow(), size_*(), allocation, and
2507+
related functions.
2508+
2509+
For more information on KUnit and unit tests in general please refer
2510+
to the KUnit documentation in Documentation/dev-tools/kunit/.
2511+
2512+
If unsure, say N.
2513+
2514+
config STACKINIT_KUNIT_TEST
2515+
tristate "Test level of stack variable initialization" if !KUNIT_ALL_TESTS
2516+
depends on KUNIT
2517+
default KUNIT_ALL_TESTS
2518+
help
2519+
Test if the kernel is zero-initializing stack variables and
2520+
padding. Coverage is controlled by compiler flags,
2521+
CONFIG_INIT_STACK_ALL_PATTERN, CONFIG_INIT_STACK_ALL_ZERO,
2522+
CONFIG_GCC_PLUGIN_STRUCTLEAK, CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF,
2523+
or CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL.
2524+
25042525
config TEST_UDELAY
25052526
tristate "udelay test driver"
25062527
help
@@ -2592,17 +2613,6 @@ config TEST_OBJAGG
25922613
Enable this option to test object aggregation manager on boot
25932614
(or module load).
25942615

2595-
2596-
config TEST_STACKINIT
2597-
tristate "Test level of stack variable initialization"
2598-
help
2599-
Test if the kernel is zero-initializing stack variables and
2600-
padding. Coverage is controlled by compiler flags,
2601-
CONFIG_GCC_PLUGIN_STRUCTLEAK, CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF,
2602-
or CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL.
2603-
2604-
If unsure, say N.
2605-
26062616
config TEST_MEMINIT
26072617
tristate "Test heap/page initialization"
26082618
help

lib/Makefile

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ obj-$(CONFIG_TEST_LIST_SORT) += test_list_sort.o
7777
obj-$(CONFIG_TEST_MIN_HEAP) += test_min_heap.o
7878
obj-$(CONFIG_TEST_LKM) += test_module.o
7979
obj-$(CONFIG_TEST_VMALLOC) += test_vmalloc.o
80-
obj-$(CONFIG_TEST_OVERFLOW) += test_overflow.o
8180
obj-$(CONFIG_TEST_RHASHTABLE) += test_rhashtable.o
8281
obj-$(CONFIG_TEST_SORT) += test_sort.o
8382
obj-$(CONFIG_TEST_USER_COPY) += test_user_copy.o
@@ -94,8 +93,6 @@ obj-$(CONFIG_TEST_KMOD) += test_kmod.o
9493
obj-$(CONFIG_TEST_DEBUG_VIRTUAL) += test_debug_virtual.o
9594
obj-$(CONFIG_TEST_MEMCAT_P) += test_memcat_p.o
9695
obj-$(CONFIG_TEST_OBJAGG) += test_objagg.o
97-
CFLAGS_test_stackinit.o += $(call cc-disable-warning, switch-unreachable)
98-
obj-$(CONFIG_TEST_STACKINIT) += test_stackinit.o
9996
obj-$(CONFIG_TEST_BLACKHOLE_DEV) += test_blackhole_dev.o
10097
obj-$(CONFIG_TEST_MEMINIT) += test_meminit.o
10198
obj-$(CONFIG_TEST_LOCKUP) += test_lockup.o
@@ -363,6 +360,9 @@ obj-$(CONFIG_BITS_TEST) += test_bits.o
363360
obj-$(CONFIG_CMDLINE_KUNIT_TEST) += cmdline_kunit.o
364361
obj-$(CONFIG_SLUB_KUNIT_TEST) += slub_kunit.o
365362
obj-$(CONFIG_MEMCPY_KUNIT_TEST) += memcpy_kunit.o
363+
obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o
364+
CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable)
365+
obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o
366366

367367
obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o
368368

0 commit comments

Comments
 (0)