| 1 | # unveil-rs |
| 2 | |
| 3 | [](https://crates.io/crates/unveil) |
| 4 | [](https://docs.rs/unveil) |
| 5 | |
| 6 | Rust binding for OpenBSD's [unveil(2)](https://man.openbsd.org/unveil.2). |
| 7 | |
| 8 | ## Requirements |
| 9 | |
| 10 | - OpenBSD 6.4 or later |
| 11 | |
| 12 | ## Usage |
| 13 | |
| 14 | ```rust |
| 15 | extern crate unveil; |
| 16 | |
| 17 | use std::fs::File; |
| 18 | use std::io::prelude::*; |
| 19 | use unveil::unveil; |
| 20 | |
| 21 | fn main() { |
| 22 | let path = "public.txt"; |
| 23 | let contents = b"Hello world!"; |
| 24 | File::create(path).unwrap().write_all(contents).unwrap(); |
| 25 | |
| 26 | // Restrict filesystem view by only allowing read operations on the specified path |
| 27 | unveil(path, "r") |
| 28 | .or_else(unveil::Error::ignore_platform) |
| 29 | .unwrap(); |
| 30 | |
| 31 | // Reading from unveiled paths will succeed |
| 32 | let mut file = File::open(path).unwrap(); |
| 33 | let mut buffer = Vec::new(); |
| 34 | file.read_to_end(&mut buffer).unwrap(); |
| 35 | assert_eq!(contents, &buffer[..]); |
| 36 | |
| 37 | // Reading from paths which have not been unveiled will fail |
| 38 | assert!(File::open("/etc/passwd").is_err()); |
| 39 | |
| 40 | // Disable further calls to unveil |
| 41 | unveil("", "") |
| 42 | .or_else(unveil::Error::ignore_platform) |
| 43 | .unwrap(); |
| 44 | |
| 45 | // All calls to unveil will now fail |
| 46 | assert!(unveil(path, "rw").is_err()); |
| 47 | } |
| 48 | ``` |
| 49 | |
| 50 | ## Related projects |
| 51 | |
| 52 | - [pledge-rs](https://crates.io/crates/pledge) - Rust binding for OpenBSD's pledge(2). |