|
| 1 | +#![cfg(any(target_os = "macos", target_os = "linux"))] |
| 2 | +mod common; |
| 3 | + |
| 4 | +use common::*; |
| 5 | +use std::collections::HashSet; |
| 6 | +use std::fs; |
| 7 | +use std::os::unix::fs::{MetadataExt, PermissionsExt}; |
| 8 | +use std::path::{Path, PathBuf}; |
| 9 | +use std::process::Command; |
| 10 | + |
| 11 | +#[cfg(target_os = "linux")] |
| 12 | +fn self_exe() -> &'static str { |
| 13 | + env!("CARGO_BIN_EXE_tmgr-sandbox-probe") |
| 14 | +} |
| 15 | + |
| 16 | +#[cfg(target_os = "macos")] |
| 17 | +fn self_exe() -> &'static str { |
| 18 | + "/usr/bin/true" |
| 19 | +} |
| 20 | + |
| 21 | +const MARK: &str = "tm786-tamper"; |
| 22 | + |
| 23 | +fn platform() -> String { |
| 24 | + format!("{}-{}", std::env::consts::OS, std::env::consts::ARCH) |
| 25 | +} |
| 26 | + |
| 27 | +fn emit(case: &str, fields: &str) { |
| 28 | + let _ = std::io::Write::write_all( |
| 29 | + &mut std::io::stderr(), |
| 30 | + format!("TM786_WRITE platform={} case={case} {fields}\n", platform()).as_bytes(), |
| 31 | + ); |
| 32 | +} |
| 33 | + |
| 34 | +fn git(dir: &Path, args: &[&str]) -> String { |
| 35 | + let o = Command::new("git") |
| 36 | + .current_dir(dir) |
| 37 | + .env("GIT_CONFIG_GLOBAL", "/dev/null") |
| 38 | + .env("GIT_CONFIG_NOSYSTEM", "1") |
| 39 | + .args(["-c", "user.name=t", "-c", "user.email=t@example.com"]) |
| 40 | + .args(args) |
| 41 | + .output() |
| 42 | + .unwrap(); |
| 43 | + assert!(o.status.success(), "{args:?}: {o:?}"); |
| 44 | + String::from_utf8_lossy(&o.stdout).trim().to_string() |
| 45 | +} |
| 46 | + |
| 47 | +fn object_files(objects: &Path) -> Vec<PathBuf> { |
| 48 | + let mut out = Vec::new(); |
| 49 | + let mut todo = vec![objects.to_path_buf()]; |
| 50 | + while let Some(d) = todo.pop() { |
| 51 | + for e in fs::read_dir(&d).unwrap() { |
| 52 | + let p = e.unwrap().path(); |
| 53 | + if p.is_dir() { |
| 54 | + todo.push(p); |
| 55 | + } else if !p.starts_with(objects.join("info")) { |
| 56 | + out.push(p); |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + out.sort(); |
| 61 | + out |
| 62 | +} |
| 63 | + |
| 64 | +fn source_repo(f: &Fx) -> PathBuf { |
| 65 | + let repo = f.root.join("source"); |
| 66 | + fs::create_dir(&repo).unwrap(); |
| 67 | + git(&repo, &["init", "-q", "-b", "main"]); |
| 68 | + fs::write(repo.join("a.txt"), "a\n").unwrap(); |
| 69 | + git(&repo, &["add", "."]); |
| 70 | + git(&repo, &["commit", "-q", "-m", "a"]); |
| 71 | + git(&repo, &["gc", "-q"]); |
| 72 | + fs::write(repo.join("b.txt"), "b\n").unwrap(); |
| 73 | + git(&repo, &["add", "."]); |
| 74 | + git(&repo, &["commit", "-q", "-m", "b"]); |
| 75 | + repo |
| 76 | +} |
| 77 | + |
| 78 | +fn usable(case: &str, f: &Fx) -> bool { |
| 79 | + let o = f.run("exit 0"); |
| 80 | + if o.code == Some(78) { |
| 81 | + skip_without_landlock(&o.stderr); |
| 82 | + } |
| 83 | + if !o.ok() { |
| 84 | + emit(case, &format!("skipped=1 sandbox_exit={:?}", o.code)); |
| 85 | + } |
| 86 | + o.ok() |
| 87 | +} |
| 88 | + |
| 89 | +fn unlock(files: &[PathBuf]) { |
| 90 | + for p in files { |
| 91 | + fs::set_permissions(p, fs::Permissions::from_mode(0o644)).unwrap(); |
| 92 | + } |
| 93 | +} |
| 94 | + |
| 95 | +fn inodes(files: &[PathBuf]) -> HashSet<(u64, u64)> { |
| 96 | + files |
| 97 | + .iter() |
| 98 | + .map(|p| { |
| 99 | + let m = p.metadata().unwrap(); |
| 100 | + (m.dev(), m.ino()) |
| 101 | + }) |
| 102 | + .collect() |
| 103 | +} |
| 104 | + |
| 105 | +fn tampered(path: &Path) -> bool { |
| 106 | + fs::read(path) |
| 107 | + .map(|b| b.windows(MARK.len()).any(|w| w == MARK.as_bytes())) |
| 108 | + .unwrap_or(false) |
| 109 | +} |
| 110 | + |
| 111 | +fn any_tampered(objects: &Path) -> usize { |
| 112 | + object_files(objects).iter().filter(|p| tampered(p)).count() |
| 113 | +} |
| 114 | + |
| 115 | +fn out_line(o: &Out, key: &str) -> String { |
| 116 | + o.stdout |
| 117 | + .lines() |
| 118 | + .find_map(|l| l.strip_prefix(&format!("{key}="))) |
| 119 | + .unwrap_or("none") |
| 120 | + .replace(' ', "_") |
| 121 | +} |
| 122 | + |
| 123 | +fn append(target: &Path, key: &str) -> String { |
| 124 | + format!("echo {MARK} >> '{}'; echo \"{key}=$?\";", target.display()) |
| 125 | +} |
| 126 | + |
| 127 | +fn control() -> &'static str { |
| 128 | + "echo ctl > ./tm786-ctl; echo \"ctl=$?\";" |
| 129 | +} |
| 130 | + |
| 131 | +fn run(f: &Fx, objects: Option<&Path>, script: &str) -> Out { |
| 132 | + match objects { |
| 133 | + Some(o) => f.run_with_objects(o, script), |
| 134 | + None => f.run(script), |
| 135 | + } |
| 136 | +} |
| 137 | + |
| 138 | +fn measure_clone_write(case: &str, f: &Fx, source_objects: &Path, objects: Option<&Path>) { |
| 139 | + let clone_objects = f.cwd.join(".git/objects"); |
| 140 | + let files = object_files(&clone_objects); |
| 141 | + let source_files = object_files(source_objects); |
| 142 | + unlock(&files); |
| 143 | + unlock(&source_files); |
| 144 | + let source_inodes = inodes(&source_files); |
| 145 | + let shared = files |
| 146 | + .iter() |
| 147 | + .filter(|p| { |
| 148 | + let m = p.metadata().unwrap(); |
| 149 | + source_inodes.contains(&(m.dev(), m.ino())) |
| 150 | + }) |
| 151 | + .count(); |
| 152 | + let target = files |
| 153 | + .iter() |
| 154 | + .find(|p| { |
| 155 | + let m = p.metadata().unwrap(); |
| 156 | + source_inodes.contains(&(m.dev(), m.ino())) |
| 157 | + }) |
| 158 | + .or(files.first()) |
| 159 | + .unwrap() |
| 160 | + .clone(); |
| 161 | + let o = run( |
| 162 | + f, |
| 163 | + objects, |
| 164 | + &format!("{}{}", control(), append(&target, "write")), |
| 165 | + ); |
| 166 | + emit( |
| 167 | + case, |
| 168 | + &format!( |
| 169 | + "sandbox_exit={:?} clone_files={} shared_inodes={shared} ctl_rc={} write_rc={} clone_tampered={} source_tampered={}", |
| 170 | + o.code, |
| 171 | + files.len(), |
| 172 | + out_line(&o, "ctl"), |
| 173 | + out_line(&o, "write"), |
| 174 | + any_tampered(&clone_objects), |
| 175 | + any_tampered(source_objects), |
| 176 | + ), |
| 177 | + ); |
| 178 | +} |
| 179 | + |
| 180 | +#[test] |
| 181 | +fn measure_prefix_clone_object_write() { |
| 182 | + let f = fixture(self_exe()); |
| 183 | + if !usable("prefix_clone_write", &f) { |
| 184 | + return; |
| 185 | + } |
| 186 | + let repo = source_repo(&f); |
| 187 | + fs::remove_dir(&f.cwd).unwrap(); |
| 188 | + git( |
| 189 | + &f.root, |
| 190 | + &[ |
| 191 | + "clone", |
| 192 | + "-q", |
| 193 | + "--reference", |
| 194 | + repo.to_str().unwrap(), |
| 195 | + repo.to_str().unwrap(), |
| 196 | + f.cwd.to_str().unwrap(), |
| 197 | + ], |
| 198 | + ); |
| 199 | + let source_objects = repo.join(".git/objects"); |
| 200 | + measure_clone_write( |
| 201 | + "prefix_clone_write", |
| 202 | + &f, |
| 203 | + &source_objects, |
| 204 | + Some(&source_objects), |
| 205 | + ); |
| 206 | +} |
| 207 | + |
| 208 | +#[test] |
| 209 | +fn measure_fixed_clone_object_write() { |
| 210 | + let f = fixture(self_exe()); |
| 211 | + if !usable("fixed_clone_write", &f) { |
| 212 | + return; |
| 213 | + } |
| 214 | + f.self_contained_clone(); |
| 215 | + let origin_objects = f.root.join("origin/.git/objects"); |
| 216 | + measure_clone_write("fixed_clone_write", &f, &origin_objects, None); |
| 217 | +} |
| 218 | + |
| 219 | +#[test] |
| 220 | +fn measure_source_objects_write_through_alternates() { |
| 221 | + let f = fixture(self_exe()); |
| 222 | + if !usable("alternates_write", &f) { |
| 223 | + return; |
| 224 | + } |
| 225 | + let objects = f.borrowing_clone(); |
| 226 | + let alt = fs::read_to_string(f.cwd.join(".git/objects/info/alternates")).unwrap(); |
| 227 | + let alt = PathBuf::from(alt.trim()); |
| 228 | + let files = object_files(&alt); |
| 229 | + unlock(&files); |
| 230 | + let existing = files[0].clone(); |
| 231 | + let mode_before = existing.metadata().unwrap().mode(); |
| 232 | + let fresh = alt.join("tm786-new-file"); |
| 233 | + let script = format!( |
| 234 | + "{}echo {MARK} > '{}'; echo \"create=$?\"; {}chmod a+x '{}'; echo \"chmod=$?\"; mkdir '{}/tm786-dir'; echo \"mkdir=$?\"; ln '{}' ./tm786-link; echo \"link=$?\"; [ -e ./tm786-link ] && {} true", |
| 235 | + control(), |
| 236 | + fresh.display(), |
| 237 | + append(&existing, "append"), |
| 238 | + existing.display(), |
| 239 | + alt.display(), |
| 240 | + existing.display(), |
| 241 | + append(Path::new("./tm786-link"), "linkwrite"), |
| 242 | + ); |
| 243 | + let o = f.run_with_objects(&objects, &script); |
| 244 | + emit( |
| 245 | + "alternates_write", |
| 246 | + &format!( |
| 247 | + "sandbox_exit={:?} ctl_rc={} create_rc={} append_rc={} chmod_rc={} mkdir_rc={} link_rc={} linkwrite_rc={} source_new_file={} source_tampered={} source_mode_changed={} source_new_dir={} link_shares_source_inode={}", |
| 248 | + o.code, |
| 249 | + out_line(&o, "ctl"), |
| 250 | + out_line(&o, "create"), |
| 251 | + out_line(&o, "append"), |
| 252 | + out_line(&o, "chmod"), |
| 253 | + out_line(&o, "mkdir"), |
| 254 | + out_line(&o, "link"), |
| 255 | + out_line(&o, "linkwrite"), |
| 256 | + fresh.exists(), |
| 257 | + any_tampered(&alt), |
| 258 | + existing.metadata().unwrap().mode() != mode_before, |
| 259 | + alt.join("tm786-dir").exists(), |
| 260 | + f.cwd |
| 261 | + .join("tm786-link") |
| 262 | + .metadata() |
| 263 | + .map(|m| m.ino() == existing.metadata().unwrap().ino()) |
| 264 | + .unwrap_or(false), |
| 265 | + ), |
| 266 | + ); |
| 267 | +} |
| 268 | + |
| 269 | +#[test] |
| 270 | +fn measure_git_env_write_to_source() { |
| 271 | + let f = fixture(self_exe()); |
| 272 | + if !usable("git_env_write", &f) { |
| 273 | + return; |
| 274 | + } |
| 275 | + let objects = f.borrowing_clone(); |
| 276 | + let source_git = objects.parent().unwrap().to_path_buf(); |
| 277 | + let source_work = source_git.parent().unwrap().to_path_buf(); |
| 278 | + let sha_of = |body: &str| { |
| 279 | + use std::io::Write; |
| 280 | + let mut c = Command::new("git") |
| 281 | + .current_dir(&source_work) |
| 282 | + .env("GIT_CONFIG_GLOBAL", "/dev/null") |
| 283 | + .env("GIT_CONFIG_NOSYSTEM", "1") |
| 284 | + .args(["hash-object", "--stdin"]) |
| 285 | + .stdin(std::process::Stdio::piped()) |
| 286 | + .stdout(std::process::Stdio::piped()) |
| 287 | + .spawn() |
| 288 | + .unwrap(); |
| 289 | + c.stdin.take().unwrap().write_all(body.as_bytes()).unwrap(); |
| 290 | + let o = c.wait_with_output().unwrap(); |
| 291 | + assert!(o.status.success()); |
| 292 | + let sha = String::from_utf8_lossy(&o.stdout).trim().to_string(); |
| 293 | + assert_eq!(sha.len(), 40); |
| 294 | + sha |
| 295 | + }; |
| 296 | + for (case, salt, prefix) in [ |
| 297 | + ( |
| 298 | + "git_object_directory_write", |
| 299 | + "objdir", |
| 300 | + format!("GIT_OBJECT_DIRECTORY='{}'", objects.display()), |
| 301 | + ), |
| 302 | + ( |
| 303 | + "git_dir_write", |
| 304 | + "gitdir", |
| 305 | + format!("GIT_DIR='{}'", source_git.display()), |
| 306 | + ), |
| 307 | + ] { |
| 308 | + let body = format!("{MARK}-{salt}"); |
| 309 | + let sha = sha_of(&body); |
| 310 | + let script = format!( |
| 311 | + "{}printf '%s' '{body}' | git hash-object -w --stdin >/dev/null 2>./tm786-err; echo \"git_ctl=$?\"; printf '%s' '{body}' | env {prefix} git hash-object -w --stdin 2>./tm786-err >/dev/null; echo \"write=$?\"; echo \"err=$(head -n 1 ./tm786-err)\"", |
| 312 | + control(), |
| 313 | + ); |
| 314 | + let o = f.run_with_objects(&objects, &script); |
| 315 | + let stored = Command::new("git") |
| 316 | + .current_dir(&source_work) |
| 317 | + .env("GIT_CONFIG_GLOBAL", "/dev/null") |
| 318 | + .env("GIT_CONFIG_NOSYSTEM", "1") |
| 319 | + .args(["cat-file", "-e", &sha]) |
| 320 | + .status() |
| 321 | + .map(|s| s.success()) |
| 322 | + .unwrap_or(false); |
| 323 | + emit( |
| 324 | + case, |
| 325 | + &format!( |
| 326 | + "sandbox_exit={:?} ctl_rc={} git_ctl_rc={} write_rc={} object_in_source={stored} loose_file_in_source={} err={}", |
| 327 | + o.code, |
| 328 | + out_line(&o, "ctl"), |
| 329 | + out_line(&o, "git_ctl"), |
| 330 | + out_line(&o, "write"), |
| 331 | + objects.join(&sha[..2]).join(&sha[2..]).exists(), |
| 332 | + out_line(&o, "err"), |
| 333 | + ), |
| 334 | + ); |
| 335 | + } |
| 336 | +} |
0 commit comments