Files
reverse-engineering-anything/src/artifacts/AndroidVbmetaArtifactReader.ts
T
3e8f6a43a9 feat(artifacts): decode Android Verified Boot vbmeta images offline (#13239)
* feat(artifacts): decode Android Verified Boot vbmeta images offline

* fix(artifacts): locate vbmeta tail-length failures at their length fields

* fix(artifacts): make vbmeta admission budgets adjustable per call

* fix(firmware): refuse vbmeta fields too long to hex-encode

With raised descriptor_bytes and auxiliary_block_bytes limits, a valid
KERNEL_CMDLINE longer than half the host string bound passed the byte
limits, and toString("hex") then threw ERR_STRING_TOO_LONG, which
surfaced as an I/O error. Check the field against the host bound before
hex encoding and report a typed capacity failure with its own limit.

* fix(firmware): check vbmeta extents before admission budgets

A header whose declared blocks do not fit the file, or whose descriptor
range does not fit its auxiliary block, now reports truncation or a
malformed range instead of a limit a larger budget cannot repair. The
over-budget fixtures are now fully present so they still prove the limit.

* perf(firmware): count vbmeta descriptor tags in one pass and reuse readers

The summary counts every descriptor tag in a single loop instead of six
filtered arrays, and androidVbmeta reuses one BinaryReader per buffer.
The malformed-header refusal fixtures follow main's unsupported_target
classification for malformed target bytes.

* chore: regenerate advertised schema hash after merging main

* docs(android-vbmeta): name unsupported_target for structural refusals

Malformed vbmeta headers classify as unsupported_target, not
invalid_request, matching analysisErrorClassification and the refusal
fixtures.

* chore: regenerate advertised schema hashes after merging main

* chore: regenerate advertised schema hashes after merging main

* fix: restore Android vbmeta artifact reader after merging main

* test(artifacts): expect partial for the CLI vbmeta descriptor refusal

* feat(artifacts): preserve vbmeta descriptor bytes and partial refusals

Co-authored-by: iamcipherdev <268238676+iamcipherdev@users.noreply.github.com>

* fix(artifacts): use Buffer.equals in vbmeta tests and update advertised schema summaries

---------

Co-authored-by: Matt Van Horn <455140+mvanhorn@users.noreply.github.com>
Co-authored-by: Matt Van Horn <mvanhorn@MacBook-Pro-10.local>
Co-authored-by: Vincent Koc <vincentkoc@ieee.org>
Co-authored-by: iamcipherdev <268238676+iamcipherdev@users.noreply.github.com>
Co-authored-by: morluto <76467478+morluto@users.noreply.github.com>
2026-11-26 20:05:59 +00:00

143 lines
4.2 KiB
TypeScript

import type { ParseFailureError } from "../domain/parseFailure.js";
import {
DEFAULT_ANDROID_VBMETA_READER_LIMITS,
type AndroidVbmetaReaderLimits,
} from "../domain/artifactReaderLimits.js";
import {
ANDROID_VBMETA_LIMIT_SETTINGS,
AVB_HEADER_SIZE,
androidVbmetaImageExtent,
parseAndroidVbmeta,
type AndroidVbmetaChild,
} from "../domain/firmware/androidVbmeta.js";
import type {
AndroidVbmetaDescriptorEntryMetadata,
AndroidVbmetaEntryMetadata,
AndroidVbmetaMetadata,
} from "../domain/formatMetadata/androidAndSystemMetadata.js";
import {
artifactParseFailure,
artifactReaderLimitFailure,
} from "../resources/index.js";
import { readArtifactFileBytes } from "./ArtifactFileRange.js";
import {
PositionalArtifactReader,
type PositionalArtifactEntries,
type PositionalArtifactEntry,
} from "./PositionalArtifactReader.js";
import type { StableRegularFileDescriptor } from "../filesystem/RegularFile.js";
type VbmetaEntry = PositionalArtifactEntry & {
readonly formatMetadata:
| AndroidVbmetaEntryMetadata
| AndroidVbmetaDescriptorEntryMetadata;
};
/** Read a standalone vbmeta image without verifying or modifying it. */
export class AndroidVbmetaArtifactReader extends PositionalArtifactReader<AndroidVbmetaMetadata> {
override readonly format = "android-vbmeta" as const;
readonly #limits: AndroidVbmetaReaderLimits;
constructor(
path: string,
admitted?: StableRegularFileDescriptor,
limits: AndroidVbmetaReaderLimits = DEFAULT_ANDROID_VBMETA_READER_LIMITS,
) {
super(path, "Android vbmeta", admitted);
this.#limits = limits;
}
formatMetadata(): AndroidVbmetaMetadata | undefined {
const metadata = this.preparedFormatMetadata;
return metadata === undefined ? undefined : structuredClone(metadata);
}
protected override async prepareEntries(
signal?: AbortSignal,
): Promise<PositionalArtifactEntries<AndroidVbmetaMetadata>> {
const { handle, initial } = await this.verifyCarrier(signal);
const headerBytes = await readArtifactFileBytes(
this.format,
handle,
0,
Math.min(initial.size, AVB_HEADER_SIZE),
initial.size,
signal,
);
this.assertOpen(signal);
const extent = androidVbmetaImageExtent(
headerBytes,
initial.size,
this.#limits,
);
if (!extent.ok) throw parseFailure(extent.error);
const imageBytes = await readArtifactFileBytes(
this.format,
handle,
0,
extent.value.imageSize,
initial.size,
signal,
);
this.assertOpen(signal);
const parsed = parseAndroidVbmeta(imageBytes, initial.size, this.#limits);
if (!parsed.ok) throw parseFailure(parsed.error);
return {
entries: parsed.value.children.map(entryFor),
formatMetadata: {
kind: "android-vbmeta",
...parsed.value.metadata,
},
coverage: {
status:
parsed.value.layoutSupported &&
parsed.value.metadata.descriptor_failure === null
? "complete"
: "unknown",
limitations: [...parsed.value.limitations],
},
};
}
}
const parseFailure = (failure: ParseFailureError): unknown => {
const facts = failure.failure;
if (facts.kind === "limit") {
const selected = ANDROID_VBMETA_LIMIT_SETTINGS.get(facts.limit.name);
if (selected !== undefined)
return artifactReaderLimitFailure(
`artifact_limits.android_vbmeta.${selected.setting}`,
facts.limit.maximum,
selected.unit,
facts.limit.required,
"capacity",
);
}
return artifactParseFailure(failure, undefined, () => "capacity");
};
const entryFor = (child: AndroidVbmetaChild): VbmetaEntry => ({
path: child.path,
kind: "file",
declaredSize: child.size,
compressedSize: null,
executable: false,
encrypted: false,
byteOffset: child.offset,
declaredIntegrity: null,
unpacked: false,
limitations: [],
adapterKey: `android-vbmeta:${child.path}`,
formatMetadata:
child.role === "descriptor"
? {
kind: "android-vbmeta-descriptor",
index: child.index,
tag: String(child.descriptor.tag),
tag_name: child.descriptor.tag_name,
descriptor_size: child.size,
}
: { kind: "android-vbmeta-entry", role: child.role },
sourcePath: child.path,
});