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