linprov 0.2.8

eBPF mark-of-the-web for Linux: tag network-touched files and enforce who can exec them.
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
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
//! Allowlist: each line is one rule whose `<dim>=<value>;<dim>=<value>`
//! conditions AND together. Multiple lines OR.
//!
//! ```text
//! # uid 1000 downloading with curl is fine, anywhere
//! creator_uid=1000;creator_comm=curl
//!
//! # uid 1000 may execute firefox-dropped binaries from ~/.local/bin
//! execution_uid=1000;creator_comm=firefox;target_folder=/home/user/.local/bin
//! ```

use std::{
    collections::HashSet,
    fs::{File, OpenOptions},
    io::{BufRead, BufReader, Write},
    path::Path,
    sync::Mutex,
};

use anyhow::{anyhow, Context, Result};
use clap::ValueEnum;
use linprov_common::{dim, fnv_hash, AllowRule, COMM_LEN, MAX_RULES};
use serde::Deserialize;

/// Allowlist dimensions. Used for `--soak=<csv>`, for the
/// `<dim>=<value>` entries in the allowlist file, and for the
/// `soak = [...]` array in the TOML config file.
#[derive(Clone, Copy, Debug, Deserialize, ValueEnum, PartialEq, Eq, Hash)]
#[clap(rename_all = "snake_case")]
#[serde(rename_all = "snake_case")]
pub enum Dim {
    /// Exact full path of the executed binary (at execve time).
    TargetFilename,
    /// Any `/`-terminated ancestor of the executed binary's path.
    TargetFolder,
    /// Basename (final component) of the file where it was first written.
    LandingFilename,
    /// The landing file's immediate parent folder, or any ancestor of it
    /// (nested, up to `MAX_FOLDER_ANCESTORS` levels).
    LandingFolder,
    /// Full exe path of the writer (`/proc/$pid/exe`).
    CreatorProcess,
    /// 16-byte `comm` of the writer.
    CreatorComm,
    /// UID of the writer.
    CreatorUid,
    /// UID running the `execve`.
    ExecutionUid,
}

impl Dim {
    pub fn as_key(self) -> &'static str {
        match self {
            Dim::TargetFilename => "target_filename",
            Dim::TargetFolder => "target_folder",
            Dim::LandingFilename => "landing_filename",
            Dim::LandingFolder => "landing_folder",
            Dim::CreatorProcess => "creator_process",
            Dim::CreatorComm => "creator_comm",
            Dim::CreatorUid => "creator_uid",
            Dim::ExecutionUid => "execution_uid",
        }
    }

    fn parse(s: &str) -> Option<Self> {
        Some(match s {
            "target_filename" => Dim::TargetFilename,
            "target_folder" => Dim::TargetFolder,
            "landing_filename" => Dim::LandingFilename,
            "landing_folder" => Dim::LandingFolder,
            "creator_process" => Dim::CreatorProcess,
            "creator_comm" => Dim::CreatorComm,
            "creator_uid" => Dim::CreatorUid,
            "execution_uid" => Dim::ExecutionUid,
            _ => return None,
        })
    }
}

/// Parsed rule, ready to be packed into [`AllowRule`] for the BPF map.
/// Field naming mirrors the dim names.
#[derive(Debug, Default, Clone)]
pub struct RuleSpec {
    pub flags: u32,
    pub target_filename: Option<String>,
    pub target_folder: Option<String>,
    pub landing_filename: Option<String>,
    pub landing_folder: Option<String>,
    pub creator_process: Option<String>,
    pub creator_comm: Option<String>,
    pub creator_uid: Option<u32>,
    pub execution_uid: Option<u32>,
}

impl RuleSpec {
    /// Parse one allowlist line. Expects `dim=val[;dim=val]*` after
    /// stripping comments / whitespace. Empty rules (zero dims) are an
    /// error at the caller; this just parses.
    pub fn parse(line: &str) -> Result<Self> {
        let mut spec = Self::default();
        for cond in line.split(';') {
            let cond = cond.trim();
            if cond.is_empty() {
                continue;
            }
            let (k, v) = cond
                .split_once('=')
                .ok_or_else(|| anyhow!("condition `{cond}` is missing `=`"))?;
            let k = k.trim();
            let v = v.trim();
            let dim = Dim::parse(k).ok_or_else(|| anyhow!("unknown dimension `{k}`"))?;
            spec.set(dim, v)
                .with_context(|| format!("condition `{k}={v}`"))?;
        }
        if spec.flags == 0 {
            return Err(anyhow!("rule has no conditions"));
        }
        Ok(spec)
    }

    pub fn set(&mut self, d: Dim, value: &str) -> Result<()> {
        let bit = match d {
            Dim::TargetFilename => dim::TARGET_FILENAME,
            Dim::TargetFolder => dim::TARGET_FOLDER,
            Dim::LandingFilename => dim::LANDING_FILENAME,
            Dim::LandingFolder => dim::LANDING_FOLDER,
            Dim::CreatorProcess => dim::CREATOR_PROCESS,
            Dim::CreatorComm => dim::CREATOR_COMM,
            Dim::CreatorUid => dim::CREATOR_UID,
            Dim::ExecutionUid => dim::EXECUTION_UID,
        };
        if self.flags & bit != 0 {
            return Err(anyhow!(
                "dim `{}` specified twice in the same rule",
                d.as_key()
            ));
        }
        self.flags |= bit;

        // Path-shaped dims are stored as FNV hashes (see `pack`), so
        // there's no length ceiling on rule values — a 4096-byte path
        // hashes to the same 8 bytes as a short one.
        match d {
            Dim::TargetFilename => {
                self.target_filename = Some(value.to_string());
            }
            Dim::TargetFolder => {
                let (folder, recursive) = split_recursive(value);
                self.target_folder = Some(normalize_folder(folder));
                if recursive {
                    self.flags |= dim::TARGET_FOLDER_RECURSIVE;
                }
            }
            Dim::LandingFilename => {
                self.landing_filename = Some(value.to_string());
            }
            Dim::LandingFolder => {
                let (folder, recursive) = split_recursive(value);
                self.landing_folder = Some(normalize_folder(folder));
                if recursive {
                    self.flags |= dim::LANDING_FOLDER_RECURSIVE;
                }
            }
            Dim::CreatorProcess => {
                self.creator_process = Some(value.to_string());
            }
            Dim::CreatorComm => {
                if value.len() >= COMM_LEN {
                    return Err(anyhow!(
                        "creator_comm `{value}` is too long ({} bytes; max {})",
                        value.len(),
                        COMM_LEN - 1
                    ));
                }
                self.creator_comm = Some(value.to_string());
            }
            Dim::CreatorUid => {
                let uid: u32 = value
                    .parse()
                    .with_context(|| format!("creator_uid `{value}` is not a u32"))?;
                self.creator_uid = Some(uid);
            }
            Dim::ExecutionUid => {
                let uid: u32 = value
                    .parse()
                    .with_context(|| format!("execution_uid `{value}` is not a u32"))?;
                self.execution_uid = Some(uid);
            }
        }
        Ok(())
    }

    /// Render the rule back to its canonical line form (dims in the
    /// enum's declared order, joined by `;`). Used for dedup / soak.
    pub fn to_line(&self) -> String {
        let mut parts = Vec::new();
        for d in DIM_ORDER {
            let bit = dim_bit(*d);
            if self.flags & bit == 0 {
                continue;
            }
            let v = match d {
                Dim::TargetFilename => self.target_filename.clone(),
                // Re-emit the trailing `*` for recursive folder rules so
                // the canonical line round-trips (and dedups correctly).
                Dim::TargetFolder => self.target_folder.clone().map(|f| {
                    if self.flags & dim::TARGET_FOLDER_RECURSIVE != 0 {
                        format!("{f}*")
                    } else {
                        f
                    }
                }),
                Dim::LandingFilename => self.landing_filename.clone(),
                Dim::LandingFolder => self.landing_folder.clone().map(|f| {
                    if self.flags & dim::LANDING_FOLDER_RECURSIVE != 0 {
                        format!("{f}*")
                    } else {
                        f
                    }
                }),
                Dim::CreatorProcess => self.creator_process.clone(),
                Dim::CreatorComm => self.creator_comm.clone(),
                Dim::CreatorUid => self.creator_uid.map(|u| u.to_string()),
                Dim::ExecutionUid => self.execution_uid.map(|u| u.to_string()),
            };
            if let Some(v) = v {
                parts.push(format!("{}={v}", d.as_key()));
            }
        }
        parts.join(";")
    }

    /// Pack into the on-the-wire [`AllowRule`] shape. Strings are
    /// hashed; userspace and BPF agree on FNV-1a-64.
    pub fn pack(&self) -> AllowRule {
        let mut creator_comm = [0u8; COMM_LEN];
        if let Some(c) = &self.creator_comm {
            let b = c.as_bytes();
            let n = b.len().min(COMM_LEN - 1);
            creator_comm[..n].copy_from_slice(&b[..n]);
        }
        AllowRule {
            flags: self.flags,
            creator_uid: self.creator_uid.unwrap_or(0),
            execution_uid: self.execution_uid.unwrap_or(0),
            _pad: 0,
            creator_comm,
            target_filename_hash: self.target_filename.as_deref().map(fnv_hash).unwrap_or(0),
            target_folder_hash: self.target_folder.as_deref().map(fnv_hash).unwrap_or(0),
            landing_filename_hash: self.landing_filename.as_deref().map(fnv_hash).unwrap_or(0),
            landing_folder_hash: self.landing_folder.as_deref().map(fnv_hash).unwrap_or(0),
            creator_process_hash: self.creator_process.as_deref().map(fnv_hash).unwrap_or(0),
        }
    }
}

const DIM_ORDER: &[Dim] = &[
    Dim::TargetFilename,
    Dim::TargetFolder,
    Dim::LandingFilename,
    Dim::LandingFolder,
    Dim::CreatorProcess,
    Dim::CreatorComm,
    Dim::CreatorUid,
    Dim::ExecutionUid,
];

fn dim_bit(d: Dim) -> u32 {
    match d {
        Dim::TargetFilename => dim::TARGET_FILENAME,
        Dim::TargetFolder => dim::TARGET_FOLDER,
        Dim::LandingFilename => dim::LANDING_FILENAME,
        Dim::LandingFolder => dim::LANDING_FOLDER,
        Dim::CreatorProcess => dim::CREATOR_PROCESS,
        Dim::CreatorComm => dim::CREATOR_COMM,
        Dim::CreatorUid => dim::CREATOR_UID,
        Dim::ExecutionUid => dim::EXECUTION_UID,
    }
}

fn normalize_folder(value: &str) -> String {
    if value.ends_with('/') {
        value.to_string()
    } else {
        // Folder rules must end in `/` so the BPF FNV walk hits the
        // hash exactly when crossing the corresponding separator.
        format!("{value}/")
    }
}

/// Split a folder rule value into `(folder, recursive)`. A trailing `*`
/// means recursive (match the folder or any descendant); without it the
/// rule is exact (only files directly in the folder). `/opt/app/*` and
/// `/opt/app*` both yield `("/opt/app", true)` → normalized
/// `/opt/app/`; `/opt/app/` yields `("/opt/app/", false)`.
fn split_recursive(value: &str) -> (&str, bool) {
    match value.strip_suffix('*') {
        Some(rest) => (rest, true),
        None => (value, false),
    }
}

/// Full parsed allowlist: an ordered list of rules. Dedup happens at
/// load time (canonical [`RuleSpec::to_line`] form).
#[derive(Debug, Default)]
pub struct Rules {
    pub rules: Vec<RuleSpec>,
    seen_lines: HashSet<String>,
}

impl Rules {
    pub fn load(path: &Path) -> Result<Self> {
        let mut rules = Self::default();
        let f = match File::open(path) {
            Ok(f) => f,
            Err(e) if e.kind() == std::io::ErrorKind::NotFound => return Ok(rules),
            Err(e) => return Err(anyhow!("opening allowlist `{}`: {e}", path.display())),
        };
        for (i, line) in BufReader::new(f).lines().enumerate() {
            let line = line.with_context(|| format!("reading line {}", i + 1))?;
            let trimmed = line.split('#').next().unwrap_or("").trim();
            if trimmed.is_empty() {
                continue;
            }
            let spec = RuleSpec::parse(trimmed)
                .with_context(|| format!("parsing allowlist line {}: `{trimmed}`", i + 1))?;
            rules.insert(spec);
        }
        Ok(rules)
    }

    pub fn insert(&mut self, spec: RuleSpec) -> bool {
        let line = spec.to_line();
        if !self.seen_lines.insert(line) {
            return false;
        }
        self.rules.push(spec);
        true
    }

    pub fn len(&self) -> usize {
        self.rules.len()
    }

    pub fn check_capacity(&self) -> Result<()> {
        if self.rules.len() > MAX_RULES {
            return Err(anyhow!(
                "{} rules exceeds BPF map capacity ({MAX_RULES}). \
                 Trim the allowlist or bump MAX_RULES.",
                self.rules.len()
            ));
        }
        Ok(())
    }
}

/// Soak-mode state: which dims to record, the append handle, and the
/// dedup set shared with [`Rules::seen_lines`] at startup.
pub struct Soak {
    pub dims: Vec<Dim>,
    pub seen: Mutex<HashSet<String>>,
    pub writer: Mutex<File>,
}

impl Soak {
    pub fn open(path: &Path, dims: Vec<Dim>, preload: &Rules) -> Result<Self> {
        let writer = OpenOptions::new()
            .create(true)
            .append(true)
            .open(path)
            .with_context(|| format!("opening allowlist `{}` for soak append", path.display()))?;
        Ok(Self {
            dims,
            seen: Mutex::new(preload.seen_lines.clone()),
            writer: Mutex::new(writer),
        })
    }

    /// Build a rule from the soak dims using the values in `ctx`, then
    /// append it to the file if not previously seen. Returns the rule
    /// line iff actually written.
    pub fn record(&self, ctx: &OriginContext<'_>) -> Result<Option<String>> {
        let mut spec = RuleSpec::default();
        for d in &self.dims {
            let val: Option<String> = match d {
                // Target dims come from the live exec path — always
                // present, any length.
                Dim::TargetFilename => non_empty(ctx.target_filename),
                Dim::TargetFolder => folder_of(ctx.target_filename),
                // Landing + creator dims are resolved from the audit db
                // (the record only carries hashes). `None` means the db
                // couldn't resolve the hash — skip that dim rather than
                // emit a rule that can't be matched.
                Dim::LandingFilename => ctx.landing_basename.map(str::to_string),
                Dim::LandingFolder => ctx.landing_folder.map(str::to_string),
                Dim::CreatorProcess => ctx.creator_path.map(str::to_string),
                Dim::CreatorComm => non_empty(ctx.creator_comm),
                Dim::CreatorUid => Some(ctx.creator_uid.to_string()),
                Dim::ExecutionUid => Some(ctx.execution_uid.to_string()),
            };
            let Some(val) = val else { continue };
            if let Err(e) = spec.set(*d, &val) {
                log::warn!("soak: skipping invalid {} value `{val}`: {e}", d.as_key());
            }
        }
        if spec.flags == 0 {
            return Ok(None);
        }

        let line = spec.to_line();
        {
            let mut seen = self.seen.lock().expect("soak seen mutex poisoned");
            if !seen.insert(line.clone()) {
                return Ok(None);
            }
        }
        let mut w = self.writer.lock().expect("soak writer mutex poisoned");
        writeln!(w, "{line}").with_context(|| format!("appending `{line}`"))?;
        w.sync_data().ok();
        Ok(Some(line))
    }
}

fn non_empty(s: &str) -> Option<String> {
    if s.is_empty() {
        None
    } else {
        Some(s.to_string())
    }
}

/// Immediate parent directory of `path`, with a trailing `/` so it
/// hashes identically to the BPF target-folder walk and to
/// `normalize_folder`.
fn folder_of(path: &str) -> Option<String> {
    if path.is_empty() {
        return None;
    }
    match path.rsplit_once('/') {
        Some((parent, _)) if !parent.is_empty() => Some(format!("{parent}/")),
        Some((_, _)) => Some("/".to_string()),
        None => None,
    }
}

/// Values the soak emitter needs per execve event.
///
/// `target_filename` is the live exec path (always present). The
/// landing/creator fields are resolved from the audit db — `None` when
/// the db can't map the record's hash back to a path, in which case
/// soak skips that dimension rather than emit an unmatchable rule.
pub struct OriginContext<'a> {
    pub target_filename: &'a str,
    pub landing_folder: Option<&'a str>,
    pub landing_basename: Option<&'a str>,
    pub creator_path: Option<&'a str>,
    pub creator_comm: &'a str,
    pub creator_uid: u32,
    pub execution_uid: u32,
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn parse_single_dim() {
        let r = RuleSpec::parse("creator_comm=curl").unwrap();
        assert_eq!(r.flags, dim::CREATOR_COMM);
        assert_eq!(r.creator_comm.as_deref(), Some("curl"));
    }

    #[test]
    fn parse_multi_dim_anded() {
        let r = RuleSpec::parse("creator_uid=1000;creator_comm=curl").unwrap();
        assert_eq!(r.flags, dim::CREATOR_UID | dim::CREATOR_COMM);
        assert_eq!(r.creator_uid, Some(1000));
        assert_eq!(r.creator_comm.as_deref(), Some("curl"));
    }

    #[test]
    fn parse_trailing_semicolons_and_whitespace_ok() {
        let r = RuleSpec::parse("  creator_uid = 1000 ;; creator_comm = curl ; ").unwrap();
        assert_eq!(r.flags, dim::CREATOR_UID | dim::CREATOR_COMM);
    }

    #[test]
    fn parse_rejects_unknown_dim() {
        let err = RuleSpec::parse("nope=1").unwrap_err();
        let msg = format!("{err:#}");
        assert!(msg.contains("unknown dimension"), "{msg}");
    }

    #[test]
    fn parse_rejects_empty_rule() {
        let err = RuleSpec::parse(";;").unwrap_err();
        let msg = format!("{err:#}");
        assert!(msg.contains("no conditions"), "{msg}");
    }

    #[test]
    fn parse_rejects_missing_equals() {
        let err = RuleSpec::parse("creator_comm").unwrap_err();
        let msg = format!("{err:#}");
        assert!(msg.contains("missing `=`"), "{msg}");
    }

    #[test]
    fn parse_rejects_duplicate_dim() {
        let err = RuleSpec::parse("creator_uid=1;creator_uid=2").unwrap_err();
        let msg = format!("{err:#}");
        assert!(msg.contains("specified twice"), "{msg}");
    }

    #[test]
    fn parse_rejects_bad_uid() {
        let err = RuleSpec::parse("creator_uid=notanumber").unwrap_err();
        let msg = format!("{err:#}");
        assert!(msg.contains("not a u32"), "{msg}");
    }

    #[test]
    fn target_folder_normalizes_trailing_slash() {
        let r = RuleSpec::parse("target_folder=/opt/installed").unwrap();
        assert_eq!(r.target_folder.as_deref(), Some("/opt/installed/"));

        let r = RuleSpec::parse("target_folder=/opt/installed/").unwrap();
        assert_eq!(r.target_folder.as_deref(), Some("/opt/installed/"));
    }

    #[test]
    fn round_trip_canonical_line() {
        // Conditions are emitted in DIM_ORDER, which doesn't necessarily
        // match input order — round-tripping through to_line() yields
        // the canonical form.
        let r = RuleSpec::parse("creator_comm=curl;target_filename=/x").unwrap();
        let line = r.to_line();
        assert_eq!(line, "target_filename=/x;creator_comm=curl");

        // The canonical form re-parses identically.
        let r2 = RuleSpec::parse(&line).unwrap();
        assert_eq!(r2.to_line(), line);
    }

    #[test]
    fn folder_of_immediate_parent() {
        assert_eq!(
            folder_of("/opt/my-app/bin/foo").as_deref(),
            Some("/opt/my-app/bin/")
        );
        assert_eq!(folder_of("/foo").as_deref(), Some("/"));
        // Any length — no truncation, since the rule value is hashed.
        let deep = format!("/{}/x", "a".repeat(5000));
        assert_eq!(
            folder_of(&deep).as_deref(),
            Some(&*format!("/{}/", "a".repeat(5000)))
        );
    }

    #[test]
    fn long_path_rule_parses_without_length_limit() {
        // Path-shaped dims hash to 8 bytes regardless of length.
        let long = format!("/opt/{}/bin/", "seg/".repeat(2000));
        let r = RuleSpec::parse(&format!("target_folder={long}")).unwrap();
        assert_eq!(r.flags, dim::TARGET_FOLDER);
    }

    #[test]
    fn folder_recursive_suffix_parses_and_round_trips() {
        // Trailing `*` → recursive modifier; folder normalized without it.
        let r = RuleSpec::parse("target_folder=/opt/app/*").unwrap();
        assert_eq!(r.flags, dim::TARGET_FOLDER | dim::TARGET_FOLDER_RECURSIVE);
        assert_eq!(r.target_folder.as_deref(), Some("/opt/app/"));
        assert_eq!(r.to_line(), "target_folder=/opt/app/*");

        // No `*` → exact, no modifier bit.
        let r = RuleSpec::parse("target_folder=/opt/app/").unwrap();
        assert_eq!(r.flags, dim::TARGET_FOLDER);
        assert_eq!(r.to_line(), "target_folder=/opt/app/");

        // Same for landing_folder, and `*` without a trailing slash still
        // normalizes the folder.
        let r = RuleSpec::parse("landing_folder=/home/u/Downloads*").unwrap();
        assert_eq!(r.flags, dim::LANDING_FOLDER | dim::LANDING_FOLDER_RECURSIVE);
        assert_eq!(r.landing_folder.as_deref(), Some("/home/u/Downloads/"));
        assert_eq!(r.to_line(), "landing_folder=/home/u/Downloads/*");
    }

    #[test]
    fn pack_sets_only_required_hashes() {
        let r = RuleSpec::parse("creator_uid=1000").unwrap();
        let packed = r.pack();
        assert_eq!(packed.flags, dim::CREATOR_UID);
        assert_eq!(packed.creator_uid, 1000);
        // Cleared dims still have a default-valued field — the BPF side
        // ignores them because the flag bit is off.
        assert_eq!(packed.target_filename_hash, 0);
        assert_eq!(packed.creator_process_hash, 0);
    }

    #[test]
    fn pack_creator_process_hashes_match_fnv() {
        let r = RuleSpec::parse("creator_process=/usr/bin/curl").unwrap();
        let packed = r.pack();
        assert_eq!(packed.flags, dim::CREATOR_PROCESS);
        assert_eq!(packed.creator_process_hash, fnv_hash("/usr/bin/curl"));
    }

    #[test]
    fn rules_dedup_on_canonical_line() {
        let mut rules = Rules::default();
        assert!(rules.insert(RuleSpec::parse("creator_uid=1000").unwrap()));
        assert!(!rules.insert(RuleSpec::parse("creator_uid=1000").unwrap()));
        assert_eq!(rules.len(), 1);
    }

    #[test]
    fn rules_load_skips_comments_and_blanks() {
        use std::io::Write;
        let mut tmp = tempfile::NamedTempFile::new().unwrap();
        writeln!(tmp, "# top comment").unwrap();
        writeln!(tmp).unwrap();
        writeln!(tmp, "creator_comm=curl  # inline comment").unwrap();
        writeln!(tmp, "creator_uid=1000;creator_comm=curl").unwrap();
        tmp.flush().unwrap();
        let rules = Rules::load(tmp.path()).unwrap();
        assert_eq!(rules.len(), 2);
    }

    #[test]
    fn capacity_check_fires_above_max_rules() {
        let mut rules = Rules::default();
        for i in 0..(MAX_RULES + 1) {
            // Use unique creator_uid values so dedup doesn't collapse them.
            let spec = RuleSpec::parse(&format!("creator_uid={i}")).unwrap();
            rules.insert(spec);
        }
        assert!(rules.check_capacity().is_err());
    }
}