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.
|
|
|
|
|
|
|
|
#include "sandboxed_api/sandbox2/policybuilder.h"
|
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <fcntl.h> // For the fcntl flags
|
|
|
|
#include <linux/bpf_common.h>
|
2021-12-13 17:31:02 +08:00
|
|
|
#include <linux/filter.h>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <linux/futex.h>
|
|
|
|
#include <linux/random.h> // For GRND_NONBLOCK
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <linux/seccomp.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <sys/ioctl.h>
|
|
|
|
#include <sys/mman.h> // For mmap arguments
|
2022-03-23 14:38:29 +08:00
|
|
|
#include <sys/prctl.h>
|
2019-07-23 19:40:47 +08:00
|
|
|
#include <sys/socket.h>
|
2022-09-16 15:36:25 +08:00
|
|
|
#include <sys/stat.h>
|
2022-03-08 15:57:28 +08:00
|
|
|
#include <sys/statvfs.h>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <syscall.h>
|
2022-09-16 15:36:25 +08:00
|
|
|
#include <unistd.h>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-12-13 17:31:02 +08:00
|
|
|
#include <array>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <cerrno>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <csignal>
|
|
|
|
#include <cstdint>
|
2023-08-31 15:43:30 +08:00
|
|
|
#include <cstdlib>
|
2021-11-12 18:44:07 +08:00
|
|
|
#include <deque>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <functional>
|
|
|
|
#include <iterator>
|
|
|
|
#include <limits>
|
2022-10-12 20:22:51 +08:00
|
|
|
#include <memory>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <optional>
|
|
|
|
#include <string>
|
2019-03-19 00:21:48 +08:00
|
|
|
#include <utility>
|
2023-08-24 21:23:03 +08:00
|
|
|
#include <vector>
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/container/flat_hash_set.h"
|
2022-10-20 21:48:06 +08:00
|
|
|
#include "absl/log/log.h"
|
2019-07-23 19:40:47 +08:00
|
|
|
#include "absl/memory/memory.h"
|
2021-09-01 16:27:56 +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/match.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/strings/str_cat.h"
|
2021-09-01 16:27:56 +08:00
|
|
|
#include "absl/strings/string_view.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "absl/types/span.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/config.h"
|
2023-03-01 21:35:51 +08:00
|
|
|
#include "sandboxed_api/sandbox2/allow_all_syscalls.h"
|
2023-05-04 14:29:00 +08:00
|
|
|
#include "sandboxed_api/sandbox2/allow_unrestricted_networking.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/namespace.h"
|
2022-10-12 20:22:51 +08:00
|
|
|
#include "sandboxed_api/sandbox2/policy.h"
|
2023-08-24 21:23:03 +08:00
|
|
|
#include "sandboxed_api/sandbox2/syscall.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
#include "sandboxed_api/sandbox2/util/bpf_helper.h"
|
2023-08-09 21:43:50 +08:00
|
|
|
#include "sandboxed_api/sandbox2/violation.pb.h"
|
2021-01-14 01:25:25 +08:00
|
|
|
#include "sandboxed_api/util/path.h"
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-09-10 20:47:32 +08:00
|
|
|
#if defined(SAPI_X86_64)
|
|
|
|
#include <asm/prctl.h>
|
|
|
|
#elif defined(SAPI_PPC64_LE)
|
|
|
|
#include <asm/termbits.h> // On PPC, TCGETS macro needs termios
|
|
|
|
#endif
|
|
|
|
|
2022-12-22 23:38:54 +08:00
|
|
|
#ifndef PR_SET_VMA
|
|
|
|
#define PR_SET_VMA 0x53564d41
|
|
|
|
#endif
|
|
|
|
#ifndef PR_SET_VMA_ANON_NAME
|
|
|
|
#define PR_SET_VMA_ANON_NAME 0
|
|
|
|
#endif
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
namespace sandbox2 {
|
|
|
|
namespace {
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
namespace file = ::sapi::file;
|
|
|
|
|
2021-12-13 17:31:02 +08:00
|
|
|
constexpr std::array<uint32_t, 2> kMmapSyscalls = {
|
2020-11-06 17:29:52 +08:00
|
|
|
#ifdef __NR_mmap2
|
|
|
|
__NR_mmap2,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_mmap
|
|
|
|
__NR_mmap,
|
|
|
|
#endif
|
|
|
|
};
|
|
|
|
|
2021-11-12 18:44:07 +08:00
|
|
|
bool CheckBpfBounds(const sock_filter& filter, size_t max_jmp) {
|
|
|
|
if (BPF_CLASS(filter.code) == BPF_JMP) {
|
|
|
|
if (BPF_OP(filter.code) == BPF_JA) {
|
|
|
|
return filter.k <= max_jmp;
|
|
|
|
}
|
|
|
|
return filter.jt <= max_jmp && filter.jf <= max_jmp;
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2022-03-08 15:57:28 +08:00
|
|
|
bool IsOnReadOnlyDev(const std::string& path) {
|
|
|
|
struct statvfs vfs;
|
|
|
|
if (TEMP_FAILURE_RETRY(statvfs(path.c_str(), &vfs)) == -1) {
|
|
|
|
PLOG(ERROR) << "Could not statvfs: " << path.c_str();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return vfs.f_flag & ST_RDONLY;
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace
|
|
|
|
|
2023-05-04 14:29:00 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::Allow(UnrestrictedNetworking tag) {
|
|
|
|
EnableNamespaces(); // NOLINT(clang-diagnostic-deprecated-declarations)
|
|
|
|
allow_unrestricted_networking_ = true;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowSyscall(uint32_t num) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (handled_syscalls_.insert(num).second) {
|
2019-10-09 01:45:07 +08:00
|
|
|
user_policy_.insert(user_policy_.end(), {SYSCALL(num, ALLOW)});
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowSyscalls(absl::Span<const uint32_t> nums) {
|
2019-03-19 00:21:48 +08:00
|
|
|
for (auto num : nums) {
|
|
|
|
AllowSyscall(num);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-12-08 22:40:48 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::BlockSyscallsWithErrno(
|
2021-12-13 17:31:02 +08:00
|
|
|
absl::Span<const uint32_t> nums, int error) {
|
2021-12-08 22:40:48 +08:00
|
|
|
for (auto num : nums) {
|
2022-02-21 16:46:16 +08:00
|
|
|
BlockSyscallWithErrno(num, error);
|
2021-12-08 22:40:48 +08:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::BlockSyscallWithErrno(uint32_t num, int error) {
|
2019-03-19 00:21:48 +08:00
|
|
|
if (handled_syscalls_.insert(num).second) {
|
2019-10-09 01:45:07 +08:00
|
|
|
user_policy_.insert(user_policy_.end(), {SYSCALL(num, ERRNO(error))});
|
2020-12-03 00:37:55 +08:00
|
|
|
if (num == __NR_bpf) {
|
|
|
|
user_policy_handles_bpf_ = true;
|
|
|
|
}
|
2022-05-27 17:57:03 +08:00
|
|
|
if (num == __NR_ptrace) {
|
|
|
|
user_policy_handles_ptrace_ = true;
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-03-04 00:36:36 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::OverridableBlockSyscallWithErrno(uint32_t num,
|
|
|
|
int error) {
|
|
|
|
overridable_policy_.insert(overridable_policy_.end(),
|
|
|
|
{SYSCALL(num, ERRNO(error))});
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
PolicyBuilder& PolicyBuilder::AllowEpollWait() {
|
2022-03-07 16:53:48 +08:00
|
|
|
return AllowSyscalls({
|
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
|
|
|
#ifdef __NR_epoll_wait
|
|
|
|
__NR_epoll_wait,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_epoll_pwait
|
|
|
|
__NR_epoll_pwait,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_epoll_pwait2
|
|
|
|
__NR_epoll_pwait2,
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowEpoll() {
|
|
|
|
AllowSyscalls({
|
2022-03-07 16:53:48 +08:00
|
|
|
#ifdef __NR_epoll_create
|
|
|
|
__NR_epoll_create,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_epoll_create1
|
|
|
|
__NR_epoll_create1,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_epoll_ctl
|
|
|
|
__NR_epoll_ctl,
|
|
|
|
#endif
|
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
|
|
|
});
|
|
|
|
|
|
|
|
return AllowEpollWait();
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowInotifyInit() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
#ifdef __NR_inotify_init
|
|
|
|
__NR_inotify_init,
|
2022-03-07 16:53:48 +08:00
|
|
|
#endif
|
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
|
|
|
#ifdef __NR_inotify_init1
|
|
|
|
__NR_inotify_init1,
|
2022-03-07 16:53:48 +08:00
|
|
|
#endif
|
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
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowSelect() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
#ifdef __NR_select
|
|
|
|
__NR_select,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_pselect6
|
|
|
|
__NR_pselect6,
|
2022-03-07 16:53:48 +08:00
|
|
|
#endif
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowExit() {
|
|
|
|
return AllowSyscalls({__NR_exit, __NR_exit_group});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowScudoMalloc() {
|
|
|
|
AllowTime();
|
|
|
|
AllowSyscalls({__NR_munmap, __NR_nanosleep});
|
|
|
|
AllowFutexOp(FUTEX_WAKE);
|
|
|
|
AllowLimitedMadvise();
|
|
|
|
AllowGetRandom();
|
2023-08-29 23:11:31 +08:00
|
|
|
AllowGetPIDs();
|
2020-04-30 15:08:31 +08:00
|
|
|
AllowWipeOnFork();
|
2023-08-29 23:11:31 +08:00
|
|
|
#ifdef __NR_open
|
|
|
|
OverridableBlockSyscallWithErrno(__NR_open, ENOENT);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_openat
|
|
|
|
OverridableBlockSyscallWithErrno(__NR_openat, ENOENT);
|
|
|
|
#endif
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
return AddPolicyOnMmap([](bpf_labels& labels) -> std::vector<sock_filter> {
|
|
|
|
return {
|
|
|
|
ARG_32(2), // prot
|
|
|
|
JEQ32(PROT_NONE, JUMP(&labels, prot_none)),
|
|
|
|
JNE32(PROT_READ | PROT_WRITE, JUMP(&labels, mmap_end)),
|
|
|
|
|
|
|
|
// PROT_READ | PROT_WRITE
|
|
|
|
ARG_32(3), // flags
|
2021-10-11 16:21:26 +08:00
|
|
|
BPF_STMT(BPF_ALU | BPF_AND | BPF_K,
|
|
|
|
~uint32_t{MAP_FIXED | MAP_NORESERVE}),
|
2019-03-19 00:21:48 +08:00
|
|
|
JEQ32(MAP_PRIVATE | MAP_ANONYMOUS, ALLOW),
|
|
|
|
JUMP(&labels, mmap_end),
|
|
|
|
|
|
|
|
// PROT_NONE
|
|
|
|
LABEL(&labels, prot_none),
|
|
|
|
ARG_32(3), // flags
|
|
|
|
JEQ32(MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, ALLOW),
|
|
|
|
|
|
|
|
LABEL(&labels, mmap_end),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowTcMalloc() {
|
|
|
|
AllowTime();
|
2021-04-13 22:14:17 +08:00
|
|
|
AllowRestartableSequences(kRequireFastFences);
|
2020-12-22 18:49:45 +08:00
|
|
|
AllowSyscalls(
|
|
|
|
{__NR_munmap, __NR_nanosleep, __NR_brk, __NR_mincore, __NR_membarrier});
|
2019-03-19 00:21:48 +08:00
|
|
|
AllowLimitedMadvise();
|
2023-06-17 00:33:25 +08:00
|
|
|
AllowPrctlSetVma();
|
2023-07-18 00:30:34 +08:00
|
|
|
AllowPoll();
|
2023-08-29 23:11:31 +08:00
|
|
|
AllowGetPIDs();
|
2019-03-19 00:21:48 +08:00
|
|
|
|
|
|
|
AddPolicyOnSyscall(__NR_mprotect, {
|
|
|
|
ARG_32(2),
|
|
|
|
JEQ32(PROT_READ | PROT_WRITE, ALLOW),
|
|
|
|
JEQ32(PROT_NONE, ALLOW),
|
|
|
|
});
|
|
|
|
|
|
|
|
return AddPolicyOnMmap([](bpf_labels& labels) -> std::vector<sock_filter> {
|
|
|
|
return {
|
|
|
|
ARG_32(2), // prot
|
|
|
|
JEQ32(PROT_NONE, JUMP(&labels, prot_none)),
|
|
|
|
JNE32(PROT_READ | PROT_WRITE, JUMP(&labels, mmap_end)),
|
|
|
|
|
|
|
|
// PROT_READ | PROT_WRITE
|
|
|
|
ARG_32(3), // flags
|
|
|
|
JNE32(MAP_ANONYMOUS | MAP_PRIVATE, JUMP(&labels, mmap_end)),
|
|
|
|
ALLOW,
|
|
|
|
|
|
|
|
// PROT_NONE
|
|
|
|
LABEL(&labels, prot_none),
|
|
|
|
ARG_32(3), // flags
|
|
|
|
JEQ32(MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, ALLOW),
|
|
|
|
JEQ32(MAP_ANONYMOUS | MAP_PRIVATE, ALLOW),
|
|
|
|
|
|
|
|
LABEL(&labels, mmap_end),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowSystemMalloc() {
|
|
|
|
AllowSyscalls({__NR_munmap, __NR_brk});
|
2021-01-26 18:31:57 +08:00
|
|
|
AllowFutexOp(FUTEX_WAKE);
|
2019-03-19 00:21:48 +08:00
|
|
|
AddPolicyOnSyscall(__NR_mremap, {
|
|
|
|
ARG_32(3),
|
|
|
|
JEQ32(MREMAP_MAYMOVE, ALLOW),
|
|
|
|
});
|
|
|
|
return AddPolicyOnMmap([](bpf_labels& labels) -> std::vector<sock_filter> {
|
|
|
|
return {
|
|
|
|
ARG_32(2), // prot
|
|
|
|
JEQ32(PROT_NONE, JUMP(&labels, prot_none)),
|
|
|
|
JNE32(PROT_READ | PROT_WRITE, JUMP(&labels, mmap_end)),
|
|
|
|
|
|
|
|
// PROT_READ | PROT_WRITE
|
|
|
|
ARG_32(3), // flags
|
|
|
|
JEQ32(MAP_ANONYMOUS | MAP_PRIVATE, ALLOW),
|
|
|
|
|
|
|
|
// PROT_NONE
|
|
|
|
LABEL(&labels, prot_none),
|
|
|
|
ARG_32(3), // flags
|
|
|
|
JEQ32(MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE, ALLOW),
|
|
|
|
|
|
|
|
LABEL(&labels, mmap_end),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-06-07 22:52:51 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowLlvmSanitizers() {
|
2023-08-31 15:43:30 +08:00
|
|
|
if constexpr (!sapi::sanitizers::IsAny()) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
// *san use a custom allocator that runs mmap/unmap under the hood. For
|
|
|
|
// example:
|
|
|
|
// https://github.com/llvm/llvm-project/blob/596d534ac3524052df210be8d3c01a33b2260a42/compiler-rt/lib/asan/asan_allocator.cpp#L980
|
|
|
|
// https://github.com/llvm/llvm-project/blob/62ec4ac90738a5f2d209ed28c822223e58aaaeb7/compiler-rt/lib/sanitizer_common/sanitizer_allocator_secondary.h#L98
|
|
|
|
AllowMmap();
|
|
|
|
AllowSyscall(__NR_munmap);
|
|
|
|
AllowSyscall(__NR_sched_yield);
|
|
|
|
|
|
|
|
// https://github.com/llvm/llvm-project/blob/4bbc3290a25c0dc26007912a96e0f77b2092ee56/compiler-rt/lib/sanitizer_common/sanitizer_stack_store.cpp#L293
|
|
|
|
AddPolicyOnSyscall(__NR_mprotect,
|
|
|
|
{
|
|
|
|
ARG_32(2),
|
|
|
|
BPF_STMT(BPF_AND | BPF_ALU | BPF_K,
|
|
|
|
~uint32_t{PROT_READ | PROT_WRITE}),
|
|
|
|
JEQ32(PROT_NONE, ALLOW),
|
|
|
|
});
|
|
|
|
|
|
|
|
AddPolicyOnSyscall(__NR_madvise, {
|
|
|
|
ARG_32(2),
|
|
|
|
JEQ32(MADV_DONTDUMP, ALLOW),
|
|
|
|
JEQ32(MADV_NOHUGEPAGE, ALLOW),
|
|
|
|
});
|
|
|
|
// Sanitizers read from /proc. For example:
|
|
|
|
// https://github.com/llvm/llvm-project/blob/634da7a1c61ee8c173e90a841eb1f4ea03caa20b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp#L1155
|
|
|
|
AddDirectoryIfNamespaced("/proc");
|
|
|
|
AllowOpen();
|
|
|
|
// Sanitizers need pid for reports. For example:
|
|
|
|
// https://github.com/llvm/llvm-project/blob/634da7a1c61ee8c173e90a841eb1f4ea03caa20b/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp#L740
|
|
|
|
AllowGetPIDs();
|
|
|
|
// Sanitizers may try color output. For example:
|
|
|
|
// https://github.com/llvm/llvm-project/blob/87dd3d350c4ce0115b2cdf91d85ddd05ae2661aa/compiler-rt/lib/sanitizer_common/sanitizer_posix_libcdep.cpp#L157
|
|
|
|
OverridableBlockSyscallWithErrno(__NR_ioctl, EPERM);
|
|
|
|
// https://github.com/llvm/llvm-project/blob/9aa39481d9eb718e872993791547053a3c1f16d5/compiler-rt/lib/sanitizer_common/sanitizer_linux_libcdep.cpp#L150
|
|
|
|
// https://sourceware.org/git/?p=glibc.git;a=blob;f=nptl/pthread_getattr_np.c;h=de7edfa0928224eb8375e2fe894d6677570fbb3b;hb=HEAD#l188
|
|
|
|
OverridableBlockSyscallWithErrno(__NR_sched_getaffinity, EPERM);
|
|
|
|
// https://github.com/llvm/llvm-project/blob/02c2b472b510ff55679844c087b66e7837e13dc2/compiler-rt/lib/sanitizer_common/sanitizer_linux.cpp#L434
|
2022-04-30 09:23:22 +08:00
|
|
|
#ifdef __NR_readlink
|
2023-08-31 15:43:30 +08:00
|
|
|
OverridableBlockSyscallWithErrno(__NR_readlink, ENOENT);
|
2022-04-30 09:23:22 +08:00
|
|
|
#endif
|
2023-08-31 15:43:30 +08:00
|
|
|
OverridableBlockSyscallWithErrno(__NR_readlinkat, ENOENT);
|
2021-02-01 23:10:43 +08:00
|
|
|
if constexpr (sapi::sanitizers::IsASan()) {
|
|
|
|
AllowSyscall(__NR_sigaltstack);
|
|
|
|
}
|
2023-08-17 17:52:04 +08:00
|
|
|
if constexpr (sapi::sanitizers::IsTSan()) {
|
|
|
|
AllowSyscall(__NR_set_robust_list);
|
|
|
|
}
|
2019-06-07 22:52:51 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-08-31 15:43:30 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowLlvmCoverage() {
|
|
|
|
if (!sapi::IsCoverageRun()) {
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
AllowStat();
|
|
|
|
AllowGetPIDs();
|
|
|
|
AllowOpen();
|
|
|
|
AllowRead();
|
|
|
|
AllowWrite();
|
|
|
|
AllowMkdir();
|
|
|
|
AllowSafeFcntl();
|
|
|
|
AllowSyscalls({
|
|
|
|
__NR_munmap, __NR_close, __NR_lseek,
|
|
|
|
#ifdef __NR__llseek
|
|
|
|
__NR__llseek, // Newer glibc on PPC
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
AllowTcMalloc();
|
|
|
|
AddPolicyOnMmap([](bpf_labels& labels) -> std::vector<sock_filter> {
|
|
|
|
return {
|
|
|
|
ARG_32(2), // prot
|
|
|
|
JNE32(PROT_READ | PROT_WRITE, JUMP(&labels, mmap_end)),
|
|
|
|
ARG_32(3), // flags
|
|
|
|
JEQ32(MAP_SHARED, ALLOW),
|
|
|
|
LABEL(&labels, mmap_end),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
AddDirectoryIfNamespaced(getenv("COVERAGE_DIR"), /*is_ro=*/false);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowLimitedMadvise() {
|
|
|
|
return AddPolicyOnSyscall(__NR_madvise, {
|
|
|
|
ARG_32(2),
|
|
|
|
JEQ32(MADV_DONTNEED, ALLOW),
|
|
|
|
JEQ32(MADV_REMOVE, ALLOW),
|
2023-01-13 20:40:47 +08:00
|
|
|
JEQ32(MADV_HUGEPAGE, ALLOW),
|
2019-03-27 22:40:27 +08:00
|
|
|
JEQ32(MADV_NOHUGEPAGE, ALLOW),
|
2019-03-19 00:21:48 +08:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowMmap() {
|
2020-11-06 17:29:52 +08:00
|
|
|
return AllowSyscalls(kMmapSyscalls);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowOpen() {
|
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
|
|
|
#ifdef __NR_creat
|
|
|
|
AllowSyscall(__NR_creat);
|
|
|
|
#endif
|
2019-03-19 00:21:48 +08:00
|
|
|
#ifdef __NR_open
|
|
|
|
AllowSyscall(__NR_open);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_openat
|
|
|
|
AllowSyscall(__NR_openat);
|
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowStat() {
|
|
|
|
#ifdef __NR_fstat
|
|
|
|
AllowSyscall(__NR_fstat);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_fstat64
|
|
|
|
AllowSyscall(__NR_fstat64);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_fstatat
|
|
|
|
AllowSyscall(__NR_fstatat);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_fstatat64
|
|
|
|
AllowSyscall(__NR_fstatat64);
|
|
|
|
#endif
|
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
|
|
|
#ifdef __NR_fstatfs
|
|
|
|
AllowSyscall(__NR_fstatfs);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_fstatfs64
|
|
|
|
AllowSyscall(__NR_fstatfs64);
|
|
|
|
#endif
|
2019-03-19 00:21:48 +08:00
|
|
|
#ifdef __NR_lstat
|
|
|
|
AllowSyscall(__NR_lstat);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_lstat64
|
|
|
|
AllowSyscall(__NR_lstat64);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_newfstatat
|
|
|
|
AllowSyscall(__NR_newfstatat);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_oldfstat
|
|
|
|
AllowSyscall(__NR_oldfstat);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_oldlstat
|
|
|
|
AllowSyscall(__NR_oldlstat);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_oldstat
|
|
|
|
AllowSyscall(__NR_oldstat);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_stat
|
|
|
|
AllowSyscall(__NR_stat);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_stat64
|
|
|
|
AllowSyscall(__NR_stat64);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_statfs
|
|
|
|
AllowSyscall(__NR_statfs);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_statfs64
|
|
|
|
AllowSyscall(__NR_statfs64);
|
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-11-03 18:20:49 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowAccess() {
|
|
|
|
#ifdef __NR_access
|
|
|
|
AllowSyscall(__NR_access);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_faccessat
|
|
|
|
AllowSyscall(__NR_faccessat);
|
2023-01-06 16:12:59 +08:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_faccessat2
|
|
|
|
AllowSyscall(__NR_faccessat2);
|
2020-11-03 18:20:49 +08:00
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
PolicyBuilder& PolicyBuilder::AllowDup() {
|
|
|
|
AllowSyscall(__NR_dup);
|
|
|
|
#ifdef __NR_dup2
|
|
|
|
AllowSyscall(__NR_dup2);
|
|
|
|
#endif
|
|
|
|
AllowSyscall(__NR_dup3);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowPipe() {
|
|
|
|
#ifdef __NR_pipe
|
|
|
|
AllowSyscall(__NR_pipe);
|
|
|
|
#endif
|
|
|
|
AllowSyscall(__NR_pipe2);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowChmod() {
|
|
|
|
#ifdef __NR_chmod
|
|
|
|
AllowSyscall(__NR_chmod);
|
|
|
|
#endif
|
|
|
|
AllowSyscall(__NR_fchmod);
|
|
|
|
AllowSyscall(__NR_fchmodat);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowChown() {
|
|
|
|
#ifdef __NR_chown
|
|
|
|
AllowSyscall(__NR_chown);
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_lchown
|
|
|
|
AllowSyscall(__NR_lchown);
|
|
|
|
#endif
|
|
|
|
AllowSyscall(__NR_fchown);
|
|
|
|
AllowSyscall(__NR_fchownat);
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowRead() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
__NR_read,
|
|
|
|
__NR_readv,
|
|
|
|
__NR_preadv,
|
|
|
|
__NR_pread64,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowWrite() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
__NR_write,
|
|
|
|
__NR_writev,
|
|
|
|
__NR_pwritev,
|
|
|
|
__NR_pwrite64,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowReaddir() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
#ifdef __NR_getdents
|
|
|
|
__NR_getdents,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_getdents64
|
|
|
|
__NR_getdents64,
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
PolicyBuilder& PolicyBuilder::AllowReadlink() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
#ifdef __NR_readlink
|
|
|
|
__NR_readlink,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_readlinkat
|
|
|
|
__NR_readlinkat,
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowLink() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
#ifdef __NR_link
|
|
|
|
__NR_link,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_linkat
|
|
|
|
__NR_linkat,
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowSymlink() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
#ifdef __NR_symlink
|
|
|
|
__NR_symlink,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_symlinkat
|
|
|
|
__NR_symlinkat,
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowMkdir() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
#ifdef __NR_mkdir
|
|
|
|
__NR_mkdir,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_mkdirat
|
|
|
|
__NR_mkdirat,
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowUtime() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
#ifdef __NR_futimens
|
|
|
|
__NR_futimens,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_utime
|
|
|
|
__NR_utime,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_utimes
|
|
|
|
__NR_utimes,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_utimensat
|
|
|
|
__NR_utimensat,
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowSafeFcntl() {
|
|
|
|
return AddPolicyOnSyscalls({__NR_fcntl,
|
|
|
|
#ifdef __NR_fcntl64
|
|
|
|
__NR_fcntl64
|
|
|
|
#endif
|
|
|
|
},
|
|
|
|
{
|
|
|
|
ARG_32(1),
|
|
|
|
JEQ32(F_GETFD, ALLOW),
|
|
|
|
JEQ32(F_SETFD, ALLOW),
|
|
|
|
JEQ32(F_GETFL, ALLOW),
|
|
|
|
JEQ32(F_SETFL, ALLOW),
|
|
|
|
JEQ32(F_GETLK, ALLOW),
|
|
|
|
JEQ32(F_SETLK, ALLOW),
|
|
|
|
JEQ32(F_SETLKW, ALLOW),
|
|
|
|
JEQ32(F_DUPFD, ALLOW),
|
|
|
|
JEQ32(F_DUPFD_CLOEXEC, ALLOW),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowFork() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
#ifdef __NR_fork
|
|
|
|
__NR_fork,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_vfork
|
|
|
|
__NR_vfork,
|
|
|
|
#endif
|
|
|
|
__NR_clone});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowWait() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
#ifdef __NR_waitpid
|
|
|
|
__NR_waitpid,
|
|
|
|
#endif
|
|
|
|
__NR_wait4});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
PolicyBuilder& PolicyBuilder::AllowAlarm() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
#ifdef __NR_alarm
|
|
|
|
__NR_alarm,
|
|
|
|
#endif
|
|
|
|
__NR_setitimer});
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowHandleSignals() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
__NR_rt_sigaction,
|
|
|
|
__NR_rt_sigreturn,
|
|
|
|
__NR_rt_sigprocmask,
|
|
|
|
#ifdef __NR_signal
|
|
|
|
__NR_signal,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_sigaction
|
|
|
|
__NR_sigaction,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_sigreturn
|
|
|
|
__NR_sigreturn,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_sigprocmask
|
|
|
|
__NR_sigprocmask,
|
2020-07-02 05:08:35 +08:00
|
|
|
#endif
|
|
|
|
#ifdef __NR_sigaltstack
|
|
|
|
__NR_sigaltstack,
|
2019-03-19 00:21:48 +08:00
|
|
|
#endif
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowTCGETS() {
|
|
|
|
return AddPolicyOnSyscall(__NR_ioctl, {
|
|
|
|
ARG_32(1),
|
|
|
|
JEQ32(TCGETS, ALLOW),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowTime() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
#ifdef __NR_time
|
|
|
|
__NR_time,
|
|
|
|
#endif
|
|
|
|
__NR_gettimeofday, __NR_clock_gettime});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowSleep() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
__NR_clock_nanosleep,
|
|
|
|
__NR_nanosleep,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowGetIDs() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
__NR_getuid,
|
|
|
|
__NR_geteuid,
|
|
|
|
__NR_getresuid,
|
|
|
|
__NR_getgid,
|
|
|
|
__NR_getegid,
|
|
|
|
__NR_getresgid,
|
|
|
|
#ifdef __NR_getuid32
|
|
|
|
__NR_getuid32,
|
|
|
|
__NR_geteuid32,
|
|
|
|
__NR_getresuid32,
|
|
|
|
__NR_getgid32,
|
|
|
|
__NR_getegid32,
|
|
|
|
__NR_getresgid32,
|
|
|
|
#endif
|
|
|
|
__NR_getgroups,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-04-13 22:14:17 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowRestartableSequences(
|
|
|
|
CpuFenceMode cpu_fence_mode) {
|
2021-07-23 17:22:18 +08:00
|
|
|
#ifdef __NR_rseq
|
|
|
|
AllowSyscall(__NR_rseq);
|
|
|
|
#endif
|
2021-03-02 05:39:13 +08:00
|
|
|
AddPolicyOnMmap([](bpf_labels& labels) -> std::vector<sock_filter> {
|
|
|
|
return {
|
|
|
|
ARG_32(2), // prot
|
2021-07-23 17:22:18 +08:00
|
|
|
JNE32(PROT_READ | PROT_WRITE, JUMP(&labels, mmap_end)),
|
2021-03-02 05:39:13 +08:00
|
|
|
|
|
|
|
ARG_32(3), // flags
|
2021-07-23 17:22:18 +08:00
|
|
|
JNE32(MAP_PRIVATE | MAP_ANONYMOUS, JUMP(&labels, mmap_end)),
|
|
|
|
|
|
|
|
ALLOW,
|
|
|
|
LABEL(&labels, mmap_end),
|
2021-03-02 05:39:13 +08:00
|
|
|
};
|
|
|
|
});
|
2021-07-23 17:22:18 +08:00
|
|
|
AllowSyscall(__NR_getcpu);
|
|
|
|
AllowSyscall(__NR_membarrier);
|
2021-03-02 05:39:13 +08:00
|
|
|
AllowFutexOp(FUTEX_WAIT);
|
|
|
|
AllowFutexOp(FUTEX_WAKE);
|
2023-09-15 23:20:33 +08:00
|
|
|
AllowRead();
|
|
|
|
AllowOpen();
|
|
|
|
AllowSyscall(__NR_close);
|
2021-03-02 05:39:13 +08:00
|
|
|
AddPolicyOnSyscall(__NR_rt_sigprocmask, {
|
|
|
|
ARG_32(0),
|
|
|
|
JEQ32(SIG_SETMASK, ALLOW),
|
|
|
|
});
|
2021-07-23 17:22:18 +08:00
|
|
|
if (cpu_fence_mode == kAllowSlowFences) {
|
|
|
|
AllowSyscall(__NR_sched_getaffinity);
|
|
|
|
AllowSyscall(__NR_sched_setaffinity);
|
|
|
|
}
|
2023-07-17 16:58:05 +08:00
|
|
|
AddFileIfNamespaced("/proc/cpuinfo");
|
|
|
|
AddFileIfNamespaced("/proc/stat");
|
|
|
|
AddDirectoryIfNamespaced("/sys/devices/system/cpu");
|
|
|
|
if (cpu_fence_mode == kAllowSlowFences) {
|
|
|
|
AddFileIfNamespaced("/proc/self/cpuset");
|
|
|
|
}
|
2021-07-23 17:22:18 +08:00
|
|
|
return *this;
|
2021-03-02 05:39:13 +08:00
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowGetPIDs() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
__NR_getpid,
|
|
|
|
__NR_getppid,
|
|
|
|
__NR_gettid,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
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
|
|
|
PolicyBuilder& PolicyBuilder::AllowGetPGIDs() {
|
|
|
|
return AllowSyscalls({
|
|
|
|
__NR_getpgid,
|
|
|
|
#ifdef __NR_getpgrp
|
|
|
|
__NR_getpgrp,
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowGetRlimit() {
|
|
|
|
return AllowSyscalls({
|
2020-12-17 01:17:53 +08:00
|
|
|
#ifdef __NR_getrlimit
|
2019-03-19 00:21:48 +08:00
|
|
|
__NR_getrlimit,
|
2020-12-17 01:17:53 +08:00
|
|
|
#endif
|
2019-03-19 00:21:48 +08:00
|
|
|
#ifdef __NR_ugetrlimit
|
|
|
|
__NR_ugetrlimit,
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowSetRlimit() {
|
|
|
|
return AllowSyscalls({
|
2020-12-17 01:17:53 +08:00
|
|
|
#ifdef __NR_setrlimit
|
2019-03-19 00:21:48 +08:00
|
|
|
__NR_setrlimit,
|
2020-12-17 01:17:53 +08:00
|
|
|
#endif
|
2019-03-19 00:21:48 +08:00
|
|
|
#ifdef __NR_usetrlimit
|
|
|
|
__NR_usetrlimit,
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowGetRandom() {
|
|
|
|
return AddPolicyOnSyscall(__NR_getrandom, {
|
|
|
|
ARG_32(2),
|
|
|
|
JEQ32(0, ALLOW),
|
|
|
|
JEQ32(GRND_NONBLOCK, ALLOW),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2020-04-30 15:08:31 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowWipeOnFork() {
|
|
|
|
// System headers may not be recent enough to include MADV_WIPEONFORK.
|
|
|
|
static constexpr uint32_t kMadv_WipeOnFork = 18;
|
|
|
|
// The -1 value is used by code to probe that the kernel returns -EINVAL for
|
|
|
|
// unknown values because some environments, like qemu, ignore madvise
|
|
|
|
// completely, but code needs to know whether WIPEONFORK took effect.
|
|
|
|
return AddPolicyOnSyscall(__NR_madvise,
|
|
|
|
{
|
|
|
|
ARG_32(2),
|
|
|
|
JEQ32(kMadv_WipeOnFork, ALLOW),
|
|
|
|
JEQ32(static_cast<uint32_t>(-1), ALLOW),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowLogForwarding() {
|
|
|
|
AllowWrite();
|
|
|
|
AllowSystemMalloc();
|
|
|
|
AllowTcMalloc();
|
|
|
|
|
2021-11-06 02:15:52 +08:00
|
|
|
// From comms
|
|
|
|
AllowGetPIDs();
|
2019-03-19 00:21:48 +08:00
|
|
|
AllowSyscalls({// from logging code
|
|
|
|
__NR_clock_gettime,
|
|
|
|
// From comms
|
|
|
|
__NR_gettid, __NR_close});
|
|
|
|
|
2021-05-27 19:40:02 +08:00
|
|
|
// For generating stacktraces in logging (e.g. `LOG(FATAL)`)
|
|
|
|
AddPolicyOnSyscall(__NR_rt_sigprocmask, {
|
|
|
|
ARG_32(0),
|
|
|
|
JEQ32(SIG_BLOCK, ALLOW),
|
|
|
|
});
|
2021-12-06 18:18:22 +08:00
|
|
|
AllowSyscall(__NR_prlimit64);
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
// For LOG(FATAL)
|
|
|
|
return AddPolicyOnSyscall(__NR_kill,
|
|
|
|
[](bpf_labels& labels) -> std::vector<sock_filter> {
|
|
|
|
return {
|
|
|
|
ARG_32(0),
|
|
|
|
JNE32(0, JUMP(&labels, pid_not_null)),
|
|
|
|
ARG_32(1),
|
|
|
|
JEQ32(SIGABRT, ALLOW),
|
|
|
|
LABEL(&labels, pid_not_null),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2021-12-08 22:40:48 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowUnlink() {
|
|
|
|
AllowSyscalls({
|
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
|
|
|
#ifdef __NR_rmdir
|
|
|
|
__NR_rmdir,
|
|
|
|
#endif
|
2021-12-08 22:40:48 +08:00
|
|
|
#ifdef __NR_unlink
|
|
|
|
__NR_unlink,
|
|
|
|
#endif
|
|
|
|
__NR_unlinkat,
|
|
|
|
});
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
PolicyBuilder& PolicyBuilder::AllowPoll() {
|
|
|
|
AllowSyscalls({
|
|
|
|
#ifdef __NR_poll
|
|
|
|
__NR_poll,
|
|
|
|
#endif
|
|
|
|
__NR_ppoll,
|
|
|
|
});
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-12-08 22:40:48 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowRename() {
|
|
|
|
AllowSyscalls({
|
|
|
|
#ifdef __NR_rename
|
|
|
|
__NR_rename,
|
|
|
|
#endif
|
|
|
|
__NR_renameat,
|
|
|
|
#ifdef __NR_renameat2
|
|
|
|
__NR_renameat2,
|
|
|
|
#endif
|
|
|
|
});
|
|
|
|
return *this;
|
2023-03-22 22:46:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowEventFd() {
|
|
|
|
AllowSyscalls({
|
|
|
|
#ifdef __NR_eventfd
|
|
|
|
__NR_eventfd,
|
|
|
|
#endif
|
|
|
|
__NR_eventfd2,
|
|
|
|
});
|
|
|
|
return *this;
|
2021-12-08 22:40:48 +08:00
|
|
|
}
|
|
|
|
|
2022-03-23 14:38:29 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowPrctlSetName() {
|
|
|
|
AddPolicyOnSyscall(__NR_prctl, {ARG_32(0), JEQ32(PR_SET_NAME, ALLOW)});
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-06-17 00:33:25 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowPrctlSetVma() {
|
|
|
|
AddPolicyOnSyscall(__NR_prctl,
|
|
|
|
[](bpf_labels& labels) -> std::vector<sock_filter> {
|
|
|
|
return {
|
|
|
|
ARG_32(0),
|
|
|
|
JNE32(PR_SET_VMA, JUMP(&labels, prctlsetvma_end)),
|
|
|
|
ARG_32(1),
|
|
|
|
JEQ32(PR_SET_VMA_ANON_NAME, ALLOW),
|
|
|
|
LABEL(&labels, prctlsetvma_end),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowFutexOp(int op) {
|
|
|
|
return AddPolicyOnSyscall(
|
|
|
|
__NR_futex, {
|
|
|
|
ARG_32(1),
|
|
|
|
// a <- a & FUTEX_CMD_MASK
|
|
|
|
BPF_STMT(BPF_ALU + BPF_AND + BPF_K,
|
|
|
|
static_cast<uint32_t>(FUTEX_CMD_MASK)),
|
|
|
|
JEQ32(static_cast<uint32_t>(op) & FUTEX_CMD_MASK, ALLOW),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowStaticStartup() {
|
|
|
|
AllowGetRlimit();
|
|
|
|
AllowSyscalls({
|
2021-04-14 22:38:47 +08:00
|
|
|
// These syscalls take a pointer, so no restriction.
|
|
|
|
__NR_uname, __NR_brk, __NR_set_tid_address,
|
2020-12-17 01:17:53 +08:00
|
|
|
|
|
|
|
#if defined(__ARM_NR_set_tls)
|
2021-04-14 22:38:47 +08:00
|
|
|
// libc sets the TLS during startup
|
|
|
|
__ARM_NR_set_tls,
|
2020-12-17 01:17:53 +08:00
|
|
|
#endif
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-04-14 22:38:47 +08:00
|
|
|
// This syscall takes a pointer and a length.
|
|
|
|
// We could restrict length, but it might change, so not worth it.
|
|
|
|
__NR_set_robust_list,
|
2019-03-19 00:21:48 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
AllowFutexOp(FUTEX_WAIT_BITSET);
|
|
|
|
|
|
|
|
AddPolicyOnSyscall(__NR_rt_sigaction,
|
|
|
|
{
|
|
|
|
ARG_32(0),
|
|
|
|
// This is real-time signals used internally by libc.
|
|
|
|
JEQ32(__SIGRTMIN + 0, ALLOW),
|
|
|
|
JEQ32(__SIGRTMIN + 1, ALLOW),
|
|
|
|
});
|
|
|
|
|
2021-12-07 21:03:35 +08:00
|
|
|
AllowSyscall(__NR_rt_sigprocmask);
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2020-09-10 20:47:32 +08:00
|
|
|
#ifdef SAPI_X86_64
|
2019-03-19 00:21:48 +08:00
|
|
|
// The second argument is a pointer.
|
|
|
|
AddPolicyOnSyscall(__NR_arch_prctl, {
|
|
|
|
ARG_32(0),
|
|
|
|
JEQ32(ARCH_SET_FS, ALLOW),
|
|
|
|
});
|
|
|
|
#endif
|
|
|
|
|
2021-01-14 01:25:25 +08:00
|
|
|
if constexpr (sapi::host_cpu::IsArm64()) {
|
2022-03-04 00:36:36 +08:00
|
|
|
OverridableBlockSyscallWithErrno(__NR_readlinkat, ENOENT);
|
2020-09-11 21:33:57 +08:00
|
|
|
}
|
|
|
|
#ifdef __NR_readlink
|
2022-03-04 00:36:36 +08:00
|
|
|
OverridableBlockSyscallWithErrno(__NR_readlink, ENOENT);
|
2020-09-11 21:33:57 +08:00
|
|
|
#endif
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-08-29 23:11:31 +08:00
|
|
|
#ifdef __NR_prlimit64
|
|
|
|
OverridableBlockSyscallWithErrno(__NR_prlimit64, EPERM);
|
|
|
|
#endif
|
2022-02-15 16:13:56 +08:00
|
|
|
AddPolicyOnSyscall(__NR_mprotect, {
|
|
|
|
ARG_32(2),
|
|
|
|
JEQ32(PROT_READ, ALLOW),
|
|
|
|
});
|
2020-12-17 01:17:53 +08:00
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowDynamicStartup() {
|
2022-06-08 21:51:02 +08:00
|
|
|
#ifdef __ANDROID__
|
|
|
|
AllowSafeFcntl();
|
|
|
|
AllowGetIDs();
|
|
|
|
AllowGetPIDs();
|
|
|
|
AllowGetRandom();
|
|
|
|
AllowSyscalls({
|
|
|
|
#ifdef __NR_fstatfs
|
|
|
|
__NR_fstatfs,
|
|
|
|
#endif
|
|
|
|
#ifdef __NR_fstatfs64
|
|
|
|
__NR_fstatfs64,
|
|
|
|
#endif
|
|
|
|
__NR_readlinkat,
|
|
|
|
__NR_sched_getaffinity,
|
|
|
|
__NR_sched_getscheduler,
|
|
|
|
});
|
|
|
|
AllowHandleSignals();
|
|
|
|
AllowFutexOp(FUTEX_WAKE_PRIVATE);
|
|
|
|
AddPolicyOnSyscall(__NR_prctl,
|
|
|
|
[](bpf_labels& labels) -> std::vector<sock_filter> {
|
|
|
|
return {
|
|
|
|
ARG_32(0), // option
|
|
|
|
JEQ32(PR_GET_DUMPABLE, ALLOW),
|
|
|
|
JNE32(PR_SET_VMA, JUMP(&labels, prctl_end)),
|
|
|
|
|
|
|
|
ARG_32(1), // arg2
|
|
|
|
JEQ32(PR_SET_VMA_ANON_NAME, ALLOW),
|
|
|
|
|
|
|
|
LABEL(&labels, prctl_end),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
AddPolicyOnSyscall(__NR_mremap,
|
|
|
|
{
|
|
|
|
ARG_32(3),
|
|
|
|
JEQ32(MREMAP_MAYMOVE | MREMAP_FIXED, ALLOW),
|
|
|
|
});
|
|
|
|
AddPolicyOnMmap([](bpf_labels& labels) -> std::vector<sock_filter> {
|
|
|
|
return {
|
|
|
|
ARG_32(2), // prot
|
|
|
|
JEQ32(PROT_NONE, JUMP(&labels, prot_none)),
|
|
|
|
JEQ32(PROT_READ, JUMP(&labels, prot_read)),
|
|
|
|
JEQ32(PROT_READ | PROT_WRITE, JUMP(&labels, prot_RW_or_RX)),
|
|
|
|
JEQ32(PROT_READ | PROT_EXEC, JUMP(&labels, prot_RW_or_RX)),
|
|
|
|
|
|
|
|
// PROT_NONE
|
|
|
|
LABEL(&labels, prot_none),
|
|
|
|
ARG_32(3), // flags
|
|
|
|
JEQ32(MAP_PRIVATE | MAP_ANONYMOUS, ALLOW),
|
|
|
|
JEQ32(MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, ALLOW),
|
|
|
|
JUMP(&labels, mmap_end),
|
|
|
|
|
|
|
|
// PROT_READ
|
|
|
|
LABEL(&labels, prot_read),
|
|
|
|
ARG_32(3), // flags
|
|
|
|
JEQ32(MAP_SHARED, ALLOW),
|
|
|
|
JEQ32(MAP_PRIVATE, ALLOW),
|
|
|
|
JEQ32(MAP_PRIVATE | MAP_FIXED, ALLOW),
|
|
|
|
JUMP(&labels, mmap_end),
|
|
|
|
|
|
|
|
// PROT_READ | PROT_WRITE
|
|
|
|
// PROT_READ | PROT_EXEC
|
|
|
|
LABEL(&labels, prot_RW_or_RX),
|
|
|
|
ARG_32(3), // flags
|
|
|
|
JEQ32(MAP_PRIVATE | MAP_FIXED, ALLOW),
|
|
|
|
|
|
|
|
LABEL(&labels, mmap_end),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
#endif
|
2022-05-27 21:45:14 +08:00
|
|
|
|
2023-08-29 23:11:31 +08:00
|
|
|
AllowAccess();
|
|
|
|
AllowOpen();
|
2019-03-19 00:21:48 +08:00
|
|
|
AllowRead();
|
|
|
|
AllowStat();
|
2020-09-14 16:18:09 +08:00
|
|
|
AllowSyscalls({__NR_lseek,
|
|
|
|
#ifdef __NR__llseek
|
|
|
|
__NR__llseek, // Newer glibc on PPC
|
|
|
|
#endif
|
|
|
|
__NR_close, __NR_munmap});
|
2019-03-19 00:21:48 +08:00
|
|
|
AddPolicyOnSyscall(__NR_mprotect, {
|
|
|
|
ARG_32(2),
|
|
|
|
JEQ32(PROT_READ, ALLOW),
|
|
|
|
JEQ32(PROT_NONE, ALLOW),
|
|
|
|
JEQ32(PROT_READ | PROT_WRITE, ALLOW),
|
|
|
|
JEQ32(PROT_READ | PROT_EXEC, ALLOW),
|
|
|
|
});
|
|
|
|
AllowStaticStartup();
|
|
|
|
|
|
|
|
return AddPolicyOnMmap([](bpf_labels& labels) -> std::vector<sock_filter> {
|
|
|
|
return {
|
|
|
|
ARG_32(2), // prot
|
|
|
|
JEQ32(PROT_READ | PROT_EXEC, JUMP(&labels, prot_exec)),
|
|
|
|
JEQ32(PROT_READ | PROT_WRITE, JUMP(&labels, prot_read_write)),
|
|
|
|
JNE32(PROT_READ, JUMP(&labels, mmap_end)),
|
|
|
|
|
|
|
|
// PROT_READ
|
|
|
|
ARG_32(3), // flags
|
|
|
|
JEQ32(MAP_PRIVATE, ALLOW),
|
|
|
|
JUMP(&labels, mmap_end),
|
|
|
|
|
|
|
|
// PROT_READ | PROT_WRITE
|
|
|
|
LABEL(&labels, prot_read_write),
|
|
|
|
ARG_32(3), // flags
|
|
|
|
JEQ32(MAP_FILE | MAP_PRIVATE | MAP_FIXED | MAP_DENYWRITE, ALLOW),
|
|
|
|
JEQ32(MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, ALLOW),
|
|
|
|
JEQ32(MAP_ANONYMOUS | MAP_PRIVATE, ALLOW),
|
|
|
|
JUMP(&labels, mmap_end),
|
|
|
|
|
|
|
|
// PROT_READ | PROT_EXEC
|
|
|
|
LABEL(&labels, prot_exec),
|
|
|
|
ARG_32(3), // flags
|
|
|
|
JEQ32(MAP_FILE | MAP_PRIVATE | MAP_DENYWRITE, ALLOW),
|
|
|
|
|
|
|
|
LABEL(&labels, mmap_end),
|
|
|
|
};
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AddPolicyOnSyscall(
|
2021-12-13 17:31:02 +08:00
|
|
|
uint32_t num, absl::Span<const sock_filter> policy) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return AddPolicyOnSyscalls({num}, policy);
|
|
|
|
}
|
|
|
|
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AddPolicyOnSyscall(uint32_t num, BpfFunc f) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return AddPolicyOnSyscalls({num}, f);
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AddPolicyOnSyscalls(
|
2021-12-13 17:31:02 +08:00
|
|
|
absl::Span<const uint32_t> nums, absl::Span<const sock_filter> policy) {
|
2023-07-21 17:24:03 +08:00
|
|
|
if (nums.empty()) {
|
|
|
|
SetError(absl::InvalidArgumentError(
|
|
|
|
"Cannot add a policy for empty list of syscalls"));
|
|
|
|
return *this;
|
|
|
|
}
|
2021-11-12 18:44:07 +08:00
|
|
|
std::deque<sock_filter> out;
|
|
|
|
// Insert and verify the policy.
|
|
|
|
out.insert(out.end(), policy.begin(), policy.end());
|
|
|
|
for (size_t i = 0; i < out.size(); ++i) {
|
|
|
|
sock_filter& filter = out[i];
|
|
|
|
const size_t max_jump = out.size() - i - 1;
|
|
|
|
if (!CheckBpfBounds(filter, max_jump)) {
|
|
|
|
SetError(absl::InvalidArgumentError("bpf jump out of bounds"));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
// Syscall arch is expected as TRACE value
|
|
|
|
if (filter.code == (BPF_RET | BPF_K) &&
|
|
|
|
(filter.k & SECCOMP_RET_ACTION) == SECCOMP_RET_TRACE &&
|
|
|
|
(filter.k & SECCOMP_RET_DATA) != Syscall::GetHostArch()) {
|
|
|
|
LOG(WARNING) << "SANDBOX2_TRACE should be used in policy instead of "
|
|
|
|
"TRACE(value)";
|
|
|
|
filter = SANDBOX2_TRACE;
|
|
|
|
}
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
// Pre-/Postcondition: Syscall number loaded into A register
|
2021-11-12 18:44:07 +08:00
|
|
|
out.push_back(LOAD_SYSCALL_NR);
|
|
|
|
if (out.size() > std::numeric_limits<uint32_t>::max()) {
|
|
|
|
SetError(absl::InvalidArgumentError("syscall policy is too long"));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
// Create jumps for each syscall.
|
|
|
|
size_t do_policy_loc = out.size();
|
|
|
|
// Iterate in reverse order and prepend instruction, so that jumps can be
|
|
|
|
// calculated easily.
|
|
|
|
constexpr size_t kMaxShortJump = 255;
|
|
|
|
bool last = true;
|
|
|
|
for (auto it = std::rbegin(nums); it != std::rend(nums); ++it) {
|
2023-08-11 16:33:46 +08:00
|
|
|
if (*it == __NR_bpf || *it == __NR_ptrace) {
|
|
|
|
SetError(absl::InvalidArgumentError(
|
|
|
|
"cannot add policy for bpf/ptrace syscall"));
|
|
|
|
return *this;
|
|
|
|
}
|
2021-11-12 18:44:07 +08:00
|
|
|
// If syscall is not matched try with the next one.
|
|
|
|
uint8_t jf = 0;
|
|
|
|
// If last syscall on the list does not match skip the policy by jumping
|
|
|
|
// over it.
|
|
|
|
if (last) {
|
|
|
|
if (out.size() > kMaxShortJump) {
|
2021-12-06 20:50:59 +08:00
|
|
|
out.push_front(
|
|
|
|
BPF_STMT(BPF_JMP + BPF_JA, static_cast<uint32_t>(out.size())));
|
2021-11-12 18:44:07 +08:00
|
|
|
} else {
|
|
|
|
jf = out.size();
|
|
|
|
}
|
|
|
|
last = false;
|
|
|
|
}
|
|
|
|
// Add a helper absolute jump if needed - the policy/last helper jump is
|
|
|
|
// out of reach of a short jump.
|
|
|
|
if ((out.size() - do_policy_loc) > kMaxShortJump) {
|
2021-12-06 20:50:59 +08:00
|
|
|
out.push_front(BPF_STMT(
|
|
|
|
BPF_JMP + BPF_JA, static_cast<uint32_t>(out.size() - policy.size())));
|
2021-11-12 18:44:07 +08:00
|
|
|
do_policy_loc = out.size();
|
|
|
|
++jf;
|
|
|
|
}
|
|
|
|
uint8_t jt = out.size() - do_policy_loc;
|
|
|
|
out.push_front(BPF_JUMP(BPF_JMP + BPF_JEQ + BPF_K, *it, jt, jf));
|
|
|
|
}
|
|
|
|
user_policy_.insert(user_policy_.end(), out.begin(), out.end());
|
2019-03-19 00:21:48 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-12-13 17:31:02 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AddPolicyOnSyscalls(
|
|
|
|
absl::Span<const uint32_t> nums, BpfFunc f) {
|
2019-03-19 00:21:48 +08:00
|
|
|
return AddPolicyOnSyscalls(nums, ResolveBpfFunc(f));
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AddPolicyOnMmap(
|
2021-12-13 17:31:02 +08:00
|
|
|
absl::Span<const sock_filter> policy) {
|
2020-11-06 17:29:52 +08:00
|
|
|
return AddPolicyOnSyscalls(kMmapSyscalls, policy);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AddPolicyOnMmap(BpfFunc f) {
|
2020-11-06 17:29:52 +08:00
|
|
|
return AddPolicyOnSyscalls(kMmapSyscalls, f);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::DangerDefaultAllowAll() {
|
2023-03-04 02:25:39 +08:00
|
|
|
return DefaultAction(AllowAllSyscalls());
|
2023-03-01 21:35:51 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::DefaultAction(AllowAllSyscalls) {
|
|
|
|
default_action_ = ALLOW;
|
2019-03-19 00:21:48 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<std::string> PolicyBuilder::ValidateAbsolutePath(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::string_view path) {
|
|
|
|
if (!file::IsAbsolutePath(path)) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InvalidArgumentError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("Path is not absolute: '", path, "'"));
|
|
|
|
}
|
|
|
|
return ValidatePath(path);
|
|
|
|
}
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<std::string> PolicyBuilder::ValidatePath(
|
2019-03-26 22:54:02 +08:00
|
|
|
absl::string_view path) {
|
2019-03-19 00:21:48 +08:00
|
|
|
std::string fixed_path = file::CleanPath(path);
|
|
|
|
if (fixed_path != path) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::InvalidArgumentError(absl::StrCat(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Path was not normalized. '", path, "' != '", fixed_path, "'"));
|
|
|
|
}
|
|
|
|
return fixed_path;
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<sock_filter> PolicyBuilder::ResolveBpfFunc(BpfFunc f) {
|
|
|
|
bpf_labels l = {0};
|
|
|
|
|
|
|
|
std::vector<sock_filter> policy = f(l);
|
|
|
|
if (bpf_resolve_jumps(&l, policy.data(), policy.size()) != 0) {
|
2020-02-28 01:23:44 +08:00
|
|
|
SetError(absl::InternalError("Cannot resolve bpf jumps"));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
return policy;
|
|
|
|
}
|
|
|
|
|
2020-09-02 23:46:48 +08:00
|
|
|
absl::StatusOr<std::unique_ptr<Policy>> PolicyBuilder::TryBuild() {
|
2023-08-09 21:43:50 +08:00
|
|
|
if (!last_status_.ok()) {
|
|
|
|
return last_status_;
|
|
|
|
}
|
2019-10-09 01:45:07 +08:00
|
|
|
|
2021-11-11 22:10:12 +08:00
|
|
|
if (user_policy_.size() > kMaxUserPolicyLength) {
|
|
|
|
return absl::FailedPreconditionError(
|
|
|
|
absl::StrCat("User syscall policy is to long (", user_policy_.size(),
|
|
|
|
" > ", kMaxUserPolicyLength, ")."));
|
|
|
|
}
|
|
|
|
|
2023-08-09 21:43:50 +08:00
|
|
|
// Using `new` to access a non-public constructor.
|
|
|
|
auto output = absl::WrapUnique(new Policy());
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2019-10-09 01:45:07 +08:00
|
|
|
if (already_built_) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError("Can only build policy once.");
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
if (use_namespaces_) {
|
|
|
|
if (allow_unrestricted_networking_ && hostname_ != kDefaultHostname) {
|
2020-02-28 01:23:44 +08:00
|
|
|
return absl::FailedPreconditionError(
|
2019-03-19 00:21:48 +08:00
|
|
|
"Cannot set hostname without network namespaces.");
|
|
|
|
}
|
2023-08-09 21:43:50 +08:00
|
|
|
output->namespace_ =
|
|
|
|
Namespace(allow_unrestricted_networking_, std::move(mounts_), hostname_,
|
|
|
|
allow_mount_propagation_);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2019-10-09 01:45:07 +08:00
|
|
|
output->collect_stacktrace_on_signal_ = collect_stacktrace_on_signal_;
|
|
|
|
output->collect_stacktrace_on_violation_ = collect_stacktrace_on_violation_;
|
|
|
|
output->collect_stacktrace_on_timeout_ = collect_stacktrace_on_timeout_;
|
|
|
|
output->collect_stacktrace_on_kill_ = collect_stacktrace_on_kill_;
|
2021-08-16 18:12:39 +08:00
|
|
|
output->collect_stacktrace_on_exit_ = collect_stacktrace_on_exit_;
|
2019-10-09 01:45:07 +08:00
|
|
|
output->user_policy_ = std::move(user_policy_);
|
2023-03-01 21:35:51 +08:00
|
|
|
if (default_action_) {
|
|
|
|
output->user_policy_.push_back(*default_action_);
|
|
|
|
}
|
2022-03-04 00:36:36 +08:00
|
|
|
output->user_policy_.insert(output->user_policy_.end(),
|
|
|
|
overridable_policy_.begin(),
|
|
|
|
overridable_policy_.end());
|
2020-12-03 00:37:55 +08:00
|
|
|
output->user_policy_handles_bpf_ = user_policy_handles_bpf_;
|
2022-05-27 17:57:03 +08:00
|
|
|
output->user_policy_handles_ptrace_ = user_policy_handles_ptrace_;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-08-09 21:43:50 +08:00
|
|
|
PolicyBuilderDescription pb_description;
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-08-09 21:43:50 +08:00
|
|
|
StoreDescription(&pb_description);
|
|
|
|
output->policy_builder_description_ = pb_description;
|
2020-02-20 23:45:22 +08:00
|
|
|
output->allowed_hosts_ = std::move(allowed_hosts_);
|
2019-10-09 01:45:07 +08:00
|
|
|
already_built_ = true;
|
|
|
|
return std::move(output);
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AddFile(absl::string_view path, bool is_ro) {
|
|
|
|
return AddFileAt(path, path, is_ro);
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AddFileAt(absl::string_view outside,
|
|
|
|
absl::string_view inside, bool is_ro) {
|
2022-02-15 16:13:56 +08:00
|
|
|
EnableNamespaces(); // NOLINT(clang-diagnostic-deprecated-declarations)
|
2023-07-17 16:58:05 +08:00
|
|
|
return AddFileAtIfNamespaced(outside, inside, is_ro);
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AddFileIfNamespaced(absl::string_view path,
|
|
|
|
bool is_ro) {
|
|
|
|
return AddFileAtIfNamespaced(path, path, is_ro);
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-07-17 16:58:05 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AddFileAtIfNamespaced(absl::string_view outside,
|
|
|
|
absl::string_view inside,
|
|
|
|
bool is_ro) {
|
2021-09-01 16:27:56 +08:00
|
|
|
auto valid_outside = ValidateAbsolutePath(outside);
|
|
|
|
if (!valid_outside.ok()) {
|
|
|
|
SetError(valid_outside.status());
|
2019-03-19 00:21:48 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:27:56 +08:00
|
|
|
if (absl::StartsWith(*valid_outside, "/proc/self") &&
|
|
|
|
*valid_outside != "/proc/self/cpuset") {
|
2021-03-09 20:33:01 +08:00
|
|
|
SetError(absl::InvalidArgumentError(
|
|
|
|
absl::StrCat("Cannot add /proc/self mounts, you need to mount the "
|
|
|
|
"whole /proc instead. You tried to mount ",
|
|
|
|
outside)));
|
|
|
|
return *this;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
2022-03-08 15:57:28 +08:00
|
|
|
if (!is_ro && IsOnReadOnlyDev(*valid_outside)) {
|
|
|
|
SetError(absl::FailedPreconditionError(
|
|
|
|
absl::StrCat("Cannot add ", outside,
|
|
|
|
" as read-write as it's on a read-only device")));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:27:56 +08:00
|
|
|
if (auto status = mounts_.AddFileAt(*valid_outside, inside, is_ro);
|
2020-04-02 22:42:17 +08:00
|
|
|
!status.ok()) {
|
2019-08-23 23:08:23 +08:00
|
|
|
SetError(
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::InternalError(absl::StrCat("Could not add file ", outside, " => ",
|
2019-08-23 23:08:23 +08:00
|
|
|
inside, ": ", status.message())));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AddLibrariesForBinary(
|
|
|
|
absl::string_view path, absl::string_view ld_library_path) {
|
2022-02-15 16:13:56 +08:00
|
|
|
EnableNamespaces(); // NOLINT(clang-diagnostic-deprecated-declarations)
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-09-01 16:27:56 +08:00
|
|
|
auto valid_path = ValidatePath(path);
|
|
|
|
if (!valid_path.ok()) {
|
|
|
|
SetError(valid_path.status());
|
2019-03-19 00:21:48 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:27:56 +08:00
|
|
|
if (auto status = mounts_.AddMappingsForBinary(*valid_path, ld_library_path);
|
2020-04-02 22:42:17 +08:00
|
|
|
!status.ok()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
SetError(absl::InternalError(absl::StrCat(
|
2021-09-01 16:27:56 +08:00
|
|
|
"Could not add libraries for ", *valid_path, ": ", status.message())));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AddLibrariesForBinary(
|
|
|
|
int fd, absl::string_view ld_library_path) {
|
|
|
|
return AddLibrariesForBinary(absl::StrCat("/proc/self/fd/", fd),
|
|
|
|
ld_library_path);
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AddDirectory(absl::string_view path, bool is_ro) {
|
|
|
|
return AddDirectoryAt(path, path, is_ro);
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AddDirectoryAt(absl::string_view outside,
|
|
|
|
absl::string_view inside,
|
|
|
|
bool is_ro) {
|
2022-02-15 16:13:56 +08:00
|
|
|
EnableNamespaces(); // NOLINT(clang-diagnostic-deprecated-declarations)
|
2023-07-17 16:58:05 +08:00
|
|
|
return AddDirectoryAtIfNamespaced(outside, inside, is_ro);
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AddDirectoryIfNamespaced(absl::string_view path,
|
|
|
|
bool is_ro) {
|
|
|
|
return AddDirectoryAtIfNamespaced(path, path, is_ro);
|
|
|
|
}
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2023-07-17 16:58:05 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AddDirectoryAtIfNamespaced(
|
|
|
|
absl::string_view outside, absl::string_view inside, bool is_ro) {
|
2021-09-01 16:27:56 +08:00
|
|
|
auto valid_outside = ValidateAbsolutePath(outside);
|
|
|
|
if (!valid_outside.ok()) {
|
|
|
|
SetError(valid_outside.status());
|
2019-03-19 00:21:48 +08:00
|
|
|
return *this;
|
|
|
|
}
|
2021-09-01 16:27:56 +08:00
|
|
|
|
|
|
|
if (absl::StartsWith(*valid_outside, "/proc/self")) {
|
2020-02-28 01:23:44 +08:00
|
|
|
SetError(absl::InvalidArgumentError(
|
2019-03-19 00:21:48 +08:00
|
|
|
absl::StrCat("Cannot add /proc/self mounts, you need to mount the "
|
|
|
|
"whole /proc instead. You tried to mount ",
|
|
|
|
outside)));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-03-08 15:57:28 +08:00
|
|
|
if (!is_ro && IsOnReadOnlyDev(*valid_outside)) {
|
|
|
|
SetError(absl::FailedPreconditionError(
|
|
|
|
absl::StrCat("Cannot add ", outside,
|
|
|
|
" as read-write as it's on a read-only device")));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:27:56 +08:00
|
|
|
if (absl::Status status =
|
|
|
|
mounts_.AddDirectoryAt(*valid_outside, inside, is_ro);
|
2020-04-02 22:42:17 +08:00
|
|
|
!status.ok()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
SetError(absl::InternalError(absl::StrCat("Could not add directory ",
|
2019-08-23 23:08:23 +08:00
|
|
|
outside, " => ", inside, ": ",
|
|
|
|
status.message())));
|
2021-09-01 16:27:56 +08:00
|
|
|
return *this;
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:27:56 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AddTmpfs(absl::string_view inside, size_t size) {
|
2022-02-15 16:13:56 +08:00
|
|
|
EnableNamespaces(); // NOLINT(clang-diagnostic-deprecated-declarations)
|
2019-03-19 00:21:48 +08:00
|
|
|
|
2021-09-01 16:27:56 +08:00
|
|
|
if (auto status = mounts_.AddTmpfs(inside, size); !status.ok()) {
|
2020-02-28 01:23:44 +08:00
|
|
|
SetError(absl::InternalError(absl::StrCat("Could not mount tmpfs ", inside,
|
2019-08-23 23:08:23 +08:00
|
|
|
": ", status.message())));
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2023-05-04 14:29:00 +08:00
|
|
|
// Use Allow(UnrestrictedNetworking()) instead.
|
2019-03-19 00:21:48 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowUnrestrictedNetworking() {
|
2023-05-04 14:29:00 +08:00
|
|
|
return Allow(UnrestrictedNetworking());
|
2019-03-19 00:21:48 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::SetHostname(absl::string_view hostname) {
|
2022-02-15 16:13:56 +08:00
|
|
|
EnableNamespaces(); // NOLINT(clang-diagnostic-deprecated-declarations)
|
2019-03-19 00:21:48 +08:00
|
|
|
hostname_ = std::string(hostname);
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::CollectStacktracesOnViolation(bool enable) {
|
|
|
|
collect_stacktrace_on_violation_ = enable;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::CollectStacktracesOnSignal(bool enable) {
|
|
|
|
collect_stacktrace_on_signal_ = enable;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::CollectStacktracesOnTimeout(bool enable) {
|
|
|
|
collect_stacktrace_on_timeout_ = enable;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::CollectStacktracesOnKill(bool enable) {
|
|
|
|
collect_stacktrace_on_kill_ = enable;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-08-16 18:12:39 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::CollectStacktracesOnExit(bool enable) {
|
|
|
|
collect_stacktrace_on_exit_ = enable;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-07-23 19:40:47 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AddNetworkProxyPolicy() {
|
2020-02-20 23:45:22 +08:00
|
|
|
if (allowed_hosts_) {
|
2020-02-28 01:23:44 +08:00
|
|
|
SetError(absl::FailedPreconditionError(
|
2020-02-20 23:45:22 +08:00
|
|
|
"AddNetworkProxyPolicy or AddNetworkProxyHandlerPolicy can be called "
|
|
|
|
"at most once"));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
allowed_hosts_ = AllowedHosts();
|
|
|
|
|
2019-07-23 19:40:47 +08:00
|
|
|
AllowFutexOp(FUTEX_WAKE);
|
|
|
|
AllowFutexOp(FUTEX_WAIT);
|
|
|
|
AllowFutexOp(FUTEX_WAIT_BITSET);
|
2022-12-21 21:35:20 +08:00
|
|
|
AllowDup();
|
2019-07-23 19:40:47 +08:00
|
|
|
AllowSyscalls({
|
|
|
|
__NR_recvmsg,
|
|
|
|
__NR_close,
|
|
|
|
__NR_gettid,
|
|
|
|
});
|
|
|
|
AddPolicyOnSyscall(__NR_socket, {
|
|
|
|
ARG_32(0),
|
|
|
|
JEQ32(AF_INET, ALLOW),
|
|
|
|
JEQ32(AF_INET6, ALLOW),
|
|
|
|
});
|
|
|
|
AddPolicyOnSyscall(__NR_getsockopt,
|
|
|
|
[](bpf_labels& labels) -> std::vector<sock_filter> {
|
|
|
|
return {
|
|
|
|
ARG_32(1),
|
|
|
|
JNE32(SOL_SOCKET, JUMP(&labels, getsockopt_end)),
|
|
|
|
ARG_32(2),
|
|
|
|
JEQ32(SO_TYPE, ALLOW),
|
|
|
|
LABEL(&labels, getsockopt_end),
|
|
|
|
};
|
|
|
|
});
|
2020-09-10 20:47:32 +08:00
|
|
|
#ifdef SAPI_PPC64_LE
|
2019-07-23 19:40:47 +08:00
|
|
|
AddPolicyOnSyscall(__NR_socketcall, {
|
|
|
|
ARG_32(0),
|
|
|
|
JEQ32(SYS_SOCKET, ALLOW),
|
|
|
|
JEQ32(SYS_GETSOCKOPT, ALLOW),
|
|
|
|
JEQ32(SYS_RECVMSG, ALLOW),
|
|
|
|
});
|
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AddNetworkProxyHandlerPolicy() {
|
|
|
|
AddNetworkProxyPolicy();
|
|
|
|
AllowSyscall(__NR_rt_sigreturn);
|
|
|
|
|
|
|
|
AddPolicyOnSyscall(__NR_rt_sigaction, {
|
|
|
|
ARG_32(0),
|
|
|
|
JEQ32(SIGSYS, ALLOW),
|
|
|
|
});
|
|
|
|
|
|
|
|
AddPolicyOnSyscall(__NR_rt_sigprocmask, {
|
|
|
|
ARG_32(0),
|
|
|
|
JEQ32(SIG_UNBLOCK, ALLOW),
|
|
|
|
});
|
|
|
|
|
|
|
|
AddPolicyOnSyscall(__NR_connect, {TRAP(0)});
|
2020-09-10 20:47:32 +08:00
|
|
|
#ifdef SAPI_PPC64_LE
|
2019-07-23 19:40:47 +08:00
|
|
|
AddPolicyOnSyscall(__NR_socketcall, {
|
|
|
|
ARG_32(0),
|
|
|
|
JEQ32(SYS_CONNECT, TRAP(0)),
|
|
|
|
});
|
|
|
|
#endif
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2022-11-30 21:59:36 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::TrapPtrace() {
|
2023-08-11 16:33:46 +08:00
|
|
|
if (handled_syscalls_.insert(__NR_ptrace).second) {
|
|
|
|
user_policy_.insert(user_policy_.end(), {SYSCALL(__NR_ptrace, TRAP(0))});
|
|
|
|
user_policy_handles_ptrace_ = true;
|
|
|
|
}
|
2022-11-30 21:59:36 +08:00
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-11-14 19:51:01 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::SetRootWritable() {
|
2022-02-15 16:13:56 +08:00
|
|
|
EnableNamespaces(); // NOLINT(clang-diagnostic-deprecated-declarations)
|
2019-11-14 19:51:01 +08:00
|
|
|
mounts_.SetRootWritable();
|
|
|
|
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
void PolicyBuilder::StoreDescription(PolicyBuilderDescription* pb_description) {
|
|
|
|
for (const auto& handled_syscall : handled_syscalls_) {
|
|
|
|
pb_description->add_handled_syscalls(handled_syscall);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-20 23:45:22 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::AllowIPv4(const std::string& ip_and_mask,
|
|
|
|
uint32_t port) {
|
|
|
|
if (!allowed_hosts_) {
|
2020-02-28 01:23:44 +08:00
|
|
|
SetError(absl::FailedPreconditionError(
|
2020-02-20 23:45:22 +08:00
|
|
|
"AddNetworkProxyPolicy or AddNetworkProxyHandlerPolicy must be called "
|
|
|
|
"before adding IP rules"));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status status = allowed_hosts_->AllowIPv4(ip_and_mask, port);
|
2020-02-20 23:45:22 +08:00
|
|
|
if (!status.ok()) {
|
|
|
|
SetError(status);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
PolicyBuilder& PolicyBuilder::AllowIPv6(const std::string& ip_and_mask,
|
|
|
|
uint32_t port) {
|
|
|
|
if (!allowed_hosts_) {
|
2020-02-28 01:23:44 +08:00
|
|
|
SetError(absl::FailedPreconditionError(
|
2020-02-20 23:45:22 +08:00
|
|
|
"AddNetworkProxyPolicy or AddNetworkProxyHandlerPolicy must be called "
|
|
|
|
"before adding IP rules"));
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2020-02-28 01:23:44 +08:00
|
|
|
absl::Status status = allowed_hosts_->AllowIPv6(ip_and_mask, port);
|
2020-02-20 23:45:22 +08:00
|
|
|
if (!status.ok()) {
|
|
|
|
SetError(status);
|
|
|
|
}
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2021-09-01 16:27:56 +08:00
|
|
|
PolicyBuilder& PolicyBuilder::SetError(const absl::Status& status) {
|
|
|
|
LOG(ERROR) << status;
|
|
|
|
last_status_ = status;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
2019-03-19 00:21:48 +08:00
|
|
|
} // namespace sandbox2
|