Message ID | 20220902204351.2521805-3-keescook@chromium.org (mailing list archive) |
---|---|
State | Mainlined |
Commit | bb26bbd0a0673bbab819deac289290aa5281925f |
Headers | show |
Series | Fix FORTIFY=y UBSAN_LOCAL_BOUNDS=y | expand |
On Sat, Sep 3, 2022 at 4:43 AM Kees Cook <keescook@chromium.org> wrote: > > Add lib/fortify_kunit.c KUnit test for checking the expected behavioral > characteristics of FORTIFY_SOURCE internals. > > Cc: Nick Desaulniers <ndesaulniers@google.com> > Cc: Nathan Chancellor <nathan@kernel.org> > Cc: Tom Rix <trix@redhat.com> > Cc: Andrew Morton <akpm@linux-foundation.org> > Cc: Vlastimil Babka <vbabka@suse.cz> > Cc: "Steven Rostedt (Google)" <rostedt@goodmis.org> > Cc: David Gow <davidgow@google.com> > Cc: Yury Norov <yury.norov@gmail.com> > Cc: Masami Hiramatsu <mhiramat@kernel.org> > Cc: Sander Vanheule <sander@svanheule.net> > Cc: linux-hardening@vger.kernel.org > Cc: llvm@lists.linux.dev > Signed-off-by: Kees Cook <keescook@chromium.org> > --- Overall, this looks good. It's a bit of a shame FORTIFY_SOURCE doesn't work under UML, but I tested it on everything else I had to hand and it looked good. One tiny typo in a comment below, but otherwise this is: Reviewed-by: David Gow <davidgow@google.com> Cheers, -- David > MAINTAINERS | 1 + > lib/Kconfig.debug | 9 ++++++ > lib/Makefile | 1 + > lib/fortify_kunit.c | 77 +++++++++++++++++++++++++++++++++++++++++++++ > 4 files changed, 88 insertions(+) > create mode 100644 lib/fortify_kunit.c > > diff --git a/MAINTAINERS b/MAINTAINERS > index 9d7f64dc0efe..640115472199 100644 > --- a/MAINTAINERS > +++ b/MAINTAINERS > @@ -8002,6 +8002,7 @@ L: linux-hardening@vger.kernel.org > S: Supported > T: git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/hardening > F: include/linux/fortify-string.h > +F: lib/fortify_kunit.c > F: lib/test_fortify/* > F: scripts/test_fortify.sh > K: \b__NO_FORTIFY\b > diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug > index 36455953d306..1f267c0ddffd 100644 > --- a/lib/Kconfig.debug > +++ b/lib/Kconfig.debug > @@ -2542,6 +2542,15 @@ config STACKINIT_KUNIT_TEST > CONFIG_GCC_PLUGIN_STRUCTLEAK, CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF, > or CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL. > > +config FORTIFY_KUNIT_TEST > + tristate "Test fortified str*() and mem*() function internals at runtime" if !KUNIT_ALL_TESTS > + depends on KUNIT && FORTIFY_SOURCE > + default KUNIT_ALL_TESTS > + help > + Builds unit tests for checking internals of FORTIFY_SOURCE as used > + by the str*() and mem*() family of functions. For testing runtime > + traps of FORTIFY_SOURCE, see LKDTM's "FORTIFY_*" tests. > + > config TEST_UDELAY > tristate "udelay test driver" > help > diff --git a/lib/Makefile b/lib/Makefile > index f545140ed9e7..4ee1ceae945a 100644 > --- a/lib/Makefile > +++ b/lib/Makefile > @@ -381,6 +381,7 @@ obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_kunit.o > obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o > CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable) > obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o > +obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o > > obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o > > diff --git a/lib/fortify_kunit.c b/lib/fortify_kunit.c > new file mode 100644 > index 000000000000..4d7930b65107 > --- /dev/null > +++ b/lib/fortify_kunit.c > @@ -0,0 +1,77 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Runtime test cases for CONFIG_FORTIFY_SOURCE that aren't expected to > + * Oops th kernel on success. (For those, see drivers/misc/lkdtm/fortify.c) Nit: Oops _the_ kernel > + * > + * For corner cases with UBSAN, try testing with: > + * > + * ./tools/testing/kunit/kunit.py run --arch=x86_64 \ > + * --kconfig_add CONFIG_FORTIFY_SOURCE=y \ > + * --kconfig_add CONFIG_UBSAN=y \ > + * --kconfig_add CONFIG_UBSAN_TRAP=y \ > + * --kconfig_add CONFIG_UBSAN_BOUNDS=y \ > + * --kconfig_add CONFIG_UBSAN_LOCAL_BOUNDS=y \ > + * --make_options LLVM=1 fortify > + */ > +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt > + > +#include <kunit/test.h> > +#include <linux/string.h> > +#include <linux/init.h> > + > +static const char array_of_10[] = "this is 10"; > +static const char *ptr_of_11 = "this is 11!"; > +static char array_unknown[] = "compiler thinks I might change"; > + > +static void known_sizes_test(struct kunit *test) > +{ > + KUNIT_EXPECT_EQ(test, __compiletime_strlen("88888888"), 8); > + KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_of_10), 10); > + KUNIT_EXPECT_EQ(test, __compiletime_strlen(ptr_of_11), 11); > + > + KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_unknown), SIZE_MAX); > + /* Externally defined and dynamically sized string pointer: */ > + KUNIT_EXPECT_EQ(test, __compiletime_strlen(saved_command_line), SIZE_MAX); > +} > + > +/* This is volatile so the optimizer can't perform DCE below. */ > +static volatile int pick; > + > +/* Not inline to keep optimizer from figuring out which string we want. */ > +static noinline size_t want_minus_one(int pick) > +{ > + const char *str; > + > + switch (pick) { > + case 1: > + str = "4444"; > + break; > + case 2: > + str = "333"; > + break; > + default: > + str = "1"; > + break; > + } > + return __compiletime_strlen(str); > +} > + > +static void control_flow_split_test(struct kunit *test) > +{ > + KUNIT_EXPECT_EQ(test, want_minus_one(pick), SIZE_MAX); > +} > + > +static struct kunit_case fortify_test_cases[] = { > + KUNIT_CASE(known_sizes_test), > + KUNIT_CASE(control_flow_split_test), > + {} > +}; > + > +static struct kunit_suite fortify_test_suite = { > + .name = "fortify", > + .test_cases = fortify_test_cases, > +}; > + > +kunit_test_suite(fortify_test_suite); > + > +MODULE_LICENSE("GPL"); > -- > 2.34.1 >
On Sat, Sep 03, 2022 at 10:59:24AM +0800, David Gow wrote: > On Sat, Sep 3, 2022 at 4:43 AM Kees Cook <keescook@chromium.org> wrote: > > > > Add lib/fortify_kunit.c KUnit test for checking the expected behavioral > > characteristics of FORTIFY_SOURCE internals. > > [...] > > Overall, this looks good. It's a bit of a shame FORTIFY_SOURCE doesn't > work under UML, but I tested it on everything else I had to hand and > it looked good. It looks like this was never picked up: https://lore.kernel.org/lkml/20220210003224.773957-1-keescook@chromium.org/ I suppose I could take it via the kernel hardening tree? > One tiny typo in a comment below, but otherwise this is: > > Reviewed-by: David Gow <davidgow@google.com> > > [...] > > +/* > > + * Runtime test cases for CONFIG_FORTIFY_SOURCE that aren't expected to > > + * Oops th kernel on success. (For those, see drivers/misc/lkdtm/fortify.c) > > Nit: Oops _the_ kernel Thanks! I'll get that updated. :)
On Sat, Sep 3, 2022 at 1:17 PM Kees Cook <keescook@chromium.org> wrote: > > On Sat, Sep 03, 2022 at 10:59:24AM +0800, David Gow wrote: > > On Sat, Sep 3, 2022 at 4:43 AM Kees Cook <keescook@chromium.org> wrote: > > > > > > Add lib/fortify_kunit.c KUnit test for checking the expected behavioral > > > characteristics of FORTIFY_SOURCE internals. > > > [...] > > > > Overall, this looks good. It's a bit of a shame FORTIFY_SOURCE doesn't > > work under UML, but I tested it on everything else I had to hand and > > it looked good. > > It looks like this was never picked up: > https://lore.kernel.org/lkml/20220210003224.773957-1-keescook@chromium.org/ Ah: I'd forgotten about that. Can confirm this test works under UML with that patch applied. Cheers, -- David
Hi Kees, On Fri, Sep 02, 2022 at 01:43:50PM -0700, Kees Cook wrote: > Add lib/fortify_kunit.c KUnit test for checking the expected behavioral > characteristics of FORTIFY_SOURCE internals. > > Cc: Nick Desaulniers <ndesaulniers@google.com> > Cc: Nathan Chancellor <nathan@kernel.org> > Cc: Tom Rix <trix@redhat.com> > Cc: Andrew Morton <akpm@linux-foundation.org> > Cc: Vlastimil Babka <vbabka@suse.cz> > Cc: "Steven Rostedt (Google)" <rostedt@goodmis.org> > Cc: David Gow <davidgow@google.com> > Cc: Yury Norov <yury.norov@gmail.com> > Cc: Masami Hiramatsu <mhiramat@kernel.org> > Cc: Sander Vanheule <sander@svanheule.net> > Cc: linux-hardening@vger.kernel.org > Cc: llvm@lists.linux.dev > Signed-off-by: Kees Cook <keescook@chromium.org> > --- > MAINTAINERS | 1 + > lib/Kconfig.debug | 9 ++++++ > lib/Makefile | 1 + > lib/fortify_kunit.c | 77 +++++++++++++++++++++++++++++++++++++++++++++ > 4 files changed, 88 insertions(+) > create mode 100644 lib/fortify_kunit.c > > diff --git a/MAINTAINERS b/MAINTAINERS > index 9d7f64dc0efe..640115472199 100644 > --- a/MAINTAINERS > +++ b/MAINTAINERS > @@ -8002,6 +8002,7 @@ L: linux-hardening@vger.kernel.org > S: Supported > T: git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/hardening > F: include/linux/fortify-string.h > +F: lib/fortify_kunit.c > F: lib/test_fortify/* > F: scripts/test_fortify.sh > K: \b__NO_FORTIFY\b > diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug > index 36455953d306..1f267c0ddffd 100644 > --- a/lib/Kconfig.debug > +++ b/lib/Kconfig.debug > @@ -2542,6 +2542,15 @@ config STACKINIT_KUNIT_TEST > CONFIG_GCC_PLUGIN_STRUCTLEAK, CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF, > or CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL. > > +config FORTIFY_KUNIT_TEST > + tristate "Test fortified str*() and mem*() function internals at runtime" if !KUNIT_ALL_TESTS > + depends on KUNIT && FORTIFY_SOURCE > + default KUNIT_ALL_TESTS > + help > + Builds unit tests for checking internals of FORTIFY_SOURCE as used > + by the str*() and mem*() family of functions. For testing runtime > + traps of FORTIFY_SOURCE, see LKDTM's "FORTIFY_*" tests. > + > config TEST_UDELAY > tristate "udelay test driver" > help > diff --git a/lib/Makefile b/lib/Makefile > index f545140ed9e7..4ee1ceae945a 100644 > --- a/lib/Makefile > +++ b/lib/Makefile > @@ -381,6 +381,7 @@ obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_kunit.o > obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o > CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable) > obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o > +obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o > > obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o > > diff --git a/lib/fortify_kunit.c b/lib/fortify_kunit.c > new file mode 100644 > index 000000000000..4d7930b65107 > --- /dev/null > +++ b/lib/fortify_kunit.c > @@ -0,0 +1,77 @@ > +// SPDX-License-Identifier: GPL-2.0 > +/* > + * Runtime test cases for CONFIG_FORTIFY_SOURCE that aren't expected to > + * Oops th kernel on success. (For those, see drivers/misc/lkdtm/fortify.c) > + * > + * For corner cases with UBSAN, try testing with: > + * > + * ./tools/testing/kunit/kunit.py run --arch=x86_64 \ > + * --kconfig_add CONFIG_FORTIFY_SOURCE=y \ > + * --kconfig_add CONFIG_UBSAN=y \ > + * --kconfig_add CONFIG_UBSAN_TRAP=y \ > + * --kconfig_add CONFIG_UBSAN_BOUNDS=y \ > + * --kconfig_add CONFIG_UBSAN_LOCAL_BOUNDS=y \ > + * --make_options LLVM=1 fortify > + */ > +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt > + > +#include <kunit/test.h> > +#include <linux/string.h> > +#include <linux/init.h> > + > +static const char array_of_10[] = "this is 10"; > +static const char *ptr_of_11 = "this is 11!"; > +static char array_unknown[] = "compiler thinks I might change"; > + > +static void known_sizes_test(struct kunit *test) > +{ > + KUNIT_EXPECT_EQ(test, __compiletime_strlen("88888888"), 8); > + KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_of_10), 10); > + KUNIT_EXPECT_EQ(test, __compiletime_strlen(ptr_of_11), 11); > + > + KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_unknown), SIZE_MAX); > + /* Externally defined and dynamically sized string pointer: */ > + KUNIT_EXPECT_EQ(test, __compiletime_strlen(saved_command_line), SIZE_MAX); > +} I noticed that my allmodconfig + ThinLTO builds broke with this change: $ make -skj"$(nproc)" ARCH=arm64 KCONFIG_ALLCONFIG=<(printf "CONFIG_%s=n\nCONFIG_%s=n\nCONFIG_%s=y\nCONFIG_%s=n\n" GCOV_KERNEL KASAN LTO_CLANG_THIN WERROR) LLVM=1 mrproper allmodconfig all ... ERROR: modpost: "saved_command_line" [lib/fortify_kunit.ko] undefined! ... saved_command_line is not exported to modules. This appears to be related to the fact that CONFIG_KCSAN gets enabled due to the way KCONFIG_ALLCONFIG works, as the issue does not show up in our CI, which uses merge_config.sh. A smaller reproducer on top of defconfig: $ make -skj"$(nproc)" ARCH=arm64 LLVM=1 defconfig $ scripts/config \ -d LTO_NONE \ -e EXPERT \ -e FORTIFY_SOURCE \ -e KCSAN \ -e LTO_CLANG_THIN \ -m FORTIFY_KUNIT_TEST \ -m KUNIT $ make -skj"$(nproc)" ARCH=arm64 LLVM=1 olddefconfig all ... ERROR: modpost: "saved_command_line" [lib/fortify_kunit.ko] undefined! ... I don't see this without LTO. Is this a compiler problem? Small note, Marco's patch is needed to avoid a separate linking error with ToT LLVM: https://lore.kernel.org/20220912094541.929856-1-elver@google.com/ Cheers, Nathan
diff --git a/MAINTAINERS b/MAINTAINERS index 9d7f64dc0efe..640115472199 100644 --- a/MAINTAINERS +++ b/MAINTAINERS @@ -8002,6 +8002,7 @@ L: linux-hardening@vger.kernel.org S: Supported T: git git://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git for-next/hardening F: include/linux/fortify-string.h +F: lib/fortify_kunit.c F: lib/test_fortify/* F: scripts/test_fortify.sh K: \b__NO_FORTIFY\b diff --git a/lib/Kconfig.debug b/lib/Kconfig.debug index 36455953d306..1f267c0ddffd 100644 --- a/lib/Kconfig.debug +++ b/lib/Kconfig.debug @@ -2542,6 +2542,15 @@ config STACKINIT_KUNIT_TEST CONFIG_GCC_PLUGIN_STRUCTLEAK, CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF, or CONFIG_GCC_PLUGIN_STRUCTLEAK_BYREF_ALL. +config FORTIFY_KUNIT_TEST + tristate "Test fortified str*() and mem*() function internals at runtime" if !KUNIT_ALL_TESTS + depends on KUNIT && FORTIFY_SOURCE + default KUNIT_ALL_TESTS + help + Builds unit tests for checking internals of FORTIFY_SOURCE as used + by the str*() and mem*() family of functions. For testing runtime + traps of FORTIFY_SOURCE, see LKDTM's "FORTIFY_*" tests. + config TEST_UDELAY tristate "udelay test driver" help diff --git a/lib/Makefile b/lib/Makefile index f545140ed9e7..4ee1ceae945a 100644 --- a/lib/Makefile +++ b/lib/Makefile @@ -381,6 +381,7 @@ obj-$(CONFIG_IS_SIGNED_TYPE_KUNIT_TEST) += is_signed_type_kunit.o obj-$(CONFIG_OVERFLOW_KUNIT_TEST) += overflow_kunit.o CFLAGS_stackinit_kunit.o += $(call cc-disable-warning, switch-unreachable) obj-$(CONFIG_STACKINIT_KUNIT_TEST) += stackinit_kunit.o +obj-$(CONFIG_FORTIFY_KUNIT_TEST) += fortify_kunit.o obj-$(CONFIG_GENERIC_LIB_DEVMEM_IS_ALLOWED) += devmem_is_allowed.o diff --git a/lib/fortify_kunit.c b/lib/fortify_kunit.c new file mode 100644 index 000000000000..4d7930b65107 --- /dev/null +++ b/lib/fortify_kunit.c @@ -0,0 +1,77 @@ +// SPDX-License-Identifier: GPL-2.0 +/* + * Runtime test cases for CONFIG_FORTIFY_SOURCE that aren't expected to + * Oops th kernel on success. (For those, see drivers/misc/lkdtm/fortify.c) + * + * For corner cases with UBSAN, try testing with: + * + * ./tools/testing/kunit/kunit.py run --arch=x86_64 \ + * --kconfig_add CONFIG_FORTIFY_SOURCE=y \ + * --kconfig_add CONFIG_UBSAN=y \ + * --kconfig_add CONFIG_UBSAN_TRAP=y \ + * --kconfig_add CONFIG_UBSAN_BOUNDS=y \ + * --kconfig_add CONFIG_UBSAN_LOCAL_BOUNDS=y \ + * --make_options LLVM=1 fortify + */ +#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt + +#include <kunit/test.h> +#include <linux/string.h> +#include <linux/init.h> + +static const char array_of_10[] = "this is 10"; +static const char *ptr_of_11 = "this is 11!"; +static char array_unknown[] = "compiler thinks I might change"; + +static void known_sizes_test(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, __compiletime_strlen("88888888"), 8); + KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_of_10), 10); + KUNIT_EXPECT_EQ(test, __compiletime_strlen(ptr_of_11), 11); + + KUNIT_EXPECT_EQ(test, __compiletime_strlen(array_unknown), SIZE_MAX); + /* Externally defined and dynamically sized string pointer: */ + KUNIT_EXPECT_EQ(test, __compiletime_strlen(saved_command_line), SIZE_MAX); +} + +/* This is volatile so the optimizer can't perform DCE below. */ +static volatile int pick; + +/* Not inline to keep optimizer from figuring out which string we want. */ +static noinline size_t want_minus_one(int pick) +{ + const char *str; + + switch (pick) { + case 1: + str = "4444"; + break; + case 2: + str = "333"; + break; + default: + str = "1"; + break; + } + return __compiletime_strlen(str); +} + +static void control_flow_split_test(struct kunit *test) +{ + KUNIT_EXPECT_EQ(test, want_minus_one(pick), SIZE_MAX); +} + +static struct kunit_case fortify_test_cases[] = { + KUNIT_CASE(known_sizes_test), + KUNIT_CASE(control_flow_split_test), + {} +}; + +static struct kunit_suite fortify_test_suite = { + .name = "fortify", + .test_cases = fortify_test_cases, +}; + +kunit_test_suite(fortify_test_suite); + +MODULE_LICENSE("GPL");
Add lib/fortify_kunit.c KUnit test for checking the expected behavioral characteristics of FORTIFY_SOURCE internals. Cc: Nick Desaulniers <ndesaulniers@google.com> Cc: Nathan Chancellor <nathan@kernel.org> Cc: Tom Rix <trix@redhat.com> Cc: Andrew Morton <akpm@linux-foundation.org> Cc: Vlastimil Babka <vbabka@suse.cz> Cc: "Steven Rostedt (Google)" <rostedt@goodmis.org> Cc: David Gow <davidgow@google.com> Cc: Yury Norov <yury.norov@gmail.com> Cc: Masami Hiramatsu <mhiramat@kernel.org> Cc: Sander Vanheule <sander@svanheule.net> Cc: linux-hardening@vger.kernel.org Cc: llvm@lists.linux.dev Signed-off-by: Kees Cook <keescook@chromium.org> --- MAINTAINERS | 1 + lib/Kconfig.debug | 9 ++++++ lib/Makefile | 1 + lib/fortify_kunit.c | 77 +++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 88 insertions(+) create mode 100644 lib/fortify_kunit.c