diff options
| author | Jordan Rupprecht <rupprecht@google.com> | 2023-03-02 13:51:30 -0800 |
|---|---|---|
| committer | Jordan Rupprecht <rupprecht@google.com> | 2023-03-02 13:51:30 -0800 |
| commit | 6db44e52ce474bbeb66042073a6e3c6c586f78a2 (patch) | |
| tree | 4ac9a857e4c6ab01b2b63095700d34008c25ba91 /lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp | |
| parent | 9679075a1ae6a20ed102597bf166b3f3adc95499 (diff) | |
[Linux] Add kernel.yama.ptrace_scope note when ptrace fails to attach.
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.
Reviewed By: DavidSpickett, labath
Differential Revision: https://reviews.llvm.org/D144904
Diffstat (limited to 'lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp')
| -rw-r--r-- | lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp index a0e5d25a233b..618252550254 100644 --- a/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp +++ b/lldb/source/Plugins/Process/Linux/NativeProcessLinux.cpp @@ -45,6 +45,7 @@ #include "lldb/Utility/StringExtractor.h" #include "llvm/ADT/ScopeExit.h" #include "llvm/Support/Errno.h" +#include "llvm/Support/Error.h" #include "llvm/Support/FileSystem.h" #include "llvm/Support/Threading.h" @@ -214,6 +215,43 @@ static Status EnsureFDFlags(int fd, int flags) { return error; } +static llvm::Error AddPtraceScopeNote(llvm::Error original_error) { + Expected<int> ptrace_scope = GetPtraceScope(); + if (auto E = ptrace_scope.takeError()) { + Log *log = GetLog(POSIXLog::Process); + LLDB_LOG(log, "error reading value of ptrace_scope: {0}", E); + + // The original error is probably more interesting than not being able to + // read or interpret ptrace_scope. + return original_error; + } + + // We only have suggestions to provide for 1-3. + switch (*ptrace_scope) { + case 1: + case 2: + return llvm::createStringError( + std::error_code(errno, std::generic_category()), + "The current value of ptrace_scope is %d, which can cause ptrace to " + "fail to attach to a running process. To fix this, run:\n" + "\tsudo sysctl -w kernel.yama.ptrace_scope=0\n" + "For more information, see: " + "https://www.kernel.org/doc/Documentation/security/Yama.txt.", + *ptrace_scope); + case 3: + return llvm::createStringError( + std::error_code(errno, std::generic_category()), + "The current value of ptrace_scope is 3, which will cause ptrace to " + "fail to attach to a running process. This value cannot be changed " + "without rebooting.\n" + "For more information, see: " + "https://www.kernel.org/doc/Documentation/security/Yama.txt."); + case 0: + default: + return original_error; + } +} + // Public Static Methods llvm::Expected<std::unique_ptr<NativeProcessProtocol>> @@ -352,6 +390,11 @@ llvm::Expected<std::vector<::pid_t>> NativeProcessLinux::Attach(::pid_t pid) { it = tids_to_attach.erase(it); continue; } + if (status.GetError() == EPERM) { + // Depending on the value of ptrace_scope, we can return a different + // error that suggests how to fix it. + return AddPtraceScopeNote(status.ToError()); + } return status.ToError(); } |
