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 <netinet/tcp.h>
21 #include <sys/mman.h>
22 #include <sys/socket.h>
23 #include <syscall.h>
24
25 #include <vector>
26
27 #include <sandboxed_api/sandbox2/allowlists/unrestricted_networking.h>
28 #include <sandboxed_api/sandbox2/policybuilder.h>
29 #include <sandboxed_api/sandbox2/util/bpf_helper.h>
30
31 namespace cuttlefish::process_sandboxer {
32
OpenWrtControlServerPolicy(const HostInfo & host)33 sandbox2::PolicyBuilder OpenWrtControlServerPolicy(const HostInfo& host) {
34 return BaselinePolicy(host, host.HostToolExe("openwrt_control_server"))
35 .AddDirectory(host.InstanceUdsDir(), /* is_ro= */ false)
36 .AddDirectory(host.log_dir)
37 .AddFile("/dev/urandom") // For gRPC
38 .AddPolicyOnSyscall(__NR_madvise,
39 {ARG_32(2), JEQ32(MADV_DONTNEED, ALLOW)})
40 .AddPolicyOnSyscall(
41 __NR_socket, {ARG_32(0), JEQ32(AF_UNIX, ALLOW), JEQ32(AF_INET, ALLOW),
42 JEQ32(AF_INET6, ALLOW)})
43 .AddPolicyOnSyscalls(
44 {__NR_getsockopt, __NR_setsockopt},
45 [](bpf_labels& labels) -> std::vector<sock_filter> {
46 return {
47 ARG_32(1), // level
48 JEQ32(IPPROTO_TCP,
49 JUMP(&labels, cf_open_wrt_control_server_sockopt_ip)),
50 JNE32(SOL_SOCKET,
51 JUMP(&labels, cf_open_wrt_control_server_sockopt_end)),
52 // SOL_SOCKET
53 ARG_32(2), // optname
54 JEQ32(SO_ERROR, ALLOW),
55 JEQ32(SO_REUSEPORT, ALLOW),
56 JUMP(&labels, cf_open_wrt_control_server_sockopt_end),
57 // IPPROTO_TCP
58 LABEL(&labels, cf_open_wrt_control_server_sockopt_ip),
59 ARG_32(2), // optname
60 JEQ32(TCP_NODELAY, ALLOW),
61 LABEL(&labels, cf_open_wrt_control_server_sockopt_end),
62 };
63 })
64 .Allow(sandbox2::UnrestrictedNetworking()) // HTTP calls to luci
65 .AllowEventFd()
66 .AllowSafeFcntl()
67 .AllowHandleSignals()
68 .AllowPipe()
69 .AllowSleep()
70 .AllowSyscall(__NR_accept)
71 .AllowSyscall(__NR_bind)
72 .AllowSyscall(__NR_clone) // Multithreading
73 .AllowSyscall(__NR_connect)
74 .AllowSyscall(__NR_getpeername)
75 .AllowSyscall(__NR_getsockname)
76 .AllowSyscall(__NR_listen)
77 .AllowSyscall(__NR_recvfrom)
78 .AllowSyscall(__NR_recvmsg)
79 .AllowSyscall(__NR_sched_getparam)
80 .AllowSyscall(__NR_sched_getscheduler)
81 .AllowSyscall(__NR_sched_yield)
82 .AllowSyscall(__NR_sendmsg)
83 .AllowSyscall(__NR_sendto)
84 .AllowSyscall(__NR_shutdown);
85 }
86
87 } // namespace cuttlefish::process_sandboxer
88