Add Kconfig and Makefile infrastructure for KStackWatch, a real-time tool for debugging kernel stack corruption.
The implementation is located under `mm/kstackwatch/`. Signed-off-by: Jinchao Wang <wangjinchao...@gmail.com> --- mm/Kconfig.debug | 10 ++++++++++ mm/Makefile | 1 + mm/kstackwatch/Makefile | 2 ++ mm/kstackwatch/kernel.c | 24 ++++++++++++++++++++++++ mm/kstackwatch/kstackwatch.h | 5 +++++ mm/kstackwatch/stack.c | 1 + mm/kstackwatch/watch.c | 1 + 7 files changed, 44 insertions(+) create mode 100644 mm/kstackwatch/Makefile create mode 100644 mm/kstackwatch/kernel.c create mode 100644 mm/kstackwatch/kstackwatch.h create mode 100644 mm/kstackwatch/stack.c create mode 100644 mm/kstackwatch/watch.c diff --git a/mm/Kconfig.debug b/mm/Kconfig.debug index 32b65073d0cc..c2cc8c7b595f 100644 --- a/mm/Kconfig.debug +++ b/mm/Kconfig.debug @@ -309,3 +309,13 @@ config PER_VMA_LOCK_STATS overhead in the page fault path. If in doubt, say N. + +config KSTACK_WATCH + tristate "Kernel Stack Watch" + depends on STACKPROTECTOR && HAVE_HW_BREAKPOINT && KPROBES && FPROBE + help + A lightweight real-time debugging tool to detect stack corruption. + It can watch either the canary or local variable and tracks + the recursive depth of the monitored function. + + If unsure, say N. diff --git a/mm/Makefile b/mm/Makefile index ef54aa615d9d..665c9f2bf987 100644 --- a/mm/Makefile +++ b/mm/Makefile @@ -92,6 +92,7 @@ obj-$(CONFIG_PAGE_POISONING) += page_poison.o obj-$(CONFIG_KASAN) += kasan/ obj-$(CONFIG_KFENCE) += kfence/ obj-$(CONFIG_KMSAN) += kmsan/ +obj-$(CONFIG_KSTACK_WATCH) += kstackwatch/ obj-$(CONFIG_FAILSLAB) += failslab.o obj-$(CONFIG_FAIL_PAGE_ALLOC) += fail_page_alloc.o obj-$(CONFIG_MEMTEST) += memtest.o diff --git a/mm/kstackwatch/Makefile b/mm/kstackwatch/Makefile new file mode 100644 index 000000000000..84a46cb9a766 --- /dev/null +++ b/mm/kstackwatch/Makefile @@ -0,0 +1,2 @@ +obj-$(CONFIG_KSTACK_WATCH) += kstackwatch.o +kstackwatch-y := kernel.o stack.o watch.o diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c new file mode 100644 index 000000000000..93379a0a0f7e --- /dev/null +++ b/mm/kstackwatch/kernel.c @@ -0,0 +1,24 @@ +// SPDX-License-Identifier: GPL-2.0 + +#include <linux/module.h> + +MODULE_AUTHOR("Jinchao Wang"); +MODULE_DESCRIPTION("Kernel Stack Watch"); +MODULE_LICENSE("GPL"); + +static int __init kstackwatch_init(void) +{ + pr_info("KSW: module loaded\n"); + pr_info("KSW: usage:\n"); + pr_info("KSW: echo 'function+ip_offset[+depth] [local_var_offset:local_var_len]' > /proc/kstackwatch\n"); + + return 0; +} + +static void __exit kstackwatch_exit(void) +{ + pr_info("KSW: Module unloaded\n"); +} + +module_init(kstackwatch_init); +module_exit(kstackwatch_exit); diff --git a/mm/kstackwatch/kstackwatch.h b/mm/kstackwatch/kstackwatch.h new file mode 100644 index 000000000000..0273ef478a26 --- /dev/null +++ b/mm/kstackwatch/kstackwatch.h @@ -0,0 +1,5 @@ +/* SPDX-License-Identifier: GPL-2.0 */ +#ifndef _KSTACKWATCH_H +#define _KSTACKWATCH_H + +#endif /* _KSTACKWATCH_H */ diff --git a/mm/kstackwatch/stack.c b/mm/kstackwatch/stack.c new file mode 100644 index 000000000000..cec594032515 --- /dev/null +++ b/mm/kstackwatch/stack.c @@ -0,0 +1 @@ +// SPDX-License-Identifier: GPL-2.0 diff --git a/mm/kstackwatch/watch.c b/mm/kstackwatch/watch.c new file mode 100644 index 000000000000..cec594032515 --- /dev/null +++ b/mm/kstackwatch/watch.c @@ -0,0 +1 @@ +// SPDX-License-Identifier: GPL-2.0 -- 2.43.0