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 <errno.h>
20 #include <sys/mman.h>
21 #include <sys/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
GnssGrpcProxyPolicy(const HostInfo & host)28 sandbox2::PolicyBuilder GnssGrpcProxyPolicy(const HostInfo& host) {
29 return BaselinePolicy(host, host.HostToolExe("gnss_grpc_proxy"))
30 .AddDirectory(host.instance_uds_dir, /* is_ro= */ false)
31 .AddDirectory(host.log_dir, /* is_ro= */ false)
32 .AddFile("/dev/urandom") // For gRPC
33 .AddFile(host.cuttlefish_config_path)
34 .AddPolicyOnSyscall(__NR_socket, {ARG_32(0), JEQ32(AF_UNIX, ALLOW),
35 JEQ32(AF_INET, ERRNO(EACCES)),
36 JEQ32(AF_INET6, ERRNO(EACCES))})
37 .AllowEventFd()
38 .AllowSafeFcntl()
39 .AllowSleep()
40 .AllowSyscall(__NR_bind)
41 .AllowSyscall(__NR_clone) // multithreading
42 .AllowSyscall(__NR_getpeername)
43 .AllowSyscall(__NR_getsockname)
44 .AllowSyscall(__NR_listen)
45 .AddPolicyOnSyscall(__NR_madvise,
46 {ARG_32(2), JEQ32(MADV_DONTNEED, ALLOW)})
47 .AllowSyscall(__NR_recvmsg)
48 .AllowSyscall(__NR_sched_getparam)
49 .AllowSyscall(__NR_sched_getscheduler)
50 .AllowSyscall(__NR_sched_yield)
51 .AllowSyscall(__NR_shutdown)
52 .AllowSyscall(__NR_sendmsg)
53 .AllowSyscalls({__NR_accept, __NR_accept4})
54 .AllowTCGETS();
55 }
56
57 } // namespace cuttlefish::process_sandboxer
58