Add struct ksw_config and ksw_parse_config() to parse user string. Update `Makefile` to pass compilation.
Signed-off-by: Jinchao Wang <wangjinchao...@gmail.com> --- mm/kstackwatch/Makefile | 2 ++ mm/kstackwatch/kernel.c | 70 +++++++++++++++++++++++++++++++++++- mm/kstackwatch/kstackwatch.h | 34 ++++++++++++++++++ 3 files changed, 105 insertions(+), 1 deletion(-) diff --git a/mm/kstackwatch/Makefile b/mm/kstackwatch/Makefile index 84a46cb9a766..d422f0e114dd 100644 --- a/mm/kstackwatch/Makefile +++ b/mm/kstackwatch/Makefile @@ -1,2 +1,4 @@ obj-$(CONFIG_KSTACK_WATCH) += kstackwatch.o kstackwatch-y := kernel.o stack.o watch.o + +CFLAGS_kernel.o := -Wno-error=unused-function diff --git a/mm/kstackwatch/kernel.c b/mm/kstackwatch/kernel.c index 93379a0a0f7e..4a6dc49449fe 100644 --- a/mm/kstackwatch/kernel.c +++ b/mm/kstackwatch/kernel.c @@ -1,11 +1,79 @@ // SPDX-License-Identifier: GPL-2.0 - +#include <linux/kstrtox.h> #include <linux/module.h> +#include <linux/string.h> + +#include "kstackwatch.h" MODULE_AUTHOR("Jinchao Wang"); MODULE_DESCRIPTION("Kernel Stack Watch"); MODULE_LICENSE("GPL"); +/* + * Format of the configuration string: + * function+ip_offset[+depth] [local_var_offset:local_var_len] + * + * - function : name of the target function + * - ip_offset : instruction pointer offset within the function + * - depth : recursion depth to watch + * - local_var_offset : offset from the stack pointer at function+ip_offset + * - local_var_len : length of the local variable + */ +static int ksw_parse_config(char *buf, struct ksw_config *config) +{ + char *func_part, *local_var_part = NULL; + char *token; + + /* Set the watch type to the default canary-based monitoring */ + config->type = WATCH_CANARY; + + func_part = strim(buf); + strscpy(config->config_str, func_part, MAX_CONFIG_STR_LEN); + + local_var_part = strchr(func_part, ' '); + if (local_var_part) { + *local_var_part = '\0'; // Terminate the function part + local_var_part = strim(local_var_part + 1); + } + + /* 1. Parse the function part: function+ip_offset[+depth] */ + token = strsep(&func_part, "+"); + if (!token) + return -EINVAL; + + strscpy(config->function, token, MAX_FUNC_NAME_LEN - 1); + + token = strsep(&func_part, "+"); + if (!token || kstrtou16(token, 0, &config->ip_offset)) { + pr_err("KSW: failed to parse instruction offset\n"); + return -EINVAL; + } + + token = strsep(&func_part, "+"); + if (token && kstrtou16(token, 0, &config->depth)) { + pr_err("KSW: failed to parse depth\n"); + return -EINVAL; + } + if (!local_var_part || !(*local_var_part)) + return 0; + + /* 2. Parse the optional local var: offset:len */ + config->type = WATCH_LOCAL_VAR; + token = strsep(&local_var_part, ":"); + if (!token || kstrtou16(token, 0, &config->local_var_offset)) { + pr_err("KSW: failed to parse stack variable offset\n"); + return -EINVAL; + } + + if (!local_var_part || + kstrtou16(local_var_part, 0, &config->local_var_len)) { + pr_err("KSW: failed to parse stack variable length\n"); + return -EINVAL; + } + + return 0; +} + static int __init kstackwatch_init(void) { pr_info("KSW: module loaded\n"); diff --git a/mm/kstackwatch/kstackwatch.h b/mm/kstackwatch/kstackwatch.h index 0273ef478a26..b5f1835586c1 100644 --- a/mm/kstackwatch/kstackwatch.h +++ b/mm/kstackwatch/kstackwatch.h @@ -2,4 +2,38 @@ #ifndef _KSTACKWATCH_H #define _KSTACKWATCH_H +#include <linux/types.h> + +#define MAX_FUNC_NAME_LEN 64 +#define MAX_CONFIG_STR_LEN 128 +#define MAX_FRAME_SEARCH 128 + +enum watch_type { + WATCH_CANARY = 0, + WATCH_LOCAL_VAR, +}; + +struct ksw_config { + /* function part */ + char function[MAX_FUNC_NAME_LEN]; + u16 ip_offset; + u16 depth; + + /* local var, useless for canary watch */ + /* offset from rsp at function+ip_offset */ + u16 local_var_offset; + + /* + * local var size (1,2,4,8 bytes) + * it will be the watching len + */ + u16 local_var_len; + + /* easy for understand*/ + enum watch_type type; + + /* save to show */ + char config_str[MAX_CONFIG_STR_LEN]; +}; + #endif /* _KSTACKWATCH_H */ -- 2.43.0