xref: /aosp_15_r20/external/sandboxed-api/sandboxed_api/sandbox2/policybuilder.h (revision ec63e07ab9515d95e79c211197c445ef84cefa6a)
1*ec63e07aSXin Li // Copyright 2019 Google LLC
2*ec63e07aSXin Li //
3*ec63e07aSXin Li // Licensed under the Apache License, Version 2.0 (the "License");
4*ec63e07aSXin Li // you may not use this file except in compliance with the License.
5*ec63e07aSXin Li // You may obtain a copy of the License at
6*ec63e07aSXin Li //
7*ec63e07aSXin Li //     https://www.apache.org/licenses/LICENSE-2.0
8*ec63e07aSXin Li //
9*ec63e07aSXin Li // Unless required by applicable law or agreed to in writing, software
10*ec63e07aSXin Li // distributed under the License is distributed on an "AS IS" BASIS,
11*ec63e07aSXin Li // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12*ec63e07aSXin Li // See the License for the specific language governing permissions and
13*ec63e07aSXin Li // limitations under the License.
14*ec63e07aSXin Li 
15*ec63e07aSXin Li #ifndef SANDBOXED_API_SANDBOX2_POLICYBUILDER_H_
16*ec63e07aSXin Li #define SANDBOXED_API_SANDBOX2_POLICYBUILDER_H_
17*ec63e07aSXin Li 
18*ec63e07aSXin Li #include <linux/filter.h>
19*ec63e07aSXin Li 
20*ec63e07aSXin Li #include <cstddef>
21*ec63e07aSXin Li #include <cstdint>
22*ec63e07aSXin Li #include <functional>
23*ec63e07aSXin Li #include <memory>
24*ec63e07aSXin Li #include <string>
25*ec63e07aSXin Li #include <utility>
26*ec63e07aSXin Li #include <vector>
27*ec63e07aSXin Li 
28*ec63e07aSXin Li #include "absl/base/attributes.h"
29*ec63e07aSXin Li #include "absl/base/macros.h"
30*ec63e07aSXin Li #include "absl/container/flat_hash_set.h"
31*ec63e07aSXin Li #include "absl/log/check.h"
32*ec63e07aSXin Li #include "absl/status/status.h"
33*ec63e07aSXin Li #include "absl/status/statusor.h"
34*ec63e07aSXin Li #include "absl/strings/string_view.h"
35*ec63e07aSXin Li #include "absl/types/optional.h"
36*ec63e07aSXin Li #include "absl/types/span.h"
37*ec63e07aSXin Li #include "sandboxed_api/sandbox2/mounts.h"
38*ec63e07aSXin Li #include "sandboxed_api/sandbox2/network_proxy/filtering.h"
39*ec63e07aSXin Li #include "sandboxed_api/sandbox2/policy.h"
40*ec63e07aSXin Li 
41*ec63e07aSXin Li struct bpf_labels;
42*ec63e07aSXin Li 
43*ec63e07aSXin Li namespace sandbox2 {
44*ec63e07aSXin Li 
45*ec63e07aSXin Li class AllowAllSyscalls;
46*ec63e07aSXin Li class TraceAllSyscalls;
47*ec63e07aSXin Li class UnrestrictedNetworking;
48*ec63e07aSXin Li 
49*ec63e07aSXin Li // PolicyBuilder is a helper class to simplify creation of policies. The builder
50*ec63e07aSXin Li // uses fluent interface for convenience and increased readability of policies.
51*ec63e07aSXin Li //
52*ec63e07aSXin Li // To build a policy you simply create a new builder object, call methods on it
53*ec63e07aSXin Li // specifying what you want and finally call BuildOrDie() to generate you
54*ec63e07aSXin Li // policy.
55*ec63e07aSXin Li //
56*ec63e07aSXin Li // For instance this would generate a simple policy suitable for binaries doing
57*ec63e07aSXin Li // only computations:
58*ec63e07aSXin Li //
59*ec63e07aSXin Li // std::unique_ptr<Policy> policy =
60*ec63e07aSXin Li //     PolicyBuilder()
61*ec63e07aSXin Li //       .AllowRead()
62*ec63e07aSXin Li //       .AllowWrite()
63*ec63e07aSXin Li //       .AllowExit()
64*ec63e07aSXin Li //       .AllowSystemMalloc()
65*ec63e07aSXin Li //       .BuildOrDie();
66*ec63e07aSXin Li //
67*ec63e07aSXin Li // Note that operations are executed in the order they are dictated, though in
68*ec63e07aSXin Li // most cases this has no influence since the operations themselves commute.
69*ec63e07aSXin Li //
70*ec63e07aSXin Li // For instance these two policies are equivalent:
71*ec63e07aSXin Li //
72*ec63e07aSXin Li // auto policy = PolicyBuilder.AllowRead().AllowWrite().BuildOrDie();
73*ec63e07aSXin Li // auto policy = PolicyBuilder.AllowWrite().AllowRead().BuildOrDie();
74*ec63e07aSXin Li //
75*ec63e07aSXin Li // While these two are not:
76*ec63e07aSXin Li //
77*ec63e07aSXin Li // auto policy = PolicyBuilder.AllowRead().BlockSyscallWithErrno(__NR_read, EIO)
78*ec63e07aSXin Li //                            .BuildOrDie();
79*ec63e07aSXin Li // auto policy = PolicyBuilder.BlockSyscallWithErrno(__NR_read, EIO).AllowRead()
80*ec63e07aSXin Li //                            .BuildOrDie();
81*ec63e07aSXin Li //
82*ec63e07aSXin Li // In fact the first one is equivalent to:
83*ec63e07aSXin Li //
84*ec63e07aSXin Li // auto policy = PolicyBuilder.AllowRead().BuildOrDie();
85*ec63e07aSXin Li //
86*ec63e07aSXin Li // If you dislike the chained style, it is also possible to write the first
87*ec63e07aSXin Li // example as this:
88*ec63e07aSXin Li //
89*ec63e07aSXin Li // PolicyBuilder builder;
90*ec63e07aSXin Li // builder.AllowRead();
91*ec63e07aSXin Li // builder.AllowWrite();
92*ec63e07aSXin Li // builder.AllowExit();
93*ec63e07aSXin Li // builder.AllowSystemMalloc();
94*ec63e07aSXin Li // auto policy = builder.BuildOrDie();
95*ec63e07aSXin Li //
96*ec63e07aSXin Li // For a more complicated example, see examples/persistent/persistent_sandbox.cc
97*ec63e07aSXin Li class PolicyBuilder final {
98*ec63e07aSXin Li  public:
99*ec63e07aSXin Li   // Possible CPU fence modes for `AllowRestartableSequences()`
100*ec63e07aSXin Li   enum CpuFenceMode {
101*ec63e07aSXin Li     // Allow only fast fences for restartable sequences.
102*ec63e07aSXin Li     kRequireFastFences,
103*ec63e07aSXin Li 
104*ec63e07aSXin Li     // Allow fast fences as well as slow fences if fast fences are unavailable.
105*ec63e07aSXin Li     kAllowSlowFences,
106*ec63e07aSXin Li   };
107*ec63e07aSXin Li 
108*ec63e07aSXin Li   static constexpr absl::string_view kDefaultHostname = "sandbox2";
109*ec63e07aSXin Li   // Seccomp takes a 16-bit filter length, so the limit would be 64k.
110*ec63e07aSXin Li   // We set it lower so that there is for sure some room for the default policy.
111*ec63e07aSXin Li   static constexpr size_t kMaxUserPolicyLength = 30000;
112*ec63e07aSXin Li 
113*ec63e07aSXin Li   using BpfFunc = const std::function<std::vector<sock_filter>(bpf_labels&)>&;
114*ec63e07aSXin Li 
115*ec63e07aSXin Li   // Appends code to allow visibility restricted policy functionality.
116*ec63e07aSXin Li   //
117*ec63e07aSXin Li   // For example:
118*ec63e07aSXin Li   // Allow(sandbox2::UnrestrictedNetworking);
119*ec63e07aSXin Li   // This allows unrestricted network access by not creating a network
120*ec63e07aSXin Li   // namespace.
121*ec63e07aSXin Li   //
122*ec63e07aSXin Li   // Each type T is defined in an individual library and individually visibility
123*ec63e07aSXin Li   // restricted.
124*ec63e07aSXin Li   template <typename... T>
Allow(T...tags)125*ec63e07aSXin Li   PolicyBuilder& Allow(T... tags) {
126*ec63e07aSXin Li     return (Allow(tags), ...);
127*ec63e07aSXin Li   }
128*ec63e07aSXin Li 
129*ec63e07aSXin Li   // Allows unrestricted access to the network by *not* creating a network
130*ec63e07aSXin Li   // namespace. Note that this only disables the network namespace. To
131*ec63e07aSXin Li   // actually allow networking, you would also need to allow networking
132*ec63e07aSXin Li   // syscalls. Calling this function will enable use of namespaces
133*ec63e07aSXin Li   PolicyBuilder& Allow(UnrestrictedNetworking tag);
134*ec63e07aSXin Li 
135*ec63e07aSXin Li   // Appends code to allow a specific syscall
136*ec63e07aSXin Li   PolicyBuilder& AllowSyscall(uint32_t num);
137*ec63e07aSXin Li 
138*ec63e07aSXin Li   // Appends code to allow a number of syscalls
139*ec63e07aSXin Li   PolicyBuilder& AllowSyscalls(absl::Span<const uint32_t> nums);
140*ec63e07aSXin Li 
141*ec63e07aSXin Li   // Appends code to block a syscalls while setting errno to the error given.
142*ec63e07aSXin Li   PolicyBuilder& BlockSyscallsWithErrno(absl::Span<const uint32_t> nums,
143*ec63e07aSXin Li                                         int error);
144*ec63e07aSXin Li 
145*ec63e07aSXin Li   // Appends code to block a specific syscall and setting errno.
146*ec63e07aSXin Li   PolicyBuilder& BlockSyscallWithErrno(uint32_t num, int error);
147*ec63e07aSXin Li 
148*ec63e07aSXin Li   // Appends code to allow waiting for events on epoll file descriptors.
149*ec63e07aSXin Li   // Allows these syscalls:
150*ec63e07aSXin Li   // - epoll_wait
151*ec63e07aSXin Li   // - epoll_pwait
152*ec63e07aSXin Li   // - epoll_pwait2
153*ec63e07aSXin Li   PolicyBuilder& AllowEpollWait();
154*ec63e07aSXin Li 
155*ec63e07aSXin Li   // Appends code to allow using epoll.
156*ec63e07aSXin Li   // Allows these syscalls:
157*ec63e07aSXin Li   // - epoll_create
158*ec63e07aSXin Li   // - epoll_create1
159*ec63e07aSXin Li   // - epoll_ctl
160*ec63e07aSXin Li   // - epoll_wait
161*ec63e07aSXin Li   // - epoll_pwait
162*ec63e07aSXin Li   // - epoll_pwait2
163*ec63e07aSXin Li   PolicyBuilder& AllowEpoll();
164*ec63e07aSXin Li 
165*ec63e07aSXin Li   // Appends code to allow initializing an inotify instance.
166*ec63e07aSXin Li   // Allows these syscalls:
167*ec63e07aSXin Li   // - inotify_init
168*ec63e07aSXin Li   // - inotify_init1
169*ec63e07aSXin Li   PolicyBuilder& AllowInotifyInit();
170*ec63e07aSXin Li 
171*ec63e07aSXin Li   // Appends code to allow synchronous I/O multiplexing.
172*ec63e07aSXin Li   // Allows these syscalls:
173*ec63e07aSXin Li   // - pselect6
174*ec63e07aSXin Li   // - select
175*ec63e07aSXin Li   PolicyBuilder& AllowSelect();
176*ec63e07aSXin Li 
177*ec63e07aSXin Li   // Appends code to allow exiting.
178*ec63e07aSXin Li   // Allows these syscalls:
179*ec63e07aSXin Li   // - exit
180*ec63e07aSXin Li   // - exit_group
181*ec63e07aSXin Li   PolicyBuilder& AllowExit();
182*ec63e07aSXin Li 
183*ec63e07aSXin Li   // Appends code to allow restartable sequences and necessary /proc files.
184*ec63e07aSXin Li   // Allows these syscalls:
185*ec63e07aSXin Li   // - rseq
186*ec63e07aSXin Li   // - mmap(..., PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, ...)
187*ec63e07aSXin Li   // - getcpu
188*ec63e07aSXin Li   // - membarrier
189*ec63e07aSXin Li   // - futex(WAIT)
190*ec63e07aSXin Li   // - futex(WAKE)
191*ec63e07aSXin Li   // - rt_sigprocmask(SIG_SETMASK)
192*ec63e07aSXin Li   // Allows these files:
193*ec63e07aSXin Li   // - "/proc/cpuinfo"
194*ec63e07aSXin Li   // - "/proc/stat"
195*ec63e07aSXin Li   // And this directory (including subdirs/files):
196*ec63e07aSXin Li   // - "/sys/devices/system/cpu/"
197*ec63e07aSXin Li   //
198*ec63e07aSXin Li   // If `cpu_fence_mode` is `kAllowSlowFences`, also permits slow CPU fences.
199*ec63e07aSXin Li   // Allows these syscalls:
200*ec63e07aSXin Li   // - sched_getaffinity
201*ec63e07aSXin Li   // - sched_setaffinity
202*ec63e07aSXin Li   // Allows these files:
203*ec63e07aSXin Li   // - "/proc/self/cpuset"
204*ec63e07aSXin Li   //
205*ec63e07aSXin Li   // If `cpu_fence_mode` is `kRequireFastFences`, RSEQ functionality may not
206*ec63e07aSXin Li   // be enabled if fast CPU fences are not available.
207*ec63e07aSXin Li   PolicyBuilder& AllowRestartableSequences(CpuFenceMode cpu_fence_mode);
208*ec63e07aSXin Li   ABSL_DEPRECATED("Use AllowRestartableSequences() instead")
AllowRestartableSequencesWithProcFiles(CpuFenceMode cpu_fence_mode)209*ec63e07aSXin Li   PolicyBuilder& AllowRestartableSequencesWithProcFiles(
210*ec63e07aSXin Li       CpuFenceMode cpu_fence_mode) {
211*ec63e07aSXin Li     return this->AllowRestartableSequences(cpu_fence_mode);
212*ec63e07aSXin Li   }
213*ec63e07aSXin Li 
214*ec63e07aSXin Li   // Appends code to allow the scudo version of malloc, free and
215*ec63e07aSXin Li   // friends. This should be used in conjunction with namespaces. If scudo
216*ec63e07aSXin Li   // options are passed to the sandboxee through an environment variable, access
217*ec63e07aSXin Li   // to "/proc/self/environ" will have to be allowed by the policy.
218*ec63e07aSXin Li   //
219*ec63e07aSXin Li   // Note: This function is tuned towards the secure scudo allocator. If you are
220*ec63e07aSXin Li   //       using another implementation, this function might not be the most
221*ec63e07aSXin Li   //       suitable.
222*ec63e07aSXin Li   PolicyBuilder& AllowScudoMalloc();
223*ec63e07aSXin Li 
224*ec63e07aSXin Li   // Appends code to allow the system-allocator version of malloc, free and
225*ec63e07aSXin Li   // friends.
226*ec63e07aSXin Li   //
227*ec63e07aSXin Li   // Note: This function is tuned towards the malloc implementation in glibc. If
228*ec63e07aSXin Li   //       you are using another implementation, this function might not be the
229*ec63e07aSXin Li   //       most suitable.
230*ec63e07aSXin Li   PolicyBuilder& AllowSystemMalloc();
231*ec63e07aSXin Li 
232*ec63e07aSXin Li   // Appends code to allow the tcmalloc version of malloc, free and
233*ec63e07aSXin Li   // friends.
234*ec63e07aSXin Li   PolicyBuilder& AllowTcMalloc();
235*ec63e07aSXin Li 
236*ec63e07aSXin Li   // Allows system calls typically used by the LLVM sanitizers (address
237*ec63e07aSXin Li   // sanitizer, memory sanitizer, and thread sanitizer). This method is
238*ec63e07aSXin Li   // intended as a best effort for adding system calls that are common to many
239*ec63e07aSXin Li   // binaries. It may not be fully inclusive of all potential system calls for
240*ec63e07aSXin Li   // all binaries.
241*ec63e07aSXin Li   PolicyBuilder& AllowLlvmSanitizers();
242*ec63e07aSXin Li 
243*ec63e07aSXin Li   // Allows system calls typically used by the LLVM coverage.
244*ec63e07aSXin Li   // This method is intended as a best effort.
245*ec63e07aSXin Li   PolicyBuilder& AllowLlvmCoverage();
246*ec63e07aSXin Li 
247*ec63e07aSXin Li   // Appends code to allow mmap. Specifically this allows mmap and mmap2 syscall
248*ec63e07aSXin Li   // on architectures where this syscalls exist.
249*ec63e07aSXin Li   // Prefer using AllowMmapWithoutExec as allowing mapping executable pages
250*ec63e07aSXin Li   // makes exploitation easier.
251*ec63e07aSXin Li   PolicyBuilder& AllowMmap();
252*ec63e07aSXin Li 
253*ec63e07aSXin Li   // Appends code to allow mmap calls that don't specify PROT_EXEC.
254*ec63e07aSXin Li   PolicyBuilder& AllowMmapWithoutExec();
255*ec63e07aSXin Li 
256*ec63e07aSXin Li   // Appends code to allow calling futex with the given operation.
257*ec63e07aSXin Li   PolicyBuilder& AllowFutexOp(int op);
258*ec63e07aSXin Li 
259*ec63e07aSXin Li   // Appends code to allow opening and possibly creating files or directories.
260*ec63e07aSXin Li   // Allows these syscalls:
261*ec63e07aSXin Li   // - creat
262*ec63e07aSXin Li   // - open
263*ec63e07aSXin Li   // - openat
264*ec63e07aSXin Li   PolicyBuilder& AllowOpen();
265*ec63e07aSXin Li 
266*ec63e07aSXin Li   // Appends code to allow calling stat, fstat and lstat.
267*ec63e07aSXin Li   // Allows these syscalls:
268*ec63e07aSXin Li   // - fstat
269*ec63e07aSXin Li   // - fstat64
270*ec63e07aSXin Li   // - fstatat
271*ec63e07aSXin Li   // - fstatat64
272*ec63e07aSXin Li   // - fstatfs
273*ec63e07aSXin Li   // - fstatfs64
274*ec63e07aSXin Li   // - lstat
275*ec63e07aSXin Li   // - lstat64
276*ec63e07aSXin Li   // - newfstatat
277*ec63e07aSXin Li   // - oldfstat
278*ec63e07aSXin Li   // - oldlstat
279*ec63e07aSXin Li   // - oldstat
280*ec63e07aSXin Li   // - stat
281*ec63e07aSXin Li   // - stat64
282*ec63e07aSXin Li   // - statfs
283*ec63e07aSXin Li   // - statfs64
284*ec63e07aSXin Li   // - ustat
285*ec63e07aSXin Li   PolicyBuilder& AllowStat();
286*ec63e07aSXin Li 
287*ec63e07aSXin Li   // Appends code to allow checking file permissions.
288*ec63e07aSXin Li   // Allows these syscalls:
289*ec63e07aSXin Li   // - access
290*ec63e07aSXin Li   // - faccessat
291*ec63e07aSXin Li   PolicyBuilder& AllowAccess();
292*ec63e07aSXin Li 
293*ec63e07aSXin Li   // Appends code to allow duplicating file descriptors.
294*ec63e07aSXin Li   // Allows these syscalls:
295*ec63e07aSXin Li   // - dup
296*ec63e07aSXin Li   // - dup2
297*ec63e07aSXin Li   // - dup3
298*ec63e07aSXin Li   PolicyBuilder& AllowDup();
299*ec63e07aSXin Li 
300*ec63e07aSXin Li   // Appends code to allow creating pipes.
301*ec63e07aSXin Li   // Allows these syscalls:
302*ec63e07aSXin Li   // - pipe
303*ec63e07aSXin Li   // - pipe2
304*ec63e07aSXin Li   PolicyBuilder& AllowPipe();
305*ec63e07aSXin Li 
306*ec63e07aSXin Li   // Appends code to allow changing file permissions.
307*ec63e07aSXin Li   // Allows these syscalls:
308*ec63e07aSXin Li   // - chmod
309*ec63e07aSXin Li   // - fchmod
310*ec63e07aSXin Li   // - fchmodat
311*ec63e07aSXin Li   PolicyBuilder& AllowChmod();
312*ec63e07aSXin Li 
313*ec63e07aSXin Li   // Appends code to allow changing file ownership.
314*ec63e07aSXin Li   // Allows these syscalls:
315*ec63e07aSXin Li   // - chown
316*ec63e07aSXin Li   // - lchown
317*ec63e07aSXin Li   // - fchown
318*ec63e07aSXin Li   // - fchownat
319*ec63e07aSXin Li   PolicyBuilder& AllowChown();
320*ec63e07aSXin Li 
321*ec63e07aSXin Li   // Appends code to the policy to allow reading from file descriptors.
322*ec63e07aSXin Li   // Allows these syscalls:
323*ec63e07aSXin Li   // - read
324*ec63e07aSXin Li   // - readv
325*ec63e07aSXin Li   // - preadv
326*ec63e07aSXin Li   // - pread64
327*ec63e07aSXin Li   PolicyBuilder& AllowRead();
328*ec63e07aSXin Li 
329*ec63e07aSXin Li   // Appends code to the policy to allow writing to file descriptors.
330*ec63e07aSXin Li   // Allows these syscalls:
331*ec63e07aSXin Li   // - write
332*ec63e07aSXin Li   // - writev
333*ec63e07aSXin Li   // - pwritev
334*ec63e07aSXin Li   // - pwrite64
335*ec63e07aSXin Li   PolicyBuilder& AllowWrite();
336*ec63e07aSXin Li 
337*ec63e07aSXin Li   // Appends code to allow reading directories.
338*ec63e07aSXin Li   // Allows these syscalls:
339*ec63e07aSXin Li   // - getdents
340*ec63e07aSXin Li   // - getdents64
341*ec63e07aSXin Li   PolicyBuilder& AllowReaddir();
342*ec63e07aSXin Li 
343*ec63e07aSXin Li   // Appends code to allow reading symbolic links.
344*ec63e07aSXin Li   // Allows these syscalls:
345*ec63e07aSXin Li   // - readlink
346*ec63e07aSXin Li   // - readlinkat
347*ec63e07aSXin Li   PolicyBuilder& AllowReadlink();
348*ec63e07aSXin Li 
349*ec63e07aSXin Li   // Appends code to allow creating links.
350*ec63e07aSXin Li   // Allows these syscalls:
351*ec63e07aSXin Li   // - link
352*ec63e07aSXin Li   // - linkat
353*ec63e07aSXin Li   PolicyBuilder& AllowLink();
354*ec63e07aSXin Li 
355*ec63e07aSXin Li   // Appends code to allow creating symbolic links.
356*ec63e07aSXin Li   // Allows these syscalls:
357*ec63e07aSXin Li   // - symlink
358*ec63e07aSXin Li   // - symlinkat
359*ec63e07aSXin Li   PolicyBuilder& AllowSymlink();
360*ec63e07aSXin Li 
361*ec63e07aSXin Li   // Appends code to allow creating directories.
362*ec63e07aSXin Li   // Allows these syscalls:
363*ec63e07aSXin Li   // - mkdir
364*ec63e07aSXin Li   // - mkdirat
365*ec63e07aSXin Li   PolicyBuilder& AllowMkdir();
366*ec63e07aSXin Li 
367*ec63e07aSXin Li   // Appends code to allow changing file timestamps.
368*ec63e07aSXin Li   // Allows these syscalls:
369*ec63e07aSXin Li   // - futimens
370*ec63e07aSXin Li   // - utime
371*ec63e07aSXin Li   // - utimensat
372*ec63e07aSXin Li   // - utimes
373*ec63e07aSXin Li   PolicyBuilder& AllowUtime();
374*ec63e07aSXin Li 
375*ec63e07aSXin Li   // Appends code to allow safe calls to fcntl.
376*ec63e07aSXin Li   // Allows these syscalls:
377*ec63e07aSXin Li   // - fcntl
378*ec63e07aSXin Li   // - fcntl64 (on architectures where it exists)
379*ec63e07aSXin Li   //
380*ec63e07aSXin Li   // The above are only allowed when the cmd is one of:
381*ec63e07aSXin Li   // F_GETFD, F_SETFD, F_GETFL, F_SETFL, F_GETLK, F_SETLKW, F_SETLK,
382*ec63e07aSXin Li   // F_DUPFD, F_DUPFD_CLOEXEC
383*ec63e07aSXin Li   PolicyBuilder& AllowSafeFcntl();
384*ec63e07aSXin Li 
385*ec63e07aSXin Li   // Appends code to allow creating new processes.
386*ec63e07aSXin Li   // Allows these syscalls:
387*ec63e07aSXin Li   // - fork
388*ec63e07aSXin Li   // - vfork
389*ec63e07aSXin Li   // - clone
390*ec63e07aSXin Li   //
391*ec63e07aSXin Li   // Note: while this function allows the calls, the default policy is run first
392*ec63e07aSXin Li   // and it has checks for dangerous flags which can create a violation. See
393*ec63e07aSXin Li   // sandbox2/policy.cc for more details.
394*ec63e07aSXin Li   PolicyBuilder& AllowFork();
395*ec63e07aSXin Li 
396*ec63e07aSXin Li   // Appends code to allow waiting for processes.
397*ec63e07aSXin Li   // Allows these syscalls:
398*ec63e07aSXin Li   // - waitpid (on architectures where it exists)
399*ec63e07aSXin Li   // - wait4
400*ec63e07aSXin Li   PolicyBuilder& AllowWait();
401*ec63e07aSXin Li 
402*ec63e07aSXin Li   // Appends code to allow setting alarms / interval timers.
403*ec63e07aSXin Li   // Allows these syscalls:
404*ec63e07aSXin Li   // - alarm (on architectures where it exists)
405*ec63e07aSXin Li   // - setitimer
406*ec63e07aSXin Li   PolicyBuilder& AllowAlarm();
407*ec63e07aSXin Li 
408*ec63e07aSXin Li   // Appends code to allow setting up signal handlers, returning from them, etc.
409*ec63e07aSXin Li   // Allows these syscalls:
410*ec63e07aSXin Li   // - rt_sigaction
411*ec63e07aSXin Li   // - rt_sigreturn
412*ec63e07aSXin Li   // - rt_procmask
413*ec63e07aSXin Li   // - signal (on architectures where it exists)
414*ec63e07aSXin Li   // - sigaction (on architectures where it exists)
415*ec63e07aSXin Li   // - sigreturn (on architectures where it exists)
416*ec63e07aSXin Li   // - sigprocmask (on architectures where it exists)
417*ec63e07aSXin Li   PolicyBuilder& AllowHandleSignals();
418*ec63e07aSXin Li 
419*ec63e07aSXin Li   // Appends code to allow doing the TCGETS ioctl.
420*ec63e07aSXin Li   // Allows these syscalls:
421*ec63e07aSXin Li   // - ioctl (when the first argument is TCGETS)
422*ec63e07aSXin Li   PolicyBuilder& AllowTCGETS();
423*ec63e07aSXin Li 
424*ec63e07aSXin Li   // Appends code to allow to getting the current time.
425*ec63e07aSXin Li   // Allows these syscalls:
426*ec63e07aSXin Li   // - time
427*ec63e07aSXin Li   // - gettimeofday
428*ec63e07aSXin Li   // - clock_gettime
429*ec63e07aSXin Li   PolicyBuilder& AllowTime();
430*ec63e07aSXin Li 
431*ec63e07aSXin Li   // Appends code to allow sleeping in the current thread.
432*ec63e07aSXin Li   // Allow these syscalls:
433*ec63e07aSXin Li   // - clock_nanosleep
434*ec63e07aSXin Li   // - nanosleep
435*ec63e07aSXin Li   PolicyBuilder& AllowSleep();
436*ec63e07aSXin Li 
437*ec63e07aSXin Li   // Appends code to allow getting the uid, euid, gid, etc.
438*ec63e07aSXin Li   // - getuid + geteuid + getresuid
439*ec63e07aSXin Li   // - getgid + getegid + getresgid
440*ec63e07aSXin Li   // - getuid32 + geteuid32 + getresuid32 (on architectures where they exist)
441*ec63e07aSXin Li   // - getgid32 + getegid32 + getresgid32 (on architectures where they exist)
442*ec63e07aSXin Li   // - getgroups
443*ec63e07aSXin Li   PolicyBuilder& AllowGetIDs();
444*ec63e07aSXin Li 
445*ec63e07aSXin Li   // Appends code to allow getting the pid, ppid and tid.
446*ec63e07aSXin Li   // Allows these syscalls:
447*ec63e07aSXin Li   // - getpid
448*ec63e07aSXin Li   // - getppid
449*ec63e07aSXin Li   // - gettid
450*ec63e07aSXin Li   PolicyBuilder& AllowGetPIDs();
451*ec63e07aSXin Li 
452*ec63e07aSXin Li   // Appends code to allow getting process groups.
453*ec63e07aSXin Li   // Allows these syscalls:
454*ec63e07aSXin Li   // - getpgid
455*ec63e07aSXin Li   // - getpgrp
456*ec63e07aSXin Li   PolicyBuilder& AllowGetPGIDs();
457*ec63e07aSXin Li 
458*ec63e07aSXin Li   // Appends code to allow getting the rlimits.
459*ec63e07aSXin Li   // Allows these syscalls:
460*ec63e07aSXin Li   // - getrlimit
461*ec63e07aSXin Li   // - ugetrlimit (on architectures where it exist)
462*ec63e07aSXin Li   PolicyBuilder& AllowGetRlimit();
463*ec63e07aSXin Li 
464*ec63e07aSXin Li   // Appends code to allow setting the rlimits.
465*ec63e07aSXin Li   // Allows these syscalls:
466*ec63e07aSXin Li   // - setrlimit
467*ec63e07aSXin Li   // - usetrlimit (on architectures where it exist)
468*ec63e07aSXin Li   PolicyBuilder& AllowSetRlimit();
469*ec63e07aSXin Li 
470*ec63e07aSXin Li   // Appends code to allow reading random bytes.
471*ec63e07aSXin Li   // Allows these syscalls:
472*ec63e07aSXin Li   // - getrandom (with no flags or GRND_NONBLOCK)
473*ec63e07aSXin Li   //
474*ec63e07aSXin Li   PolicyBuilder& AllowGetRandom();
475*ec63e07aSXin Li 
476*ec63e07aSXin Li   // Appends code to allow configuring wipe-on-fork memory
477*ec63e07aSXin Li   // Allows these syscalls:
478*ec63e07aSXin Li   // - madvise (with advice equal to -1 or MADV_WIPEONFORK).
479*ec63e07aSXin Li   PolicyBuilder& AllowWipeOnFork();
480*ec63e07aSXin Li 
481*ec63e07aSXin Li   // Enables syscalls required to use the logging support enabled via
482*ec63e07aSXin Li   // Client::SendLogsToSupervisor()
483*ec63e07aSXin Li   // Allows the following:
484*ec63e07aSXin Li   // - Writes
485*ec63e07aSXin Li   // - kill(0, SIGABRT) (for LOG(FATAL))
486*ec63e07aSXin Li   // - clock_gettime
487*ec63e07aSXin Li   // - gettid
488*ec63e07aSXin Li   // - close
489*ec63e07aSXin Li   PolicyBuilder& AllowLogForwarding();
490*ec63e07aSXin Li 
491*ec63e07aSXin Li   // Appends code to allow deleting files and directories.
492*ec63e07aSXin Li   // Allows these syscalls:
493*ec63e07aSXin Li   // - rmdir (if available)
494*ec63e07aSXin Li   // - unlink (if available)
495*ec63e07aSXin Li   // - unlinkat
496*ec63e07aSXin Li   PolicyBuilder& AllowUnlink();
497*ec63e07aSXin Li 
498*ec63e07aSXin Li   // Appends code to allow renaming files
499*ec63e07aSXin Li   // Allows these syscalls:
500*ec63e07aSXin Li   // - rename (if available)
501*ec63e07aSXin Li   // - renameat
502*ec63e07aSXin Li   // - renameat2
503*ec63e07aSXin Li   PolicyBuilder& AllowRename();
504*ec63e07aSXin Li 
505*ec63e07aSXin Li   // Appends code to allow creating event notification file descriptors.
506*ec63e07aSXin Li   // Allows these syscalls:
507*ec63e07aSXin Li   // - eventfd (if available)
508*ec63e07aSXin Li   // - eventfd2
509*ec63e07aSXin Li   PolicyBuilder& AllowEventFd();
510*ec63e07aSXin Li 
511*ec63e07aSXin Li   // Appends code to allow polling files.
512*ec63e07aSXin Li   // Allows these syscalls:
513*ec63e07aSXin Li   // - poll (if available)
514*ec63e07aSXin Li   // - ppoll
515*ec63e07aSXin Li   PolicyBuilder& AllowPoll();
516*ec63e07aSXin Li 
517*ec63e07aSXin Li   // Appends code to allow setting the name of a thread
518*ec63e07aSXin Li   // Allows the following
519*ec63e07aSXin Li   // - prctl(PR_SET_NAME, ...)
520*ec63e07aSXin Li   PolicyBuilder& AllowPrctlSetName();
521*ec63e07aSXin Li 
522*ec63e07aSXin Li   // Appends code to allow setting a name for an anonymous memory region.
523*ec63e07aSXin Li   // Allows the following
524*ec63e07aSXin Li   // - prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ...)
525*ec63e07aSXin Li   PolicyBuilder& AllowPrctlSetVma();
526*ec63e07aSXin Li 
527*ec63e07aSXin Li   // Enables the syscalls necessary to start a statically linked binary
528*ec63e07aSXin Li   //
529*ec63e07aSXin Li   // NOTE: This will call BlockSyscallWithErrno(__NR_readlink, ENOENT). If you
530*ec63e07aSXin Li   // do not want readlink blocked, put a different call before this call.
531*ec63e07aSXin Li   //
532*ec63e07aSXin Li   // The current list of allowed syscalls are below. However you should *not*
533*ec63e07aSXin Li   // depend on the specifics, as these will change whenever the startup code
534*ec63e07aSXin Li   // changes.
535*ec63e07aSXin Li   //
536*ec63e07aSXin Li   // - uname,
537*ec63e07aSXin Li   // - brk,
538*ec63e07aSXin Li   // - set_tid_address,
539*ec63e07aSXin Li   // - set_robust_list,
540*ec63e07aSXin Li   // - futex(FUTEX_WAIT_BITSET, ...)
541*ec63e07aSXin Li   // - rt_sigaction(0x20, ...)
542*ec63e07aSXin Li   // - rt_sigaction(0x21, ...)
543*ec63e07aSXin Li   // - rt_sigprocmask(SIG_UNBLOCK, ...)
544*ec63e07aSXin Li   // - arch_prctl(ARCH_SET_FS)
545*ec63e07aSXin Li   //
546*ec63e07aSXin Li   // Additionally it will block calls to readlink.
547*ec63e07aSXin Li   PolicyBuilder& AllowStaticStartup();
548*ec63e07aSXin Li 
549*ec63e07aSXin Li   // In addition to syscalls allowed by AllowStaticStartup, also allow reading,
550*ec63e07aSXin Li   // seeking, mmapping and closing files. It does not allow opening them, as
551*ec63e07aSXin Li   // the mechanism for doing so depends on whether GetFs-checks are used or not.
552*ec63e07aSXin Li   PolicyBuilder& AllowDynamicStartup();
553*ec63e07aSXin Li 
554*ec63e07aSXin Li   // Appends a policy, which will be run on the specified syscall.
555*ec63e07aSXin Li   // This policy must be written without labels. If you need labels, use
556*ec63e07aSXin Li   // the overloaded function passing a BpfFunc object instead of the
557*ec63e07aSXin Li   // sock_filter.
558*ec63e07aSXin Li   PolicyBuilder& AddPolicyOnSyscall(uint32_t num,
559*ec63e07aSXin Li                                     absl::Span<const sock_filter> policy);
560*ec63e07aSXin Li 
561*ec63e07aSXin Li   // Appends a policy, which will be run on the specified syscall.
562*ec63e07aSXin Li   // This policy may use labels.
563*ec63e07aSXin Li   // Example of how to use it:
564*ec63e07aSXin Li   //  builder.AddPolicyOnSyscall(
565*ec63e07aSXin Li   //      __NR_socket, [](bpf_labels& labels) -> std::vector<sock_filter> {
566*ec63e07aSXin Li   //        return {
567*ec63e07aSXin Li   //            ARG(0),  // domain is first argument of socket
568*ec63e07aSXin Li   //            JEQ(AF_UNIX, JUMP(&labels, af_unix)),
569*ec63e07aSXin Li   //            JEQ(AF_NETLINK, JUMP(&labels, af_netlink)),
570*ec63e07aSXin Li   //            KILL,
571*ec63e07aSXin Li   //
572*ec63e07aSXin Li   //            LABEL(&labels, af_unix),
573*ec63e07aSXin Li   //            ARG(1),
574*ec63e07aSXin Li   //            JEQ(SOCK_STREAM | SOCK_NONBLOCK, ALLOW),
575*ec63e07aSXin Li   //            KILL,
576*ec63e07aSXin Li   //
577*ec63e07aSXin Li   //            LABEL(&labels, af_netlink),
578*ec63e07aSXin Li   //            ARG(2),
579*ec63e07aSXin Li   //            JEQ(NETLINK_ROUTE, ALLOW),
580*ec63e07aSXin Li   //        };
581*ec63e07aSXin Li   //      });
582*ec63e07aSXin Li   PolicyBuilder& AddPolicyOnSyscall(uint32_t num, BpfFunc f);
583*ec63e07aSXin Li 
584*ec63e07aSXin Li   // Appends a policy, which will be run on the specified syscalls.
585*ec63e07aSXin Li   // This policy must be written without labels.
586*ec63e07aSXin Li   PolicyBuilder& AddPolicyOnSyscalls(absl::Span<const uint32_t> nums,
587*ec63e07aSXin Li                                      absl::Span<const sock_filter> policy);
588*ec63e07aSXin Li 
589*ec63e07aSXin Li   // Appends a policy, which will be run on the specified syscalls.
590*ec63e07aSXin Li   // This policy may use labels.
591*ec63e07aSXin Li   PolicyBuilder& AddPolicyOnSyscalls(absl::Span<const uint32_t> nums,
592*ec63e07aSXin Li                                      BpfFunc f);
593*ec63e07aSXin Li 
594*ec63e07aSXin Li   // Equivalent to AddPolicyOnSyscalls(mmap_syscalls, policy), where
595*ec63e07aSXin Li   // mmap_syscalls is a subset of {__NR_mmap, __NR_mmap2}, which exists on the
596*ec63e07aSXin Li   // target architecture.
597*ec63e07aSXin Li   PolicyBuilder& AddPolicyOnMmap(absl::Span<const sock_filter> policy);
598*ec63e07aSXin Li 
599*ec63e07aSXin Li   // Equivalent to AddPolicyOnSyscalls(mmap_syscalls, f), where mmap_syscalls
600*ec63e07aSXin Li   // is a subset of {__NR_mmap, __NR_mmap2}, which exists on the target
601*ec63e07aSXin Li   // architecture.
602*ec63e07aSXin Li   PolicyBuilder& AddPolicyOnMmap(BpfFunc f);
603*ec63e07aSXin Li 
604*ec63e07aSXin Li   // Builds the policy returning a unique_ptr to it. This should only be
605*ec63e07aSXin Li   // called once.
606*ec63e07aSXin Li   absl::StatusOr<std::unique_ptr<Policy>> TryBuild();
607*ec63e07aSXin Li 
608*ec63e07aSXin Li   // Builds the policy returning a unique_ptr to it. This should only be
609*ec63e07aSXin Li   // called once. This function will abort if an error happened in any of the
610*ec63e07aSXin Li   // PolicyBuilder methods.
BuildOrDie()611*ec63e07aSXin Li   std::unique_ptr<Policy> BuildOrDie() { return TryBuild().value(); }
612*ec63e07aSXin Li 
613*ec63e07aSXin Li   // Adds a bind-mount for a file from outside the namespace to inside. This
614*ec63e07aSXin Li   // will also create parent directories inside the namespace if needed.
615*ec63e07aSXin Li   //
616*ec63e07aSXin Li   // Calling these function will enable use of namespaces.
617*ec63e07aSXin Li   PolicyBuilder& AddFile(absl::string_view path, bool is_ro = true);
618*ec63e07aSXin Li   PolicyBuilder& AddFileAt(absl::string_view outside, absl::string_view inside,
619*ec63e07aSXin Li                            bool is_ro = true);
620*ec63e07aSXin Li 
621*ec63e07aSXin Li   // Best-effort function that adds the libraries and linker required by a
622*ec63e07aSXin Li   // binary.
623*ec63e07aSXin Li   //
624*ec63e07aSXin Li   // This does not add the binary itself, only the libraries it depends on.
625*ec63e07aSXin Li   //
626*ec63e07aSXin Li   // This function should work correctly for most binaries, but you might need
627*ec63e07aSXin Li   // to tweak it in some cases.
628*ec63e07aSXin Li   //
629*ec63e07aSXin Li   // This function is safe even for untrusted/potentially malicious binaries.
630*ec63e07aSXin Li   // It adds libraries only from standard library dirs and ld_library_path.
631*ec63e07aSXin Li   //
632*ec63e07aSXin Li   // run `ldd` yourself and use AddFile or AddDirectory.
633*ec63e07aSXin Li   PolicyBuilder& AddLibrariesForBinary(absl::string_view path,
634*ec63e07aSXin Li                                        absl::string_view ld_library_path = {});
635*ec63e07aSXin Li 
636*ec63e07aSXin Li   // Similar to AddLibrariesForBinary, but binary is specified with an open
637*ec63e07aSXin Li   // fd.
638*ec63e07aSXin Li   PolicyBuilder& AddLibrariesForBinary(int fd,
639*ec63e07aSXin Li                                        absl::string_view ld_library_path = {});
640*ec63e07aSXin Li 
641*ec63e07aSXin Li   // Adds a bind-mount for a directory from outside the namespace to
642*ec63e07aSXin Li   // inside.  This will also create parent directories inside the namespace if
643*ec63e07aSXin Li   // needed.
644*ec63e07aSXin Li   //
645*ec63e07aSXin Li   // Calling these function will enable use of namespaces.
646*ec63e07aSXin Li   PolicyBuilder& AddDirectory(absl::string_view path, bool is_ro = true);
647*ec63e07aSXin Li   PolicyBuilder& AddDirectoryAt(absl::string_view outside,
648*ec63e07aSXin Li                                 absl::string_view inside, bool is_ro = true);
649*ec63e07aSXin Li 
650*ec63e07aSXin Li   // Adds a tmpfs inside the namespace. This will also create parent
651*ec63e07aSXin Li   // directories inside the namespace if needed.
652*ec63e07aSXin Li   //
653*ec63e07aSXin Li   // Calling this function will enable use of namespaces.
654*ec63e07aSXin Li   PolicyBuilder& AddTmpfs(absl::string_view inside, size_t size);
655*ec63e07aSXin Li 
656*ec63e07aSXin Li   // Allows unrestricted access to the network by *not* creating a network
657*ec63e07aSXin Li   // namespace. Note that this only disables the network namespace. To
658*ec63e07aSXin Li   // actually allow networking, you would also need to allow networking
659*ec63e07aSXin Li   // syscalls. Calling this function will enable use of namespaces.
660*ec63e07aSXin Li   PolicyBuilder& AllowUnrestrictedNetworking();
661*ec63e07aSXin Li 
662*ec63e07aSXin Li   // Enables the use of namespaces.
663*ec63e07aSXin Li   //
664*ec63e07aSXin Li   // Namespaces are enabled by default.
665*ec63e07aSXin Li   // This is a no-op.
666*ec63e07aSXin Li   ABSL_DEPRECATED("Namespaces are enabled by default; no need to call this")
EnableNamespaces()667*ec63e07aSXin Li   PolicyBuilder& EnableNamespaces() {
668*ec63e07aSXin Li     CHECK(use_namespaces_) << "Namespaces cannot be both disabled and enabled";
669*ec63e07aSXin Li     requires_namespaces_ = true;
670*ec63e07aSXin Li     return *this;
671*ec63e07aSXin Li   }
672*ec63e07aSXin Li 
673*ec63e07aSXin Li   // Disables the use of namespaces.
674*ec63e07aSXin Li   //
675*ec63e07aSXin Li   // Call in order to use Sandbox2 without namespaces.
676*ec63e07aSXin Li   // This is not recommended.
DisableNamespaces()677*ec63e07aSXin Li   PolicyBuilder& DisableNamespaces() {
678*ec63e07aSXin Li     CHECK(!requires_namespaces_)
679*ec63e07aSXin Li         << "Namespaces cannot be both disabled and enabled. You're probably "
680*ec63e07aSXin Li            "using features that implicitly enable namespaces (SetHostname, "
681*ec63e07aSXin Li            "AddFile, AddDirectory, AddDataDependency, AddLibrariesForBinary "
682*ec63e07aSXin Li            "or similar)";
683*ec63e07aSXin Li     use_namespaces_ = false;
684*ec63e07aSXin Li     return *this;
685*ec63e07aSXin Li   }
686*ec63e07aSXin Li 
687*ec63e07aSXin Li   // Set hostname in the network namespace instead of default "sandbox2".
688*ec63e07aSXin Li   //
689*ec63e07aSXin Li   // Calling this function will enable use of namespaces.
690*ec63e07aSXin Li   // It is an error to also call AllowUnrestrictedNetworking.
691*ec63e07aSXin Li   PolicyBuilder& SetHostname(absl::string_view hostname);
692*ec63e07aSXin Li 
693*ec63e07aSXin Li   // Enables/disables stack trace collection on violations.
694*ec63e07aSXin Li   PolicyBuilder& CollectStacktracesOnViolation(bool enable);
695*ec63e07aSXin Li 
696*ec63e07aSXin Li   // Enables/disables stack trace collection on signals (e.g. crashes / killed
697*ec63e07aSXin Li   // from a signal).
698*ec63e07aSXin Li   PolicyBuilder& CollectStacktracesOnSignal(bool enable);
699*ec63e07aSXin Li 
700*ec63e07aSXin Li   // Enables/disables stack trace collection on hitting a timeout.
701*ec63e07aSXin Li   PolicyBuilder& CollectStacktracesOnTimeout(bool enable);
702*ec63e07aSXin Li 
703*ec63e07aSXin Li   // Enables/disables stack trace collection on getting killed by the sandbox
704*ec63e07aSXin Li   // monitor / the user.
705*ec63e07aSXin Li   PolicyBuilder& CollectStacktracesOnKill(bool enable);
706*ec63e07aSXin Li 
707*ec63e07aSXin Li   // Enables/disables stack trace collection on normal process exit.
708*ec63e07aSXin Li   PolicyBuilder& CollectStacktracesOnExit(bool enable);
709*ec63e07aSXin Li 
710*ec63e07aSXin Li   // Changes the default action to ALLOW.
711*ec63e07aSXin Li   // All syscalls not handled explicitly by the policy will thus be allowed.
712*ec63e07aSXin Li   // Do not use in environment with untrusted code and/or data, ask
713*ec63e07aSXin Li   // sandbox-team@ first if unsure.
714*ec63e07aSXin Li   PolicyBuilder& DefaultAction(AllowAllSyscalls);
715*ec63e07aSXin Li 
716*ec63e07aSXin Li   // Changes the default action to SANDBOX2_TRACE.
717*ec63e07aSXin Li   // All syscalls not handled explicitly by the policy will be passed off to
718*ec63e07aSXin Li   // the `sandbox2::Notify` implementation given to the `sandbox2::Sandbox2`
719*ec63e07aSXin Li   // instance.
720*ec63e07aSXin Li   PolicyBuilder& DefaultAction(TraceAllSyscalls);
721*ec63e07aSXin Li 
722*ec63e07aSXin Li   ABSL_DEPRECATED("Use DefaultAction(sandbox2::AllowAllSyscalls()) instead")
723*ec63e07aSXin Li   PolicyBuilder& DangerDefaultAllowAll();
724*ec63e07aSXin Li 
725*ec63e07aSXin Li   // Allows syscalls that are necessary for the NetworkProxyClient
726*ec63e07aSXin Li   PolicyBuilder& AddNetworkProxyPolicy();
727*ec63e07aSXin Li 
728*ec63e07aSXin Li   // Allows syscalls that are necessary for the NetworkProxyClient and
729*ec63e07aSXin Li   // the NetworkProxyHandler
730*ec63e07aSXin Li   PolicyBuilder& AddNetworkProxyHandlerPolicy();
731*ec63e07aSXin Li 
732*ec63e07aSXin Li   // Makes root of the filesystem writeable
733*ec63e07aSXin Li   // Not recommended
734*ec63e07aSXin Li   PolicyBuilder& SetRootWritable();
735*ec63e07aSXin Li 
736*ec63e07aSXin Li   // Changes mounts propagation from MS_PRIVATE to MS_SLAVE.
DangerAllowMountPropagation()737*ec63e07aSXin Li   PolicyBuilder& DangerAllowMountPropagation() {
738*ec63e07aSXin Li     allow_mount_propagation_ = true;
739*ec63e07aSXin Li     return *this;
740*ec63e07aSXin Li   }
741*ec63e07aSXin Li 
742*ec63e07aSXin Li   // Allows connections to this IP.
743*ec63e07aSXin Li   PolicyBuilder& AllowIPv4(const std::string& ip_and_mask, uint32_t port = 0);
744*ec63e07aSXin Li   PolicyBuilder& AllowIPv6(const std::string& ip_and_mask, uint32_t port = 0);
745*ec63e07aSXin Li 
746*ec63e07aSXin Li  private:
747*ec63e07aSXin Li   friend class PolicyBuilderPeer;  // For testing
748*ec63e07aSXin Li   friend class StackTracePeer;
749*ec63e07aSXin Li 
750*ec63e07aSXin Li   static absl::StatusOr<std::string> ValidateAbsolutePath(
751*ec63e07aSXin Li       absl::string_view path);
752*ec63e07aSXin Li   static absl::StatusOr<std::string> ValidatePath(absl::string_view path);
753*ec63e07aSXin Li 
754*ec63e07aSXin Li   // Similar to AddFile(At)/AddDirectory(At) but it won't force use of
755*ec63e07aSXin Li   // namespaces - files will only be added to the namespace if it is not
756*ec63e07aSXin Li   // disabled by the time of TryBuild().
757*ec63e07aSXin Li   PolicyBuilder& AddFileIfNamespaced(absl::string_view path, bool is_ro = true);
758*ec63e07aSXin Li   PolicyBuilder& AddFileAtIfNamespaced(absl::string_view outside,
759*ec63e07aSXin Li                                        absl::string_view inside,
760*ec63e07aSXin Li                                        bool is_ro = true);
761*ec63e07aSXin Li   PolicyBuilder& AddDirectoryIfNamespaced(absl::string_view path,
762*ec63e07aSXin Li                                           bool is_ro = true);
763*ec63e07aSXin Li   PolicyBuilder& AddDirectoryAtIfNamespaced(absl::string_view outside,
764*ec63e07aSXin Li                                             absl::string_view inside,
765*ec63e07aSXin Li                                             bool is_ro = true);
766*ec63e07aSXin Li 
767*ec63e07aSXin Li   // Allows a limited version of madvise
768*ec63e07aSXin Li   PolicyBuilder& AllowLimitedMadvise();
769*ec63e07aSXin Li 
770*ec63e07aSXin Li   // Traps instead of denying ptrace.
771*ec63e07aSXin Li   PolicyBuilder& TrapPtrace();
772*ec63e07aSXin Li 
773*ec63e07aSXin Li   // Appends code to block a specific syscall and setting errno at the end of
774*ec63e07aSXin Li   // the policy - decision taken by user policy take precedence.
775*ec63e07aSXin Li   PolicyBuilder& OverridableBlockSyscallWithErrno(uint32_t num, int error);
776*ec63e07aSXin Li 
SetMounts(Mounts mounts)777*ec63e07aSXin Li   PolicyBuilder& SetMounts(Mounts mounts) {
778*ec63e07aSXin Li     mounts_ = std::move(mounts);
779*ec63e07aSXin Li     return *this;
780*ec63e07aSXin Li   }
781*ec63e07aSXin Li 
782*ec63e07aSXin Li   std::vector<sock_filter> ResolveBpfFunc(BpfFunc f);
783*ec63e07aSXin Li 
784*ec63e07aSXin Li   void StoreDescription(PolicyBuilderDescription* pb_description);
785*ec63e07aSXin Li 
786*ec63e07aSXin Li   // This function returns a PolicyBuilder so that we can use it in the status
787*ec63e07aSXin Li   // macros
788*ec63e07aSXin Li   PolicyBuilder& SetError(const absl::Status& status);
789*ec63e07aSXin Li 
790*ec63e07aSXin Li   Mounts mounts_;
791*ec63e07aSXin Li   bool use_namespaces_ = true;
792*ec63e07aSXin Li   bool requires_namespaces_ = false;
793*ec63e07aSXin Li   bool allow_unrestricted_networking_ = false;
794*ec63e07aSXin Li   bool allow_mount_propagation_ = false;
795*ec63e07aSXin Li   std::string hostname_ = std::string(kDefaultHostname);
796*ec63e07aSXin Li 
797*ec63e07aSXin Li   bool collect_stacktrace_on_violation_ = true;
798*ec63e07aSXin Li   bool collect_stacktrace_on_signal_ = true;
799*ec63e07aSXin Li   bool collect_stacktrace_on_timeout_ = true;
800*ec63e07aSXin Li   bool collect_stacktrace_on_kill_ = false;
801*ec63e07aSXin Li   bool collect_stacktrace_on_exit_ = false;
802*ec63e07aSXin Li 
803*ec63e07aSXin Li   // Seccomp fields
804*ec63e07aSXin Li   std::vector<sock_filter> user_policy_;
805*ec63e07aSXin Li   std::vector<sock_filter> overridable_policy_;
806*ec63e07aSXin Li   std::optional<sock_filter> default_action_;
807*ec63e07aSXin Li   bool user_policy_handles_bpf_ = false;
808*ec63e07aSXin Li   bool user_policy_handles_ptrace_ = false;
809*ec63e07aSXin Li   absl::flat_hash_set<uint32_t> handled_syscalls_;
810*ec63e07aSXin Li 
811*ec63e07aSXin Li   // Error handling
812*ec63e07aSXin Li   absl::Status last_status_;
813*ec63e07aSXin Li   bool already_built_ = false;
814*ec63e07aSXin Li 
815*ec63e07aSXin Li   // Contains list of allowed hosts.
816*ec63e07aSXin Li   absl::optional<AllowedHosts> allowed_hosts_;
817*ec63e07aSXin Li };
818*ec63e07aSXin Li 
819*ec63e07aSXin Li }  // namespace sandbox2
820*ec63e07aSXin Li 
821*ec63e07aSXin Li #endif  // SANDBOXED_API_SANDBOX2_POLICYBUILDER_H_
822