Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

lkdtm/bugs: Don't expect thread termination without CONFIG_UBSAN_TRAP

When you don't select CONFIG_UBSAN_TRAP, you get:

# echo ARRAY_BOUNDS > /sys/kernel/debug/provoke-crash/DIRECT
[ 102.265827] ================================================================================
[ 102.278433] UBSAN: array-index-out-of-bounds in drivers/misc/lkdtm/bugs.c:342:16
[ 102.287207] index 8 is out of range for type 'char [8]'
[ 102.298722] ================================================================================
[ 102.313712] lkdtm: FAIL: survived array bounds overflow!
[ 102.318770] lkdtm: Unexpected! This kernel (5.16.0-rc1-s3k-dev-01884-g720dcf79314a ppc) was built with CONFIG_UBSAN_BOUNDS=y

It is not correct because when CONFIG_UBSAN_TRAP is not selected
you can't expect array bounds overflow to kill the thread.

Modify the logic so that when the kernel is built with
CONFIG_UBSAN_BOUNDS but without CONFIG_UBSAN_TRAP, you get a warning
about CONFIG_UBSAN_TRAP not been selected instead.

This also require a fix of pr_expected_config(), otherwise the
following error is encountered.

CC drivers/misc/lkdtm/bugs.o
drivers/misc/lkdtm/bugs.c: In function 'lkdtm_ARRAY_BOUNDS':
drivers/misc/lkdtm/bugs.c:351:2: error: 'else' without a previous 'if'
351 | else
| ^~~~

Fixes: c75be56e35b2 ("lkdtm/bugs: Add ARRAY_BOUNDS to selftests")
Signed-off-by: Christophe Leroy <christophe.leroy@csgroup.eu>
Signed-off-by: Kees Cook <keescook@chromium.org>
Link: https://lore.kernel.org/r/363b58690e907c677252467a94fe49444c80ea76.1649704381.git.christophe.leroy@csgroup.eu

authored by

Christophe Leroy and committed by
Kees Cook
8bfdbddd f387e86d

+8 -5
+4 -1
drivers/misc/lkdtm/bugs.c
··· 351 351 kfree(not_checked); 352 352 kfree(checked); 353 353 pr_err("FAIL: survived array bounds overflow!\n"); 354 - pr_expected_config(CONFIG_UBSAN_BOUNDS); 354 + if (IS_ENABLED(CONFIG_UBSAN_BOUNDS)) 355 + pr_expected_config(CONFIG_UBSAN_TRAP); 356 + else 357 + pr_expected_config(CONFIG_UBSAN_BOUNDS); 355 358 } 356 359 357 360 void lkdtm_CORRUPT_LIST_ADD(void)
+4 -4
drivers/misc/lkdtm/lkdtm.h
··· 9 9 extern char *lkdtm_kernel_info; 10 10 11 11 #define pr_expected_config(kconfig) \ 12 - { \ 12 + do { \ 13 13 if (IS_ENABLED(kconfig)) \ 14 14 pr_err("Unexpected! This %s was built with " #kconfig "=y\n", \ 15 15 lkdtm_kernel_info); \ 16 16 else \ 17 17 pr_warn("This is probably expected, since this %s was built *without* " #kconfig "=y\n", \ 18 18 lkdtm_kernel_info); \ 19 - } 19 + } while (0) 20 20 21 21 #ifndef MODULE 22 22 int lkdtm_check_bool_cmdline(const char *param); 23 23 #define pr_expected_config_param(kconfig, param) \ 24 - { \ 24 + do { \ 25 25 if (IS_ENABLED(kconfig)) { \ 26 26 switch (lkdtm_check_bool_cmdline(param)) { \ 27 27 case 0: \ ··· 52 52 break; \ 53 53 } \ 54 54 } \ 55 - } 55 + } while (0) 56 56 #else 57 57 #define pr_expected_config_param(kconfig, param) pr_expected_config(kconfig) 58 58 #endif