#freebsd #sandbox

capsicum

为FreeBSD capsicum框架提供简单直观的Rust绑定

9个不稳定版本 (3个破坏性更新)

0.4.2 2024年6月4日
0.4.1 2024年6月4日
0.3.0 2023年9月21日
0.2.0 2023年2月16日
0.1.1 2016年6月12日

#76 in Unix API

Download history 8/week @ 2024-04-29 8/week @ 2024-05-06 10/week @ 2024-05-13 37/week @ 2024-05-20 8/week @ 2024-05-27 451/week @ 2024-06-03 35/week @ 2024-06-10 37/week @ 2024-06-17 27/week @ 2024-06-24 22/week @ 2024-07-01 17/week @ 2024-07-08 9/week @ 2024-07-15 16/week @ 2024-07-22 15/week @ 2024-07-29 10/week @ 2024-08-05 14/week @ 2024-08-12

56 每月下载量
用于 3 crates

MPL-2.0 许可证

58KB
844 代码行

capsicum

Current Version

包含精彩内容!

为FreeBSD capsicum框架提供Rust绑定,用于操作系统能力和沙盒

先决条件

RustCargo,和 FreeBSD.

注意:当前仅支持在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.3–0.9MB
~21K SLoC