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/socket.h>
22 #include <syscall.h>
23 
24 #include <sandboxed_api/sandbox2/policybuilder.h>
25 #include <sandboxed_api/sandbox2/util/bpf_helper.h>
26 
27 namespace cuttlefish::process_sandboxer {
28 
ScreenRecordingServerPolicy(const HostInfo & host)29 sandbox2::PolicyBuilder ScreenRecordingServerPolicy(const HostInfo& host) {
30   return BaselinePolicy(host, host.HostToolExe("screen_recording_server"))
31       .AddDirectory(host.instance_uds_dir, /* is_ro= */ false)
32       .AddDirectory(host.log_dir, /* is_ro= */ false)
33       .AddFile("/dev/urandom")  // For gRPC
34       .AddFile(host.cuttlefish_config_path)
35       .AddPolicyOnSyscalls(
36           {__NR_getsockopt, __NR_setsockopt},
37           [](bpf_labels& labels) -> std::vector<sock_filter> {
38             return {
39                 ARG_32(1),  // level
40                 JNE32(SOL_SOCKET,
41                       JUMP(&labels, cf_screen_recording_server_getsockopt_end)),
42                 ARG_32(2),  // optname
43                 JEQ32(SO_REUSEPORT, ALLOW),
44                 LABEL(&labels, cf_screen_recording_server_getsockopt_end),
45             };
46           })
47       .AddPolicyOnSyscall(__NR_madvise,
48                           {ARG_32(2), JEQ32(MADV_DONTNEED, ALLOW)})
49       // Unclear where the INET and INET6 sockets come from
50       .AddPolicyOnSyscall(__NR_socket, {ARG_32(0), JEQ32(AF_UNIX, ALLOW),
51                                         JEQ32(AF_INET, ERRNO(EACCES)),
52                                         JEQ32(AF_INET6, ERRNO(EACCES))})
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