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/ioctl.h>
20 #include <sys/mman.h>
21 #include <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 
VhostDeviceVsockPolicy(const HostInfo & host)28 sandbox2::PolicyBuilder VhostDeviceVsockPolicy(const HostInfo& host) {
29   return BaselinePolicy(host, host.HostToolExe("vhost_device_vsock"))
30       .AddDirectory(host.vsock_device_dir, /* is_ro= */ false)
31       .AddPolicyOnMmap([](bpf_labels& labels) -> std::vector<sock_filter> {
32         return {
33             ARG_32(2),  // prot
34             JNE32(PROT_READ | PROT_WRITE, JUMP(&labels, cf_webrtc_mmap_end)),
35             ARG_32(3),  // flags
36             JEQ32(MAP_STACK | MAP_PRIVATE | MAP_ANONYMOUS, ALLOW),
37             JEQ32(MAP_NORESERVE | MAP_SHARED, ALLOW),
38             LABEL(&labels, cf_webrtc_mmap_end),
39         };
40       })
41       .AddPolicyOnSyscall(__NR_ioctl, {ARG_32(1), JEQ32(FIONBIO, ALLOW)})
42       .AddPolicyOnSyscall(__NR_socket, {ARG_32(0), JEQ32(AF_UNIX, ALLOW)})
43       .AllowDup()
44       .AllowEpoll()
45       .AllowEpollWait()
46       .AllowEventFd()
47       .AllowHandleSignals()
48       .AllowPrctlSetName()
49       .AllowSafeFcntl()
50       .AllowSyscall(__NR_accept4)
51       .AllowSyscall(__NR_bind)
52       .AllowSyscall(__NR_clone)
53       .AllowSyscall(__NR_connect)
54       .AllowSyscall(__NR_getrandom)  // AllowGetRandom won't take GRND_INSECURE
55       .AllowSyscall(__NR_listen)
56       .AllowSyscall(__NR_recvfrom)
57       .AllowSyscall(__NR_recvmsg)
58       .AllowSyscall(__NR_sendmsg)
59       .AllowUnlink();
60 }
61 
62 }  // namespace cuttlefish::process_sandboxer
63