priv_sep

Privilege separation library.
git clone https://git.philomathiclife.com/repos/priv_sep
Log | Files | Refs | README

commit 5047736d6f7a7724765d77699c7b1fb0b0ec3964
parent 1d851f90648606c63a8c956a75f7dbb4dbda6920
Author: Zack Newman <zack@philomathiclife.com>
Date:   Wed,  8 Nov 2023 15:43:36 -0700

remove libc dep on non-openbsd. minor cleanup

Diffstat:
MCargo.toml | 6+++---
Msrc/lib.rs | 36+++++++++++++++++++++---------------
2 files changed, 24 insertions(+), 18 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml @@ -9,14 +9,14 @@ license = "MIT OR Apache-2.0" name = "priv_sep" readme = "README.md" repository = "https://git.philomathiclife.com/repos/priv_sep/" -version = "0.8.0" +version = "0.8.1" [lib] name = "priv_sep" path = "src/lib.rs" -[dependencies] -libc = { version = "0.2.149", default-features = false, features = ["std"], optional = true } +[target.'cfg(target_os = "openbsd")'.dependencies] +libc = { version = "0.2.150", default-features = false, features = ["std"], optional = true } [build-dependencies] rustc_version = "0.4.0" diff --git a/src/lib.rs b/src/lib.rs @@ -581,25 +581,31 @@ impl Permissions { /// Returns [`NulError`] iff [`CString::new`] does. /// Returns [`io::Error`] iff `unveil(2)` errors. #[inline] - #[allow(unsafe_code)] + #[allow(unsafe_code, clippy::as_conversions, clippy::cast_lossless)] pub fn unveil<P: AsRef<Path>>(self, path: P) -> Result<(), UnveilErr> { - let mut vec = Vec::new(); - if self.create { - vec.push(b'c'); - } - if self.execute { - vec.push(b'x'); - } - if self.read { - vec.push(b'r'); - } - if self.write { - vec.push(b'w'); - } - vec.push(0); CString::new(path.as_ref().as_os_str().as_bytes()).map_or_else( |e| Err(UnveilErr::Nul(e)), |path_c| { + let mut vec = Vec::with_capacity( + self.create as usize + + self.execute as usize + + self.read as usize + + self.write as usize + + 1, + ); + if self.create { + vec.push(b'c'); + } + if self.execute { + vec.push(b'x'); + } + if self.read { + vec.push(b'r'); + } + if self.write { + vec.push(b'w'); + } + vec.push(0); // SAFETY: // `vec` was populated above with correct ASCII-encoding of the literal // values all of which do not have 0 bytes.