kcode-k1-web-podman 0.1.3

Fresh rootless Podman execution leaf for one K1 Web check.
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
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
use kcode_k1_web_checker_protocol::{
    Outcome, Report, Request, SCHEMA_VERSION, SelectionInput, WebIdInput, decode_report,
    encode_report, encode_request,
};
use nix::sys::signal::{Signal, killpg};
use nix::unistd::Pid;
use sha2::{Digest, Sha256};
use std::error::Error;
use std::ffi::OsString;
use std::fmt;
use std::fs;
use std::io::{self, Write};
use std::os::unix::ffi::OsStrExt;
use std::os::unix::fs::PermissionsExt;
use std::os::unix::process::CommandExt;
use std::path::{Component, Path, PathBuf};
use std::process::{Command, ExitStatus, Stdio};
use std::sync::mpsc::{self, RecvTimeoutError};
use std::thread;
use std::time::{Duration, Instant};

const CANDIDATE: &str = "/k1/input/candidate";
const ADMITTED: &str = "/k1/input/admitted";
const PROJECTION: &str = "/k1/input/public";
const CHECKER: &str = "/k1/bin/web-checker";
const DATA_OPTIONS: &str = "ro,nosuid,nodev,noexec";
const CHECKER_OPTIONS: &str = "ro,nosuid,nodev";

#[derive(Clone)]
pub struct WebPodmanConfig {
    pub podman: PathBuf,
    pub image: String,
    pub checker: PathBuf,
    pub chromium: PathBuf,
    pub chromium_version: String,
    pub cpu_millis: u32,
    pub memory_bytes: u64,
    pub pids_limit: u32,
    pub tmpfs_bytes: u64,
    pub shm_bytes: u64,
    pub checker_timeout: Duration,
    pub wall_timeout: Duration,
}

pub struct CheckInput {
    pub candidate: WebIdInput,
    pub candidate_root: PathBuf,
    pub projection_root: PathBuf,
    pub entry: String,
    pub tests: String,
    pub selections: Vec<SelectionInput>,
}

#[derive(Debug)]
pub struct CommandDiagnostics {
    pub status: ExitStatus,
    pub stdout: Vec<u8>,
    pub stderr: Vec<u8>,
}

pub struct CheckOutput {
    pub diagnostics: CommandDiagnostics,
    pub report: Report,
}

#[derive(Debug)]
pub enum WebPodmanError {
    InvalidInput {
        field: &'static str,
        reason: String,
    },
    Spawn(io::Error),
    Process(String),
    Timeout {
        diagnostics: CommandDiagnostics,
        kill_failure: Option<String>,
    },
    Infrastructure {
        diagnostics: CommandDiagnostics,
        report: Option<Box<Report>>,
        reason: String,
    },
}

impl fmt::Display for WebPodmanError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{self:?}")
    }
}

impl Error for WebPodmanError {}

pub struct WebPodman {
    config: WebPodmanConfig,
    checker_digest: String,
    image_digest: String,
    policy_identity: String,
    frozen_policy_identity: String,
    args: Vec<OsString>,
}

impl WebPodman {
    pub fn new(mut config: WebPodmanConfig) -> Result<Self, WebPodmanError> {
        config.podman = host_path(&config.podman, "podman", false, true)?;
        config.checker = host_path(&config.checker, "checker", false, true)?;
        container_path(&config.chromium, "chromium")?;
        if config.chromium_version.is_empty()
            || config.chromium_version.trim() != config.chromium_version
            || config.chromium_version.chars().any(char::is_control)
        {
            return Err(invalid(
                "chromium_version",
                "must be nonempty trimmed text without controls",
            ));
        }
        let image_digest = image_identity(&config.image)?;
        let timeout_ms = milliseconds(config.checker_timeout, "checker_timeout")?;
        let wall_ms = milliseconds(config.wall_timeout, "wall_timeout")?;
        if wall_ms < timeout_ms {
            return Err(invalid("wall_timeout", "must not precede checker_timeout"));
        }
        let checker_digest = file_digest(&config.checker, "checker")?;
        let args = run_args(&config);
        let policy_identity = make_policy_identity(
            b"k1-web-podman-policy-v1",
            &args,
            &[
                CANDIDATE,
                DATA_OPTIONS,
                PROJECTION,
                DATA_OPTIONS,
                CHECKER,
                CHECKER_OPTIONS,
            ],
            &config.chromium,
            timeout_ms,
            wall_ms,
        );
        let frozen_policy_identity = make_policy_identity(
            b"k1-web-podman-frozen-policy-v1",
            &args,
            &[
                "candidate",
                CANDIDATE,
                DATA_OPTIONS,
                "admitted",
                ADMITTED,
                DATA_OPTIONS,
                "public",
                PROJECTION,
                DATA_OPTIONS,
                "checker",
                CHECKER,
                CHECKER_OPTIONS,
                "request_projection",
                ADMITTED,
            ],
            &config.chromium,
            timeout_ms,
            wall_ms,
        );
        Ok(Self {
            config,
            checker_digest,
            image_digest,
            policy_identity,
            frozen_policy_identity,
            args,
        })
    }

    pub fn checker_digest(&self) -> &str {
        &self.checker_digest
    }
    pub fn image_identity(&self) -> &str {
        &self.config.image
    }
    pub fn image_digest(&self) -> &str {
        &self.image_digest
    }
    pub fn chromium_version(&self) -> &str {
        &self.config.chromium_version
    }
    pub fn command_policy_identity(&self) -> &str {
        &self.policy_identity
    }
    pub fn frozen_command_policy_identity(&self) -> &str {
        &self.frozen_policy_identity
    }

    pub fn check(&self, input: CheckInput) -> Result<CheckOutput, WebPodmanError> {
        self.check_with_public(input, None)
    }

    pub fn check_frozen(
        &self,
        input: CheckInput,
        public_projection_root: impl AsRef<Path>,
    ) -> Result<CheckOutput, WebPodmanError> {
        self.check_with_public(input, Some(public_projection_root.as_ref()))
    }

    fn check_with_public(
        &self,
        input: CheckInput,
        public_projection_root: Option<&Path>,
    ) -> Result<CheckOutput, WebPodmanError> {
        let candidate = host_path(&input.candidate_root, "candidate_root", true, false)?;
        let projection = host_path(&input.projection_root, "projection_root", true, false)?;
        let public = public_projection_root
            .map(|path| host_path(path, "public_projection_root", true, false))
            .transpose()?;
        if public.is_none()
            && (overlaps(&candidate, &projection)
                || self.config.checker.starts_with(&candidate)
                || self.config.checker.starts_with(&projection))
        {
            return Err(invalid("paths", "mounted host paths overlap"));
        }
        let checker = host_path(&self.config.checker, "checker", false, true)?;
        if file_digest(&checker, "checker")? != self.checker_digest {
            return Err(invalid("checker", "bytes changed after construction"));
        }
        let request_projection = if let Some(public) = &public {
            let paths = [
                candidate.as_path(),
                projection.as_path(),
                public.as_path(),
                checker.as_path(),
            ];
            let overlapping = paths
                .iter()
                .enumerate()
                .any(|(index, left)| paths[index + 1..].iter().any(|right| overlaps(left, right)));
            if overlapping {
                return Err(invalid("paths", "mounted host paths overlap"));
            }
            ADMITTED
        } else {
            PROJECTION
        };
        let request = Request {
            schema: SCHEMA_VERSION,
            candidate: input.candidate,
            candidate_root: PathBuf::from(CANDIDATE),
            projection_root: PathBuf::from(request_projection),
            entry: input.entry,
            tests: input.tests,
            selections: input.selections,
            chromium: self.config.chromium.clone(),
            timeout_ms: self.config.checker_timeout.as_millis() as u64,
        };
        let bytes = encode_request(&request)
            .map_err(|source| invalid("request", format!("cannot encode: {source}")))?;
        let mut command = Command::new(&self.config.podman);
        command.args(&self.args);
        volume(&mut command, &candidate, CANDIDATE, DATA_OPTIONS);
        if let Some(public) = &public {
            volume(&mut command, &projection, ADMITTED, DATA_OPTIONS);
            volume(&mut command, public, PROJECTION, DATA_OPTIONS);
        } else {
            volume(&mut command, &projection, PROJECTION, DATA_OPTIONS);
        }
        volume(&mut command, &checker, CHECKER, CHECKER_OPTIONS);
        command.arg("--").arg(&self.config.image).arg(CHECKER);
        let diagnostics = self.execute(command, bytes)?;
        let report = match decode_report(&diagnostics.stdout) {
            Ok(report) => report,
            Err(source) => {
                return Err(infrastructure(
                    diagnostics,
                    None,
                    format!("invalid report: {source}"),
                ));
            }
        };
        if report.schema != SCHEMA_VERSION {
            return Err(infrastructure(
                diagnostics,
                Some(report),
                "report schema mismatch",
            ));
        }
        let canonical = match encode_report(&report) {
            Ok(canonical) => canonical,
            Err(source) => {
                return Err(infrastructure(
                    diagnostics,
                    None,
                    format!("cannot re-encode report: {source}"),
                ));
            }
        };
        if canonical != diagnostics.stdout {
            return Err(infrastructure(
                diagnostics,
                Some(report),
                "report is not canonical",
            ));
        }
        let matching = matches!(
            (diagnostics.status.code(), &report.outcome),
            (Some(0), Outcome::Success) | (Some(1), Outcome::Failure { .. })
        );
        if !matching {
            return Err(infrastructure(
                diagnostics,
                Some(report),
                "status and outcome mismatch",
            ));
        }
        Ok(CheckOutput {
            diagnostics,
            report,
        })
    }

    fn execute(
        &self,
        mut command: Command,
        bytes: Vec<u8>,
    ) -> Result<CommandDiagnostics, WebPodmanError> {
        command
            .process_group(0)
            .stdin(Stdio::piped())
            .stdout(Stdio::piped())
            .stderr(Stdio::piped());
        let started = Instant::now();
        let mut child = command.spawn().map_err(WebPodmanError::Spawn)?;
        let pid = Pid::from_raw(child.id() as i32);
        let mut stdin = child.stdin.take().expect("piped stdin");
        let writer = thread::spawn(move || stdin.write_all(&bytes));
        let (sender, receiver) = mpsc::sync_channel(1);
        let waiter = thread::spawn(move || sender.send(child.wait_with_output()));
        let remaining = self.config.wall_timeout.saturating_sub(started.elapsed());
        let (result, timed_out, kill_failure) = match receiver.recv_timeout(remaining) {
            Ok(result) => (result, false, None),
            Err(RecvTimeoutError::Timeout) => {
                let failure = killpg(pid, Signal::SIGKILL)
                    .err()
                    .map(|source| source.to_string());
                let result = receiver
                    .recv()
                    .map_err(|source| WebPodmanError::Process(source.to_string()))?;
                (result, true, failure)
            }
            Err(source) => return Err(WebPodmanError::Process(source.to_string())),
        };
        waiter
            .join()
            .map_err(|_| WebPodmanError::Process("wait thread panicked".to_owned()))?
            .map_err(|source| WebPodmanError::Process(source.to_string()))?;
        let write_failure = match writer.join() {
            Ok(Ok(())) => None,
            Ok(Err(source)) => Some(format!("stdin: {source}")),
            Err(_) => Some("stdin thread panicked".to_owned()),
        };
        let output = result.map_err(|source| WebPodmanError::Process(source.to_string()))?;
        let diagnostics = CommandDiagnostics {
            status: output.status,
            stdout: output.stdout,
            stderr: output.stderr,
        };
        if timed_out {
            return Err(WebPodmanError::Timeout {
                diagnostics,
                kill_failure,
            });
        }
        if let Some(reason) = write_failure {
            return Err(infrastructure(diagnostics, None, reason));
        }
        Ok(diagnostics)
    }
}

fn run_args(config: &WebPodmanConfig) -> Vec<OsString> {
    let mut args = [
        "--remote=false",
        "run",
        "--rm",
        "--pull=never",
        "--network=none",
        "--read-only",
        "--userns=keep-id",
        "--cap-drop=ALL",
        "--security-opt=no-new-privileges",
        "--http-proxy=false",
        "--ipc=private",
        "--workdir=/tmp",
        "--env=HOME=/tmp/home",
        "--env=TMPDIR=/tmp",
    ]
    .into_iter()
    .map(OsString::from)
    .collect::<Vec<_>>();
    if config.cpu_millis != 0 {
        args.push(
            format!(
                "--cpus={}.{:03}",
                config.cpu_millis / 1000,
                config.cpu_millis % 1000
            )
            .into(),
        );
    }
    if config.memory_bytes != 0 {
        args.push(format!("--memory={}", config.memory_bytes).into());
        args.push(format!("--memory-swap={}", config.memory_bytes).into());
    }
    if config.pids_limit != 0 {
        args.push(format!("--pids-limit={}", config.pids_limit).into());
    }
    let tmpfs = if config.tmpfs_bytes == 0 {
        "--tmpfs=/tmp:rw,nosuid,nodev,noexec".to_owned()
    } else {
        format!(
            "--tmpfs=/tmp:rw,nosuid,nodev,noexec,size={}",
            config.tmpfs_bytes
        )
    };
    args.push(tmpfs.into());
    if config.shm_bytes != 0 {
        args.push(format!("--shm-size={}", config.shm_bytes).into());
    }
    args
}

fn host_path(
    path: &Path,
    field: &'static str,
    directory: bool,
    executable: bool,
) -> Result<PathBuf, WebPodmanError> {
    if !path.is_absolute() || path.as_os_str().as_bytes().contains(&b':') {
        return Err(invalid(field, "must be an absolute colon-free path"));
    }
    let metadata = fs::symlink_metadata(path)
        .map_err(|source| invalid(field, format!("metadata failed: {source}")))?;
    let ordinary = if directory {
        metadata.is_dir()
    } else {
        metadata.is_file()
    };
    if metadata.file_type().is_symlink() || !ordinary {
        return Err(invalid(
            field,
            "must be an ordinary nonsymlink path of the required kind",
        ));
    }
    if executable && metadata.permissions().mode() & 0o111 == 0 {
        return Err(invalid(field, "must be executable"));
    }
    let canonical = fs::canonicalize(path)
        .map_err(|source| invalid(field, format!("canonicalization failed: {source}")))?;
    if canonical != path {
        return Err(invalid(
            field,
            "must be canonical and contain no symlink component",
        ));
    }
    Ok(canonical)
}

fn container_path(path: &Path, field: &'static str) -> Result<(), WebPodmanError> {
    let clean = path.is_absolute()
        && path.to_str().is_some()
        && path
            .components()
            .filter(|part| matches!(part, Component::Normal(_)))
            .count()
            > 0
        && path
            .components()
            .all(|part| matches!(part, Component::RootDir | Component::Normal(_)));
    if clean {
        Ok(())
    } else {
        Err(invalid(field, "must be a normalized absolute UTF-8 path"))
    }
}

fn image_identity(value: &str) -> Result<String, WebPodmanError> {
    if value.is_empty()
        || value.trim() != value
        || value.bytes().any(|byte| !byte.is_ascii_graphic())
    {
        return Err(invalid(
            "image",
            "must be a nonempty trimmed ordinary image reference",
        ));
    }
    if let Some((name, digest)) = value.rsplit_once("@sha256:") {
        let canonical = !name.is_empty()
            && !name.contains('@')
            && digest.len() == 64
            && digest
                .bytes()
                .all(|byte| byte.is_ascii_digit() || (b'a'..=b'f').contains(&byte));
        if canonical {
            return Ok(format!("sha256:{digest}"));
        }
    }
    let mut hash = Sha256::new();
    add(&mut hash, b"k1-web-podman-image-reference-v1");
    add(&mut hash, value.as_bytes());
    Ok(finish(hash))
}

fn milliseconds(value: Duration, field: &'static str) -> Result<u64, WebPodmanError> {
    let millis = value.as_millis();
    if value.is_zero()
        || !value.subsec_nanos().is_multiple_of(1_000_000)
        || millis > u64::MAX as u128
    {
        Err(invalid(
            field,
            "must be a positive whole number of milliseconds",
        ))
    } else {
        Ok(millis as u64)
    }
}

fn file_digest(path: &Path, field: &'static str) -> Result<String, WebPodmanError> {
    let bytes =
        fs::read(path).map_err(|source| invalid(field, format!("read failed: {source}")))?;
    let mut hash = Sha256::new();
    hash.update(bytes);
    Ok(finish(hash))
}

fn overlaps(left: &Path, right: &Path) -> bool {
    left.starts_with(right) || right.starts_with(left)
}

fn volume(command: &mut Command, host: &Path, target: &str, options: &str) {
    let mut value = host.as_os_str().to_os_string();
    value.push(format!(":{target}:{options}"));
    command.arg("--volume").arg(value);
}

fn make_policy_identity(
    tag: &[u8],
    args: &[OsString],
    bindings: &[&str],
    chromium: &Path,
    checker_ms: u64,
    wall_ms: u64,
) -> String {
    let mut hash = Sha256::new();
    add(&mut hash, tag);
    for arg in args {
        add(&mut hash, arg.as_bytes());
    }
    for value in bindings {
        add(&mut hash, value.as_bytes());
    }
    add(&mut hash, chromium.as_os_str().as_bytes());
    add(&mut hash, &checker_ms.to_be_bytes());
    add(&mut hash, &wall_ms.to_be_bytes());
    finish(hash)
}

fn add(hash: &mut Sha256, value: &[u8]) {
    hash.update((value.len() as u64).to_be_bytes());
    hash.update(value);
}

fn finish(hash: Sha256) -> String {
    let mut value = String::from("sha256:");
    for byte in hash.finalize() {
        value.push_str(&format!("{byte:02x}"));
    }
    value
}

fn invalid(field: &'static str, reason: impl Into<String>) -> WebPodmanError {
    WebPodmanError::InvalidInput {
        field,
        reason: reason.into(),
    }
}

fn infrastructure(
    diagnostics: CommandDiagnostics,
    report: Option<Report>,
    reason: impl Into<String>,
) -> WebPodmanError {
    WebPodmanError::Infrastructure {
        diagnostics,
        report: report.map(Box::new),
        reason: reason.into(),
    }
}

#[cfg(test)]
mod tests;