polkit elevation from UI: pkexec re-exec button, capture gating, no-CLI flow
Browse files at this commit35195ecb1c585b1cdbd8c3cee1a442c497f55146Parent: 108d8704 changed files +149 -7
- docs/USAGE.md +9 -2
- src/iface.rs +41 -0
- src/main.rs +1 -1
- src/ui/app.rs +98 -4
docs/USAGE.mdmodified+9 -2
| @@ -5,13 +5,20 @@ crack it on the GPU (CPU fallback). Single binary, GPUI shell. | |||
| 5 | 5 | ||
| 6 | ## Running | 6 | ## Running |
| 7 | 7 | ||
| 8 | -The real backend is the default and requires root: | 8 | +No terminal privileges needed. Start the app normally: |
| 9 | 9 | ||
| 10 | ```sh | 10 | ```sh |
| 11 | cargo build --release | 11 | cargo build --release |
| 12 | -sudo ./target/release/wifilab --iface wlp14s0 | 12 | +./target/release/wifilab |
| 13 | ``` | 13 | ``` |
| 14 | 14 | ||
| 15 | +Privileges are handled **inside the UI**: when not running as root the | ||
| 16 | +header shows a **"Elevate via polkit"** button — one click pops the | ||
| 17 | +system polkit dialog, and the app restarts itself elevated (same window, | ||
| 18 | +same selected interface). The unprivileged window closes automatically | ||
| 19 | +once the elevated instance is up. Starting a capture while unprivileged | ||
| 20 | +just logs a reminder; nothing fails mid-attack. | ||
| 21 | + | ||
| 15 | - `--iface <name>` picks the wireless interface (default: first detected | 22 | - `--iface <name>` picks the wireless interface (default: first detected |
| 16 | wireless interface, falling back to `wlp14s0`). The header bar's picker | 23 | wireless interface, falling back to `wlp14s0`). The header bar's picker |
| 17 | can also switch interfaces while capture is stopped. | 24 | can also switch interfaces while capture is stopped. |
src/iface.rsmodified+41 -0
| @@ -170,6 +170,47 @@ pub fn is_root() -> bool { | |||
| 170 | (unsafe { libc::geteuid() }) == 0 | 170 | (unsafe { libc::geteuid() }) == 0 |
| 171 | } | 171 | } |
| 172 | 172 | ||
| 173 | +/// True when `pkexec` (polkit) is available for GUI privilege elevation. | ||
| 174 | +pub fn pkexec_available() -> bool { | ||
| 175 | + Command::new("pkexec") | ||
| 176 | + .arg("--version") | ||
| 177 | + .stdout(std::process::Stdio::null()) | ||
| 178 | + .stderr(std::process::Stdio::null()) | ||
| 179 | + .status() | ||
| 180 | + .map(|s| s.success()) | ||
| 181 | + .unwrap_or(false) | ||
| 182 | +} | ||
| 183 | + | ||
| 184 | +/// Re-exec this binary via polkit (`pkexec`) as root, preserving the desktop | ||
| 185 | +/// session environment so the elevated instance can open its own window. | ||
| 186 | +/// | ||
| 187 | +/// Blocks until `pkexec` returns. A dismissed/cancelled authentication | ||
| 188 | +/// dialog surfaces as a non-zero exit status (126/127). MUST be called off | ||
| 189 | +/// the UI thread. | ||
| 190 | +pub fn relaunch_elevated(extra_args: &[&str]) -> Result<std::process::ExitStatus> { | ||
| 191 | + let exe = std::env::current_exe().context("resolving current_exe for pkexec re-exec")?; | ||
| 192 | + let mut cmd = Command::new("pkexec"); | ||
| 193 | + cmd.arg("env"); | ||
| 194 | + // GUI + session bus env the elevated child needs to open a window and | ||
| 195 | + // talk to the user's services (GPU/DRI inherits via the session). | ||
| 196 | + for var in [ | ||
| 197 | + "WAYLAND_DISPLAY", | ||
| 198 | + "XDG_RUNTIME_DIR", | ||
| 199 | + "DISPLAY", | ||
| 200 | + "DBUS_SESSION_BUS_ADDRESS", | ||
| 201 | + "XAUTHORITY", | ||
| 202 | + ] { | ||
| 203 | + if let Ok(v) = std::env::var(var) { | ||
| 204 | + cmd.arg(format!("{var}={v}")); | ||
| 205 | + } | ||
| 206 | + } | ||
| 207 | + cmd.arg(exe).args(extra_args); | ||
| 208 | + let status = cmd | ||
| 209 | + .status() | ||
| 210 | + .context("spawning pkexec — polkit not installed or no agent running")?; | ||
| 211 | + Ok(status) | ||
| 212 | +} | ||
| 213 | + | ||
| 173 | #[cfg(test)] | 214 | #[cfg(test)] |
| 174 | mod tests { | 215 | mod tests { |
| 175 | use super::*; | 216 | use super::*; |
src/main.rsmodified+1 -1
| @@ -51,7 +51,7 @@ fn main() { | |||
| 51 | } else if iface::is_root() { | 51 | } else if iface::is_root() { |
| 52 | "monitor mode + injection available" | 52 | "monitor mode + injection available" |
| 53 | } else { | 53 | } else { |
| 54 | - "WARNING: not root, monitor mode and injection will fail (run with sudo)" | 54 | + "not root — use the \"Elevate via polkit\" button in the UI header" |
| 55 | } | 55 | } |
| 56 | ); | 56 | ); |
| 57 | ui::run_app(cli.iface, cli.mock); | 57 | ui::run_app(cli.iface, cli.mock); |
src/ui/app.rsmodified+98 -4
| @@ -3,6 +3,8 @@ | |||
| 3 | //! task that polls the backend event channel. | 3 | //! task that polls the backend event channel. |
| 4 | 4 | ||
| 5 | use std::collections::BTreeMap; | 5 | use std::collections::BTreeMap; |
| 6 | +use std::sync::Arc; | ||
| 7 | +use std::sync::atomic::{AtomicU8, Ordering}; | ||
| 6 | use std::time::Duration; | 8 | use std::time::Duration; |
| 7 | 9 | ||
| 8 | use crossbeam_channel::{Receiver, Sender}; | 10 | use crossbeam_channel::{Receiver, Sender}; |
| @@ -79,6 +81,10 @@ pub struct AppRoot { | |||
| 79 | /// Toast queued by the event loop (no Window there) — shown on the | 81 | /// Toast queued by the event loop (no Window there) — shown on the |
| 80 | /// next render pass. | 82 | /// next render pass. |
| 81 | pub pending_toast: Option<SharedString>, | 83 | pub pending_toast: Option<SharedString>, |
| 84 | + /// Elevate-via-polkit handshake: Some while a pkexec re-exec is in | ||
| 85 | + /// flight. 0 = waiting on the polkit dialog, 1 = elevated instance | ||
| 86 | + /// launched (quit this one), 2 = declined/failed (message was logged). | ||
| 87 | + pub elevation: Option<Arc<AtomicU8>>, | ||
| 82 | /// Used to invalidate stale cracker simulation ticks after Stop/re-Start. | 88 | /// Used to invalidate stale cracker simulation ticks after Stop/re-Start. |
| 83 | pub crack_epoch: u64, | 89 | pub crack_epoch: u64, |
| 84 | pub ap_sort: Option<SortDescriptor>, | 90 | pub ap_sort: Option<SortDescriptor>, |
| @@ -151,6 +157,7 @@ impl AppRoot { | |||
| 151 | attacking_pmkid: false, | 157 | attacking_pmkid: false, |
| 152 | crack_inflight: false, | 158 | crack_inflight: false, |
| 153 | pending_toast: None, | 159 | pending_toast: None, |
| 160 | + elevation: None, | ||
| 154 | crack_epoch: 0, | 161 | crack_epoch: 0, |
| 155 | ap_sort: None, | 162 | ap_sort: None, |
| 156 | client_sort: None, | 163 | client_sort: None, |
| @@ -181,9 +188,14 @@ impl AppRoot { | |||
| 181 | }) | 188 | }) |
| 182 | .detach(); | 189 | .detach(); |
| 183 | 190 | ||
| 184 | - if !this.is_root { | 191 | + if !this.is_root && !this.is_mock { |
| 185 | - cx.toast("not root: monitor mode and injection will fail — run with sudo") | 192 | + if crate::iface::pkexec_available() { |
| 186 | - .show(window, cx); | 193 | + cx.toast("not root — click \"Elevate via polkit\" in the header to gain privileges") |
| 194 | + .show(window, cx); | ||
| 195 | + } else { | ||
| 196 | + cx.toast("not root and polkit (pkexec) unavailable — monitor mode and injection will fail") | ||
| 197 | + .show(window, cx); | ||
| 198 | + } | ||
| 187 | } | 199 | } |
| 188 | 200 | ||
| 189 | // Poll the backend event channel: wait on the background executor, | 201 | // Poll the backend event channel: wait on the background executor, |
| @@ -233,6 +245,13 @@ impl AppRoot { | |||
| 233 | self.state.push_log("ui: capture stopped, interface restored"); | 245 | self.state.push_log("ui: capture stopped, interface restored"); |
| 234 | cx.notify(); | 246 | cx.notify(); |
| 235 | } else { | 247 | } else { |
| 248 | + if !self.is_root { | ||
| 249 | + self.state.push_log( | ||
| 250 | + "ui: capture needs root — click \"Elevate via polkit\" in the header first".to_string(), | ||
| 251 | + ); | ||
| 252 | + cx.notify(); | ||
| 253 | + return; | ||
| 254 | + } | ||
| 236 | let ifname = self.state.ifname.clone(); | 255 | let ifname = self.state.ifname.clone(); |
| 237 | match Backend::start_scan(&ifname, self.tx.clone()) { | 256 | match Backend::start_scan(&ifname, self.tx.clone()) { |
| 238 | Ok(be) => { | 257 | Ok(be) => { |
| @@ -250,9 +269,61 @@ impl AppRoot { | |||
| 250 | } | 269 | } |
| 251 | } | 270 | } |
| 252 | 271 | ||
| 272 | + /// Re-exec this app as root via polkit (pkexec). The polkit dialog is | ||
| 273 | + /// fully OS-driven; on success the elevated instance opens its own | ||
| 274 | + /// window and this one closes itself. | ||
| 275 | + pub fn elevate(&mut self, cx: &mut Context<Self>) { | ||
| 276 | + if self.is_root || self.is_mock || self.elevation.is_some() { | ||
| 277 | + return; | ||
| 278 | + } | ||
| 279 | + if self.backend.is_some() { | ||
| 280 | + self.state.push_log("ui: stop capture before elevating"); | ||
| 281 | + cx.notify(); | ||
| 282 | + return; | ||
| 283 | + } | ||
| 284 | + let flag = Arc::new(AtomicU8::new(0)); | ||
| 285 | + self.elevation = Some(flag.clone()); | ||
| 286 | + let ifname = self.state.ifname.clone(); | ||
| 287 | + let tx = self.tx.clone(); | ||
| 288 | + self.state.push_log(format!( | ||
| 289 | + "ui: requesting administrative privileges via polkit (restarting elevated on {ifname})…" | ||
| 290 | + )); | ||
| 291 | + cx.notify(); | ||
| 292 | + std::thread::spawn(move || { | ||
| 293 | + match crate::iface::relaunch_elevated(&["--iface", ifname.as_str()]) { | ||
| 294 | + Ok(st) if st.success() => flag.store(1, Ordering::SeqCst), | ||
| 295 | + Ok(st) => { | ||
| 296 | + flag.store(2, Ordering::SeqCst); | ||
| 297 | + let _ = tx.send(Event::Log(format!( | ||
| 298 | + "ui: elevation dialog dismissed or failed (pkexec exit {st})" | ||
| 299 | + ))); | ||
| 300 | + } | ||
| 301 | + Err(e) => { | ||
| 302 | + flag.store(2, Ordering::SeqCst); | ||
| 303 | + let _ = tx.send(Event::Error(format!("polkit elevation failed: {e:#}"))); | ||
| 304 | + } | ||
| 305 | + } | ||
| 306 | + }); | ||
| 307 | + } | ||
| 308 | + | ||
| 253 | /// Drain every queued event, track handshake progress, refresh widgets | 309 | /// Drain every queued event, track handshake progress, refresh widgets |
| 254 | /// derived from the capture set. | 310 | /// derived from the capture set. |
| 255 | fn drain_events(&mut self, cx: &mut Context<Self>) { | 311 | fn drain_events(&mut self, cx: &mut Context<Self>) { |
| 312 | + // Elevation handshake: success → the new elevated window is up, | ||
| 313 | + // close this one; failure → allow retry (message already queued). | ||
| 314 | + if let Some(flag) = &self.elevation { | ||
| 315 | + match flag.load(Ordering::SeqCst) { | ||
| 316 | + 1 => { | ||
| 317 | + self.state | ||
| 318 | + .push_log("ui: elevated instance started — closing this window"); | ||
| 319 | + self.elevation = None; | ||
| 320 | + cx.quit(); | ||
| 321 | + return; | ||
| 322 | + } | ||
| 323 | + 2 => self.elevation = None, | ||
| 324 | + _ => {} | ||
| 325 | + } | ||
| 326 | + } | ||
| 256 | let mut changed = false; | 327 | let mut changed = false; |
| 257 | let mut captures_changed = false; | 328 | let mut captures_changed = false; |
| 258 | while let Ok(event) = self.rx.try_recv() { | 329 | while let Ok(event) = self.rx.try_recv() { |
| @@ -370,10 +441,32 @@ impl AppRoot { | |||
| 370 | 441 | ||
| 371 | let privilege = if self.is_root { | 442 | let privilege = if self.is_root { |
| 372 | badge("root").secondary() | 443 | badge("root").secondary() |
| 444 | + } else if self.is_mock { | ||
| 445 | + badge("not root (mock)").secondary() | ||
| 373 | } else { | 446 | } else { |
| 374 | - badge("not root — attacks need sudo").destructive() | 447 | + badge("not root — elevate to attack").destructive() |
| 375 | }; | 448 | }; |
| 376 | 449 | ||
| 450 | + let elevate_btn = (!self.is_root && !self.is_mock && crate::iface::pkexec_available()) | ||
| 451 | + .then(|| { | ||
| 452 | + let waiting = self.elevation.is_some(); | ||
| 453 | + button( | ||
| 454 | + "elevate-btn", | ||
| 455 | + if waiting { | ||
| 456 | + "waiting for polkit…" | ||
| 457 | + } else { | ||
| 458 | + "Elevate via polkit" | ||
| 459 | + }, | ||
| 460 | + ) | ||
| 461 | + .disabled(waiting) | ||
| 462 | + .on_click({ | ||
| 463 | + let view = view.clone(); | ||
| 464 | + move |_, _window, cx| { | ||
| 465 | + view.update(cx, |app, cx| app.elevate(cx)); | ||
| 466 | + } | ||
| 467 | + }) | ||
| 468 | + }); | ||
| 469 | + | ||
| 377 | let toggle = button( | 470 | let toggle = button( |
| 378 | "monitor-toggle", | 471 | "monitor-toggle", |
| 379 | if running { | 472 | if running { |
| @@ -405,6 +498,7 @@ impl AppRoot { | |||
| 405 | .bg(theme.surface()) | 498 | .bg(theme.surface()) |
| 406 | .child(div().text_lg().font_weight(FontWeight::BOLD).child("wifilab")) | 499 | .child(div().text_lg().font_weight(FontWeight::BOLD).child("wifilab")) |
| 407 | .child(privilege) | 500 | .child(privilege) |
| 501 | + .children(elevate_btn) | ||
| 408 | .child(div().w(px(220.)).child(self.iface_select.clone())) | 502 | .child(div().w(px(220.)).child(self.iface_select.clone())) |
| 409 | .child(toggle) | 503 | .child(toggle) |
| 410 | .child(div().flex_1()) | 504 | .child(div().flex_1()) |