nelhage / reptyr

Reparent a running program to a new terminal
MIT License
5.71k stars 216 forks source link

Update ptrace instructions for linux systems #119

Closed dosy4ev closed 3 years ago

dosy4ev commented 3 years ago

Using setcap to enable ptrace calls for reptyr is more secure way than system wide configuration.

nelhage commented 3 years ago

Thanks for the PR! Unfortunately, this change has the effect of allowing reptyr to attach to any process on the system; CAP_SYS_PTRACE allows tracing any process.

I tested on a development machine, and with that fscap I was able to attach (with -s) to a root shell, confirming that this advice opens up a privilege escalation vulnerability. We could in principle implement permission checking in reptyr itself to make this safe, but I'm not comfortable maintaining that level of attack surface in reptyr; setuid binaries are notoriously difficult to write safely, and CAP_SYS_PTRACE is effectively as powerful as setuid root, since you can ptrace a uid 0 binary and gain root that way.

kees commented 3 years ago

Would something like this help? (Totally untested...)

https://git.kernel.org/pub/scm/linux/kernel/git/kees/linux.git/log/?h=yama/devel/any_tracee

Basically, a CAP_SYS_PTRACE tracer could call prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, PR_SET_PTRACER_ANY_TRACEE) and then drop CAP_SYS_PTRACE, and be left with the privileges to bypass the ptrace ancestry requirements.

nelhage commented 3 years ago

Huh, that's an interesting proposal. A few thoughts here: