[Lldb-commits] [lldb] [lldb] checks beforehand if lldb can trace/attach a process on FreeBSD. (PR #79662)

David CARLIER via lldb-commits lldb-commits at lists.llvm.org
Fri Jan 26 14:50:00 PST 2024


https://github.com/devnexen created https://github.com/llvm/llvm-project/pull/79662

before having the generic EINVAL message, we check if the `security.bsd.unprivileged_proc_debug` allows process debugging.

close #79634

>From 492521e66288a497bf5b4a4a8ce554dffeb35727 Mon Sep 17 00:00:00 2001
From: David Carlier <devnexen at gmail.com>
Date: Fri, 26 Jan 2024 22:47:15 +0000
Subject: [PATCH] [lldb] checks beforehand if lldb can trace/attach a process
 on FreeBSD.

before having the generic EINVAL message, we check if the
`security.bsd.unprivileged_proc_debug` allows process debugging.

close #79634
---
 .../Process/FreeBSD/NativeProcessFreeBSD.cpp  | 29 ++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)

diff --git a/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp b/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
index 19e0986ace31ff..7540ba29651782 100644
--- a/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
+++ b/lldb/source/Plugins/Process/FreeBSD/NativeProcessFreeBSD.cpp
@@ -48,14 +48,37 @@ static Status EnsureFDFlags(int fd, int flags) {
   return error;
 }
 
+static Status CanTrace() {
+  Status status;
+  int proc_debug, ret;
+  size_t len = sizeof(proc_debug);
+  ret = ::sysctlbyname("security.bsd.unprivileged_proc_debug", &proc_debug, &len, nullptr, 0);
+  if (ret != 0) {
+    return Status("sysctlbyname() security.bsd.unprivileged_proc_debug failed");
+  }
+
+  if (proc_debug < 1) {
+    return Status("process debug disabled by security.bsd.unprivileged_proc_debug oid");
+  }
+
+  return status;
+}
+
 // Public Static Methods
 
 llvm::Expected<std::unique_ptr<NativeProcessProtocol>>
 NativeProcessFreeBSD::Manager::Launch(ProcessLaunchInfo &launch_info,
                                       NativeDelegate &native_delegate) {
   Log *log = GetLog(POSIXLog::Process);
-
   Status status;
+
+  status = CanTrace();
+
+  if (status.Fail()) {
+    LLDB_LOG(log, "failed to launch process: {0}", status);
+    return status.ToError();
+  }
+
   ::pid_t pid = ProcessLauncherPosixFork()
                     .LaunchProcess(launch_info, status)
                     .GetProcessId();
@@ -388,6 +411,10 @@ Status NativeProcessFreeBSD::PtraceWrapper(int req, lldb::pid_t pid, void *addr,
   Status error;
   int ret;
 
+  error = CanTrace();
+  if (error.Fail())
+    return error;
+
   errno = 0;
   ret =
       ptrace(req, static_cast<::pid_t>(pid), static_cast<caddr_t>(addr), data);



More information about the lldb-commits mailing list