Debian Codegraph @

src/lib.rs

raw symbols SCIP
1 extern crate libcrust-analyzer cargo unveil 0.3.2 libc/;
2
3 #[cfg(target_os = "openbsd")]
4 mod openbsd;
5
6 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::NulErrorrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/NulError#;
7 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/, fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/};
8
9 #[deriverust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/derive:(Debugrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/macros/Debug:, PartialEqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core cmp/PartialEq:)]
10 pub enum Errorrust-analyzer cargo unveil 0.3.2 Error# {
11 NotSupportedrust-analyzer cargo unveil 0.3.2 Error#NotSupported#,
12 Pathrust-analyzer cargo unveil 0.3.2 Error#Path#(NulErrorrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/NulError#),
13 Permissionsrust-analyzer cargo unveil 0.3.2 Error#Permissions#(NulErrorrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/NulError#),
14 Osrust-analyzer cargo unveil 0.3.2 Error#Os#(i32),
15 }
16
17 impl Errorrust-analyzer cargo unveil 0.3.2 Error# {
18 pub fn ignore_platformrust-analyzer cargo unveil 0.3.2 impl#[Error]ignore_platform().(self) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Selfrust-analyzer cargo unveil 0.3.2 impl#[Error]> {
19 match self {
20 Errorrust-analyzer cargo unveil 0.3.2 Error#::NotSupportedrust-analyzer cargo unveil 0.3.2 Error#NotSupported# => Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(()),
21 x => Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(x),
22 }
23 }
24 }
25
26 impl fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Displayrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Display# for Errorrust-analyzer cargo unveil 0.3.2 Error# {
27 fn fmtrust-analyzer cargo unveil 0.3.2 impl#[Error][Display]fmt().(&self, f: &mut fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Formatterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Formatter#) -> fmtrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc fmt/::Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core fmt/Result# {
28 match *rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/impl#[`&T`][Deref]deref().self {
29 Errorrust-analyzer cargo unveil 0.3.2 Error#::NotSupportedrust-analyzer cargo unveil 0.3.2 Error#NotSupported# => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "platform is not supported"),
30 Errorrust-analyzer cargo unveil 0.3.2 Error#::Pathrust-analyzer cargo unveil 0.3.2 Error#Path#(_) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "unexpected NUL character in path argument"),
31 Errorrust-analyzer cargo unveil 0.3.2 Error#::Permissionsrust-analyzer cargo unveil 0.3.2 Error#Permissions#(_) => {
32 writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "unexpected NUL character in permissions argument")
33 }
34 Errorrust-analyzer cargo unveil 0.3.2 Error#::Osrust-analyzer cargo unveil 0.3.2 Error#Os#(errno) => writerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/write!!(f, "unable to unveil ({})", errno),
35 }
36 }
37 }
38
39 impl errorrust-analyzer cargo std https://github.com/rust-lang/rust/library/std error/::Errorrust-analyzer cargo core https://github.com/rust-lang/rust/library/core error/Error# for Errorrust-analyzer cargo unveil 0.3.2 Error# {}
40
41 #[cfg(target_os = "openbsd")]
42 pub fn unveil(path: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>, permissions: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo unveil 0.3.2 Error#> {
43 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ffirust-analyzer cargo std https://github.com/rust-lang/rust/library/std ffi/::CStringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc ffi/c_str/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(Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#, Nonerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#None#).map_err(Errorrust-analyzer cargo unveil 0.3.2 Error#::Osrust-analyzer cargo unveil 0.3.2 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(Errorrust-analyzer cargo unveil 0.3.2 Error#::Pathrust-analyzer cargo unveil 0.3.2 Error#Path#)?;
58 let permissions = CString::new(permissions).map_err(Errorrust-analyzer cargo unveil 0.3.2 Error#::Permissionsrust-analyzer cargo unveil 0.3.2 Error#Permissions#)?;
59
60 openbsd::unveil(Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&path), Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(&permissions)).map_err(Errorrust-analyzer cargo unveil 0.3.2 Error#::Osrust-analyzer cargo unveil 0.3.2 Error#Os#)
61 }
62
63 #[cfg(not(target_os = "openbsd"))]
64 #[allow(unused_variables)]
65 pub fn unveilrust-analyzer cargo unveil 0.3.2 unveil().(path: impl AsRefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsRef#<[u8]>, permissions: &str) -> Resultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#<(), Errorrust-analyzer cargo unveil 0.3.2 Error#> {
66 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(Errorrust-analyzer cargo unveil 0.3.2 Error#::NotSupportedrust-analyzer cargo unveil 0.3.2 Error#NotSupported#)
67 }
68
69 #[cfg(test)]
70 mod testsrust-analyzer cargo unveil 0.3.2 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 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
79 #[cfg(target_os = "openbsd")]
80 fn test_unveil() {
81 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/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_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/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_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
92 unveil("/dev/null", ""),
93 Ok(()),
94 "unveil child with empty permissions should succeed",
95 );
96
97 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(unveil("/dev", "r"), Ok(()), "unveil parent should succeed");
98
99 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
100 unveil("", ""),
101 Ok(()),
102 "unveil empty strings should lock successfully",
103 );
104
105 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(
106 unveil(".", "r").unwrap_err(),
107 Error::Os(libc::EPERM),
108 "simple unveil after locking should throw EPERM",
109 );
110
111 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::fsrust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/::Filerust-analyzer cargo std https://github.com/rust-lang/rust/library/std fs/File#;
112
113 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
114 File::open("/dev/zero").is_ok(),
115 "opening /dev/zero should succeed",
116 );
117
118 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(
119 File::open("/dev/null").is_err(),
120 "opening /dev/null should fail",
121 );
122 }
123
124 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
125 #[cfg(not(target_os = "openbsd"))]
126 fn test_unveil_not_supportedrust-analyzer cargo unveil 0.3.2 tests/test_unveil_not_supported().() {
127 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(unveil(".", "r").unwrap_err(), Error::NotSupported);
128 }
129 }