tapid-runner 0.0.6

Policy-aware package execution foundations for Tapid
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
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
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
#![cfg(all(windows, test))]
use super::*;
use windows_sys::Win32::System::JobObjects::{
    JOB_OBJECT_LIMIT_ACTIVE_PROCESS, JOB_OBJECT_LIMIT_JOB_MEMORY,
    JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
};

#[test]
fn assigning_an_invalid_process_handle_fails_closed() {
    let job = WindowsJob::new(
        &ExecutionLimits::new(None, None, None, None).unwrap(),
        false,
    )
    .unwrap();
    assert!(job.assign_suspended_process(0).is_err());
}

#[test]
#[ignore = "one-off Windows 11 ACL diagnosis; not a support acceptance test"]
fn appcontainer_actual_token_file_access_probe() {
    use super::super::{FilesystemBindingMode, FilesystemGrantSource, ResolvedFilesystemGrant};
    use std::io::Read;
    use std::os::windows::ffi::OsStrExt;
    use windows_sys::Win32::Foundation::{
        CloseHandle, GENERIC_WRITE, GetLastError, INVALID_HANDLE_VALUE,
    };
    use windows_sys::Win32::Security::{
        DuplicateTokenEx, ImpersonateLoggedOnUser, RevertToSelf, SecurityImpersonation,
        TOKEN_DUPLICATE, TOKEN_IMPERSONATE, TOKEN_QUERY, TokenImpersonation,
    };
    use windows_sys::Win32::Storage::FileSystem::{
        CREATE_ALWAYS, CREATE_NEW, CreateFileW, FILE_APPEND_DATA, FILE_ATTRIBUTE_NORMAL,
        FILE_EXECUTE, FILE_FLAG_BACKUP_SEMANTICS, FILE_LIST_DIRECTORY, FILE_READ_ATTRIBUTES,
        FILE_SHARE_DELETE, FILE_SHARE_READ, FILE_SHARE_WRITE, FILE_WRITE_DATA, OPEN_ALWAYS,
        OPEN_EXISTING, WriteFile,
    };
    use windows_sys::Win32::System::Threading::OpenProcessToken;

    struct RevertImpersonation;
    impl Drop for RevertImpersonation {
        fn drop(&mut self) {
            unsafe { RevertToSelf() };
        }
    }

    fn path_wide(path: &std::path::Path) -> Vec<u16> {
        path.as_os_str().encode_wide().chain(Some(0)).collect()
    }

    fn probe_file(
        path: &std::path::Path,
        access: u32,
        disposition: u32,
        label: &str,
    ) -> (bool, bool) {
        let wide = path_wide(path);
        let handle = unsafe {
            CreateFileW(
                wide.as_ptr(),
                access,
                FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                std::ptr::null(),
                disposition,
                FILE_ATTRIBUTE_NORMAL,
                0,
            )
        };
        if handle == INVALID_HANDLE_VALUE {
            eprintln!("{label}: OPEN_DENIED win32={}", unsafe { GetLastError() });
            return (false, false);
        }
        if disposition == CREATE_NEW {
            eprintln!("{label}: CREATE_OPEN_OK");
        }
        let byte = *b"X";
        let mut written = 0;
        let result =
            unsafe { WriteFile(handle, byte.as_ptr(), 1, &mut written, std::ptr::null_mut()) };
        let write_succeeded = result != 0;
        if !write_succeeded {
            eprintln!("{label}: WRITE_DENIED win32={}", unsafe { GetLastError() });
        } else {
            eprintln!("{label}: WRITE_OK bytes={written}");
        }
        unsafe { CloseHandle(handle) };
        (true, write_succeeded)
    }

    fn probe_directory(path: &std::path::Path, label: &str) -> bool {
        let wide = path_wide(path);
        let mut execute_ok = false;
        for (access, access_label) in [
            (FILE_EXECUTE, "FILE_TRAVERSE"),
            (FILE_READ_ATTRIBUTES, "FILE_READ_ATTRIBUTES"),
            (FILE_LIST_DIRECTORY, "FILE_LIST_DIRECTORY"),
            (
                FILE_EXECUTE | FILE_READ_ATTRIBUTES,
                "FILE_TRAVERSE|FILE_READ_ATTRIBUTES",
            ),
        ] {
            let handle = unsafe {
                CreateFileW(
                    wide.as_ptr(),
                    access,
                    FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
                    std::ptr::null(),
                    OPEN_EXISTING,
                    FILE_FLAG_BACKUP_SEMANTICS,
                    0,
                )
            };
            let succeeded = handle != INVALID_HANDLE_VALUE;
            if succeeded {
                unsafe { CloseHandle(handle) };
            }
            let error = if succeeded {
                0
            } else {
                unsafe { GetLastError() }
            };
            eprintln!(
                "{label}: {access_label} {}{}",
                if succeeded { "OK" } else { "DENIED win32=" },
                if succeeded {
                    String::new()
                } else {
                    error.to_string()
                }
            );
            if access == FILE_EXECUTE {
                execute_ok = succeeded;
            }
        }
        execute_ok
    }

    fn query_acl(icacls: &std::path::Path, path: &std::path::Path) -> Vec<u8> {
        let output = std::process::Command::new(icacls)
            .arg(path)
            .output()
            .unwrap();
        assert!(
            output.status.success(),
            "icacls failed for {}: {}{}",
            path.display(),
            String::from_utf8_lossy(&output.stdout),
            String::from_utf8_lossy(&output.stderr)
        );
        output.stdout
    }

    let root = std::env::temp_dir().join(format!(
        "tapid-actual-token-acl-probe-{}-{}",
        std::process::id(),
        std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    let writable = root.join("writable");
    std::fs::create_dir_all(&writable).unwrap();
    let existing = writable.join("existing.txt");
    let created = writable.join("created.txt");
    std::fs::write(&existing, b"before").unwrap();

    let system_root = std::env::var_os("SystemRoot").expect("Windows SystemRoot is required");
    let icacls = std::path::PathBuf::from(&system_root).join("System32/icacls.exe");
    let label = std::process::Command::new(&icacls)
        .arg(&writable)
        .args(["/setintegritylevel", "(OI)(CI)L", "/c"])
        .output()
        .unwrap();
    assert!(
        label.status.success(),
        "icacls failed: {}",
        String::from_utf8_lossy(&label.stdout)
    );

    let command_target = existing.clone();
    let root = std::fs::canonicalize(&root).unwrap();
    let writable = std::fs::canonicalize(&writable).unwrap();
    let existing = std::fs::canonicalize(&existing).unwrap();
    let system_root_canonical =
        std::fs::canonicalize(std::path::PathBuf::from(&system_root)).unwrap();
    let volume_root = system_root_canonical.parent().unwrap().to_path_buf();
    let mut audit_paths = vec![
        root.clone(),
        writable.clone(),
        existing.clone(),
        root.parent().unwrap().to_path_buf(),
        system_root_canonical.clone(),
        volume_root.clone(),
    ];
    audit_paths.sort();
    audit_paths.dedup();
    let baseline_acls = audit_paths
        .iter()
        .map(|path| query_acl(&icacls, path))
        .collect::<Vec<_>>();

    let mut container = WindowsAppContainer::create().unwrap();
    let payload = format!("echo TAPID_FROM_CMD>\"{}\"", command_target.display());
    let mut pipes = WindowsStdioPipes::new().unwrap();
    let limits =
        ExecutionLimits::new(Some(10), Some(4096), Some(4), Some(128 * 1024 * 1024)).unwrap();
    let (job, mut child) = create_appcontainer_child_inner_with_cwd(
        &container,
        &payload,
        &root,
        &limits,
        true,
        true,
        Some(&mut pipes),
    );
    let (mut stdout_pipe, mut stderr_pipe) = pipes.into_parent_readers().unwrap();
    let system32 = std::fs::canonicalize(
        std::path::PathBuf::from(std::env::var_os("SystemRoot").unwrap()).join("System32"),
    )
    .unwrap();
    let resolved_grants = vec![
        ResolvedFilesystemGrant {
            path: root.clone(),
            access: FilesystemAccess::Read,
            kind: FilesystemGrantKind::DirectorySubtree,
            source: FilesystemGrantSource::ProjectPolicy,
            binding: FilesystemBindingMode::CanonicalPath,
        },
        ResolvedFilesystemGrant {
            path: system32,
            access: FilesystemAccess::Read,
            kind: FilesystemGrantKind::DirectorySubtree,
            source: FilesystemGrantSource::BackendRuntime,
            binding: FilesystemBindingMode::CanonicalPath,
        },
        ResolvedFilesystemGrant {
            path: writable.clone(),
            access: FilesystemAccess::Write,
            kind: FilesystemGrantKind::DirectorySubtree,
            source: FilesystemGrantSource::ProjectPolicy,
            binding: FilesystemBindingMode::CanonicalPath,
        },
    ];
    let mut grants = WindowsFilesystemGrants::apply(&container, &resolved_grants).unwrap();
    let active_acls = audit_paths
        .iter()
        .map(|path| query_acl(&icacls, path))
        .collect::<Vec<_>>();
    for ((path, before), active) in audit_paths.iter().zip(&baseline_acls).zip(&active_acls) {
        eprintln!(
            "active DACL path={} changed={}\n{}",
            path.display(),
            before != active,
            String::from_utf8_lossy(active)
        );
    }
    let system_temp = root.parent().unwrap().to_path_buf();
    let system_temp_index = audit_paths
        .iter()
        .position(|path| path == &system_temp)
        .unwrap();
    assert_eq!(
        active_acls[system_temp_index], baseline_acls[system_temp_index],
        "project grant must not modify shared SystemTemp"
    );
    eprintln!("SystemTemp DACL unchanged; testing declared descendant access");

    let dacl = std::process::Command::new(&icacls)
        .arg(&existing)
        .output()
        .unwrap();
    eprintln!(
        "actual grant target DACL: {}",
        String::from_utf8_lossy(&dacl.stdout)
    );

    let mut primary_token = 0;
    assert_ne!(
        unsafe {
            OpenProcessToken(
                child.process_handle(),
                TOKEN_QUERY | TOKEN_DUPLICATE | TOKEN_IMPERSONATE,
                &mut primary_token,
            )
        },
        0,
        "OpenProcessToken win32={}",
        unsafe { GetLastError() }
    );
    let mut impersonation_token = 0;
    assert_ne!(
        unsafe {
            DuplicateTokenEx(
                primary_token,
                TOKEN_QUERY | TOKEN_IMPERSONATE,
                std::ptr::null(),
                SecurityImpersonation,
                TokenImpersonation,
                &mut impersonation_token,
            )
        },
        0,
        "DuplicateTokenEx win32={}",
        unsafe { GetLastError() }
    );
    assert_ne!(
        unsafe { ImpersonateLoggedOnUser(impersonation_token) },
        0,
        "ImpersonateLoggedOnUser win32={}",
        unsafe { GetLastError() }
    );
    let revert = RevertImpersonation;

    assert!(
        probe_directory(&root, "project root"),
        "AppContainer cannot traverse project root"
    );
    assert!(
        probe_directory(&writable, "write directory"),
        "AppContainer cannot traverse write directory"
    );
    for (path, label) in [
        (root.parent().unwrap(), "SystemTemp ancestor"),
        (system_root_canonical.as_path(), "Windows ancestor"),
        (volume_root.as_path(), "volume-root ancestor"),
    ] {
        eprintln!(
            "ancestor access label={label} path={} traverse={}",
            path.display(),
            probe_directory(path, label)
        );
    }
    for (path, access, disposition, label) in [
        (
            &existing,
            FILE_WRITE_DATA,
            OPEN_EXISTING,
            "existing FILE_WRITE_DATA",
        ),
        (
            &existing,
            FILE_APPEND_DATA,
            OPEN_EXISTING,
            "existing FILE_APPEND_DATA",
        ),
        (
            &existing,
            GENERIC_WRITE,
            OPEN_EXISTING,
            "existing GENERIC_WRITE",
        ),
        (
            &existing,
            GENERIC_WRITE,
            CREATE_ALWAYS,
            "existing CREATE_ALWAYS + GENERIC_WRITE",
        ),
        (
            &existing,
            GENERIC_WRITE,
            OPEN_ALWAYS,
            "existing OPEN_ALWAYS + GENERIC_WRITE",
        ),
        (
            &created,
            FILE_WRITE_DATA,
            CREATE_NEW,
            "new CREATE_NEW + FILE_WRITE_DATA",
        ),
        (
            &created,
            FILE_WRITE_DATA,
            OPEN_EXISTING,
            "new reopen FILE_WRITE_DATA",
        ),
        (
            &created,
            FILE_APPEND_DATA,
            OPEN_EXISTING,
            "new reopen FILE_APPEND_DATA",
        ),
    ] {
        assert_eq!(
            probe_file(path, access, disposition, label),
            (true, true),
            "{label} failed"
        );
    }
    drop(revert);
    unsafe {
        CloseHandle(impersonation_token);
        CloseHandle(primary_token);
    }

    let termination = child.resume_and_wait_for_exit(&job, 5_000).unwrap();
    let mut stdout = Vec::new();
    let mut stderr = Vec::new();
    stdout_pipe.read_to_end(&mut stdout).unwrap();
    stderr_pipe.read_to_end(&mut stderr).unwrap();
    eprintln!(
        "actual cmd child termination={termination:?}; stdout={:?}; stderr={:?}; file={:?}",
        String::from_utf8_lossy(&stdout),
        String::from_utf8_lossy(&stderr),
        std::fs::read_to_string(&existing).unwrap_or_else(|error| format!("READ_ERROR: {error}"))
    );
    assert_eq!(
        termination,
        0,
        "cmd.exe write failed: {}",
        String::from_utf8_lossy(&stderr)
    );
    assert_eq!(
        std::fs::read_to_string(&existing).unwrap().trim(),
        "TAPID_FROM_CMD"
    );
    drop(child);
    drop(job);
    grants.restore().unwrap();
    let restored_acls = audit_paths
        .iter()
        .map(|path| query_acl(&icacls, path))
        .collect::<Vec<_>>();
    for ((path, before), restored) in audit_paths.iter().zip(&baseline_acls).zip(&restored_acls) {
        assert_eq!(
            before,
            restored,
            "DACL was not restored for {}",
            path.display()
        );
    }
    container.cleanup().unwrap();
    std::fs::remove_dir_all(root).unwrap();
}

#[test]
fn appcontainer_child_is_verified_and_assigned_before_resume() {
    let (mut container, job, mut child) = create_appcontainer_child("exit 0");
    assert_eq!(child.resume_and_wait_for_exit(&job, 5_000).unwrap(), 0);
    drop(child);
    drop(job);
    container.cleanup().unwrap();
}

#[test]
fn appcontainer_child_captures_stdout_and_stderr_separately() {
    use std::io::Read;

    let (mut container, job, mut child, pipes) =
        create_appcontainer_child_with_stdio("echo TAPID_OUT & echo TAPID_ERR 1>&2");
    let (mut stdout_pipe, mut stderr_pipe) = pipes.into_parent_readers().unwrap();
    assert_eq!(child.resume_and_wait_for_exit(&job, 5_000).unwrap(), 0);
    let mut stdout = Vec::new();
    let mut stderr = Vec::new();
    stdout_pipe.read_to_end(&mut stdout).unwrap();
    stderr_pipe.read_to_end(&mut stderr).unwrap();
    drop(child);
    drop(job);
    container.cleanup().unwrap();
    assert_eq!(String::from_utf8_lossy(&stdout).trim(), "TAPID_OUT");
    assert_eq!(String::from_utf8_lossy(&stderr).trim(), "TAPID_ERR");
}

#[test]
fn appcontainer_output_limit_terminates_and_bounds_combined_capture() {
    let (mut container, job, mut child, pipes) = create_appcontainer_child_with_stdio(
        "for /L %i in (1,1,1000000) do @echo TAPID_OUTPUT_CHUNK",
    );
    let capture = WindowsOutputCapture::start(pipes.into_parent_readers().unwrap(), Some(128));
    let termination = child
        .resume_and_wait_for_status(&job, 5_000, capture.output_limit_exceeded(), None)
        .unwrap();
    let (stdout, stderr) = capture.finish().unwrap();
    drop(child);
    drop(job);
    container.cleanup().unwrap();
    assert_eq!(termination, WindowsChildTermination::OutputLimitExceeded);
    assert!(stdout.len() + stderr.len() <= 128);
}

#[test]
fn appcontainer_timeout_terminates_and_reaps_the_job() {
    let (mut container, job, mut child) =
        create_appcontainer_child("for /L %i in (1,1,10000000) do @set /a x=%i >nul");
    let error = child.resume_and_wait_for_exit(&job, 100).unwrap_err();
    assert_eq!(error.category(), ExecutionErrorCategory::Timeout);
    assert!(child.wait_for_signal(5_000).unwrap());
    drop(child);
    drop(job);
    container.cleanup().unwrap();
}

#[test]
fn explicit_job_termination_signals_a_suspended_assigned_child() {
    let (mut container, job, child) = create_appcontainer_child("exit 0");
    job.terminate_all().unwrap();
    assert!(child.wait_for_signal(5_000).unwrap());
    drop(child);
    drop(job);
    container.cleanup().unwrap();
}

#[test]
fn job_completion_port_reports_when_the_last_member_exits() {
    let (mut container, job, child) = create_appcontainer_child("exit 0");
    job.terminate_all().unwrap();
    assert!(child.wait_for_signal(5_000).unwrap());
    job.wait_for_active_process_zero_notification(5_000)
        .unwrap();
    drop(child);
    drop(job);
    container.cleanup().unwrap();
}

#[test]
fn restoring_appcontainer_grant_preserves_concurrent_dacl_changes() {
    let root = std::env::temp_dir().join(format!(
        "tapid-appcontainer-concurrent-dacl-{}-{}",
        std::process::id(),
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    std::fs::create_dir(&root).unwrap();
    let mut container = WindowsAppContainer::create().unwrap();
    let mut grant = WindowsPathAcl::grant(
        &root,
        container.sid(),
        FilesystemAccess::Write,
        FilesystemGrantKind::DirectorySubtree,
    )
    .unwrap();

    let system_root = std::env::var_os("SystemRoot").expect("Windows SystemRoot is required");
    let icacls =
        std::fs::canonicalize(std::path::PathBuf::from(system_root).join("System32/icacls.exe"))
            .unwrap();
    // BUILTIN\\Remote Desktop Users is a valid, distinct trustee that is not
    // inherited by this temporary directory. The numeric form avoids localization.
    let concurrent_sid = "*S-1-5-32-555";
    let concurrent_ace = format!("{concurrent_sid}:(OI)(CI)(R)");
    let change = std::process::Command::new(&icacls)
        .arg(&root)
        .arg("/grant")
        .arg(&concurrent_ace)
        .output()
        .unwrap();
    assert!(
        change.status.success(),
        "icacls concurrent DACL update failed: {}{}",
        String::from_utf8_lossy(&change.stdout),
        String::from_utf8_lossy(&change.stderr)
    );

    grant.restore().unwrap();
    let saved_acl = std::env::temp_dir().join(format!(
        "tapid-appcontainer-concurrent-dacl-{}.acl",
        std::process::id()
    ));
    let saved = std::process::Command::new(&icacls)
        .arg(&root)
        .arg("/save")
        .arg(&saved_acl)
        .arg("/c")
        .output()
        .unwrap();
    assert!(saved.status.success());
    let bytes = std::fs::read(&saved_acl).unwrap();
    let utf16 = bytes
        .as_chunks::<2>()
        .0
        .iter()
        .map(|pair| u16::from_le_bytes(*pair))
        .collect::<Vec<_>>();
    let acl = String::from_utf16_lossy(&utf16);
    assert!(
        acl.contains(";;;RD)"),
        "restoring Tapid's ACE discarded the concurrent ACE: {acl}"
    );
    std::fs::remove_file(saved_acl).unwrap();
    container.cleanup().unwrap();
    std::fs::remove_dir_all(root).unwrap();
}

#[test]
fn appcontainer_child_can_write_a_granted_tree_and_loses_access_on_restore() {
    let root = std::env::temp_dir().join(format!(
        "tapid-appcontainer-grant-{}-{}",
        std::process::id(),
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    std::fs::create_dir(&root).unwrap();
    let marker = root.join("allowed.txt");
    let mut container = WindowsAppContainer::create().unwrap();
    let mut grant = WindowsPathAcl::grant(
        &root,
        container.sid(),
        FilesystemAccess::Write,
        FilesystemGrantKind::DirectorySubtree,
    )
    .unwrap();

    let payload = format!("echo authorized>\"{}\"", marker.display());
    let (job, mut child) = create_appcontainer_child_in(&container, &payload);
    assert_eq!(child.resume_and_wait_for_exit(&job, 5_000).unwrap(), 0);
    drop(child);
    drop(job);
    assert_eq!(
        std::fs::read_to_string(&marker).unwrap().trim(),
        "authorized"
    );

    let payload = format!("type \"{}\" >nul", marker.display());
    let (job, mut child) = create_appcontainer_child_in(&container, &payload);
    assert_ne!(
        child.resume_and_wait_for_exit(&job, 5_000).unwrap(),
        0,
        "write-only grants must not permit reading the granted subtree"
    );
    drop(child);
    drop(job);

    grant.restore().unwrap();
    let payload = format!("echo denied>\"{}\"", marker.display());
    let (job, mut child) = create_appcontainer_child_in(&container, &payload);
    assert_ne!(child.resume_and_wait_for_exit(&job, 5_000).unwrap(), 0);
    drop(child);
    drop(job);
    container.cleanup().unwrap();
    assert_eq!(
        std::fs::read_to_string(&marker).unwrap().trim(),
        "authorized"
    );
    std::fs::remove_dir_all(root).unwrap();
}

#[test]
fn appcontainer_policy_only_allows_declared_write_subtree() {
    use super::super::{FilesystemBindingMode, FilesystemGrantSource, ResolvedFilesystemGrant};

    let root = std::env::temp_dir().join(format!(
        "tapid-appcontainer-policy-split-{}-{}",
        std::process::id(),
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_nanos()
    ));
    let writable = root.join("writable");
    std::fs::create_dir_all(&writable).unwrap();
    let root = std::fs::canonicalize(&root).unwrap();
    let writable = std::fs::canonicalize(&writable).unwrap();
    let readonly_file = root.join("read-only.txt");
    let allowed_file = writable.join("allowed.txt");
    std::fs::write(&readonly_file, b"read-only baseline").unwrap();
    let mut container = WindowsAppContainer::create().unwrap();
    let resolved_grants = [
        ResolvedFilesystemGrant {
            path: root.clone(),
            access: FilesystemAccess::Read,
            kind: FilesystemGrantKind::DirectorySubtree,
            source: FilesystemGrantSource::ProjectPolicy,
            binding: FilesystemBindingMode::CanonicalPath,
        },
        ResolvedFilesystemGrant {
            path: writable,
            access: FilesystemAccess::Write,
            kind: FilesystemGrantKind::DirectorySubtree,
            source: FilesystemGrantSource::ProjectPolicy,
            binding: FilesystemBindingMode::CanonicalPath,
        },
    ];
    let mut grants = WindowsFilesystemGrants::apply(&container, &resolved_grants).unwrap();

    let payload = format!("echo authorized>\"{}\"", allowed_file.display());
    let (job, mut child) = create_appcontainer_child_in(&container, &payload);
    let allowed_exit = child.resume_and_wait_for_exit(&job, 5_000).unwrap();
    drop(child);
    drop(job);

    let payload = format!("echo unauthorized>\"{}\"", readonly_file.display());
    let (job, mut child) = create_appcontainer_child_in(&container, &payload);
    let denied_exit = child.resume_and_wait_for_exit(&job, 5_000).unwrap();
    drop(child);
    drop(job);
    let allowed_contents = std::fs::read_to_string(&allowed_file).unwrap();
    let readonly_contents = std::fs::read(&readonly_file).unwrap();

    grants.restore().unwrap();
    container.cleanup().unwrap();
    std::fs::remove_dir_all(root).unwrap();

    assert_eq!(allowed_exit, 0, "declared write subtree must be writable");
    assert_eq!(allowed_contents.trim(), "authorized");
    assert_eq!(
        readonly_contents, b"read-only baseline",
        "write to read-only project sibling changed the file (cmd exit {denied_exit})"
    );
}

#[test]
fn appcontainer_child_cannot_write_an_ungranted_temp_file() {
    let marker =
        std::env::temp_dir().join(format!("tapid-appcontainer-{}.txt", std::process::id()));
    std::fs::write(&marker, b"parent-owned").unwrap();
    let payload = format!("echo child>\"{}\" & exit 42", marker.display());
    let (mut container, job, mut child) = create_appcontainer_child(&payload);
    let exit_code = child.resume_and_wait_for_exit(&job, 5_000).unwrap();
    let contents = std::fs::read(&marker).unwrap();
    drop(child);
    drop(job);
    container.cleanup().unwrap();
    std::fs::remove_file(marker).unwrap();
    assert_ne!(
        exit_code, 0,
        "redirection to the parent-owned marker must be denied"
    );
    assert_eq!(contents, b"parent-owned");
}

fn create_appcontainer_child(
    payload: &str,
) -> (WindowsAppContainer, WindowsJob, WindowsSuspendedChild) {
    let container = WindowsAppContainer::create().unwrap();
    let (job, child) = create_appcontainer_child_in(&container, payload);
    (container, job, child)
}

fn create_appcontainer_child_with_stdio(
    payload: &str,
) -> (
    WindowsAppContainer,
    WindowsJob,
    WindowsSuspendedChild,
    WindowsStdioPipes,
) {
    let container = WindowsAppContainer::create().unwrap();
    let mut pipes = WindowsStdioPipes::new().unwrap();
    let (job, child) = create_appcontainer_child_inner(&container, payload, Some(&mut pipes));
    (container, job, child, pipes)
}

fn create_appcontainer_child_in(
    container: &WindowsAppContainer,
    payload: &str,
) -> (WindowsJob, WindowsSuspendedChild) {
    create_appcontainer_child_inner(container, payload, None)
}

fn create_appcontainer_child_inner(
    container: &WindowsAppContainer,
    payload: &str,
    stdio: Option<&mut WindowsStdioPipes>,
) -> (WindowsJob, WindowsSuspendedChild) {
    let current_directory = std::path::PathBuf::from(std::env::var_os("SystemRoot").unwrap());
    let limits = ExecutionLimits::new(None, None, None, None).unwrap();
    create_appcontainer_child_inner_with_cwd(
        container,
        payload,
        &current_directory,
        &limits,
        false,
        false,
        stdio,
    )
}

fn create_appcontainer_child_inner_with_cwd(
    container: &WindowsAppContainer,
    payload: &str,
    current_directory: &std::path::Path,
    limits: &ExecutionLimits,
    restrict_subprocesses: bool,
    canonicalize_program: bool,
    stdio: Option<&mut WindowsStdioPipes>,
) -> (WindowsJob, WindowsSuspendedChild) {
    use std::os::windows::ffi::OsStrExt;

    let system_root = std::env::var_os("SystemRoot").unwrap();
    let system_directory = std::path::PathBuf::from(&system_root);
    let program_path = system_directory.join("System32").join("cmd.exe");
    let program_path = if canonicalize_program {
        std::fs::canonicalize(program_path).unwrap()
    } else {
        program_path
    };
    let current_directory = current_directory.to_path_buf();
    let program = program_path.as_os_str().encode_wide().collect::<Vec<_>>();
    let arguments = ["/D", "/S", "/C", payload]
        .into_iter()
        .map(|argument| argument.encode_utf16().collect::<Vec<_>>())
        .collect::<Vec<_>>();
    let command_line =
        super::super::serialize_windows_command_line_units(&program, &arguments, true).unwrap();
    let environment =
        super::super::windows_environment_block_units(&std::collections::BTreeMap::from([(
            std::ffi::OsString::from("SystemRoot"),
            system_root.clone(),
        )]))
        .unwrap();
    let mut application = program;
    application.push(0);
    let mut working_directory = current_directory
        .as_os_str()
        .encode_wide()
        .collect::<Vec<_>>();
    working_directory.push(0);

    let job = WindowsJob::new(limits, restrict_subprocesses).unwrap();
    let child = match stdio {
        Some(pipes) => WindowsSuspendedChild::create_with_stdio(
            container,
            &application,
            &command_line,
            &environment,
            &working_directory,
            pipes,
        ),
        None => WindowsSuspendedChild::create(
            container,
            &application,
            &command_line,
            &environment,
            &working_directory,
        ),
    }
    .unwrap();
    job.assign_suspended_process(child.process_handle())
        .unwrap();
    (job, child)
}

#[test]
fn disabled_subprocess_policy_caps_the_job_to_its_root_process() {
    let job = WindowsJob::new(&ExecutionLimits::default(), true).unwrap();
    let information = job.query_extended_limits().unwrap();
    assert_eq!(information.BasicLimitInformation.ActiveProcessLimit, 1);
    assert_eq!(
        information.BasicLimitInformation.LimitFlags & JOB_OBJECT_LIMIT_ACTIVE_PROCESS,
        JOB_OBJECT_LIMIT_ACTIVE_PROCESS
    );
}

#[test]
fn job_limits_are_job_wide_and_kill_members_when_owner_closes() {
    const PROCESS_LIMIT: u32 = 4;
    const MEMORY_LIMIT: u64 = 64 * 1024 * 1024;
    let limits = ExecutionLimits::new(None, None, Some(PROCESS_LIMIT), Some(MEMORY_LIMIT)).unwrap();
    let job = WindowsJob::new(&limits, false).unwrap();
    let information = job.query_extended_limits().unwrap();

    assert_eq!(
        information.BasicLimitInformation.ActiveProcessLimit,
        PROCESS_LIMIT
    );
    assert_eq!(
        information.BasicLimitInformation.LimitFlags & JOB_OBJECT_LIMIT_ACTIVE_PROCESS,
        JOB_OBJECT_LIMIT_ACTIVE_PROCESS
    );
    assert_eq!(
        information.BasicLimitInformation.LimitFlags & JOB_OBJECT_LIMIT_JOB_MEMORY,
        JOB_OBJECT_LIMIT_JOB_MEMORY
    );
    assert_eq!(
        information.BasicLimitInformation.LimitFlags & JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE,
        JOB_OBJECT_LIMIT_KILL_ON_JOB_CLOSE
    );
    assert_eq!(information.JobMemoryLimit, MEMORY_LIMIT as usize);
    assert_eq!(information.ProcessMemoryLimit, 0);
}