Skip to content

Commit 2220f53

Browse files
committed
[lldb][Linux] Fix checking of error values when attach fails
Relates to #161510 (it's not fixing it, it's just making the error not be an unhandled error) When we fail to attach to a process we see if we can add more information about why it happpened: ``` 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()); } ``` ToError creates a new error value and leaves the one in `status` unchecked. `status`'s error is ok because it will be checked by Status' destructor. The problem happens in `AddPtraceScopeNote`. If we take certain return paths, this new error, or the one we get when trying to find the ptrace scope, may be unchecked on destruction when the function returns. To fix this, in AddPtraceScopeNote, consume any errors that we are not going to return. Anything returned will be checked by some caller. Reproducing this failure mode is difficult but it can be faked by calling AddPtraceScopeNote earlier. Which is what I did to prove the concept of the problem.
1 parent 664b227 commit 2220f53

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,8 @@ static llvm::Error AddPtraceScopeNote(llvm::Error original_error) {
219219
Expected<int> ptrace_scope = GetPtraceScope();
220220
if (auto E = ptrace_scope.takeError()) {
221221
Log *log = GetLog(POSIXLog::Process);
222-
LLDB_LOG(log, "error reading value of ptrace_scope: {0}", E);
222+
LLDB_LOG(log, "error reading value of ptrace_scope: {0}",
223+
llvm::toString(std::move(E)));
223224

224225
// The original error is probably more interesting than not being able to
225226
// read or interpret ptrace_scope.
@@ -230,6 +231,7 @@ static llvm::Error AddPtraceScopeNote(llvm::Error original_error) {
230231
switch (*ptrace_scope) {
231232
case 1:
232233
case 2:
234+
llvm::consumeError(std::move(original_error));
233235
return llvm::createStringError(
234236
std::error_code(errno, std::generic_category()),
235237
"The current value of ptrace_scope is %d, which can cause ptrace to "
@@ -239,6 +241,7 @@ static llvm::Error AddPtraceScopeNote(llvm::Error original_error) {
239241
"https://www.kernel.org/doc/Documentation/security/Yama.txt.",
240242
*ptrace_scope);
241243
case 3:
244+
llvm::consumeError(std::move(original_error));
242245
return llvm::createStringError(
243246
std::error_code(errno, std::generic_category()),
244247
"The current value of ptrace_scope is 3, which will cause ptrace to "

0 commit comments

Comments
 (0)