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