Repository navigation
Conversation
CPython's zipfile.extractall writes each symlink member's payload (the link-target text) as a regular file, so every lib*.so link in an upstream sd.cpp release zip landed as a 19-byte text file — sd-cli then dies with 'error while loading shared libraries: libwebpmux.so.3: file too short' and MiniMax-H3 reports video.load_failed (unslothai#9268). _safe_extractall now recreates symlink members after extraction, with the link target resolved against the same containment guard the member paths use: a link escaping the install dir raises instead of writing outside it.
|
Confirmed _safe_extractall in studio/install_sd_cpp_prebuilt.py still calls plain extractall, so the lib*.so links in the Linux prebuilt zips land as text files and sd-cli cannot start. Will get this reviewed. |
Extraction merges into the managed tree, so install() re-extracts over the links a previous install wrote. Resolving the member destination followed those links, so extractall wrote the link text through them into the real library and the restore loop then replaced that library with a link to itself. A second install of the same bundle turned every lib*.so in the ROCm archive into a cycle and sd-cli exited 127; a third could no longer extract at all. - keep the lexical member destination, resolving only for the containment check - skip symlink members in extractall so their payloads are never written - validate every member path and link target before the first write - reject absolute, drive-relative, empty, NUL-bearing, oversized and cyclic targets - require create_system == 3 before reading external_attr as a Unix mode - fall back to the flattened member when symlink_to has no privilege (Windows) - compare binary paths lexically now that the tree can hold symlinks
|
Thanks for this, the diagnosis in #9268 is exactly right and the first install is fixed by it. I pushed a follow-up commit to your branch because re-extracting over the tree the fix itself created was destroying it. What I hit
Measured on the exact archive from the issue,
That last row is the one that worried me most. What the follow-up changes
Chained links keep working, which matters because upstream ships them: Verification
Beyond the suite I ran a simulation matrix comparing the merge base, this PR as submitted, and the follow-up across 69 checks: asset selection over 80 platform x accelerator x arch combinations (identical in all three, nothing about selection changed), every real release archive, repeated and upgrade installs, 18 adversarial archives, an install directory under a symlinked parent, the downstream binary sweep, and Backwards compatibility: the Linux CPU, Linux Vulkan and macOS arm64 assets carry no symlink members at all, and their extracted manifests are byte identical between the merge base and this branch, so those installs are untouched. Only the Linux ROCm asset has symlinks, which is why this surfaced on an AMD host. Existing installs are covered too: a flattened tree from an older Unsloth is repaired, and a tree a user repaired by hand following the issue survives. The |
The pre-extraction pass validates every member before the first write, but it runs while no archive link exists yet. An earlier member can then redirect a later one's parent: a -> . followed by a/out -> ../outside_dir makes a/out/victim resolve outside the install dir, so the unlink and symlink_to replaced a file the archive never owned. Re-resolve the parent and the target immediately before each link is created, and refuse a parent that no longer resolves to itself.
The creation-time re-check refused any parent that did not resolve to itself, which also refused a managed tree that symlinks one of its own subdirectories, with no security gain: a write through a link that stays inside the install dir cannot escape, and a parent pointing outside is already refused by the member-path check. Ask both the parent and the target whether they still resolve inside the install dir. The redirected-parent archive is still rejected, one member earlier than before.
Three holes reachable through the merge that install() performs: extractall opens each destination "wb" and follows a link a previous bundle left there, so a name shipped as a link by one bundle and as a regular file by the next lost its member's bytes into the file the old link pointed at. An accelerator switch lands exactly there, since upstream ships lib*.so as links and the mirror ships plain copies. Stale links at any destination this archive writes now go first. The cycle check only walked archive-to-archive edges, so an archive edge could close a loop with a link the previous bundle left. The created link is now resolved and undone if it loops, so the retry does not meet the same pair. A symlink member at the install-record or ownership-marker path made _write_install_record follow it and overwrite its target while the record still read back, reporting a corrupted install as successful. Those paths are now refused.
Making the binary comparison fully lexical fixed a bundle that ships sd-cli as a symlink, but broke a managed tree that symlinks one of its own subdirectories: rglob reports the extracted binary under the real directory while the member path still spells it through the link, so the sweep deleted the executable the bundle had just supplied and finalisation then found no sd-cli, on every retry. Both halves are needed, so compare on the parent resolved plus the final component untouched, on both sides.
The creation-time check stopped the outside write, but it fired half way through: extractall had already replaced the working binary and the first link had been created before the second one was rejected, so a refused archive left a mixed tree and retrying it repeated the same partial replacement. No member may sit under a directory the same archive turns into a link, and that is decidable from the member list alone. Refusing it in preflight puts the rejection back before the first write, so the archive changes nothing at all.
Path.resolve(strict = True) raises RuntimeError on a loop up to 3.12 and OSError only from 3.13, so the except OSError branch never ran on the versions the repo still supports: the loop escaped with the wrong message and, worse, the half this archive had just created was left on disk for the retry to meet again. os.stat reports ELOOP as an OSError on 3.9 through 3.13 alike, so switch to the raw errno. Checked on all five interpreters: the cycle is rejected and our link removed on every one. Also lift the check out of the symlink_to try, so an unrelated stat error cannot be mistaken for a missing symlink privilege.
The list tracking every destination had drifted to holding tuples while still annotated as paths, and the ancestor walk compared with str.startswith, which treats /a/bc as sitting under /a/b. Neither could bite today, since the containment check above keeps every destination under base, but both are traps for the next reader. Normalise once where the destination is built, then compare Paths throughout and walk dest.parents instead of slicing strings.
A member path is not where its link lands. A directory link a previous bundle left behind redirects it, and every preflight check here compared names instead. alias -> . makes a member named alias/<record> land on the install record itself, so the reserved-path comparison missed it, symlink_to created the link at the real record path, and _write_install_record followed it and overwrote sd-cli with JSON while the record still read back as a valid install. alias -> real makes members alias/a -> b and real/b -> a one cycle, not two unrelated lexical paths, so the walk terminated and an ELOOP layout installed clean. The directory-collision check ran after extractall, so an archive refused for it had already replaced the working binary. _plan_key resolves the parents and leaves the leaf alone, skipping links this archive replaces since they are gone before anything is written, and the reserved, collision and cycle checks all key on it. Everything is decided before the first write.
a -> a/x never reaches a second node, so exact-node repetition never fires, but resolving a walks a again and the install lands with an ELOOP library the loader cannot read and the runtime probe would discard and refetch on every load. Anything under the link it came from is a loop. Refusing to flatten off Windows was still being decided at creation time, so a filesystem that rejects symlinks (EPERM on exFAT and on SMB without unix extensions) took the new binary before it raised, and the upgrade lost the install it was replacing. Probe once, alongside the other preflight checks, so an upgrade that cannot finish leaves the previous one runnable.
extractall DROPS a .. component instead of cancelling the one before it, so 'a/../victim' is 'a/victim' to it. The guard normalised the name first and cleared a path zipfile never writes to, and with a link at 'a' left by a previous bundle the member landed outside the install tree entirely. No release ships '..', so refuse it. Two link members can also point through each other's directories: a -> b/x with b -> a/y is one cycle, but neither link exists on disk while the graph is built, so following only on-disk parents missed it and an ELOOP layout installed clean. Parent resolution now follows the archive's own links, bounded at the depth the loader gives up at, and a runaway is the cycle it describes. The three sweep tests spelled the binary the POSIX way, so they failed on a Windows runner where _binary_names() spells it sd-cli.exe. They ask the host now.
…ts on it The ownership marker is already there before extraction on any root Studio owns, so sd-cli -> .unsloth-studio-owned resolves to a file, _locate_sd_cli reports the empty marker as the executable, and the install claims success. Only the destination was being compared against the reserved paths; the target is now resolved the same way, so a chain that ends on one is refused too.
|
Codex Review: Didn't find any major issues. Keep them coming! Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
@codex review |
|
Codex Review: Didn't find any major issues. Chef's kiss. Reviewed commit: ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
If Codex has suggestions, it will comment; otherwise it will react with 👍. Codex can also answer questions or update the PR. Try commenting "@codex address that feedback". |
|
Closing this in favour of #9270, which is the original report and the original fix for #9268, and which I have brought up to parity rather than landing a second PR over it. Nothing here is lost. The hardening from this branch is now on #9270: the pre-write ordering, the Thanks for the work on this, it is what made the failure modes in the naive fix visible, and the reinstall case in particular was worth catching before this shipped. |
|
Validated this on a real gfx1151 ROCm host (AMD DevLab runner, Linux, HIP 7.13.99004) against the actual release asset rather than a synthetic zip. Confirmed. Before running any of this PR's code I checked the three Linux assets of
So the defect is specific to the ROCm build, which is consistent with #9268 being reported from ROCm hardware. Then installed the real asset (sha256
The merge base reproduces the reported failure exactly: and the flattened members are the shape the issue describes, for example Backend suite on the same host: 116 passed at the base, 148 passed at the head. Two notes for reviewers, neither blocking:
|
Summary
What
_safe_extractallinstudio/install_sd_cpp_prebuilt.pynow recreates symlink members afterzipfile.extractall: members carrying the Unix symlink mode inexternal_attrare re-created withsymlink_to, and the link target is resolved through the same containment guard the member paths already use — a link whose target escapes the install dir raisesunsafe symlink in archiveinstead of writing outside it.Why
#9268 — CPython's
zipfile.extractalldoes not restore symlinks: it writes each symlink member's data (the link-target text) as a regular file. Upstream sd.cpp release zips shiplibggml*.so,libwebp*.so,libwebpmux*.so,libsharpyuv*.soas symlinks, so after installlibwebpmux.so.3is a 19-byte file containing the textlibwebpmux.so.3.1.2, andsd-clifails witherror while loading shared libraries: ... file too short, surfacing asvideo.load_failed: stable-diffusion.cpp could not be installed or started for MiniMax-H3.Same shape as the reporter's suggested fix, with the containment check kept in one loop and applied to both member paths and link targets.
Testing (
studio/backend/tests/test_sd_cpp_install.py):test_safe_extractall_restores_symlink_members: builds a zip with a real member plus a symlink-mode member (the exact shape CPython writes when zipping a symlink), extracts, assertslibwebpmux.so.3is_symlink(), itsreadlink()names the real library, and its size equals the real file's — the pre-fix behavior was a tiny text file. Runs on CI's Linux runners; skips on hosts where symlink creation needs privilege (Windows non-dev-mode — my Windows host skips it locally, the same CI-gating pattern as the whisper/llama installer suites).test_safe_extractall_rejects_escaping_symlink: a../../outside.solink target raisesunsafe symlinkand nothing lands outside the install dir — this one runs everywhere (the check precedes any link creation) and fails on the pre-fix code (no such rejection existed).safe_extractalltraversal/normal-members tests unchanged: 3 passed + 1 skipped locally.Fixes #9268