Truly independent web browser
LibWebView: Use sealed memfds for Linux crash scratch reports
Andreas Klingdbbc600b79c7
diff --git a/Libraries/LibWebView/CrashReport.cpp b/Libraries/LibWebView/CrashReport.cppindex c94d7adf901..f6ed976a3c1 100644--- a/Libraries/LibWebView/CrashReport.cpp+++ b/Libraries/LibWebView/CrashReport.cpp@@ -17,6 +17,7 @@#include <LibWebView/ProcessManager.h>#if !defined(AK_OS_WINDOWS)+# include <fcntl.h># include <signal.h># include <sys/utsname.h># include <sys/wait.h>@@ -29,8 +30,21 @@ using namespace Core::CrashReportData;#if !defined(AK_OS_WINDOWS)+// A record keeps a description of the build that wrote it after its last frame, where the crash handler never writes,+// as its length followed by its text.+static constexpr off_t build_description_offset = sizeof(ReportHeader) + maximum_frames * sizeof(ReportFrame);+static constexpr size_t maximum_build_description_length = 16 * KiB;+ErrorOr<NonnullOwnPtr<CrashReport>> CrashReport::create(ProcessType process_type){+# if defined(AK_OS_LINUX)+ // NB: The record has room for the frames and a build description, so it never needs to change size. Like the+ // temporary file on other platforms, only its owner can read and write it.+ auto fd = TRY(Core::System::anon_create(build_description_offset + sizeof(u32) + maximum_build_description_length, O_CLOEXEC, Core::System::AllowSealing::Yes));+ auto file = TRY(Core::File::adopt_fd(fd, Core::File::OpenMode::ReadWrite));+ TRY(Core::System::fchmod(fd, 0600));+ TRY(Core::System::fcntl(fd, F_ADD_SEALS, static_cast<uintptr_t>(F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL)));+# elseauto pattern = ByteString::formatted("{}/ladybird-crash-XXXXXX", Core::StandardPaths::tempfile_directory());auto pattern_buffer = TRY(ByteBuffer::create_zeroed(pattern.length() + 1));pattern.bytes().copy_to(pattern_buffer);@@ -39,6 +53,7 @@ ErrorOr<NonnullOwnPtr<CrashReport>> CrashReport::create(ProcessType process_typeauto file = TRY(Core::File::adopt_fd(fd, Core::File::OpenMode::ReadWrite));TRY(Core::System::unlink(StringView { pattern_buffer.bytes().trim(pattern.length()) }));TRY(Core::System::set_close_on_exec(fd, true));+# endifreturn make<CrashReport>(move(file), process_type);}@@ -92,11 +107,6 @@ static void append_system_description(StringBuilder& builder)}}-// A record keeps a description of the build that wrote it after its last frame, where the crash handler never writes,-// as its length followed by its text.-static constexpr off_t build_description_offset = sizeof(ReportHeader) + maximum_frames * sizeof(ReportFrame);-static constexpr size_t maximum_build_description_length = 16 * KiB;-ByteString CrashReport::describe_current_build(ProcessType process_type){StringBuilder builder;diff --git a/Tests/LibWebView/TestCrashReport.cpp b/Tests/LibWebView/TestCrashReport.cppindex c82716230a2..c659767f849 100644--- a/Tests/LibWebView/TestCrashReport.cpp+++ b/Tests/LibWebView/TestCrashReport.cpp@@ -16,12 +16,59 @@#include <LibWebCommon/WebView/ProcessHandle.h>#include <LibWebView/CrashReport.h>#include <LibWebView/ProcessManager.h>+#include <fcntl.h>#include <pthread.h>#include <signal.h>#include <sys/resource.h>#include <sys/wait.h>#include <unistd.h>+#if defined(AK_OS_LINUX)+TEST_CASE(crash_scratch_has_fixed_size_and_allows_report_writes)+{+ auto report = MUST(WebView::CrashReport::create(WebView::ProcessType::WebContent));+ auto fd = report->fd();+ // NB: Depending on vm.memfd_noexec, the kernel can also add F_SEAL_EXEC.+ auto seals = MUST(Core::System::fcntl(fd, F_GET_SEALS, static_cast<uintptr_t>(0)));+ auto required_seals = F_SEAL_SHRINK | F_SEAL_GROW | F_SEAL_SEAL;+ EXPECT_EQ(seals & required_seals, required_seals);+ EXPECT_EQ(seals & F_SEAL_WRITE, 0);+ EXPECT(MUST(Core::System::fcntl(fd, F_GETFD, static_cast<uintptr_t>(0))) & FD_CLOEXEC);++ auto metadata = MUST(Core::System::fstat(fd));+ EXPECT_EQ(metadata.st_nlink, 0u);+ EXPECT_EQ(metadata.st_mode & 0777, 0600u);+ auto size = metadata.st_size;+ // NB: The record holds the header, the frames, and a build description of at most 16 KiB after its length.+ auto description_offset = sizeof(Core::CrashReportData::ReportHeader) + Core::CrashReportData::maximum_frames * sizeof(Core::CrashReportData::ReportFrame);+ EXPECT_EQ(size, static_cast<off_t>(description_offset + sizeof(u32) + 16 * KiB));++ auto shrink_result = ftruncate(fd, 0);+ auto shrink_error = errno;+ EXPECT_EQ(shrink_result, -1);+ EXPECT_EQ(shrink_error, EPERM);+ auto grow_result = ftruncate(fd, size + 1);+ auto grow_error = errno;+ EXPECT_EQ(grow_result, -1);+ EXPECT_EQ(grow_error, EPERM);++ Core::CrashReportData::ReportHeader header;+ EXPECT_EQ(pwrite(fd, &header, sizeof(header), 0), static_cast<ssize_t>(sizeof(header)));+ Core::CrashReportData::ReportHeader recorded;+ EXPECT_EQ(pread(fd, &recorded, sizeof(recorded), 0), static_cast<ssize_t>(sizeof(recorded)));+ EXPECT_EQ(recorded.magic, header.magic);++ auto description = WebView::CrashReport::describe_current_build(WebView::ProcessType::WebContent);+ MUST(report->record_build_description(description));+ u32 recorded_length = 0;+ EXPECT_EQ(pread(fd, &recorded_length, sizeof(recorded_length), description_offset), static_cast<ssize_t>(sizeof(recorded_length)));+ EXPECT_EQ(recorded_length, description.length());+ auto recorded_description = MUST(ByteBuffer::create_uninitialized(recorded_length));+ EXPECT_EQ(pread(fd, recorded_description.data(), recorded_length, description_offset + sizeof(recorded_length)), static_cast<ssize_t>(recorded_length));+ EXPECT_EQ(StringView { recorded_description.bytes() }, description.view());+}+#endif+static ByteString test_directory(){return ByteString::formatted("{}/test-crash-report-{}", Core::StandardPaths::tempfile_directory(), getpid());