diff --git a/docs/android-boot-image.md b/docs/android-boot-image.md index effacdfec3..cdb7d19a81 100644 --- a/docs/android-boot-image.md +++ b/docs/android-boot-image.md @@ -81,7 +81,8 @@ 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 header revisions return `unsupported_target`; truncated declared structures -return `truncated`; malformed layouts return `invalid_request`. These refusals +return `truncated`; malformed layouts return `invalid_request`. The page +padding after the last component carries no data and may be absent. These refusals do not produce an empty successful inventory. Gaps inside a vendor ramdisk section, or bytes between the sequential layout and a recovery DTBO, are named in limitations instead of being invented as entries. diff --git a/docs/specs/firmware/android-boot-image/spec.md b/docs/specs/firmware/android-boot-image/spec.md index 436fa478bb..29a0e7e57d 100644 --- a/docs/specs/firmware/android-boot-image/spec.md +++ b/docs/specs/firmware/android-boot-image/spec.md @@ -75,7 +75,8 @@ these files. 8. The header probe is at most 4096 bytes. The reader admits at most 4096 vendor ramdisk table entries, an entry size of at most 4096 bytes, and a vendor `header_size` of at most 1 MiB. Those checks run before the table - is read. Truncated declared structures return `truncated`; unsupported + is read. Truncated declared structures return `truncated`; the page + padding after the last component may be absent. Unsupported revisions return `unsupported_target`; malformed layouts return `invalid_request`. Gaps inside a vendor ramdisk section, and bytes between the sequential layout and a recovery DTBO, are diff --git a/src/artifacts/AndroidBootImageArtifactReader.test.ts b/src/artifacts/AndroidBootImageArtifactReader.test.ts index bb3ec0f79d..f7d0719498 100644 --- a/src/artifacts/AndroidBootImageArtifactReader.test.ts +++ b/src/artifacts/AndroidBootImageArtifactReader.test.ts @@ -292,6 +292,31 @@ it("records an AVB footer without treating it as a child", async () => { }); }); +it("inventories an image whose final page padding is absent", async () => { + const kernel = Buffer.from("kernel-bytes"); + const ramdisk = Buffer.from("ramdisk-bytes"); + const padded = buildBootImage({ version: 0, kernel, ramdisk }); + // The ramdisk starts on page 2; drop only the zero padding after it. + const unpadded = padded.subarray(0, 4096 + ramdisk.length); + expect(padded.subarray(unpadded.length).every((byte) => byte === 0)).toBe( + true, + ); + const path = await writeCarrier("unpadded.img", unpadded); + const snapshot = await scanCanonicalArtifactInventory(path); + expect(snapshot.coverage.status).toBe("complete"); + expect( + snapshot.occurrences.find((row) => row.logical_path === ".") + ?.format_metadata, + ).toMatchObject({ + component_count: 2, + avb_footer_present: false, + avb_footer_size: null, + }); + const stored = await openAll(path); + expect(stored.get("kernel")?.equals(kernel)).toBe(true); + expect(stored.get("ramdisk")?.equals(ramdisk)).toBe(true); +}); + it("fails closed on truncation and overlapping ranges", async () => { const truncated = await writeCarrier( "truncated.img", diff --git a/src/artifacts/AndroidBootImageArtifactReader.ts b/src/artifacts/AndroidBootImageArtifactReader.ts index bc84a0a0f1..d4dc16727b 100644 --- a/src/artifacts/AndroidBootImageArtifactReader.ts +++ b/src/artifacts/AndroidBootImageArtifactReader.ts @@ -151,6 +151,7 @@ export class AndroidBootImageArtifactReader extends PositionalArtifactReader { + // Each end follows the layout: the last structure starts on a page and the + // image ends at its final byte. + const cases = [ + // 2048-byte pages: kernel, then the 7-byte ramdisk on page 2. + { image: buildBootImage({ version: 0 }), end: 2 * 2048 + 7 }, + // Recovery DTBO on page 3, then the 3-byte DTB on page 4. + { + image: buildBootImage({ + version: 2, + recoveryDtbo: Buffer.from("dtbo"), + dtb: Buffer.from("dtb"), + }), + end: 4 * 2048 + 3, + }, + // 4096-byte pages: kernel, ramdisk, then the 9-byte signature on page 3. + { + image: buildBootImage({ + version: 4, + signature: Buffer.from("signature"), + }), + end: 3 * 4096 + 9, + }, + // Vendor ramdisk section on page 1, then the 3-byte DTB on page 2. + { + image: buildVendorBootImage({ + version: 3, + ramdisk: Buffer.from("vendor-ramdisk"), + dtb: Buffer.from("dtb"), + }), + end: 2 * 4096 + 3, + }, + // Section, one 108-byte table entry, then 26 bootconfig bytes on page 3. + { + image: buildVendorBootImage({ + version: 4, + ramdisks: [{ name: "one", type: 1, bytes: Buffer.from("aaa") }], + bootconfig: Buffer.from("androidboot.hardware=test\n"), + }), + end: 3 * 4096 + 26, + }, + // The 108-byte ramdisk table on page 2 is the last declared structure. + { + image: buildVendorBootImage({ + version: 4, + ramdisks: [{ name: "one", type: 1, bytes: Buffer.from("aaa") }], + }), + end: 2 * 4096 + 108, + }, + ]; + for (const { image, end } of cases) { + expect(image.subarray(end).every((byte) => byte === 0)).toBe(true); + const padded = parsed(image); + expect(padded).toMatchObject({ dataEnd: end, imageEnd: image.length }); + expect(parsed(image.subarray(0, end))).toEqual(padded); + expect(failed(image.subarray(0, end - 1))).toMatchObject({ + kind: "truncated", + message: `image is truncated: components end at byte ${String(end)} but the file is ${String(end - 1)} bytes`, + }); + } +}); + it("aligns a kernel larger than 2^31 without allocating the image", () => { const header = Buffer.alloc(1632); header.write("ANDROID!", 0, "ascii"); @@ -544,6 +606,7 @@ it("records an AVB footer and rejects vbmeta that overlaps a component", () => { const happy = appendAvbFooter(image); const footer = inspectAvbFooter({ fileSize: happy.length, + dataEnd: facts.dataEnd, imageEnd: facts.imageEnd, headerOccupied: facts.headerOccupied, components: facts.components, @@ -563,6 +626,7 @@ it("records an AVB footer and rejects vbmeta that overlaps a component", () => { }); const bare = inspectAvbFooter({ fileSize: image.length, + dataEnd: facts.dataEnd, imageEnd: facts.imageEnd, headerOccupied: facts.headerOccupied, components: facts.components, @@ -575,6 +639,7 @@ it("records an AVB footer and rejects vbmeta that overlaps a component", () => { const short = Buffer.concat([image, Buffer.alloc(10)]); const shortFooter = inspectAvbFooter({ fileSize: short.length, + dataEnd: facts.dataEnd, imageEnd: facts.imageEnd, headerOccupied: facts.headerOccupied, components: facts.components, @@ -583,12 +648,53 @@ it("records an AVB footer and rejects vbmeta that overlaps a component", () => { expect(shortFooter.ok).toBe(true); if (!shortFooter.ok) return; expect(shortFooter.value.limitations[0]).toContain("10 trailing bytes"); + const unpadded = inspectAvbFooter({ + fileSize: facts.dataEnd, + dataEnd: facts.dataEnd, + imageEnd: facts.imageEnd, + headerOccupied: facts.headerOccupied, + components: facts.components, + tail: null, + }); + expect(unpadded).toMatchObject({ + ok: true, + value: { present: false, limitations: [] }, + }); + const cut = inspectAvbFooter({ + fileSize: facts.dataEnd - 1, + dataEnd: facts.dataEnd, + imageEnd: facts.imageEnd, + headerOccupied: facts.headerOccupied, + components: facts.components, + tail: null, + }); + expect(cut).toMatchObject({ ok: false, error: { kind: "truncated" } }); + // avbtool records the unpadded size and places vbmeta on the next block. + const signed = appendAvbFooter(image.subarray(0, facts.dataEnd), { + vbmetaOffset: facts.imageEnd, + originalImageSize: facts.dataEnd, + partitionSize: 2 * facts.imageEnd, + }); + expect( + inspectAvbFooter({ + fileSize: signed.length, + dataEnd: facts.dataEnd, + imageEnd: facts.imageEnd, + headerOccupied: facts.headerOccupied, + components: facts.components, + tail: signed.subarray(signed.length - 64), + }), + ).toMatchObject({ + ok: true, + value: { present: true, originalImageSize: facts.dataEnd }, + }); const colliding = appendAvbFooter(image, { vbmetaOffset: 2048, vbmeta: Buffer.alloc(16), }); const collision = inspectAvbFooter({ fileSize: colliding.length, + dataEnd: facts.dataEnd, imageEnd: facts.imageEnd, headerOccupied: facts.headerOccupied, components: facts.components, diff --git a/src/artifacts/AndroidBootImageFormat.ts b/src/artifacts/AndroidBootImageFormat.ts index 9badf398fc..bc793d000c 100644 --- a/src/artifacts/AndroidBootImageFormat.ts +++ b/src/artifacts/AndroidBootImageFormat.ts @@ -111,6 +111,9 @@ export interface AndroidBootParse { readonly vendorRamdiskTableEntrySize: number | null; readonly bootconfigSize: number | null; readonly components: readonly AndroidBootComponentPlan[]; + /** End of the last declared header or component byte. */ + readonly dataEnd: number; + /** Page-aligned end after the last component; trailing padding may be absent. */ readonly imageEnd: number; readonly limitations: readonly string[]; readonly tableRange: { @@ -271,15 +274,36 @@ const requireHeaderSize = ( return value; }; -const requireFileCovers = (fileSize: number, imageEnd: number): void => { +/** + * Require the file to hold every declared header and component byte, and + * return where those bytes end. Producers pad each component to the next + * page, but the padding after the final one carries no data, so a file that + * stops at the last declared byte is complete. + */ +const requireFileCovers = ( + fileSize: number, + headerSize: number, + ranges: readonly ({ + readonly offset: number; + readonly size: number; + } | null)[], +): number => { if (!Number.isSafeInteger(fileSize) || fileSize < 0) formatError("file size is not a safe nonnegative integer"); - if (imageEnd > fileSize) + const dataEnd = ranges.reduce( + (end, range) => + range === null || range.size === 0 + ? end + : Math.max(end, endOf(range.offset, range.size)), + headerSize, + ); + if (dataEnd > fileSize) throw truncatedInput( ANDROID_BOOT_FORMAT, - `image is truncated: components end at byte ${imageEnd} but the file is ${fileSize} bytes`, + `image is truncated: components end at byte ${dataEnd} but the file is ${fileSize} bytes`, { offset: fileSize, field: "components" }, ); + return dataEnd; }; const untilNul = (bytes: Buffer): Buffer => { @@ -425,6 +449,7 @@ const bootParse = ( | "headerOccupied" | "cmdlineBytesHex" | "components" + | "dataEnd" | "imageEnd" | "limitations" > & @@ -636,7 +661,7 @@ const parseLegacyBoot = ( components, limitations, ); - requireFileCovers(fileSize, tail.cursor); + const dataEnd = requireFileCovers(fileSize, headerSize, components); return bootParse({ imageKind: "boot", magic: ANDROID_BOOT_MAGIC, @@ -663,6 +688,7 @@ const parseLegacyBoot = ( recoveryDtboOffset: tail.recoveryDtboOffset, dtbSize: tail.dtbSize, components, + dataEnd, imageEnd: tail.cursor, limitations, }); @@ -721,7 +747,7 @@ const parseModernBoot = ( ? null : component("signature", "signature", cursor, signatureSize, null), ); - requireFileCovers(fileSize, cursor); + const dataEnd = requireFileCovers(fileSize, headerSize, components); return bootParse({ imageKind: "boot", magic: ANDROID_BOOT_MAGIC, @@ -737,6 +763,7 @@ const parseModernBoot = ( ramdiskSize, signatureSize, components, + dataEnd, imageEnd: cursor, limitations, }); @@ -851,7 +878,9 @@ const parseVendorHeader = ( limitations.push( "Vendor ramdisk section bytes outside the ramdisk table were not exposed as entries.", ); - requireFileCovers(fileSize, cursor); + const section = { offset: sectionOffset, size: vendorRamdiskSize }; + const declared = [...components, section, tableRange]; + const dataEnd = requireFileCovers(fileSize, headerSize, declared); return bootParse({ imageKind: "vendor-boot", magic: VENDOR_BOOT_MAGIC, @@ -872,10 +901,11 @@ const parseVendorHeader = ( vendorRamdiskTableEntrySize: table?.entrySize ?? null, bootconfigSize, components, + dataEnd, imageEnd: cursor, limitations, tableRange, - vendorRamdiskSection: { offset: sectionOffset, size: vendorRamdiskSize }, + vendorRamdiskSection: section, }); }; @@ -1106,6 +1136,7 @@ const rejectVbmetaOverlap = ( const readAvbFooter = (input: { readonly fileSize: number; + readonly dataEnd: number; readonly imageEnd: number; readonly headerOccupied: number; readonly components: readonly { @@ -1114,14 +1145,16 @@ const readAvbFooter = (input: { }[]; readonly tail: Buffer | null; }): AvbFooterFacts => { - if (input.fileSize < input.imageEnd) + if (input.fileSize < input.dataEnd) throw truncatedInput( ANDROID_BOOT_FORMAT, - `image is truncated: components end at byte ${input.imageEnd} but the file is ${input.fileSize} bytes`, + `image is truncated: components end at byte ${input.dataEnd} but the file is ${input.fileSize} bytes`, { offset: input.fileSize, field: "components" }, ); + // A footer follows the page-aligned image. A file that ends inside the + // final page padding has no room for one. const trailing = input.fileSize - input.imageEnd; - if (trailing === 0) return absentFooter(); + if (trailing <= 0) return absentFooter(); if (trailing < AVB_FOOTER_SIZE) return absentFooter([ `${trailing} trailing bytes follow the boot image and are shorter than a 64-byte AVB footer.`, @@ -1136,9 +1169,9 @@ const readAvbFooter = (input: { const originalImageSize = u64be(tail, 12, "AVB original image size"); const vbmetaOffset = u64be(tail, 20, "AVB vbmeta offset"); const vbmetaSize = u64be(tail, 28, "AVB vbmeta size"); - if (originalImageSize < input.imageEnd) + if (originalImageSize < input.dataEnd) formatError( - `AVB original image size ${originalImageSize} ends before boot image components at ${input.imageEnd}`, + `AVB original image size ${originalImageSize} ends before boot image components at ${input.dataEnd}`, ); if (originalImageSize > input.fileSize - AVB_FOOTER_SIZE) formatError( @@ -1168,6 +1201,7 @@ const readAvbFooter = (input: { */ export const inspectAvbFooter = (input: { readonly fileSize: number; + readonly dataEnd: number; readonly imageEnd: number; readonly headerOccupied: number; readonly components: readonly { diff --git a/tests/fixtures/androidBootImage.ts b/tests/fixtures/androidBootImage.ts index dbd357b0dd..78d6164d36 100644 --- a/tests/fixtures/androidBootImage.ts +++ b/tests/fixtures/androidBootImage.ts @@ -380,9 +380,16 @@ export const appendAvbFooter = ( return out; }; -/** Drop the final byte. The view is a copy, so the source image stays intact. */ -export const truncateImage = (image: Buffer): Buffer => - Buffer.from(image.subarray(0, Math.max(0, image.length - 1))); +/** + * Drop the final page padding and the last declared byte, so the image ends + * inside its last component. That component must end with a nonzero byte. + * The view is a copy, so the source image stays intact. + */ +export const truncateImage = (image: Buffer): Buffer => { + let end = image.length; + while (end > 0 && image[end - 1] === 0) end -= 1; + return Buffer.from(image.subarray(0, Math.max(0, end - 1))); +}; /** * Rewrite a v4 vendor table count. `entry_size` stays 108 and `table_size`