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