Skip to content

Truly independent web browser

LibCore+LibSandbox+LibWebView: Name the refused call in SIGSYS reports

sideshowbarker2b718c4be832
diff --git a/Libraries/LibCore/CrashHandler.h b/Libraries/LibCore/CrashHandler.h
index 2e94da8935b..d268d95620d 100644
--- a/Libraries/LibCore/CrashHandler.h
+++ b/Libraries/LibCore/CrashHandler.h
@@ -22,6 +22,8 @@ CORE_API ErrorOr<void> initialize(int fd);
#if !defined(AK_OS_WINDOWS)
[[noreturn]] CORE_API void handle_fatal_signal(int signal, siginfo_t*, void* context);
+// For a SIGSYS from the sandbox: The report carries the sandbox's description of the refused call, as the reason.
+[[noreturn]] CORE_API void handle_sandbox_violation(siginfo_t*, void* context, char const* description);
#endif
}
diff --git a/Libraries/LibCore/CrashHandlerPOSIX.cpp b/Libraries/LibCore/CrashHandlerPOSIX.cpp
index 073bac31117..29a3da25cc0 100644
--- a/Libraries/LibCore/CrashHandlerPOSIX.cpp
+++ b/Libraries/LibCore/CrashHandlerPOSIX.cpp
@@ -270,14 +270,19 @@ static u32 capture_native_stack(void* context)
return frame_count;
}
-void CrashHandler::handle_fatal_signal(int signal, siginfo_t* info, void* context)
+// A sandbox violation's description comes straight from the trapped thread, so a second thread trapped at the same
+// moment can't replace it.
+[[noreturn]] static void handle_fatal_signal_with_reason(int signal, siginfo_t* info, void* context, char const* sandbox_violation)
{
if (__atomic_exchange_n(&s_crashing, 1, __ATOMIC_RELAXED))
_exit(128 + signal);
ReportHeader header { report_magic, signal, info ? info->si_code : 0, s_executable, {} };
+ // NB: A completed assertion comes first, since the frames below are its backtrace when it has one.
if (__atomic_load_n(&s_assertion_state, __ATOMIC_ACQUIRE) == 2)
header.assertion = s_assertion;
+ else if (sandbox_violation)
+ header.assertion = sanitize_sandbox_violation(sandbox_violation);
else if (auto const* message = AK::current_rust_panic_message())
header.assertion = sanitize_assertion(AK::AssertionFailureKind::RustPanic, message);
// Persist the reason first, even if unwinding is impossible or interrupted.
@@ -320,6 +325,19 @@ void CrashHandler::handle_fatal_signal(int signal, siginfo_t* info, void* contex
_exit(128 + signal);
}
+// ASan's hook for a call to a noreturn function looks up the thread's stack bounds first, which the sandbox refuses in
+// helper processes — so ASan would abort there before the report gets written. These handlers make that call, so they
+// stay uninstrumented.
+NO_SANITIZE_ADDRESS void CrashHandler::handle_fatal_signal(int signal, siginfo_t* info, void* context)
+{
+ handle_fatal_signal_with_reason(signal, info, context, nullptr);
+}
+
+NO_SANITIZE_ADDRESS void CrashHandler::handle_sandbox_violation(siginfo_t* info, void* context, char const* description)
+{
+ handle_fatal_signal_with_reason(SIGSYS, info, context, description);
+}
+
ErrorOr<void> CrashHandler::initialize(int fd)
{
TRY(Core::System::set_close_on_exec(fd, true));
diff --git a/Libraries/LibCore/CrashReportData.cpp b/Libraries/LibCore/CrashReportData.cpp
index 01993bb86f8..70a28347fb4 100644
--- a/Libraries/LibCore/CrashReportData.cpp
+++ b/Libraries/LibCore/CrashReportData.cpp
@@ -87,6 +87,22 @@ Assertion sanitize_assertion(AK::AssertionFailureKind kind, char const* message)
return assertion;
}
+Assertion sanitize_sandbox_violation(char const* description)
+{
+ Assertion assertion;
+ assertion.kind = 4;
+ size_t length = 0;
+ while (description[length] != '\0') {
+ if (assertion.length == assertion.message.size()) {
+ assertion.truncated = 1;
+ break;
+ }
+ auto ch = description[length++];
+ assertion.message[assertion.length++] = is_ascii_printable(ch) ? ch : '?';
+ }
+ return assertion;
+}
+
StringView sanitize_backtrace_symbol(StringView symbol)
{
// Demangled symbols may embed source paths; omit those names entirely.
diff --git a/Libraries/LibCore/CrashReportData.h b/Libraries/LibCore/CrashReportData.h
index 2a70591c1ec..759cd479b0b 100644
--- a/Libraries/LibCore/CrashReportData.h
+++ b/Libraries/LibCore/CrashReportData.h
@@ -28,7 +28,7 @@ struct Image {
};
struct Assertion {
- u32 kind { 0 }; // 1: VERIFY, 2: ASSERT, 3: Rust panic.
+ u32 kind { 0 }; // 1: VERIFY, 2: ASSERT, 3: Rust panic, 4: sandbox violation.
u32 length { 0 };
u32 truncated { 0 };
Array<char, 2048> message {};
@@ -52,6 +52,8 @@ struct ReportFrame {
// Bounded and allocation-free, suitable for a fatal assertion callback.
CORE_API Assertion sanitize_assertion(AK::AssertionFailureKind, char const* message);
+// The sandbox's description of a system call it refused, e.g. "disallowed syscall fcntl (72), [...], args [...]".
+CORE_API Assertion sanitize_sandbox_violation(char const* description);
CORE_API void describe_backtrace_frame(ReportFrame&, AK::AssertionBacktraceFrame const&);
CORE_API StringView sanitize_backtrace_symbol(StringView);
diff --git a/Libraries/LibSandbox/Seccomp.cpp b/Libraries/LibSandbox/Seccomp.cpp
index 56332971069..89d652e53d4 100644
--- a/Libraries/LibSandbox/Seccomp.cpp
+++ b/Libraries/LibSandbox/Seccomp.cpp
@@ -530,6 +530,22 @@ static size_t safe_string_length(char const* string)
}
// Builds one line of a log message, so that it reaches stderr in one write and lines from two threads never mix.
+static void write_bytes_to_stderr(char const* data, size_t length)
+{
+ while (length > 0) {
+ auto written = write(STDERR_FILENO, data, length);
+ if (written <= 0)
+ return;
+ data += written;
+ length -= static_cast<size_t>(written);
+ }
+}
+
+static void write_string_to_stderr(char const* string)
+{
+ write_bytes_to_stderr(string, safe_string_length(string));
+}
+
class LogLine {
public:
void append(char const* string) { append_bytes(string, safe_string_length(string)); }
@@ -558,18 +574,17 @@ public:
append_bytes(buffer + index, sizeof(buffer) - index);
}
+ // The line as a C string. The byte that append_bytes() keeps free for the newline holds the terminator.
+ char const* text()
+ {
+ m_data[m_length] = '\0';
+ return m_data;
+ }
+
void write_to_stderr()
{
append("\n");
- char const* data = m_data;
- size_t length = m_length;
- while (length > 0) {
- auto written = write(STDERR_FILENO, data, length);
- if (written <= 0)
- return;
- data += written;
- length -= static_cast<size_t>(written);
- }
+ write_bytes_to_stderr(m_data, m_length);
}
private:
@@ -1829,7 +1844,8 @@ static void append_syscall_details(LogLine& line, siginfo_t const* info, void* c
line.append("]");
}
-static void handle_sigsys(int, siginfo_t* info, void* context)
+// Uninstrumented like the crash handler it hands off to (see CrashHandler::handle_fatal_signal()).
+NO_SANITIZE_ADDRESS static void handle_sigsys(int, siginfo_t* info, void* context)
{
if (info->si_code == SYS_SECCOMP && info->si_arch == audit_architecture) {
#if defined(__NR_newfstatat) && defined(__NR_fstat)
@@ -1875,14 +1891,21 @@ static void handle_sigsys(int, siginfo_t* info, void* context)
}
}
- LogLine line;
- line.append("Sandbox violation in ");
- line.append(s_process_name);
- line.append(": disallowed ");
- append_syscall_details(line, info, context);
- line.write_to_stderr();
-
- Core::CrashHandler::handle_fatal_signal(SIGSYS, info, context);
+ // NB: This handler also runs for every emulated system call, so its stack frame stays as small as one line: Under
+ // AddressSanitizer, a bigger frame can need a fake stack that's mapped on first use, and a mapping refused
+ // here, with SIGSYS still blocked, kills the process. So the crash report's description of the refused call
+ // is the same line that goes to stderr, after the process name.
+ LogLine refused_call;
+ refused_call.append("disallowed ");
+ append_syscall_details(refused_call, info, context);
+ auto const* description = refused_call.text();
+ write_string_to_stderr("Sandbox violation in ");
+ write_string_to_stderr(s_process_name);
+ write_string_to_stderr(": ");
+ write_string_to_stderr(description);
+ write_string_to_stderr("\n");
+
+ Core::CrashHandler::handle_sandbox_violation(info, context, description);
}
static ErrorOr<void> install_sigsys_handler()
diff --git a/Libraries/LibWebView/CrashReport.cpp b/Libraries/LibWebView/CrashReport.cpp
index b929d284f84..cc0d92e4c08 100644
--- a/Libraries/LibWebView/CrashReport.cpp
+++ b/Libraries/LibWebView/CrashReport.cpp
@@ -193,9 +193,9 @@ ErrorOr<void> CrashReport::save(int wait_status, ByteString const& path, Optiona
if (header.signal)
builder.appendff("Captured signal: {}\nCaptured signal number: {}\nSignal code: {}\n", signal_name(header.signal), header.signal, header.code);
auto const& assertion = header.assertion;
- if ((assertion.kind == 1 || assertion.kind == 2 || assertion.kind == 3) && assertion.length <= assertion.message.size()) {
- builder.append(assertion.kind == 1 ? "Verification failed: "sv : assertion.kind == 2 ? "Assertion failed: "sv
- : "Rust panic: "sv);
+ if (assertion.kind >= 1 && assertion.kind <= 4 && assertion.length <= assertion.message.size()) {
+ static constexpr StringView prefixes[] = { "Verification failed: "sv, "Assertion failed: "sv, "Rust panic: "sv, "Sandbox violation: "sv };
+ builder.append(prefixes[assertion.kind - 1]);
for (u32 i = 0; i < assertion.length; ++i) {
auto ch = assertion.message[i];
builder.append(is_ascii_printable(ch) ? ch : '?');
diff --git a/Tests/LibWebView/TestCrashReport.cpp b/Tests/LibWebView/TestCrashReport.cpp
index 483c47979a7..c6abc5e1963 100644
--- a/Tests/LibWebView/TestCrashReport.cpp
+++ b/Tests/LibWebView/TestCrashReport.cpp
@@ -424,6 +424,43 @@ TEST_CASE(assertion_text_and_source_location)
#endif
}
+static void report_sandbox_violation(int, siginfo_t* info, void* context)
+{
+ Core::CrashHandler::handle_sandbox_violation(info, context, "disallowed syscall fcntl (72), args [0x5, 0x406]");
+}
+
+TEST_CASE(sandbox_violations_name_the_refused_call)
+{
+ cleanup();
+ ScopeGuard guard = cleanup;
+ auto report = MUST(WebView::CrashReport::create(WebView::ProcessType::WebContent));
+ auto pid = fork();
+ VERIFY(pid >= 0);
+ if (pid == 0) {
+ struct rlimit limit { 0, 0 };
+ setrlimit(RLIMIT_CORE, &limit);
+ MUST(Core::CrashHandler::initialize(report->fd()));
+ struct sigaction action {};
+ action.sa_sigaction = report_sandbox_violation;
+ action.sa_flags = SA_SIGINFO;
+ sigemptyset(&action.sa_mask);
+ sigaction(SIGSYS, &action, nullptr);
+ raise(SIGSYS);
+ _exit(0);
+ }
+ int status;
+ VERIFY(waitpid(pid, &status, 0) == pid);
+ EXPECT(WIFSIGNALED(status));
+ EXPECT_EQ(WTERMSIG(status), SIGSYS);
+ MUST(report->save(status, test_directory()));
+ auto paths = report_paths();
+ VERIFY(paths.size() == 1);
+ auto file = MUST(Core::File::open(paths[0], Core::File::OpenMode::Read));
+ auto contents = MUST(file->read_until_eof());
+ EXPECT(StringView { contents }.contains("Sandbox violation: disallowed syscall fcntl (72), args [0x5, 0x406]\n"sv));
+ EXPECT(StringView { contents }.contains("Captured signal: SIGSYS"sv));
+}
+
TEST_CASE(assertion_locations_outside_the_checkout)
{
cleanup();