Debian Codegraph @

src/pam/securemem.rs

raw symbols SCIP
1 //! Routines for "secure" memory operations; i.e. data that we need to send to Linux-PAM and don't
2 //! want any copies to leak (that we would then need to zeroize).
3 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::{
4 allocrust-analyzer cargo std https://github.com/rust-lang/rust/library/std alloc/::{selfrust-analyzer cargo std https://github.com/rust-lang/rust/library/std alloc/, Layoutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/Layout#},
5 ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#,
6 slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/,
7 };
8
9 const SIZErust-analyzer cargo sudo-rs 0.2.14 pam/securemem/SIZE.: usize = superrust-analyzer cargo sudo-rs 0.2.14 pam/::sysrust-analyzer cargo sudo-rs 0.2.14 pam/sys/::PAM_MAX_RESP_SIZErust-analyzer cargo sudo-rs 0.2.14 pam/sys/PAM_MAX_RESP_SIZE. as usize;
10
11 pub struct PamBufferrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer#(NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#<[u8; SIZErust-analyzer cargo sudo-rs 0.2.14 pam/securemem/SIZE.]>);
12
13 const LAYOUTrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/LAYOUT.: Layoutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/Layout# = match Layoutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/Layout#::from_size_alignrust-analyzer cargo core https://github.com/rust-lang/rust/library/core alloc/layout/impl#[Layout]from_size_align().(SIZErust-analyzer cargo sudo-rs 0.2.14 pam/securemem/SIZE., 1) {
14 Okrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Ok#(layout) => layout,
15 Errrust-analyzer cargo core https://github.com/rust-lang/rust/library/core result/Result#Err#(_) => unreachablerust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/unreachable!!(),
16 };
17
18 impl PamBufferrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer# {
19 // consume this buffer and return its internal pointer
20 // (ending the type-level security, but guaranteeing you need unsafe code to access the data)
21 pub fn leakrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/impl#[PamBuffer]leak().(self) -> NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#<u8> {
22 let result = self.0rust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer#0.;
23 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::forgetrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/forget().(self);
24
25 result.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]cast().()
26 }
27
28 // initialize the buffer with already existing data (otherwise populating it is a bit hairy)
29 // this is inferior to placing the data into the securebuffer directly
30 #[cfg(test)]
31 pub fn newrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/impl#[PamBuffer]new().(mut src: impl AsMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#<[u8]>) -> Selfrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/impl#[PamBuffer] {
32 let mut buffer = PamBufferrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer#::defaultrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/impl#[PamBuffer][Default]default().();
33 let src = src.as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core convert/AsMut#as_mut().();
34 buffer[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()...rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/range/RangeTo#src.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]len().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/IndexMut#index_mut()..copy_from_slicerust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]copy_from_slice().(src);
35 wipe_memoryrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/wipe_memory().(src);
36
37 buffer
38 }
39 }
40
41 impl Defaultrust-analyzer cargo core https://github.com/rust-lang/rust/library/core default/Default# for PamBufferrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer# {
42 fn defaultrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/impl#[PamBuffer][Default]default().() -> Selfrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/impl#[PamBuffer][Default] {
43 // SAFETY: `calloc` returns either a cleared, allocated chunk of `SIZE` bytes
44 // or NULL to indicate that the allocation request failed
45 let res = unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::callocrust-analyzer cargo libc 0.2.186 calloc().(1, SIZErust-analyzer cargo sudo-rs 0.2.14 pam/securemem/SIZE.) };
46 if let Somerust-analyzer cargo core https://github.com/rust-lang/rust/library/core option/Option#Some#(nn) = NonNullrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/NonNull#::newrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]new().(res) {
47 PamBufferrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer#(nn.castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]cast().())
48 } else {
49 allocrust-analyzer cargo std https://github.com/rust-lang/rust/library/std alloc/::handle_alloc_errorrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc alloc/handle_alloc_error().(LAYOUTrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/LAYOUT.)
50 }
51 }
52 }
53
54 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::Derefrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/Deref# for PamBufferrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer# {
55 type Targetrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/impl#[PamBuffer][Deref]Target# = [u8];
56
57 fn derefrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/impl#[PamBuffer][Deref]deref().(&self) -> &[u8] {
58 // SAFETY: `self.0.as_ptr()` is non-null, aligned, and initialized, and points to `SIZE` bytes.
59 // The lifetime of the slice does not exceed that of `self`.
60 //
61 // We make the slice one less in size to guarantee the existence of a terminating NUL.
62 unsafe { slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/::from_raw_partsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts().(self.0rust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer#0..as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]as_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().(), SIZErust-analyzer cargo sudo-rs 0.2.14 pam/securemem/SIZE. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1) }
63 }
64 }
65
66 impl stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::opsrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/::DerefMutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/deref/DerefMut# for PamBufferrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer# {
67 fn deref_mutrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/impl#[PamBuffer][DerefMut]deref_mut().(&mut self) -> &mut [u8] {
68 // SAFETY: see above
69 unsafe { slicerust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc slice/::from_raw_parts_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/raw/from_raw_parts_mut().(self.0rust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer#0..as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]as_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().(), SIZErust-analyzer cargo sudo-rs 0.2.14 pam/securemem/SIZE. rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().-rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub(). rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/arith/impl#[usize][`Sub<Self>`]sub().1) }
70 }
71 }
72
73 impl Droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/drop/Drop# for PamBufferrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer# {
74 fn droprust-analyzer cargo sudo-rs 0.2.14 pam/securemem/impl#[PamBuffer][Drop]drop().(&mut self) {
75 // SAFETY: same as for `deref()` and `deref_mut()`
76 wipe_memoryrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/wipe_memory().(unsafe { self.0rust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer#0..as_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]as_mut().() });
77 // SAFETY: `self.0.as_ptr()` was obtained via `calloc`, so calling `free` is proper.
78 unsafe { libcrust-analyzer cargo libc 0.2.186 crate/::freerust-analyzer cargo libc 0.2.186 free().(self.0rust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer#0..as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]as_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().()) }
79 }
80 }
81
82 /// Used to zero out memory and protect sensitive data from leaking; inspired by Conrad Kleinespel's
83 /// Rustatic rtoolbox::SafeString, <https://crates.io/crates/rtoolbox/0.0.1>
84 fn wipe_memoryrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/wipe_memory().(memory: &mut [u8]) {
85 use stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::syncrust-analyzer cargo std https://github.com/rust-lang/rust/library/std sync/::atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/;
86
87 let nonsense: u8 = 0x55;
88 for c in memory {
89 // SAFETY: `c` is safe for writes (it comes from a &mut reference)
90 unsafe { stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/::write_volatilerust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/write_volatile().(c, nonsense) };
91 }
92
93 atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::fencerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/fence().(atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#);
94 atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::compiler_fencerust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/compiler_fence().(atomicrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/::Orderingrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#::SeqCstrust-analyzer cargo core https://github.com/rust-lang/rust/library/core sync/atomic/Ordering#SeqCst#);
95 }
96
97 #[allow(clippy::undocumented_unsafe_blocks)]
98 #[cfg(test)]
99 mod testrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/test/ {
100 use superrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/::PamBufferrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer#;
101
102 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
103 fn miri_test_leaky_cstringrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/test/miri_test_leaky_cstring().() {
104 let test = |text: &str| unsafe {
105 let buf = PamBufferrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer#::newrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/impl#[PamBuffer]new().(text.to_stringrust-analyzer cargo alloc https://github.com/rust-lang/rust/library/alloc string/impl#[T][ToString]to_string().().as_bytes_mutrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes_mut().());
106 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(&buf[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()...text.lenrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]len().()]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., text.as_bytesrust-analyzer cargo core https://github.com/rust-lang/rust/library/core str/impl#[str]as_bytes().());
107 let nn = buf.leakrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/impl#[PamBuffer]leak().();
108 let result = craterust-analyzer cargo sudo-rs 0.2.14 crate/::cutilsrust-analyzer cargo sudo-rs 0.2.14 cutils/::string_from_ptrrust-analyzer cargo sudo-rs 0.2.14 cutils/string_from_ptr().(nn.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]as_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().());
109 libcrust-analyzer cargo libc 0.2.186 crate/::freerust-analyzer cargo libc 0.2.186 free().(nn.as_ptrrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/non_null/impl#[`NonNull<T>`]as_ptr().().castrust-analyzer cargo core https://github.com/rust-lang/rust/library/core ptr/mut_ptr/impl#[`*mut T`]cast().());
110 result
111 };
112 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(test(""), "");
113 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(test("hello"), "hello");
114 }
115
116 #[testrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/test:]
117 fn miri_test_wiperust-analyzer cargo sudo-rs 0.2.14 pam/securemem/test/miri_test_wipe().() {
118 let mut memory: [u8; 3] = [1, 2, 3];
119 let fix = PamBufferrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/PamBuffer#::newrust-analyzer cargo sudo-rs 0.2.14 pam/securemem/impl#[PamBuffer]new().(&mut memory);
120 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(memory, [0x55, 0x55, 0x55]);
121 assert_eqrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/assert_eq!!(fix[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().0..=2]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()., [1, 2, 3]);
122 assertrust-analyzer cargo core https://github.com/rust-lang/rust/library/core macros/builtin/assert!!(fix[rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index().3..]rust-analyzer cargo core https://github.com/rust-lang/rust/library/core ops/index/Index#index()..iterrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/impl#[`[T]`]iter().().allrust-analyzer cargo core https://github.com/rust-lang/rust/library/core slice/iter/impl#[`Iter<'a, T>`][Iterator]all().(|&x| x == 0));
123 stdrust-analyzer cargo std https://github.com/rust-lang/rust/library/std crate/::memrust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/::droprust-analyzer cargo core https://github.com/rust-lang/rust/library/core mem/drop().(fix);
124 }
125 }