1 /*
2 * Copyright (C) 2024 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include "host/commands/process_sandboxer/policies.h"
18
19 #include <netinet/in.h>
20 #include <netinet/tcp.h>
21 #include <sys/mman.h>
22 #include <sys/prctl.h>
23 #include <sys/socket.h>
24 #include <syscall.h>
25
26 #include <sandboxed_api/sandbox2/allow_unrestricted_networking.h>
27 #include <sandboxed_api/sandbox2/policybuilder.h>
28 #include <sandboxed_api/sandbox2/util/bpf_helper.h>
29
30 #include "host/commands/process_sandboxer/filesystem.h"
31
32 namespace cuttlefish::process_sandboxer {
33
NetsimdPolicy(const HostInfo & host)34 sandbox2::PolicyBuilder NetsimdPolicy(const HostInfo& host) {
35 return BaselinePolicy(host, host.HostToolExe("netsimd"))
36 .AddDirectory(JoinPath(host.host_artifacts_path, "bin", "netsim-ui"))
37 .AddDirectory(JoinPath(host.runtime_dir, "internal"), /* is_ro= */ false)
38 .AddFile("/dev/urandom") // For gRPC
39 .AddPolicyOnSyscalls(
40 {__NR_getsockopt, __NR_setsockopt},
41 [](bpf_labels& labels) -> std::vector<sock_filter> {
42 return {
43 ARG_32(1), // level
44 JEQ32(IPPROTO_TCP, JUMP(&labels, cf_netsimd_getsockopt_tcp)),
45 JEQ32(IPPROTO_IPV6, JUMP(&labels, cf_netsimd_getsockopt_ipv6)),
46 JNE32(SOL_SOCKET, JUMP(&labels, cf_netsimd_getsockopt_end)),
47 // SOL_SOCKET
48 ARG_32(2), // optname
49 JEQ32(SO_REUSEADDR, ALLOW),
50 JEQ32(SO_REUSEPORT, ALLOW),
51 JUMP(&labels, cf_netsimd_getsockopt_end),
52 // IPPROTO_TCP
53 LABEL(&labels, cf_netsimd_getsockopt_tcp),
54 ARG_32(2), // optname
55 JEQ32(TCP_NODELAY, ALLOW),
56 JEQ32(TCP_USER_TIMEOUT, ALLOW),
57 JUMP(&labels, cf_netsimd_getsockopt_end),
58 // IPPROTO_IPV6
59 LABEL(&labels, cf_netsimd_getsockopt_ipv6),
60 ARG_32(2), // optname
61 JEQ32(IPV6_V6ONLY, ALLOW),
62 LABEL(&labels, cf_netsimd_getsockopt_end),
63 };
64 })
65 .AddPolicyOnSyscall(__NR_madvise,
66 {ARG_32(2), JEQ32(MADV_DONTNEED, ALLOW)})
67 .AddPolicyOnSyscall(__NR_prctl,
68 {ARG_32(0), JEQ32(PR_CAPBSET_READ, ALLOW)})
69 .AddPolicyOnSyscall(__NR_socket, {ARG_32(0), JEQ32(AF_INET, ALLOW),
70 JEQ32(AF_INET6, ALLOW)})
71 .AddTmpfs("/tmp", 1 << 20)
72 .Allow(sandbox2::UnrestrictedNetworking())
73 .AllowDup()
74 .AllowEpoll()
75 .AllowEpollWait()
76 .AllowEventFd()
77 .AllowHandleSignals()
78 .AllowMkdir()
79 .AllowPipe()
80 .AllowPrctlSetName()
81 .AllowReaddir()
82 .AllowSafeFcntl()
83 .AllowSelect()
84 .AllowSleep()
85 .AllowSyscall(__NR_accept4)
86 .AllowSyscall(__NR_bind)
87 .AllowSyscall(__NR_clone)
88 .AllowSyscall(__NR_getcwd)
89 .AllowSyscall(__NR_getrandom)
90 .AllowSyscall(__NR_getsockname)
91 .AllowSyscall(__NR_listen)
92 .AllowSyscall(__NR_sched_getparam)
93 .AllowSyscall(__NR_sched_getscheduler)
94 .AllowSyscall(__NR_sched_yield)
95 .AllowSyscall(__NR_statx); // Not covered by AllowStat
96 }
97
98 } // namespace cuttlefish::process_sandboxer
99