Debian Codegraph @

src/common/mod.rs

raw symbols SCIP
1 #![forbid(unsafe_code)]
2
3 use std::ffi::OsStr;
4 use std::fmt::{self, Write};
5
6 pub use command::CommandAndArguments;
7 pub use context::Context;
8 pub use error::Error;
9 pub use path::SudoPath;
10 pub use string::SudoString;
11
12 pub mod bin_serde;
13 pub mod command;
14 pub mod context;
15 pub mod error;
16 mod path;
17 pub mod resolve;
18 mod string;
19
20 // Hardened enum values used for critical enums to mitigate attacks like Rowhammer.
21 // See for example https://arxiv.org/pdf/2309.02545.pdf
22 // The values are copied from https://github.com/sudo-project/sudo/commit/7873f8334c8d31031f8cfa83bd97ac6029309e4f#diff-b8ac7ab4c3c4a75aed0bb5f7c5fd38b9ea6c81b7557f775e46c6f8aa115e02cd
23 pub const HARDENED_ENUM_VALUE_0: u32 = 0x52a2925; // 0101001010100010100100100101
24 pub const HARDENED_ENUM_VALUE_1: u32 = 0xad5d6da; // 1010110101011101011011011010
25 pub const HARDENED_ENUM_VALUE_2: u32 = 0x69d61fc8; // 1101001110101100001111111001000
26 pub const HARDENED_ENUM_VALUE_3: u32 = 0x1629e037; // 0010110001010011110000000110111
27 pub const HARDENED_ENUM_VALUE_4: u32 = 0x1fc8d3ac; // 11111110010001101001110101100
28 pub const HARDENED_ENUM_VALUE_5: u32 = 0x70372c52; // 1110000001101110010110001010011
29
30 // FIXME replace with OsStr::display() once our MSRV is 1.87
31 pub(crate) struct DisplayOsStr<'a>(pub(crate) &'a OsStr);
32
33 impl fmt::Display for DisplayOsStr<'_> {
34 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35 for chunk in self.0.as_encoded_bytes().utf8_chunks() {
36 let valid = chunk.valid();
37 // If we successfully decoded the whole chunk as a valid string then
38 // we can return a direct formatting of the string which will also
39 // respect various formatting flags if possible.
40 if chunk.invalid().is_empty() {
41 return valid.fmt(f);
42 }
43
44 f.write_str(valid)?;
45 f.write_char(char::REPLACEMENT_CHARACTER)?;
46 }
47 Ok(())
48 }
49 }