Unpack app tarballs without setuid modes or link entries

The tarball installer preserved archive modes and unpacked every entry type,
so a package could install setuid/setgid files and symlink or hardlink
entries; only bin/<app.id> was validated afterwards. Extraction now keeps the
default mode sanitising and accepts only regular files and directories, and a
regression test sends a symlink entry.
This commit is contained in:
Dylan M. Taylor committed 2026-11-14 14:21:44 -05:00
1 parent ba56c4587f
commit d14f43ed58
1 file changed
+31 -3
+31 -3
View File
@@ -1935,7 +1935,9 @@ mod tree {
std::fs::create_dir_all(staging).map_err(|e| format!("couldn't create {} ({e})", staging.display()))?;
let gz = std::fs::File::open(file).map_err(|e| e.to_string())?;
let mut archive = tar::Archive::new(flate2::read::GzDecoder::new(gz));
archive.set_preserve_permissions(true);
// Keep the default mode sanitising: an archive must not install setuid/setgid files or
// control the exact modes under /usr/local (audit: launcher #3).
archive.set_preserve_permissions(false);
unpack_entries(&mut archive, staging)?;
let top = single_top_folder(staging)?;
if top.join("bin").join(app.id).is_file() {
@@ -1987,10 +1989,15 @@ mod tree {
if total_unpacked > MAX_UNPACKED_BYTES {
return Err("archive exceeds maximum unpacked size".into());
}
if entry.header().entry_type().is_dir() {
let kind = entry.header().entry_type();
if kind.is_dir() {
directories.push(entry);
} else {
} else if kind.is_file() {
entry.unpack_in(staging).map_err(|e| format!("couldn't unpack archive entry ({e})"))?;
} else {
// Symlinks and hardlinks could redirect a later write outside the staging folder,
// and device/FIFO entries have no place in an app package (audit: launcher #3).
return Err("archive contains a link or special file".into());
}
}
// Delay directory entries until child entries are written so restrictive modes do not break child extraction.
@@ -3162,6 +3169,27 @@ mod tests {
assert!(tree::unpack(app, &bad, &dir.path().join("staging")).is_err());
}
#[cfg(unix)]
#[test]
fn tree_unpack_rejects_link_entries() {
let app = CraftApp::by_id("wordcraft").unwrap();
let dir = tempfile::tempdir().unwrap();
let file = dir.path().join("link.tar.gz");
{
let enc = flate2::write::GzEncoder::new(std::fs::File::create(&file).unwrap(), flate2::Compression::default());
let mut b = tar::Builder::new(enc);
let mut h = tar::Header::new_gnu();
h.set_entry_type(tar::EntryType::Symlink);
h.set_size(0);
h.set_mode(0o777);
h.set_link_name("/etc/passwd").unwrap();
h.set_cksum();
b.append_data(&mut h, "wordcraft-0.3.0-freebsd-x86_64/bin/wordcraft", &[][..]).unwrap();
b.into_inner().unwrap().finish().unwrap();
}
assert!(tree::unpack(app, &file, &dir.path().join("staging")).is_err());
}
#[cfg(unix)]
#[test]
fn tree_files_does_not_traverse_directory_symlinks() {