• 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 <linux/prctl.h>
21 #include <netinet/ip_icmp.h>
22 #include <netinet/tcp.h>
23 #include <sys/mman.h>
24 #include <sys/socket.h>
25 #include <sys/syscall.h>
26 
27 #include <vector>
28 
29 #include <sandboxed_api/sandbox2/allowlists/unrestricted_networking.h>
30 #include <sandboxed_api/sandbox2/policybuilder.h>
31 #include <sandboxed_api/sandbox2/util/bpf_helper.h>
32 #include <sandboxed_api/util/path.h>
33 
34 namespace cuttlefish::process_sandboxer {
35 
36 using sapi::file::JoinPath;
37 
WebRtcOperatorPolicy(const HostInfo & host)38 sandbox2::PolicyBuilder WebRtcOperatorPolicy(const HostInfo& host) {
39   return BaselinePolicy(host, host.HostToolExe("webrtc_operator"))
40       .AddDirectory(host.log_dir, /* is_ro= */ false)
41       .AddDirectory(
42           JoinPath(host.host_artifacts_path, "usr", "share", "webrtc"))
43       .AddFile("/dev/urandom")  // For libwebsockets
44       .AddFile(host.cuttlefish_config_path)
45       .AllowEventFd()
46       .AllowHandleSignals()
47       .AddPolicyOnSyscall(
48           __NR_madvise,
49           {ARG_32(2), JEQ32(MADV_WIPEONFORK, ALLOW), JEQ32(0xffffffff, ALLOW)})
50       .AddPolicyOnSyscall(__NR_prctl,
51                           {ARG_32(0), JEQ32(PR_CAPBSET_READ, ALLOW)})
52       .AddPolicyOnSyscall(__NR_socket, {ARG_32(0), JEQ32(AF_INET, ALLOW)})
53       .AddPolicyOnSyscall(
54           __NR_setsockopt,
55           [](bpf_labels& labels) -> std::vector<sock_filter> {
56             return {ARG_32(1),  // level
57                     JEQ32(IPPROTO_ICMP,
58                           JUMP(&labels, cf_webrtc_operator_setsockopt_icmp)),
59                     JNE32(IPPROTO_TCP,
60                           JUMP(&labels, cf_webrtc_operator_setsockopt_end)),
61                     // IPPROTO_TCP
62                     ARG_32(2),  // optname
63                     JEQ32(TCP_NODELAY, ALLOW),
64                     JUMP(&labels, cf_webrtc_operator_setsockopt_end),
65                     // IPPROTO_ICMP
66                     LABEL(&labels, cf_webrtc_operator_setsockopt_icmp),
67                     ARG_32(2),  // optname
68                     JEQ32(ICMP_REDIR_NETTOS, ALLOW),
69                     LABEL(&labels, cf_webrtc_operator_setsockopt_end)};
70           })
71       .Allow(sandbox2::UnrestrictedNetworking())
72       .AllowSafeFcntl()
73       .AllowSyscall(__NR_accept)
74       .AllowSyscall(__NR_bind)
75       .AllowSyscall(__NR_getpeername)
76       .AllowSyscall(__NR_getsockname)
77       .AllowSyscall(__NR_listen)
78       .AllowTCGETS();
79 }
80 
81 }  // namespace cuttlefish::process_sandboxer
82