Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions crates/pi-natives/src/path_identity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5974,7 +5974,11 @@ pub(crate) mod platform {
if fd < 0 {
return Err(security_code(&std::io::Error::last_os_error()));
}
let result = (|| {
// SAFETY: this function exclusively owns the freshly opened descriptor.
// Hash it directly; duplicating it shares the same file offset and adds no
// authority.
let mut file = unsafe { File::from_raw_fd(fd) };
(|| {
// SAFETY: zero is a valid initialized representation for fstat output.
let mut opened: libc::stat = unsafe { std::mem::zeroed() };
// SAFETY: fd is live and opened is writable.
Expand All @@ -5984,7 +5988,7 @@ pub(crate) mod platform {
if opened.st_mode & libc::S_IFMT != libc::S_IFREG {
return Ok(false);
}
let digest = digest_fd(fd)?;
let digest = digest_reader(&mut file).map_err(|_| "io_error")?;
// Linearize the pathname observation after descriptor hashing: the live name
// must still resolve no-follow to the descriptor whose metadata and bytes were
// checked above.
Expand All @@ -6011,10 +6015,7 @@ pub(crate) mod platform {
&& named.st_mode & libc::S_IFMT == libc::S_IFREG
&& named.st_dev == opened.st_dev
&& named.st_ino == opened.st_ino)
})();
// SAFETY: this function owns fd exactly once.
unsafe { libc::close(fd) };
result
})()
}

#[expect(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
### Fixed
- Pin an immutable parent identity for each managed remnant-reaping pass, while requiring genuine native no-follow and exact-identity validation for every removal. Refuse replacement directories instead of adopting them mid-pass, preserve failure accounting, and avoid redundant parent observations.
Original file line number Diff line number Diff line change
Expand Up @@ -883,43 +883,24 @@ async function hashReaperFile(pathname: string, expected: fs.BigIntStats): Promi
}
}

function exactReaperUnlinkSync(
pathname: string,
stat: fs.BigIntStats,
sha256: string,
allowHardLink: boolean,
): NativeExactUnlinkResult {
const parent = fs.lstatSync(path.dirname(pathname), { bigint: true });
if (!parent.isDirectory() || parent.isSymbolicLink()) return { ok: false, code: "parent_mismatch" };
const identity: NativeExactFileIdentity = {
dev: stat.dev,
ino: stat.ino,
nlink: stat.nlink,
parentDev: parent.dev,
parentIno: parent.ino,
size: stat.size,
mtimeNs: stat.mtimeNs,
sha256,
quarantineName: remnantReapQuarantineName(pathname),
...(allowHardLink ? { allowHardLink: true, requireHardLink: true } : {}),
};
return nativeSessionStorage().exactUnlinkDirect(pathname, identity);
interface ReaperParentIdentity {
readonly dev: bigint;
readonly ino: bigint;
}

async function exactReaperUnlink(
function exactReaperUnlinkSync(
pathname: string,
stat: fs.BigIntStats,
sha256: string,
allowHardLink: boolean,
): Promise<NativeExactUnlinkResult> {
const parent = await fsp.lstat(path.dirname(pathname), { bigint: true });
if (!parent.isDirectory() || parent.isSymbolicLink()) return { ok: false, code: "parent_mismatch" };
parentIdentity: ReaperParentIdentity,
): NativeExactUnlinkResult {
const identity: NativeExactFileIdentity = {
dev: stat.dev,
ino: stat.ino,
nlink: stat.nlink,
parentDev: parent.dev,
parentIno: parent.ino,
parentDev: parentIdentity.dev,
parentIno: parentIdentity.ino,
size: stat.size,
mtimeNs: stat.mtimeNs,
sha256,
Expand Down Expand Up @@ -966,6 +947,8 @@ export function reapScrubbedProtocolRemnantsSync(
const cutoff = Date.now() - minAgeMs;
let reaped = 0;
let failures = 0;
// Immutable expected identity only; native reopens and revalidates the parent per unlink.
let parentIdentity: ReaperParentIdentity | undefined;
for (const name of names) {
const terminalRemnant = SCRUBBED_REMNANT_PREFIXES.some(prefix => name.startsWith(prefix));
const replacementCandidate = REPLACEMENT_STAGING_NAME.test(name);
Expand All @@ -989,9 +972,29 @@ export function reapScrubbedProtocolRemnantsSync(
if (named.mtimeMs > cutoff) continue;
const sha256 = terminalRemnant ? EMPTY_FILE_SHA256 : hashReaperFileSync(pathname, named);
if (!sha256) continue;
const removed = exactReaperUnlinkSync(pathname, named, sha256, !terminalRemnant);
if (!parentIdentity) {
const parent = fs.lstatSync(path.dirname(pathname), { bigint: true });
if (!parent.isDirectory() || parent.isSymbolicLink()) {
failures += 1;
continue;
}
parentIdentity = { dev: parent.dev, ino: parent.ino };
}
const removed = exactReaperUnlinkSync(pathname, named, sha256, !terminalRemnant, parentIdentity);
if (removed.ok) reaped += 1;
else if (!isBenignReaperRace(removed)) failures += 1;
else {
let parentKindMismatch = false;
if (removed.code === "reparse_point") {
try {
const parent = fs.lstatSync(path.dirname(pathname), { bigint: true });
parentKindMismatch = !parent.isDirectory() || parent.isSymbolicLink();
} catch (error) {
// Preserve lookup-error accounting without changing or retrying the native result.
if (!isEnoent(error)) failures += 1;
}
}
if (parentKindMismatch || !isBenignReaperRace(removed)) failures += 1;
}
} catch (error) {
if (!isEnoent(error)) failures += 1;
}
Expand Down Expand Up @@ -1025,6 +1028,7 @@ export async function reapScrubbedProtocolRemnants(
let reaped = 0;
let failures = 0;
let scanned = 0;
let parentIdentity: ReaperParentIdentity | undefined;
for (const name of names) {
const terminalRemnant = SCRUBBED_REMNANT_PREFIXES.some(prefix => name.startsWith(prefix));
const replacementCandidate = REPLACEMENT_STAGING_NAME.test(name);
Expand All @@ -1047,9 +1051,29 @@ export async function reapScrubbedProtocolRemnants(
if (named.mtimeMs > cutoff) continue;
const sha256 = terminalRemnant ? EMPTY_FILE_SHA256 : await hashReaperFile(pathname, named);
if (!sha256) continue;
const removed = await exactReaperUnlink(pathname, named, sha256, !terminalRemnant);
if (!parentIdentity) {
const parent = await fsp.lstat(path.dirname(pathname), { bigint: true });
if (!parent.isDirectory() || parent.isSymbolicLink()) {
failures += 1;
continue;
}
parentIdentity = { dev: parent.dev, ino: parent.ino };
}
const removed = await exactReaperUnlinkSync(pathname, named, sha256, !terminalRemnant, parentIdentity);
if (removed.ok) reaped += 1;
else if (!isBenignReaperRace(removed)) failures += 1;
else {
let parentKindMismatch = false;
if (removed.code === "reparse_point") {
try {
const parent = await fsp.lstat(path.dirname(pathname), { bigint: true });
parentKindMismatch = !parent.isDirectory() || parent.isSymbolicLink();
} catch (error) {
// Preserve lookup-error accounting without changing or retrying the native result.
if (!isEnoent(error)) failures += 1;
}
}
if (parentKindMismatch || !isBenignReaperRace(removed)) failures += 1;
}
} catch (error) {
if (!isEnoent(error)) failures += 1;
}
Expand Down
Loading
Loading