-
Notifications
You must be signed in to change notification settings - Fork 5
/
trust.rs
382 lines (326 loc) · 10.2 KB
/
trust.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
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
use std::collections::HashMap;
use std::fs::File;
use std::io::prelude::*;
use std::io::BufReader;
use std::path::Path;
use lmdb::{Cursor, Environment, Transaction};
use crate::api;
use crate::api::{Trust, TrustSource};
use crate::sha::sha256_digest;
use crate::trust::TrustOp::{Add, Del};
/// Trust status tag
/// T / U / unk
#[derive(Clone)]
pub enum Status {
/// No entry in database
Unknown(api::Trust),
/// filesystem matches database
Trusted(api::Trust),
/// filesystem does not match database
/// lhs expected, rhs actual
Discrepancy(api::Trust, String),
// todo;; what about file does not exist?
}
struct TrustPair {
k: String,
v: String,
}
impl TrustPair {
fn new(b: (&[u8], &[u8])) -> TrustPair {
TrustPair {
k: String::from_utf8(Vec::from(b.0)).unwrap(),
v: String::from_utf8(Vec::from(b.1)).unwrap(),
}
}
}
impl From<TrustPair> for api::Trust {
fn from(kv: TrustPair) -> Self {
let (t, v) = kv.v.split_once(' ').unwrap();
parse_strtyped_trust_record(format!("{} {}", kv.k, v).as_str(), t).unwrap()
}
}
/// load the fapolicyd backend lmdb database
/// parse the results into trust entries
pub fn load_trust_db(path: &str) -> Vec<api::Trust> {
let env = Environment::new().set_max_dbs(1).open(Path::new(path));
let env = match env {
Ok(e) => e,
_ => {
println!("WARN: fapolicyd trust db was not found");
return vec![];
}
};
let db = env.open_db(Some("trust.db")).expect("load trust.db");
env.begin_ro_txn()
.map(|t| {
t.open_ro_cursor(db).map(|mut c| {
c.iter()
.map(|c| c.unwrap())
.map(TrustPair::new)
.map(|kv| kv.into())
.collect()
})
})
.unwrap()
.unwrap()
}
/// load a fapolicyd ancillary file trust database
/// used to analyze the fapolicyd trust db for out of sync issues
pub fn load_ancillary_trust(path: &str) -> Vec<api::Trust> {
let f = File::open(path);
let f = match f {
Ok(e) => e,
_ => {
println!("WARN: fapolicyd trust file was not found");
return vec![];
}
};
let r = BufReader::new(f);
r.lines()
.map(|r| r.unwrap())
.filter(|s| !s.is_empty() && !s.starts_with('#'))
.map(|l| parse_trust_record(&l).unwrap())
.collect()
}
fn parse_strtyped_trust_record(s: &str, t: &str) -> Result<api::Trust, String> {
match t {
"1" => parse_typed_trust_record(s, TrustSource::System),
"2" => parse_typed_trust_record(s, TrustSource::Ancillary),
_ => Err("unknown trust type".to_string()),
}
}
fn parse_trust_record(s: &str) -> Result<api::Trust, String> {
parse_typed_trust_record(s, TrustSource::Ancillary)
}
fn parse_typed_trust_record(s: &str, t: api::TrustSource) -> Result<api::Trust, String> {
let mut v: Vec<&str> = s.rsplitn(3, ' ').collect();
v.reverse();
match v.as_slice() {
[f, sz, sha] => Ok(api::Trust {
path: f.to_string(),
size: sz.parse().unwrap(),
hash: sha.to_string(),
source: t,
}),
_ => Err(format!("failed to read record; {}", s)),
}
}
#[derive(Clone, Debug)]
enum TrustOp {
Add(String),
Del(String),
Ins(String, u64, String),
}
impl TrustOp {
fn run(&self, trust: &mut HashMap<String, Trust>) -> Result<(), String> {
match self {
TrustOp::Add(path) => match new_trust_record(&path) {
Ok(t) => {
trust.insert(path.to_string(), t);
Ok(())
}
Err(_) => Err("failed to add trust".to_string()),
},
TrustOp::Ins(path, size, hash) => {
trust.insert(
path.clone(),
Trust {
path: path.to_string(),
size: *size,
hash: hash.clone(),
source: TrustSource::Ancillary,
},
);
Ok(())
}
TrustOp::Del(path) => {
trust.remove(path);
Ok(())
}
}
}
}
fn to_pair(trust_op: &TrustOp) -> (String, String) {
match trust_op {
TrustOp::Add(path) => (path.to_string(), "Add".to_string()),
TrustOp::Del(path) => (path.to_string(), "Del".to_string()),
TrustOp::Ins(path, _size, _hash) => (path.to_string(), "Ins".to_string()),
}
}
pub enum ChangesetErr {
NotFound,
}
/// mutable append-only container for change operations
#[derive(Clone, Debug)]
pub struct Changeset {
changes: Vec<TrustOp>,
}
impl Changeset {
pub fn new() -> Self {
Changeset { changes: vec![] }
}
/// generate a modified trust map
pub fn apply(&self, trust: HashMap<String, Trust>) -> HashMap<String, Trust> {
let mut modified = trust;
for change in self.changes.iter() {
change.run(&mut modified).unwrap()
}
modified
}
pub fn add(&mut self, path: &str) {
self.changes.push(Add(path.to_string()))
}
pub fn del(&mut self, path: &str) {
self.changes.push(Del(path.to_string()))
}
pub fn len(&self) -> usize {
self.changes.len()
}
pub fn is_empty(&self) -> bool {
self.changes.is_empty()
}
}
pub fn get_path_action_map(cs: &Changeset) -> HashMap<String, String> {
cs.changes.iter().map(to_pair).collect()
}
impl ::std::default::Default for Changeset {
fn default() -> Self {
Self { changes: vec![] }
}
}
fn new_trust_record(path: &str) -> Result<Trust, String> {
let f = File::open(path).map_err(|_| "failed to open file".to_string())?;
let sha = sha256_digest(BufReader::new(&f)).map_err(|_| "failed to hash file".to_string())?;
Ok(Trust {
path: path.to_string(),
size: f.metadata().unwrap().len(),
hash: sha,
source: TrustSource::Ancillary,
})
}
trait InsChange {
fn ins(&mut self, path: &str, size: u64, hash: &str);
}
impl InsChange for Changeset {
fn ins(&mut self, path: &str, size: u64, hash: &str) {
self.changes
.push(TrustOp::Ins(path.to_string(), size, hash.to_string()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_record() {
let s =
"/home/user/my-ls 157984 61a9960bf7d255a85811f4afcac51067b8f2e4c75e21cf4f2af95319d4ed1b87";
let e = parse_trust_record(s).unwrap();
assert_eq!(e.path, "/home/user/my-ls");
assert_eq!(e.size, 157984);
assert_eq!(
e.hash,
"61a9960bf7d255a85811f4afcac51067b8f2e4c75e21cf4f2af95319d4ed1b87"
);
}
#[test]
fn parse_record_with_space() {
let s =
"/home/user/my ls 157984 61a9960bf7d255a85811f4afcac51067b8f2e4c75e21cf4f2af95319d4ed1b87";
let e = parse_trust_record(s).unwrap();
assert_eq!(e.path, "/home/user/my ls");
assert_eq!(e.size, 157984);
assert_eq!(
e.hash,
"61a9960bf7d255a85811f4afcac51067b8f2e4c75e21cf4f2af95319d4ed1b87"
);
}
#[test]
// todo;; additional coverage for type 2 and invalid type
fn parse_trust_pair() {
let tp = TrustPair::new((
"/home/user/my-ls".as_bytes(),
"1 157984 61a9960bf7d255a85811f4afcac51067b8f2e4c75e21cf4f2af95319d4ed1b87".as_bytes(),
));
let t: Trust = tp.into();
assert_eq!(t.path, "/home/user/my-ls");
assert_eq!(t.size, 157984);
assert_eq!(
t.hash,
"61a9960bf7d255a85811f4afcac51067b8f2e4c75e21cf4f2af95319d4ed1b87"
);
}
fn make_trust(path: &str, size: u64, hash: &str) -> Trust {
Trust {
path: path.to_string(),
size,
hash: hash.to_string(),
source: TrustSource::Ancillary,
}
}
fn make_default_trust_at(path: &str) -> Trust {
Trust {
path: path.to_string(),
..make_default_trust()
}
}
fn make_default_trust() -> Trust {
make_trust(
"/home/user/my_ls",
157984,
"61a9960bf7d255a85811f4afcac51067b8f2e4c75e21cf4f2af95319d4ed1b87",
)
}
#[test]
fn changeset_simple() {
let expected = make_default_trust();
let mut xs = Changeset::new();
xs.ins(&*expected.path, expected.size, &*expected.hash);
assert_eq!(xs.len(), 1);
let store = xs.apply(HashMap::new());
assert_eq!(store.len(), 1);
let actual = store.get(&expected.path).unwrap();
assert_eq!(*actual, expected);
}
#[test]
fn changeset_multiple_changes() {
let mut xs = Changeset::new();
xs.ins("/foo/bar", 1000, "12345");
xs.ins("/foo/fad", 1000, "12345");
assert_eq!(xs.len(), 2);
let store = xs.apply(HashMap::new());
assert_eq!(store.len(), 2);
}
#[test]
fn changeset_del_existing() {
let mut existing = HashMap::new();
existing.insert("/foo/bar".to_string(), make_default_trust_at("/foo/bar"));
assert_eq!(existing.len(), 1);
let mut xs = Changeset::new();
xs.del("/foo/bar");
assert_eq!(xs.len(), 1);
let store = xs.apply(existing);
assert_eq!(store.len(), 0);
}
#[test]
fn changeset_add_then_del() {
let mut xs = Changeset::new();
xs.ins("/foo/bar", 1000, "12345");
assert_eq!(xs.len(), 1);
xs.del("/foo/bar");
assert_eq!(xs.len(), 2);
let store = xs.apply(HashMap::new());
assert_eq!(store.len(), 0);
}
#[test]
fn changeset_multiple_changes_same_file() {
let expected = make_default_trust();
let mut xs = Changeset::new();
xs.ins(&*expected.path, 1000, "12345");
assert_eq!(xs.len(), 1);
xs.ins(&*expected.path, expected.size, &*expected.hash);
let store = xs.apply(HashMap::new());
assert_eq!(store.len(), 1);
let actual = store.get(&expected.path).unwrap();
assert_eq!(*actual, expected);
}
}