Skip to content

Truly independent web browser

LibSandbox: Don’t kill AudioServer when libpulse has no cookie yet

sideshowbarker790be92e6c66
diff --git a/Libraries/LibSandbox/Seccomp.cpp b/Libraries/LibSandbox/Seccomp.cpp
index 968f7eac50d..df326aee151 100644
--- a/Libraries/LibSandbox/Seccomp.cpp
+++ b/Libraries/LibSandbox/Seccomp.cpp
@@ -1552,9 +1552,38 @@ void set_connect_broker_fd(int fd)
void SeccompPolicy::allow_pulseaudio_client_file_operations()
{
- // libpulse re-creates its runtime directory before it looks for a socket there; Landlock decides where that succeeds.
+ // libpulse re-creates its runtime directory before it looks for a socket there, and removes a directory it made
+ // again when it can't use it; Landlock decides where that succeeds.
SECCOMP_APPEND_ALLOW_SYSCALL_IF_DEFINED(*this, mkdir);
SECCOMP_APPEND_ALLOW_SYSCALL_IF_DEFINED(*this, mkdirat);
+ SECCOMP_APPEND_ALLOW_SYSCALL_IF_DEFINED(*this, rmdir);
+#ifdef __NR_unlinkat
+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_unlinkat, 0, 4));
+ append(SECCOMP_LOAD_ARGUMENT(2));
+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AT_REMOVEDIR, 0, 1));
+ append(SECCOMP_ALLOW);
+ append(SECCOMP_LOAD_SYSCALL_NR);
+#endif
+
+ // With no cookie yet, libpulse opens one for writing, to create it. Refuse that the way Landlock refuses a write
+ // there, rather than killing the process: libpulse then opens the cookie read-only, and goes on without one if
+ // there's none to read. A read-only open falls through to whatever else the policy allows.
+#ifdef __NR_open
+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_open, 0, 5));
+ append(SECCOMP_LOAD_ARGUMENT(1));
+ append(BPF_STMT(BPF_ALU | BPF_AND | BPF_K, ~read_only_open_flags));
+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0));
+ append(SECCOMP_ERRNO(EACCES));
+ append(SECCOMP_LOAD_SYSCALL_NR);
+#endif
+#ifdef __NR_openat
+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_openat, 0, 5));
+ append(SECCOMP_LOAD_ARGUMENT(2));
+ append(BPF_STMT(BPF_ALU | BPF_AND | BPF_K, ~read_only_open_flags));
+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0));
+ append(SECCOMP_ERRNO(EACCES));
+ append(SECCOMP_LOAD_SYSCALL_NR);
+#endif
// It also holds a record lock on its cookie while reading it.
append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_fcntl, 0, 5));
diff --git a/Tests/LibSandbox/TestSeccomp.cpp b/Tests/LibSandbox/TestSeccomp.cpp
index 2588fdca594..6f669c80b25 100644
--- a/Tests/LibSandbox/TestSeccomp.cpp
+++ b/Tests/LibSandbox/TestSeccomp.cpp
@@ -423,6 +423,38 @@ TEST_CASE(pulseaudio_client_policy_allows_directory_creation_and_record_locks)
VERIFY(rmdir(existing_directory) == 0);
}
+// A first run has no cookie yet: libpulse tries to create one, and removes the directory it made for it when that
+// fails. Both have to fail or succeed as plain calls, since libpulse carries on without a cookie.
+TEST_CASE(pulseaudio_client_policy_refuses_cookie_creation_and_allows_directory_removal)
+{
+ char directory[] = "/tmp/ladybird-pulse-config-XXXXXX";
+ VERIFY(mkdtemp(directory));
+ auto cookie_path = ByteString::formatted("{}/cookie", directory);
+ auto made_directory = ByteString::formatted("{}/made", directory);
+ VERIFY(mkdir(made_directory.characters(), 0700) == 0);
+
+ auto status = run_with_policy(
+ [](auto& policy) {
+ policy.allow_readonly_file_opens();
+ policy.allow_pulseaudio_client_file_operations();
+ },
+ [&] {
+ VERIFY(open(cookie_path.characters(), O_RDWR | O_CREAT | O_CLOEXEC, 0600) == -1);
+ VERIFY(errno == EACCES);
+ VERIFY(open(cookie_path.characters(), O_RDONLY | O_CLOEXEC) == -1);
+ VERIFY(errno == ENOENT);
+ VERIFY(unlinkat(AT_FDCWD, made_directory.characters(), AT_REMOVEDIR) == 0);
+ VERIFY(mkdir(made_directory.characters(), 0700) == 0);
+ VERIFY(rmdir(made_directory.characters()) == 0);
+ });
+ EXPECT(WIFEXITED(status));
+ if (WIFEXITED(status))
+ EXPECT_EQ(WEXITSTATUS(status), 0);
+
+ (void)rmdir(made_directory.characters());
+ VERIFY(rmdir(directory) == 0);
+}
+
// A domain that no group asked for fails cleanly, so the child survives and sees the error.
template<typename Configure>
static void expect_socket_domain_is_refused(Configure configure, int domain)