bitwarden_crypto/
allocator.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
use core::slice;
use std::alloc::{GlobalAlloc, Layout};

use zeroize::Zeroize;

/// Custom allocator that zeroizes memory before deallocating it
///
/// This is highly recommended to be enabled when using the Bitwarden crates to avoid sensitive data
/// persisting in memory after it has been deallocated.
///
/// This allocator is a decorator around another allocator.
///
/// # Example
///
/// This example shows how to use the `ZeroizingAllocator` with the system allocator.
///
/// ```rust,ignore
/// #[global_allocator]
/// static ALLOC: bitwarden_crypto::ZeroizingAllocator<std::alloc::System> =
///    bitwarden_crypto::ZeroizingAllocator(std::alloc::System);
/// ```
pub struct ZeroizingAllocator<Alloc: GlobalAlloc>(pub Alloc);

unsafe impl<T: GlobalAlloc> GlobalAlloc for ZeroizingAllocator<T> {
    unsafe fn alloc(&self, layout: Layout) -> *mut u8 {
        self.0.alloc(layout)
    }

    unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) {
        slice::from_raw_parts_mut(ptr, layout.size()).zeroize();

        self.0.dealloc(ptr, layout);
    }

    unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 {
        self.0.alloc_zeroed(layout)
    }
}

#[cfg(test)]
mod tests {
    #[test]
    #[ignore = "It produces inconsistent results on some platforms"]
    fn string() {
        let s = String::from("hello");

        let p1 = s.as_str().as_ptr();
        let c1 = s.capacity();

        assert_eq!(
            unsafe { std::slice::from_raw_parts(p1, c1) },
            b"hello",
            "String is not at the expected memory location"
        );

        drop(s);

        assert_eq!(
            unsafe { std::slice::from_raw_parts(p1, c1) },
            [0, 0, 0, 0, 0],
            "memory was not zeroized after dropping the string"
        );
    }

    #[test]
    #[ignore = "It produces inconsistent results on some platforms"]
    fn string_expand() {
        let mut s = String::from("hello");

        let p1 = s.as_str().as_ptr();
        let c1 = s.capacity();

        assert_eq!(unsafe { std::slice::from_raw_parts(p1, c1) }, b"hello");

        s.push_str(" world");

        let p2 = s.as_str().as_ptr();
        let c2 = s.capacity();

        // We allocated a new string
        assert_ne!(p1, p2);
        assert_eq!(
            unsafe { std::slice::from_raw_parts(p1, c1) },
            [0, 0, 0, 0, 0],
            "old string was not zeroized"
        );

        assert_eq!(
            unsafe { std::slice::from_raw_parts(p2, c2) },
            b"hello world"
        );

        // Drop the expanded string
        drop(s);

        assert_eq!(
            unsafe { std::slice::from_raw_parts(p2, c2) },
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            "expanded string was not zeroized"
        );
    }

    #[test]
    #[ignore = "It produces inconsistent results on some platforms"]
    fn vec() {
        let v = vec![1, 2, 3, 4, 5];

        let p1 = v.as_slice().as_ptr();
        let c1 = v.capacity();

        assert_eq!(
            unsafe { std::slice::from_raw_parts(p1, c1) },
            [1, 2, 3, 4, 5],
            "vec is not at the expected memory location"
        );

        drop(v);

        assert_eq!(
            unsafe { std::slice::from_raw_parts(p1, c1) },
            [0, 0, 0, 0, 0],
            "vec was not zeroized after dropping"
        );
    }

    #[test]
    #[ignore = "It produces inconsistent results on some platforms"]
    fn vec_expand() {
        let mut v = vec![1, 2, 3, 4, 5];

        let p1 = v.as_slice().as_ptr();
        let c1 = v.capacity();

        assert_eq!(
            unsafe { std::slice::from_raw_parts(p1, c1) },
            [1, 2, 3, 4, 5],
            "vec is not at the expected memory location"
        );

        v.extend_from_slice(&[6, 7, 8, 9, 10]);

        let p2 = v.as_slice().as_ptr();
        let c2 = v.capacity();

        // We allocated a new vector
        assert_ne!(p1, p2);
        assert_eq!(
            unsafe { std::slice::from_raw_parts(p1, c1) },
            [0, 0, 0, 0, 0],
            "old vec was not zeroized"
        );

        assert_eq!(
            unsafe { std::slice::from_raw_parts(p2, c2) },
            [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
        );

        // Drop the expanded vector
        drop(v);

        assert_eq!(
            unsafe { std::slice::from_raw_parts(p2, c2) },
            [0, 0, 0, 0, 0, 0, 0, 0, 0, 0],
            "expanded vec was not zeroized"
        );
    }
}