| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | | use flow_init::prune; |
| 15 | | use serde::Deserialize; |
| 16 | | use std::ffi::CString; |
| 17 | | use std::io::{Read, Write}; |
| 18 | | use std::os::fd::FromRawFd; |
| 19 | | use std::sync::{Arc, Mutex}; |
| 20 | |
|
| 21 | | mod egress; |
| 22 | | mod job; |
| 23 | | use job::{dev_links, job_env, loopback_up, resolve_program, PATH}; |
| 24 | |
|
| 25 | | #[derive(Deserialize)] |
| 26 | | struct JobSpec { |
| 27 | | argv: Vec<String>, |
| 28 | | env: Vec<(String, String)>, |
| 29 | | #[serde(default)] |
| 30 | | mounts: Vec<MountSpec>, |
| 31 | | |
| 32 | | #[serde(default)] |
| 33 | | loopback: bool, |
| 34 | | |
| 35 | | |
| 36 | | #[serde(default)] |
| 37 | | egress: bool, |
| 38 | | |
| 39 | | |
| 40 | | #[serde(default)] |
| 41 | | secrets: Vec<SecretSpec>, |
| 42 | | } |
| 43 | |
|
| 44 | | #[derive(serde::Deserialize)] |
| 45 | | struct SecretSpec { |
| 46 | | path: String, |
| 47 | | value: String, |
| 48 | | } |
| 49 | |
|
| 50 | |
|
| 51 | |
|
| 52 | | #[derive(serde::Deserialize)] |
| 53 | | struct MountSpec { |
| 54 | | kind: String, |
| 55 | | target: String, |
| 56 | | size_mib: u32, |
| 57 | | #[serde(default)] |
| 58 | | device: Option<String>, |
| 59 | | |
| 60 | | |
| 61 | | |
| 62 | | #[serde(default)] |
| 63 | | prune: Option<u8>, |
| 64 | | |
| 65 | | |
| 66 | | #[serde(default)] |
| 67 | | keep: Vec<String>, |
| 68 | | } |
| 69 | |
|
| 70 | 0× | fn main() { |
| 71 | 0× | let console = Arc::new(Mutex::new(std::io::stdout())); |
| 72 | 0× | let code = match run(&console) { |
| 73 | 0× | Ok(code) => code, |
| 74 | 0× | Err(e) => { |
| 75 | 0× | frame(&console, b'F', e.as_bytes()); |
| 76 | 0× | 125 |
| 77 | | } |
| 78 | | }; |
| 79 | 0× | frame(&console, b'X', code.to_string().as_bytes()); |
| 80 | 0× | unsafe { |
| 81 | 0× | libc::sync(); |
| 82 | 0× | |
| 83 | 0× | libc::reboot(libc::LINUX_REBOOT_CMD_RESTART); |
| 84 | 0× | } |
| 85 | 0× | } |
| 86 | |
|
| 87 | 0× | fn frame(console: &Mutex<std::io::Stdout>, kind: u8, text: &[u8]) { |
| 88 | 0× | let mut out = console.lock().unwrap_or_else(|p| p.into_inner()); |
| 89 | 0× | let mut line = Vec::with_capacity(text.len() + 4); |
| 90 | 0× | line.extend_from_slice(&[0x1e, kind, b' ']); |
| 91 | 0× | line.extend_from_slice(text); |
| 92 | 0× | line.push(b'\n'); |
| 93 | 0× | let _ = out.write_all(&line); |
| 94 | 0× | let _ = out.flush(); |
| 95 | 0× | } |
| 96 | |
|
| 97 | 0× | fn cstr(s: &str) -> Result<CString, String> { |
| 98 | 0× | CString::new(s).map_err(|_| format!("{s:?} contains NUL")) |
| 99 | 0× | } |
| 100 | |
|
| 101 | 0× | fn chown(path: &str, uid: libc::uid_t, gid: libc::gid_t) -> Result<(), String> { |
| 102 | 0× | let p = cstr(path)?; |
| 103 | 0× | if unsafe { libc::chown(p.as_ptr(), uid, gid) } != 0 { |
| 104 | 0× | return Err(format!("chown {path}: {}", std::io::Error::last_os_error())); |
| 105 | 0× | } |
| 106 | 0× | Ok(()) |
| 107 | 0× | } |
| 108 | |
|
| 109 | |
|
| 110 | 0× | fn make_dirs_owned(path: &str, uid: libc::uid_t, gid: libc::gid_t) -> Result<(), String> { |
| 111 | 0× | let mut missing = Vec::new(); |
| 112 | 0× | let mut p = std::path::Path::new(path); |
| 113 | 0× | while !p.exists() { |
| 114 | 0× | missing.push(p.to_path_buf()); |
| 115 | 0× | p = match p.parent() { |
| 116 | 0× | Some(parent) => parent, |
| 117 | 0× | None => break, |
| 118 | | }; |
| 119 | | } |
| 120 | 0× | for d in missing.iter().rev() { |
| 121 | 0× | std::fs::create_dir(d).map_err(|e| format!("mkdir {}: {e}", d.display()))?; |
| 122 | 0× | chown(&d.to_string_lossy(), uid, gid)?; |
| 123 | | } |
| 124 | 0× | Ok(()) |
| 125 | 0× | } |
| 126 | |
|
| 127 | 0× | fn mount(src: &str, target: &str, fstype: &str, flags: libc::c_ulong, data: &str) -> Result<(), String> { |
| 128 | 0× | let (s, t, f, d) = (cstr(src)?, cstr(target)?, cstr(fstype)?, cstr(data)?); |
| 129 | 0× | let rc = unsafe { libc::mount(s.as_ptr(), t.as_ptr(), f.as_ptr(), flags, d.as_ptr() as *const libc::c_void) }; |
| 130 | 0× | if rc != 0 { |
| 131 | 0× | return Err(format!("mount {src} on {target}: {}", std::io::Error::last_os_error())); |
| 132 | 0× | } |
| 133 | 0× | Ok(()) |
| 134 | 0× | } |
| 135 | |
|
| 136 | 0× | fn run(console: &Arc<Mutex<std::io::Stdout>>) -> Result<i32, String> { |
| 137 | 0× | let job: JobSpec = serde_json::from_slice(&std::fs::read("/job.json").map_err(|e| format!("read /job.json: {e}"))?) |
| 138 | 0× | .map_err(|e| format!("parse /job.json: {e}"))?; |
| 139 | 0× | if job.argv.is_empty() { |
| 140 | 0× | return Err("empty argv".into()); |
| 141 | 0× | } |
| 142 | 0× | let nosuid = libc::MS_NOSUID | libc::MS_NODEV; |
| 143 | 0× | std::fs::create_dir_all("/dev").ok(); |
| 144 | 0× | mount("devtmpfs", "/dev", "devtmpfs", libc::MS_NOSUID, "")?; |
| 145 | 0× | std::fs::create_dir_all("/newroot").map_err(|e| e.to_string())?; |
| 146 | 0× | mount("/dev/vda", "/newroot", "ext4", libc::MS_RDONLY | libc::MS_NODEV, "")?; |
| 147 | 0× | mount("/dev/vdb", "/newroot/src", "ext4", nosuid, "")?; |
| 148 | 0× | mount("tmpfs", "/newroot/tmp", "tmpfs", nosuid, "mode=1777")?; |
| 149 | 0× | mount("proc", "/newroot/proc", "proc", nosuid | libc::MS_NOEXEC, "")?; |
| 150 | 0× | mount("devtmpfs", "/newroot/dev", "devtmpfs", libc::MS_NOSUID, "")?; |
| 151 | 0× | dev_links("/newroot/dev")?; |
| 152 | 0× | if std::path::Path::new("/newroot/run").is_dir() { |
| 153 | 0× | mount("tmpfs", "/newroot/run", "tmpfs", nosuid, "mode=755")?; |
| 154 | 0× | } |
| 155 | | |
| 156 | 0× | let (uid, gid) = { |
| 157 | 0× | let mut st: libc::stat = unsafe { std::mem::zeroed() }; |
| 158 | 0× | let p = cstr("/newroot/src")?; |
| 159 | 0× | if unsafe { libc::stat(p.as_ptr(), &mut st) } != 0 { |
| 160 | 0× | return Err("stat /src".into()); |
| 161 | 0× | } |
| 162 | 0× | if st.st_uid == 0 { |
| 163 | 0× | (1000, 1000) |
| 164 | | } else { |
| 165 | 0× | (st.st_uid, st.st_gid) |
| 166 | | } |
| 167 | | }; |
| 168 | | |
| 169 | | |
| 170 | 0× | for m in &job.mounts { |
| 171 | 0× | let target = format!("/newroot{}", m.target); |
| 172 | 0× | make_dirs_owned(&target, uid, gid)?; |
| 173 | 0× | match (m.kind.as_str(), &m.device) { |
| 174 | 0× | ("tmpfs", _) => mount("tmpfs", &target, "tmpfs", nosuid, &format!("size={}m,uid={uid},gid={gid},mode=755", m.size_mib))?, |
| 175 | 0× | ("disk", Some(dev)) => { |
| 176 | 0× | let atime = if m.prune.is_some() { libc::MS_STRICTATIME | libc::MS_LAZYTIME } else { 0 }; |
| 177 | 0× | mount(dev, &target, "ext4", nosuid | atime, "")?; |
| 178 | 0× | chown(&target, uid, gid)?; |
| 179 | | } |
| 180 | 0× | (kind, _) => return Err(format!("unknown mount kind {kind}")), |
| 181 | | } |
| 182 | | } |
| 183 | | |
| 184 | | |
| 185 | 0× | if !job.secrets.is_empty() { |
| 186 | 0× | if !std::path::Path::new("/newroot/run").is_dir() { |
| 187 | 0× | return Err("the image has no /run, where secrets are mounted".into()); |
| 188 | 0× | } |
| 189 | 0× | let dir = "/newroot/run/secrets"; |
| 190 | 0× | std::fs::create_dir_all(dir).map_err(|e| format!("mkdir {dir}: {e}"))?; |
| 191 | 0× | mount("tmpfs", dir, "tmpfs", nosuid | libc::MS_NOEXEC, &format!("size=1m,uid={uid},gid={gid},mode=700"))?; |
| 192 | 0× | for s in &job.secrets { |
| 193 | 0× | let Some(rel) = s.path.strip_prefix("/run/secrets/") else { |
| 194 | 0× | return Err(format!("secret path {:?} is not under /run/secrets/", s.path)); |
| 195 | | }; |
| 196 | 0× | let full = format!("{dir}/{rel}"); |
| 197 | 0× | if let Some(parent) = std::path::Path::new(&full).parent().and_then(|p| p.to_str()) { |
| 198 | 0× | make_dirs_owned(parent, uid, gid)?; |
| 199 | 0× | } |
| 200 | 0× | std::fs::write(&full, s.value.as_bytes()).map_err(|e| format!("write {}: {e}", s.path))?; |
| 201 | 0× | chown(&full, uid, gid)?; |
| 202 | 0× | std::fs::set_permissions(&full, std::os::unix::fs::PermissionsExt::from_mode(0o400)) |
| 203 | 0× | .map_err(|e| format!("chmod {}: {e}", s.path))?; |
| 204 | | } |
| 205 | 0× | } |
| 206 | 0× | if job.loopback || job.egress { |
| 207 | 0× | loopback_up()?; |
| 208 | 0× | } |
| 209 | 0× | if job.egress { |
| 210 | 0× | egress::start()?; |
| 211 | 0× | } |
| 212 | 0× | let root = cstr("/newroot")?; |
| 213 | 0× | if unsafe { libc::chroot(root.as_ptr()) } != 0 || std::env::set_current_dir("/src").is_err() { |
| 214 | 0× | return Err(format!("enter the rootfs: {}", std::io::Error::last_os_error())); |
| 215 | 0× | } |
| 216 | |
|
| 217 | | |
| 218 | | |
| 219 | 0× | let started = job.mounts.iter().any(|m| m.prune.is_some()).then(prune::start_clock); |
| 220 | 0× | let argv: Vec<CString> = job.argv.iter().map(|a| cstr(a)).collect::<Result<_, _>>()?; |
| 221 | | |
| 222 | | |
| 223 | 0× | let vars = job_env(std::fs::read_to_string("/etc/flow/env").ok().as_deref(), &job.env); |
| 224 | 0× | let path = vars.get("PATH").map(String::as_str).unwrap_or(PATH); |
| 225 | 0× | let program = cstr(&resolve_program(&job.argv[0], path).ok_or_else(|| format!("cannot run {:?}: not found in {path}", job.argv[0]))?)?; |
| 226 | 0× | let env: Vec<CString> = vars.iter().map(|(k, v)| cstr(&format!("{k}={v}"))).collect::<Result<_, _>>()?; |
| 227 | 0× | let mut out = [0; 2]; |
| 228 | 0× | let mut err = [0; 2]; |
| 229 | 0× | if unsafe { libc::pipe2(out.as_mut_ptr(), libc::O_CLOEXEC) } != 0 || unsafe { libc::pipe2(err.as_mut_ptr(), libc::O_CLOEXEC) } != 0 { |
| 230 | 0× | return Err("pipe".into()); |
| 231 | 0× | } |
| 232 | 0× | let pid = unsafe { libc::fork() }; |
| 233 | 0× | if pid < 0 { |
| 234 | 0× | return Err("fork".into()); |
| 235 | 0× | } |
| 236 | 0× | if pid == 0 { |
| 237 | | |
| 238 | | unsafe { |
| 239 | 0× | let null = libc::open(c"/dev/null".as_ptr(), libc::O_RDONLY); |
| 240 | 0× | libc::dup2(null, 0); |
| 241 | 0× | libc::dup2(out[1], 1); |
| 242 | 0× | libc::dup2(err[1], 2); |
| 243 | 0× | libc::setsid(); |
| 244 | 0× | if libc::setgroups(0, std::ptr::null()) != 0 || libc::setgid(gid) != 0 || libc::setuid(uid) != 0 { |
| 245 | 0× | libc::_exit(126); |
| 246 | 0× | } |
| 247 | 0× | let mut a: Vec<*const libc::c_char> = argv.iter().map(|c| c.as_ptr()).collect(); |
| 248 | 0× | a.push(std::ptr::null()); |
| 249 | 0× | let mut e: Vec<*const libc::c_char> = env.iter().map(|c| c.as_ptr()).collect(); |
| 250 | 0× | e.push(std::ptr::null()); |
| 251 | 0× | libc::execve(program.as_ptr(), a.as_ptr(), e.as_ptr()); |
| 252 | 0× | let msg = format!("flow: cannot run {:?}: {}\n", job.argv[0], std::io::Error::last_os_error()); |
| 253 | 0× | libc::write(2, msg.as_ptr() as *const libc::c_void, msg.len()); |
| 254 | 0× | libc::_exit(127); |
| 255 | | } |
| 256 | 0× | } |
| 257 | 0× | unsafe { |
| 258 | 0× | libc::close(out[1]); |
| 259 | 0× | libc::close(err[1]); |
| 260 | 0× | } |
| 261 | 0× | let readers: Vec<_> = [(out[0], b'O'), (err[0], b'E')] |
| 262 | 0× | .into_iter() |
| 263 | 0× | .map(|(fd, kind)| { |
| 264 | 0× | let console = console.clone(); |
| 265 | 0× | std::thread::spawn(move || pump(unsafe { std::fs::File::from_raw_fd(fd) }, kind, &console)) |
| 266 | 0× | }) |
| 267 | 0× | .collect(); |
| 268 | 0× | let mut status = 0; |
| 269 | | |
| 270 | | loop { |
| 271 | 0× | let r = unsafe { libc::waitpid(-1, &mut status, 0) }; |
| 272 | 0× | if r == pid || r < 0 { |
| 273 | 0× | break; |
| 274 | 0× | } |
| 275 | | } |
| 276 | 0× | for r in readers { |
| 277 | 0× | let _ = r.join(); |
| 278 | 0× | } |
| 279 | 0× | let code = if libc::WIFEXITED(status) { libc::WEXITSTATUS(status) } else { 128 + libc::WTERMSIG(status) }; |
| 280 | | |
| 281 | | |
| 282 | 0× | if let (Some(started), 0) = (started, code) { |
| 283 | 0× | prune_caches(console, &job.mounts, started); |
| 284 | 0× | } |
| 285 | 0× | Ok(code) |
| 286 | 0× | } |
| 287 | |
|
| 288 | |
|
| 289 | |
|
| 290 | 0× | fn prune_caches(console: &Mutex<std::io::Stdout>, mounts: &[MountSpec], started: std::time::SystemTime) { |
| 291 | | unsafe { |
| 292 | 0× | libc::kill(-1, libc::SIGKILL); |
| 293 | 0× | let mut status = 0; |
| 294 | 0× | while libc::waitpid(-1, &mut status, 0) > 0 {} |
| 295 | | } |
| 296 | 0× | for m in mounts { |
| 297 | 0× | let Some(percent) = m.prune else { continue }; |
| 298 | 0× | let root = std::path::Path::new(&m.target); |
| 299 | 0× | let pruned = match prune::usage(root) { |
| 300 | 0× | Ok((used, size)) => { |
| 301 | 0× | let mut p = prune::prune(root, started, &m.keep, size, used, percent).unwrap_or_else(|e| prune::Pruned { |
| 302 | 0× | size, |
| 303 | 0× | percent, |
| 304 | 0× | before: used, |
| 305 | 0× | after: used, |
| 306 | 0× | unpruned: Some(format!("pruning failed: {e}")), |
| 307 | 0× | ..Default::default() |
| 308 | 0× | }); |
| 309 | | |
| 310 | 0× | if p.freed > 0 { |
| 311 | 0× | unsafe { libc::sync() }; |
| 312 | 0× | if let Ok((now, _)) = prune::usage(root) { |
| 313 | 0× | p.after = now; |
| 314 | 0× | } |
| 315 | 0× | } |
| 316 | 0× | p |
| 317 | | } |
| 318 | 0× | Err(e) => prune::Pruned { percent, unpruned: Some(format!("statvfs {}: {e}", m.target)), ..Default::default() }, |
| 319 | | }; |
| 320 | 0× | let msg = serde_json::json!({ "target": m.target, "pruned": pruned }); |
| 321 | 0× | frame(console, b'C', msg.to_string().as_bytes()); |
| 322 | | } |
| 323 | 0× | } |
| 324 | |
|
| 325 | |
|
| 326 | 0× | fn pump(mut f: std::fs::File, kind: u8, console: &Mutex<std::io::Stdout>) { |
| 327 | 0× | let mut buf = [0u8; 8192]; |
| 328 | 0× | let mut line: Vec<u8> = Vec::new(); |
| 329 | | loop { |
| 330 | 0× | let n = match f.read(&mut buf) { |
| 331 | 0× | Ok(0) | Err(_) => break, |
| 332 | 0× | Ok(n) => n, |
| 333 | | }; |
| 334 | 0× | for &b in &buf[..n] { |
| 335 | 0× | if b == b'\n' || line.len() >= 16 * 1024 { |
| 336 | 0× | frame(console, kind, &line); |
| 337 | 0× | line.clear(); |
| 338 | 0× | if b == b'\n' { |
| 339 | 0× | continue; |
| 340 | 0× | } |
| 341 | 0× | } |
| 342 | 0× | line.push(b); |
| 343 | | } |
| 344 | | } |
| 345 | 0× | if !line.is_empty() { |
| 346 | 0× | frame(console, kind, &line); |
| 347 | 0× | } |
| 348 | 0× | } |