| 1 | use super::*; |
| 2 | use std::os::unix::fs::{MetadataExt, PermissionsExt}; |
| 3 | |
| 4 | fn check_case(case: &str, safe: bool) { |
| 5 | run_case(case, safe, false); |
| 6 | } |
| 7 | |
| 8 | pub(super) fn check_extraction(case: &str, safe: bool) { |
| 9 | run_case(case, safe, true); |
| 10 | } |
| 11 | |
| 12 | fn run_case(case: &str, safe: bool, extraction: bool) { |
| 13 | let scratch = Scratch::new(); |
| 14 | let output = scratch.0.join("output"); |
| 15 | fs::create_dir(&output).unwrap(); |
| 16 | let mode = if case == "world-writable" { |
| 17 | 0o777 |
| 18 | } else { |
| 19 | 0o775 |
| 20 | }; |
| 21 | fs::set_permissions(&output, fs::Permissions::from_mode(mode)).unwrap(); |
| 22 | if extraction { |
| 23 | fs::write( |
| 24 | scratch.0.join("input"), |
| 25 | include_bytes!("fixtures/store.zip"), |
| 26 | ) |
| 27 | .unwrap(); |
| 28 | } |
| 29 | let metadata = fs::metadata(&output).unwrap(); |
| 30 | let uid = metadata.uid(); |
| 31 | let gid = metadata.gid(); |
| 32 | let mut passwd = format!("owner:x:{uid}:{gid}::/:/bin/sh\n"); |
| 33 | match case { |
| 34 | "shared-primary" => passwd.push_str(&format!( |
| 35 | "other:x:{}:{gid}::/:/bin/sh\n", |
| 36 | uid.wrapping_add(1) |
| 37 | )), |
| 38 | "unrelated-primary" => passwd.push_str(&format!( |
| 39 | "other:x:{}:{}::/:/bin/sh\n", |
| 40 | uid.wrapping_add(1), |
| 41 | gid.wrapping_add(1) |
| 42 | )), |
| 43 | "missing-owner" => passwd.clear(), |
| 44 | "malformed-passwd" => passwd.push_str("broken:x:not-a-uid:1000::/:/bin/sh\n"), |
| 45 | "nis-passwd" => passwd.push_str("+::::::\n"), |
| 46 | _ => {} |
| 47 | } |
| 48 | let passwd_path = scratch.0.join("passwd"); |
| 49 | fs::write(&passwd_path, passwd).unwrap(); |
| 50 | if case == "passwd-unavailable" { |
| 51 | fs::remove_file(&passwd_path).unwrap(); |
| 52 | } |
| 53 | let nss = match case { |
| 54 | "remote-accounts" => "passwd: files sss\n", |
| 55 | "systemd-personal" |
| 56 | | "systemd-dynamic-shared" |
| 57 | | "systemd-homed-shared" |
| 58 | | "systemd-unrelated" |
| 59 | | "systemd-large-record" |
| 60 | | "systemd-buffer-limit" |
| 61 | | "systemd-enumeration-error" |
| 62 | | "systemd-set-error" |
| 63 | | "systemd-load-error" |
| 64 | | "systemd-symbol-error" |
| 65 | | "systemd-partial-error" => "passwd: files systemd\n", |
| 66 | "ldap-accounts" => "passwd: files ldap\n", |
| 67 | "winbind-accounts" => "passwd: files winbind\n", |
| 68 | "systemd-remote" => "passwd: files systemd sss\n", |
| 69 | "unknown-accounts" => "passwd: files unknown\n", |
| 70 | "short-circuit-policy" => "passwd: files [SUCCESS=return] systemd\n", |
| 71 | "duplicate-policy" => "passwd: files\npasswd: files\n", |
| 72 | "missing-policy" => "group: files\n", |
| 73 | _ => "# fixture\npasswd: files # local accounts\n", |
| 74 | }; |
| 75 | let nss_path = scratch.0.join("nsswitch.conf"); |
| 76 | fs::write(&nss_path, nss).unwrap(); |
| 77 | if case == "nss-unavailable" { |
| 78 | fs::remove_file(&nss_path).unwrap(); |
| 79 | } |
| 80 | let library = scratch.0.join("private-group.so"); |
| 81 | assert!( |
| 82 | Command::new("cc") |
| 83 | .args(["-shared", "-fPIC", "-O2", "-Wall", "-Wextra", "-Werror"]) |
| 84 | .arg( |
| 85 | PathBuf::from(env!("CARGO_MANIFEST_DIR")) |
| 86 | .join("../../tests/rust-archive-worker/private_group_nss.c") |
| 87 | ) |
| 88 | .args(["-ldl", "-o"]) |
| 89 | .arg(&library) |
| 90 | .status() |
| 91 | .unwrap() |
| 92 | .success() |
| 93 | ); |
| 94 | let result = Command::new(std::env::current_exe().unwrap()) |
| 95 | .args([ |
| 96 | "--exact", |
| 97 | "private_group::probe_child", |
| 98 | "--test-threads=1", |
| 99 | "--nocapture", |
| 100 | ]) |
| 101 | .env("LD_PRELOAD", library) |
| 102 | .env("ZIPELO_GROUP_CASE", case) |
| 103 | .env("ZIPELO_GROUP_PASSWD", passwd_path) |
| 104 | .env("ZIPELO_GROUP_NSS", nss_path) |
| 105 | .env("ZIPELO_GROUP_OUTPUT", &output) |
| 106 | .env("ZIPELO_GROUP_SAFE", if safe { "1" } else { "0" }) |
| 107 | .env("ZIPELO_GROUP_EXTRACT", if extraction { "1" } else { "0" }) |
| 108 | .output() |
| 109 | .unwrap(); |
| 110 | assert!( |
| 111 | result.status.success(), |
| 112 | "{case}: {} {}", |
| 113 | String::from_utf8_lossy(&result.stdout), |
| 114 | String::from_utf8_lossy(&result.stderr) |
| 115 | ); |
| 116 | if extraction && safe { |
| 117 | assert_eq!(fs::read(output.join("folder/hello.txt")).unwrap(), PAYLOAD); |
| 118 | } else { |
| 119 | assert_eq!(fs::read_dir(&output).unwrap().count(), 0); |
| 120 | } |
| 121 | assert_eq!(fs::metadata(&output).unwrap().mode() & 0o777, mode); |
| 122 | } |
| 123 | |
| 124 | #[test] |
| 125 | fn probe_child() { |
| 126 | let Some(path) = std::env::var_os("ZIPELO_GROUP_OUTPUT") else { |
| 127 | return; |
| 128 | }; |
| 129 | let path = std::path::Path::new(&path); |
| 130 | let actual = zipelo_archive_worker::filesystem::Dir::root(path); |
| 131 | if std::env::var("ZIPELO_GROUP_SAFE").unwrap() == "1" { |
| 132 | assert!(actual.is_ok()); |
| 133 | } else { |
| 134 | assert!(matches!(actual, Err(ErrorCode::UnsafePath))); |
| 135 | } |
| 136 | if std::env::var("ZIPELO_GROUP_EXTRACT").unwrap() == "1" { |
| 137 | let events = execute(Start { |
| 138 | action: Action::Extract { |
| 139 | archive: NativePath::from_path(&path.parent().unwrap().join("input")), |
| 140 | destination: NativePath::from_path(path), |
| 141 | selection: Selection::All, |
| 142 | list_id: None, |
| 143 | gzip_output_name: None, |
| 144 | }, |
| 145 | limits: Limits::default(), |
| 146 | }); |
| 147 | if std::env::var("ZIPELO_GROUP_SAFE").unwrap() == "1" { |
| 148 | assert_eq!(terminal(&events).outcome, Outcome::Completed, "{events:?}"); |
| 149 | } else { |
| 150 | assert_eq!(terminal(&events).error, ErrorCode::UnsafePath, "{events:?}"); |
| 151 | } |
| 152 | } |
| 153 | } |
| 154 | |
| 155 | #[test] |
| 156 | fn private_group_accepts_only_current_primary_member() { |
| 157 | for case in ["personal", "unrelated-primary"] { |
| 158 | check_case(case, true); |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | #[test] |
| 163 | fn private_group_rejects_another_primary_member() { |
| 164 | check_case("shared-primary", false); |
| 165 | } |
| 166 | |
| 167 | #[test] |
| 168 | fn private_group_rejects_incomplete_account_information() { |
| 169 | for case in [ |
| 170 | "missing-owner", |
| 171 | "malformed-passwd", |
| 172 | "nis-passwd", |
| 173 | "passwd-unavailable", |
| 174 | "nss-unavailable", |
| 175 | "remote-accounts", |
| 176 | "unknown-accounts", |
| 177 | "ldap-accounts", |
| 178 | "winbind-accounts", |
| 179 | "systemd-remote", |
| 180 | "short-circuit-policy", |
| 181 | "duplicate-policy", |
| 182 | "missing-policy", |
| 183 | "group-error", |
| 184 | "group-missing", |
| 185 | "group-null-members", |
| 186 | "group-wrong-gid", |
| 187 | "supplementary-member", |
| 188 | ] { |
| 189 | check_case(case, false); |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | #[test] |
| 194 | fn private_group_checks_systemd_enumerated_accounts() { |
| 195 | for case in [ |
| 196 | "systemd-personal", |
| 197 | "systemd-unrelated", |
| 198 | "systemd-large-record", |
| 199 | ] { |
| 200 | check_extraction(case, true); |
| 201 | } |
| 202 | for case in [ |
| 203 | "systemd-dynamic-shared", |
| 204 | "systemd-homed-shared", |
| 205 | "systemd-buffer-limit", |
| 206 | "systemd-enumeration-error", |
| 207 | "systemd-set-error", |
| 208 | "systemd-load-error", |
| 209 | "systemd-symbol-error", |
| 210 | "systemd-partial-error", |
| 211 | ] { |
| 212 | check_extraction(case, false); |
| 213 | } |
| 214 | check_extraction("remote-accounts", false); |
| 215 | check_extraction("systemd-remote", false); |
| 216 | } |