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 <sandboxed_api/sandbox2/policybuilder.h>
24 #include <sandboxed_api/sandbox2/util/bpf_helper.h>
25 
26 #include "host/commands/process_sandboxer/filesystem.h"
27 
28 namespace cuttlefish::process_sandboxer {
29 
WmediumdPolicy(const HostInfo & host)30 sandbox2::PolicyBuilder WmediumdPolicy(const HostInfo& host) {
31   return BaselinePolicy(host, host.HostToolExe("wmediumd"))
32       .AddDirectory(host.environments_uds_dir, /* is_ro= */ false)
33       .AddDirectory(host.instance_uds_dir, /* is_ro= */ false)
34       .AddDirectory(host.log_dir, /* is_ro= */ false)
35       .AddFile("/dev/urandom")  // For gRPC
36       .AddFile(JoinPath(host.environments_dir, "env-1", "wmediumd.cfg"),
37                /* is_ro= */ false)
38       .AddFile(host.cuttlefish_config_path)
39       // Shared memory with crosvm for wifi
40       .AddPolicyOnMmap([](bpf_labels& labels) -> std::vector<sock_filter> {
41         return {
42             ARG_32(2),  // prot
43             JNE32(PROT_READ | PROT_WRITE, JUMP(&labels, cf_webrtc_mmap_end)),
44             ARG_32(3),  // flags
45             JEQ32(MAP_SHARED, ALLOW),
46             LABEL(&labels, cf_webrtc_mmap_end),
47         };
48       })
49       .AddPolicyOnSyscalls(
50           {__NR_getsockopt, __NR_setsockopt},
51           [](bpf_labels& labels) -> std::vector<sock_filter> {
52             return {
53                 ARG_32(1),  // level
54                 JNE32(SOL_SOCKET,
55                       JUMP(&labels, cf_screen_recording_server_getsockopt_end)),
56                 ARG_32(2),  // optname
57                 JEQ32(SO_REUSEPORT, ALLOW),
58                 LABEL(&labels, cf_screen_recording_server_getsockopt_end),
59             };
60           })
61       .AddPolicyOnSyscall(__NR_madvise,
62                           {ARG_32(2), JEQ32(MADV_DONTNEED, ALLOW)})
63       // Unclear what's creating the INET and INET6 sockets
64       .AddPolicyOnSyscall(__NR_socket, {ARG_32(0), JEQ32(AF_UNIX, ALLOW),
65                                         JEQ32(AF_INET, ERRNO(EACCES)),
66                                         JEQ32(AF_INET6, ERRNO(EACCES))})
67       .AllowEventFd()
68       .AllowHandleSignals()
69       .AllowSafeFcntl()
70       .AllowSelect()
71       .AllowSleep()
72       .AllowSyscall(__NR_accept)
73       .AllowSyscall(__NR_bind)
74       .AllowSyscall(__NR_clone)  // Multithreading
75       .AllowSyscall(__NR_getpeername)
76       .AllowSyscall(__NR_getsockname)
77       .AllowSyscall(__NR_listen)
78       .AllowSyscall(__NR_msgget)
79       .AllowSyscall(__NR_msgsnd)
80       .AllowSyscall(__NR_msgrcv)
81       .AllowSyscall(__NR_recvmsg)
82       .AllowSyscall(__NR_sched_getparam)
83       .AllowSyscall(__NR_sched_getscheduler)
84       .AllowSyscall(__NR_sched_yield)
85       .AllowSyscall(__NR_sendmsg)
86       .AllowSyscall(__NR_shutdown)
87       .AllowSyscall(__NR_timerfd_create)
88       .AllowSyscall(__NR_timerfd_settime);
89 }
90 
91 }  // namespace cuttlefish::process_sandboxer
92