News for hackers. Stuff that matters.
Re:Killing processes that don't want to die by jacobparker on Monday September 24, 2018 @10:13PM (#18063055)
Fun article!

A modern way to do this on Linux (specifically) is to use PID-namespaces for isolation.

You can pass CLONE_NEWPID to the flags argument of clone(2) so that the new process is PID 1 in a new PID-namespace. One of the special properties of PID 1 is that when it dies all of other processes in the PID-namespace will receive SIGKILL.

So, create a new "shim" process in a new PID-namespace and have that process fork&exec the rogue program (it's probably better to fork&exec than just exec because of the special properties of PID 1 that the rogue programming you're running may not be built to handle.)

If you want to kill the rogue program, just kill your shim.

You need to be root (have CAP_SYS_ADMIN) to create a new PID-namespace. If you're not (or more likely don't want to be) you can first create a new user-namespace with the CLONE_NEWUSER flag. You'll be root in that user namespace and can create a new PID-namespace. You'll probably also want to launch the rogue program as a non-root user though, so make sure to figure that out.

Docker does this sort of stuff.

man pages:

* http://man7.org/linux/man-pages/man7/pid_namespaces.7.html

* http://man7.org/linux/man-pages/man7/user_namespaces.7.html

* http://man7.org/linux/man-pages/man7/namespaces.7.html

* http://man7.org/linux/man-pages/man2/clone.2.html

Re:Killing processes that don't want to die by cyphar on Tuesday September 25, 2018 @01:11AM (#18063761)
In a shell you can do this with `unshare -prf` which will create an unprivileged user namespace (mapping only yourself -- if you want more complicated stuff you'll need to use rootlesskit[1] or a shell script). Note that most programs really don't like being in an unprivileged user namespace (something the rootless-containers project has been working on fixing so you can have completely unprivileged containers in every sense of the word "just work").

[1]: https://github.com/rootless-containers/rootlesskit

Re:Killing processes that don't want to die by Liskni_si on Wednesday September 26, 2018 @12:11PM (#18077395)
You might also want to have a look at bwrap [1], a handy little sandboxing tool for unprivileged users.

[1]: https://github.com/projectatomic/bubblewrap

Re:Killing processes that don't want to die by mehrdadn on Monday September 24, 2018 @11:42PM (#18063404)
Windows has job objects that work nicely for this purpose. :)
Re:Killing processes that don't want to die by lostmsu on Tuesday September 25, 2018 @12:26AM (#18063595)
Isn't their purpose basically the same as process groups?
Re:Killing processes that don't want to die by mehrdadn on Tuesday September 25, 2018 @02:58AM (#18064185)
I think they're similar, yeah, though they seem more general/orthogonal... e.g. they allow a process to be associated with multiple jobs (or so I think -- I've never needed to associate 1 process with multiple jobs), and this is independent of whether the process is detached and starts a new hierarchy or not.