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 <sys/mman.h>
20 #include <sys/socket.h>
21 #include <syscall.h>
22 
23 #include <sandboxed_api/sandbox2/policybuilder.h>
24 #include <sandboxed_api/sandbox2/util/bpf_helper.h>
25 
26 namespace cuttlefish::process_sandboxer {
27 
ControlEnvProxyServerPolicy(const HostInfo & host)28 sandbox2::PolicyBuilder ControlEnvProxyServerPolicy(const HostInfo& host) {
29   return BaselinePolicy(host, host.HostToolExe("control_env_proxy_server"))
30       .AddDirectory(host.instance_uds_dir, /* is_ro= */ false)
31       .AddFile("/dev/urandom")  // For gRPC
32       .AddPolicyOnSyscall(__NR_madvise,
33                           {ARG_32(2), JEQ32(MADV_DONTNEED, ALLOW)})
34       .AddPolicyOnSyscall(__NR_socket, {ARG_32(0), JEQ32(AF_UNIX, ALLOW),
35                                         JEQ32(AF_INET, ERRNO(EACCES)),
36                                         JEQ32(AF_INET6, ERRNO(EACCES))})
37       .AddPolicyOnSyscalls(
38           {__NR_getsockopt, __NR_setsockopt},
39           [](bpf_labels& labels) -> std::vector<sock_filter> {
40             return {
41                 ARG_32(1),  // level
42                 JNE32(SOL_SOCKET,
43                       JUMP(&labels, cf_control_env_proxy_server_sockopt_end)),
44                 ARG_32(2),  // optname
45                 JEQ32(SO_REUSEPORT, ALLOW),
46                 LABEL(&labels, cf_control_env_proxy_server_sockopt_end),
47             };
48           })
49       .AllowChmod()
50       .AllowEventFd()
51       .AllowReaddir()
52       .AllowSafeFcntl()
53       .AllowSleep()
54       .AllowSyscall(__NR_accept)
55       .AllowSyscall(__NR_bind)
56       .AllowSyscall(__NR_clone)  // Multi-threading
57       .AllowSyscall(__NR_connect)
58       .AllowSyscall(__NR_getpeername)
59       .AllowSyscall(__NR_getsockname)
60       .AllowSyscall(__NR_listen)
61       .AllowSyscall(__NR_recvmsg)
62       .AllowSyscall(__NR_shutdown)
63       .AllowSyscall(__NR_sendmsg)
64       .AllowSyscall(__NR_sched_getparam)
65       .AllowSyscall(__NR_sched_getscheduler)
66       .AllowSyscall(__NR_sched_yield);
67 }
68 
69 }  // namespace cuttlefish::process_sandboxer
70