ptrace: Remove arch_ptrace_attach
authorEric W. Biederman <ebiederm@xmission.com>
Fri, 29 Apr 2022 16:09:11 +0000 (11:09 -0500)
committerEric W. Biederman <ebiederm@xmission.com>
Wed, 11 May 2022 19:33:54 +0000 (14:33 -0500)
The last remaining implementation of arch_ptrace_attach is ia64's
ptrace_attach_sync_user_rbs which was added at the end of 2007 in
commit aa91a2e90044 ("[IA64] Synchronize RBS on PTRACE_ATTACH").

Reading the comments and examining the code ptrace_attach_sync_user_rbs
has the sole purpose of saving registers to the stack when ptrace_attach
changes TASK_STOPPED to TASK_TRACED.  In all other cases arch_ptrace_stop
takes care of the register saving.

In commit d79fdd6d96f4 ("ptrace: Clean transitions between TASK_STOPPED and TRACED")
modified ptrace_attach to wake up the thread and enter ptrace_stop normally even
when the thread starts out stopped.

This makes ptrace_attach_sync_user_rbs completely unnecessary.  So just
remove it.

I read through the code to verify that ptrace_attach_sync_user_rbs is
unnecessary.  What I found is that the code is quite dead.

Reading ptrace_attach_sync_user_rbs it is easy to see that the it does
nothing unless __state == TASK_STOPPED.

Calling arch_ptrace_attach (aka ptrace_attach_sync_user_rbs) after
ptrace_traceme it is easy to see that because we are talking about the
current process the value of __state is TASK_RUNNING.  Which means
ptrace_attach_sync_user_rbs does nothing.

The only other call of arch_ptrace_attach (aka
ptrace_attach_sync_user_rbs) is after ptrace_attach.

If the task is running (and PTRACE_SEIZE is not specified), a SIGSTOP
is sent which results in do_signal_stop setting JOBCTL_TRAP_STOP on
the target task (as it is ptraced) and the target task stopping
in ptrace_stop with __state == TASK_TRACED.

If the task was already stopped then ptrace_attach sets
JOBCTL_TRAPPING and JOBCTL_TRAP_STOP, wakes it out of __TASK_STOPPED,
and waits until the JOBCTL_TRAPPING_BIT is clear.  At which point
the task stops in ptrace_stop.

In both cases there are a couple of funning excpetions such as if the
traced task receiveds a SIGCONT, or is set a fatal signal.

However in all of those cases the tracee never stops in __state
TASK_STOPPED.  Which is a long way of saying that ptrace_attach_sync_user_rbs
is guaranteed never to do anything.

Cc: linux-ia64@vger.kernel.org
Tested-by: Kees Cook <keescook@chromium.org>
Reviewed-by: Oleg Nesterov <oleg@redhat.com>
Link: https://lkml.kernel.org/r/20220505182645.497868-4-ebiederm@xmission.com
Signed-off-by: "Eric W. Biederman" <ebiederm@xmission.com>
arch/ia64/include/asm/ptrace.h
arch/ia64/kernel/ptrace.c
kernel/ptrace.c

index a10a498eede1bac7958af7efe7cb29e083069126..402874489890aa35674aa677a47d315acce233b0 100644 (file)
@@ -139,10 +139,6 @@ static inline long regs_return_value(struct pt_regs *regs)
   #define arch_ptrace_stop_needed() \
        (!test_thread_flag(TIF_RESTORE_RSE))
 
-  extern void ptrace_attach_sync_user_rbs (struct task_struct *);
-  #define arch_ptrace_attach(child) \
-       ptrace_attach_sync_user_rbs(child)
-
   #define arch_has_single_step()  (1)
   #define arch_has_block_step()   (1)
 
index a19acd9f5e1f6ce0399d8e0e4d2a5afa036d38f3..a45f529046c39c45a75c844f39c70ddc830c753a 100644 (file)
@@ -617,63 +617,6 @@ void ia64_sync_krbs(void)
        unw_init_running(do_sync_rbs, ia64_sync_kernel_rbs);
 }
 
-/*
- * After PTRACE_ATTACH, a thread's register backing store area in user
- * space is assumed to contain correct data whenever the thread is
- * stopped.  arch_ptrace_stop takes care of this on tracing stops.
- * But if the child was already stopped for job control when we attach
- * to it, then it might not ever get into ptrace_stop by the time we
- * want to examine the user memory containing the RBS.
- */
-void
-ptrace_attach_sync_user_rbs (struct task_struct *child)
-{
-       int stopped = 0;
-       struct unw_frame_info info;
-
-       /*
-        * If the child is in TASK_STOPPED, we need to change that to
-        * TASK_TRACED momentarily while we operate on it.  This ensures
-        * that the child won't be woken up and return to user mode while
-        * we are doing the sync.  (It can only be woken up for SIGKILL.)
-        */
-
-       read_lock(&tasklist_lock);
-       if (child->sighand) {
-               spin_lock_irq(&child->sighand->siglock);
-               if (READ_ONCE(child->__state) == TASK_STOPPED &&
-                   !test_and_set_tsk_thread_flag(child, TIF_RESTORE_RSE)) {
-                       set_notify_resume(child);
-
-                       WRITE_ONCE(child->__state, TASK_TRACED);
-                       stopped = 1;
-               }
-               spin_unlock_irq(&child->sighand->siglock);
-       }
-       read_unlock(&tasklist_lock);
-
-       if (!stopped)
-               return;
-
-       unw_init_from_blocked_task(&info, child);
-       do_sync_rbs(&info, ia64_sync_user_rbs);
-
-       /*
-        * Now move the child back into TASK_STOPPED if it should be in a
-        * job control stop, so that SIGCONT can be used to wake it up.
-        */
-       read_lock(&tasklist_lock);
-       if (child->sighand) {
-               spin_lock_irq(&child->sighand->siglock);
-               if (READ_ONCE(child->__state) == TASK_TRACED &&
-                   (child->signal->flags & SIGNAL_STOP_STOPPED)) {
-                       WRITE_ONCE(child->__state, TASK_STOPPED);
-               }
-               spin_unlock_irq(&child->sighand->siglock);
-       }
-       read_unlock(&tasklist_lock);
-}
-
 /*
  * Write f32-f127 back to task->thread.fph if it has been modified.
  */
index ccc4b465775b82b78267d6f96f56097c07eabd9c..da30dcd477a03f3dcd08782e938e035ac11f7e37 100644 (file)
@@ -1285,10 +1285,6 @@ int ptrace_request(struct task_struct *child, long request,
        return ret;
 }
 
-#ifndef arch_ptrace_attach
-#define arch_ptrace_attach(child)      do { } while (0)
-#endif
-
 SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
                unsigned long, data)
 {
@@ -1297,8 +1293,6 @@ SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
 
        if (request == PTRACE_TRACEME) {
                ret = ptrace_traceme();
-               if (!ret)
-                       arch_ptrace_attach(current);
                goto out;
        }
 
@@ -1310,12 +1304,6 @@ SYSCALL_DEFINE4(ptrace, long, request, long, pid, unsigned long, addr,
 
        if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
                ret = ptrace_attach(child, request, addr, data);
-               /*
-                * Some architectures need to do book-keeping after
-                * a ptrace attach.
-                */
-               if (!ret)
-                       arch_ptrace_attach(child);
                goto out_put_task_struct;
        }
 
@@ -1455,12 +1443,6 @@ COMPAT_SYSCALL_DEFINE4(ptrace, compat_long_t, request, compat_long_t, pid,
 
        if (request == PTRACE_ATTACH || request == PTRACE_SEIZE) {
                ret = ptrace_attach(child, request, addr, data);
-               /*
-                * Some architectures need to do book-keeping after
-                * a ptrace attach.
-                */
-               if (!ret)
-                       arch_ptrace_attach(child);
                goto out_put_task_struct;
        }