|
| 1 | +use anyhow::{Context, Result}; |
| 2 | +use std::env; |
| 3 | +use std::fs; |
| 4 | +use std::io::Write; |
| 5 | +use std::os::unix::ffi::OsStrExt; |
| 6 | +use std::os::unix::fs::MetadataExt; |
| 7 | +use std::path::{Path, PathBuf}; |
| 8 | +use structopt::StructOpt; |
| 9 | +use Action::*; |
| 10 | + |
| 11 | +#[derive(StructOpt, Clone, Copy)] |
| 12 | +enum Action { |
| 13 | + /// Register wasmer as binfmt interpreter |
| 14 | + Register, |
| 15 | + /// Unregister a binfmt interpreter for wasm32 |
| 16 | + Unregister, |
| 17 | + /// Soft unregister, and register |
| 18 | + Reregister, |
| 19 | +} |
| 20 | + |
| 21 | +/// Unregister and/or register wasmer as binfmt interpreter |
| 22 | +/// |
| 23 | +/// Check the wasmer repository for a systemd service definition example |
| 24 | +/// to automate the process at start-up. |
| 25 | +#[derive(StructOpt)] |
| 26 | +pub struct Binfmt { |
| 27 | + // Might be better to traverse the mount list |
| 28 | + /// Mount point of binfmt_misc fs |
| 29 | + #[structopt(long, default_value = "/proc/sys/fs/binfmt_misc/")] |
| 30 | + binfmt_misc: PathBuf, |
| 31 | + |
| 32 | + #[structopt(subcommand)] |
| 33 | + action: Action, |
| 34 | +} |
| 35 | + |
| 36 | +// Quick safety check: |
| 37 | +// This folder isn't world writeable (or else its sticky bit is set), and neither are its parents. |
| 38 | +// |
| 39 | +// If somebody mounted /tmp wrong, this might result in a TOCTOU problem. |
| 40 | +fn seccheck(path: &Path) -> Result<()> { |
| 41 | + if let Some(parent) = path.parent() { |
| 42 | + seccheck(parent)?; |
| 43 | + } |
| 44 | + let m = std::fs::metadata(path) |
| 45 | + .with_context(|| format!("Can't check permissions of {}", path.to_string_lossy()))?; |
| 46 | + anyhow::ensure!( |
| 47 | + m.mode() & 0o2 == 0 || m.mode() & 0o1000 != 0, |
| 48 | + "{} is world writeable and not sticky", |
| 49 | + path.to_string_lossy() |
| 50 | + ); |
| 51 | + Ok(()) |
| 52 | +} |
| 53 | + |
| 54 | +impl Binfmt { |
| 55 | + /// execute [Binfmt] |
| 56 | + pub fn execute(&self) -> Result<()> { |
| 57 | + if !self.binfmt_misc.exists() { |
| 58 | + panic!("{} does not exist", self.binfmt_misc.to_string_lossy()); |
| 59 | + } |
| 60 | + let temp_dir; |
| 61 | + let specs = match self.action { |
| 62 | + Register | Reregister => { |
| 63 | + temp_dir = tempfile::tempdir().context("Make temporary directory")?; |
| 64 | + seccheck(temp_dir.path())?; |
| 65 | + let bin_path_orig: PathBuf = env::args_os() |
| 66 | + .nth(0) |
| 67 | + .map(Into::into) |
| 68 | + .filter(|p: &PathBuf| p.exists()) |
| 69 | + .context("Cannot get path to wasmer executable")?; |
| 70 | + let bin_path = temp_dir.path().join("wasmer-binfmt-interpreter"); |
| 71 | + fs::copy(&bin_path_orig, &bin_path).context("Copy wasmer binary to temp folder")?; |
| 72 | + let bin_path = fs::canonicalize(&bin_path).with_context(|| { |
| 73 | + format!( |
| 74 | + "Couldn't get absolute path for {}", |
| 75 | + bin_path.to_string_lossy() |
| 76 | + ) |
| 77 | + })?; |
| 78 | + Some([ |
| 79 | + [ |
| 80 | + b":wasm32:M::\\x00asm\\x01\\x00\\x00::".as_ref(), |
| 81 | + bin_path.as_os_str().as_bytes(), |
| 82 | + b":PFC", |
| 83 | + ] |
| 84 | + .concat(), |
| 85 | + [ |
| 86 | + b":wasm32-wat:E::wat::".as_ref(), |
| 87 | + bin_path.as_os_str().as_bytes(), |
| 88 | + b":PFC", |
| 89 | + ] |
| 90 | + .concat(), |
| 91 | + ]) |
| 92 | + } |
| 93 | + _ => None, |
| 94 | + }; |
| 95 | + let wasm_registration = self.binfmt_misc.join("wasm32"); |
| 96 | + let wat_registration = self.binfmt_misc.join("wasm32-wat"); |
| 97 | + match self.action { |
| 98 | + Reregister | Unregister => { |
| 99 | + let unregister = [wasm_registration, wat_registration] |
| 100 | + .iter() |
| 101 | + .map(|registration| { |
| 102 | + if registration.exists() { |
| 103 | + let mut registration = fs::OpenOptions::new() |
| 104 | + .write(true) |
| 105 | + .open(registration) |
| 106 | + .context("Open existing binfmt entry to remove")?; |
| 107 | + registration |
| 108 | + .write_all(b"-1") |
| 109 | + .context("Couldn't write binfmt unregister request")?; |
| 110 | + Ok(true) |
| 111 | + } else { |
| 112 | + eprintln!( |
| 113 | + "Warning: {} does not exist, not unregistered.", |
| 114 | + registration.to_string_lossy() |
| 115 | + ); |
| 116 | + Ok(false) |
| 117 | + } |
| 118 | + }) |
| 119 | + .collect::<Vec<_>>() |
| 120 | + .into_iter() |
| 121 | + .collect::<Result<Vec<_>>>()?; |
| 122 | + match (self.action, unregister.into_iter().any(|b| b)) { |
| 123 | + (Unregister, false) => bail!("Nothing unregistered"), |
| 124 | + _ => (), |
| 125 | + } |
| 126 | + } |
| 127 | + _ => (), |
| 128 | + }; |
| 129 | + if let Some(specs) = specs { |
| 130 | + if cfg!(target_env = "gnu") { |
| 131 | + // Approximate. ELF parsing for a proper check feels like overkill here. |
| 132 | + eprintln!("Warning: wasmer has been compiled for glibc, and is thus likely dynamically linked. Invoking wasm binaries in chroots or mount namespaces (lxc, docker, ...) may not work."); |
| 133 | + } |
| 134 | + specs |
| 135 | + .iter() |
| 136 | + .map(|spec| { |
| 137 | + let register = self.binfmt_misc.join("register"); |
| 138 | + let mut register = fs::OpenOptions::new() |
| 139 | + .write(true) |
| 140 | + .open(register) |
| 141 | + .context("Open binfmt misc for registration")?; |
| 142 | + register |
| 143 | + .write_all(&spec) |
| 144 | + .context("Couldn't register binfmt")?; |
| 145 | + Ok(()) |
| 146 | + }) |
| 147 | + .collect::<Vec<_>>() |
| 148 | + .into_iter() |
| 149 | + .collect::<Result<Vec<_>>>()?; |
| 150 | + } |
| 151 | + Ok(()) |
| 152 | + } |
| 153 | +} |
0 commit comments