Truly independent web browser
LibCore+LibSandbox: Capture seccomp violations in crash reports
Jelle Raaijmakersd458de26e853
diff --git a/Libraries/LibCore/CrashHandler.h b/Libraries/LibCore/CrashHandler.hindex 937c8ce8897..2e94da8935b 100644--- a/Libraries/LibCore/CrashHandler.h+++ b/Libraries/LibCore/CrashHandler.h@@ -7,12 +7,21 @@#pragma once#include <AK/Error.h>+#include <AK/Platform.h>#include <LibCore/Export.h>+#if !defined(AK_OS_WINDOWS)+# include <signal.h>+#endif+namespace Core::CrashHandler {// Install before sandboxing. The caller owns the descriptor and must keep it// open for the process lifetime. Only bounded native diagnostics and assertion text are written.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);+#endif+}diff --git a/Libraries/LibCore/CrashHandlerPOSIX.cpp b/Libraries/LibCore/CrashHandlerPOSIX.cppindex 2498a024d58..b45dfdb654f 100644--- a/Libraries/LibCore/CrashHandlerPOSIX.cpp+++ b/Libraries/LibCore/CrashHandlerPOSIX.cpp@@ -242,7 +242,7 @@ static u32 capture_native_stack(void* context)return frame_count;}-static void crash_signal_handler(int signal, siginfo_t* info, void* context)+void CrashHandler::handle_fatal_signal(int signal, siginfo_t* info, void* context){if (__atomic_exchange_n(&s_crashing, 1, __ATOMIC_RELAXED))_exit(128 + signal);@@ -304,7 +304,7 @@ ErrorOr<void> CrashHandler::initialize(int fd)if (sigaltstack(&stack, nullptr) < 0)return Error::from_errno(errno);struct sigaction action {};- action.sa_sigaction = crash_signal_handler;+ action.sa_sigaction = CrashHandler::handle_fatal_signal;sigfillset(&action.sa_mask);action.sa_flags = SA_SIGINFO | SA_ONSTACK;for (int signal : { SIGSEGV, SIGBUS, SIGFPE, SIGABRT, SIGILL, SIGTRAP }) {diff --git a/Libraries/LibSandbox/Seccomp.cpp b/Libraries/LibSandbox/Seccomp.cppindex 99d12ca2205..1cc31300316 100644--- a/Libraries/LibSandbox/Seccomp.cpp+++ b/Libraries/LibSandbox/Seccomp.cpp@@ -5,6 +5,7 @@*/#include <AK/Array.h>+#include <LibCore/CrashHandler.h>#include <LibSandbox/ConnectBroker.h>#include <LibSandbox/Seccomp.h>#include <asm/termbits.h>@@ -1089,12 +1090,7 @@ static void handle_sigsys(int, siginfo_t* info, void* context)write_string("\n");- struct sigaction action {};- action.sa_handler = SIG_DFL;- sigemptyset(&action.sa_mask);- sigaction(SIGSYS, &action, nullptr);- raise(SIGSYS);- _exit(128 + SIGSYS);+ Core::CrashHandler::handle_fatal_signal(SIGSYS, info, context);}static ErrorOr<void> install_sigsys_handler()diff --git a/Tests/LibSandbox/TestImageDecoderSandbox.cpp b/Tests/LibSandbox/TestImageDecoderSandbox.cppindex aabd5be08f2..439200c894e 100644--- a/Tests/LibSandbox/TestImageDecoderSandbox.cpp+++ b/Tests/LibSandbox/TestImageDecoderSandbox.cpp@@ -10,6 +10,7 @@#include <errno.h>#include <fcntl.h>#include <signal.h>+#include <sys/resource.h>#include <sys/socket.h>#include <sys/wait.h>#include <unistd.h>@@ -22,6 +23,8 @@ static int run_in_image_decoder_sandbox(Function<void()> const& body)auto child = fork();VERIFY(child >= 0);if (child == 0) {+ rlimit limit { 0, 0 };+ setrlimit(RLIMIT_CORE, &limit);if (ImageDecoder::apply_sandbox({}).is_error())_exit(2);body();@@ -40,9 +43,9 @@ TEST_CASE(image_decoder_cannot_pair_sockets)(void)socketpair(AF_UNIX, SOCK_STREAM, 0, fds);});- EXPECT(WIFEXITED(status));- if (WIFEXITED(status))- EXPECT_EQ(WEXITSTATUS(status), 128 + SIGSYS);+ EXPECT(WIFSIGNALED(status));+ if (WIFSIGNALED(status))+ EXPECT_EQ(WTERMSIG(status), SIGSYS);}TEST_CASE(image_decoder_cannot_create_sockets)diff --git a/Tests/LibSandbox/TestSeccomp.cpp b/Tests/LibSandbox/TestSeccomp.cppindex 1860ffc2f4a..0d760d9428e 100644--- a/Tests/LibSandbox/TestSeccomp.cpp+++ b/Tests/LibSandbox/TestSeccomp.cpp@@ -104,6 +104,8 @@ static int run_with_policy(Configure configure, Body body)auto child = fork();VERIFY(child >= 0);if (child == 0) {+ rlimit limit { 0, 0 };+ setrlimit(RLIMIT_CORE, &limit);MUST(Sandbox::install_no_new_privileges());Sandbox::SeccompPolicy policy;policy.allow_file_descriptor_operations();@@ -231,9 +233,9 @@ TEST_CASE(gpu_policy_does_not_allow_unrelated_ioctls)auto status = run_with_policy([](auto& policy) { policy.allow_gpu_device_operations(); },[&] { (void)ioctl(-1, request, nullptr); });- EXPECT(WIFEXITED(status));- if (WIFEXITED(status))- EXPECT_EQ(WEXITSTATUS(status), 128 + SIGSYS);+ EXPECT(WIFSIGNALED(status));+ if (WIFSIGNALED(status))+ EXPECT_EQ(WTERMSIG(status), SIGSYS);}}@@ -348,9 +350,9 @@ TEST_CASE(readonly_open_policy_accepts_the_flags_opendir_and_libpulse_use)[&] {(void)open(path, O_WRONLY | O_CLOEXEC);});- EXPECT(WIFEXITED(status));- if (WIFEXITED(status))- EXPECT_EQ(WEXITSTATUS(status), 128 + SIGSYS);+ EXPECT(WIFSIGNALED(status));+ if (WIFSIGNALED(status))+ EXPECT_EQ(WTERMSIG(status), SIGSYS);VERIFY(unlink(path) == 0);}@@ -396,9 +398,9 @@ TEST_CASE(pulseaudio_client_policy_allows_directory_creation_and_record_locks)[&] {(void)mkdir(existing_directory, 0700);});- EXPECT(WIFEXITED(status));- if (WIFEXITED(status))- EXPECT_EQ(WEXITSTATUS(status), 128 + SIGSYS);+ EXPECT(WIFSIGNALED(status));+ if (WIFSIGNALED(status))+ EXPECT_EQ(WTERMSIG(status), SIGSYS);VERIFY(close(cookie) == 0);VERIFY(unlink(cookie_path) == 0);@@ -489,9 +491,9 @@ TEST_CASE(ipc_policy_alone_does_not_pair_sockets)(void)socketpair(AF_UNIX, SOCK_STREAM, 0, fds);});- EXPECT(WIFEXITED(status));- if (WIFEXITED(status))- EXPECT_EQ(WEXITSTATUS(status), 128 + SIGSYS);+ EXPECT(WIFSIGNALED(status));+ if (WIFSIGNALED(status))+ EXPECT_EQ(WTERMSIG(status), SIGSYS);}TEST_CASE(socket_pair_policy_lets_a_process_pair_sockets_with_itself)@@ -939,9 +941,9 @@ TEST_CASE(network_policy_refuses_incoming_connections)auto status = run_with_policy(configure, [listener, number] {(void)syscall(number, listener, nullptr, nullptr, 0);});- EXPECT(WIFEXITED(status));- if (WIFEXITED(status))- EXPECT_EQ(WEXITSTATUS(status), 128 + SIGSYS);+ EXPECT(WIFSIGNALED(status));+ if (WIFSIGNALED(status))+ EXPECT_EQ(WTERMSIG(status), SIGSYS);}VERIFY(close(listener) == 0);