#freebsd #sandbox #capsicum

sys casper-sys

FreeBSD 的 libcasper 的 FFI 绑定

2 个版本

0.1.1 2023 年 8 月 7 日
0.1.0 2023 年 2 月 16 日

#4#free-bsd

Download history 26/week @ 2024-03-11 16/week @ 2024-03-18 27/week @ 2024-03-25 58/week @ 2024-04-01 22/week @ 2024-04-08 13/week @ 2024-04-15 23/week @ 2024-04-22 9/week @ 2024-04-29 8/week @ 2024-05-06 12/week @ 2024-05-13 40/week @ 2024-05-20 8/week @ 2024-05-27 48/week @ 2024-06-03 7/week @ 2024-06-10 21/week @ 2024-06-17 15/week @ 2024-06-24

91 每月下载量
4 个crate中使用(直接使用2个)

MPL-2.0 许可证

9KB
110

capsicum

Current Version

包含令人惊叹的功能!

Rust 为 FreeBSD 的 capsicum 框架提供的 OS 能力和沙盒绑定的 Rust 绑定

先决条件

RustCargoFreeBSD

注意: 目前仅在 FreeBSD 上编译

入门

获取代码

git clone https://github.com/danlrobertson/capsicum-rs
cd capsicum-rs
cargo build

使用 capsicum-rs 编写代码

进入能力模式

    use capsicum::{enter, sandboxed};
    use std::fs::File;
    use std::io::Read;

    let mut ok_file = File::open("/tmp/foo").unwrap();
    let mut s = String::new();

    enter().expect("enter failed!");
    assert!(sandboxed(), "application is not sandboxed!");

    match File::create("/tmp/cant_touch_this") {
        Ok(_) => panic!("application is not properly sandboxed!"),
        Err(e) => println!("properly sandboxed: {:?}", e)
    }

    match ok_file.read_to_string(&mut s) {
        Ok(_) => println!("This is okay since we opened the descriptor before sandboxing"),
        Err(_) => panic!("application is not properly sandboxed!")
    }

限制文件的能力权限

    use capsicum::{CapRights, Right, RightsBuilder};
    use std::fs::File;
    use std::io::Read;

    let x = rand::random::<bool>();
    
    let mut ok_file = File::open("/tmp/foo").unwrap();
    let mut s = String::new();
    
    let mut builder = RightsBuilder::new(Right::Seek);
    
    if x {
        builder.add(Right::Read);
    }

    let rights = builder.finalize().unwrap();

    rights.limit(&ok_file).unwrap();
    
    match ok_file.read_to_string(&mut s) {
        Ok(_) if x => println!("Allowed reading: x = {} ", x),
        Err(_) if !x => println!("Did not allow reading: x = {}", x),
        _ => panic!("Not properly sandboxed"),
    }

依赖项

~0–1.9MB
~37K SLoC