Skip to content

Commit 3bf8a45

Browse files
committed
Merge tag 'hardening-v6.15-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux
Pull hardening fixes from Kees Cook: - lib/prime_numbers: KUnit test should not select PRIME_NUMBERS (Geert Uytterhoeven) - ubsan: Fix panic from test_ubsan_out_of_bounds (Mostafa Saleh) - ubsan: Remove 'default UBSAN' from UBSAN_INTEGER_WRAP (Nathan Chancellor) - string: Add load_unaligned_zeropad() code path to sized_strscpy() (Peter Collingbourne) - kasan: Add strscpy() test to trigger tag fault on arm64 (Vincenzo Frascino) - Disable GCC randstruct for COMPILE_TEST * tag 'hardening-v6.15-rc3' of git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux: lib/prime_numbers: KUnit test should not select PRIME_NUMBERS ubsan: Fix panic from test_ubsan_out_of_bounds lib/Kconfig.ubsan: Remove 'default UBSAN' from UBSAN_INTEGER_WRAP hardening: Disable GCC randstruct for COMPILE_TEST kasan: Add strscpy() test to trigger tag fault on arm64 string: Add load_unaligned_zeropad() code path to sized_strscpy()
2 parents 30d451e + 3f29251 commit 3bf8a45

File tree

7 files changed

+44
-13
lines changed

7 files changed

+44
-13
lines changed

lib/Kconfig.debug

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3290,7 +3290,7 @@ config GCD_KUNIT_TEST
32903290
config PRIME_NUMBERS_KUNIT_TEST
32913291
tristate "Prime number generator test" if !KUNIT_ALL_TESTS
32923292
depends on KUNIT
3293-
select PRIME_NUMBERS
3293+
depends on PRIME_NUMBERS
32943294
default KUNIT_ALL_TESTS
32953295
help
32963296
This option enables the KUnit test suite for the {is,next}_prime_number

lib/Kconfig.ubsan

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,6 @@ config UBSAN_UNREACHABLE
118118

119119
config UBSAN_INTEGER_WRAP
120120
bool "Perform checking for integer arithmetic wrap-around"
121-
default UBSAN
122121
depends on !COMPILE_TEST
123122
depends on $(cc-option,-fsanitize-undefined-ignore-overflow-pattern=all)
124123
depends on $(cc-option,-fsanitize=signed-integer-overflow)

lib/string.c

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,7 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)
119119
if (count == 0 || WARN_ON_ONCE(count > INT_MAX))
120120
return -E2BIG;
121121

122+
#ifndef CONFIG_DCACHE_WORD_ACCESS
122123
#ifdef CONFIG_HAVE_EFFICIENT_UNALIGNED_ACCESS
123124
/*
124125
* If src is unaligned, don't cross a page boundary,
@@ -133,20 +134,26 @@ ssize_t sized_strscpy(char *dest, const char *src, size_t count)
133134
/* If src or dest is unaligned, don't do word-at-a-time. */
134135
if (((long) dest | (long) src) & (sizeof(long) - 1))
135136
max = 0;
137+
#endif
136138
#endif
137139

138140
/*
139-
* read_word_at_a_time() below may read uninitialized bytes after the
140-
* trailing zero and use them in comparisons. Disable this optimization
141-
* under KMSAN to prevent false positive reports.
141+
* load_unaligned_zeropad() or read_word_at_a_time() below may read
142+
* uninitialized bytes after the trailing zero and use them in
143+
* comparisons. Disable this optimization under KMSAN to prevent
144+
* false positive reports.
142145
*/
143146
if (IS_ENABLED(CONFIG_KMSAN))
144147
max = 0;
145148

146149
while (max >= sizeof(unsigned long)) {
147150
unsigned long c, data;
148151

152+
#ifdef CONFIG_DCACHE_WORD_ACCESS
153+
c = load_unaligned_zeropad(src+res);
154+
#else
149155
c = read_word_at_a_time(src+res);
156+
#endif
150157
if (has_zero(c, &data, &constants)) {
151158
data = prep_zero_mask(c, data, &constants);
152159
data = create_zero_mask(data);

lib/test_ubsan.c

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -77,18 +77,22 @@ static void test_ubsan_shift_out_of_bounds(void)
7777

7878
static void test_ubsan_out_of_bounds(void)
7979
{
80-
volatile int i = 4, j = 5, k = -1;
81-
volatile char above[4] = { }; /* Protect surrounding memory. */
82-
volatile int arr[4];
83-
volatile char below[4] = { }; /* Protect surrounding memory. */
80+
int i = 4, j = 4, k = -1;
81+
volatile struct {
82+
char above[4]; /* Protect surrounding memory. */
83+
int arr[4];
84+
char below[4]; /* Protect surrounding memory. */
85+
} data;
8486

85-
above[0] = below[0];
87+
OPTIMIZER_HIDE_VAR(i);
88+
OPTIMIZER_HIDE_VAR(j);
89+
OPTIMIZER_HIDE_VAR(k);
8690

8791
UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "above");
88-
arr[j] = i;
92+
data.arr[j] = i;
8993

9094
UBSAN_TEST(CONFIG_UBSAN_BOUNDS, "below");
91-
arr[k] = i;
95+
data.arr[k] = i;
9296
}
9397

9498
enum ubsan_test_enum {

mm/kasan/kasan_test_c.c

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1567,6 +1567,7 @@ static void kasan_memcmp(struct kunit *test)
15671567
static void kasan_strings(struct kunit *test)
15681568
{
15691569
char *ptr;
1570+
char *src;
15701571
size_t size = 24;
15711572

15721573
/*
@@ -1578,6 +1579,25 @@ static void kasan_strings(struct kunit *test)
15781579
ptr = kmalloc(size, GFP_KERNEL | __GFP_ZERO);
15791580
KUNIT_ASSERT_NOT_ERR_OR_NULL(test, ptr);
15801581

1582+
src = kmalloc(KASAN_GRANULE_SIZE, GFP_KERNEL | __GFP_ZERO);
1583+
strscpy(src, "f0cacc1a0000000", KASAN_GRANULE_SIZE);
1584+
1585+
/*
1586+
* Make sure that strscpy() does not trigger KASAN if it overreads into
1587+
* poisoned memory.
1588+
*
1589+
* The expected size does not include the terminator '\0'
1590+
* so it is (KASAN_GRANULE_SIZE - 2) ==
1591+
* KASAN_GRANULE_SIZE - ("initial removed character" + "\0").
1592+
*/
1593+
KUNIT_EXPECT_EQ(test, KASAN_GRANULE_SIZE - 2,
1594+
strscpy(ptr, src + 1, KASAN_GRANULE_SIZE));
1595+
1596+
/* strscpy should fail if the first byte is unreadable. */
1597+
KUNIT_EXPECT_KASAN_FAIL(test, strscpy(ptr, src + KASAN_GRANULE_SIZE,
1598+
KASAN_GRANULE_SIZE));
1599+
1600+
kfree(src);
15811601
kfree(ptr);
15821602

15831603
/*

security/Kconfig.hardening

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -344,7 +344,7 @@ config CC_HAS_RANDSTRUCT
344344

345345
choice
346346
prompt "Randomize layout of sensitive kernel structures"
347-
default RANDSTRUCT_FULL if COMPILE_TEST && (GCC_PLUGINS || CC_HAS_RANDSTRUCT)
347+
default RANDSTRUCT_FULL if COMPILE_TEST && CC_HAS_RANDSTRUCT
348348
default RANDSTRUCT_NONE
349349
help
350350
If you enable this, the layouts of structures that are entirely

tools/testing/selftests/lib/config

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,3 @@
11
CONFIG_TEST_BITMAP=m
2+
CONFIG_PRIME_NUMBERS=m
23
CONFIG_TEST_BITOPS=m

0 commit comments

Comments
 (0)