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
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
// Copyright (C) 2015 Mickaël Salaün
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Lesser General Public License as published by
// the Free Software Foundation, version 3 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.

#![allow(deprecated)]

#![feature(btree_range)]
#![feature(collections)]
#![feature(collections_bound)]
#![feature(into_cow)]
#![feature(rustc_private)]

extern crate collections;
extern crate graphviz;

use collections::{Bound, BTreeMap, BTreeSet};
use collections::btree_map::Entry;
use collections::btree_set::Range;
use graphviz as dot;
use std::borrow::{Cow, IntoCow};
use std::cmp::Ordering;
use std::fmt;
use std::hash::{Hash, Hasher, SipHasher};
use std::ops::Deref;
use std::sync::Arc;

pub use fs::{absolute_path, FileAccess, RefAccess};

macro_rules! set {
    ($($v: expr),+) => ({
        let mut b = BTreeSet::new();
        $(let _ = b.insert($v);)+
        b
    })
}

mod fs;

pub trait VecAccess {
    fn uniquify(mut self) -> Self;
}

pub trait SetAccess<A> where A: Access {
    fn is_allowed(&self, access: &A) -> bool;
    fn insert_dedup(&mut self, access: A) -> bool;
    fn range_read<'a>(&'a self) -> Range<'a, A>;
    fn range_write<'a>(&'a self) -> Range<'a, A>;

    fn insert_dedup_all<I>(&mut self, access_iter: I) -> bool where I: Iterator<Item=A> {
        access_iter.fold(true, |prev, x| self.insert_dedup(x) && prev)
    }
}

pub trait Access: Deref<Target=FileAccess> + Clone + fmt::Debug + Eq + Ord + Sized {
    fn new(inner: FileAccess) -> Self;

    fn new_intersect(&self, access: Self) -> Option<Self> {
        // Avoid duplicate ressources
        if *self == access {
            return Some(self.clone());
        }
        if self.contains(&*access) {
            return Some(access);
        }
        None
    }

    // Assume there is no duplicates in `other`
    fn new_intersect_all(&self, other: Vec<Self>) -> Option<Vec<Self>> {
        // TODO: Uniquify vec!(self).append(other_access)
        vec2opt(other.into_iter().filter_map(|x| self.new_intersect(x)).collect())
    }
}

#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub enum Action {
    Read,
    Write,
}

impl fmt::Display for Action {
    fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
        let txt = match *self {
            Action::Read => "read",
            Action::Write => "write",
        };
        write!(out, "{}", txt)
    }
}

fn vec2opt<A>(vec: Vec<A>) -> Option<Vec<A>> {
    if vec.len() == 0 {
        None
    } else {
        Some(vec)
    }
}

fn set2opt<T>(list: BTreeSet<T>) -> Option<BTreeSet<T>> where T: Ord {
    if list.len() == 0 {
        None
    } else {
        Some(list)
    }
}

/// Intersection domains should precede final domain to allow more transitions
// TODO: Check order and priority
#[derive(Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
enum DomainKind {
    Intersection = 1,
    Final = 2,
}

#[derive(Eq, Debug)]
pub struct Domain<A> where A: Access {
    pub name: String,
    kind: DomainKind,
    pub acl: BTreeSet<A>,
    underlays: BTreeSet<Arc<Domain<A>>>,
}

// Do not check underlays: do not add duplicate domains
// Do not check name: omit equivalent domain with different name
impl<A> PartialEq for Domain<A> where A: Access {
    // TODO: Check ptr value?
    fn eq(&self, other: &Self) -> bool {
        self.kind == self.kind &&
            self.acl == other.acl
    }
}

// Do not check underlays: optimize sorting
// Do not check name: optimize sorting
impl<A> PartialOrd for Domain<A> where A: Access {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match self.acl.partial_cmp(&other.acl) {
            Some(Ordering::Equal) => self.kind.partial_cmp(&other.kind),
            pord => pord,
        }
    }
}

// Do not check underlays: optimize sorting
// Do not check name: optimize sorting
impl<A> Ord for Domain<A> where A: Access {
    fn cmp(&self, other: &Self) -> Ordering {
        match self.acl.cmp(&other.acl) {
            Ordering::Equal => self.kind.cmp(&other.kind),
            ord => ord,
        }
    }
}

impl<A> Domain<A> where A: Access {
    /// The ressources should have been uniquified
    fn new(name: String, kind: DomainKind, acl: BTreeSet<A>,
           underlays: BTreeSet<Arc<Domain<A>>>) -> Domain<A> {
        Domain {
            name: name,
            kind: kind,
            acl: acl,
            underlays: underlays,
        }
    }
}

/// Avoid recursive hashing by assuming the domain name is unique!
impl<A> Hash for Domain<A> where A: Access {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.name.hash(state);
        self.kind.hash(state);
    }
}

impl<A> fmt::Display for Domain<A> where A: Access {
    fn fmt(&self, out: &mut fmt::Formatter) -> fmt::Result {
        write!(out, "{}", self.name)
    }
}

pub trait RefDom<A> where A: Access, Self: Sized {
    fn is_allowed(&self, access: &A) -> bool;
    fn allow(&self, acl: &Vec<A>) -> Option<Vec<A>>;
    fn reachable(&self, acl: &Vec<A>) -> Option<Self>;
    fn transition(self, target: Self) -> Option<Self>;
}

macro_rules! get_allow {
    ($a: expr, $b: expr) => {
        $a.acl.iter().filter_map(
            |x| {
                if $b.is_allowed(&x) {
                    Some(x.clone())
                } else {
                    None
                }
            })
    }
}

// Hack to use Rc/Arc with self
impl<A> RefDom<A> for Arc<Domain<A>> where A: Access {
    fn is_allowed(&self, access: &A) -> bool {
        self.acl.is_allowed(access)
    }

    fn allow(&self, acl: &Vec<A>) -> Option<Vec<A>> {
        vec2opt(acl.iter().filter_map(
            |x| {
                if self.is_allowed(&x) {
                    Some(x.clone())
                } else {
                    None
                }
            }).collect())
    }

    /// Split the transition in two steps: transition(reachable(acl).unwrap())
    fn reachable(&self, acl: &Vec<A>) -> Option<Self> {
        // Check if all ACL are allowed
        let denied: Vec<_> = acl.iter().filter(|x| !self.is_allowed(x)).cloned().collect();
        if denied.is_empty() {
            return Some(self.clone());
        }
        self.underlays.iter().fold(None,
            |prev, x| {
                // Do not need to re-check the current (domains) allowed ACL because underlays
                // domains are part of the current intersection.
                match x.reachable(&denied) {
                    Some(dom) => match prev {
                        None => Some(dom),
                        Some(p) => p.new_intersect(&dom),
                    },
                    None => prev,
                }
            })
    }

    /// Useful on the monitor side
    fn transition(self, target: Self) -> Option<Self> {
        if target == self {
            Some(self)
        } else {
            self.transition_underlays(&target)
        }
    }
}

trait RefDomPriv where Self: Sized {
    fn new_intersect(&self, other: &Self) -> Option<Self>;
    fn new_intersect_all(&self, others: &Vec<&Self>) -> Option<Self>;
    fn connect_names(&self, others: &Vec<&Self>, separator: &str) -> String;
    fn leaves_names(&self) -> Vec<String>;
    fn leaves(&self) -> BTreeSet<Self>;
    fn transition_underlays(&self, target: &Self) -> Option<Self>;
}

impl<A> RefDomPriv for Arc<Domain<A>> where A: Access {
    /// Get an intersection domain
    fn new_intersect(&self, other: &Self) -> Option<Self> {
        // TODO: Replace with BTree range
        let self_allow = get_allow!(self, other);
        let other_allow = get_allow!(other, self);
        let acl: Vec<_> = self_allow.chain(other_allow).collect();
        // No new ressources to uniquify so it's OK
        let acl = acl.uniquify();
        let acl: BTreeSet<_> = acl.into_iter().collect();
        // No need to dedup acl
        if acl.len() == 0 {
            // Non-overlapping domains
            None
        } else {
            // Overlapping domains
            Some(Arc::new(Domain::new(self.connect_names(&vec!(other), " ∩ "),
                DomainKind::Intersection, acl, set!(self.clone(), other.clone()))))
        }
    }

    // FIXME: Only create an intersection domains, not new by pair (cf. underlays)
    // TODO: Do flat intersections
    fn new_intersect_all(&self, others: &Vec<&Self>) -> Option<Self> {
        others.iter().fold(Some(self.clone()),
            |prev, x| {
                match prev {
                    None => None,
                    Some(p) => p.new_intersect(x),
                }
            })
    }

    fn connect_names(&self, others: &Vec<&Self>, separator: &str) -> String {
        let mut names = others.iter().fold(self.leaves_names(),
            |mut prev, x| {
                prev.append(&mut x.leaves_names());
                prev
            });
        names.sort();
        names.dedup();
        names.join(separator)
    }

    // TODO: Return Vec<&str>
    fn leaves_names(&self) -> Vec<String> {
        // TODO: Forbid domain with same name (and return a BTreeSet)
        self.leaves().iter().map(|x| x.name.clone()).collect()
    }

    fn leaves(&self) -> BTreeSet<Self> {
        if self.underlays.len() == 0 {
            set!(self.clone())
        } else {
            // Recursive call
            self.underlays.iter().flat_map(|x| x.leaves().into_iter()).collect()
        }
    }

    fn transition_underlays(&self, target: &Self) -> Option<Self> {
        match self.underlays.range(Bound::Included(target), Bound::Included(target)).next() {
            Some(x) => Some(x.clone()),
            None => {
                for dom in self.underlays.iter() {
                    match dom.transition_underlays(&target) {
                        Some(d) => return Some(d),
                        None => {}
                    }
                }
                None
            }
        }
    }
}


// TODO: Remove all `fs` module references
pub struct ResPool<A> where A: Access {
    ressources: BTreeMap<A, BTreeSet<Arc<Domain<A>>>>,
    domains: BTreeSet<Arc<Domain<A>>>,
}

impl<A> ResPool<A> where A: Access {
    pub fn new() -> ResPool<A> {
        ResPool {
            ressources: BTreeMap::new(),
            domains: BTreeSet::new(),
        }
    }

    /// Create a domain if it doesn't have a twin or return an existing equivalent domain (can have
    /// a different name).
    // FIXME: Check the domain name uniqueness (cf. domain hash)
    pub fn new_dom(&mut self, name: String, acl: Vec<A>) -> Arc<Domain<A>> {
        let acl: BTreeSet<_> = acl.uniquify().into_iter().collect();
        let dom = Arc::new(Domain::new(name, DomainKind::Final, acl, BTreeSet::new()));
        self.insert_dom(dom)
    }

    pub fn contains_dom(&self, dom: &Arc<Domain<A>>) -> bool {
        self.domains.contains(dom)
    }

    /// Record a domain if it doesn't have a twin or return an existing equivalent domain (can have
    /// a different name).
    pub fn insert_dom(&mut self, dom: Arc<Domain<A>>) -> Arc<Domain<A>> {
        // If the domain is already registered
        if ! self.domains.insert(dom.clone()) {
            // A BTreeSet::entry() would avoid unwrap()
            return self.domains.range(Bound::Included(&dom), Bound::Included(&dom)).
                next().unwrap().clone();
        }

        for access in dom.acl.iter() {
            match self.ressources.entry(access.clone()) {
                Entry::Vacant(view) => {
                    let _ = view.insert(set!(dom.clone()));
                }
                Entry::Occupied(view) => {
                    let _ = view.into_mut().insert(dom.clone());
                }
            }
        }

        // Recursive domain insertion
        for sub in dom.underlays.iter() {
            if ! self.contains_dom(sub) {
                let _ = self.insert_dom(sub.clone());
            }
        }
        dom
    }

    /// Get (or create) the tighter domain with all this ACL
    pub fn allow(&mut self, acl: &Vec<A>) -> Option<Arc<Domain<A>>> {
        let doms = {
            let allow = |access| {
                // Take all domains globing the access
                set2opt(self.ressources.range(Bound::Included(access), Bound::Unbounded).
                    take_while(|&(k, _)| k.action == access.action).
                    filter(|&(k, _)| k.contains(&*access)).
                    fold(BTreeSet::new(), |prev, (_, v)| prev.union(v).cloned().collect()))
            };
            let mut aci = acl.iter();
            let doms = match aci.next() {
                Some(x) => {
                    match allow(x) {
                        Some(y) => y,
                        None => return None,
                    }
                }
                None => return None,
            };
            aci.fold(Some(doms), |prev, access| {
                match prev {
                    Some(p) => {
                        match allow(access) {
                            Some(da) => set2opt(p.intersection(&da).cloned().collect()),
                            None => None,
                        }
                    }
                    None => None,
                }
            })
        };
        match doms {
            Some(doms) => {
                let mut domi = doms.iter();
                match domi.next() {
                    Some(d) => {
                        match d.new_intersect_all(&domi.collect()) {
                            Some(d) => Some(self.insert_dom(d)),
                            None => None,
                        }
                    }
                    None => None,
                }
            }
            None => None,
        }
    }
}

#[derive(Clone, Eq)]
pub enum Node<A> where A: Access {
    Access(A),
    Dom(Arc<Domain<A>>),
}

/// A node do not take into account the access mode: read, write.
impl<A> PartialEq for Node<A> where A: Access {
    fn eq(&self, other: &Self) -> bool {
        match (self, other) {
            (&Node::Access(ref x), &Node::Access(ref y)) => x.path == y.path,
            (&Node::Dom(ref x), &Node::Dom(ref y)) => {
                // Same as Hash implementation for Domain (name must be unique)
                x.kind == y.kind && x.name == y.name
            }
            (_, _) => false,
        }
    }
}

impl<A> PartialOrd for Node<A> where A: Access {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match (self, other) {
            (&Node::Access(ref x), &Node::Access(ref y)) => x.path.partial_cmp(&y.path),
            (&Node::Dom(ref x), &Node::Dom(ref y)) => {
                // Same as Hash implementation for Domain (name must be unique)
                match x.kind.partial_cmp(&y.kind) {
                    None => x.name.partial_cmp(&y.name),
                    o => o,
                }
            }
            (_, _) => None,
        }
    }
}

impl<A> Ord for Node<A> where A: Access {
    fn cmp(&self, other: &Self) -> Ordering {
        match (self, other) {
            (&Node::Access(ref x), &Node::Access(ref y)) => x.path.cmp(&y.path),
            (&Node::Dom(ref x), &Node::Dom(ref y)) => {
                // Same as Hash implementation for Domain (name must be unique)
                match x.kind.cmp(&y.kind) {
                    Ordering::Equal => x.name.cmp(&y.name),
                    o => o,
                }
            }
            (&Node::Dom(_), _) => Ordering::Less,
            (&Node::Access(_), _) => Ordering::Greater,
        }
    }
}

#[derive(Clone, Eq)]
pub struct Edge<A> where A: Access {
    source: Node<A>,
    target: Node<A>,
}

/// An edge take into account the target kind: domain, access/read, access/write.
impl<A> PartialEq for Edge<A> where A: Access {
    fn eq(&self, other: &Self) -> bool {
        self.source == other.source &&
            match (&self.target, &other.target) {
                (&Node::Access(ref x), &Node::Access(ref y)) => x == y,
                (s, o) => s == o,
            }
    }
}

impl<A> PartialOrd for Edge<A> where A: Access {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        match self.source.partial_cmp(&other.source) {
            None | Some(Ordering::Equal) => {
                match (&self.target, &other.target) {
                    (&Node::Access(ref x), &Node::Access(ref y)) => x.partial_cmp(y),
                    (s, o) => s.partial_cmp(o),
                }
            }
            o => o,
        }
    }
}

impl<A> Ord for Edge<A> where A: Access {
    fn cmp(&self, other: &Self) -> Ordering {
        match self.source.cmp(&other.source) {
            Ordering::Equal => {
                match (&self.target, &other.target) {
                    (&Node::Access(ref x), &Node::Access(ref y)) => x.cmp(y),
                    (s, o) => s.cmp(o),
                }
            }
            o => o,
        }
    }
}

fn hash<T: Hash>(t: &T) -> u64 {
    let mut s = SipHasher::new();
    t.hash(&mut s);
    s.finish()
}

impl<'a, A> dot::Labeller<'a, Node<A>, Edge<A>> for ResPool<A> where A: Access {
    fn graph_id(&'a self) -> dot::Id<'a> {
        // Regex "[a-zA-Z_][a-zA-Z_0-9]*"
        dot::Id::new("G_stemflow").unwrap()
    }

    fn node_id(&'a self, node: &Node<A>) -> dot::Id<'a> {
        let id = match *node {
            Node::Access(ref a) => format!("A_{}", hash(&a.path)),
            Node::Dom(ref d) => format!("D_{}", hash(d)),
        };
        // Regex "[a-zA-Z_][a-zA-Z_0-9]*"
        dot::Id::new(id).unwrap()
    }

    fn node_label(&'a self, node: &Node<A>) -> dot::LabelText<'a> {
        let name = match *node {
            Node::Access(ref a) => format!("{}", a.path.display()),
            Node::Dom(ref d) => format!("{}", d).replace("∩", "&cap;"),
        };
        dot::LabelText::LabelStr(name.into_cow())
    }

    fn edge_label(&'a self, edge: &Edge<A>) -> dot::LabelText<'a> {
        let name: Cow<_> = match edge.target {
            Node::Access(ref a) => format!("{}", a.action).into(),
            Node::Dom(..) => "transition".into(),
        };
        dot::LabelText::LabelStr(name)
    }
}

impl<'a, A> dot::GraphWalk<'a, Node<A>, Edge<A>> for ResPool<A> where A: Access {
    fn nodes(&self) -> dot::Nodes<'a, Node<A>> {
        let mut nodes: Vec<_> = self.domains.iter().map(|x| Node::Dom(x.clone()))
            .chain(self.ressources.keys().map(|x| Node::Access(x.clone())))
            .collect();
        nodes.sort();
        nodes.dedup();
        nodes.into_cow()
    }

    fn edges(&'a self) -> dot::Edges<'a, Edge<A>> {
        let mut edges: Vec<_> = self.domains.iter().cloned().flat_map(|x| {
            x.underlays.iter().cloned()
                .map(|y| Edge { source: Node::Dom(x.clone()), target: Node::Dom(y) })
                .chain(x.acl.iter().cloned()
                       .map(|y| Edge { source: Node::Dom(x.clone()), target: Node::Access(y) }))
                .collect::<Vec<_>>().into_iter()
        }).collect();
        edges.sort();
        edges.dedup();
        edges.into_cow()
    }

    fn source(&self, edge: &Edge<A>) -> Node<A> {
        edge.source.clone()
    }

    fn target(&self, edge: &Edge<A>) -> Node<A> {
        edge.target.clone()
    }
}