Jan is an open source alternative to ChatGPT that runs 100% offline on your computer.
fix(llamacpp): pin the worker's working directory when listing devices
James Damd4436be60fdf
diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/commands.rs b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/commands.rsindex 5b641657e..48833af40 100644--- a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/commands.rs+++ b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/commands.rs@@ -337,6 +337,10 @@ pub async fn engine_devices<R: tauri::Runtime>() -> Result<Vec<EngineDevice>, String> {let exe = resolve_worker_exe(&app_handle)?;let mut cmd = tokio::process::Command::new(&exe);+ // Same pinned cwd as a serving worker: device listing loads the backends too.+ if let Some(dir) = worker::worker_cwd(&exe) {+ cmd.current_dir(dir);+ }cmd.arg("--list-devices").envs(envs);cmd.stdout(std::process::Stdio::piped());cmd.stderr(std::process::Stdio::piped());diff --git a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/worker.rs b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/worker.rsindex 0d7fa671b..7bd3054cf 100644--- a/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/worker.rs+++ b/src-tauri/plugins/tauri-plugin-llamacpp/src/engine/worker.rs@@ -450,6 +450,30 @@ impl WorkerHandle {}}+/// The working directory every worker spawn pins.+///+/// ggml's backend scan puts `fs::current_path()` in its search paths and+/// calls dl_load_library on every `ggml-*` filename match *before* reading+/// its score, so a planted module in an inherited cwd runs its DllMain /+/// constructor even if it is never selected -- and `best_score` is one+/// accumulator across all search paths, so a higher-scoring plant wins+/// outright rather than losing to path order. Pinning the cwd to the+/// worker's own directory, which is where the bundled modules already live,+/// removes the attacker-writable entry from that list.+///+/// Absolute only: `current_dir` also changes how the OS resolves a *relative*+/// program path, so doing this unconditionally would make a dev's+/// `--bin build/jan-llama-worker` be looked up inside `build/`. Every shipped+/// path is absolute (`resource_dir()`, or `sidecar_path` off+/// `current_exe`), so the hardening lands exactly where it matters.+pub(crate) fn worker_cwd(exe: &Path) -> Option<&Path> {+ if exe.is_absolute() {+ exe.parent()+ } else {+ None+ }+}+/// Spawns the worker and waits for its handshake line.#[allow(clippy::too_many_arguments)]pub async fn spawn(@@ -469,24 +493,8 @@ pub async fn spawn(// Without this the worker flashes a console window on Windows, the way// every other spawn in this plugin already avoids.jan_utils::system::setup_windows_process_flags(&mut cmd);- // ggml's backend scan puts `fs::current_path()` in its search paths and- // calls dl_load_library on every `ggml-*` filename match *before* reading- // its score, so a planted module in an inherited cwd runs its DllMain /- // constructor even if it is never selected -- and `best_score` is one- // accumulator across all search paths, so a higher-scoring plant wins- // outright rather than losing to path order. Pinning the cwd to the- // worker's own directory, which is where the bundled modules already live,- // removes the attacker-writable entry from that list.- //- // Absolute only: `current_dir` also changes how the OS resolves a *relative*- // program path, so doing this unconditionally would make a dev's- // `--bin build/jan-llama-worker` be looked up inside `build/`. Every shipped- // path is absolute (`resource_dir()`, or `sidecar_path` off- // `current_exe`), so the hardening lands exactly where it matters.- if exe.is_absolute() {- if let Some(dir) = exe.parent() {- cmd.current_dir(dir);- }+ if let Some(dir) = worker_cwd(exe) {+ cmd.current_dir(dir);}cmd.args(&args).envs(envs)@@ -633,6 +641,14 @@ pub fn sidecar_path() -> Option<PathBuf> {mod tests {use super::*;+ #[test]+ fn worker_cwd_is_the_exe_dir_only_for_absolute_paths() {+ let exe = std::env::temp_dir().join("bin").join("jan-llama-worker");+ assert_eq!(worker_cwd(&exe), exe.parent());+ assert!(worker_cwd(Path::new("build/jan-llama-worker")).is_none());+ assert!(worker_cwd(Path::new("jan-llama-worker")).is_none());+ }+// Restored with the marker lists intact: each entry came from a real crash// report, and a fault the classifier misses is a silent hang for the user.#[test]