Truly independent web browser
LibCore+LibSandbox: Use native pipe2 on macOS 27
Timothy Flynn96f73e322d56
diff --git a/Libraries/LibCore/System.cpp b/Libraries/LibCore/System.cppindex 202d98664c4..4fa25a73c96 100644--- a/Libraries/LibCore/System.cpp+++ b/Libraries/LibCore/System.cpp@@ -764,6 +764,14 @@ ErrorOr<Array<int, 2>> pipe2(int flags, PipeReadEnd)if (::pipe2(fds.data(), flags) < 0)return Error::from_syscall("pipe2"sv, errno);#else+# if defined(AK_OS_MACOS) && __MAC_OS_X_VERSION_MAX_ALLOWED >= 270000+ if (__builtin_available(macOS 27.0, *)) {+ if (::pipe2(fds.data(), flags) < 0)+ return Error::from_syscall("pipe2"sv, errno);+ return fds;+ }+# endif+if (::pipe(fds.data()) < 0)return Error::from_syscall("pipe2"sv, errno);diff --git a/Libraries/LibSandbox/Sandbox.cpp b/Libraries/LibSandbox/Sandbox.cppindex a8f10dfc57b..a1d1e3018c0 100644--- a/Libraries/LibSandbox/Sandbox.cpp+++ b/Libraries/LibSandbox/Sandbox.cpp@@ -671,6 +671,10 @@ ErrorOr<void> apply_macos_sandbox(SeatbeltProfile const& options))~~~"sv));+ // Core::System::pipe2() uses the native syscall on macOS 27. Older Seatbelt versions do not know this syscall.+ if (__builtin_available(macOS 27.0, *))+ TRY(profile.try_append("(allow syscall-unix (syscall-number SYS_pipe2))\n"sv));+if (options.network_access == NetworkAccess::Allowed) {TRY(profile.try_append(R"~~~(; Connect to hosts on the network and to the system's network daemons, but not to local sockets of other processes.diff --git a/Tests/LibSandbox/TestSeatbelt.cpp b/Tests/LibSandbox/TestSeatbelt.cppindex 3a92df37c19..95102dc2e9a 100644--- a/Tests/LibSandbox/TestSeatbelt.cpp+++ b/Tests/LibSandbox/TestSeatbelt.cpp@@ -164,6 +164,50 @@ TEST_CASE(sandboxed_process_can_do_ordinary_work)Outcome::Allowed);}+TEST_CASE(sandboxed_process_can_create_nonblocking_pipes)+{+ EXPECT_EQ(run_sandboxed([] {+ auto result = Core::System::pipe2(O_CLOEXEC | O_NONBLOCK);+ if (result.is_error())+ return false;+ auto fds = result.release_value();+ for (auto fd : fds) {+ if ((fcntl(fd, F_GETFD) & FD_CLOEXEC) == 0 || (fcntl(fd, F_GETFL) & O_NONBLOCK) == 0)+ return false;+ }+ char byte = 0;+ bool success = write(fds[1], "x", 1) == 1 && read(fds[0], &byte, 1) == 1 && byte == 'x';+ close(fds[0]);+ close(fds[1]);+ return success;+ }),+ Outcome::Allowed);+}++# if defined(SYS_pipe2)+TEST_CASE(sandboxed_process_can_use_native_pipe2)+{+ if (__builtin_available(macOS 27.0, *)) {+ Function<bool()> create_pipe = [] {+ int fds[2];+ if (pipe2(fds, O_CLOEXEC | O_NONBLOCK) != 0)+ return false;+ for (auto fd : fds) {+ if ((fcntl(fd, F_GETFD) & FD_CLOEXEC) == 0 || (fcntl(fd, F_GETFL) & O_NONBLOCK) == 0)+ return false;+ }+ char byte = 0;+ bool success = write(fds[1], "x", 1) == 1 && read(fds[0], &byte, 1) == 1 && byte == 'x';+ close(fds[0]);+ close(fds[1]);+ return success;+ };+ EXPECT_EQ(run_sandboxed(create_pipe), Outcome::Allowed);+ EXPECT_EQ(run_sandboxed({ .network_access = Sandbox::NetworkAccess::Allowed }, create_pipe), Outcome::Allowed);+ }+}+# endif+// The helpers raise their main thread's QoS before the sandbox is applied, but every thread they create afterwards,// including the IPC IO threads, gets its QoS class through pthread_create() attributes. Both calls run on a new// thread here: the main thread of a fork()ed child refuses QoS changes with EPERM even without a sandbox.