8个版本
使用旧Rust 2015
0.4.2 | 2021年6月16日 |
---|---|
0.4.1 | 2020年11月21日 |
0.4.0 | 2019年12月24日 |
0.3.1 | 2017年11月28日 |
0.1.0 | 2016年9月4日 |
#153 in Unix API
513次每月下载
用于 17 个crate(13直接使用)
14KB
250 行
pledge-rs
OpenBSD的pledge(2)接口的Rust绑定。
用法
/* Rust 2015 only */ #[macro_use] extern crate pledge;
/* Rust 2018 only */ use pledge::{pledge, pledge_promises, pledge_execpromises};
fn foo() {
// make both promises and execpromises
pledge![Stdio Proc Exec, Stdio Tty].unwrap();
// make promises only
pledge_promises![Stdio Exec].unwrap();
// make execpromises only
pledge_execpromises![Stdio].unwrap();
}
这大致等同于
/* Rust 2015 only */ extern crate pledge;
use pledge::{pledge, Promise, ToPromiseString};
fn foo() {
// make both promises and execpromises
let promises = vec![Promise::Stdio, Promise::Proc, Promise::Exec];
let execpromises = vec![Promise::Stdio, Promise::Tty];
pledge(&*promises.to_promise_string(), &*execpromises.to_promise_string()).unwrap();
// make promises only
let promises = vec![Promise::Stdio, Promise::Exec];
pledge(&*promises.to_promise_string(), None).unwrap();
// make execpromises only
let execpromises = vec![Promise::Stdio];
pledge(None, &*execpromises.to_promise_string()).unwrap();
}
您也可以直接提供承诺作为字符串
/* Rust 2015 only */ extern crate pledge;
use pledge::pledge;
fn foo() {
// make both promises and execpromises
pledge("stdio proc exec", "stdio tty").unwrap();
// make promises only
pledge("stdio exec", None).unwrap();
// make execpromises only
pledge(None, "stdio").unwrap();
}
所有这些在不支持pledge(2)的平台都将产生 pledge::Error::UnsupportedPlatform
。您可以使用 pledge::Error::ignore_platform
忽略该变体,并使您的程序可移植到那些平台
/* Rust 2015 only */ extern crate pledge;
/* Rust 2018 only */ use pledge::pledge_promises;
fn foo() {
...
pledge_promises![Stdio Exec]
.or_else(pledge::Error::ignore_platform)
.unwrap();
...
}
兼容性
此crate版本与OpenBSD 6.3+接口兼容,其中第二个参数限制了execve(2)之后的进程权限,并且保证与Rust 1.24.0+(由OpenBSD 6.3提供)兼容。
对于OpenBSD 5.9+接口,使用版本 ^0.3
,这是由Bitrig支持的最后一个版本,其中第二个参数设置允许的路径白名单。
要从旧版本迁移代码
- 将
pledge![P, Q, R]
调用站点更改为pledge_promises![P Q R]
- 将
pledge("p q r")
调用站点更改为pledge("p q r", None)
- 将
pledge_with_paths(promises, paths)
修改为pledge(promises)
- 更新重命名后的
Promise
变体使用(例如MCast
→Mcast
) - 考虑在 execve(2) 之后使用 execpromises 限制进程
- 考虑使用 unveil(2) 和 unveil crate(OpenBSD 6.4+)
依赖项
~43KB