Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,16 @@ is on main, the release is published with that section as its notes (AGENTS.md,

### Installer and launcher

- On Linux, a start through palera1n or checkra1n begins only once the
computer has given your account access to the device, so the tool is no
longer refused it in its first moment. When a tool is refused the device
anyway, as it comes back on USB during the exploit, the start ends with
"USB permission: … was refused the device" and how to fix it (Orchard's
USB rule, or starting orchard with sudo), instead of "Pongo did not
appear on USB" or a timeout. The iOS 12 to 14 start with checkra1n
reports checkra1n's own failure about a minute after it gives up, instead
of "usbmuxd is not running" seven minutes later.

- Choosing Dopamine in the Options after a start with palera1n didn't work
now switches to Dopamine: the installer finds the device again as it is,
and Continue installs the helper app. Before, it kept sending the device
Expand Down
70 changes: 67 additions & 3 deletions crates/runtime/src/access_checkra1n.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@ const SSH_PORT: u16 = 44;
const PASSWORD: &str = "alpine";
/// checkra1n's own run, then iOS's start until its SSH server answers.
const BOOT: Duration = Duration::from_secs(120 + 300);
/// How long checkra1n may say nothing more after an error before the start
/// counts as failed: after "Timed out in state 1" it waits on with the
/// device in DFU mode and never tries again (orchard#416).
const SILENT_AFTER_ERROR: Duration = Duration::from_secs(60);

fn fail(message: impl Into<String>) -> Error {
Error::new(Code::Refused, message.into())
Expand Down Expand Up @@ -105,6 +109,23 @@ fn spawn_error(error: std::io::Error) -> Error {
error.into()
}

/// checkra1n's last error, when it is its last word: "checkra1n: Timed out
/// in state 1, …", or a USB permission timeout when it was refused the
/// device (usb_node.rs). None while it carries on after its errors.
fn last_word(log: &str) -> Option<Error> {
let line = crate::chain::strip_ansi(log.lines().rev().find(|l| !l.trim().is_empty())?);
let message = line.split("<Error>").nth(1)?.trim_start_matches(':').trim();
let failed = fail(format!("checkra1n: {message}"));
Some(if cfg!(target_os = "linux") && crate::usb_node::refused_in(log) { crate::usb_node::refused("checkra1n", &failed) } else { failed })
}

/// Why a start that ran out of time failed: checkra1n's own last error over
/// usbmuxd's absence (iOS never started, so nothing started usbmuxd), else
/// that, else that the SSH server did not come up.
fn timed_out(log: &str, usbmuxd_down: Option<Error>) -> Error {
last_word(log).or(usbmuxd_down).unwrap_or_else(|| fail("checkra1n's SSH server did not come up. Unlock the device; if it stays in iOS without the checkra1n app, start again"))
}

/// Runs checkra1n until the device's SSH server answers, and returns the
/// device's UDID.
fn start_ios(executor: &Executor) -> Result<String> {
Expand All @@ -119,6 +140,11 @@ fn start_ios(executor: &Executor) -> Result<String> {
// Resolved while the device is in DFU mode: its node goes away after.
let reset_off = crate::flags::port_reset().is_some_and(|w| w.is_zero());
let in_dfu: Vec<_> = orchard_usb::list().map(|all| all.into_iter().filter(|d| d.mode == orchard_usb::Mode::Dfu).collect()).unwrap_or_default();
// Linux: a device that has just entered DFU mode may not be open to this
// account yet; checkra1n 0.12.4 doesn't try again when refused.
if let [device] = in_dfu.as_slice() {
crate::usb_node::wait_accessible(&device.location)?;
}
let replug = match in_dfu.as_slice() {
[device] if !reset_off => crate::replug::port_control(&device.location, Path::new("/sys/bus/usb/devices")).map(|c| crate::replug::Watcher::start(log_path.clone(), c)),
_ => None,
Expand All @@ -139,6 +165,8 @@ fn start_ios(executor: &Executor) -> Result<String> {
}
};
let until = Instant::now() + BOOT;
let read_log = || std::fs::read(&log_path).map(|t| String::from_utf8_lossy(&t).into_owned()).unwrap_or_default();
let (mut seen, mut changed) = (0usize, Instant::now());
let outcome = loop {
std::thread::sleep(Duration::from_secs(3));
let usbmuxd_down = match ssh_answers() {
Expand All @@ -151,10 +179,17 @@ fn start_ios(executor: &Executor) -> Result<String> {
break Err(fail(format!("checkra1n failed ({status}); its log: {}", log_path.display())));
}
}
let text = read_log();
if text.len() != seen {
(seen, changed) = (text.len(), Instant::now());
}
if changed.elapsed() >= SILENT_AFTER_ERROR {
if let Some(failed) = last_word(&text) {
break Err(failed);
}
}
if Instant::now() > until {
break Err(usbmuxd_down.unwrap_or_else(|| {
fail("checkra1n's SSH server did not come up. Unlock the device; if it stays in iOS without the checkra1n app, start again")
}));
break Err(timed_out(&text, usbmuxd_down));
}
};
let _ = child.kill();
Expand Down Expand Up @@ -562,6 +597,35 @@ mod tests {
assert_eq!(on_host(failed(), "s5l8960x", false).hint, None);
}

#[test]
fn a_failed_checkra1n_is_named_not_usbmuxd() {
// orchard#416, run 7: refused after the trigger, then timed out.
let refused = "14:24:05 <Info>: Right before trigger (this is the real bug setup)\n\
14:24:05 <Error>: Couldn't open USB interface due to EACCES. Run as root maybe? (error code: -75)\n\
14:24:30 <Error>: Timed out in state 1, assuming we are back to square one with this device. (error code: -31)\n";
let usbmuxd = || Some(Error::new(Code::NotFound, "usbmuxd is not running (Connection refused (os error 111))"));
let error = timed_out(refused, usbmuxd());
if cfg!(target_os = "linux") {
assert!(error.message.starts_with("USB permission: checkra1n was refused the device"), "{}", error.message);
assert!(error.to_string().contains("orchard setup usb"));
} else {
assert!(error.message.starts_with("checkra1n: Timed out in state 1"));
}
assert!(!error.message.contains("usbmuxd"));
// As root (no EACCES): checkra1n's own error.
let root = "14:43:17 <Info>: Right before trigger (this is the real bug setup)\n\
14:43:42 <Error>: Timed out in state 1, assuming we are back to square one with this device. (error code: -31)\n";
assert_eq!(
timed_out(root, usbmuxd()).message,
"checkra1n: Timed out in state 1, assuming we are back to square one with this device. (error code: -31)"
);
// checkra1n went on after an error: not its last word.
assert!(last_word(&format!("{root}14:43:45 <Info>: Exploiting\n")).is_none());
// Nothing from checkra1n: usbmuxd, then the SSH server.
assert!(timed_out("14:43:01 <Info>: Booting...\n", usbmuxd()).message.starts_with("usbmuxd is not running"));
assert!(timed_out("", None).message.starts_with("checkra1n's SSH server did not come up"));
}

#[test]
fn a_leftover_session_is_undone_with_its_own_helper() {
let script = leftover(SESSION, APP);
Expand Down
32 changes: 21 additions & 11 deletions crates/runtime/src/chain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -142,21 +142,21 @@ fn install_bundle(executor: &Executor, session: &Session<Host>, device: &Device)
/// Runs palera1n on the device in DFU: checks it is the user's device
/// first, resets its USB port on Linux if it doesn't come back after
/// checkm8, and logs palera1n's output in the cache.
fn run_palera1n(executor: &Executor, session: &mut Session<Host>, args: &[std::ffi::OsString], seconds: u64) -> Result<()> {
fn run_palera1n(executor: &Executor, session: &mut Session<Host>, args: &[std::ffi::OsString], seconds: u64) -> Result<std::path::PathBuf> {
run_palera1n_from(executor, session, Mode::Dfu, args, seconds)
}

/// palera1n on the device in `from`: DFU (the exploit, with the port reset
/// on Linux), or Pongo (palera1n carries on from there).
fn run_palera1n_from(executor: &Executor, session: &mut Session<Host>, from: Mode, args: &[std::ffi::OsString], seconds: u64) -> Result<()> {
fn run_palera1n_from(executor: &Executor, session: &mut Session<Host>, from: Mode, args: &[std::ffi::OsString], seconds: u64) -> Result<std::path::PathBuf> {
let (pin, release) = crate::flags::palera1n_pin().ok_or_else(|| fail("palera1n publishes no build for this computer"))?;
let palera1n = executor.palera1n(pin, release)?;
run_tool(executor, session, from, "palera1n", &labelled("palera1n", release), &palera1n, args, seconds)
}

/// checkra1n's exploit and Pongo with `pongo_image`,
/// with the arguments orchard's Python CLI runs it with.
fn run_checkra1n(executor: &Executor, session: &mut Session<Host>, pongo_image: &std::path::Path, seconds: u64) -> Result<()> {
fn run_checkra1n(executor: &Executor, session: &mut Session<Host>, pongo_image: &std::path::Path, seconds: u64) -> Result<std::path::PathBuf> {
let (pin, release) = crate::flags::checkra1n_pin().ok_or_else(|| fail("checkra1n publishes no build for this computer"))?;
let checkra1n = executor.palera1n(pin, release)?;
let args: Vec<std::ffi::OsString> = ["-E", "-p", "-v", "-n", "-k"].iter().map(Into::into).chain([pongo_image.as_os_str().to_owned()]).collect();
Expand Down Expand Up @@ -487,12 +487,18 @@ fn step(text: &str) {
}

/// One run of the tool that takes the device on from `from`, its output in
/// a log of its own and followed while it runs.
fn run_tool(executor: &Executor, session: &mut Session<Host>, from: Mode, name: &str, label: &str, palera1n: &std::path::Path, args: &[std::ffi::OsString], seconds: u64) -> Result<()> {
/// a log of its own and followed while it runs; returns that log. A failure
/// whose log shows the tool was refused the device is a USB permission
/// timeout (usb_node.rs).
fn run_tool(executor: &Executor, session: &mut Session<Host>, from: Mode, name: &str, label: &str, palera1n: &std::path::Path, args: &[std::ffi::OsString], seconds: u64) -> Result<std::path::PathBuf> {
if !now_in(session, from)? {
return Err(fail(if from == Mode::Dfu { "the device is no longer in DFU mode" } else { "the device is not in Pongo" }));
}
let location = session.device().location.clone();
// Linux: the device has just shown up in this mode (a replug, Pongo
// after the exploit); the tool opens it once, and only after udev gave
// this account access.
crate::usb_node::wait_accessible(&location)?;
if let Some(ecid) = &session.identity().ecid {
orchard_cache::runlog::hide(ecid);
}
Expand Down Expand Up @@ -565,7 +571,7 @@ fn run_tool(executor: &Executor, session: &mut Session<Host>, from: Mode, name:
step(&format!("{label} did not end within {seconds} s: ended by orchard"));
let _ = child.kill();
let _ = child.wait();
break Err(fail(format!("{name} did not finish")));
break Err(crate::usb_node::explain(name, &log_path, fail(format!("{name} did not finish"))));
}
// From DFU mode, iOS on USB means the device restarted normally:
// this start can't work any more, whatever palera1n still waits for.
Expand Down Expand Up @@ -601,9 +607,10 @@ fn run_tool(executor: &Executor, session: &mut Session<Host>, from: Mode, name:
}
let status = status?;
if !status.success() {
return Err(fail(last_error(&log_path).unwrap_or_else(|| format!("{name} failed ({status})"))));
let failed = fail(last_error(&log_path).unwrap_or_else(|| format!("{name} failed ({status})")));
return Err(crate::usb_node::explain(name, &log_path, failed));
}
Ok(())
Ok(log_path)
}

/// palera1n's last error line, without colors and timestamp: "palera1n:
Expand Down Expand Up @@ -703,8 +710,9 @@ fn into_pongo(executor: &Executor, session: &mut Session<Host>, pongo_image: &st
Tool::Checkra1n => run_checkra1n(executor, session, pongo_image, PALERA1N_SECONDS),
Tool::Palera1n => run_palera1n(executor, session, &args, PALERA1N_SECONDS),
};
let name = if with == Tool::Checkra1n { "checkra1n" } else { "palera1n" };
let failed = match ran {
Ok(()) => {
Ok(log) => {
let deadline = Instant::now() + Duration::from_secs(PONGO_SECONDS);
loop {
if now_in(session, Mode::Pongo)? {
Expand All @@ -714,7 +722,9 @@ fn into_pongo(executor: &Executor, session: &mut Session<Host>, pongo_image: &st
return Err(fail(format!("{RESTARTED} of starting Pongo")));
}
if Instant::now() > deadline {
break fail("Pongo did not appear on USB");
// The tool said it started Pongo: one refused open
// on the way is what went wrong.
break crate::usb_node::explain(name, &log, fail("Pongo did not appear on USB"));
}
std::thread::sleep(Duration::from_millis(250));
}
Expand Down Expand Up @@ -857,7 +867,7 @@ pub fn boot_ipados_with(executor: &Executor, session: &mut Session<Host>, overla
// palera1n goes on from its own Pongo start.
into_pongo(executor, session, &ours, Tool::Palera1n)?;
let second = vec![std::ffi::OsString::from("-l"), "-o".into(), overlay.as_os_str().to_owned(), "-k".into(), ours.into_os_string()];
run_palera1n_from(executor, session, Mode::Pongo, &second, IPADOS_SECONDS)
run_palera1n_from(executor, session, Mode::Pongo, &second, IPADOS_SECONDS).map(|_| ())
}

/// The launcher's loader, by the boot route: palera1n or checkra1n → Pongo →
Expand Down
1 change: 1 addition & 0 deletions crates/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub mod ready;
pub mod records;
pub mod recovery_mode;
pub mod usb_access;
mod usb_node;
pub mod helper_app;
mod held;
pub mod usbmux;
Expand Down
Loading
Loading