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 <cerrno>
24
25 #include <sandboxed_api/sandbox2/policybuilder.h>
26 #include <sandboxed_api/sandbox2/util/bpf_helper.h>
27
28 namespace cuttlefish::process_sandboxer {
29
EchoServerPolicy(const HostInfo & host)30 sandbox2::PolicyBuilder EchoServerPolicy(const HostInfo& host) {
31 return BaselinePolicy(host, host.HostToolExe("echo_server"))
32 .AddDirectory(host.InstanceUdsDir(), /* is_ro= */ false)
33 .AddDirectory(host.log_dir, /* is_ro= */ false)
34 .AddFile("/dev/urandom") // For gRPC
35 .AddFile(host.cuttlefish_config_path)
36 .AddPolicyOnSyscall(__NR_madvise,
37 {ARG_32(2), JEQ32(MADV_DONTNEED, ALLOW)})
38 // Unclear where the INET and INET6 sockets come from
39 .AddPolicyOnSyscall(__NR_socket, {ARG_32(0), JEQ32(AF_UNIX, ALLOW),
40 JEQ32(AF_INET, ERRNO(EACCES)),
41 JEQ32(AF_INET6, ERRNO(EACCES))})
42 .AllowEventFd()
43 .AllowSafeFcntl()
44 .AllowSleep()
45 .AllowSyscall(__NR_accept)
46 .AllowSyscall(__NR_bind)
47 .AllowSyscall(__NR_clone) // Multithreading
48 .AllowSyscall(__NR_getpeername)
49 .AllowSyscall(__NR_getsockname)
50 .AllowSyscall(__NR_listen)
51 .AllowSyscall(__NR_recvmsg)
52 .AllowSyscall(__NR_sendmsg)
53 .AllowSyscall(__NR_sched_getparam)
54 .AllowSyscall(__NR_sched_getscheduler)
55 .AllowSyscall(__NR_sched_yield)
56 .AllowSyscall(__NR_shutdown);
57 }
58
59 } // namespace cuttlefish::process_sandboxer
60