openmoat-core 0.4.0

Policy model and decision engine for OpenMoat. Pure: no I/O, no OS calls.
Documentation
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
//! From tokens to simple commands to atomic actions.

mod piped;

use super::tables::{
    ENV_BUILTINS, INLINE_INTERPRETERS, MAKES, PACKAGE_RUNNERS, SHELLS, SOURCE_BUILTINS,
    WRAPPER_OPTIONS_WITH_VALUE, WRAPPER_SUBCOMMANDS, WRAPPERS, WRAPPERS_WITH_VALUE,
    WRITE_ALL_PATHS,
};
use super::tokens::{assignment_name, basename, env_refs, flag_payload, strip_at, word_env_refs};
use super::{ClassifyError, MAX_DEPTH, ShellContext, Sink};
use super::{copy, cwd, database, decoders, git, invocation, make, operands, options, text};
use crate::action::AtomicAction;
use crate::host;
use crate::lexer::{self, Operator, Token, Word};
use crate::paths;
use piped::Piped;

/// A simple command, or the opening or closing parenthesis of a subshell.
#[derive(Debug)]
enum Item {
    Command(SimpleCommand),
    Open,
    Close,
}

/// One simple command: words (assignments + argv), its redirection targets and
/// its stdin data.
#[derive(Debug, Default)]
struct SimpleCommand {
    words: Vec<Word>,
    reads: Vec<String>,
    writes: Vec<String>,
    /// `<<<` here-strings and unquoted here-document bodies: expanded by the shell.
    stdin: Vec<Word>,
    /// Here-document bodies with a quoted delimiter: taken literally.
    literal_stdin: Vec<String>,
    /// Data the previous command of a pipe passes on (`piped.rs`).
    piped: Vec<Piped>,
}

impl SimpleCommand {
    fn is_empty(&self) -> bool {
        self.words.is_empty()
            && self.reads.is_empty()
            && self.writes.is_empty()
            && self.stdin.is_empty()
            && self.literal_stdin.is_empty()
    }

    fn stdin_texts(&self) -> impl Iterator<Item = &str> {
        let expanded = self.stdin.iter().map(|w| w.text.as_str());
        expanded.chain(self.literal_stdin.iter().map(String::as_str))
    }
}

pub(super) fn classify_into(
    command: &str,
    ctx: &ShellContext<'_>,
    sink: &mut Sink,
    depth: u8,
) -> Result<(), ClassifyError> {
    if depth > MAX_DEPTH {
        return Err(ClassifyError::TooDeep);
    }
    let (tokens, too_many) = lexer::expand_braces(lexer::lex(command)?);
    if let Some(e) = too_many {
        sink.skip(e.into());
    }
    sink.note_globs(&tokens);
    let mut dirs = ctx.cwd.to_vec();
    let mut saved = Vec::new();
    for item in group_commands(&tokens) {
        match item {
            Item::Open => saved.push(dirs.clone()),
            // The shell refuses a `)` it did not open; so does the classifier.
            Item::Close => match saved.pop() {
                Some(outer) => dirs = outer,
                None => sink.skip(ClassifyError::UnbalancedParens),
            },
            Item::Command(cmd) => {
                let here = ShellContext { cwd: &dirs, ..*ctx };
                if let Err(e) = classify_simple(&cmd, &here, sink, depth) {
                    sink.skip(e);
                }
                let moved = cwd::targets(&cmd.words, &here);
                cwd::merge(&mut dirs, moved)?;
            }
        }
    }
    if !saved.is_empty() {
        sink.skip(ClassifyError::UnbalancedParens);
    }
    push_pipelines(&tokens, sink)?;
    decoders::push(&tokens, sink)
}

/// Group tokens into simple commands. The commands inside subshell
/// parentheses are governed exactly like top-level ones; the parentheses are
/// kept only to scope `cd`.
fn group_commands(tokens: &[Token]) -> Vec<Item> {
    let mut commands = Vec::new();
    let mut current = SimpleCommand::default();
    let mut pending_redirect: Option<Operator> = None;

    for token in tokens {
        match token {
            Token::Operator(op) if op.is_separator() || op.is_grouping() => {
                pending_redirect = None;
                if !current.is_empty() {
                    let piped = piped::data(&current).filter(|_| *op == Operator::Pipe);
                    commands.push(Item::Command(std::mem::take(&mut current)));
                    current.piped.extend(piped);
                }
                match op {
                    Operator::OpenParen => commands.push(Item::Open),
                    Operator::CloseParen => commands.push(Item::Close),
                    _ => {}
                }
            }
            Token::Operator(op) => pending_redirect = Some(*op),
            Token::HereDoc {
                body,
                literal: true,
            } => current.literal_stdin.push(body.text.clone()),
            Token::HereDoc { body, .. } => current.stdin.push(body.clone()),
            Token::Word(w) => match pending_redirect.take() {
                Some(Operator::RedirectIn) => current.reads.push(w.text.clone()),
                Some(Operator::HereString) => current.stdin.push(w.clone()),
                Some(Operator::RedirectOut | Operator::RedirectAppend) => {
                    current.writes.push(w.text.clone());
                }
                Some(Operator::RedirectReadWrite) => {
                    current.reads.push(w.text.clone());
                    current.writes.push(w.text.clone());
                }
                Some(Operator::DuplicateDescriptor) => {
                    // `>&2` / `<&-` duplicate or close a descriptor; anything else is a file.
                    if !w.text.chars().all(|c| c.is_ascii_digit() || c == '-') {
                        current.writes.push(w.text.clone());
                    }
                }
                None | Some(_) => current.words.push(w.clone()),
            },
        }
    }
    if !current.is_empty() {
        commands.push(Item::Command(current));
    }
    commands
}

fn classify_simple(
    cmd: &SimpleCommand,
    ctx: &ShellContext<'_>,
    sink: &mut Sink,
    depth: u8,
) -> Result<(), ClassifyError> {
    for w in cmd.words.iter().chain(&cmd.stdin) {
        for inner in &w.substitutions {
            classify_into(inner, ctx, sink, depth + 1)?;
        }
    }
    for w in &cmd.stdin {
        for name in word_env_refs(w) {
            sink.push(AtomicAction::EnvRead { name })?;
        }
    }
    for r in &cmd.reads {
        sink.read(ctx, r)?;
    }
    for w in &cmd.writes {
        sink.write(ctx, w)?;
    }

    let mut idx = 0;
    while let Some(word) = cmd.words.get(idx) {
        let Some(name) = assignment_name(&word.text) else {
            break;
        };
        sink.push(AtomicAction::EnvSet {
            name: name.to_owned(),
        })?;
        for name in word_env_refs(word) {
            sink.push(AtomicAction::EnvRead { name })?;
        }
        idx += 1;
    }
    let words = &cmd.words[idx..];
    if words.is_empty() {
        return Ok(());
    }
    let argv: Vec<String> = words.iter().map(|w| w.text.clone()).collect();
    let program = basename(&argv[0]);

    if program == "git" {
        git::push_shell(&argv, ctx, sink)?;
    } else {
        sink.push(AtomicAction::Shell { argv: argv.clone() })?;
    }

    if ENV_BUILTINS.contains(&program) {
        for name in argv[1..].iter().filter_map(|w| assignment_name(w)) {
            sink.push(AtomicAction::EnvSet {
                name: name.to_owned(),
            })?;
        }
    }
    if program == "printenv" {
        for name in argv[1..].iter().filter(|a| !a.starts_with('-')) {
            sink.push(AtomicAction::EnvRead { name: name.clone() })?;
        }
    }
    if SOURCE_BUILTINS.contains(&program)
        && let Some(file) = argv.get(1)
    {
        sink.read(ctx, file)?;
    }
    let mut text_operands = text::classify(&argv, program, ctx, sink)?;
    let stdin = cmd
        .stdin_texts()
        .chain(cmd.piped.iter().filter_map(Piped::text));
    text_operands
        .data
        .extend(database::classify(&argv, program, stdin, ctx, sink)?);
    classify_arguments(&argv, program, words, &text_operands, ctx, sink)?;

    if MAKES.contains(&program) {
        return make::classify(&argv, ctx, sink, depth);
    }

    if program == "eval" && argv.len() > 1 {
        return classify_into(&argv[1..].join(" "), ctx, sink, depth + 1);
    }
    if SHELLS.contains(&program) {
        let run = invocation::parse(program, &argv)?;
        // `bash <<< 'cmd'`, `bash <<EOF` and `cat <<EOF | bash` run their stdin
        // as the program.
        let piped = cmd.piped.iter().filter_map(Piped::text);
        let stdin_code = cmd.stdin_texts().chain(piped).filter(|_| run.reads_stdin);
        for code in run.code.into_iter().chain(stdin_code) {
            classify_into(code, ctx, sink, depth + 1)?;
        }
        if let Some(script) = run.script {
            sink.read(ctx, script)?;
        }
        return match cmd.piped.iter().find_map(Piped::unknown) {
            Some(program) if run.reads_stdin => Err(ClassifyError::UnknownStdin(program.into())),
            _ => Ok(()),
        };
    }
    if PACKAGE_RUNNERS.contains(&program)
        && let Some(payload) = flag_payload(&argv, &["-c", "--call"])
    {
        return classify_into(payload, ctx, sink, depth + 1);
    }
    if let Some(inner) = wrapped_command(&argv, program) {
        return classify_wrapped(inner, ctx, sink, depth);
    }
    if let Some((_, flags)) = INLINE_INTERPRETERS
        .iter()
        .find(|(name, _)| *name == program)
        && let Some(payload) = flag_payload(&argv, flags)
    {
        scan_payload(payload, ctx, sink)?;
    }
    if INLINE_INTERPRETERS.iter().any(|(name, _)| *name == program) {
        for text in cmd.stdin_texts() {
            scan_payload(text, ctx, sink)?;
        }
    }
    options::classify(&argv, program, ctx, sink, depth)
}

/// Classify a wrapper's inner argv (`sudo rm …` → `rm …`) as its own command.
pub(super) fn classify_wrapped(
    inner: &[String],
    ctx: &ShellContext<'_>,
    sink: &mut Sink,
    depth: u8,
) -> Result<(), ClassifyError> {
    if depth >= MAX_DEPTH {
        return Err(ClassifyError::TooDeep);
    }
    // The wrapping command already recorded the variables these words expand,
    // with their quoting; the inner words carry none, so they are not rescanned.
    let cmd = SimpleCommand {
        words: inner
            .iter()
            .map(|t| Word {
                text: t.clone(),
                literal: std::iter::once(0..t.len()).collect(),
                ..Word::default()
            })
            .collect(),
        ..SimpleCommand::default()
    };
    classify_simple(&cmd, ctx, sink, depth + 1)
}

/// For `sudo -u x cmd …`, `env A=1 cmd …`, `xargs -0 cmd …`, `timeout 5 cmd …`
/// return the wrapped argv.
fn wrapped_command<'a>(argv: &'a [String], program: &str) -> Option<&'a [String]> {
    if let Some((_, subcommands)) = WRAPPER_SUBCOMMANDS
        .iter()
        .find(|(name, _)| *name == program)
    {
        if !subcommands.contains(&argv.get(1)?.as_str()) {
            return None;
        }
        let mut i = 2;
        while let Some(arg) = argv.get(i) {
            if arg == "--" {
                i += 1;
                break;
            }
            if !arg.starts_with('-') {
                break;
            }
            i += 1;
        }
        return argv.get(i..).filter(|rest| !rest.is_empty());
    }
    if !WRAPPERS.contains(&program) {
        return None;
    }
    let options_with_value = WRAPPER_OPTIONS_WITH_VALUE
        .iter()
        .find(|(name, _)| *name == program)
        .map_or(&[][..], |(_, opts)| *opts);
    let mut i = 1;
    let mut value_pending = WRAPPERS_WITH_VALUE.contains(&program);
    while let Some(arg) = argv.get(i) {
        if arg == "--" {
            i += 1;
            break;
        }
        if options_with_value.contains(&arg.as_str()) {
            i += 2;
            continue;
        }
        let is_env_assignment = program == "env" && assignment_name(arg).is_some();
        if arg.starts_with('-') || is_env_assignment {
            i += 1;
            continue;
        }
        if value_pending {
            value_pending = false;
            i += 1;
            continue;
        }
        break;
    }
    argv.get(i..).filter(|rest| !rest.is_empty())
}

fn classify_arguments(
    argv: &[String],
    program: &str,
    words: &[Word],
    text_operands: &text::Operands,
    ctx: &ShellContext<'_>,
    sink: &mut Sink,
) -> Result<(), ClassifyError> {
    // A copy or a move writes its destination, or the entries it creates in it
    // (`copy.rs`); a remote destination (`host:/dir`) is not a local write.
    let copy = copy::destination(argv, program).filter(|d| {
        paths::looks_like_path(d.word) || operands::file_operand(d.word, program, true).is_some()
    });
    let mut sources = Vec::new();
    let options_end = argv.iter().position(|a| a == "--");

    for (i, (tok, word)) in argv.iter().zip(words).enumerate() {
        for name in word_env_refs(word) {
            sink.push(AtomicAction::EnvRead { name })?;
        }
        if program == "env"
            && i > 0
            && let Some(name) = assignment_name(tok)
        {
            sink.push(AtomicAction::EnvSet {
                name: name.to_owned(),
            })?;
        }
        let is_destination = copy.as_ref().is_some_and(|d| d.index == i);
        if i == 0 || text_operands.data.contains(&i) || is_destination {
            continue;
        }
        let options_ended = options_end.is_some_and(|end| i > end);
        if options_ended || !tok.starts_with('-') {
            sources.push(tok.as_str());
        }
        let candidate = strip_at(tok);
        let file = if paths::looks_like_path(candidate) {
            Some(candidate)
        } else if program == "dd"
            && let Some((key, value)) = tok.split_once('=')
        {
            match key {
                "if" => sink.read(ctx, value)?,
                "of" => sink.write(ctx, value)?,
                _ => {}
            }
            continue;
        } else if (!word.quoted || tok.contains("://"))
            && let Some(host) = host::of_word(tok)
        {
            // Quoted prose ("see example.com") is not a host unless it is a full URL.
            sink.push(AtomicAction::Net { host })?;
            continue;
        } else {
            operands::file_operand(tok, program, options_ended)
        };
        if let Some(file) = file {
            if WRITE_ALL_PATHS.contains(&program) || text_operands.writes.contains(&i) {
                sink.write(ctx, file)?;
            } else {
                sink.read(ctx, file)?;
            }
        }
    }
    if let Some(dest) = &copy {
        for path in copy::written(dest, &sources) {
            sink.write(ctx, &path)?;
        }
    }
    Ok(())
}

/// Mine inline interpreter code for the resources it names. The payload is
/// not shell, so it is scanned rather than parsed.
fn scan_payload(
    payload: &str,
    ctx: &ShellContext<'_>,
    sink: &mut Sink,
) -> Result<(), ClassifyError> {
    let is_separator =
        |c: char| c.is_whitespace() || matches!(c, '(' | ')' | ',' | ';' | '\'' | '"' | '`');
    for raw in payload.split(is_separator).filter(|s| !s.is_empty()) {
        if paths::looks_like_path(raw) {
            sink.read(ctx, raw)?;
        } else if let Some(host) = host::of_word(raw) {
            sink.push(AtomicAction::Net { host })?;
        }
        for name in env_refs(raw) {
            sink.push(AtomicAction::EnvRead { name })?;
        }
    }
    Ok(())
}

/// Emit a `Pipeline` atom for every suffix of a pipeline/list with ≥ 2 commands
/// so prefix rules such as `curl * | sh` match at any boundary.
fn push_pipelines(tokens: &[Token], sink: &mut Sink) -> Result<(), ClassifyError> {
    let mut flat: Vec<String> = Vec::new();
    let mut starts: Vec<usize> = Vec::new();
    let mut at_command_start = true;
    // Separators pushed since the last word. Counted from the tokens, not by
    // comparing text: an escaped word `\&` has the same text as the operator.
    let mut trailing_separators = 0;
    for token in tokens {
        match token {
            Token::Word(w) => {
                if at_command_start {
                    starts.push(flat.len());
                    at_command_start = false;
                }
                flat.push(w.text.clone());
                trailing_separators = 0;
            }
            Token::Operator(op) if op.is_separator() => {
                if !at_command_start {
                    flat.push(op.symbol().to_owned());
                    trailing_separators += 1;
                }
                at_command_start = true;
            }
            Token::Operator(op) if op.is_redirect() => {
                flat.push(op.symbol().to_owned());
                trailing_separators = 0;
            }
            Token::Operator(_) | Token::HereDoc { .. } => {}
        }
    }
    flat.truncate(flat.len() - trailing_separators);
    if starts.len() < 2 {
        return Ok(());
    }
    for &start in &starts[..starts.len() - 1] {
        sink.push(AtomicAction::Pipeline {
            argv: flat[start..].to_vec(),
        })?;
    }
    Ok(())
}