Skip to main content
Menu
Advisories

ZipStreamReader::extract() and ZipArchive::extract() apply Unix permissions through symlinks without confining the target

GHSA-4684-H2Q9-2VCPPublished Updated GitHub

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.0 and on audited HEAD 6f09b3064ec87c3a69ea9f3413f9f9638b4aa446.

Vulnerable code paths (source review):

  • src/read/stream.rs:89-109 (ZipStreamReader::extract implementation of visit_additional_metadata on #[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::extract visit_file for file.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_link target 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)

  1. Public entry point: zip::read::stream::ZipStreamReader::extract(directory).
  2. extract() canonicalizes directory and then calls self.visit(&mut Extractor(directory, IndexMap::new())).
  3. In the visit_file path 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).
  1. After all visit_file calls, visit_additional_metadata() is invoked for central directory metadata:
  • On Unix, visit_additional_metadata uses metadata.enclosed_name() and computes outpath = base.join(filepath).
  • It then calls std::fs::set_permissions(outpath, ...).
  1. Because outpath may 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:

  1. Use ZipWriter to create an archive with:
  • a symlink entry whose destination-relative path is inside dest and whose symlink target is an absolute or sibling path outside dest.
  • Unix mode metadata for that symlink entry so that ZipStreamFileMetadata::unix_mode() returns Some(mode).
  1. Call ZipStreamReader::extract(dest) in the consuming application.
  2. The code will reach src/read/stream.rs:104 and invoke fs::set_permissions on the extracted symlink path.

Expected behavior from source semantics:

  • The set_permissions call will follow the extracted symlink and apply the permission change to the symlink target outside dest, or fail with an Err that 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:

  1. In visit_additional_metadata (Unix), do not call fs::set_permissions() on paths that are symlinks.
  2. Or, before calling set_permissions, validate the extracted outpath using the same containment checks as ZipFile::safe_prepare_path so 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.

Affected versions

References

GitHub · zip-rs/zip2