seccomp-sandbox/README.md

66 lines
2.3 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# x2t 沙箱
为了避免 x2t 解析文档时,被通过内存溢出而执行未信任代码,对 x2t 做一层沙箱,限制 syscall 调用
## Quick start
### Setup
```bash
curl --proto '=https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
```
### Build
```
cargo build
```
### Run
```
cargo run
```
### Generate syscalls with strace
```
strace -f --output x2t-syscalls.txt /path/to/x2t some.xml
```
### Generate syscalls with tracing mode
cargo 开启 tracing-mode 后,宏找不到环境变量和文件不会失败,可以直接生成一个。
```
RUST_LOG=trace cargo run --features tracing-mode -- -l x2t-syscalls.txt /path/to/x2t some.xml
cat x2t-syscalls.txt | sort | uniq | sponge x2t-syscalls.txt
cargo build
```
更新 syscall 列表后重新构建二进制会生成新的 sandbox。
## 项目结构
- [项目](/)
- [x2t-syscalls.txt](x2t-syscalls.txt): x2t 用到的 syscall
- [build.rs](build.rs): 解决少数构建平台无法找到 libseccomp 的问题
- [x2t-sandbox-rulegen](x2t-sandbox-rulegen/): 通过 macro 直接从 syscall 列表生成代码不用内嵌syscall名字文本了不容易被修改
- Cargo.toml/Cargo.lock/rust-toolchain.toml: 已经被配置为默认构建静态链接的二进制
- optional: flake.nix/flake.lock: nix 搭建的、完全统一的统一开发环境,配置为默认构建静态链接的二进制
- .envrc: 如果不适用nix注释掉或者 direnv block 一下不加载就行
- .gitignore
## TODOs
- 看看怎么样对特定的 syscall 限制参数,尤其是
- execve
- prctl
## 比较相近的项目
- [cloudflare/sandbox](https://github.com/cloudflare/sandbox): cloudflare/sandbox 在运行时配置规则,我们是在构建时配置规则
- [Cloudflare | Sandboxing in Linux with zero lines of code](https://blog.cloudflare.com/sandboxing-in-linux-with-zero-lines-of-code/): cloudflare 写了一篇博客讲解 seccomp 工作机制和 sandbox 使用
- [google/nsjail](https://github.com/google/nsjail): 隔离更全面,使用了命名空间和 cgroup 来限制访问,是一个类似于容器但是目标主要是解决安全问题的解决方案
- [Figma | Server-side sandboxing: Containers and seccomp](https://www.figma.com/blog/server-side-sandboxing-containers-and-seccomp) figma 的 use case