• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <netinet/ip_icmp.h>
21 #include <sys/ioctl.h>
22 #include <sys/mman.h>
23 #include <sys/socket.h>
24 #include <sys/syscall.h>
25 
26 #include <vector>
27 
28 #include <sandboxed_api/sandbox2/policybuilder.h>
29 #include <sandboxed_api/sandbox2/util/bpf_helper.h>
30 
31 namespace cuttlefish::process_sandboxer {
32 
CasimirPolicy(const HostInfo & host)33 sandbox2::PolicyBuilder CasimirPolicy(const HostInfo& host) {
34   return BaselinePolicy(host, host.HostToolExe("casimir"))
35       // `librustutils::inherited_fd` scans `/proc/self/fd` for open FDs.
36       // Mounting a subset of `/proc/` is invalid.
37       .AddDirectory("/proc", /* is_ro = */ false)
38       .AddDirectory(host.EnvironmentsUdsDir(), /* is_ro= */ false)
39       .AddPolicyOnMmap([](bpf_labels& labels) -> std::vector<sock_filter> {
40         return {
41             ARG_32(2),  // prot
42             JNE32(PROT_READ | PROT_WRITE, JUMP(&labels, cf_casimir_mmap_end)),
43             ARG_32(3),  // flags
44             JEQ32(MAP_PRIVATE | MAP_ANONYMOUS | MAP_STACK, ALLOW),
45             LABEL(&labels, cf_casimir_mmap_end),
46         };
47       })
48       .AddPolicyOnSyscall(
49           __NR_setsockopt,
50           [](bpf_labels& labels) -> std::vector<sock_filter> {
51             return {
52                 ARG_32(1),  // level
53                 JNE32(IPPROTO_ICMP, JUMP(&labels, cf_casimir_setsockopt_end)),
54                 // IPPROTO_ICMP
55                 ARG_32(2),  // optname
56                 JEQ32(ICMP_REDIR_NETTOS, ALLOW),
57                 LABEL(&labels, cf_casimir_setsockopt_end)};
58           })
59       .AddPolicyOnSyscall(__NR_ioctl, {ARG_32(1), JEQ32(FIONBIO, ALLOW)})
60       .AddPolicyOnSyscall(__NR_socket, {ARG_32(0), JEQ32(AF_UNIX, ALLOW)})
61       .AllowEpoll()
62       .AllowEpollWait()
63       .AllowEventFd()
64       .AllowHandleSignals()
65       .AllowPrctlSetName()
66       .AllowReaddir()
67       .AllowSafeFcntl()
68       .AllowSyscall(__NR_accept4)
69       .AllowSyscall(__NR_bind)
70       .AllowSyscall(__NR_clone)
71       .AllowSyscall(__NR_listen)
72       // Uses GRND_INSECURE which is not covered by AllowGetRandom()
73       .AllowSyscall(__NR_getrandom)
74       .AllowSyscall(__NR_recvfrom)
75       .AllowSyscall(__NR_sendto)
76       .AllowSyscall(__NR_shutdown)
77       .AllowSyscall(__NR_statx);  // Not covered by AllowStat
78 }
79 
80 }  // namespace cuttlefish::process_sandboxer
81