Skip to content

Commit 4ce615e

Browse files
committed
fortify: Provide KUnit counters for failure testing
The standard C string APIs were not designed to have a failure mode; they were expected to always succeed without memory safety issues. Normally, CONFIG_FORTIFY_SOURCE will use fortify_panic() to stop processing, as truncating a read or write may provide an even worse system state. However, this creates a problem for testing under things like KUnit, which needs a way to survive failures. When building with CONFIG_KUNIT, provide a failure path for all users of fortify_panic, and track whether the failure was a read overflow or a write overflow, for KUnit tests to examine. Inspired by similar logic in the slab tests. Signed-off-by: Kees Cook <keescook@chromium.org>
1 parent 1a78f8c commit 4ce615e

3 files changed

Lines changed: 66 additions & 20 deletions

File tree

include/linux/fortify-string.h

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,10 @@
1515
#define FORTIFY_REASON(func, write) (FIELD_PREP(BIT(0), write) | \
1616
FIELD_PREP(GENMASK(7, 1), func))
1717

18-
#define fortify_panic(func, write) \
19-
__fortify_panic(FORTIFY_REASON(func, write))
18+
#ifndef fortify_panic
19+
# define fortify_panic(func, write, retfail) \
20+
__fortify_panic(FORTIFY_REASON(func, write))
21+
#endif
2022

2123
#define FORTIFY_READ 0
2224
#define FORTIFY_WRITE 1
@@ -181,7 +183,7 @@ char *strncpy(char * const POS p, const char *q, __kernel_size_t size)
181183
if (__compiletime_lessthan(p_size, size))
182184
__write_overflow();
183185
if (p_size < size)
184-
fortify_panic(FORTIFY_FUNC_strncpy, FORTIFY_WRITE);
186+
fortify_panic(FORTIFY_FUNC_strncpy, FORTIFY_WRITE, p);
185187
return __underlying_strncpy(p, q, size);
186188
}
187189

@@ -212,7 +214,7 @@ __FORTIFY_INLINE __kernel_size_t strnlen(const char * const POS p, __kernel_size
212214
/* Do not check characters beyond the end of p. */
213215
ret = __real_strnlen(p, maxlen < p_size ? maxlen : p_size);
214216
if (p_size <= ret && maxlen != ret)
215-
fortify_panic(FORTIFY_FUNC_strnlen, FORTIFY_READ);
217+
fortify_panic(FORTIFY_FUNC_strnlen, FORTIFY_READ, ret);
216218
return ret;
217219
}
218220

@@ -248,7 +250,7 @@ __kernel_size_t __fortify_strlen(const char * const POS p)
248250
return __underlying_strlen(p);
249251
ret = strnlen(p, p_size);
250252
if (p_size <= ret)
251-
fortify_panic(FORTIFY_FUNC_strlen, FORTIFY_READ);
253+
fortify_panic(FORTIFY_FUNC_strlen, FORTIFY_READ, ret);
252254
return ret;
253255
}
254256

@@ -299,7 +301,7 @@ __FORTIFY_INLINE ssize_t sized_strscpy(char * const POS p, const char * const PO
299301
* p_size.
300302
*/
301303
if (len > p_size)
302-
fortify_panic(FORTIFY_FUNC_strscpy, FORTIFY_WRITE);
304+
fortify_panic(FORTIFY_FUNC_strscpy, FORTIFY_WRITE, -E2BIG);
303305

304306
/*
305307
* We can now safely call vanilla strscpy because we are protected from:
@@ -357,7 +359,7 @@ size_t strlcat(char * const POS p, const char * const POS q, size_t avail)
357359

358360
/* Give up if string is already overflowed. */
359361
if (p_size <= p_len)
360-
fortify_panic(FORTIFY_FUNC_strlcat, FORTIFY_READ);
362+
fortify_panic(FORTIFY_FUNC_strlcat, FORTIFY_READ, wanted);
361363

362364
if (actual >= avail) {
363365
copy_len = avail - p_len - 1;
@@ -366,7 +368,7 @@ size_t strlcat(char * const POS p, const char * const POS q, size_t avail)
366368

367369
/* Give up if copy will overflow. */
368370
if (p_size <= actual)
369-
fortify_panic(FORTIFY_FUNC_strlcat, FORTIFY_WRITE);
371+
fortify_panic(FORTIFY_FUNC_strlcat, FORTIFY_WRITE, wanted);
370372
__underlying_memcpy(p + p_len, q, copy_len);
371373
p[actual] = '\0';
372374

@@ -395,7 +397,7 @@ char *strcat(char * const POS p, const char *q)
395397
const size_t p_size = __member_size(p);
396398

397399
if (strlcat(p, q, p_size) >= p_size)
398-
fortify_panic(FORTIFY_FUNC_strcat, FORTIFY_WRITE);
400+
fortify_panic(FORTIFY_FUNC_strcat, FORTIFY_WRITE, p);
399401
return p;
400402
}
401403

@@ -431,13 +433,13 @@ char *strncat(char * const POS p, const char * const POS q, __kernel_size_t coun
431433
p_len = strlen(p);
432434
copy_len = strnlen(q, count);
433435
if (p_size < p_len + copy_len + 1)
434-
fortify_panic(FORTIFY_FUNC_strncat, FORTIFY_WRITE);
436+
fortify_panic(FORTIFY_FUNC_strncat, FORTIFY_WRITE, p);
435437
__underlying_memcpy(p + p_len, q, copy_len);
436438
p[p_len + copy_len] = '\0';
437439
return p;
438440
}
439441

440-
__FORTIFY_INLINE void fortify_memset_chk(__kernel_size_t size,
442+
__FORTIFY_INLINE bool fortify_memset_chk(__kernel_size_t size,
441443
const size_t p_size,
442444
const size_t p_size_field)
443445
{
@@ -472,7 +474,8 @@ __FORTIFY_INLINE void fortify_memset_chk(__kernel_size_t size,
472474
* lengths are unknown.)
473475
*/
474476
if (p_size != SIZE_MAX && p_size < size)
475-
fortify_panic(FORTIFY_FUNC_memset, FORTIFY_WRITE);
477+
fortify_panic(FORTIFY_FUNC_memset, FORTIFY_WRITE, true);
478+
return false;
476479
}
477480

478481
#define __fortify_memset_chk(p, c, size, p_size, p_size_field) ({ \
@@ -571,9 +574,9 @@ __FORTIFY_INLINE bool fortify_memcpy_chk(__kernel_size_t size,
571574
* lengths are unknown.)
572575
*/
573576
if (p_size != SIZE_MAX && p_size < size)
574-
fortify_panic(func, FORTIFY_WRITE);
577+
fortify_panic(func, FORTIFY_WRITE, true);
575578
else if (q_size != SIZE_MAX && q_size < size)
576-
fortify_panic(func, FORTIFY_READ);
579+
fortify_panic(func, FORTIFY_READ, true);
577580

578581
/*
579582
* Warn when writing beyond destination field size.
@@ -673,7 +676,7 @@ __FORTIFY_INLINE void *memscan(void * const POS0 p, int c, __kernel_size_t size)
673676
if (__compiletime_lessthan(p_size, size))
674677
__read_overflow();
675678
if (p_size < size)
676-
fortify_panic(FORTIFY_FUNC_memscan, FORTIFY_READ);
679+
fortify_panic(FORTIFY_FUNC_memscan, FORTIFY_READ, NULL);
677680
return __real_memscan(p, c, size);
678681
}
679682

@@ -690,7 +693,7 @@ int memcmp(const void * const POS0 p, const void * const POS0 q, __kernel_size_t
690693
__read_overflow2();
691694
}
692695
if (p_size < size || q_size < size)
693-
fortify_panic(FORTIFY_FUNC_memcmp, FORTIFY_READ);
696+
fortify_panic(FORTIFY_FUNC_memcmp, FORTIFY_READ, INT_MIN);
694697
return __underlying_memcmp(p, q, size);
695698
}
696699

@@ -702,7 +705,7 @@ void *memchr(const void * const POS0 p, int c, __kernel_size_t size)
702705
if (__compiletime_lessthan(p_size, size))
703706
__read_overflow();
704707
if (p_size < size)
705-
fortify_panic(FORTIFY_FUNC_memchr, FORTIFY_READ);
708+
fortify_panic(FORTIFY_FUNC_memchr, FORTIFY_READ, NULL);
706709
return __underlying_memchr(p, c, size);
707710
}
708711

@@ -714,7 +717,7 @@ __FORTIFY_INLINE void *memchr_inv(const void * const POS0 p, int c, size_t size)
714717
if (__compiletime_lessthan(p_size, size))
715718
__read_overflow();
716719
if (p_size < size)
717-
fortify_panic(FORTIFY_FUNC_memchr_inv, FORTIFY_READ);
720+
fortify_panic(FORTIFY_FUNC_memchr_inv, FORTIFY_READ, NULL);
718721
return __real_memchr_inv(p, c, size);
719722
}
720723

@@ -727,7 +730,7 @@ __FORTIFY_INLINE void *kmemdup(const void * const POS0 p, size_t size, gfp_t gfp
727730
if (__compiletime_lessthan(p_size, size))
728731
__read_overflow();
729732
if (p_size < size)
730-
fortify_panic(FORTIFY_FUNC_kmemdup, FORTIFY_READ);
733+
fortify_panic(FORTIFY_FUNC_kmemdup, FORTIFY_READ, NULL);
731734
return __real_kmemdup(p, size, gfp);
732735
}
733736

@@ -764,7 +767,7 @@ char *strcpy(char * const POS p, const char * const POS q)
764767
__write_overflow();
765768
/* Run-time check for dynamic size overflow. */
766769
if (p_size < size)
767-
fortify_panic(FORTIFY_FUNC_strcpy, FORTIFY_WRITE);
770+
fortify_panic(FORTIFY_FUNC_strcpy, FORTIFY_WRITE, p);
768771
__underlying_memcpy(p, q, size);
769772
return p;
770773
}

lib/fortify_kunit.c

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,17 @@
1515
*/
1616
#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
1717

18+
/* Redefine fortify_panic() to track failures. */
19+
void fortify_add_kunit_error(int write);
20+
#define fortify_panic(func, write, retfail) do { \
21+
__fortify_report(FORTIFY_REASON(func, write)); \
22+
fortify_add_kunit_error(write); \
23+
return (retfail); \
24+
} while (0)
25+
1826
#include <kunit/device.h>
1927
#include <kunit/test.h>
28+
#include <kunit/test-bug.h>
2029
#include <linux/device.h>
2130
#include <linux/slab.h>
2231
#include <linux/string.h>
@@ -27,10 +36,34 @@
2736
# define __compiletime_strlen __builtin_strlen
2837
#endif
2938

39+
static struct kunit_resource read_resource;
40+
static struct kunit_resource write_resource;
41+
static int fortify_read_overflows;
42+
static int fortify_write_overflows;
43+
3044
static const char array_of_10[] = "this is 10";
3145
static const char *ptr_of_11 = "this is 11!";
3246
static char array_unknown[] = "compiler thinks I might change";
3347

48+
void fortify_add_kunit_error(int write)
49+
{
50+
struct kunit_resource *resource;
51+
struct kunit *current_test;
52+
53+
current_test = kunit_get_current_test();
54+
if (!current_test)
55+
return;
56+
57+
resource = kunit_find_named_resource(current_test,
58+
write ? "fortify_write_overflows"
59+
: "fortify_read_overflows");
60+
if (!resource)
61+
return;
62+
63+
(*(int *)resource->data)++;
64+
kunit_put_resource(resource);
65+
}
66+
3467
static void known_sizes_test(struct kunit *test)
3568
{
3669
KUNIT_EXPECT_EQ(test, __compiletime_strlen("88888888"), 8);
@@ -318,6 +351,14 @@ static int fortify_test_init(struct kunit *test)
318351
if (!IS_ENABLED(CONFIG_FORTIFY_SOURCE))
319352
kunit_skip(test, "Not built with CONFIG_FORTIFY_SOURCE=y");
320353

354+
fortify_read_overflows = 0;
355+
kunit_add_named_resource(test, NULL, NULL, &read_resource,
356+
"fortify_read_overflows",
357+
&fortify_read_overflows);
358+
fortify_write_overflows = 0;
359+
kunit_add_named_resource(test, NULL, NULL, &write_resource,
360+
"fortify_write_overflows",
361+
&fortify_write_overflows);
321362
return 0;
322363
}
323364

lib/string_helpers.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
#include <linux/slab.h>
1919
#include <linux/string.h>
2020
#include <linux/string_helpers.h>
21+
#include <kunit/test.h>
22+
#include <kunit/test-bug.h>
2123

2224
/**
2325
* string_get_size - get the size in the specified units

0 commit comments

Comments
 (0)