Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Deal with inheriting traced processes. #16

Merged
merged 1 commit into from
Aug 6, 2020

Conversation

peadar
Copy link
Member

@peadar peadar commented Aug 5, 2020

If a debugger exits and leaves its child traced, then init inherits the
tracing of the process as well as becoming its parent.

Change our wait behaviour to detach from such children, and forward them
the signal that caused them to be stopped.

This stops us accumulating zombies if a debugger doesn't clean up on exit

init.c Outdated
* explicitly asking for that notification, detach from
* it, forwarding WSTOPSIG.
*/
int rc = ptrace(PTRACE_DETACH, pid, 0, WSTOPSIG(status));
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Isn't the data arg of ptrace ignored with PTRACE_DETACH? what's the purpose of passing in WSTOPSIG(status)?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No - "addr" is ignored. "data" is the signal you want to send to the target.

       PTRACE_DETACH
              Restart the stopped tracee as for PTRACE_CONT, but first detach from it.  Under Linux, a tracee can be detached in this way regardless  of
              which method was used to initiate tracing.  (addr is ignored.)

And:

       PTRACE_CONT
              Restart the stopped tracee process.  If data is nonzero, it is interpreted as the number of a signal to be delivered to the tracee; other‐
              wise, no signal is delivered.  Thus, for example, the tracer can control whether a signal sent to the tracee is delivered or  not.   (addr
              is ignored.)

init.c Outdated Show resolved Hide resolved
If a debugger exits and leaves its child traced, then init inherits the
tracing of the process as well as becoming its parent.

Change our wait behaviour to detach from such children, and forward them
the signal that caused them to be stopped.
@peadar
Copy link
Member Author

peadar commented Aug 5, 2020

BTW: I checked this because dumb-init's behaviour is to abort when this happens, killing the container.
It appears systemd's init also leaves the process attached to the init, and renders it unkillable except with kill -9. So if you want to discard this, it'll be no worse than systemd.
Here's what I used to repro:


#include <assert.h>
#include <stdarg.h>
#include <errno.h>
#include <iostream>
#include <unistd.h>
#include <err.h>
#include <string.h>

#include <sys/ptrace.h>
#include <sys/wait.h>

void
okOrExit(int value, int expected, const char *reason, ...) {
   if (value != expected) {
      va_list args; va_start(args, reason);
      verr(1, reason, args);
   }
}

void sighandle(int) {}
static int child() {
   okOrExit(ptrace(PTRACE_TRACEME, 0, 0), 0, "ptrace(PTRACE_TRACEME) failed");      
   signal(SIGALRM, sighandle);
   okOrExit(alarm(4), 0, "alarm failed");
   std::clog << "pausing...\n";
   int rc = pause();
   std::clog << "pause returned " << rc << "/" << strerror(errno) << std::endl;     
   return 0;
}

static int parent(pid_t childpid) {
   int status;
   okOrExit(ptrace(PTRACE_ATTACH, childpid, 0, 0), 0, "ptrace attach failed");      
   int rc = waitpid(childpid, &status, 0);
   okOrExit(rc, childpid, "waitpid for %d got %d", childpid, rc);
   std::clog << "child continued\n";
   return 0;
}

int main() {
   pid_t childpid;
   switch (childpid = fork()) {
      case  0: return child();
      default: return parent(childpid);
      case -1: return err(1, "fork failed"), 1;
   } 
}

@Snaipe Snaipe merged commit 2bd421d into aristanetworks:master Aug 6, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants