Skip to content

Commit 2174cac

Browse files
authored
Reject GNU tar long-name records that end before their padding (#1402)
A GNU long-name or long-link record whose text was complete but whose 512-byte padding was cut off, or whose padded size overflowed, stopped the listing without saying why, so the engine installer could still write the files listed before it and report success. Both cases now count as a cut-short GNU record, and the installer refuses the archive before writing anything.
1 parent a80cc82 commit 2174cac

4 files changed

Lines changed: 92 additions & 3 deletions

File tree

‎crates/engines/src/install.rs‎

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -625,6 +625,64 @@ mod tests {
625625
assert_eq!(err.code, ErrorCode::GarbledOutput);
626626
}
627627

628+
fn assert_gnu_record_extent_refused(kind: u8, overflow: bool) {
629+
use craft_anything_testkit::firmware as fx;
630+
let body = b"next\0";
631+
let mut tar = fx::tar_entries(&[("pkg/bin/tool", b'0', b"replacement", ""), ("././@LongLink", kind, body, "")]);
632+
if overflow {
633+
let header = &mut tar[1024..1536];
634+
header[124..136].fill(0);
635+
header[124] = 0x80;
636+
header[128..136].copy_from_slice(&u64::MAX.to_be_bytes());
637+
header[148..156].fill(b' ');
638+
let checksum: usize = header.iter().map(|byte| usize::from(*byte)).sum();
639+
header[148..156].copy_from_slice(format!("{checksum:06o}\0 ").as_bytes());
640+
} else {
641+
tar.truncate(1536 + body.len());
642+
}
643+
let listing = craft_anything_formats::tarcpio::tar(&tar, 100).unwrap();
644+
assert_eq!(listing.members.len(), 1);
645+
assert!(!listing.complete);
646+
let dir = tempfile::tempdir().unwrap();
647+
std::fs::create_dir(dir.path().join("bin")).unwrap();
648+
let installed = dir.path().join("bin/tool");
649+
std::fs::write(&installed, b"installed").unwrap();
650+
let result = unpack_tar(&tar, dir.path(), Some("pkg/"), None, &|_| {});
651+
let retained = std::fs::read(&installed).unwrap();
652+
assert!(result.is_err() && retained == b"installed", "{kind:?}, overflow: {overflow}, result: {result:?}, retained: {retained:?}");
653+
assert_eq!(result.unwrap_err().code, ErrorCode::GarbledOutput);
654+
}
655+
656+
#[test]
657+
fn tar_metadata_padding_guard_refuses_short_long_name_padding() {
658+
assert_gnu_record_extent_refused(b'L', false);
659+
}
660+
661+
#[test]
662+
fn tar_metadata_padding_guard_refuses_short_long_link_padding() {
663+
assert_gnu_record_extent_refused(b'K', false);
664+
}
665+
666+
#[test]
667+
fn tar_metadata_padding_guard_refuses_overflowing_long_name_extent() {
668+
assert_gnu_record_extent_refused(b'L', true);
669+
}
670+
671+
#[test]
672+
fn tar_metadata_padding_guard_refuses_overflowing_long_link_extent() {
673+
assert_gnu_record_extent_refused(b'K', true);
674+
}
675+
676+
#[test]
677+
fn tar_metadata_padding_guard_keeps_regular_member_compatibility() {
678+
use craft_anything_testkit::firmware as fx;
679+
let mut tar = fx::tar_entries(&[("pkg/bin/tool", b'0', b"tool", "")]);
680+
tar.truncate(512 + 4);
681+
let dir = tempfile::tempdir().unwrap();
682+
assert_eq!(unpack_tar(&tar, dir.path(), Some("pkg/"), None, &|_| {}).unwrap(), 1);
683+
assert_eq!(std::fs::read(dir.path().join("bin/tool")).unwrap(), b"tool");
684+
}
685+
628686
#[test]
629687
fn unpacks_tar_xz() {
630688
// pkg/ (dir), pkg/bin/tool (file), pkg/evil -> /etc/passwd (symlink), ../escape (file).

‎crates/formats/src/tarcpio.rs‎

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,16 +162,19 @@ pub fn tar(data: &[u8], max_members: usize) -> Option<Listing> {
162162
}
163163
let data_off = pos + BLOCK;
164164
let Some(next) = round_up(size, BLOCK as u64).and_then(|s| u64::try_from(data_off).ok()?.checked_add(s)).and_then(|n| usize::try_from(n).ok()) else {
165+
if matches!(typeflag, b'L' | b'K') {
166+
out.stop_reason = Some(StopReason::GnuMetadataTruncated);
167+
}
165168
out.warnings.push(format!("size overflows at {pos:#x}"));
166169
out.length = pos as u64;
167170
break;
168171
};
169172
let body = r.slice(data_off, usize::try_from(size).unwrap_or(usize::MAX));
170173
match typeflag {
171174
b'L' | b'K' => {
172-
let Some(body) = body else {
175+
let Some(body) = body.filter(|_| next <= data.len()) else {
173176
out.stop_reason = Some(StopReason::GnuMetadataTruncated);
174-
out.warnings.push(format!("GNU long-name data ends early at {pos:#x}"));
177+
out.warnings.push(format!("GNU long-name record ends early at {pos:#x}"));
175178
out.length = data.len() as u64;
176179
break;
177180
};
@@ -600,6 +603,30 @@ mod tests {
600603
assert_eq!(cpio_listing.stop_reason, None);
601604
}
602605

606+
#[test]
607+
fn gnu_metadata_requires_its_complete_padded_record() {
608+
let body = b"next\0";
609+
for kind in *b"LK" {
610+
let data = fx::tar_entries(&[
611+
("before", b'0', b"kept", ""),
612+
("././@LongLink", kind, body, ""),
613+
("fallback", if kind == b'L' { b'0' } else { b'2' }, b"", "fallback"),
614+
]);
615+
for padding in [0, 1, BLOCK - body.len() - 1] {
616+
let short = &data[..3 * BLOCK + body.len() + padding];
617+
let listing = tar(short, 10).unwrap();
618+
assert_eq!(listing.stop_reason, Some(StopReason::GnuMetadataTruncated));
619+
assert!(!listing.complete);
620+
assert_eq!(listing.members.len(), 1);
621+
assert_eq!(member_data(short, &listing.members[0]).unwrap(), b"kept");
622+
}
623+
let listing = tar(&data, 10).unwrap();
624+
assert!(listing.complete);
625+
assert_eq!(listing.stop_reason, None);
626+
assert_eq!(listing.members.len(), 2);
627+
}
628+
}
629+
603630
#[test]
604631
fn walks_tar() {
605632
let t = fx::tar(&[("etc/passwd", b"root:x:0:0\n"), ("bin/", b""), ("long/".repeat(30).as_str(), b"deep")]);

‎docs/ROADMAP.md‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1384,6 +1384,10 @@ Owner: open · Status: in progress
13841384
— claimed: Afloat16, branch bugfix/r3.12-tar-install-stop-reasons-20261109
13851385
Source `3e588c55` passed [formats and engines tests, formatting, clippy and L0 gates](https://github.com/Afloat16/claude-mem/actions/runs/42649935938/job/129273248608).
13861386
The [unchanged-production baseline](https://github.com/Afloat16/claude-mem/actions/runs/42648642576/job/129268658248) reproduces four failed guards while two compatibility controls pass.
1387+
- [x] Require the complete padded extent of GNU tar name and link records before allowing installation, including extents that overflow checked offsets; preserve ordinary member reads without trailing padding.
1388+
— claimed: Afloat16, branch bugfix/r3.12-gnu-tar-padded-records-20261109
1389+
Exact source `25c45ef0` passed [formats and engines tests (1,200 passed, one existing network test ignored), fmt, clippy and L0 gates](https://github.com/Afloat16/claude-mem/actions/runs/42659466852/job/129306793039).
1390+
The [unchanged-production baseline](https://github.com/Afloat16/claude-mem/actions/runs/42659466852/job/129306793039) reproduces four failed guards while seven compatibility controls pass.
13871391
- [x] Extract legacy PyInstaller PYZ data as raw files and report modern namespace packages without fabricating bytecode.
13881392
— claimed: Afloat16, branch bugfix/r3.12-pyz-member-kinds
13891393
- [x] Keep PyInstaller CArchive member reads before the table and cookie, including signed files with trailing bytes.

‎docs/STATUS.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ workspace clippy, layers, fmt and the secret scan of all history were clean. Chi
133133
| Android resource locale groups | Explicit script, variant and numbering-system subtags distinguish resource groups, while region-only locales retain their existing form. | Resource and binary XML parser tests including traditional and simplified Chinese resource configurations. |
134134
| Swift optional function types (`apple_swift_symbols_demangle`) | The type inside `?` or `!` sugar goes in parentheses when it is a function type, async and throwing ones included, or a protocol composition, as `swift-demangle` prints it, so `(() -> Swift.Int)?` no longer reads as `() -> Swift.Int?`. Other wrapped types print as before | Integration test on four symbols from macOS system binaries and six constructed ones checked with `swift-demangle`. 133,643 distinct Swift symbols from the system apps, frameworks and `/usr/libexec` of macOS 15.7 compared with `swift-demangle`: the 506 decoded ones with an optional function type now put it in parentheses (413 match exactly; 93 differ only in the `(extension in …)` prefix, an `_:` label or `__allocating_init`), and no symbol that matched before changed |
135135
| Engine | 300 engine tools (320 with the AI and setup layers') across native, managed, Java, Android, Apple, JS/Electron, web, firmware, packets, databases, game archives and assets; see [tools.md](tools.md) and [tool-status.md](tool-status.md). A saved result can be narrowed without running again (`core_evidence_browse` keeps the rows matching given field values, cut to chosen fields) | A functional audit that ran every tool on real inputs on Windows, plus 600+ unit and integration tests |
136-
| Tar engine installation | GNU name or link metadata that stops the archive walk refuses installation before any member is written. Earlier members remain available to inspection, and readable tar members without an end marker still unpack. | Four oversized/truncated name/link fixtures preserve an existing file; bounded names and missing end markers remain accepted. Exact source `3e588c55` passed [formats and engines tests, clippy and L0 gates](https://github.com/Afloat16/claude-mem/actions/runs/42649935938/job/129273248608). |
136+
| Tar engine installation | GNU name or link metadata with a truncated body, missing block padding, overflowing padded extent or an overlong identity refuses installation before any member is written. Earlier members remain available to inspection, and readable regular members without trailing block padding or an end marker still unpack. | Oversized/truncated identities and short/overflowing GNU records preserve an existing file; valid bounded names and ordinary members remain accepted. Exact source `25c45ef0` passed [formats and engines tests (1,200 passed, one existing network test ignored), fmt, clippy and L0 gates](https://github.com/Afloat16/claude-mem/actions/runs/42659466852/job/129306793039). The same run reproduces four failures and seven passing controls with production code unchanged. |
137137
| Tool argument correction | Shared parameter parsing keeps schema types strict and reports the rejected value's JSON Pointer when known, the original type error and guidance to correct the call. Unknown locations are null; a root failure uses the empty pointer | Parser regressions cover string/array mismatches, nested indices, escaped keys, oversized paths, enum locations, defaults and strict fields. Shared engine dispatch is checked for Agent, MCP and CLI callers: rejected arrays create no record, and corrected input creates one. No live Qwen or Ollama build was run for this correction |
138138
| JSONC registration comments | A bare carriage return ends a line comment, so registrations on the next line remain visible when settings are opened or edited. | Seven JSONC module tests on Linux, including a comment before the root and one inside it. |
139139
| German UI | German (`de`, informal "du") in Settings → General → Language; System picks it for a German OS language. The catalog has the same keys as the Chinese ones. Drafted with an AI, not yet reviewed by a native speaker. Requests such as "Ein einfaches Snake-Spiel" or "Eine Website für …" keep their project kind. See [localisation](localization.md). | Catalog and locale tests, key-set comparison with the Chinese catalogs, the isolated language-switch test, a kind-detection test; fmt, clippy, layers, locks and secrets are clean. Not checked: how the German texts look in the running window (length, line breaks), and the `de.tsv` wording by a native speaker. |

0 commit comments

Comments
 (0)