2020-01-17 21:05:03 +08:00
|
|
|
// Copyright 2019 Google LLC
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
2022-01-28 17:38:27 +08:00
|
|
|
// https://www.apache.org/licenses/LICENSE-2.0
|
2019-03-19 00:21:48 +08:00
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
#ifndef SANDBOXED_API_SANDBOX2_POLICYBUILDER_H_
|
|
|
|
#define SANDBOXED_API_SANDBOX2_POLICYBUILDER_H_
|
|
|
|
|
|
|
|
#include <linux/filter.h>
|
|
|
|
|
|
|
|
#include <cstddef>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <cstdint>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <functional>
|
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <utility>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <vector>
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/base/attributes.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/base/macros.h"
|
2021-12-13 17:31:02 +08:00
|
|
|
#include "absl/container/flat_hash_set.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/log/check.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/status/status.h"
|
2020-09-02 23:46:48 +08:00
|
|
|
#include "absl/status/statusor.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/types/optional.h"
|
2021-12-13 17:31:02 +08:00
|
|
|
#include "absl/types/span.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/mounts.h"
|
2020-02-20 23:45:22 +08:00
|
|
|
#include "sandboxed_api/sandbox2/network_proxy/filtering.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/policy.h"
|
|
|
|
|
|
|
|
struct bpf_labels;
|
|
|
|
|
|
|
|
namespace sandbox2 {
|
|
|
|
|
2023-03-01 21:35:51 +08:00
|
|
|
class AllowAllSyscalls;
|
2023-05-04 14:29:00 +08:00
|
|
|
class UnrestrictedNetworking;
|
2023-03-01 21:35:51 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// PolicyBuilder is a helper class to simplify creation of policies. The builder
|
|
|
|
// uses fluent interface for convenience and increased readability of policies.
|
|
|
|
//
|
|
|
|
// To build a policy you simply create a new builder object, call methods on it
|
|
|
|
// specifying what you want and finally call BuildOrDie() to generate you
|
|
|
|
// policy.
|
|
|
|
//
|
|
|
|
// For instance this would generate a simple policy suitable for binaries doing
|
|
|
|
// only computations:
|
|
|
|
//
|
|
|
|
// std::unique_ptr<Policy> policy =
|
|
|
|
// PolicyBuilder()
|
|
|
|
// .AllowRead()
|
|
|
|
// .AllowWrite()
|
|
|
|
// .AllowExit()
|
|
|
|
// .AllowSystemMalloc()
|
|
|
|
// .BuildOrDie();
|
|
|
|
//
|
|
|
|
// Note that operations are executed in the order they are dictated, though in
|
|
|
|
// most cases this has no influence since the operations themselves commute.
|
|
|
|
//
|
|
|
|
// For instance these two policies are equivalent:
|
|
|
|
//
|
|
|
|
// auto policy = PolicyBuilder.AllowRead().AllowWrite().BuildOrDie();
|
|
|
|
// auto policy = PolicyBuilder.AllowWrite().AllowRead().BuildOrDie();
|
|
|
|
//
|
|
|
|
// While these two are not:
|
|
|
|
//
|
|
|
|
// auto policy = PolicyBuilder.AllowRead().BlockSyscallWithErrno(__NR_read, EIO)
|
|
|
|
// .BuildOrDie();
|
|
|
|
// auto policy = PolicyBuilder.BlockSyscallWithErrno(__NR_read, EIO).AllowRead()
|
|
|
|
// .BuildOrDie();
|
|
|
|
//
|
|
|
|
// In fact the first one is equivalent to:
|
|
|
|
//
|
|
|
|
// auto policy = PolicyBuilder.AllowRead().BuildOrDie();
|
|
|
|
//
|
2022-05-27 17:41:42 +08:00
|
|
|
// If you dislike the chained style, it is also possible to write the first
|
2019-03-19 00:21:48 +08:00
|
|
|
// example as this:
|
|
|
|
//
|
|
|
|
// PolicyBuilder builder;
|
|
|
|
// builder.AllowRead();
|
|
|
|
// builder.AllowWrite();
|
|
|
|
// builder.AllowExit();
|
|
|
|
// builder.AllowSystemMalloc();
|
|
|
|
// auto policy = builder.BuildOrDie();
|
|
|
|
//
|
|
|
|
// For a more complicated example, see examples/persistent/persistent_sandbox.cc
|
|
|
|
class PolicyBuilder final {
|
|
|
|
public:
|
2021-04-13 22:14:17 +08:00
|
|
|
// Possible CPU fence modes for `AllowRestartableSequences()`
|
|
|
|
enum CpuFenceMode {
|
|
|
|
// Allow only fast fences for restartable sequences.
|
|
|
|
kRequireFastFences,
|
|
|
|
|
|
|
|
// Allow fast fences as well as slow fences if fast fences are unavailable.
|
|
|
|
kAllowSlowFences,
|
|
|
|
};
|
|
|
|
|
2020-09-10 20:47:32 +08:00
|
|
|
static constexpr absl::string_view kDefaultHostname = "sandbox2";
|
2021-11-11 22:10:12 +08:00
|
|
|
// Seccomp takes a 16-bit filter length, so the limit would be 64k.
|
|
|
|
// We set it lower so that there is for sure some room for the default policy.
|
|
|
|
static constexpr size_t kMaxUserPolicyLength = 30000;
|
2020-09-10 20:47:32 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
using BpfFunc = const std::function<std::vector<sock_filter>(bpf_labels&)>&;
|
2021-12-13 17:31:02 +08:00
|
|
|
|
2023-05-04 14:29:00 +08:00
|
|
|
// Appends code to allow visibility restricted policy functionality.
|
|
|
|
//
|
|
|
|
// For example:
|
|
|
|
// Allow(sandbox2::UnrestrictedNetworking);
|
|
|
|
// This allows unrestricted network access by not creating a network
|
|
|
|
// namespace.
|
|
|
|
//
|
|
|
|
// Each type T is defined in an individual library and individually visibility
|
|
|
|
// restricted.
|
|
|
|
template <typename... T>
|
|
|
|
PolicyBuilder& Allow(T... tags) {
|
|
|
|
return (Allow(tags), ...);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Allows unrestricted access to the network by *not* creating a network
|
|
|
|
// namespace. Note that this only disables the network namespace. To
|
|
|
|
// actually allow networking, you would also need to allow networking
|
|
|
|
// syscalls. Calling this function will enable use of namespaces
|
|
|
|
PolicyBuilder& Allow(UnrestrictedNetworking tag);
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Appends code to allow a specific syscall
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& AllowSyscall(uint32_t num);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Appends code to allow a number of syscalls
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& AllowSyscalls(absl::Span<const uint32_t> nums);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-12-08 22:40:48 +08:00
|
|
|
// Appends code to block a syscalls while setting errno to the error given.
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& BlockSyscallsWithErrno(absl::Span<const uint32_t> nums,
|
2021-12-08 22:40:48 +08:00
|
|
|
int error);
|
|
|
|
|
|
|
|
// Appends code to block a specific syscall and setting errno.
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& BlockSyscallWithErrno(uint32_t num, int error);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
Update PolicyBuilder to include wrappers for more syscall families that differ between platforms.
New wrappers:
- `AllowEpollWait` (`epoll_wait`, `epoll_pwait`, `epoll_pwait2`)
- `AllowInotifyInit` (`inotify_init`, `inotify_init1`)
- `AllowSelect` (`select`, `pselect6`)
- `AllowDup` (`dup`, `dup2`, `dup3`)
- `AllowPipe` (`pipe`, `pipe2`)
- `AllowChmod` (`chmod`, `fchmod`, `fchmodat`)
- `AllowChown` (`chown`, `lchown`, `fchown`, `fchownat`)
- `AllowReadlink` (`readlink`, `readlinkat`)
- `AllowLink` (`link`, `linkat`)
- `AllowSymlink` (`symlink`, `symlinkat`)
- `AllowMkdir` (`mkdir`, `mkdirat`)
- `AllowUtime` (`utime`, `utimes`, `futimens`, `utimensat`)
- `AllowAlarm` (`alarm`, `setitimer`)
- `AllowGetPGIDs` (`getpgid`, `getpgrp`)
- `AllowPoll` (`poll`, `ppoll`)
Updated wrappers:
- `AllowOpen` now includes `creat`. `openat` already grants the ability to create files, and is the designated replacement for `creat` on newer platforms.
- `AllowStat` now includes `fstatfs` and `fstatfs64`. The comment already claimed that these syscalls were included; I believe they were omitted by accident.
- `AllowUnlink` now includes `rmdir`. `unlinkat` already grants the ability to remove empty directories, and is the designated replacement for `rmdir` on newer platforms.
PiperOrigin-RevId: 495045432
Change-Id: I41eccb74fda250b27586b6b7fe4c480332e48846
2022-12-14 01:31:29 +08:00
|
|
|
// Appends code to allow waiting for events on epoll file descriptors.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - epoll_wait
|
|
|
|
// - epoll_pwait
|
|
|
|
// - epoll_pwait2
|
|
|
|
PolicyBuilder& AllowEpollWait();
|
|
|
|
|
2022-03-07 16:53:48 +08:00
|
|
|
// Appends code to allow using epoll.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - epoll_create
|
|
|
|
// - epoll_create1
|
|
|
|
// - epoll_ctl
|
|
|
|
// - epoll_wait
|
|
|
|
// - epoll_pwait
|
|
|
|
// - epoll_pwait2
|
|
|
|
PolicyBuilder& AllowEpoll();
|
|
|
|
|
Update PolicyBuilder to include wrappers for more syscall families that differ between platforms.
New wrappers:
- `AllowEpollWait` (`epoll_wait`, `epoll_pwait`, `epoll_pwait2`)
- `AllowInotifyInit` (`inotify_init`, `inotify_init1`)
- `AllowSelect` (`select`, `pselect6`)
- `AllowDup` (`dup`, `dup2`, `dup3`)
- `AllowPipe` (`pipe`, `pipe2`)
- `AllowChmod` (`chmod`, `fchmod`, `fchmodat`)
- `AllowChown` (`chown`, `lchown`, `fchown`, `fchownat`)
- `AllowReadlink` (`readlink`, `readlinkat`)
- `AllowLink` (`link`, `linkat`)
- `AllowSymlink` (`symlink`, `symlinkat`)
- `AllowMkdir` (`mkdir`, `mkdirat`)
- `AllowUtime` (`utime`, `utimes`, `futimens`, `utimensat`)
- `AllowAlarm` (`alarm`, `setitimer`)
- `AllowGetPGIDs` (`getpgid`, `getpgrp`)
- `AllowPoll` (`poll`, `ppoll`)
Updated wrappers:
- `AllowOpen` now includes `creat`. `openat` already grants the ability to create files, and is the designated replacement for `creat` on newer platforms.
- `AllowStat` now includes `fstatfs` and `fstatfs64`. The comment already claimed that these syscalls were included; I believe they were omitted by accident.
- `AllowUnlink` now includes `rmdir`. `unlinkat` already grants the ability to remove empty directories, and is the designated replacement for `rmdir` on newer platforms.
PiperOrigin-RevId: 495045432
Change-Id: I41eccb74fda250b27586b6b7fe4c480332e48846
2022-12-14 01:31:29 +08:00
|
|
|
// Appends code to allow initializing an inotify instance.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - inotify_init
|
|
|
|
// - inotify_init1
|
|
|
|
PolicyBuilder& AllowInotifyInit();
|
|
|
|
|
|
|
|
// Appends code to allow synchronous I/O multiplexing.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - pselect6
|
|
|
|
// - select
|
|
|
|
PolicyBuilder& AllowSelect();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Appends code to allow exiting.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - exit
|
|
|
|
// - exit_group
|
|
|
|
PolicyBuilder& AllowExit();
|
|
|
|
|
2021-07-23 17:22:18 +08:00
|
|
|
// Appends code to allow restartable sequences and necessary /proc files.
|
2021-03-02 05:39:13 +08:00
|
|
|
// Allows these syscalls:
|
|
|
|
// - rseq
|
2021-07-23 17:22:18 +08:00
|
|
|
// - mmap(..., PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, ...)
|
|
|
|
// - getcpu
|
2021-03-02 05:39:13 +08:00
|
|
|
// - membarrier
|
2021-07-23 17:22:18 +08:00
|
|
|
// - futex(WAIT)
|
|
|
|
// - futex(WAKE)
|
|
|
|
// - rt_sigprocmask(SIG_SETMASK)
|
|
|
|
// Allows these files:
|
|
|
|
// - "/proc/cpuinfo"
|
|
|
|
// - "/proc/stat"
|
2022-04-05 15:28:44 +08:00
|
|
|
// And this directory (including subdirs/files):
|
|
|
|
// - "/sys/devices/system/cpu/"
|
2021-04-13 22:14:17 +08:00
|
|
|
//
|
2021-07-23 17:22:18 +08:00
|
|
|
// If `cpu_fence_mode` is `kAllowSlowFences`, also permits slow CPU fences.
|
|
|
|
// Allows these syscalls:
|
2021-04-13 22:14:17 +08:00
|
|
|
// - sched_getaffinity
|
|
|
|
// - sched_setaffinity
|
2021-07-23 17:22:18 +08:00
|
|
|
// Allows these files:
|
2021-04-13 22:14:17 +08:00
|
|
|
// - "/proc/self/cpuset"
|
|
|
|
//
|
2021-07-23 17:22:18 +08:00
|
|
|
// If `cpu_fence_mode` is `kRequireFastFences`, RSEQ functionality may not
|
|
|
|
// be enabled if fast CPU fences are not available.
|
2021-04-13 22:14:17 +08:00
|
|
|
PolicyBuilder& AllowRestartableSequences(CpuFenceMode cpu_fence_mode);
|
2023-07-17 16:58:05 +08:00
|
|
|
ABSL_DEPRECATED("Use AllowRestartableSequences() instead")
|
|
|
|
PolicyBuilder& AllowRestartableSequencesWithProcFiles(
|
|
|
|
CpuFenceMode cpu_fence_mode) {
|
|
|
|
return this->AllowRestartableSequences(cpu_fence_mode);
|
|
|
|
}
|
2021-03-02 05:39:13 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Appends code to allow the scudo version of malloc, free and
|
|
|
|
// friends. This should be used in conjunction with namespaces. If scudo
|
|
|
|
// options are passed to the sandboxee through an environment variable, access
|
|
|
|
// to "/proc/self/environ" will have to be allowed by the policy.
|
|
|
|
//
|
|
|
|
// Note: This function is tuned towards the secure scudo allocator. If you are
|
|
|
|
// using another implementation, this function might not be the most
|
|
|
|
// suitable.
|
|
|
|
PolicyBuilder& AllowScudoMalloc();
|
|
|
|
|
|
|
|
// Appends code to allow the system-allocator version of malloc, free and
|
|
|
|
// friends.
|
|
|
|
//
|
|
|
|
// Note: This function is tuned towards the malloc implementation in glibc. If
|
|
|
|
// you are using another implementation, this function might not be the
|
|
|
|
// most suitable.
|
|
|
|
PolicyBuilder& AllowSystemMalloc();
|
|
|
|
|
|
|
|
// Appends code to allow the tcmalloc version of malloc, free and
|
|
|
|
// friends.
|
|
|
|
PolicyBuilder& AllowTcMalloc();
|
|
|
|
|
2019-06-07 22:52:51 +08:00
|
|
|
// Allows system calls typically used by the LLVM sanitizers (address
|
|
|
|
// sanitizer, memory sanitizer, and thread sanitizer). This method is
|
|
|
|
// intended as a best effort for adding system calls that are common to many
|
|
|
|
// binaries. It may not be fully inclusive of all potential system calls for
|
|
|
|
// all binaries.
|
|
|
|
PolicyBuilder& AllowLlvmSanitizers();
|
|
|
|
|
2023-08-31 15:43:30 +08:00
|
|
|
// Allows system calls typically used by the LLVM coverage.
|
|
|
|
// This method is intended as a best effort.
|
|
|
|
PolicyBuilder& AllowLlvmCoverage();
|
|
|
|
|
2020-11-06 17:29:52 +08:00
|
|
|
// Appends code to allow mmap. Specifically this allows mmap and mmap2 syscall
|
|
|
|
// on architectures where this syscalls exist.
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& AllowMmap();
|
|
|
|
|
|
|
|
// Appends code to allow calling futex with the given operation.
|
|
|
|
PolicyBuilder& AllowFutexOp(int op);
|
|
|
|
|
Update PolicyBuilder to include wrappers for more syscall families that differ between platforms.
New wrappers:
- `AllowEpollWait` (`epoll_wait`, `epoll_pwait`, `epoll_pwait2`)
- `AllowInotifyInit` (`inotify_init`, `inotify_init1`)
- `AllowSelect` (`select`, `pselect6`)
- `AllowDup` (`dup`, `dup2`, `dup3`)
- `AllowPipe` (`pipe`, `pipe2`)
- `AllowChmod` (`chmod`, `fchmod`, `fchmodat`)
- `AllowChown` (`chown`, `lchown`, `fchown`, `fchownat`)
- `AllowReadlink` (`readlink`, `readlinkat`)
- `AllowLink` (`link`, `linkat`)
- `AllowSymlink` (`symlink`, `symlinkat`)
- `AllowMkdir` (`mkdir`, `mkdirat`)
- `AllowUtime` (`utime`, `utimes`, `futimens`, `utimensat`)
- `AllowAlarm` (`alarm`, `setitimer`)
- `AllowGetPGIDs` (`getpgid`, `getpgrp`)
- `AllowPoll` (`poll`, `ppoll`)
Updated wrappers:
- `AllowOpen` now includes `creat`. `openat` already grants the ability to create files, and is the designated replacement for `creat` on newer platforms.
- `AllowStat` now includes `fstatfs` and `fstatfs64`. The comment already claimed that these syscalls were included; I believe they were omitted by accident.
- `AllowUnlink` now includes `rmdir`. `unlinkat` already grants the ability to remove empty directories, and is the designated replacement for `rmdir` on newer platforms.
PiperOrigin-RevId: 495045432
Change-Id: I41eccb74fda250b27586b6b7fe4c480332e48846
2022-12-14 01:31:29 +08:00
|
|
|
// Appends code to allow opening and possibly creating files or directories.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - creat
|
2019-03-19 00:21:48 +08:00
|
|
|
// - open
|
|
|
|
// - openat
|
|
|
|
PolicyBuilder& AllowOpen();
|
|
|
|
|
|
|
|
// Appends code to allow calling stat, fstat and lstat.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - fstat
|
|
|
|
// - fstat64
|
|
|
|
// - fstatat
|
|
|
|
// - fstatat64
|
|
|
|
// - fstatfs
|
|
|
|
// - fstatfs64
|
|
|
|
// - lstat
|
|
|
|
// - lstat64
|
|
|
|
// - newfstatat
|
|
|
|
// - oldfstat
|
|
|
|
// - oldlstat
|
|
|
|
// - oldstat
|
|
|
|
// - stat
|
|
|
|
// - stat64
|
|
|
|
// - statfs
|
|
|
|
// - statfs64
|
|
|
|
// - ustat
|
|
|
|
PolicyBuilder& AllowStat();
|
|
|
|
|
2020-11-03 18:20:49 +08:00
|
|
|
// Appends code to allow checking file permissions.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - access
|
|
|
|
// - faccessat
|
|
|
|
PolicyBuilder& AllowAccess();
|
|
|
|
|
Update PolicyBuilder to include wrappers for more syscall families that differ between platforms.
New wrappers:
- `AllowEpollWait` (`epoll_wait`, `epoll_pwait`, `epoll_pwait2`)
- `AllowInotifyInit` (`inotify_init`, `inotify_init1`)
- `AllowSelect` (`select`, `pselect6`)
- `AllowDup` (`dup`, `dup2`, `dup3`)
- `AllowPipe` (`pipe`, `pipe2`)
- `AllowChmod` (`chmod`, `fchmod`, `fchmodat`)
- `AllowChown` (`chown`, `lchown`, `fchown`, `fchownat`)
- `AllowReadlink` (`readlink`, `readlinkat`)
- `AllowLink` (`link`, `linkat`)
- `AllowSymlink` (`symlink`, `symlinkat`)
- `AllowMkdir` (`mkdir`, `mkdirat`)
- `AllowUtime` (`utime`, `utimes`, `futimens`, `utimensat`)
- `AllowAlarm` (`alarm`, `setitimer`)
- `AllowGetPGIDs` (`getpgid`, `getpgrp`)
- `AllowPoll` (`poll`, `ppoll`)
Updated wrappers:
- `AllowOpen` now includes `creat`. `openat` already grants the ability to create files, and is the designated replacement for `creat` on newer platforms.
- `AllowStat` now includes `fstatfs` and `fstatfs64`. The comment already claimed that these syscalls were included; I believe they were omitted by accident.
- `AllowUnlink` now includes `rmdir`. `unlinkat` already grants the ability to remove empty directories, and is the designated replacement for `rmdir` on newer platforms.
PiperOrigin-RevId: 495045432
Change-Id: I41eccb74fda250b27586b6b7fe4c480332e48846
2022-12-14 01:31:29 +08:00
|
|
|
// Appends code to allow duplicating file descriptors.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - dup
|
|
|
|
// - dup2
|
|
|
|
// - dup3
|
|
|
|
PolicyBuilder& AllowDup();
|
|
|
|
|
|
|
|
// Appends code to allow creating pipes.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - pipe
|
|
|
|
// - pipe2
|
|
|
|
PolicyBuilder& AllowPipe();
|
|
|
|
|
|
|
|
// Appends code to allow changing file permissions.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - chmod
|
|
|
|
// - fchmod
|
|
|
|
// - fchmodat
|
|
|
|
PolicyBuilder& AllowChmod();
|
|
|
|
|
|
|
|
// Appends code to allow changing file ownership.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - chown
|
|
|
|
// - lchown
|
|
|
|
// - fchown
|
|
|
|
// - fchownat
|
|
|
|
PolicyBuilder& AllowChown();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Appends code to the policy to allow reading from file descriptors.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - read
|
|
|
|
// - readv
|
|
|
|
// - preadv
|
|
|
|
// - pread64
|
|
|
|
PolicyBuilder& AllowRead();
|
|
|
|
|
|
|
|
// Appends code to the policy to allow writing to file descriptors.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - write
|
|
|
|
// - writev
|
|
|
|
// - pwritev
|
|
|
|
// - pwrite64
|
|
|
|
PolicyBuilder& AllowWrite();
|
|
|
|
|
|
|
|
// Appends code to allow reading directories.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - getdents
|
|
|
|
// - getdents64
|
|
|
|
PolicyBuilder& AllowReaddir();
|
|
|
|
|
Update PolicyBuilder to include wrappers for more syscall families that differ between platforms.
New wrappers:
- `AllowEpollWait` (`epoll_wait`, `epoll_pwait`, `epoll_pwait2`)
- `AllowInotifyInit` (`inotify_init`, `inotify_init1`)
- `AllowSelect` (`select`, `pselect6`)
- `AllowDup` (`dup`, `dup2`, `dup3`)
- `AllowPipe` (`pipe`, `pipe2`)
- `AllowChmod` (`chmod`, `fchmod`, `fchmodat`)
- `AllowChown` (`chown`, `lchown`, `fchown`, `fchownat`)
- `AllowReadlink` (`readlink`, `readlinkat`)
- `AllowLink` (`link`, `linkat`)
- `AllowSymlink` (`symlink`, `symlinkat`)
- `AllowMkdir` (`mkdir`, `mkdirat`)
- `AllowUtime` (`utime`, `utimes`, `futimens`, `utimensat`)
- `AllowAlarm` (`alarm`, `setitimer`)
- `AllowGetPGIDs` (`getpgid`, `getpgrp`)
- `AllowPoll` (`poll`, `ppoll`)
Updated wrappers:
- `AllowOpen` now includes `creat`. `openat` already grants the ability to create files, and is the designated replacement for `creat` on newer platforms.
- `AllowStat` now includes `fstatfs` and `fstatfs64`. The comment already claimed that these syscalls were included; I believe they were omitted by accident.
- `AllowUnlink` now includes `rmdir`. `unlinkat` already grants the ability to remove empty directories, and is the designated replacement for `rmdir` on newer platforms.
PiperOrigin-RevId: 495045432
Change-Id: I41eccb74fda250b27586b6b7fe4c480332e48846
2022-12-14 01:31:29 +08:00
|
|
|
// Appends code to allow reading symbolic links.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - readlink
|
|
|
|
// - readlinkat
|
|
|
|
PolicyBuilder& AllowReadlink();
|
|
|
|
|
|
|
|
// Appends code to allow creating links.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - link
|
|
|
|
// - linkat
|
|
|
|
PolicyBuilder& AllowLink();
|
|
|
|
|
|
|
|
// Appends code to allow creating symbolic links.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - symlink
|
|
|
|
// - symlinkat
|
|
|
|
PolicyBuilder& AllowSymlink();
|
|
|
|
|
|
|
|
// Appends code to allow creating directories.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - mkdir
|
|
|
|
// - mkdirat
|
|
|
|
PolicyBuilder& AllowMkdir();
|
|
|
|
|
|
|
|
// Appends code to allow changing file timestamps.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - futimens
|
|
|
|
// - utime
|
|
|
|
// - utimensat
|
|
|
|
// - utimes
|
|
|
|
PolicyBuilder& AllowUtime();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Appends code to allow safe calls to fcntl.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - fcntl
|
|
|
|
// - fcntl64 (on architectures where it exists)
|
|
|
|
//
|
|
|
|
// The above are only allowed when the cmd is one of:
|
|
|
|
// F_GETFD, F_SETFD, F_GETFL, F_SETFL, F_GETLK, F_SETLKW, F_SETLK,
|
|
|
|
// F_DUPFD, F_DUPFD_CLOEXEC
|
|
|
|
PolicyBuilder& AllowSafeFcntl();
|
|
|
|
|
|
|
|
// Appends code to allow creating new processes.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - fork
|
|
|
|
// - vfork
|
|
|
|
// - clone
|
|
|
|
//
|
|
|
|
// Note: while this function allows the calls, the default policy is run first
|
|
|
|
// and it has checks for dangerous flags which can create a violation. See
|
|
|
|
// sandbox2/policy.cc for more details.
|
|
|
|
PolicyBuilder& AllowFork();
|
|
|
|
|
|
|
|
// Appends code to allow waiting for processes.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - waitpid (on architectures where it exists)
|
|
|
|
// - wait4
|
|
|
|
PolicyBuilder& AllowWait();
|
|
|
|
|
Update PolicyBuilder to include wrappers for more syscall families that differ between platforms.
New wrappers:
- `AllowEpollWait` (`epoll_wait`, `epoll_pwait`, `epoll_pwait2`)
- `AllowInotifyInit` (`inotify_init`, `inotify_init1`)
- `AllowSelect` (`select`, `pselect6`)
- `AllowDup` (`dup`, `dup2`, `dup3`)
- `AllowPipe` (`pipe`, `pipe2`)
- `AllowChmod` (`chmod`, `fchmod`, `fchmodat`)
- `AllowChown` (`chown`, `lchown`, `fchown`, `fchownat`)
- `AllowReadlink` (`readlink`, `readlinkat`)
- `AllowLink` (`link`, `linkat`)
- `AllowSymlink` (`symlink`, `symlinkat`)
- `AllowMkdir` (`mkdir`, `mkdirat`)
- `AllowUtime` (`utime`, `utimes`, `futimens`, `utimensat`)
- `AllowAlarm` (`alarm`, `setitimer`)
- `AllowGetPGIDs` (`getpgid`, `getpgrp`)
- `AllowPoll` (`poll`, `ppoll`)
Updated wrappers:
- `AllowOpen` now includes `creat`. `openat` already grants the ability to create files, and is the designated replacement for `creat` on newer platforms.
- `AllowStat` now includes `fstatfs` and `fstatfs64`. The comment already claimed that these syscalls were included; I believe they were omitted by accident.
- `AllowUnlink` now includes `rmdir`. `unlinkat` already grants the ability to remove empty directories, and is the designated replacement for `rmdir` on newer platforms.
PiperOrigin-RevId: 495045432
Change-Id: I41eccb74fda250b27586b6b7fe4c480332e48846
2022-12-14 01:31:29 +08:00
|
|
|
// Appends code to allow setting alarms / interval timers.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - alarm (on architectures where it exists)
|
|
|
|
// - setitimer
|
|
|
|
PolicyBuilder& AllowAlarm();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Appends code to allow setting up signal handlers, returning from them, etc.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - rt_sigaction
|
|
|
|
// - rt_sigreturn
|
|
|
|
// - rt_procmask
|
|
|
|
// - signal (on architectures where it exists)
|
|
|
|
// - sigaction (on architectures where it exists)
|
|
|
|
// - sigreturn (on architectures where it exists)
|
|
|
|
// - sigprocmask (on architectures where it exists)
|
|
|
|
PolicyBuilder& AllowHandleSignals();
|
|
|
|
|
|
|
|
// Appends code to allow doing the TCGETS ioctl.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - ioctl (when the first argument is TCGETS)
|
|
|
|
PolicyBuilder& AllowTCGETS();
|
|
|
|
|
|
|
|
// Appends code to allow to getting the current time.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - time
|
|
|
|
// - gettimeofday
|
|
|
|
// - clock_gettime
|
|
|
|
PolicyBuilder& AllowTime();
|
|
|
|
|
|
|
|
// Appends code to allow sleeping in the current thread.
|
|
|
|
// Allow these syscalls:
|
|
|
|
// - clock_nanosleep
|
|
|
|
// - nanosleep
|
|
|
|
PolicyBuilder& AllowSleep();
|
|
|
|
|
|
|
|
// Appends code to allow getting the uid, euid, gid, etc.
|
|
|
|
// - getuid + geteuid + getresuid
|
|
|
|
// - getgid + getegid + getresgid
|
|
|
|
// - getuid32 + geteuid32 + getresuid32 (on architectures where they exist)
|
|
|
|
// - getgid32 + getegid32 + getresgid32 (on architectures where they exist)
|
|
|
|
// - getgroups
|
|
|
|
PolicyBuilder& AllowGetIDs();
|
|
|
|
|
|
|
|
// Appends code to allow getting the pid, ppid and tid.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - getpid
|
|
|
|
// - getppid
|
|
|
|
// - gettid
|
|
|
|
PolicyBuilder& AllowGetPIDs();
|
|
|
|
|
Update PolicyBuilder to include wrappers for more syscall families that differ between platforms.
New wrappers:
- `AllowEpollWait` (`epoll_wait`, `epoll_pwait`, `epoll_pwait2`)
- `AllowInotifyInit` (`inotify_init`, `inotify_init1`)
- `AllowSelect` (`select`, `pselect6`)
- `AllowDup` (`dup`, `dup2`, `dup3`)
- `AllowPipe` (`pipe`, `pipe2`)
- `AllowChmod` (`chmod`, `fchmod`, `fchmodat`)
- `AllowChown` (`chown`, `lchown`, `fchown`, `fchownat`)
- `AllowReadlink` (`readlink`, `readlinkat`)
- `AllowLink` (`link`, `linkat`)
- `AllowSymlink` (`symlink`, `symlinkat`)
- `AllowMkdir` (`mkdir`, `mkdirat`)
- `AllowUtime` (`utime`, `utimes`, `futimens`, `utimensat`)
- `AllowAlarm` (`alarm`, `setitimer`)
- `AllowGetPGIDs` (`getpgid`, `getpgrp`)
- `AllowPoll` (`poll`, `ppoll`)
Updated wrappers:
- `AllowOpen` now includes `creat`. `openat` already grants the ability to create files, and is the designated replacement for `creat` on newer platforms.
- `AllowStat` now includes `fstatfs` and `fstatfs64`. The comment already claimed that these syscalls were included; I believe they were omitted by accident.
- `AllowUnlink` now includes `rmdir`. `unlinkat` already grants the ability to remove empty directories, and is the designated replacement for `rmdir` on newer platforms.
PiperOrigin-RevId: 495045432
Change-Id: I41eccb74fda250b27586b6b7fe4c480332e48846
2022-12-14 01:31:29 +08:00
|
|
|
// Appends code to allow getting process groups.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - getpgid
|
|
|
|
// - getpgrp
|
|
|
|
PolicyBuilder& AllowGetPGIDs();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Appends code to allow getting the rlimits.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - getrlimit
|
|
|
|
// - ugetrlimit (on architectures where it exist)
|
|
|
|
PolicyBuilder& AllowGetRlimit();
|
|
|
|
|
|
|
|
// Appends code to allow setting the rlimits.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - setrlimit
|
|
|
|
// - usetrlimit (on architectures where it exist)
|
|
|
|
PolicyBuilder& AllowSetRlimit();
|
|
|
|
|
|
|
|
// Appends code to allow reading random bytes.
|
|
|
|
// Allows these sycalls:
|
|
|
|
// - getrandom (with no flags or GRND_NONBLOCK)
|
2020-04-30 15:08:31 +08:00
|
|
|
//
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& AllowGetRandom();
|
|
|
|
|
2020-04-30 15:08:31 +08:00
|
|
|
// Appends code to allow configuring wipe-on-fork memory
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - madvise (with advice equal to -1 or MADV_WIPEONFORK).
|
|
|
|
PolicyBuilder& AllowWipeOnFork();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Enables syscalls required to use the logging support enabled via
|
|
|
|
// Client::SendLogsToSupervisor()
|
|
|
|
// Allows the following:
|
|
|
|
// - Writes
|
|
|
|
// - kill(0, SIGABRT) (for LOG(FATAL))
|
|
|
|
// - clock_gettime
|
|
|
|
// - gettid
|
|
|
|
// - close
|
|
|
|
PolicyBuilder& AllowLogForwarding();
|
|
|
|
|
Update PolicyBuilder to include wrappers for more syscall families that differ between platforms.
New wrappers:
- `AllowEpollWait` (`epoll_wait`, `epoll_pwait`, `epoll_pwait2`)
- `AllowInotifyInit` (`inotify_init`, `inotify_init1`)
- `AllowSelect` (`select`, `pselect6`)
- `AllowDup` (`dup`, `dup2`, `dup3`)
- `AllowPipe` (`pipe`, `pipe2`)
- `AllowChmod` (`chmod`, `fchmod`, `fchmodat`)
- `AllowChown` (`chown`, `lchown`, `fchown`, `fchownat`)
- `AllowReadlink` (`readlink`, `readlinkat`)
- `AllowLink` (`link`, `linkat`)
- `AllowSymlink` (`symlink`, `symlinkat`)
- `AllowMkdir` (`mkdir`, `mkdirat`)
- `AllowUtime` (`utime`, `utimes`, `futimens`, `utimensat`)
- `AllowAlarm` (`alarm`, `setitimer`)
- `AllowGetPGIDs` (`getpgid`, `getpgrp`)
- `AllowPoll` (`poll`, `ppoll`)
Updated wrappers:
- `AllowOpen` now includes `creat`. `openat` already grants the ability to create files, and is the designated replacement for `creat` on newer platforms.
- `AllowStat` now includes `fstatfs` and `fstatfs64`. The comment already claimed that these syscalls were included; I believe they were omitted by accident.
- `AllowUnlink` now includes `rmdir`. `unlinkat` already grants the ability to remove empty directories, and is the designated replacement for `rmdir` on newer platforms.
PiperOrigin-RevId: 495045432
Change-Id: I41eccb74fda250b27586b6b7fe4c480332e48846
2022-12-14 01:31:29 +08:00
|
|
|
// Appends code to allow deleting files and directories.
|
2021-12-08 22:40:48 +08:00
|
|
|
// Allows these syscalls:
|
Update PolicyBuilder to include wrappers for more syscall families that differ between platforms.
New wrappers:
- `AllowEpollWait` (`epoll_wait`, `epoll_pwait`, `epoll_pwait2`)
- `AllowInotifyInit` (`inotify_init`, `inotify_init1`)
- `AllowSelect` (`select`, `pselect6`)
- `AllowDup` (`dup`, `dup2`, `dup3`)
- `AllowPipe` (`pipe`, `pipe2`)
- `AllowChmod` (`chmod`, `fchmod`, `fchmodat`)
- `AllowChown` (`chown`, `lchown`, `fchown`, `fchownat`)
- `AllowReadlink` (`readlink`, `readlinkat`)
- `AllowLink` (`link`, `linkat`)
- `AllowSymlink` (`symlink`, `symlinkat`)
- `AllowMkdir` (`mkdir`, `mkdirat`)
- `AllowUtime` (`utime`, `utimes`, `futimens`, `utimensat`)
- `AllowAlarm` (`alarm`, `setitimer`)
- `AllowGetPGIDs` (`getpgid`, `getpgrp`)
- `AllowPoll` (`poll`, `ppoll`)
Updated wrappers:
- `AllowOpen` now includes `creat`. `openat` already grants the ability to create files, and is the designated replacement for `creat` on newer platforms.
- `AllowStat` now includes `fstatfs` and `fstatfs64`. The comment already claimed that these syscalls were included; I believe they were omitted by accident.
- `AllowUnlink` now includes `rmdir`. `unlinkat` already grants the ability to remove empty directories, and is the designated replacement for `rmdir` on newer platforms.
PiperOrigin-RevId: 495045432
Change-Id: I41eccb74fda250b27586b6b7fe4c480332e48846
2022-12-14 01:31:29 +08:00
|
|
|
// - rmdir (if available)
|
2021-12-08 22:40:48 +08:00
|
|
|
// - unlink (if available)
|
|
|
|
// - unlinkat
|
|
|
|
PolicyBuilder& AllowUnlink();
|
|
|
|
|
|
|
|
// Appends code to allow renaming files
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - rename (if available)
|
|
|
|
// - renameat
|
|
|
|
// - renameat2
|
|
|
|
PolicyBuilder& AllowRename();
|
|
|
|
|
2023-03-22 22:46:14 +08:00
|
|
|
// Appends code to allow creating event notification file descriptors.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - eventfd (if available)
|
|
|
|
// - eventfd2
|
|
|
|
PolicyBuilder& AllowEventFd();
|
|
|
|
|
Update PolicyBuilder to include wrappers for more syscall families that differ between platforms.
New wrappers:
- `AllowEpollWait` (`epoll_wait`, `epoll_pwait`, `epoll_pwait2`)
- `AllowInotifyInit` (`inotify_init`, `inotify_init1`)
- `AllowSelect` (`select`, `pselect6`)
- `AllowDup` (`dup`, `dup2`, `dup3`)
- `AllowPipe` (`pipe`, `pipe2`)
- `AllowChmod` (`chmod`, `fchmod`, `fchmodat`)
- `AllowChown` (`chown`, `lchown`, `fchown`, `fchownat`)
- `AllowReadlink` (`readlink`, `readlinkat`)
- `AllowLink` (`link`, `linkat`)
- `AllowSymlink` (`symlink`, `symlinkat`)
- `AllowMkdir` (`mkdir`, `mkdirat`)
- `AllowUtime` (`utime`, `utimes`, `futimens`, `utimensat`)
- `AllowAlarm` (`alarm`, `setitimer`)
- `AllowGetPGIDs` (`getpgid`, `getpgrp`)
- `AllowPoll` (`poll`, `ppoll`)
Updated wrappers:
- `AllowOpen` now includes `creat`. `openat` already grants the ability to create files, and is the designated replacement for `creat` on newer platforms.
- `AllowStat` now includes `fstatfs` and `fstatfs64`. The comment already claimed that these syscalls were included; I believe they were omitted by accident.
- `AllowUnlink` now includes `rmdir`. `unlinkat` already grants the ability to remove empty directories, and is the designated replacement for `rmdir` on newer platforms.
PiperOrigin-RevId: 495045432
Change-Id: I41eccb74fda250b27586b6b7fe4c480332e48846
2022-12-14 01:31:29 +08:00
|
|
|
// Appends code to allow polling files.
|
|
|
|
// Allows these syscalls:
|
|
|
|
// - poll (if available)
|
|
|
|
// - ppoll
|
|
|
|
PolicyBuilder& AllowPoll();
|
|
|
|
|
2022-03-23 14:38:29 +08:00
|
|
|
// Appends code to allow setting the name of a thread
|
|
|
|
// Allows the following
|
|
|
|
// - prctl(PR_SET_NAME, ...)
|
|
|
|
PolicyBuilder& AllowPrctlSetName();
|
|
|
|
|
2023-06-17 00:33:25 +08:00
|
|
|
// Appends code to allow setting a name for an anonymous memory region.
|
|
|
|
// Allows the following
|
|
|
|
// - prctl(PR_SET_VMA, PR_SET_VMA_ANON_NAME, ...)
|
|
|
|
PolicyBuilder& AllowPrctlSetVma();
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Enables the syscalls necessary to start a statically linked binary
|
|
|
|
//
|
|
|
|
// NOTE: This will call BlockSyscallWithErrno(__NR_readlink, ENOENT). If you
|
|
|
|
// do not want readlink blocked, put a different call before this call.
|
|
|
|
//
|
|
|
|
// The current list of allowed syscalls are below. However you should *not*
|
|
|
|
// depend on the specifics, as these will change whenever the startup code
|
|
|
|
// changes.
|
|
|
|
//
|
|
|
|
// - uname,
|
|
|
|
// - brk,
|
|
|
|
// - set_tid_address,
|
|
|
|
// - set_robust_list,
|
|
|
|
// - futex(FUTEX_WAIT_BITSET, ...)
|
|
|
|
// - rt_sigaction(0x20, ...)
|
|
|
|
// - rt_sigaction(0x21, ...)
|
|
|
|
// - rt_sigprocmask(SIG_UNBLOCK, ...)
|
|
|
|
// - arch_prctl(ARCH_SET_FS)
|
|
|
|
//
|
|
|
|
// Additionally it will block calls to readlink.
|
|
|
|
PolicyBuilder& AllowStaticStartup();
|
|
|
|
|
|
|
|
// In addition to syscalls allowed by AllowStaticStartup, also allow reading,
|
|
|
|
// seeking, mmapping and closing files. It does not allow opening them, as
|
|
|
|
// the mechanism for doing so depends on whether GetFs-checks are used or not.
|
|
|
|
PolicyBuilder& AllowDynamicStartup();
|
|
|
|
|
|
|
|
// Appends a policy, which will be run on the specified syscall.
|
2022-05-27 21:45:14 +08:00
|
|
|
// This policy must be written without labels. If you need labels, use
|
|
|
|
// the overloaded function passing a BpfFunc object instead of the
|
|
|
|
// sock_filter.
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& AddPolicyOnSyscall(uint32_t num,
|
|
|
|
absl::Span<const sock_filter> policy);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Appends a policy, which will be run on the specified syscall.
|
|
|
|
// This policy may use labels.
|
|
|
|
// Example of how to use it:
|
|
|
|
// builder.AddPolicyOnSyscall(
|
|
|
|
// __NR_socket, [](bpf_labels& labels) -> std::vector<sock_filter> {
|
|
|
|
// return {
|
|
|
|
// ARG(0), // domain is first argument of socket
|
|
|
|
// JEQ(AF_UNIX, JUMP(&labels, af_unix)),
|
|
|
|
// JEQ(AF_NETLINK, JUMP(&labels, af_netlink)),
|
|
|
|
// KILL,
|
|
|
|
//
|
|
|
|
// LABEL(&labels, af_unix),
|
|
|
|
// ARG(1),
|
|
|
|
// JEQ(SOCK_STREAM | SOCK_NONBLOCK, ALLOW),
|
|
|
|
// KILL,
|
|
|
|
//
|
|
|
|
// LABEL(&labels, af_netlink),
|
|
|
|
// ARG(2),
|
|
|
|
// JEQ(NETLINK_ROUTE, ALLOW),
|
|
|
|
// };
|
|
|
|
// });
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& AddPolicyOnSyscall(uint32_t num, BpfFunc f);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Appends a policy, which will be run on the specified syscalls.
|
|
|
|
// This policy must be written without labels.
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& AddPolicyOnSyscalls(absl::Span<const uint32_t> nums,
|
|
|
|
absl::Span<const sock_filter> policy);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Appends a policy, which will be run on the specified syscalls.
|
|
|
|
// This policy may use labels.
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& AddPolicyOnSyscalls(absl::Span<const uint32_t> nums,
|
|
|
|
BpfFunc f);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-11-06 17:29:52 +08:00
|
|
|
// Equivalent to AddPolicyOnSyscalls(mmap_syscalls, policy), where
|
|
|
|
// mmap_syscalls is a subset of {__NR_mmap, __NR_mmap2}, which exists on the
|
|
|
|
// target architecture.
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& AddPolicyOnMmap(absl::Span<const sock_filter> policy);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-05-27 21:45:14 +08:00
|
|
|
// Equivalent to AddPolicyOnSyscalls(mmap_syscalls, f), where mmap_syscalls
|
|
|
|
// is a subset of {__NR_mmap, __NR_mmap2}, which exists on the target
|
2020-11-06 17:29:52 +08:00
|
|
|
// architecture.
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& AddPolicyOnMmap(BpfFunc f);
|
|
|
|
|
2022-05-27 21:45:14 +08:00
|
|
|
// Builds the policy returning a unique_ptr to it. This should only be
|
|
|
|
// called once.
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<std::unique_ptr<Policy>> TryBuild();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2022-05-27 21:45:14 +08:00
|
|
|
// Builds the policy returning a unique_ptr to it. This should only be
|
|
|
|
// called once. This function will abort if an error happened in any of the
|
|
|
|
// PolicyBuilder methods.
|
2020-04-02 22:42:17 +08:00
|
|
|
std::unique_ptr<Policy> BuildOrDie() { return TryBuild().value(); }
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Adds a bind-mount for a file from outside the namespace to inside. This
|
|
|
|
// will also create parent directories inside the namespace if needed.
|
|
|
|
//
|
|
|
|
// Calling these function will enable use of namespaces.
|
|
|
|
PolicyBuilder& AddFile(absl::string_view path, bool is_ro = true);
|
|
|
|
PolicyBuilder& AddFileAt(absl::string_view outside, absl::string_view inside,
|
|
|
|
bool is_ro = true);
|
|
|
|
|
|
|
|
// Best-effort function that adds the libraries and linker required by a
|
|
|
|
// binary.
|
|
|
|
//
|
|
|
|
// This does not add the binary itself, only the libraries it depends on.
|
|
|
|
//
|
|
|
|
// This function should work correctly for most binaries, but you might need
|
|
|
|
// to tweak it in some cases.
|
|
|
|
//
|
|
|
|
// This function is safe even for untrusted/potentially malicious binaries.
|
|
|
|
// It adds libraries only from standard library dirs and ld_library_path.
|
|
|
|
//
|
|
|
|
// run `ldd` yourself and use AddFile or AddDirectory.
|
|
|
|
PolicyBuilder& AddLibrariesForBinary(absl::string_view path,
|
|
|
|
absl::string_view ld_library_path = {});
|
|
|
|
|
2022-05-27 21:45:14 +08:00
|
|
|
// Similar to AddLibrariesForBinary, but binary is specified with an open
|
|
|
|
// fd.
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& AddLibrariesForBinary(int fd,
|
|
|
|
absl::string_view ld_library_path = {});
|
|
|
|
|
|
|
|
// Adds a bind-mount for a directory from outside the namespace to
|
|
|
|
// inside. This will also create parent directories inside the namespace if
|
|
|
|
// needed.
|
|
|
|
//
|
|
|
|
// Calling these function will enable use of namespaces.
|
|
|
|
PolicyBuilder& AddDirectory(absl::string_view path, bool is_ro = true);
|
|
|
|
PolicyBuilder& AddDirectoryAt(absl::string_view outside,
|
|
|
|
absl::string_view inside, bool is_ro = true);
|
|
|
|
|
|
|
|
// Adds a tmpfs inside the namespace. This will also create parent
|
|
|
|
// directories inside the namespace if needed.
|
|
|
|
//
|
|
|
|
// Calling this function will enable use of namespaces.
|
2021-07-29 20:51:48 +08:00
|
|
|
PolicyBuilder& AddTmpfs(absl::string_view inside, size_t size);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Allows unrestricted access to the network by *not* creating a network
|
2022-05-27 21:45:14 +08:00
|
|
|
// namespace. Note that this only disables the network namespace. To
|
|
|
|
// actually allow networking, you would also need to allow networking
|
|
|
|
// syscalls. Calling this function will enable use of namespaces.
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& AllowUnrestrictedNetworking();
|
|
|
|
|
|
|
|
// Enables the use of namespaces.
|
|
|
|
//
|
2019-09-11 17:39:07 +08:00
|
|
|
// Namespaces are enabled by default.
|
|
|
|
// This is a no-op.
|
|
|
|
ABSL_DEPRECATED("Namespaces are enabled by default; no need to call this")
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& EnableNamespaces() {
|
2019-09-11 17:39:07 +08:00
|
|
|
CHECK(use_namespaces_) << "Namespaces cannot be both disabled and enabled";
|
|
|
|
requires_namespaces_ = true;
|
2019-03-19 00:21:48 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-05-22 21:53:49 +08:00
|
|
|
// Disables the use of namespaces.
|
|
|
|
//
|
2019-09-11 17:39:07 +08:00
|
|
|
// Call in order to use Sandbox2 without namespaces.
|
|
|
|
// This is not recommended.
|
2019-05-22 21:53:49 +08:00
|
|
|
PolicyBuilder& DisableNamespaces() {
|
2019-09-11 17:39:07 +08:00
|
|
|
CHECK(!requires_namespaces_)
|
2019-05-22 21:53:49 +08:00
|
|
|
<< "Namespaces cannot be both disabled and enabled. You're probably "
|
|
|
|
"using features that implicitly enable namespaces (SetHostname, "
|
2022-05-27 21:45:14 +08:00
|
|
|
"AddFile, AddDirectory, AddDataDependency, AddLibrariesForBinary "
|
|
|
|
"or similar)";
|
2019-09-11 17:39:07 +08:00
|
|
|
use_namespaces_ = false;
|
2019-05-22 21:53:49 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Set hostname in the network namespace instead of default "sandbox2".
|
|
|
|
//
|
|
|
|
// Calling this function will enable use of namespaces.
|
|
|
|
// It is an error to also call AllowUnrestrictedNetworking.
|
|
|
|
PolicyBuilder& SetHostname(absl::string_view hostname);
|
|
|
|
|
|
|
|
// Enables/disables stack trace collection on violations.
|
|
|
|
PolicyBuilder& CollectStacktracesOnViolation(bool enable);
|
|
|
|
|
|
|
|
// Enables/disables stack trace collection on signals (e.g. crashes / killed
|
|
|
|
// from a signal).
|
|
|
|
PolicyBuilder& CollectStacktracesOnSignal(bool enable);
|
|
|
|
|
|
|
|
// Enables/disables stack trace collection on hitting a timeout.
|
|
|
|
PolicyBuilder& CollectStacktracesOnTimeout(bool enable);
|
|
|
|
|
|
|
|
// Enables/disables stack trace collection on getting killed by the sandbox
|
|
|
|
// monitor / the user.
|
|
|
|
PolicyBuilder& CollectStacktracesOnKill(bool enable);
|
|
|
|
|
2021-08-16 18:12:39 +08:00
|
|
|
// Enables/disables stack trace collection on normal process exit.
|
|
|
|
PolicyBuilder& CollectStacktracesOnExit(bool enable);
|
|
|
|
|
2023-03-01 21:35:51 +08:00
|
|
|
// Changes the default action to ALLOW.
|
|
|
|
// All syscalls not handled explicitly by the policy will thus be allowed.
|
2019-03-19 00:21:48 +08:00
|
|
|
// Do not use in environment with untrusted code and/or data, ask
|
|
|
|
// sandbox-team@ first if unsure.
|
2023-03-04 02:25:39 +08:00
|
|
|
PolicyBuilder& DefaultAction(AllowAllSyscalls);
|
|
|
|
|
2023-03-01 21:35:51 +08:00
|
|
|
ABSL_DEPRECATED("Use DefaultAction(sandbox2::AllowAllSyscalls()) instead")
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& DangerDefaultAllowAll();
|
|
|
|
|
2019-07-23 19:40:47 +08:00
|
|
|
// Allows syscalls that are necessary for the NetworkProxyClient
|
|
|
|
PolicyBuilder& AddNetworkProxyPolicy();
|
|
|
|
|
|
|
|
// Allows syscalls that are necessary for the NetworkProxyClient and
|
|
|
|
// the NetworkProxyHandler
|
|
|
|
PolicyBuilder& AddNetworkProxyHandlerPolicy();
|
|
|
|
|
2019-11-14 19:51:01 +08:00
|
|
|
// Makes root of the filesystem writeable
|
|
|
|
// Not recommended
|
|
|
|
PolicyBuilder& SetRootWritable();
|
|
|
|
|
2022-03-09 00:00:46 +08:00
|
|
|
// Changes mounts propagation from MS_PRIVATE to MS_SLAVE.
|
|
|
|
PolicyBuilder& DangerAllowMountPropagation() {
|
|
|
|
allow_mount_propagation_ = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-02-20 23:45:22 +08:00
|
|
|
// Allows connections to this IP.
|
|
|
|
PolicyBuilder& AllowIPv4(const std::string& ip_and_mask, uint32_t port = 0);
|
|
|
|
PolicyBuilder& AllowIPv6(const std::string& ip_and_mask, uint32_t port = 0);
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
private:
|
|
|
|
friend class PolicyBuilderPeer; // For testing
|
|
|
|
friend class StackTracePeer;
|
|
|
|
|
2021-09-01 16:27:56 +08:00
|
|
|
static absl::StatusOr<std::string> ValidateAbsolutePath(
|
|
|
|
absl::string_view path);
|
|
|
|
static absl::StatusOr<std::string> ValidatePath(absl::string_view path);
|
|
|
|
|
2023-07-17 16:58:05 +08:00
|
|
|
// Similar to AddFile(At)/AddDirectory(At) but it won't force use of
|
|
|
|
// namespaces - files will only be added to the namespace if it is not
|
|
|
|
// disabled by the time of TryBuild().
|
|
|
|
PolicyBuilder& AddFileIfNamespaced(absl::string_view path, bool is_ro = true);
|
|
|
|
PolicyBuilder& AddFileAtIfNamespaced(absl::string_view outside,
|
|
|
|
absl::string_view inside,
|
|
|
|
bool is_ro = true);
|
|
|
|
PolicyBuilder& AddDirectoryIfNamespaced(absl::string_view path,
|
|
|
|
bool is_ro = true);
|
|
|
|
PolicyBuilder& AddDirectoryAtIfNamespaced(absl::string_view outside,
|
|
|
|
absl::string_view inside,
|
|
|
|
bool is_ro = true);
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// Allows a limited version of madvise
|
|
|
|
PolicyBuilder& AllowLimitedMadvise();
|
|
|
|
|
2022-11-30 21:59:36 +08:00
|
|
|
// Traps instead of denying ptrace.
|
|
|
|
PolicyBuilder& TrapPtrace();
|
|
|
|
|
2022-03-04 00:36:36 +08:00
|
|
|
// Appends code to block a specific syscall and setting errno at the end of
|
|
|
|
// the policy - decision taken by user policy take precedence.
|
|
|
|
PolicyBuilder& OverridableBlockSyscallWithErrno(uint32_t num, int error);
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& SetMounts(Mounts mounts) {
|
|
|
|
mounts_ = std::move(mounts);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<sock_filter> ResolveBpfFunc(BpfFunc f);
|
|
|
|
|
|
|
|
void StoreDescription(PolicyBuilderDescription* pb_description);
|
|
|
|
|
2021-09-01 16:27:56 +08:00
|
|
|
// This function returns a PolicyBuilder so that we can use it in the status
|
|
|
|
// macros
|
|
|
|
PolicyBuilder& SetError(const absl::Status& status);
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
Mounts mounts_;
|
2019-09-11 17:39:07 +08:00
|
|
|
bool use_namespaces_ = true;
|
|
|
|
bool requires_namespaces_ = false;
|
2019-03-19 00:21:48 +08:00
|
|
|
bool allow_unrestricted_networking_ = false;
|
2022-03-09 00:00:46 +08:00
|
|
|
bool allow_mount_propagation_ = false;
|
2020-09-10 20:47:32 +08:00
|
|
|
std::string hostname_ = std::string(kDefaultHostname);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
bool collect_stacktrace_on_violation_ = true;
|
|
|
|
bool collect_stacktrace_on_signal_ = true;
|
|
|
|
bool collect_stacktrace_on_timeout_ = true;
|
|
|
|
bool collect_stacktrace_on_kill_ = false;
|
2021-08-16 18:12:39 +08:00
|
|
|
bool collect_stacktrace_on_exit_ = false;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Seccomp fields
|
2019-10-09 01:45:07 +08:00
|
|
|
std::vector<sock_filter> user_policy_;
|
2022-03-04 00:36:36 +08:00
|
|
|
std::vector<sock_filter> overridable_policy_;
|
2023-03-01 21:35:51 +08:00
|
|
|
std::optional<sock_filter> default_action_;
|
2020-12-03 00:37:55 +08:00
|
|
|
bool user_policy_handles_bpf_ = false;
|
2022-05-27 17:57:03 +08:00
|
|
|
bool user_policy_handles_ptrace_ = false;
|
2021-12-13 17:31:02 +08:00
|
|
|
absl::flat_hash_set<uint32_t> handled_syscalls_;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
// Error handling
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status last_status_;
|
2019-10-09 01:45:07 +08:00
|
|
|
bool already_built_ = false;
|
2020-02-20 23:45:22 +08:00
|
|
|
|
|
|
|
// Contains list of allowed hosts.
|
|
|
|
absl::optional<AllowedHosts> allowed_hosts_;
|
2019-03-19 00:21:48 +08:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace sandbox2
|
|
|
|
|
|
|
|
#endif // SANDBOXED_API_SANDBOX2_POLICYBUILDER_H_
|