1 /*
2 * Copyright (C) 2025 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 <syscall.h>
21
22 #include <sandboxed_api/sandbox2/policybuilder.h>
23 #include <sandboxed_api/sandbox2/util/bpf_helper.h>
24 #include <sandboxed_api/util/path.h>
25
26 namespace cuttlefish::process_sandboxer {
27
28 using sapi::file::JoinPath;
29
CfVhostUserInput(const HostInfo & host)30 sandbox2::PolicyBuilder CfVhostUserInput(const HostInfo& host) {
31 return BaselinePolicy(host, host.HostToolExe("cf_vhost_user_input"))
32 .AddDirectory(host.runtime_dir, /* is_ro= */ false)
33 .AddDirectory("/proc", /* is_ro= */ false) // for inherited_fds
34 .AddDirectory(
35 JoinPath(host.host_artifacts_path, "etc", "default_input_devices"))
36 .AddPolicyOnMmap([](bpf_labels& labels) -> std::vector<sock_filter> {
37 return {
38 ARG_32(2), // prot
39 JNE32(PROT_READ | PROT_WRITE,
40 JUMP(&labels, cf_vhost_user_input_mmap_end)),
41 ARG_32(3), // flags
42 JEQ32(MAP_STACK | MAP_ANONYMOUS | MAP_PRIVATE, ALLOW),
43 JEQ32(MAP_NORESERVE | MAP_SHARED, ALLOW),
44 LABEL(&labels, cf_vhost_user_input_mmap_end),
45 };
46 })
47 .AllowEpoll()
48 .AllowEventFd()
49 .AllowHandleSignals()
50 .AllowReaddir()
51 .AllowPrctlSetName()
52 .AllowSyscall(__NR_accept4)
53 .AllowSyscall(__NR_clone)
54 .AllowSyscall(__NR_getrandom)
55 .AllowSyscall(__NR_recvmsg)
56 .AllowSyscall(__NR_sendmsg)
57 .AllowSyscall(__NR_statx)
58 .AllowSafeFcntl();
59 }
60
61 } // namespace cuttlefish::process_sandboxer
62