Skip to content

armgit / rea

Public Imported from GitHub
0

Reverse engineer anything with agents, from app behavior down to native binaries.

fix(native): bound LLDB PID reads before validation

morluto292e7bef27c9
diff --git a/src/native/LldbReadbackFiles.ts b/src/native/LldbReadbackFiles.ts
index 3ebd4a9bb..9914a893e 100644
--- a/src/native/LldbReadbackFiles.ts
+++ b/src/native/LldbReadbackFiles.ts
@@ -7,6 +7,9 @@ import {
} from "../artifacts/ArtifactFileOperation.js";
import type { AnalysisCleanupLease } from "../artifacts/AnalysisCleanupLease.js";
+/** PID publication is decimal; allow the largest safe PID and a CRLF ending. */
+const TARGET_PID_FILE_MAX_BYTES = String(Number.MAX_SAFE_INTEGER).length + 2;
+
/** One trace's readback descriptors, separate from the facts already read. */
export class LldbReadbackFiles {
readonly #resources = new ArtifactResourceScope();
@@ -76,10 +79,28 @@ export class LldbReadbackFiles {
async readTargetPid(path: string): Promise<number | undefined> {
const known = this.#targetPids.get(path);
if (known !== undefined) return known;
- const contents = await this.read(path, async (file) =>
- (await file.readFile("utf8")).trim(),
- );
- if (!contents.ok || !/^[1-9]\d*$/u.test(contents.value)) return undefined;
+ const contents = await this.read(path, async (file) => {
+ const bytes = Buffer.alloc(TARGET_PID_FILE_MAX_BYTES + 1);
+ let length = 0;
+ while (length < bytes.length) {
+ const { bytesRead } = await file.read(
+ bytes,
+ length,
+ bytes.length - length,
+ length,
+ );
+ if (bytesRead === 0) break;
+ length += bytesRead;
+ }
+ if (length > TARGET_PID_FILE_MAX_BYTES) return undefined;
+ return bytes.subarray(0, length).toString("utf8").trim();
+ });
+ if (
+ !contents.ok ||
+ contents.value === undefined ||
+ !/^[1-9]\d*$/u.test(contents.value)
+ )
+ return undefined;
const pid = Number(contents.value);
if (!Number.isSafeInteger(pid)) return undefined;
this.#targetPids.set(path, pid);
diff --git a/tests/boundary/providers/native/lldbReadbackFiles.test.ts b/tests/boundary/providers/native/lldbReadbackFiles.test.ts
new file mode 100644
index 000000000..bb17b3825
--- /dev/null
+++ b/tests/boundary/providers/native/lldbReadbackFiles.test.ts
@@ -0,0 +1,72 @@
+import { mkdtemp, open, rm, writeFile } from "node:fs/promises";
+import { tmpdir } from "node:os";
+import { join } from "node:path";
+import { expect, it, vi } from "vitest";
+
+import { LldbReadbackFiles } from "../../../../src/native/LldbReadbackFiles.js";
+
+it("bounds PID readback while retaining newline-terminated producer fixtures", async () => {
+ const directory = await mkdtemp(join(tmpdir(), "rea-lldb-pid-"));
+ const values = [
+ ["max-safe.pid", String(Number.MAX_SAFE_INTEGER), Number.MAX_SAFE_INTEGER],
+ [
+ "max-safe-crlf.pid",
+ `${Number.MAX_SAFE_INTEGER}\r\n`,
+ Number.MAX_SAFE_INTEGER,
+ ],
+ ["lf.pid", "12345\n", 12345],
+ ["crlf.pid", "12345\r\n", 12345],
+ ["unsafe.pid", "9007199254740992", undefined],
+ ["too-many-digits.pid", "10000000000000000", undefined],
+ ["empty.pid", "", undefined],
+ ["oversized.pid", "1".repeat(64), undefined],
+ ] as const;
+ for (const [name, contents] of values)
+ await writeFile(join(directory, name), contents);
+ const sample = await open(join(directory, "max-safe.pid"), "r");
+ const prototype = Object.getPrototypeOf(sample);
+ await sample.close();
+ let bytesRead = 0;
+ const requestedLengths: number[] = [];
+ const originalReadFile = prototype.readFile;
+ const readFileSpy = vi
+ .spyOn(prototype, "readFile")
+ .mockImplementation(async function (...args) {
+ const value = await Reflect.apply(originalReadFile, this, args);
+ bytesRead += Buffer.byteLength(value);
+ return value;
+ });
+ const originalRead = prototype.read;
+ const readSpy = vi
+ .spyOn(prototype, "read")
+ .mockImplementation(async function (...args) {
+ const requestedLength = args[2] ?? 0;
+ requestedLengths.push(requestedLength);
+ const result = await Reflect.apply(originalRead, this, [
+ args[0],
+ args[1],
+ Math.min(requestedLength, 1),
+ args[3],
+ ]);
+ bytesRead += result.bytesRead;
+ return result;
+ });
+ const files = new LldbReadbackFiles();
+ try {
+ for (const [name, , expected] of values) {
+ const before = bytesRead;
+ expect(await files.readTargetPid(join(directory, name))).toBe(expected);
+ expect(bytesRead - before).toBeLessThanOrEqual(19);
+ }
+ expect(
+ await files.readTargetPid(join(directory, "missing.pid")),
+ ).toBeUndefined();
+ expect(requestedLengths.every((length) => length <= 19)).toBe(true);
+ expect(bytesRead).toBeLessThanOrEqual(19 * values.length);
+ } finally {
+ readSpy.mockRestore();
+ readFileSpy.mockRestore();
+ await files.retryCleanup();
+ await rm(directory, { recursive: true, force: true });
+ }
+});