Truly independent web browser
LibSandbox: Limit Internet sockets to TCP and UDP
Andreas Kling4c227bba1b66
diff --git a/Libraries/LibSandbox/Seccomp.cpp b/Libraries/LibSandbox/Seccomp.cppindex 9cad1ca7481..78d10f55f18 100644--- a/Libraries/LibSandbox/Seccomp.cpp+++ b/Libraries/LibSandbox/Seccomp.cpp@@ -1436,35 +1436,6 @@ void SeccompPolicy::allow_process_creation()SECCOMP_APPEND_ALLOW_SYSCALL_IF_DEFINED(*this, waitid);}-// NB: Seccomp cannot follow the sockaddr pointer that connect() is given, and Landlock does not-// mediate UNIX socket connections, so neither mechanism can restrict which endpoint a process-// reaches. The domain argument of socket() is the only part of this the kernel lets us filter,-// and connect() fails with EAFNOSUPPORT when the address family does not match the socket.-// Being able to create a socket in a domain is therefore the same thing as being able to-// address every endpoint in it.-void SeccompPolicy::append_allow_socket_with_domains([[maybe_unused]] ReadonlySpan<u32> domains)-{-#ifdef __NR_socket- // Every jump offset below is a byte, so the group has to stay well short of 255 domains.- VERIFY(!domains.is_empty());- VERIFY(domains.size() < 32);-- // Jump past the whole group when this is not socket(). The group is one argument load, one- // comparison per domain, and the allow.- append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_socket, 0, static_cast<u8>(domains.size() + 2)));- append(SECCOMP_LOAD_ARGUMENT(0));- for (size_t i = 0; i < domains.size(); ++i) {- // A matching domain jumps to the allow. The last comparison is the one that has to step- // over the allow when it does not match.- auto instructions_to_allow = static_cast<u8>(domains.size() - i - 1);- auto instructions_past_allow = static_cast<u8>(i + 1 == domains.size() ? 1 : 0);- append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, domains[i], instructions_to_allow, instructions_past_allow));- }- append(SECCOMP_ALLOW);- append(SECCOMP_LOAD_SYSCALL_NR);-#endif-}-// NB: Socket options reach deep into the kernel. SO_ATTACH_FILTER, for one, loads a classic BPF program into it. So we// allow only the options that we know to be used, and install() refuses every other one with EPERM.void SeccompPolicy::append_allow_socket_options(u32 syscall_number, u32 level, ReadonlySpan<u32> options)@@ -1608,6 +1579,12 @@ void SeccompPolicy::allow_pulseaudio_client_file_operations()append(BPF_STMT(BPF_ALU | BPF_ADD | BPF_K, 0));}+// NB: Seccomp cannot follow the sockaddr pointer that connect() is given, and Landlock does not+// mediate UNIX socket connections, so neither mechanism can restrict which endpoint a process+// reaches. The domain argument of socket() is the only part of this the kernel lets us filter,+// and connect() fails with EAFNOSUPPORT when the address family does not match the socket.+// Being able to create a socket in a domain is therefore the same thing as being able to+// address every endpoint in it.void SeccompPolicy::allow_network(){#ifdef __NR_socket@@ -1624,9 +1601,28 @@ void SeccompPolicy::allow_network()append(SECCOMP_ALLOW);append(SECCOMP_ERRNO(EPERM));append(SECCOMP_LOAD_SYSCALL_NR);++ // Internet sockets serve TCP streams and UDP datagrams, including the default protocol. Other domains fall+ // through to the groups after this one.+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, __NR_socket, 0, 16));+ append(SECCOMP_LOAD_ARGUMENT(0));+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AF_INET, 1, 0));+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, AF_INET6, 0, 13));+ append(SECCOMP_LOAD_ARGUMENT(1));+ append(BPF_STMT(BPF_ALU | BPF_AND | BPF_K, ~static_cast<u32>(SOCK_CLOEXEC | SOCK_NONBLOCK)));+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SOCK_STREAM, 0, 4));+ append(SECCOMP_LOAD_ARGUMENT(2));+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0));+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_TCP, 0, 6));+ append(SECCOMP_ALLOW);+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, SOCK_DGRAM, 0, 4));+ append(SECCOMP_LOAD_ARGUMENT(2));+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, 0, 1, 0));+ append(BPF_JUMP(BPF_JMP | BPF_JEQ | BPF_K, IPPROTO_UDP, 0, 1));+ append(SECCOMP_ALLOW);+ append(SECCOMP_ERRNO(EPERM));+ append(SECCOMP_LOAD_SYSCALL_NR);#endif- static constexpr Array<u32, 2> domains { AF_INET, AF_INET6 };- append_allow_socket_with_domains(domains);SECCOMP_APPEND_ALLOW_SYSCALL_IF_DEFINED(*this, sendto);SECCOMP_APPEND_ALLOW_SYSCALL_IF_DEFINED(*this, connect);diff --git a/Libraries/LibSandbox/Seccomp.h b/Libraries/LibSandbox/Seccomp.hindex 2e6676c76cd..3cb6c101ead 100644--- a/Libraries/LibSandbox/Seccomp.h+++ b/Libraries/LibSandbox/Seccomp.h@@ -46,7 +46,6 @@ public:private:void append(sock_filter);- void append_allow_socket_with_domains(ReadonlySpan<u32> domains);void append_allow_socket_options(u32 syscall_number, u32 level, ReadonlySpan<u32> options);void append_architecture_check();void append_load_syscall_number();diff --git a/Tests/LibSandbox/TestSeccomp.cpp b/Tests/LibSandbox/TestSeccomp.cppindex f9b6454087b..34d002df771 100644--- a/Tests/LibSandbox/TestSeccomp.cpp+++ b/Tests/LibSandbox/TestSeccomp.cpp@@ -863,12 +863,56 @@ TEST_CASE(network_policy_is_limited_to_internet_sockets)policy.allow_network();};- expect_socket_domain_is_allowed(configure, AF_INET, SOCK_STREAM, 0);- expect_socket_domain_is_allowed(configure, AF_INET6, SOCK_STREAM, 0);+ for (auto domain : { AF_INET, AF_INET6 }) {+ for (auto flags : Array<int, 4> { 0, SOCK_CLOEXEC, SOCK_NONBLOCK, SOCK_CLOEXEC | SOCK_NONBLOCK }) {+ for (auto protocol : Array<int, 2> { 0, IPPROTO_TCP })+ expect_socket_domain_is_allowed(configure, domain, SOCK_STREAM | flags, protocol);+ for (auto protocol : Array<int, 2> { 0, IPPROTO_UDP })+ expect_socket_domain_is_allowed(configure, domain, SOCK_DGRAM | flags, protocol);+ }+ }+ auto status = run_with_policy(configure, [] {+ for (auto domain : { AF_INET, AF_INET6 }) {+ for (auto flags : Array<int, 4> { 0, SOCK_CLOEXEC, SOCK_NONBLOCK, SOCK_CLOEXEC | SOCK_NONBLOCK }) {+ for (auto protocol : Array<int, 4> { IPPROTO_ICMP, IPPROTO_ICMPV6, IPPROTO_UDPLITE, IPPROTO_TCP }) {+ VERIFY(socket(domain, SOCK_DGRAM | flags, protocol) == -1);+ VERIFY(errno == EPERM);+ }+ for (auto type : { SOCK_RAW, SOCK_SEQPACKET }) {+ VERIFY(socket(domain, type | flags, 0) == -1);+ VERIFY(errno == EPERM);+ }+ VERIFY(socket(domain, SOCK_STREAM | flags, IPPROTO_UDP) == -1);+ VERIFY(errno == EPERM);+ }+ }+ });+ EXPECT(WIFEXITED(status));+ if (WIFEXITED(status))+ EXPECT_EQ(WEXITSTATUS(status), 0);expect_socket_domain_is_allowed(configure, AF_NETLINK, SOCK_RAW, 0);expect_socket_domain_is_refused(configure, AF_UNIX);}+TEST_CASE(network_policy_leaves_other_socket_domains_to_later_groups)+{+ // Without a broker to answer, a brokered socket() fails with EACCES. Getting that rather than+ // EAFNOSUPPORT shows that the request came through the network group to the broker.+ auto status = run_with_policy(+ [](Sandbox::SeccompPolicy& policy) {+ policy.allow_ipc();+ policy.allow_network();+ policy.broker_unix_socket_connections();+ },+ [] {+ VERIFY(socket(AF_UNIX, SOCK_STREAM, 0) == -1);+ VERIFY(errno == EACCES);+ });+ EXPECT(WIFEXITED(status));+ if (WIFEXITED(status))+ EXPECT_EQ(WEXITSTATUS(status), 0);+}+// Waits for a descriptor to become readable. The deadline is far longer than any of this work takes,// so a slow machine cannot trip it; it is here so that a broker which never answers is reported as a// failure instead of leaving the test to hang.