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 #include "host/commands/process_sandboxer/policies.h"
17 
18 #include <sys/prctl.h>
19 #include <sys/socket.h>
20 #include <syscall.h>
21 
22 #include <absl/log/log.h>
23 #include <absl/strings/str_cat.h>
24 #include <absl/strings/str_replace.h>
25 #include <sandboxed_api/sandbox2/policybuilder.h>
26 #include <sandboxed_api/sandbox2/util/bpf_helper.h>
27 
28 namespace cuttlefish::process_sandboxer {
29 
ProcessRestarterPolicy(const HostInfo & host)30 sandbox2::PolicyBuilder ProcessRestarterPolicy(const HostInfo& host) {
31   std::string sandboxer_proxy = host.HostToolExe("sandboxer_proxy");
32   return BaselinePolicy(host, host.HostToolExe("process_restarter"))
33       .AddDirectory(host.runtime_dir, /* is_ro= */ false)
34       .AddFile(host.cuttlefish_config_path)
35       .AddFileAt(sandboxer_proxy, host.HostToolExe("adb_connector"))
36       .AddFileAt(sandboxer_proxy, host.HostToolExe("casimir"))
37       .AddFileAt(sandboxer_proxy, host.HostToolExe("crosvm"))
38       .AddFileAt(sandboxer_proxy, host.HostToolExe("root-canal"))
39       .AddFileAt(sandboxer_proxy, host.HostToolExe("vhost_device_vsock"))
40       .AddPolicyOnSyscall(__NR_prctl,
41                           {ARG_32(0), JEQ32(PR_SET_PDEATHSIG, ALLOW)})
42       .AllowFork()
43       .AllowSafeFcntl()
44       .AllowSyscall(SYS_execve)  // To enter sandboxer_proxy
45       .AllowSyscall(SYS_waitid)
46       .AllowTCGETS()
47       // For sandboxer_proxy
48       .AddPolicyOnSyscall(__NR_socket, {ARG_32(0), JEQ32(AF_UNIX, ALLOW)})
49       .AllowExit()
50       .AllowSyscall(SYS_connect)
51       .AllowSyscall(SYS_recvmsg)
52       .AllowSyscall(SYS_sendmsg);
53 }
54 
55 }  // namespace cuttlefish::process_sandboxer
56