Skip to content

Truly independent web browser

LibSandbox: Allow file description queries with older Linux headers

sideshowbarker66550bef1fc5
diff --git a/Libraries/LibSandbox/LinuxFcntl.h b/Libraries/LibSandbox/LinuxFcntl.h
new file mode 100644
index 00000000000..9a2b4e5e763
--- /dev/null
+++ b/Libraries/LibSandbox/LinuxFcntl.h
@@ -0,0 +1,14 @@
+/*
+ * Copyright (c) 2026-present, the Ladybird developers.
+ *
+ * SPDX-License-Identifier: BSD-2-Clause
+ */
+
+#pragma once
+
+#include <fcntl.h>
+
+// Linux assigns F_DUPFD_QUERY the command F_LINUX_SPECIFIC_BASE + 3.
+#ifndef F_DUPFD_QUERY
+# define F_DUPFD_QUERY 1027
+#endif
diff --git a/Libraries/LibSandbox/Seccomp.cpp b/Libraries/LibSandbox/Seccomp.cpp
index 89d652e53d4..97df88d50fa 100644
--- a/Libraries/LibSandbox/Seccomp.cpp
+++ b/Libraries/LibSandbox/Seccomp.cpp
@@ -8,11 +8,11 @@
#include <LibCore/CrashHandler.h>
#include <LibSandbox/ConnectBroker.h>
#include <LibSandbox/FileBroker.h>
+#include <LibSandbox/LinuxFcntl.h>
#include <LibSandbox/Sandbox.h>
#include <LibSandbox/Seccomp.h>
#include <asm/termbits.h>
#include <errno.h>
-#include <fcntl.h>
#include <linux/audit.h>
#include <linux/netlink.h>
#include <linux/sched.h>
@@ -2144,14 +2144,12 @@ void SeccompPolicy::allow_file_descriptor_operations()
append(SECCOMP_LOAD_SYSCALL_NR);
append(BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 0));
-#ifdef F_DUPFD_QUERY
append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_fcntl, 0, 5));
append(SECCOMP_LOAD_ARGUMENT(1));
append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, F_DUPFD_QUERY, 0, 1));
append(SECCOMP_ALLOW);
append(SECCOMP_LOAD_SYSCALL_NR);
append(BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 0));
-#endif
#ifdef F_ADD_SEALS
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 c7758af5358..7ff40f3d59f 100644
--- a/Tests/LibSandbox/TestSeccomp.cpp
+++ b/Tests/LibSandbox/TestSeccomp.cpp
@@ -11,12 +11,12 @@
#include <AK/Time.h>
#include <LibCore/DirIterator.h>
#include <LibSandbox/ConnectBroker.h>
+#include <LibSandbox/LinuxFcntl.h>
#include <LibSandbox/Sandbox.h>
#include <LibSandbox/Seccomp.h>
#include <LibTest/TestCase.h>
#include <dirent.h>
#include <errno.h>
-#include <fcntl.h>
#include <ifaddrs.h>
#include <linux/filter.h>
#include <linux/landlock.h>
@@ -125,6 +125,26 @@ static int run_with_policy(Configure configure, Body body)
return status;
}
+TEST_CASE(file_descriptor_policy_allows_open_file_description_queries)
+{
+ auto body = [] {
+ int descriptors[2];
+ VERIFY(pipe(descriptors) == 0);
+ auto result = fcntl(descriptors[0], F_DUPFD_QUERY, descriptors[0]);
+ if (result == -1) {
+ // Kernels before Linux 6.10 don't implement F_DUPFD_QUERY.
+ VERIFY(errno == EINVAL);
+ return;
+ }
+ VERIFY(result == 1);
+ VERIFY(fcntl(descriptors[0], F_DUPFD_QUERY, descriptors[1]) == 0);
+ };
+ auto status = run_with_policy([](auto&) { }, body);
+ EXPECT(WIFEXITED(status));
+ if (WIFEXITED(status))
+ EXPECT_EQ(WEXITSTATUS(status), 0);
+}
+
TEST_CASE(runtime_policy_refuses_other_processes_and_resource_limit_changes)
{
auto parent = getpid();