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/socket.h>
21 #include <syscall.h>
22
23 #include <vector>
24
25 #include <sandboxed_api/sandbox2/policybuilder.h>
26 #include <sandboxed_api/sandbox2/util/bpf_helper.h>
27 #include <sandboxed_api/util/path.h>
28
29 namespace cuttlefish::process_sandboxer {
30
31 using sapi::file::JoinPath;
32
ModemSimulatorPolicy(const HostInfo & host)33 sandbox2::PolicyBuilder ModemSimulatorPolicy(const HostInfo& host) {
34 return BaselinePolicy(host, host.HostToolExe("modem_simulator"))
35 .AddDirectory(JoinPath(host.host_artifacts_path, "/etc/modem_simulator"))
36 .AddDirectory(host.log_dir, /* is_ro= */ false)
37 .AddDirectory(host.runtime_dir, /* is_ro= */ false) // modem_nvram.json
38 .AddFile(host.cuttlefish_config_path)
39 .AddPolicyOnSyscall(
40 __NR_setsockopt,
41 [](bpf_labels& labels) -> std::vector<sock_filter> {
42 return {
43 ARG_32(1),
44 JNE32(SOL_SOCKET, JUMP(&labels, cf_setsockopt_end)),
45 ARG_32(2),
46 JEQ32(SO_REUSEADDR, ALLOW),
47 LABEL(&labels, cf_setsockopt_end),
48 };
49 })
50 .AddPolicyOnSyscall(__NR_socket, {ARG_32(0), JEQ32(AF_UNIX, ALLOW)})
51 .AllowHandleSignals()
52 .AllowPipe()
53 .AllowSafeFcntl()
54 .AllowSelect()
55 .AllowSyscall(__NR_accept)
56 .AllowSyscall(__NR_bind)
57 .AllowSyscall(__NR_clone) // multithreading
58 .AllowSyscall(__NR_listen)
59 .AllowTCGETS();
60 }
61
62 } // namespace cuttlefish::process_sandboxer
63