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