• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <linux/filter.h>
20 #include <linux/prctl.h>
21 #include <netinet/in.h>
22 #include <netinet/tcp.h>
23 #include <sys/mman.h>
24 #include <sys/socket.h>
25 #include <syscall.h>
26 
27 #include <vector>
28 
29 #include <sandboxed_api/sandbox2/allowlists/unrestricted_networking.h>
30 #include <sandboxed_api/sandbox2/policybuilder.h>
31 #include <sandboxed_api/sandbox2/util/bpf_helper.h>
32 #include <sandboxed_api/util/path.h>
33 
34 namespace cuttlefish::process_sandboxer {
35 
36 using sapi::file::JoinPath;
37 
NetsimdPolicy(const HostInfo & host)38 sandbox2::PolicyBuilder NetsimdPolicy(const HostInfo& host) {
39   return BaselinePolicy(host, host.HostToolExe("netsimd"))
40       .AddDirectory(JoinPath(host.host_artifacts_path, "bin", "netsim-ui"))
41       .AddDirectory(JoinPath(host.runtime_dir, "internal"), /* is_ro= */ false)
42       .AddDirectory(host.tmp_dir, /* is_ro= */ false)
43       .AddFile("/dev/urandom")  // For gRPC
44       .AddPolicyOnSyscalls(
45           {__NR_getsockopt, __NR_setsockopt},
46           [](bpf_labels& labels) -> std::vector<sock_filter> {
47             return {
48                 ARG_32(1),  // level
49                 JEQ32(IPPROTO_TCP, JUMP(&labels, cf_netsimd_getsockopt_tcp)),
50                 JEQ32(IPPROTO_IPV6, JUMP(&labels, cf_netsimd_getsockopt_ipv6)),
51                 JNE32(SOL_SOCKET, JUMP(&labels, cf_netsimd_getsockopt_end)),
52                 // SOL_SOCKET
53                 ARG_32(2),  // optname
54                 JEQ32(SO_REUSEADDR, ALLOW),
55                 JEQ32(SO_REUSEPORT, ALLOW),
56                 JUMP(&labels, cf_netsimd_getsockopt_end),
57                 // IPPROTO_TCP
58                 LABEL(&labels, cf_netsimd_getsockopt_tcp),
59                 ARG_32(2),  // optname
60                 JEQ32(TCP_NODELAY, ALLOW),
61                 JEQ32(TCP_USER_TIMEOUT, ALLOW),
62                 JUMP(&labels, cf_netsimd_getsockopt_end),
63                 // IPPROTO_IPV6
64                 LABEL(&labels, cf_netsimd_getsockopt_ipv6),
65                 ARG_32(2),  // optname
66                 JEQ32(IPV6_V6ONLY, ALLOW),
67                 LABEL(&labels, cf_netsimd_getsockopt_end),
68             };
69           })
70       .AddPolicyOnSyscall(__NR_madvise,
71                           {ARG_32(2), JEQ32(MADV_DONTNEED, ALLOW)})
72       .AddPolicyOnSyscall(__NR_prctl,
73                           {ARG_32(0), JEQ32(PR_CAPBSET_READ, ALLOW)})
74       .AddPolicyOnSyscall(__NR_socket, {ARG_32(0), JEQ32(AF_INET, ALLOW),
75                                         JEQ32(AF_INET6, ALLOW)})
76       .AddTmpfs("/tmp", 1 << 20)
77       .Allow(sandbox2::UnrestrictedNetworking())
78       .AllowDup()
79       .AllowEpoll()
80       .AllowEpollWait()
81       .AllowEventFd()
82       .AllowHandleSignals()
83       .AllowMkdir()
84       .AllowPipe()
85       .AllowPrctlSetName()
86       .AllowReaddir()
87       .AllowSafeFcntl()
88       .AllowSelect()
89       .AllowSleep()
90       .AllowSyscall(__NR_accept4)
91       .AllowSyscall(__NR_bind)
92       .AllowSyscall(__NR_clone)
93       .AllowSyscall(__NR_getcwd)
94       .AllowSyscall(__NR_getrandom)
95       .AllowSyscall(__NR_getsockname)
96       .AllowSyscall(__NR_listen)
97       .AllowSyscall(__NR_sched_getparam)
98       .AllowSyscall(__NR_sched_getscheduler)
99       .AllowSyscall(__NR_sched_yield)
100       .AllowSyscall(__NR_statx);  // Not covered by AllowStat
101 }
102 
103 }  // namespace cuttlefish::process_sandboxer
104