This is an archive of the discontinued LLVM Phabricator instance.

[Linux] Add kernel.yama.ptrace_scope note when ptrace fails to attach.
ClosedPublic

Authored by rupprecht on Feb 27 2023, 12:07 PM.

Details

Summary

A common reason for LLDB failing to attach to an already-running process on Linux is the Yama security module: https://www.kernel.org/doc/Documentation/security/Yama.txt. This patch adds an explaination and suggested fix when it detects that case happening.

This was previously proposed in D106226, but hasn't been updated in a while. The last request was to put the check in a target-specific location, which is the approach this patch takes. I believe Yama only exists on Linux, so it's put in that package.

This has no end-to-end test because I'm not sure how to set ptrace_scope in a test environment -- if there are suggestions on how to do that, I'd be happy to add it. (Also, setting it to 3 is comically irreversible). I tested this locally.

Diff Detail

Event Timeline

rupprecht created this revision.Feb 27 2023, 12:07 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 27 2023, 12:07 PM
rupprecht requested review of this revision.Feb 27 2023, 12:07 PM
Herald added a project: Restricted Project. · View Herald TranscriptFeb 27 2023, 12:07 PM
DavidSpickett added inline comments.Feb 28 2023, 1:16 AM
lldb/unittests/Process/Linux/ProcfsTests.cpp
113

What do you mean by core ids?

And testing would require the test to sudo ... so I don't think this can be tested.

lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
235

Tell me if I understand correctly. This error is only used if you've already failed to attach. So if you had a value of 1 or 2, but attaching worked fine, you wouldn't see this.

Which is why you've said "which can cause" instead of "does cause". As there are some situations with 1 or 2 that do work.

rupprecht updated this revision to Diff 501367.Feb 28 2023, 8:19 PM
rupprecht marked an inline comment as done.
  • Fix comment typo
lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
235

That's exactly right, at least to my understanding.

Level 3 is basically a global kill switch -- no ptrace ever, in any context, even if you're root. The only way to undo that is to reboot, and even then, a hardened machine might set it to 3 in some startup config, so the only way to undo it is remove that setting and *then* reboot.

At level 2 you need CAP_SYS_PTRACE to ptrace an arbitrary process, i.e. you need to be root or have some root-like capability granted. So IIUC, sudo lldb -n blah should still work at that level.

At level 1, ptrace is allowed for anything LLDB launches, as there's a parent-child relationship, but usually not arbitrary other processes. However, an arbitrary process that wants to be debugged could call prctl(PR_SET_PTRACER, PR_SET_PTRACER_ANY, ...) or ptrace(PTRACE_TRACEME, ...), in which case a ptrace on that process should be successful. We could suggest these as other workarounds, but common advice is just to use sudo sysctl -w kernel.yama.ptrace_scope=0, and that's certainly a lot easier to fit into an error message.

Admittedly, using "which can cause" is sort of pedantic -- it can cause ptrace to fail, but there are situations above where it doesn't -- but failing in ptrace with EPERM followed by a non-zero ptrace_scope value is a very strong signal that this *is* the reason. I can't come up with a scenario where it isn't the case, but I this isn't my area of expertise.

The difference between 1 and 2 is subtle enough that I think we can use the same error message in each case, but if we also used it for level 3, that would just frustrate the user who tries to figure out why sudo sysctl -w kernel.yama.ptrace_scope=0 doesn't work. But in general, I'd be happy with whatever wording people would find more useful.

lldb/unittests/Process/Linux/ProcfsTests.cpp
113

Sorry, bad copy/paste from the test above.

DavidSpickett accepted this revision.Mar 1 2023, 1:12 AM

LGTM

lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp
235

Sounds good to me.

At level 2 you need CAP_SYS_PTRACE to ptrace an arbitrary process

I've had this problem with docker containers started in certain ways, this error message will be really useful there.

This revision is now accepted and ready to land.Mar 1 2023, 1:12 AM
labath accepted this revision.Mar 1 2023, 3:03 AM