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 <linux/filter.h>
21 #include <sys/mman.h>
22 #include <sys/socket.h>
23 #include <syscall.h>
24
25 #include <vector>
26
27 #include <sandboxed_api/sandbox2/policybuilder.h>
28 #include <sandboxed_api/sandbox2/util/bpf_helper.h>
29
30 namespace cuttlefish::process_sandboxer {
31
ScreenRecordingServerPolicy(const HostInfo & host)32 sandbox2::PolicyBuilder ScreenRecordingServerPolicy(const HostInfo& host) {
33 return BaselinePolicy(host, host.HostToolExe("screen_recording_server"))
34 .AddDirectory(host.InstanceUdsDir(), /* is_ro= */ false)
35 .AddDirectory(host.log_dir, /* is_ro= */ false)
36 .AddFile("/dev/urandom") // For gRPC
37 .AddFile(host.cuttlefish_config_path)
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_screen_recording_server_getsockopt_end)),
45 ARG_32(2), // optname
46 JEQ32(SO_REUSEPORT, ALLOW),
47 LABEL(&labels, cf_screen_recording_server_getsockopt_end),
48 };
49 })
50 .AddPolicyOnSyscall(__NR_madvise,
51 {ARG_32(2), JEQ32(MADV_DONTNEED, ALLOW)})
52 // Unclear where the INET and INET6 sockets come from
53 .AddPolicyOnSyscall(__NR_socket, {ARG_32(0), JEQ32(AF_UNIX, ALLOW),
54 JEQ32(AF_INET, ERRNO(EACCES)),
55 JEQ32(AF_INET6, ERRNO(EACCES))})
56 .AllowEventFd()
57 .AllowSafeFcntl()
58 .AllowSleep()
59 .AllowSyscall(__NR_accept)
60 .AllowSyscall(__NR_bind)
61 .AllowSyscall(__NR_clone) // Multithreading
62 .AllowSyscall(__NR_connect)
63 .AllowSyscall(__NR_getpeername)
64 .AllowSyscall(__NR_getsockname)
65 .AllowSyscall(__NR_listen)
66 .AllowSyscall(__NR_recvmsg)
67 .AllowSyscall(__NR_sched_getparam)
68 .AllowSyscall(__NR_sched_getscheduler)
69 .AllowSyscall(__NR_sched_yield)
70 .AllowSyscall(__NR_sendmsg)
71 .AllowSyscall(__NR_shutdown)
72 .AllowTCGETS();
73 }
74
75 } // namespace cuttlefish::process_sandboxer
76