mirror of
https://github.com/morluto/rea.git
synced 2026-11-18 20:53:00 +00:00
fix(ota): merge partition info without discarding integrity claims (#11836)
This commit is contained in:
1 parent
004c43e32a
commit
75482a9ef9
5 files changed
+194
-8
No files matched your search
@@ -46,6 +46,11 @@ and `estimate_cow_size`. Operation types include REPLACE, ZERO,
|
||||
REPLACE_BZ/XZ/ZSTD, SOURCE_COPY, BSDIFF family, PUFFDIFF, ZUCCHINI and
|
||||
LZ4DIFF.
|
||||
|
||||
Repeated encodings of the singular old/new `PartitionInfo` messages merge
|
||||
their fields under Protobuf rules. A later size-only or empty message does
|
||||
not erase an earlier declared hash; a repeated scalar field keeps its last
|
||||
value. The resulting size and hash feed both inventory and extraction.
|
||||
|
||||
Metadata and payload signature blobs are counted from the `Signatures`
|
||||
protobuf. `signatures_verified` is always `false`: sizes and counts are
|
||||
facts, not a cryptographic check. `payload_properties.txt` and
|
||||
|
||||
@@ -30,6 +30,9 @@ export interface AndroidOtaPartitionFixture {
|
||||
readonly newHash?: Uint8Array;
|
||||
readonly oldSize?: number;
|
||||
readonly oldHash?: Uint8Array;
|
||||
/** Exact occurrences of the singular message, for protobuf merge controls. */
|
||||
readonly oldInfoFragments?: readonly AndroidOtaPartitionInfoFixture[];
|
||||
readonly newInfoFragments?: readonly AndroidOtaPartitionInfoFixture[];
|
||||
readonly version?: string;
|
||||
readonly runPostinstall?: boolean;
|
||||
readonly postinstallPath?: string;
|
||||
@@ -40,6 +43,11 @@ export interface AndroidOtaPartitionFixture {
|
||||
readonly omitNewHash?: boolean;
|
||||
}
|
||||
|
||||
interface AndroidOtaPartitionInfoFixture {
|
||||
readonly size?: number;
|
||||
readonly hash?: Uint8Array;
|
||||
}
|
||||
|
||||
export interface AndroidOtaGroupFixture {
|
||||
readonly name: string;
|
||||
readonly size?: number;
|
||||
@@ -201,9 +209,14 @@ const encodePartition = (
|
||||
writer.tag(3, WireType.LengthDelimited).string(partition.postinstallPath);
|
||||
if (partition.filesystemType !== undefined)
|
||||
writer.tag(4, WireType.LengthDelimited).string(partition.filesystemType);
|
||||
if (partition.oldSize !== undefined || partition.oldHash !== undefined) {
|
||||
const oldInfos =
|
||||
partition.oldInfoFragments ??
|
||||
(partition.oldSize !== undefined || partition.oldHash !== undefined
|
||||
? [{ size: partition.oldSize, hash: partition.oldHash }]
|
||||
: []);
|
||||
for (const info of oldInfos) {
|
||||
writer.tag(6, WireType.LengthDelimited).fork();
|
||||
encodePartitionInfo(writer, partition.oldSize, partition.oldHash);
|
||||
encodePartitionInfo(writer, info.size, info.hash);
|
||||
writer.join();
|
||||
}
|
||||
const image = partitionImage(partition, blockSize);
|
||||
@@ -214,9 +227,14 @@ const encodePartition = (
|
||||
(image === undefined
|
||||
? undefined
|
||||
: createHash("sha256").update(image).digest()));
|
||||
if (newSize !== undefined || newHash !== undefined) {
|
||||
const newInfos =
|
||||
partition.newInfoFragments ??
|
||||
(newSize !== undefined || newHash !== undefined
|
||||
? [{ size: newSize, hash: newHash }]
|
||||
: []);
|
||||
for (const info of newInfos) {
|
||||
writer.tag(7, WireType.LengthDelimited).fork();
|
||||
encodePartitionInfo(writer, newSize, newHash);
|
||||
encodePartitionInfo(writer, info.size, info.hash);
|
||||
writer.join();
|
||||
}
|
||||
for (const operation of partition.operations) {
|
||||
|
||||
@@ -47,6 +47,35 @@ const header = (fields: {
|
||||
return bytes;
|
||||
};
|
||||
|
||||
it.each([0, 4])(
|
||||
"merges empty PartitionInfo occurrences and keeps last scalar values (size: %s)",
|
||||
(size) => {
|
||||
const hash = Buffer.alloc(32, 0xab);
|
||||
const fragments = [
|
||||
{ size: 8, hash: Buffer.alloc(32) },
|
||||
{ size },
|
||||
{ hash },
|
||||
{},
|
||||
];
|
||||
const bytes = buildAndroidOtaPayload({
|
||||
partitions: [
|
||||
{
|
||||
name: "boot",
|
||||
oldInfoFragments: fragments,
|
||||
newInfoFragments: fragments,
|
||||
operations: [{ type: 0, data: Buffer.from("BOOT") }],
|
||||
},
|
||||
],
|
||||
});
|
||||
const parsed = parse(bytes);
|
||||
if (!parsed.ok) throw parsed.error;
|
||||
expect(parsed.value.partitions[0]).toMatchObject({
|
||||
oldPartitionInfo: { size, hashHex: hash.toString("hex") },
|
||||
newPartitionInfo: { size, hashHex: hash.toString("hex") },
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
it("recognizes CrAU v2 and refuses any other readable major version", () => {
|
||||
expect(recognizeAndroidOtaPayload(Buffer.from("nope"))).toBeUndefined();
|
||||
expect(recognizeAndroidOtaPayload(Buffer.from("CrAU"))).toEqual({
|
||||
|
||||
@@ -514,8 +514,10 @@ const decodeExtent = (
|
||||
|
||||
const decodePartitionInfo = (
|
||||
bytes: Uint8Array,
|
||||
info: PartitionInfoDraft,
|
||||
): Result<PartitionInfoDraft, AndroidOtaPayloadParseError> => {
|
||||
const info = emptyPartitionInfo();
|
||||
// Protobuf singular messages merge across occurrences. Only fields present
|
||||
// in this occurrence replace earlier values; an empty message erases none.
|
||||
const walked = walk(bytes, (field, wire, reader) => {
|
||||
if (field === 1 && wire === WireType.Varint) {
|
||||
info.size = unwrap(asSafeUint(reader.uint64(), "partition size"));
|
||||
@@ -622,11 +624,18 @@ const decodePartition = (
|
||||
return;
|
||||
}
|
||||
if (field === 6 && wire === WireType.LengthDelimited) {
|
||||
partition.oldPartitionInfo = unwrap(decodePartitionInfo(reader.bytes()));
|
||||
partition.oldPartitionInfo = unwrap(
|
||||
decodePartitionInfo(
|
||||
reader.bytes(),
|
||||
partition.oldPartitionInfo ?? emptyPartitionInfo(),
|
||||
),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (field === 7 && wire === WireType.LengthDelimited) {
|
||||
partition.newPartitionInfo = unwrap(decodePartitionInfo(reader.bytes()));
|
||||
partition.newPartitionInfo = unwrap(
|
||||
decodePartitionInfo(reader.bytes(), partition.newPartitionInfo),
|
||||
);
|
||||
return;
|
||||
}
|
||||
if (field === 8 && wire === WireType.LengthDelimited) {
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
import { readFile, writeFile } from "node:fs/promises";
|
||||
import { readFile, readdir, writeFile } from "node:fs/promises";
|
||||
import { createHash } from "node:crypto";
|
||||
import { join } from "node:path";
|
||||
|
||||
import { expect } from "vitest";
|
||||
@@ -12,11 +13,135 @@ import {
|
||||
artifactInventoryResultSchema,
|
||||
} from "../../src/domain/artifactGraph.js";
|
||||
import {
|
||||
buildAndroidOtaPayload,
|
||||
deltaAndroidOtaPayload,
|
||||
fullAndroidOtaPayload,
|
||||
v1AndroidOtaPayload,
|
||||
} from "../../src/domain/firmware/androidOtaPayload.fixture.js";
|
||||
|
||||
cliTest.for([false, true])(
|
||||
"retains split partition metadata and extracts verified bytes (hash first: %s)",
|
||||
async (hashFirst, { cli }) => {
|
||||
const root = await createTestTempDirectory("rea-ota-partition-info-cli-");
|
||||
const source = join(root, "payload.bin");
|
||||
const data = Buffer.from("BOOT");
|
||||
const hash = createHash("sha256").update(data).digest();
|
||||
const fragments = hashFirst
|
||||
? [{ hash }, { size: data.length }]
|
||||
: [{ size: data.length }, { hash }];
|
||||
const bytes = buildAndroidOtaPayload({
|
||||
partitions: [
|
||||
{
|
||||
name: "boot",
|
||||
oldInfoFragments: fragments,
|
||||
newInfoFragments: fragments,
|
||||
operations: [{ type: 0, data }],
|
||||
},
|
||||
],
|
||||
});
|
||||
await writeFile(source, bytes);
|
||||
const inspected = await cli.run({
|
||||
arguments: ["inspect-artifact", source, "--json"],
|
||||
});
|
||||
expect(inspected.exitCode, inspected.stdout + inspected.stderr).toBe(0);
|
||||
const inspection = artifactInspectionResultSchema.parse(
|
||||
parseEvidence(inspected.json).normalized_result,
|
||||
);
|
||||
const graph = artifactInventoryResultSchema.parse(
|
||||
inspection.substeps[0]?.evidence.normalized_result,
|
||||
);
|
||||
expect(
|
||||
graph.occurrences.find((row) => row.logical_path === "boot.img"),
|
||||
).toMatchObject({
|
||||
hash_status: "verified",
|
||||
format_metadata: {
|
||||
new_partition_info: {
|
||||
size: data.length,
|
||||
hash_hex: hash.toString("hex"),
|
||||
},
|
||||
old_partition_info: {
|
||||
size: data.length,
|
||||
hash_hex: hash.toString("hex"),
|
||||
},
|
||||
},
|
||||
});
|
||||
const extracted = await cli.run({
|
||||
arguments: ["extract-artifact", source, "--json"],
|
||||
environment: { TMPDIR: root, TMP: root, TEMP: root },
|
||||
});
|
||||
expect(extracted.exitCode, extracted.stdout + extracted.stderr).toBe(0);
|
||||
const result = artifactExtractionResultSchema.parse(
|
||||
parseEvidence(extracted.json).normalized_result,
|
||||
);
|
||||
expect(
|
||||
(await readFile(join(result.output_root, "boot.img"))).equals(data),
|
||||
).toBe(true);
|
||||
expect((await readFile(source)).equals(bytes)).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"enforces a split declared hash through shared integrity policies",
|
||||
async ({ cli }) => {
|
||||
const root = await createTestTempDirectory("rea-ota-split-hash-cli-");
|
||||
const source = join(root, "payload.bin");
|
||||
const data = Buffer.from("BOOT");
|
||||
const bytes = buildAndroidOtaPayload({
|
||||
partitions: [
|
||||
{
|
||||
name: "boot",
|
||||
newInfoFragments: [
|
||||
{ hash: Buffer.alloc(32, 0xab) },
|
||||
{ size: data.length },
|
||||
],
|
||||
operations: [{ type: 0, data }],
|
||||
},
|
||||
],
|
||||
});
|
||||
await writeFile(source, bytes);
|
||||
const environment = { TMPDIR: root, TMP: root, TEMP: root };
|
||||
const refused = await cli.run({
|
||||
arguments: ["extract-artifact", source, "--json"],
|
||||
environment,
|
||||
});
|
||||
expect(refused.exitCode).toBe(1);
|
||||
expect(refused.json).toMatchObject({
|
||||
code: "artifact_integrity_mismatch",
|
||||
details: {
|
||||
logical_path: "boot.img",
|
||||
declared_sha256: "ab".repeat(32),
|
||||
calculated_sha256: createHash("sha256").update(data).digest("hex"),
|
||||
},
|
||||
});
|
||||
expect(await readdir(root)).toEqual(["payload.bin"]);
|
||||
const recorded = await cli.run({
|
||||
arguments: [
|
||||
"inspect-artifact",
|
||||
source,
|
||||
"--integrity-policy",
|
||||
"record-and-continue",
|
||||
"--json",
|
||||
],
|
||||
});
|
||||
expect(recorded.exitCode, recorded.stdout + recorded.stderr).toBe(0);
|
||||
const inspection = artifactInspectionResultSchema.parse(
|
||||
parseEvidence(recorded.json).normalized_result,
|
||||
);
|
||||
const graph = artifactInventoryResultSchema.parse(
|
||||
inspection.substeps[0]?.evidence.normalized_result,
|
||||
);
|
||||
expect(graph.integrity_contradictions).toEqual([
|
||||
expect.objectContaining({
|
||||
logical_path: "boot.img",
|
||||
declared_sha256: "ab".repeat(32),
|
||||
observed_sha256: createHash("sha256").update(data).digest("hex"),
|
||||
trust: "observed-untrusted",
|
||||
}),
|
||||
]);
|
||||
expect((await readFile(source)).equals(bytes)).toBe(true);
|
||||
},
|
||||
);
|
||||
|
||||
cliTest(
|
||||
"inspects and extracts a full Android OTA payload through the compiled CLI",
|
||||
async ({ cli }) => {
|
||||
|
||||
Reference in new issue
Block a user