From 90550aa22307f6c01245dbbd244d7b3984852d50 Mon Sep 17 00:00:00 2001 From: ShuaixinHuang <1836656514@qq.com> Date: Thu, 19 Nov 2026 02:03:06 +0800 Subject: [PATCH] fix(artifacts): detect AVB footers before boot page padding ends (#12046) --- docs/android-boot-image.md | 13 ++++---- .../AndroidBootImageArtifactReader.test.ts | 29 ++++++++++++++++ .../AndroidBootImageArtifactReader.ts | 7 ++-- src/artifacts/AndroidBootImageFormat.test.ts | 4 +-- src/artifacts/AndroidBootImageFormat.ts | 33 +++++++++++-------- 5 files changed, 62 insertions(+), 24 deletions(-) diff --git a/docs/android-boot-image.md b/docs/android-boot-image.md index cdb7d19a81..688c02d450 100644 --- a/docs/android-boot-image.md +++ b/docs/android-boot-image.md @@ -68,12 +68,13 @@ stays a file; extract it and run `inspect-devicetree` on the extracted file. ## AVB footer -When bytes follow the page-aligned image, the last 64 bytes are compared with -the `AVBf` footer. A matching footer records its version, original image size, -and vbmeta offset and size. The footer and vbmeta are not children and are not -verified. A shorter tail, or a tail that does not start with `AVBf`, is -reported as undecoded trailing bytes. A vbmeta range that overlaps the header, -a component, or the footer is malformed. +The reader checks the final 64 bytes for an `AVBf` footer when those bytes +begin at or after the end of the last declared image component, even if they +start before the page-rounded image end. A matching footer records its version, +original image size, and vbmeta offset and size. The footer and vbmeta remain +unverified metadata. If no footer is recognized, the reader reports undecoded +trailing bytes beyond the page-rounded image end. A vbmeta range that overlaps +the header, a component, or the footer is malformed. ## Bounded work diff --git a/src/artifacts/AndroidBootImageArtifactReader.test.ts b/src/artifacts/AndroidBootImageArtifactReader.test.ts index f7d0719498..ded0324582 100644 --- a/src/artifacts/AndroidBootImageArtifactReader.test.ts +++ b/src/artifacts/AndroidBootImageArtifactReader.test.ts @@ -353,3 +353,32 @@ it("fails closed on truncation and overlapping ranges", async () => { ), ).rejects.toThrow(/overlaps entry/u); }); + +it("finds an AVB footer before the final boot page boundary", async () => { + const pageSize = 32 * 1024; + const kernel = Buffer.alloc(64, 0x4b); + const padded = buildBootImage({ + version: 0, + pageSize, + kernel, + ramdisk: Buffer.alloc(0), + }); + const unpadded = padded.subarray(0, pageSize + kernel.length); + // avbtool append_vbmeta_image uses 4 KiB blocks independently of boot pages. + const path = await writeCarrier( + "early-footer.img", + appendAvbFooter(unpadded, { + vbmeta: Buffer.alloc(256), + vbmetaOffset: 36 * 1024, + partitionSize: 44 * 1024, + }), + ); + const snapshot = await scanCanonicalArtifactInventory(path); + const root = snapshot.occurrences.find((row) => row.logical_path === "."); + expect(root?.format_metadata).toMatchObject({ + avb_footer_present: true, + avb_original_image_size: unpadded.length, + avb_vbmeta_offset: 36 * 1024, + avb_vbmeta_size: 256, + }); +}); diff --git a/src/artifacts/AndroidBootImageArtifactReader.ts b/src/artifacts/AndroidBootImageArtifactReader.ts index d4dc16727b..b3ece5cb49 100644 --- a/src/artifacts/AndroidBootImageArtifactReader.ts +++ b/src/artifacts/AndroidBootImageArtifactReader.ts @@ -22,6 +22,7 @@ import { ANDROID_BOOT_FIXED_LIMITS, AVB_FOOTER_SIZE, compressionFromPrefix, + avbFooterOffset, inspectAvbFooter, parseAndroidBootHeader, parseVendorRamdiskTable, @@ -137,13 +138,13 @@ export class AndroidBootImageArtifactReader extends PositionalArtifactReader { - const trailing = fileSize - header.imageEnd; + const offset = avbFooterOffset(fileSize, header.dataEnd); this.assertOpen(signal); const tail = - trailing >= AVB_FOOTER_SIZE + offset !== null ? await readArtifactFileBytes( handle, - fileSize - AVB_FOOTER_SIZE, + offset, AVB_FOOTER_SIZE, fileSize, signal, diff --git a/src/artifacts/AndroidBootImageFormat.test.ts b/src/artifacts/AndroidBootImageFormat.test.ts index 7a9680928c..989ae6afa4 100644 --- a/src/artifacts/AndroidBootImageFormat.test.ts +++ b/src/artifacts/AndroidBootImageFormat.test.ts @@ -626,7 +626,7 @@ it("records an AVB footer and rejects vbmeta that overlaps a component", () => { imageEnd: facts.imageEnd, headerOccupied: facts.headerOccupied, components: facts.components, - tail: null, + tail: image.subarray(image.length - 64), }); expect(bare).toMatchObject({ ok: true, @@ -639,7 +639,7 @@ it("records an AVB footer and rejects vbmeta that overlaps a component", () => { imageEnd: facts.imageEnd, headerOccupied: facts.headerOccupied, components: facts.components, - tail: null, + tail: short.subarray(short.length - 64), }); expect(shortFooter.ok).toBe(true); if (!shortFooter.ok) return; diff --git a/src/artifacts/AndroidBootImageFormat.ts b/src/artifacts/AndroidBootImageFormat.ts index 9253f8c4aa..a26d765ce9 100644 --- a/src/artifacts/AndroidBootImageFormat.ts +++ b/src/artifacts/AndroidBootImageFormat.ts @@ -1131,6 +1131,15 @@ const rejectVbmetaOverlap = ( ); }; +/** The partition-tail footer can start anywhere after the declared boot bytes. */ +export const avbFooterOffset = ( + fileSize: number, + dataEnd: number, +): number | null => { + const offset = fileSize - AVB_FOOTER_SIZE; + return offset >= dataEnd ? offset : null; +}; + const readAvbFooter = (input: { readonly fileSize: number; readonly dataEnd: number; @@ -1148,21 +1157,19 @@ const readAvbFooter = (input: { `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 < AVB_FOOTER_SIZE) - return absentFooter([ - `${trailing} trailing bytes follow the boot image and are shorter than a 64-byte AVB footer.`, - ]); - const tail = input.tail; - if (tail === null || tail.length !== AVB_FOOTER_SIZE) + const { tail } = input; + const offset = avbFooterOffset(input.fileSize, input.dataEnd); + if (offset !== null && (tail === null || tail.length !== AVB_FOOTER_SIZE)) return formatError("AVB footer bytes were not supplied"); - if (!tail.subarray(0, 4).equals(AVB_FOOTER_MAGIC_BYTES)) + if (offset === null || !tail?.subarray(0, 4).equals(AVB_FOOTER_MAGIC_BYTES)) { + const trailing = input.fileSize - input.imageEnd; + if (trailing <= 0) return absentFooter(); return absentFooter([ - `${trailing} trailing bytes follow the boot image and were not decoded as an AVB footer.`, + trailing < AVB_FOOTER_SIZE + ? `${trailing} trailing bytes follow the boot image and are shorter than a 64-byte AVB footer.` + : `${trailing} trailing bytes follow the boot image and were not decoded as an AVB footer.`, ]); + } 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"); @@ -1194,7 +1201,7 @@ const readAvbFooter = (input: { /** * Report an AVB footer when the last 64 bytes start with AVBf and sit past - * the page-aligned image. Sizes are recorded; the footer is not verified. + * the actual boot components. Sizes are recorded; the footer is not verified. */ export const inspectAvbFooter = (input: { readonly fileSize: number;