mirror of
https://github.com/morluto/rea.git
synced 2026-11-21 14:05:49 +00:00
fix(pwndbg): reject core mapping paths that escape sysroot
Interpret source-bound ELF64 NT_FILE descriptors once before debugger launch. Bound counts and pathname coverage by the descriptor, retain exact path bytes, and refuse relative names or dot segments rather than letting GDB resolve them against the host. Pin GDB's pathname semantics to Unix. Preserve the decoded partial observation on refusal, basic-only recovery, and pre-admission cancellation. Share an exact mapping fixture and replace the old fixture boolean modes with explicit report producers. Validation: final series has 33 passing Vitest cases plus five Python producer controls; 38 Linux x64 provider cases are explicitly skipped on macOS. Typecheck/lint, docs checks and test discovery pass. Real GDB16.3 and strace reproduce the relative-path escape using only an owned fixture; compiled admission admits a real absolute-path core and refuses that relative-path core. Full native pwndbg / Linux x64 remain unverified here. Task token, CPU and peak-family-memory costs are unmeasured; no speed claim. Fixes #4942
This commit is contained in:
1 parent
5bf4fffc1b
commit
b33c0df02e
8 files changed
+322
-45
No files matched your search
@@ -0,0 +1,85 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { validateCoreFileMappingAuthority } from "./CoreFileMappingAuthority.js";
|
||||
import {
|
||||
recordedCrashFixture,
|
||||
recordedCrashFileMappingFixture,
|
||||
} from "../../../tests/fixtures/binaryDiagnostics/recordedCrash.js";
|
||||
|
||||
function mapping(paths: readonly Buffer[]) {
|
||||
const report = recordedCrashFileMappingFixture(paths);
|
||||
const note = report.notes[0];
|
||||
if (note === undefined) throw new Error("Expected a mapping note");
|
||||
const bytes = Buffer.from(note.descriptor_bytes_base64, "base64");
|
||||
note.descriptor_location.offset = "0x0";
|
||||
return { report, bytes };
|
||||
}
|
||||
|
||||
describe("core-only NT_FILE authority", () => {
|
||||
it.each([
|
||||
"/usr/bin/app",
|
||||
"//usr/lib/libc.so",
|
||||
"/a/.../b",
|
||||
"/a/../b",
|
||||
"../../app",
|
||||
"relative/app",
|
||||
"/a/./b",
|
||||
"",
|
||||
"target:/app",
|
||||
])(
|
||||
"retains original pathname bytes and admits only confined Unix paths: %s",
|
||||
(path) => {
|
||||
const { report, bytes } = mapping([Buffer.from(path)]);
|
||||
const original = Buffer.from(bytes);
|
||||
const result = validateCoreFileMappingAuthority(report, bytes);
|
||||
expect(result.ok).toBe(
|
||||
["/usr/bin/app", "//usr/lib/libc.so", "/a/.../b"].includes(path),
|
||||
);
|
||||
expect(bytes).toEqual(original);
|
||||
if (!result.ok)
|
||||
expect(result.error).toMatchObject({
|
||||
_tag: "AnalysisCapabilityUnavailableError",
|
||||
capturedOutput: report.decoder_diagnostics,
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("does not decode invalid UTF-8 to decide pathname authority", () => {
|
||||
const { report, bytes } = mapping([Buffer.from([47, 255, 47, 46, 46, 46])]);
|
||||
expect(validateCoreFileMappingAuthority(report, bytes).ok).toBe(true);
|
||||
});
|
||||
|
||||
it.each([
|
||||
"short-header",
|
||||
"huge-count",
|
||||
"unterminated",
|
||||
"extra-path",
|
||||
"unsafe-second",
|
||||
])("refuses unresolved or unsafe mapping coverage: %s", (scenario) => {
|
||||
const { report, bytes } = mapping([
|
||||
Buffer.from("/safe"),
|
||||
Buffer.from(scenario === "unsafe-second" ? "../unsafe" : "/also-safe"),
|
||||
]);
|
||||
let snapshot = bytes;
|
||||
if (scenario === "short-header") snapshot = bytes.subarray(0, 8);
|
||||
if (scenario === "huge-count") bytes.writeBigUInt64LE(0xffffffffffffffffn);
|
||||
if (scenario === "unterminated")
|
||||
snapshot = bytes.subarray(0, bytes.length - 1);
|
||||
if (scenario === "extra-path")
|
||||
snapshot = Buffer.concat([bytes, Buffer.from("extra\0")]);
|
||||
const note = report.notes[0];
|
||||
if (note === undefined) throw new Error("Expected a mapping note");
|
||||
note.descriptor_location.bytes = `0x${snapshot.length.toString(16)}`;
|
||||
expect(validateCoreFileMappingAuthority(report, snapshot).ok).toBe(false);
|
||||
});
|
||||
|
||||
it("permits cores without file mappings and empty mapping tables", () => {
|
||||
const report = recordedCrashFixture();
|
||||
expect(validateCoreFileMappingAuthority(report, Buffer.alloc(0)).ok).toBe(
|
||||
true,
|
||||
);
|
||||
const empty = mapping([]);
|
||||
expect(validateCoreFileMappingAuthority(empty.report, empty.bytes).ok).toBe(
|
||||
true,
|
||||
);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,63 @@
|
||||
import { AnalysisCapabilityUnavailableError } from "../../domain/analysisErrorCore.js";
|
||||
import type { RecordedCrash } from "../../domain/native/recordedCrash.js";
|
||||
import { err, ok, type Result } from "../../domain/result.js";
|
||||
|
||||
/** Interpret source-bound ELF64 NT_FILE paths once, before GDB can open them. */
|
||||
export function validateCoreFileMappingAuthority(
|
||||
report: RecordedCrash,
|
||||
snapshot: Buffer,
|
||||
): Result<void, AnalysisCapabilityUnavailableError> {
|
||||
for (const note of report.notes) {
|
||||
if (note.type !== "NT_FILE") continue;
|
||||
const refuse = (reason: string) => {
|
||||
const message = `Core mapping note ${note.index} cannot be confined to the owned debugger sysroot: ${reason}.`;
|
||||
return err(
|
||||
new AnalysisCapabilityUnavailableError(
|
||||
"pwndbg-core",
|
||||
"inspect_recorded_crash",
|
||||
message,
|
||||
{
|
||||
userMessage: `${message} Omit include_debugger_context for basic recorded evidence.`,
|
||||
capturedOutput: report.decoder_diagnostics,
|
||||
},
|
||||
),
|
||||
);
|
||||
};
|
||||
// The provider has already bound this range and note kind to the snapshot.
|
||||
const start = Number(BigInt(note.descriptor_location.offset));
|
||||
const bytes = snapshot.subarray(
|
||||
start,
|
||||
start + Number(BigInt(note.descriptor_location.bytes)),
|
||||
);
|
||||
if (bytes.length < 16) return refuse("missing mapping table header");
|
||||
const count = bytes.readBigUInt64LE(0);
|
||||
if (count > BigInt(Math.floor((bytes.length - 16) / 24)))
|
||||
return refuse("mapping count exceeds its descriptor");
|
||||
let position = 16 + Number(count) * 24;
|
||||
for (let index = 0; index < Number(count); index++) {
|
||||
const end = bytes.indexOf(0, position);
|
||||
if (end < 0) return refuse("unterminated mapping pathname");
|
||||
const path = bytes.subarray(position, end);
|
||||
// Inspect Unix path bytes without lossy UTF-8 decoding or host resolution.
|
||||
// Relative paths bypass GDB's sysroot; dot segments can escape its prefix.
|
||||
if (path[0] !== 0x2f) return refuse("mapping pathname is not absolute");
|
||||
let component = 0;
|
||||
for (let cursor = 0; cursor <= path.length; cursor++) {
|
||||
if (cursor !== path.length && path[cursor] !== 0x2f) continue;
|
||||
const length = cursor - component;
|
||||
if (
|
||||
(length === 1 && path[component] === 0x2e) ||
|
||||
(length === 2 &&
|
||||
path[component] === 0x2e &&
|
||||
path[component + 1] === 0x2e)
|
||||
)
|
||||
return refuse("mapping pathname contains a dot segment");
|
||||
component = cursor + 1;
|
||||
}
|
||||
position = end + 1;
|
||||
}
|
||||
if (bytes.subarray(position).some((byte) => byte !== 0))
|
||||
return refuse("mapping pathname coverage is unresolved");
|
||||
}
|
||||
return ok(undefined);
|
||||
}
|
||||
@@ -1,4 +1,7 @@
|
||||
import { AnalysisOutputError } from "../../domain/analysisErrorCore.js";
|
||||
import {
|
||||
AnalysisCancelledError,
|
||||
AnalysisOutputError,
|
||||
} from "../../domain/analysisErrorCore.js";
|
||||
import { AnalysisError } from "../../domain/analysisErrorBase.js";
|
||||
import {
|
||||
recordedCrashPayloadSchema,
|
||||
@@ -10,6 +13,7 @@ import type { RecordedCrashPort } from "../../application/binaryDiagnostics/Reco
|
||||
import { PrivateRuntimeRoot } from "../../process/PrivateRuntimeRoot.js";
|
||||
import { PwntoolsDecoder, type PwntoolsLauncher } from "./PwntoolsDecoder.js";
|
||||
import { PWNTOOLS_PROVIDER_IDENTITY } from "./PwntoolsRelease.js";
|
||||
import { validateCoreFileMappingAuthority } from "../pwndbg/CoreFileMappingAuthority.js";
|
||||
import { validateRecordedCrashSources } from "./RecordedCrashSourceBindings.js";
|
||||
import { validateRecordedCrashStructure } from "./RecordedCrashStructureBindings.js";
|
||||
import {
|
||||
@@ -118,6 +122,16 @@ export class PwntoolsRecordedCrashProvider
|
||||
}
|
||||
if (!input.include_debugger_context) return value;
|
||||
try {
|
||||
if (options?.signal?.aborted)
|
||||
throw new AnalysisCancelledError("inspect_recorded_crash", {
|
||||
cause: options.signal.reason,
|
||||
capturedOutput: diagnostics,
|
||||
});
|
||||
const authority = validateCoreFileMappingAuthority(
|
||||
value,
|
||||
snapshot.bytes,
|
||||
);
|
||||
if (!authority.ok) throw authority.error;
|
||||
const context = await inspectPwndbgCore({
|
||||
environment,
|
||||
architecture: value.architecture,
|
||||
|
||||
Reference in new issue
Block a user