diff --git a/src/domain/elfSymbol.ts b/src/domain/elfSymbol.ts index ba1c4138589..5fdaa3f2442 100644 --- a/src/domain/elfSymbol.ts +++ b/src/domain/elfSymbol.ts @@ -73,6 +73,17 @@ export const readElfSymbolFieldsAt = ( }; }; +/** Decode only the `st_value` word of a symbol already bounds-checked by `readElfSymbolFieldsAt`. */ +export const readElfSymbolValueAt = ( + bytes: Uint8Array, + offset: number, + bits: 32 | 64, + littleEndian: boolean, +): bigint => + bits === 64 + ? readUint64At(bytes, offset + 8, littleEndian) + : BigInt(readUint32At(bytes, offset + 4, littleEndian)); + /** Decode one fixed ELF32/ELF64 symbol record without slicing its bytes. */ export const readElfSymbolAt = ( bytes: Uint8Array, diff --git a/src/native/elf/ElfDependencyParser.ts b/src/native/elf/ElfDependencyParser.ts index 5deaa8bfd45..397d8c2e64b 100644 --- a/src/native/elf/ElfDependencyParser.ts +++ b/src/native/elf/ElfDependencyParser.ts @@ -32,7 +32,7 @@ import { import { ELF_SYMBOL_TRUNCATED_MESSAGE, elfSymbolSize, - readElfSymbolAt, + readElfSymbolFieldsAt, } from "../../domain/elfSymbol.js"; import { readElfGnuHashSymbolCount, @@ -1736,7 +1736,7 @@ const parseImports = ( : new BinaryReader(versymBytes, reader.little, "elf"); for (let index = 1; index < symbolCount; index += 1) { const at = dynsym.offset + index * entrySize; - const parsed = readElfSymbolAt(bytes, at, bits, reader.little); + const parsed = readElfSymbolFieldsAt(bytes, at, bits, reader.little); if (parsed === null) { issues.push({ code: "malformed_entry", diff --git a/src/native/elf/ElfUnwindTables.ts b/src/native/elf/ElfUnwindTables.ts index 2a879a77ec4..346ea72f78a 100644 --- a/src/native/elf/ElfUnwindTables.ts +++ b/src/native/elf/ElfUnwindTables.ts @@ -12,7 +12,11 @@ import { } from "../../resources/index.js"; import { decodeLimit } from "../../domain/parseFailure.js"; import { readElfDynamicEntry } from "../../domain/elfDynamicEntry.js"; -import { elfSymbolSize, readElfSymbolAt } from "../../domain/elfSymbol.js"; +import { + elfSymbolSize, + readElfSymbolFieldsAt, + readElfSymbolValueAt, +} from "../../domain/elfSymbol.js"; import { elfStringTableText, observeElfStringTableEntry, @@ -1890,7 +1894,7 @@ const scanSymbols = ( for (let index = 0; index < count; index += 1) { assertArtifactReadActive(signal); const at = start + index * width; - const symbol = readElfSymbolAt( + const symbol = readElfSymbolFieldsAt( bytes, at, directory.bits, @@ -1913,7 +1917,12 @@ const scanSymbols = ( continue; if (sectionIndex === SHN_UNDEF) continue; if (sectionIndex >= SHN_LORESERVE && sectionIndex !== SHN_ABS) continue; - let address = symbol.value; + let address = readElfSymbolValueAt( + bytes, + at, + directory.bits, + directory.little, + ); if (relocatable && sectionIndex !== SHN_ABS) { const owner = byIndex.get(sectionIndex); if (owner !== undefined) address += owner.address; diff --git a/src/native/hardening/MachoHardening.ts b/src/native/hardening/MachoHardening.ts index a8afdef1bbc..4a08de2fcd1 100644 --- a/src/native/hardening/MachoHardening.ts +++ b/src/native/hardening/MachoHardening.ts @@ -641,7 +641,8 @@ const stackRow = ( continue; // A common definition shares those type bits but carries its size in // n_value, so only a zero n_value names an import. - if (symbols.word(entry + 8, wide ? 64 : 32) !== 0n) continue; + if (symbols.u32(entry + 8) !== 0 || (wide && symbols.u32(entry + 12) !== 0)) + continue; // After a match, later records are still admitted so symbol order cannot // hide a damaged table; their names need not be read. if (found !== null) { @@ -664,6 +665,12 @@ const stackRow = ( unterminated = true; continue; } + if ( + (observed.contentBytes !== 16 && observed.contentBytes !== 17) || + strings[nameOffset] !== 0x5f || + strings[nameOffset + 1] !== 0x5f + ) + continue; const name = elfStringTableText(observed) ?? ""; if (name === "__stack_chk_fail" || name === "___stack_chk_fail") found = row( diff --git a/src/process/ProcessOwnership.ts b/src/process/ProcessOwnership.ts index 07426e2cce9..8b1f1394bd4 100644 --- a/src/process/ProcessOwnership.ts +++ b/src/process/ProcessOwnership.ts @@ -1641,16 +1641,21 @@ const recheckOwnershipFailures = async ( try { let processes = await host.listProcesses(); const isDarwin = (host.platform ?? HOST_PLATFORM.platform) === "darwin"; + const retryableFailurePids = isDarwin + ? new Set( + failures + .filter((failure) => failure.reason !== "run-token-mismatch") + .map((failure) => failure.pid), + ) + : undefined; for ( let attempt = 0; - isDarwin && + retryableFailurePids !== undefined && + retryableFailurePids.size > 0 && attempt < TOKEN_READ_RETRY_ATTEMPTS && processes.some( ({ pid, state }) => - failures.some( - (failure) => - failure.pid === pid && failure.reason !== "run-token-mismatch", - ) && state.slice(1).includes("E"), + retryableFailurePids.has(pid) && state.slice(1).includes("E"), ); attempt += 1 ) { diff --git a/src/process/ProcessOwnershipObservation.ts b/src/process/ProcessOwnershipObservation.ts index 53709cf7bfa..d8371c2d60b 100644 --- a/src/process/ProcessOwnershipObservation.ts +++ b/src/process/ProcessOwnershipObservation.ts @@ -1609,16 +1609,18 @@ export const createSystemProcessOwnershipHost = ( const unresolved = stillLive.filter( ({ pid }) => identities.get(pid)?.state !== "readable", ); - const liveAfterRetryPids = new Set( - liveProcesses(await safeListProcesses(signal)).map(({ pid }) => pid), - ); - const stillUnresolved = unresolved.filter(({ pid }) => - liveAfterRetryPids.has(pid), - ); - if (stillUnresolved.length > 0) - throw new ProcessAdmissionError("identity_snapshot_unavailable", { - live_processes: stillUnresolved.length, - }); + if (unresolved.length > 0) { + const liveAfterRetryPids = new Set( + liveProcesses(await safeListProcesses(signal)).map(({ pid }) => pid), + ); + const stillUnresolved = unresolved.filter(({ pid }) => + liveAfterRetryPids.has(pid), + ); + if (stillUnresolved.length > 0) + throw new ProcessAdmissionError("identity_snapshot_unavailable", { + live_processes: stillUnresolved.length, + }); + } } return processes.map(({ pid }) => { const observation = identities.get(pid); diff --git a/src/process/capture/ProcessCaptureLifecycle.ts b/src/process/capture/ProcessCaptureLifecycle.ts index 51f9fc4be39..1f76ab43396 100644 --- a/src/process/capture/ProcessCaptureLifecycle.ts +++ b/src/process/capture/ProcessCaptureLifecycle.ts @@ -810,9 +810,10 @@ export const observeSettlement = async ({ consecutiveEmpty = 0; sawLive = true; } - deadlineReached = monotonicProcessCaptureClock() - started >= settleMs; + const elapsed = monotonicProcessCaptureClock() - started; + deadlineReached = elapsed >= settleMs; if (deadlineReached) break; - await delay(50, undefined, { signal }); + await delay(Math.min(50, settleMs - elapsed), undefined, { signal }); } recordEvent("lifecycle", 1); // A lone empty observation proves neither liveness nor quiescence; only an