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