| 1 | extern crate libc; |
| 2 | |
| 3 | #[cfg(target_os = "openbsd")] |
| 4 | mod openbsd; |
| 5 | |
| 6 | use std::ffi::NulError; |
| 7 | use std::{error, fmt}; |
| 8 | |
| 9 | #[derive(Debug, PartialEq)] |
| 10 | pub enum Error { |
| 11 | NotSupported, |
| 12 | Path(NulError), |
| 13 | Permissions(NulError), |
| 14 | Os(i32), |
| 15 | } |
| 16 | |
| 17 | impl Error { |
| 18 | pub fn ignore_platform(self) -> Result<(), Self> { |
| 19 | match self { |
| 20 | Error::NotSupported => Ok(()), |
| 21 | x => Err(x), |
| 22 | } |
| 23 | } |
| 24 | } |
| 25 | |
| 26 | impl fmt::Display for Error { |
| 27 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 28 | match *self { |
| 29 | Error::NotSupported => write!(f, "platform is not supported"), |
| 30 | Error::Path(_) => write!(f, "unexpected NUL character in path argument"), |
| 31 | Error::Permissions(_) => { |
| 32 | write!(f, "unexpected NUL character in permissions argument") |
| 33 | } |
| 34 | Error::Os(errno) => write!(f, "unable to unveil ({})", errno), |
| 35 | } |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | impl error::Error for Error {} |
| 40 | |
| 41 | #[cfg(target_os = "openbsd")] |
| 42 | pub fn unveil(path: impl AsRef<[u8]>, permissions: &str) -> Result<(), Error> { |
| 43 | use std::ffi::CString; |
| 44 | |
| 45 | let path = path.as_ref(); |
| 46 | |
| 47 | // iff path is empty, pass (NULL, NULL) to lock unveil(2). POSIX |
| 48 | // doesn't allow empty pathnames, and unveil(2) assigns no other |
| 49 | // meaning to empty path as of OpenBSD 6.8, so this is safe. |
| 50 | if path.is_empty() { |
| 51 | return openbsd::unveil(None, None).map_err(Error::Os); |
| 52 | } |
| 53 | |
| 54 | // empty permissions means "deny all operations on path", which |
| 55 | // is useful to override an ancestor's allowed operations. there |
| 56 | // is no meaning for (non-NULL, NULL) as of OpenBSD 6.8. |
| 57 | let path = CString::new(path).map_err(Error::Path)?; |
| 58 | let permissions = CString::new(permissions).map_err(Error::Permissions)?; |
| 59 | |
| 60 | openbsd::unveil(Some(&path), Some(&permissions)).map_err(Error::Os) |
| 61 | } |
| 62 | |
| 63 | #[cfg(not(target_os = "openbsd"))] |
| 64 | #[allow(unused_variables)] |
| 65 | pub fn unveil(path: impl AsRef<[u8]>, permissions: &str) -> Result<(), Error> { |
| 66 | Err(Error::NotSupported) |
| 67 | } |
| 68 | |
| 69 | #[cfg(test)] |
| 70 | mod tests { |
| 71 | use *; |
| 72 | |
| 73 | // all tests for OpenBSD targets should live in one test function, |
| 74 | // because the ones that affect process state need to run in a |
| 75 | // specific order. with separate test functions, the tests would |
| 76 | // run in a random order (by default) or in lexicographic order |
| 77 | // of test names (cargo test -- --test-threads=1). |
| 78 | #[test] |
| 79 | #[cfg(target_os = "openbsd")] |
| 80 | fn test_unveil() { |
| 81 | assert_eq!(unveil(".", "r"), Ok(()), "simple unveil should succeed"); |
| 82 | |
| 83 | // null(4) is a file, and FFh is a valid filename (despite |
| 84 | // being invalid UTF-8), so this should only throw ENOTDIR |
| 85 | assert_eq!( |
| 86 | unveil(b"/dev/null/\xFF", "r").unwrap_err(), |
| 87 | Error::Os(libc::ENOTDIR), |
| 88 | "unveil binary path under regular file should throw ENOTDIR", |
| 89 | ); |
| 90 | |
| 91 | assert_eq!( |
| 92 | unveil("/dev/null", ""), |
| 93 | Ok(()), |
| 94 | "unveil child with empty permissions should succeed", |
| 95 | ); |
| 96 | |
| 97 | assert_eq!(unveil("/dev", "r"), Ok(()), "unveil parent should succeed"); |
| 98 | |
| 99 | assert_eq!( |
| 100 | unveil("", ""), |
| 101 | Ok(()), |
| 102 | "unveil empty strings should lock successfully", |
| 103 | ); |
| 104 | |
| 105 | assert_eq!( |
| 106 | unveil(".", "r").unwrap_err(), |
| 107 | Error::Os(libc::EPERM), |
| 108 | "simple unveil after locking should throw EPERM", |
| 109 | ); |
| 110 | |
| 111 | use std::fs::File; |
| 112 | |
| 113 | assert!( |
| 114 | File::open("/dev/zero").is_ok(), |
| 115 | "opening /dev/zero should succeed", |
| 116 | ); |
| 117 | |
| 118 | assert!( |
| 119 | File::open("/dev/null").is_err(), |
| 120 | "opening /dev/null should fail", |
| 121 | ); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | #[cfg(not(target_os = "openbsd"))] |
| 126 | fn test_unveil_not_supported() { |
| 127 | assert_eq!(unveil(".", "r").unwrap_err(), Error::NotSupported); |
| 128 | } |
| 129 | } |