| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use anyhow::{bail, Context, Result}; |
| 6 |
use flow_core::{Job, MountSource}; |
| 7 |
use std::path::{Path, PathBuf}; |
| 8 |
|
| 9 |
|
| 10 |
pub const MAX_ARTIFACT_BYTES: u64 = 512 << 20; |
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
pub fn locate(job: &Job, workspace: &Path, scratch: &Path, path: &str) -> (PathBuf, String) { |
| 16 |
locate_in(job, workspace, path, |i| scratch.join(format!("mount-{i}.ext4"))) |
| 17 |
} |
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
pub fn locate_dir(job: &Job, workspace: &Path, scratch: &Path, path: &str) -> (PathBuf, String) { |
| 22 |
locate_in(job, workspace, path, |i| crate::syscall::disk_dir(scratch, i)) |
| 23 |
} |
| 24 |
|
| 25 |
fn locate_in(job: &Job, workspace: &Path, path: &str, disk: impl Fn(usize) -> PathBuf) -> (PathBuf, String) { |
| 26 |
for (i, m) in job.mounts.iter().filter(|m| m.source == MountSource::Disk).enumerate() { |
| 27 |
if let Some(rest) = path.strip_prefix(&format!("{}/", m.target)) { |
| 28 |
return (disk(i), format!("/{rest}")); |
| 29 |
} |
| 30 |
} |
| 31 |
(workspace.to_path_buf(), path.strip_prefix("/src").unwrap_or(path).to_string()) |
| 32 |
} |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
pub fn read_dir_file(base: &Path, inner: &str) -> Result<Vec<u8>> { |
| 39 |
use std::ffi::CString; |
| 40 |
use std::io::Read; |
| 41 |
use std::os::fd::FromRawFd; |
| 42 |
let parts: Vec<&str> = inner.split('/').filter(|p| !p.is_empty()).collect(); |
| 43 |
if parts.is_empty() || parts.iter().any(|p| *p == "." || *p == "..") { |
| 44 |
bail!("{inner} is not a clean path"); |
| 45 |
} |
| 46 |
let c = |s: &str| CString::new(s).context("a path holds NUL"); |
| 47 |
let mut dir = unsafe { libc::open(c(&base.to_string_lossy())?.as_ptr(), libc::O_DIRECTORY | libc::O_RDONLY | libc::O_CLOEXEC) }; |
| 48 |
if dir < 0 { |
| 49 |
bail!("open {}: {}", base.display(), std::io::Error::last_os_error()); |
| 50 |
} |
| 51 |
let (last, dirs) = parts.split_last().unwrap(); |
| 52 |
for d in dirs { |
| 53 |
let next = unsafe { libc::openat(dir, c(d)?.as_ptr(), libc::O_DIRECTORY | libc::O_NOFOLLOW | libc::O_RDONLY | libc::O_CLOEXEC) }; |
| 54 |
let err = std::io::Error::last_os_error(); |
| 55 |
unsafe { libc::close(dir) }; |
| 56 |
if next < 0 { |
| 57 |
bail!("{inner}: {d} is not a directory in the job's files ({err})"); |
| 58 |
} |
| 59 |
dir = next; |
| 60 |
} |
| 61 |
let fd = unsafe { libc::openat(dir, c(last)?.as_ptr(), libc::O_NOFOLLOW | libc::O_RDONLY | libc::O_CLOEXEC | libc::O_NONBLOCK) }; |
| 62 |
let err = std::io::Error::last_os_error(); |
| 63 |
unsafe { libc::close(dir) }; |
| 64 |
if fd < 0 { |
| 65 |
bail!("{inner} is not a regular file in the job's files ({err})"); |
| 66 |
} |
| 67 |
let file = unsafe { std::fs::File::from_raw_fd(fd) }; |
| 68 |
let meta = file.metadata()?; |
| 69 |
if !meta.is_file() { |
| 70 |
bail!("{inner} is not a regular file in the job's files (it may be missing or a link)"); |
| 71 |
} |
| 72 |
if meta.len() > MAX_ARTIFACT_BYTES { |
| 73 |
bail!("{inner} is {} MiB; artifacts are at most {} MiB", meta.len() >> 20, MAX_ARTIFACT_BYTES >> 20); |
| 74 |
} |
| 75 |
let mut bytes = Vec::with_capacity(meta.len() as usize); |
| 76 |
file.take(MAX_ARTIFACT_BYTES + 1).read_to_end(&mut bytes)?; |
| 77 |
if bytes.len() as u64 > MAX_ARTIFACT_BYTES { |
| 78 |
bail!("{inner} grew past {} MiB while it was read", MAX_ARTIFACT_BYTES >> 20); |
| 79 |
} |
| 80 |
Ok(bytes) |
| 81 |
} |
| 82 |
|
| 83 |
|
| 84 |
pub const MAX_REPORT_FILES: usize = 2000; |
| 85 |
|
| 86 |
pub const MAX_REPORT_BYTES: u64 = 512 << 20; |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
pub fn read_report(image: &Path, inner: &str, scratch: &Path) -> Result<Vec<(String, Vec<u8>)>> { |
| 91 |
let stat = debugfs(image, &format!("stat \"{inner}\""))?; |
| 92 |
if !stat.contains("Type: directory") { |
| 93 |
bail!("{inner} is not a directory in the job's disk: a report is a directory holding report.toml"); |
| 94 |
} |
| 95 |
let dest = scratch.join("report.out"); |
| 96 |
let _ = std::fs::remove_dir_all(&dest); |
| 97 |
std::fs::create_dir_all(&dest)?; |
| 98 |
debugfs(image, &format!("rdump \"{inner}\" \"{}\"", dest.display()))?; |
| 99 |
let base = inner.trim_end_matches('/').rsplit('/').next().unwrap_or_default(); |
| 100 |
let files = read_tree(&dest.join(base)); |
| 101 |
let _ = std::fs::remove_dir_all(&dest); |
| 102 |
files |
| 103 |
} |
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
|
| 108 |
pub fn read_report_dir(base: &Path, inner: &str) -> Result<Vec<(String, Vec<u8>)>> { |
| 109 |
let mut dir = base.to_path_buf(); |
| 110 |
for part in inner.split('/').filter(|p| !p.is_empty()) { |
| 111 |
if part == "." || part == ".." { |
| 112 |
bail!("{inner} is not a clean path"); |
| 113 |
} |
| 114 |
dir.push(part); |
| 115 |
let meta = std::fs::symlink_metadata(&dir).with_context(|| format!("{inner}: no such directory in the job's tree"))?; |
| 116 |
if !meta.is_dir() { |
| 117 |
bail!("{inner} is not a directory in the job's tree (a link is not followed): a report is a directory holding report.toml"); |
| 118 |
} |
| 119 |
} |
| 120 |
read_tree(&dir) |
| 121 |
} |
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
fn report_name_ok(n: &str) -> bool { |
| 126 |
!n.is_empty() && n != "." && n != ".." && n.len() <= 255 && n.bytes().all(|b| b.is_ascii_alphanumeric() || b"._-+".contains(&b)) |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
pub fn read_tree(root: &Path) -> Result<Vec<(String, Vec<u8>)>> { |
| 134 |
let mut out: Vec<(String, Vec<u8>)> = Vec::new(); |
| 135 |
let mut total = 0u64; |
| 136 |
let mut stack = vec![(root.to_path_buf(), String::new())]; |
| 137 |
while let Some((dir, rel)) = stack.pop() { |
| 138 |
for entry in std::fs::read_dir(&dir).with_context(|| format!("read {}", dir.display()))? { |
| 139 |
let entry = entry?; |
| 140 |
let name = entry.file_name().into_string().map_err(|_| anyhow::anyhow!("a report file name is not UTF-8"))?; |
| 141 |
let path = if rel.is_empty() { name.clone() } else { format!("{rel}/{name}") }; |
| 142 |
if !report_name_ok(&name) { |
| 143 |
bail!("report file {path:?}: names may hold letters, digits, '.', '_', '-' and '+'"); |
| 144 |
} |
| 145 |
let meta = std::fs::symlink_metadata(entry.path())?; |
| 146 |
if meta.is_dir() { |
| 147 |
stack.push((entry.path(), path)); |
| 148 |
} else if meta.is_file() { |
| 149 |
if meta.len() > MAX_ARTIFACT_BYTES { |
| 150 |
bail!("report file {path} is {} MiB; a file is at most {} MiB", meta.len() >> 20, MAX_ARTIFACT_BYTES >> 20); |
| 151 |
} |
| 152 |
total += meta.len(); |
| 153 |
if total > MAX_REPORT_BYTES { |
| 154 |
bail!("the report holds more than {} MiB", MAX_REPORT_BYTES >> 20); |
| 155 |
} |
| 156 |
if out.len() == MAX_REPORT_FILES { |
| 157 |
bail!("the report holds more than {MAX_REPORT_FILES} files"); |
| 158 |
} |
| 159 |
out.push((path, std::fs::read(entry.path())?)); |
| 160 |
} |
| 161 |
} |
| 162 |
} |
| 163 |
if !out.iter().any(|(p, _)| p == "report.toml") { |
| 164 |
bail!("the report directory has no report.toml at its top"); |
| 165 |
} |
| 166 |
out.sort(); |
| 167 |
Ok(out) |
| 168 |
} |
| 169 |
|
| 170 |
fn debugfs(image: &Path, request: &str) -> Result<String> { |
| 171 |
let out = std::process::Command::new("debugfs").arg("-R").arg(request).arg(image).output().context("run debugfs (e2fsprogs)")?; |
| 172 |
if !out.status.success() { |
| 173 |
bail!("debugfs {request:?}: {}", String::from_utf8_lossy(&out.stderr).trim()); |
| 174 |
} |
| 175 |
Ok(String::from_utf8_lossy(&out.stdout).into_owned()) |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
pub fn read(image: &Path, inner: &str, scratch: &Path) -> Result<Vec<u8>> { |
| 182 |
let stat = debugfs(image, &format!("stat \"{inner}\""))?; |
| 183 |
if !stat.contains("Type: regular") { |
| 184 |
bail!("{inner} is not a regular file in the job's disk (it may be missing or a link)"); |
| 185 |
} |
| 186 |
let size: u64 = stat |
| 187 |
.split("Size: ") |
| 188 |
.nth(1) |
| 189 |
.and_then(|s| s.split_whitespace().next()) |
| 190 |
.and_then(|s| s.parse().ok()) |
| 191 |
.context("debugfs gave no size")?; |
| 192 |
if size > MAX_ARTIFACT_BYTES { |
| 193 |
bail!("{inner} is {} MiB; artifacts are at most {} MiB", size >> 20, MAX_ARTIFACT_BYTES >> 20); |
| 194 |
} |
| 195 |
let dest = scratch.join("artifact.out"); |
| 196 |
let _ = std::fs::remove_file(&dest); |
| 197 |
debugfs(image, &format!("dump \"{inner}\" \"{}\"", dest.display()))?; |
| 198 |
let bytes = std::fs::read(&dest).with_context(|| format!("{inner}: debugfs wrote nothing"))?; |
| 199 |
let _ = std::fs::remove_file(&dest); |
| 200 |
if bytes.len() as u64 != size { |
| 201 |
bail!("{inner}: read {} bytes of {size}", bytes.len()); |
| 202 |
} |
| 203 |
Ok(bytes) |
| 204 |
} |
| 205 |
|
| 206 |
#[cfg(test)] |
| 207 |
mod tests { |
| 208 |
use super::*; |
| 209 |
use flow_core::Mount; |
| 210 |
|
| 211 |
fn job() -> Job { |
| 212 |
Job { |
| 213 |
name: "build".into(), |
| 214 |
sandbox: "rust".into(), |
| 215 |
scope: vec![], |
| 216 |
depends_on: vec![], |
| 217 |
env: Default::default(), |
| 218 |
exec: vec!["make".into()], |
| 219 |
secrets: vec![], |
| 220 |
identities: vec![], |
| 221 |
mounts: vec![ |
| 222 |
Mount { source: MountSource::Tmpfs, target: "/src/cache".into(), size_mib: 64 }, |
| 223 |
Mount { source: MountSource::Disk, target: "/src/target".into(), size_mib: 1024 }, |
| 224 |
Mount { source: MountSource::Disk, target: "/src/work".into(), size_mib: 1024 }, |
| 225 |
], |
| 226 |
outputs: vec![], |
| 227 |
runs_on: vec![], |
| 228 |
timeout_s: None, |
| 229 |
on: None, |
| 230 |
} |
| 231 |
} |
| 232 |
|
| 233 |
#[test] |
| 234 |
fn paths_are_found_on_the_disk_that_holds_them() { |
| 235 |
let (w, s) = (Path::new("/w.ext4"), Path::new("/scratch")); |
| 236 |
assert_eq!(locate(&job(), w, s, "/src/target/release/oxy"), (s.join("mount-0.ext4"), "/release/oxy".into())); |
| 237 |
assert_eq!(locate(&job(), w, s, "/src/work/a"), (s.join("mount-1.ext4"), "/a".into())); |
| 238 |
assert_eq!(locate(&job(), w, s, "/src/docs/a.1"), (w.to_path_buf(), "/docs/a.1".into())); |
| 239 |
assert_eq!(locate(&job(), w, s, "/src/targets/x"), (w.to_path_buf(), "/targets/x".into()), "a prefix is not a parent"); |
| 240 |
} |
| 241 |
|
| 242 |
#[test] |
| 243 |
fn files_are_read_from_directories_without_following_links() { |
| 244 |
let dir = tempfile::tempdir().unwrap(); |
| 245 |
let tree = dir.path().join("tree"); |
| 246 |
std::fs::create_dir_all(tree.join("release")).unwrap(); |
| 247 |
std::fs::write(tree.join("release/tool"), b"built").unwrap(); |
| 248 |
std::fs::write(dir.path().join("secret"), b"runner key").unwrap(); |
| 249 |
std::os::unix::fs::symlink(dir.path().join("secret"), tree.join("release/link")).unwrap(); |
| 250 |
std::os::unix::fs::symlink(dir.path(), tree.join("up")).unwrap(); |
| 251 |
assert_eq!(read_dir_file(&tree, "/release/tool").unwrap(), b"built"); |
| 252 |
assert!(read_dir_file(&tree, "/release/link").is_err(), "a link to a host file"); |
| 253 |
assert!(read_dir_file(&tree, "/up/secret").is_err(), "a linked directory"); |
| 254 |
assert!(read_dir_file(&tree, "/release/../release/tool").is_err()); |
| 255 |
assert!(read_dir_file(&tree, "/release").is_err(), "a directory"); |
| 256 |
let s = Path::new("/scratch"); |
| 257 |
assert_eq!(locate_dir(&job(), &tree, s, "/src/target/a"), (s.join("mount-0"), "/a".into())); |
| 258 |
} |
| 259 |
|
| 260 |
#[test] |
| 261 |
fn reports_are_read_as_trees_without_following_links() { |
| 262 |
let dir = tempfile::tempdir().unwrap(); |
| 263 |
let tree = dir.path().join("tree"); |
| 264 |
let report = tree.join("out/report"); |
| 265 |
std::fs::create_dir_all(report.join("data")).unwrap(); |
| 266 |
std::fs::write(report.join("report.toml"), b"kind = \"custom\"\n").unwrap(); |
| 267 |
std::fs::write(report.join("index.html"), b"<p>hi</p>").unwrap(); |
| 268 |
std::fs::write(report.join("data/frames.csv"), b"1,2\n").unwrap(); |
| 269 |
std::fs::write(dir.path().join("secret"), b"runner key").unwrap(); |
| 270 |
std::os::unix::fs::symlink(dir.path().join("secret"), report.join("data/leak")).unwrap(); |
| 271 |
std::os::unix::fs::symlink(dir.path(), report.join("up")).unwrap(); |
| 272 |
let files = read_report_dir(&tree, "/out/report").unwrap(); |
| 273 |
let paths: Vec<&str> = files.iter().map(|(p, _)| p.as_str()).collect(); |
| 274 |
assert_eq!(paths, ["data/frames.csv", "index.html", "report.toml"], "sorted, links left out"); |
| 275 |
assert_eq!(files[0].1, b"1,2\n"); |
| 276 |
|
| 277 |
std::os::unix::fs::symlink(&report, tree.join("linked")).unwrap(); |
| 278 |
assert!(read_report_dir(&tree, "/linked").is_err(), "a linked report directory"); |
| 279 |
assert!(read_report_dir(&tree, "/out/../out/report").is_err()); |
| 280 |
assert!(read_report_dir(&tree, "/out/report/index.html").is_err(), "a file"); |
| 281 |
|
| 282 |
std::fs::remove_file(report.join("report.toml")).unwrap(); |
| 283 |
assert!(format!("{:#}", read_tree(&report).unwrap_err()).contains("report.toml")); |
| 284 |
std::fs::write(report.join("report.toml"), b"").unwrap(); |
| 285 |
std::fs::write(report.join("bad name"), b"").unwrap(); |
| 286 |
assert!(read_tree(&report).is_err(), "a name that cannot travel as is"); |
| 287 |
} |
| 288 |
|
| 289 |
#[test] |
| 290 |
fn files_are_read_back_from_an_ext4_image() { |
| 291 |
let dir = tempfile::tempdir().unwrap(); |
| 292 |
let tree = dir.path().join("tree"); |
| 293 |
std::fs::create_dir_all(tree.join("release")).unwrap(); |
| 294 |
let body: Vec<u8> = (0..200_000u32).map(|i| (i % 251) as u8).collect(); |
| 295 |
std::fs::write(tree.join("release/tool"), &body).unwrap(); |
| 296 |
std::os::unix::fs::symlink("tool", tree.join("release/link")).unwrap(); |
| 297 |
let image = dir.path().join("disk.ext4"); |
| 298 |
crate::workspace::image(&tree, &image).unwrap(); |
| 299 |
assert_eq!(read(&image, "/release/tool", dir.path()).unwrap(), body); |
| 300 |
assert!(read(&image, "/release/missing", dir.path()).is_err()); |
| 301 |
assert!(read(&image, "/release/link", dir.path()).unwrap_err().to_string().contains("not a regular file")); |
| 302 |
|
| 303 |
std::fs::create_dir_all(tree.join("report/data")).unwrap(); |
| 304 |
std::fs::write(tree.join("report/report.toml"), b"kind = \"custom\"\n").unwrap(); |
| 305 |
std::fs::write(tree.join("report/data/a.csv"), &body).unwrap(); |
| 306 |
std::os::unix::fs::symlink("/etc/passwd", tree.join("report/data/leak")).unwrap(); |
| 307 |
crate::workspace::image(&tree, &image).unwrap(); |
| 308 |
let files = read_report(&image, "/report", dir.path()).unwrap(); |
| 309 |
assert_eq!(files.iter().map(|(p, _)| p.as_str()).collect::<Vec<_>>(), ["data/a.csv", "report.toml"]); |
| 310 |
assert_eq!(files[0].1, body); |
| 311 |
assert!(read_report(&image, "/release/tool", dir.path()).is_err(), "a file is not a report"); |
| 312 |
} |
| 313 |
} |