ZipStreamReader::extract() and ZipArchive::extract() apply Unix permissions through symlinks without confining the target
Description
ZipStreamReader::extract() applies Unix permissions through extracted symlinks without validating the symlink target stays within the destination
Details
Affected versions and vulnerable location
- Confirmed present on shipped release
v8.6.0and on audited HEAD6f09b3064ec87c3a69ea9f3413f9f9638b4aa446.
Vulnerable code paths (source review):
src/read/stream.rs:89-109(ZipStreamReader::extractimplementation ofvisit_additional_metadataon#[cfg(unix)]):- Computes
filepath = metadata.enclosed_name()?. - Computes
outpath = base.join(filepath). - Calls
fs::set_permissions(outpath, ...)without re-validating containment or symlink target safety. src/read/stream.rs:63-86(ZipStreamReader::extractvisit_fileforfile.is_symlink()):- Calls
file.safe_prepare_path(base, &mut outpath, ...). - Creates the symlink via
make_symlink(&outpath, &target, ...). src/read.rs:857-968(ZipFile::safe_prepare_path):- Validates existing filesystem symlink path components by reading each symlink’s
read_linktarget and checking it is inherently safe with respect to the canonicalized base. - Does not validate the attacker-controlled target when the symlink itself does not yet exist (the last component can break out on
NotFound), so newly created symlink targets can remain unchecked until a later sink.
Reachability trace (verified by source)
- Public entry point:
zip::read::stream::ZipStreamReader::extract(directory). extract()canonicalizesdirectoryand then callsself.visit(&mut Extractor(directory, IndexMap::new())).- In the
visit_filepath for each ZIP entry:
ZipFile::safe_prepare_path(&base, &mut outpath, ...)is called.- If the entry is a symlink, the code reads the symlink target bytes and calls
make_symlink(&outpath, &target, &shared).
- After all
visit_filecalls,visit_additional_metadata()is invoked for central directory metadata:
- On Unix,
visit_additional_metadatausesmetadata.enclosed_name()and computesoutpath = base.join(filepath). - It then calls
std::fs::set_permissions(outpath, ...).
- Because
outpathmay refer to an extracted symlink path,set_permissions()follows the symlink and applies mode changes to the symlink target, even if that target is outside the destination directory.
PoC
Reproduction (source-only)
No runtime execution was performed in this environment.
Deterministic construction to reproduce the code path:
- Use
ZipWriterto create an archive with:
- a symlink entry whose destination-relative path is inside
destand whose symlink target is an absolute or sibling path outsidedest. - Unix mode metadata for that symlink entry so that
ZipStreamFileMetadata::unix_mode()returnsSome(mode).
- Call
ZipStreamReader::extract(dest)in the consuming application. - The code will reach
src/read/stream.rs:104and invokefs::set_permissionson the extracted symlink path.
Expected behavior from source semantics:
- The
set_permissionscall will follow the extracted symlink and apply the permission change to the symlink target outsidedest, or fail with anErrthat aborts extraction.
Impact
Attacker model and impact
- Attacker controls the ZIP archive contents (including symlink entries and their Unix mode metadata).
- Victim application calls
zip::read::stream::ZipStreamReader::extract(dest)on attacker-controlled archives. - Impact:
- Integrity: attempts to change permissions (chmod) on filesystem objects outside the destination directory via symlink following.
- Availability: if the extraction process lacks permissions to change the external target, extraction can fail after partially extracting earlier entries (library returns
Err).
Practical limitation: the process running extraction still needs OS permissions over the external target path for the chmod to succeed.
Severity and CVSS (argued)
Suggested CVSS v3.1 vector: CVSS:3.1/AV:N/AC:L/PR:N/UI:N/S:U/C:N/I:H/A:H.
Rationale:
- No network control is required by the library itself; assume attacker reaches the victim via supplying untrusted archives (typical ZIP ingestion).
- Low complexity: crafting a ZIP with a symlink entry and Unix mode metadata.
- No privileges required beyond the victim extracting the attacker-controlled archive.
- Integrity and availability impact are high because the chmod can target arbitrary external filesystem objects reachable by the process identity, and extraction can fail (returning
Err) if chmod fails.
Summary
The stream extractor’s metadata pass uses fs::set_permissions() on the extracted entry path without re-running the same containment logic (safe_prepare_path) that protects write sinks. On Unix, set_permissions() follows symlinks, so a crafted archive can cause permission changes on a symlink target outside the destination directory.
Suggested fix
Ensure the metadata pass enforces the same symlink-target containment policy as the main extraction write sinks.
Concrete options:
- In
visit_additional_metadata(Unix), do not callfs::set_permissions()on paths that are symlinks. - Or, before calling
set_permissions, validate the extractedoutpathusing the same containment checks asZipFile::safe_prepare_pathso that existing symlink targets are re-checked relative to the canonicalized destination.
The lowest-regression approach is to factor the symlink-target containment check used inside ZipFile::safe_prepare_path into a helper and call it from visit_additional_metadata for the computed outpath.
How I found it and a note on tooling
Source-only verification against the local checkout in .scratch/zip2: I traced ZipStreamReader::extract() to the call to visit_additional_metadata, then verified that this metadata pass computes outpath and directly calls fs::set_permissions without any call to ZipFile::safe_prepare_path or any containment validation.
AI tooling
I used AI assistance for the code audit and for drafting this report. I manually verified the finding against the project's source at the location cited above before reporting it, and the severity and impact assessment are my own.