Skip to content

Commit 2eb72d8

Browse files
committed
Merge tag 'hardening-v6.1-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull hardening fixes from Kees Cook: - Fix older Clang vs recent overflow KUnit test additions (Nick Desaulniers, Kees Cook) - Fix kern-doc visibility for overflow helpers (Kees Cook) * tag 'hardening-v6.1-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: overflow: Refactor test skips for Clang-specific issues overflow: disable failing tests for older clang versions overflow: Fix kern-doc markup for functions
2 parents 7f9a7cd + 72c3ebe commit 2eb72d8

File tree

4 files changed

+58
-36
lines changed

4 files changed

+58
-36
lines changed

Documentation/core-api/kernel-api.rst

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ Text Searching
118118
CRC and Math Functions in Linux
119119
===============================
120120

121+
Arithmetic Overflow Checking
122+
----------------------------
123+
124+
.. kernel-doc:: include/linux/overflow.h
125+
:internal:
126+
121127
CRC Functions
122128
-------------
123129

Documentation/driver-api/basics.rst

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -107,9 +107,6 @@ Kernel utility functions
107107
.. kernel-doc:: kernel/panic.c
108108
:export:
109109

110-
.. kernel-doc:: include/linux/overflow.h
111-
:internal:
112-
113110
Device Resource Management
114111
--------------------------
115112

include/linux/overflow.h

Lines changed: 16 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,8 @@ static inline bool __must_check __must_check_overflow(bool overflow)
5151
return unlikely(overflow);
5252
}
5353

54-
/** check_add_overflow() - Calculate addition with overflow checking
55-
*
54+
/**
55+
* check_add_overflow() - Calculate addition with overflow checking
5656
* @a: first addend
5757
* @b: second addend
5858
* @d: pointer to store sum
@@ -66,8 +66,8 @@ static inline bool __must_check __must_check_overflow(bool overflow)
6666
#define check_add_overflow(a, b, d) \
6767
__must_check_overflow(__builtin_add_overflow(a, b, d))
6868

69-
/** check_sub_overflow() - Calculate subtraction with overflow checking
70-
*
69+
/**
70+
* check_sub_overflow() - Calculate subtraction with overflow checking
7171
* @a: minuend; value to subtract from
7272
* @b: subtrahend; value to subtract from @a
7373
* @d: pointer to store difference
@@ -81,8 +81,8 @@ static inline bool __must_check __must_check_overflow(bool overflow)
8181
#define check_sub_overflow(a, b, d) \
8282
__must_check_overflow(__builtin_sub_overflow(a, b, d))
8383

84-
/** check_mul_overflow() - Calculate multiplication with overflow checking
85-
*
84+
/**
85+
* check_mul_overflow() - Calculate multiplication with overflow checking
8686
* @a: first factor
8787
* @b: second factor
8888
* @d: pointer to store product
@@ -96,23 +96,24 @@ static inline bool __must_check __must_check_overflow(bool overflow)
9696
#define check_mul_overflow(a, b, d) \
9797
__must_check_overflow(__builtin_mul_overflow(a, b, d))
9898

99-
/** check_shl_overflow() - Calculate a left-shifted value and check overflow
100-
*
99+
/**
100+
* check_shl_overflow() - Calculate a left-shifted value and check overflow
101101
* @a: Value to be shifted
102102
* @s: How many bits left to shift
103103
* @d: Pointer to where to store the result
104104
*
105105
* Computes *@d = (@a << @s)
106106
*
107-
* Returns true if '*d' cannot hold the result or when 'a << s' doesn't
107+
* Returns true if '*@d' cannot hold the result or when '@a << @s' doesn't
108108
* make sense. Example conditions:
109-
* - 'a << s' causes bits to be lost when stored in *d.
110-
* - 's' is garbage (e.g. negative) or so large that the result of
111-
* 'a << s' is guaranteed to be 0.
112-
* - 'a' is negative.
113-
* - 'a << s' sets the sign bit, if any, in '*d'.
114109
*
115-
* '*d' will hold the results of the attempted shift, but is not
110+
* - '@a << @s' causes bits to be lost when stored in *@d.
111+
* - '@s' is garbage (e.g. negative) or so large that the result of
112+
* '@a << @s' is guaranteed to be 0.
113+
* - '@a' is negative.
114+
* - '@a << @s' sets the sign bit, if any, in '*@d'.
115+
*
116+
* '*@d' will hold the results of the attempted shift, but is not
116117
* considered "safe for use" if true is returned.
117118
*/
118119
#define check_shl_overflow(a, s, d) __must_check_overflow(({ \
@@ -129,7 +130,6 @@ static inline bool __must_check __must_check_overflow(bool overflow)
129130

130131
/**
131132
* size_mul() - Calculate size_t multiplication with saturation at SIZE_MAX
132-
*
133133
* @factor1: first factor
134134
* @factor2: second factor
135135
*
@@ -149,7 +149,6 @@ static inline size_t __must_check size_mul(size_t factor1, size_t factor2)
149149

150150
/**
151151
* size_add() - Calculate size_t addition with saturation at SIZE_MAX
152-
*
153152
* @addend1: first addend
154153
* @addend2: second addend
155154
*
@@ -169,7 +168,6 @@ static inline size_t __must_check size_add(size_t addend1, size_t addend2)
169168

170169
/**
171170
* size_sub() - Calculate size_t subtraction with saturation at SIZE_MAX
172-
*
173171
* @minuend: value to subtract from
174172
* @subtrahend: value to subtract from @minuend
175173
*
@@ -192,7 +190,6 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
192190

193191
/**
194192
* array_size() - Calculate size of 2-dimensional array.
195-
*
196193
* @a: dimension one
197194
* @b: dimension two
198195
*
@@ -205,7 +202,6 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
205202

206203
/**
207204
* array3_size() - Calculate size of 3-dimensional array.
208-
*
209205
* @a: dimension one
210206
* @b: dimension two
211207
* @c: dimension three
@@ -220,7 +216,6 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
220216
/**
221217
* flex_array_size() - Calculate size of a flexible array member
222218
* within an enclosing structure.
223-
*
224219
* @p: Pointer to the structure.
225220
* @member: Name of the flexible array member.
226221
* @count: Number of elements in the array.
@@ -237,7 +232,6 @@ static inline size_t __must_check size_sub(size_t minuend, size_t subtrahend)
237232

238233
/**
239234
* struct_size() - Calculate size of structure with trailing flexible array.
240-
*
241235
* @p: Pointer to the structure.
242236
* @member: Name of the array member.
243237
* @count: Number of elements in the array.

lib/overflow_kunit.c

Lines changed: 36 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,34 @@
1616
#include <linux/types.h>
1717
#include <linux/vmalloc.h>
1818

19+
#define SKIP(cond, reason) do { \
20+
if (cond) { \
21+
kunit_skip(test, reason); \
22+
return; \
23+
} \
24+
} while (0)
25+
26+
/*
27+
* Clang 11 and earlier generate unwanted libcalls for signed output
28+
* on unsigned input.
29+
*/
30+
#if defined(CONFIG_CC_IS_CLANG) && __clang_major__ <= 11
31+
# define SKIP_SIGN_MISMATCH(t) SKIP(t, "Clang 11 unwanted libcalls")
32+
#else
33+
# define SKIP_SIGN_MISMATCH(t) do { } while (0)
34+
#endif
35+
36+
/*
37+
* Clang 13 and earlier generate unwanted libcalls for 64-bit tests on
38+
* 32-bit hosts.
39+
*/
40+
#if defined(CONFIG_CC_IS_CLANG) && __clang_major__ <= 13 && \
41+
BITS_PER_LONG != 64
42+
# define SKIP_64_ON_32(t) SKIP(t, "Clang 13 unwanted libcalls")
43+
#else
44+
# define SKIP_64_ON_32(t) do { } while (0)
45+
#endif
46+
1947
#define DEFINE_TEST_ARRAY_TYPED(t1, t2, t) \
2048
static const struct test_ ## t1 ## _ ## t2 ## __ ## t { \
2149
t1 a; \
@@ -94,7 +122,6 @@ DEFINE_TEST_ARRAY(u32) = {
94122
{-4U, 5U, 1U, -9U, -20U, true, false, true},
95123
};
96124

97-
#if BITS_PER_LONG == 64
98125
DEFINE_TEST_ARRAY(u64) = {
99126
{0, 0, 0, 0, 0, false, false, false},
100127
{1, 1, 2, 0, 1, false, false, false},
@@ -118,7 +145,6 @@ DEFINE_TEST_ARRAY(u64) = {
118145
false, true, false},
119146
{-15ULL, 10ULL, -5ULL, -25ULL, -150ULL, false, false, true},
120147
};
121-
#endif
122148

123149
DEFINE_TEST_ARRAY(s8) = {
124150
{0, 0, 0, 0, 0, false, false, false},
@@ -194,7 +220,6 @@ DEFINE_TEST_ARRAY(s32) = {
194220
{S32_MAX, S32_MAX, -2, 0, 1, true, false, true},
195221
};
196222

197-
#if BITS_PER_LONG == 64
198223
DEFINE_TEST_ARRAY(s64) = {
199224
{0, 0, 0, 0, 0, false, false, false},
200225

@@ -223,7 +248,6 @@ DEFINE_TEST_ARRAY(s64) = {
223248
{-128, -1, -129, -127, 128, false, false, false},
224249
{0, -S64_MAX, -S64_MAX, S64_MAX, 0, false, false, false},
225250
};
226-
#endif
227251

228252
#define check_one_op(t, fmt, op, sym, a, b, r, of) do { \
229253
int _a_orig = a, _a_bump = a + 1; \
@@ -246,7 +270,7 @@ DEFINE_TEST_ARRAY(s64) = {
246270

247271
#define DEFINE_TEST_FUNC_TYPED(n, t, fmt) \
248272
static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \
249-
{ \
273+
{ \
250274
check_one_op(t, fmt, add, "+", p->a, p->b, p->sum, p->s_of); \
251275
check_one_op(t, fmt, add, "+", p->b, p->a, p->sum, p->s_of); \
252276
check_one_op(t, fmt, sub, "-", p->a, p->b, p->diff, p->d_of); \
@@ -257,6 +281,12 @@ static void do_test_ ## n(struct kunit *test, const struct test_ ## n *p) \
257281
static void n ## _overflow_test(struct kunit *test) { \
258282
unsigned i; \
259283
\
284+
SKIP_64_ON_32(__same_type(t, u64)); \
285+
SKIP_64_ON_32(__same_type(t, s64)); \
286+
SKIP_SIGN_MISMATCH(__same_type(n ## _tests[0].a, u32) && \
287+
__same_type(n ## _tests[0].b, u32) && \
288+
__same_type(n ## _tests[0].sum, int)); \
289+
\
260290
for (i = 0; i < ARRAY_SIZE(n ## _tests); ++i) \
261291
do_test_ ## n(test, &n ## _tests[i]); \
262292
kunit_info(test, "%zu %s arithmetic tests finished\n", \
@@ -272,10 +302,8 @@ DEFINE_TEST_FUNC(u16, "%d");
272302
DEFINE_TEST_FUNC(s16, "%d");
273303
DEFINE_TEST_FUNC(u32, "%u");
274304
DEFINE_TEST_FUNC(s32, "%d");
275-
#if BITS_PER_LONG == 64
276305
DEFINE_TEST_FUNC(u64, "%llu");
277306
DEFINE_TEST_FUNC(s64, "%lld");
278-
#endif
279307

280308
DEFINE_TEST_ARRAY_TYPED(u32, u32, u8) = {
281309
{0, 0, 0, 0, 0, false, false, false},
@@ -715,13 +743,10 @@ static struct kunit_case overflow_test_cases[] = {
715743
KUNIT_CASE(s16_s16__s16_overflow_test),
716744
KUNIT_CASE(u32_u32__u32_overflow_test),
717745
KUNIT_CASE(s32_s32__s32_overflow_test),
718-
/* Clang 13 and earlier generate unwanted libcalls on 32-bit. */
719-
#if BITS_PER_LONG == 64
720746
KUNIT_CASE(u64_u64__u64_overflow_test),
721747
KUNIT_CASE(s64_s64__s64_overflow_test),
722-
#endif
723-
KUNIT_CASE(u32_u32__u8_overflow_test),
724748
KUNIT_CASE(u32_u32__int_overflow_test),
749+
KUNIT_CASE(u32_u32__u8_overflow_test),
725750
KUNIT_CASE(u8_u8__int_overflow_test),
726751
KUNIT_CASE(int_int__u8_overflow_test),
727752
KUNIT_CASE(shift_sane_test),

0 commit comments

Comments
 (0)