]> git.ipfire.org Git - thirdparty/kernel/linux.git/commitdiff
ptrace: add ptracer_access_allowed()
authorChristian Brauner (Amutable) <brauner@kernel.org>
Wed, 20 May 2026 21:48:54 +0000 (23:48 +0200)
committerChristian Brauner <brauner@kernel.org>
Tue, 26 May 2026 09:02:01 +0000 (11:02 +0200)
Add a helper that encapsulates all of the logic for checking ptrace
access and remove open-coded versions in follow-up patches.

Reviewed-by: Jann Horn <jannh@google.com>
Reviewed-by: David Hildenbrand (arm) <david@kernel.org>
Link: https://patch.msgid.link/20260520-work-task_exec_state-v3-3-69f895bc1385@kernel.org
Signed-off-by: Christian Brauner (Amutable) <brauner@kernel.org>
include/linux/ptrace.h
kernel/ptrace.c

index 90507d4afcd6debb80eef494c4c874c8a1732e49..ef314f7a9ecc500a38dc40e76ef23b26d4c6f8b4 100644 (file)
@@ -17,6 +17,7 @@ struct syscall_info {
        struct seccomp_data     data;
 };
 
+bool ptracer_access_allowed(struct task_struct *tsk);
 extern int ptrace_access_vm(struct task_struct *tsk, unsigned long addr,
                            void *buf, int len, unsigned int gup_flags);
 
index 07398c9c8fe30161041dfaec345de3edc381515d..4be5e718db03c041c05edd7f80212babe2e39ba4 100644 (file)
@@ -13,6 +13,7 @@
 #include <linux/sched.h>
 #include <linux/sched/mm.h>
 #include <linux/sched/coredump.h>
+#include <linux/sched/exec_state.h>
 #include <linux/sched/task.h>
 #include <linux/errno.h>
 #include <linux/mm.h>
 
 #include <asm/syscall.h>       /* for syscall_get_* */
 
+/**
+ * ptracer_access_allowed - may current peek/poke @tsk's address space?
+ * @tsk: tracee
+ *
+ * Per-access check used by ptrace_access_vm() and architecture-specific
+ * tag/register accessors.  Returns true iff current is the registered
+ * ptracer of @tsk and either @tsk is owner-dumpable or current holds
+ * CAP_SYS_PTRACE in @tsk's exec namespace.  Lighter than
+ * __ptrace_may_access(): it re-validates only dumpability and
+ * capability on every access, without re-running LSM hooks or
+ * cred_cap_issubset() checks performed at attach time.
+ */
+bool ptracer_access_allowed(struct task_struct *tsk)
+{
+       const struct task_exec_state *es;
+
+       guard(rcu)();
+       if (ptrace_parent(tsk) != current)
+               return false;
+       es = task_exec_state_rcu(tsk);
+       return READ_ONCE(es->dumpable) == TASK_DUMPABLE_OWNER ||
+              ptracer_capable(tsk, es->user_ns);
+}
+
 /*
  * Access another process' address space via ptrace.
  * Source/target buffer must be kernel space,