|
| 1 | +use std::cell::Cell; |
1 | 2 | use std::ffi::OsString; |
2 | 3 | use std::fs::{self, OpenOptions}; |
3 | 4 | use std::io::{self, Read, Write}; |
4 | 5 | use std::mem; |
5 | 6 | use std::os::windows::fs::OpenOptionsExt; |
6 | 7 | use std::os::windows::io::AsHandle; |
7 | 8 | use std::path::Path; |
| 9 | +use std::rc::Rc; |
8 | 10 | use std::thread; |
9 | 11 | use std::time::{Duration, Instant}; |
10 | 12 |
|
@@ -940,6 +942,81 @@ fn local_link(name: &[u16], target: &[u16]) -> OwnedHandle { |
940 | 942 | owned(link).unwrap() |
941 | 943 | } |
942 | 944 |
|
| 945 | +#[test] |
| 946 | +fn vm_spawns_refuse_untrusted_log_job_names_before_creating_a_child() { |
| 947 | + use crate::host::supervisor::{self, VmmCommand}; |
| 948 | + use crate::protocol::CAPABILITY_LEN; |
| 949 | + |
| 950 | + let directory = tempfile::tempdir().unwrap(); |
| 951 | + let log = fs::File::create(directory.path().join("vmm.log")).unwrap(); |
| 952 | + let id = log_job_id(&log).unwrap().unwrap(); |
| 953 | + let name = log_job_name(id); |
| 954 | + let system = std::env::var_os("SystemRoot").unwrap(); |
| 955 | + let command = VmmCommand::new( |
| 956 | + Path::new(&system).join("System32").join("cmd.exe"), |
| 957 | + ["/c", "exit", "0"].map(OsString::from).to_vec(), |
| 958 | + directory.path(), |
| 959 | + ); |
| 960 | + let refused = || { |
| 961 | + let entered = Rc::new(Cell::new(false)); |
| 962 | + let observed = Rc::clone(&entered); |
| 963 | + let result = with_spawn_window( |
| 964 | + move || observed.set(true), |
| 965 | + || supervisor::spawn_vm(&command, &[7; CAPABILITY_LEN], log.try_clone().unwrap()), |
| 966 | + ); |
| 967 | + let error = match result { |
| 968 | + Err(error) => error, |
| 969 | + Ok(child) => { |
| 970 | + assert!(child.kill_and_wait(), "the unexpected child did not exit"); |
| 971 | + panic!("an untrusted log-job name started a child"); |
| 972 | + } |
| 973 | + }; |
| 974 | + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); |
| 975 | + assert!( |
| 976 | + !entered.get(), |
| 977 | + "a refused spawn entered the creation window" |
| 978 | + ); |
| 979 | + }; |
| 980 | + |
| 981 | + for sddl in ["D:P", "D:P(A;;GA;;;WD)"] { |
| 982 | + let other = job_with_descriptor(&name, sddl); |
| 983 | + refused(); |
| 984 | + drop(other); |
| 985 | + } |
| 986 | + // SAFETY: the name is NUL-terminated; this event occupies only this test's fresh log name. |
| 987 | + let event = owned(unsafe { CreateEventW(ptr::null(), 1, 0, name.as_ptr()) }).unwrap(); |
| 988 | + refused(); |
| 989 | + drop(event); |
| 990 | + |
| 991 | + let other_log = fs::File::create(directory.path().join("other.log")).unwrap(); |
| 992 | + let other = create_log_job(&other_log).unwrap().unwrap(); |
| 993 | + let link = local_link(&name, &object_name(raw(&other)).unwrap()); |
| 994 | + refused(); |
| 995 | + drop(link); |
| 996 | + drop(other); |
| 997 | + let elsewhere: Vec<u16> = local_directory() |
| 998 | + .unwrap() |
| 999 | + .into_iter() |
| 1000 | + .chain(format!(r"\nvx-vm-spawn-{:032x}", id.1).encode_utf16()) |
| 1001 | + .collect(); |
| 1002 | + let link = local_link(&name, &elsewhere); |
| 1003 | + refused(); |
| 1004 | + drop(link); |
| 1005 | + |
| 1006 | + let entered = Rc::new(Cell::new(false)); |
| 1007 | + let observed = Rc::clone(&entered); |
| 1008 | + let child = with_spawn_window( |
| 1009 | + move || observed.set(true), |
| 1010 | + || supervisor::spawn_vm(&command, &[7; CAPABILITY_LEN], log), |
| 1011 | + ) |
| 1012 | + .unwrap(); |
| 1013 | + assert!(child.kill_and_wait(), "the trusted child did not exit"); |
| 1014 | + assert!( |
| 1015 | + entered.get(), |
| 1016 | + "the successful spawn bypassed the observation" |
| 1017 | + ); |
| 1018 | +} |
| 1019 | + |
943 | 1020 | #[test] |
944 | 1021 | fn what_a_link_under_the_name_of_a_logs_job_leads_to_is_not_the_logs() { |
945 | 1022 | let directory = tempfile::tempdir().unwrap(); |
@@ -1033,6 +1110,15 @@ fn a_vmm_whose_log_is_no_file_of_a_disk_volume_starts_without_a_job() { |
1033 | 1110 | std::env::temp_dir(), |
1034 | 1111 | ); |
1035 | 1112 | for log in [null, pipe] { |
| 1113 | + let error = |
| 1114 | + match supervisor::spawn_vm(&command, &[7; CAPABILITY_LEN], log.try_clone().unwrap()) { |
| 1115 | + Err(error) => error, |
| 1116 | + Ok(child) => { |
| 1117 | + assert!(child.kill_and_wait(), "the unexpected child did not exit"); |
| 1118 | + panic!("a strict spawn accepted a log without a job"); |
| 1119 | + } |
| 1120 | + }; |
| 1121 | + assert_eq!(error.kind(), io::ErrorKind::PermissionDenied, "{error}"); |
1036 | 1122 | let launched = supervisor::spawn(&command, &[7; CAPABILITY_LEN], log).unwrap(); |
1037 | 1123 | assert!(launched.kill_and_wait(), "the stand-in did not exit"); |
1038 | 1124 | } |
|
0 commit comments