public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Roman Kisel <romank@linux.microsoft.com>
To: oleg@redhat.com, linux-kernel@vger.kernel.org
Cc: apais@microsoft.com, benhill@microsoft.com,
	ssengar@microsoft.com, sunilmut@microsoft.com, vdso@hexbites.dev
Subject: [PATCH 1/1] ptrace: Get tracer PID without reliance on the proc FS
Date: Thu,  5 Sep 2024 14:27:41 -0700	[thread overview]
Message-ID: <20240905212741.143626-2-romank@linux.microsoft.com> (raw)
In-Reply-To: <20240905212741.143626-1-romank@linux.microsoft.com>

For debugging, it might be useful to run the debug trap
instruction to break into the debugger. To detect the debugger
presence, the kernel provides the `/proc/self/status` pseudo-file
that needs to be searched for the "TracerPid:" string.

Provide a prctl command that returns the PID of the tracer if any.
That allows for much simpler logic in the user land, and makes it
possible to detect tracer presence even if PROC_FS is not enabled.

Signed-off-by: Roman Kisel <romank@linux.microsoft.com>
---
 include/uapi/linux/ptrace.h |  1 +
 kernel/ptrace.c             | 11 ++++++++++-
 2 files changed, 11 insertions(+), 1 deletion(-)

diff --git a/include/uapi/linux/ptrace.h b/include/uapi/linux/ptrace.h
index 72c038fc71d0..5056f5d1df6b 100644
--- a/include/uapi/linux/ptrace.h
+++ b/include/uapi/linux/ptrace.h
@@ -21,6 +21,7 @@
 
 #define PTRACE_ATTACH		  16
 #define PTRACE_DETACH		  17
+#define PTRACE_TRACER		  18
 
 #define PTRACE_SYSCALL		  24
 
diff --git a/kernel/ptrace.c b/kernel/ptrace.c
index d5f89f9ef29f..91275c5c4f57 100644
--- a/kernel/ptrace.c
+++ b/kernel/ptrace.c
@@ -1258,7 +1258,7 @@ int ptrace_request(struct task_struct *child, long request,
 SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
 		unsigned long, data)
 {
-	struct task_struct *child;
+	struct task_struct *child, *tracer;
 	long ret;
 
 	if (request == PTRACE_TRACEME) {
@@ -1277,6 +1277,15 @@ SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
 		goto out_put_task_struct;
 	}
 
+	if (request == PTRACE_TRACER) {
+		rcu_read_lock();
+		tracer = ptrace_parent(current);
+		ret = tracer ? task_pid_nr_ns(tracer,
+					task_active_pid_ns(current->parent)) : -ESRCH;
+		rcu_read_unlock();
+		goto out;
+	}
+
 	ret = ptrace_check_attach(child, request == PTRACE_KILL ||
 				  request == PTRACE_INTERRUPT);
 	if (ret < 0)
-- 
2.34.1


  reply	other threads:[~2024-09-05 21:27 UTC|newest]

Thread overview: 29+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-09-05 21:27 [PATCH 0/1] Get tracer PID without reliance on the proc FS Roman Kisel
2024-09-05 21:27 ` Roman Kisel [this message]
2024-09-06 11:24   ` [PATCH 1/1] ptrace: " Oleg Nesterov
2024-09-06 11:48     ` Oleg Nesterov
2024-09-06 19:09     ` Linus Torvalds
2024-09-06 20:08       ` Roman Kisel
2024-09-06 20:26         ` Linus Torvalds
2024-09-06 21:15           ` Roman Kisel
2024-09-09 16:18             ` Eric W. Biederman
2024-09-09 17:05               ` Oleg Nesterov
2024-09-09 17:34                 ` Eric W. Biederman
2024-09-09 17:22               ` Roman Kisel
2024-09-06 20:55         ` Oleg Nesterov
2024-09-06 21:25           ` Roman Kisel
2024-09-08 14:08             ` Oleg Nesterov
2024-09-09 15:19               ` Roman Kisel
2024-09-09 16:42                 ` Oleg Nesterov
2024-09-09 17:05                   ` Roman Kisel
2024-09-07 19:33   ` kernel test robot
  -- strict thread matches above, loose matches on Subject: below --
2024-09-07  8:45 Jubilee Young
2024-09-09 19:37 ` Oleg Nesterov
2024-09-10 15:40   ` Roman Kisel
2024-09-11 14:44     ` Oleg Nesterov
2024-09-11 17:41       ` Roman Kisel
2024-09-11 19:53         ` Oleg Nesterov
2024-09-11 19:57           ` Linus Torvalds
2024-09-11 20:14             ` Oleg Nesterov
2024-09-11 20:25           ` Roman Kisel
2024-09-10 16:34 ` Eric W. Biederman

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=20240905212741.143626-2-romank@linux.microsoft.com \
    --to=romank@linux.microsoft.com \
    --cc=apais@microsoft.com \
    --cc=benhill@microsoft.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=oleg@redhat.com \
    --cc=ssengar@microsoft.com \
    --cc=sunilmut@microsoft.com \
    --cc=vdso@hexbites.dev \
    /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