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