fix(artifacts): accept Android boot images without final page padding (#11857)

* fix(artifacts): accept Android boot images without final page padding

The truncation check required the file to reach the page-aligned end
after the last component, so an image holding every declared byte but
omitting the trailing zero padding was refused as truncated, and the
message named the padded end. Bound truncation by the last declared
header or component byte instead, keep the page-aligned end for
locating an AVB footer, and check the AVB original image size against
the declared bytes.

Fixes #11854

* test(artifacts): check boot image declared ends against the fixture layout

Derive each expected end from the fixture layout instead of the
parser's dataEnd, so a declared structure dropped from the coverage
check fails the test.

---------

Co-authored-by: Anurag Singh <211784907+Anurag-5ingh@users.noreply.github.com>
This commit is contained in:
Anurag SinghandAnurag Singh authored and GitHub committed 2026-11-17 15:06:28 -05:00
1 parent 84ee604c6d
commit bb5278c7e2
7 files changed
+192 -17

No files matched your search

+46 -12
View File
@@ -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 {