linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jinchao Wang <wangjinchao600@gmail.com>
To: Andrew Morton <akpm@linux-foundation.org>,
	Masami Hiramatsu <mhiramat@kernel.org>,
	"Naveen N . Rao" <naveen@kernel.org>,
	linux-mm@kvack.org, linux-trace-kernel@vger.kernel.org
Cc: linux-kernel@vger.kernel.org, Jinchao Wang <wangjinchao600@gmail.com>
Subject: [PATCH 08/17] mm/ksw: implement stack canary and local var resolution logic
Date: Thu, 28 Aug 2025 15:32:41 +0800	[thread overview]
Message-ID: <20250828073311.1116593-9-wangjinchao600@gmail.com> (raw)
In-Reply-To: <20250828073311.1116593-1-wangjinchao600@gmail.com>

Implement logic to resolve stack watch target for kstackwatch:
 - Locate the stack canary within the current frame
 - Resolve local variable offsets relative to the stack pointer
 - Validate addresses against current task's stack bounds

This logic prepares watch addr and len for use in kprobe/fprobe handlers,
enabling dynamic stack monitoring.

Signed-off-by: Jinchao Wang <wangjinchao600@gmail.com>
---
 mm/kstackwatch/stack.c | 102 +++++++++++++++++++++++++++++++++++++++--
 1 file changed, 99 insertions(+), 3 deletions(-)

diff --git a/mm/kstackwatch/stack.c b/mm/kstackwatch/stack.c
index 3b72177315cc..1d9814a58fde 100644
--- a/mm/kstackwatch/stack.c
+++ b/mm/kstackwatch/stack.c
@@ -1,22 +1,118 @@
 // SPDX-License-Identifier: GPL-2.0
 
 #include <linux/fprobe.h>
+#include <linux/interrupt.h>
 #include <linux/kprobes.h>
+#include <linux/percpu.h>
 #include <linux/sched.h>
 #include <linux/spinlock.h>
+#include <linux/stackprotector.h>
 
 #include "kstackwatch.h"
 
 struct ksw_config *probe_config;
 
+/* Find canary address in current stack frame */
+static unsigned long ksw_stack_find_canary(struct pt_regs *regs)
+{
+	unsigned long *stack_ptr, *stack_end;
+	unsigned long expected_canary;
+	unsigned int i;
+
+	if (!regs || !regs->sp)
+		return 0;
+
+	stack_ptr = (unsigned long *)regs->sp;
+	stack_end =
+		(unsigned long *)current->stack + THREAD_SIZE / sizeof(long);
+	expected_canary = current->stack_canary;
+
+	for (i = 0; i < MAX_FRAME_SEARCH && &stack_ptr[i] < stack_end; i++) {
+		if (stack_ptr[i] == expected_canary) {
+			pr_info("KSW: canary found i:%d 0x%px\n", i,
+				&stack_ptr[i]);
+			return (unsigned long)&stack_ptr[i];
+		}
+	}
+
+	return 0;
+}
+
+/* Resolve stack offset to actual address */
+static unsigned long ksw_stack_resolve_offset(struct pt_regs *regs,
+					      s64 local_var_offset)
+{
+	unsigned long stack_base;
+	unsigned long target_addr;
+
+	if (!regs)
+		return 0;
+
+	/* Use stack pointer as base for offset calculation */
+	stack_base = regs->sp;
+	target_addr = stack_base + local_var_offset;
+
+	pr_debug("KSW: stack resolve offset target: 0x%lx\n", target_addr);
+
+	return target_addr;
+}
+
+/* Validate that address is within current stack bounds */
+static int ksw_stack_validate_addr(unsigned long addr, size_t size)
+{
+	unsigned long stack_start, stack_end;
+
+	if (!addr || !size)
+		return -EINVAL;
+
+	stack_start = (unsigned long)current->stack;
+	stack_end = stack_start + THREAD_SIZE;
+
+	if (addr < stack_start || (addr + size) > stack_end) {
+		pr_warn("KSW: address 0x%lx (size %zu) outside stack bounds [0x%lx-0x%lx]\n",
+			addr, size, stack_start, stack_end);
+		return -ERANGE;
+	}
+
+	return 0;
+}
+
 /* prepare watch_addr and watch_len for watch */
 static int ksw_stack_prepare_watch(struct pt_regs *regs,
 				   struct ksw_config *config, u64 *watch_addr,
 				   u64 *watch_len)
 {
-	/* TODO: implement logic */
-	*watch_addr = 0;
-	*watch_len = 0;
+	u64 addr;
+	u64 len;
+
+	/* Resolve addresses for all active watches */
+	switch (config->type) {
+	case WATCH_CANARY:
+		addr = ksw_stack_find_canary(regs);
+		len = 8;
+		break;
+
+	case WATCH_LOCAL_VAR:
+		addr = ksw_stack_resolve_offset(regs, config->local_var_offset);
+		if (!addr) {
+			pr_err("KSW: invalid stack var offset %u\n",
+			       config->local_var_offset);
+			return -EINVAL;
+		}
+		if (ksw_stack_validate_addr(addr, config->local_var_len)) {
+			pr_err("KSW: invalid stack var len %u\n",
+			       config->local_var_len);
+		}
+		len = config->local_var_len;
+		break;
+
+	default:
+		pr_warn("KSW: Unknown watch type %d\n", config->type);
+		return -EINVAL;
+	}
+
+	*watch_addr = addr;
+	*watch_len = len;
 	return 0;
 }
 
-- 
2.43.0


  parent reply	other threads:[~2025-08-28  7:34 UTC|newest]

Thread overview: 18+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2025-08-28  7:32 [PATCH 00/17] mm/ksw: Introduce real-time Kernel Stack Watch debugging tool Jinchao Wang
2025-08-28  7:32 ` [PATCH 01/17] mm/ksw: add build system support Jinchao Wang
2025-08-28  7:32 ` [PATCH 02/17] mm/ksw: add ksw_config struct and parser Jinchao Wang
2025-08-28  7:32 ` [PATCH 03/17] mm/ksw: add /proc/kstackwatch interface Jinchao Wang
2025-08-28  7:32 ` [PATCH 04/17] mm/ksw: add HWBP pre-allocation support Jinchao Wang
2025-08-28  7:32 ` [PATCH 05/17] x86/HWBP: introduce arch_reinstall_hw_breakpoint() for atomic context Jinchao Wang
2025-08-28  7:32 ` [PATCH 06/17] mm/ksw: add atomic watch on/off operations Jinchao Wang
2025-08-28  7:32 ` [PATCH 07/17] mm/ksw: add stack probe support Jinchao Wang
2025-08-28  7:32 ` Jinchao Wang [this message]
2025-08-28  7:32 ` [PATCH 09/17] mm/ksw: add per-task recursion depth tracking Jinchao Wang
2025-08-28  7:32 ` [PATCH 10/17] mm/ksw: coordinate watch and stack for full functionality Jinchao Wang
2025-08-28  7:32 ` [PATCH 11/17] mm/ksw: add self-debug functions for kstackwatch watch Jinchao Wang
2025-08-28  7:32 ` [PATCH 12/17] mm/ksw: add test module Jinchao Wang
2025-08-28  7:32 ` [PATCH 13/17] mm/ksw: add stack overflow test Jinchao Wang
2025-08-28  7:32 ` [PATCH 14/17] mm/ksw: add simplified silent corruption test Jinchao Wang
2025-08-28  7:32 ` [PATCH 15/17] mm/ksw: add recursive " Jinchao Wang
2025-08-28  7:32 ` [PATCH 16/17] tools/kstackwatch: add interactive test script for KStackWatch Jinchao Wang
2025-08-28  7:32 ` [PATCH 17/17] MAINTAINERS: add entry for KStackWatch (Kernel Stack Watch) Jinchao Wang

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20250828073311.1116593-9-wangjinchao600@gmail.com \
    --to=wangjinchao600@gmail.com \
    --cc=akpm@linux-foundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-mm@kvack.org \
    --cc=linux-trace-kernel@vger.kernel.org \
    --cc=mhiramat@kernel.org \
    --cc=naveen@kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).