4 ms·
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(
by jacobparker 8y ago
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/pid_namespaces.7.html
* http://man7.org/linux/man-pages/man7/user_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/man7/namespaces.7.html
* http://man7.org/linux/man-pages/man2/clone.2.html http://man7.org/linux/man-pages/man2/clone.2.html
- mehrdadn 8y agoWindows has job objects that work nicely for this purpose. :)
- lostmsu 8y agoIsn't their purpose basically the same as process groups?
- mehrdadn 8y agoI 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.
- cyphar 8y agoIn 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 https://github.com/rootless-containers/rootlesskit
- Liskni_si 8y agoYou might also want to have a look at bwrap [1], a handy little sandboxing tool for unprivileged users. [1]: https://github.com/projectatomic/bubblewrap https://github.com/projectatomic/bubblewrap