diff --git a/docs/android-boot-image.md b/docs/android-boot-image.md index 688c02d450..7c6eae1c1b 100644 --- a/docs/android-boot-image.md +++ b/docs/android-boot-image.md @@ -79,8 +79,9 @@ the header, a component, or the footer is malformed. ## Bounded work The reader admits at most 4,096 vendor ramdisk table entries and an entry size -of at most 4,096 bytes. A vendor `header_size` above 1 MiB is the same kind of -capacity limit. Those checks happen before the table is read. Unsupported +of at most 4,096 bytes. It verifies that the table dimensions agree and the +declared bytes fit in the image before applying those limits and reading the +table. A vendor `header_size` above 1 MiB also exceeds a capacity limit. Unsupported header revisions return `unsupported_target`; truncated declared structures return `truncated`; malformed layouts return `invalid_request`. The page padding after the last component carries no data and may be absent. These refusals diff --git a/src/artifacts/AndroidBootImageArtifactReader.boundaries.test.ts b/src/artifacts/AndroidBootImageArtifactReader.boundaries.test.ts index 4bda72eaac..3ecac69c23 100644 --- a/src/artifacts/AndroidBootImageArtifactReader.boundaries.test.ts +++ b/src/artifacts/AndroidBootImageArtifactReader.boundaries.test.ts @@ -197,6 +197,30 @@ it("rejects an excessive vendor ramdisk count before reading the table", async ( } }); +it("reports a truncated vendor ramdisk table above the count allowance without reading its rows", async () => { + const image = vendorBootWithRamdisks(4097); + // One header page and two pages of 1-byte ramdisks precede 4097 108-byte rows. + const tableEnd = 3 * 4096 + 4097 * 108; + const path = await fixture(image.subarray(0, tableEnd - 1)); + const admitted = await openRegularFile(path, { symlinks: "reject" }); + const reader = new AndroidBootImageArtifactReader(path, admitted); + const reads = vi.spyOn(admitted.handle, "read"); + try { + await expect(entries(reader)).rejects.toMatchObject({ + reason: "truncated", + cause: { + kind: "truncated", + failure: { field: "components", offset: tableEnd - 1 }, + }, + }); + expect(reads).toHaveBeenCalledTimes(1); + } finally { + await reader.close(); + reads.mockRestore(); + await admitted.handle.close(); + } +}); + artifactReaderLifecycleCases({ image: () => buildBootImage({ version: 0 }), createReader: (path, admitted) => diff --git a/src/artifacts/AndroidBootImageFormat.test.ts b/src/artifacts/AndroidBootImageFormat.test.ts index 989ae6afa4..e0a7aac1d4 100644 --- a/src/artifacts/AndroidBootImageFormat.test.ts +++ b/src/artifacts/AndroidBootImageFormat.test.ts @@ -4,7 +4,6 @@ import { appendAvbFooter, buildBootImage, buildVendorBootImage, - declareVendorRamdiskCount, osVersionRaw, truncateImage, } from "../../tests/fixtures/androidBootImage.js"; @@ -392,7 +391,7 @@ it("splits a v4 vendor ramdisk table and a v3 section", () => { ]); }); -it("rejects vendor table overlap, overrun, and an excessive entry count", () => { +it("rejects truncated, overlapping, and out-of-range vendor ramdisk rows", () => { const image = buildVendorBootImage({ version: 4, ramdisks: [ @@ -440,20 +439,6 @@ it("rejects vendor table overlap, overrun, and an excessive entry count", () => expect(overran.ok).toBe(false); if (overran.ok) return; expect(overran.error.message).toContain("overruns the vendor ramdisk"); - const capacity = failed( - declareVendorRamdiskCount(buildVendorBootImage({ version: 4 }), 4097), - ); - expect(capacity).toMatchObject({ - kind: "limit", - failure: { - format: "android-boot-image", - limit: { - name: "Android vendor ramdisk table entry count", - maximum: 4096, - }, - }, - }); - expect(capacity.message).toContain("4096"); expect( failed( buildVendorBootImage({ @@ -465,6 +450,44 @@ it("rejects vendor table overlap, overrun, and an excessive entry count", () => ).toContain("smaller than the 108-byte"); }); +it("checks vendor ramdisk table dimensions before the entry count allowance", () => { + const image = buildVendorBootImage({ version: 4 }); + image.writeUInt32LE(108, 2112); // Only one row declared by table_size. + image.writeUInt32LE(4097, 2116); + expect(failed(image)).toMatchObject({ + kind: "malformed", + message: + "vendor ramdisk table size 108 does not equal 4097 entries of 108 bytes", + }); + + image.writeUInt32LE(4097 * 107, 2112); + image.writeUInt32LE(107, 2120); + expect(failed(image)).toMatchObject({ + kind: "malformed", + message: + "vendor ramdisk table entry size 107 is smaller than the 108-byte v4 entry", + }); +}); + +it("applies the vendor ramdisk row-width allowance only to file-backed tables", () => { + const image = buildVendorBootImage({ + version: 4, + entrySize: 4097, + ramdisks: [{ name: "wide", bytes: Buffer.from("a") }], + }); + const end = 2 * 4096 + 4097; + expect(failed(image.subarray(0, end))).toMatchObject({ + kind: "limit", + failure: { + limit: { name: "Android vendor ramdisk table entry size", maximum: 4096 }, + }, + }); + expect(failed(image.subarray(0, end - 1))).toMatchObject({ + kind: "truncated", + failure: { field: "components", offset: end - 1 }, + }); +}); + it("names unsafe vendor ramdisk paths and reports an uncovered section", () => { const entry = (name: string, offset: number): Buffer => { const row = Buffer.alloc(108); diff --git a/src/artifacts/AndroidBootImageFormat.ts b/src/artifacts/AndroidBootImageFormat.ts index bc234021d1..cb2c02e96c 100644 --- a/src/artifacts/AndroidBootImageFormat.ts +++ b/src/artifacts/AndroidBootImageFormat.ts @@ -766,6 +766,22 @@ const parseModernBoot = ( }); }; +const vendorTableByteLength = ( + entryCount: number, + entrySize: number, +): number => { + if (entryCount > 0 && entrySize < VENDOR_RAMDISK_ENTRY_SIZE) + formatError( + `vendor ramdisk table entry size ${entrySize} is smaller than the ${VENDOR_RAMDISK_ENTRY_SIZE}-byte v4 entry`, + ); + const bytes = entryCount * entrySize; + if (!Number.isSafeInteger(bytes)) + formatError( + `vendor ramdisk table dimensions ${entryCount} × ${entrySize} exceed a safe byte length`, + ); + return bytes; +}; + const parseVendorTableHeader = ( prefix: Buffer, ): { @@ -778,6 +794,17 @@ const parseVendorTableHeader = ( const entryCount = u32(prefix, VENDOR_V3_SIZE + 4); const entrySize = u32(prefix, VENDOR_V3_SIZE + 8); const bootconfigSize = u32(prefix, VENDOR_V3_SIZE + 12); + if (vendorTableByteLength(entryCount, entrySize) !== tableSize) + formatError( + `vendor ramdisk table size ${tableSize} does not equal ${entryCount} entries of ${entrySize} bytes`, + ); + return { tableSize, entryCount, entrySize, bootconfigSize }; +}; + +const requireVendorTableCapacity = ( + entryCount: number, + entrySize: number, +): void => { if (entryCount > MAX_VENDOR_RAMDISKS) capacityError( `vendor ramdisk table declares more than ${MAX_VENDOR_RAMDISKS} entries`, @@ -788,16 +815,6 @@ const parseVendorTableHeader = ( `vendor ramdisk table entry size ${entrySize} exceeds the ${MAX_RAMDISK_ENTRY_SIZE} byte entry bound`, VENDOR_RAMDISK_ENTRY_SIZE_LIMIT, ); - if (entryCount > 0 && entrySize < VENDOR_RAMDISK_ENTRY_SIZE) - formatError( - `vendor ramdisk table entry size ${entrySize} is smaller than the ${VENDOR_RAMDISK_ENTRY_SIZE}-byte v4 entry`, - ); - const product = entryCount * entrySize; - if (!Number.isSafeInteger(product) || product !== tableSize) - formatError( - `vendor ramdisk table size ${tableSize} does not equal ${entryCount} entries of ${entrySize} bytes`, - ); - return { tableSize, entryCount, entrySize, bootconfigSize }; }; const parseVendorHeader = ( @@ -878,6 +895,8 @@ const parseVendorHeader = ( const section = { offset: sectionOffset, size: vendorRamdiskSize }; const declared = [...components, section, tableRange]; const dataEnd = requireFileCovers(fileSize, headerSize, declared); + if (table !== null) + requireVendorTableCapacity(table.entryCount, table.entrySize); return bootParse({ imageKind: "vendor-boot", magic: VENDOR_BOOT_MAGIC, @@ -995,11 +1014,7 @@ const readVendorRamdiskTable = ( readonly sectionSize: number; }, ): VendorRamdiskTableParse => { - if (input.entryCount > 0 && input.entrySize < VENDOR_RAMDISK_ENTRY_SIZE) - formatError( - `vendor ramdisk table entry size ${input.entrySize} is smaller than the ${VENDOR_RAMDISK_ENTRY_SIZE}-byte v4 entry`, - ); - const expected = input.entryCount * input.entrySize; + const expected = vendorTableByteLength(input.entryCount, input.entrySize); requireBytes(table, expected, "vendor ramdisk table"); if (table.length > expected) formatError( diff --git a/tests/fixtures/androidBootImage.ts b/tests/fixtures/androidBootImage.ts index 78d6164d36..155d921042 100644 --- a/tests/fixtures/androidBootImage.ts +++ b/tests/fixtures/androidBootImage.ts @@ -391,19 +391,6 @@ export const truncateImage = (image: Buffer): Buffer => { return Buffer.from(image.subarray(0, Math.max(0, end - 1))); }; -/** - * Rewrite a v4 vendor table count. `entry_size` stays 108 and `table_size` - * becomes `count * 108`. - */ -export const declareVendorRamdiskCount = ( - image: Buffer, - count: number, -): Buffer => { - const copy = Buffer.from(image); - writeU32(copy, VENDOR_V3, count * VENDOR_ENTRY); - writeU32(copy, VENDOR_V3 + 4, count); - return copy; -}; /** Small byte-format refusals for the public artifact adapters. */ export const androidBootImageRefusals = () => { const unsupportedBoot = buildBootImage({ version: 0 });