Skip to content

The agent that grows with you

fix(desktop): the arch audit reads a .NET AnyCPU helper's CLR flags, not its i386 COFF machine

alt-glitch20c519ec4821
diff --git a/apps/desktop/scripts/audit-bundle-arch.mjs b/apps/desktop/scripts/audit-bundle-arch.mjs
index 9aaeaabb2c7..ea97820478d 100644
--- a/apps/desktop/scripts/audit-bundle-arch.mjs
+++ b/apps/desktop/scripts/audit-bundle-arch.mjs
@@ -114,7 +114,45 @@ export function peArch(machineCode) {
* not a shrug.
*/
export function archMatches(arches, targetArch) {
- return arches.some((a) => a === targetArch || (targetArch === "arm64" && a === "arm64ec"))
+ return arches.some((a) => a === targetArch || a === "anycpu" || (targetArch === "arm64" && a === "arm64ec"))
+}
+
+const COMIMAGE_FLAGS_ILONLY = 0x1
+const COMIMAGE_FLAGS_32BITREQUIRED = 0x2
+const COMIMAGE_FLAGS_32BITPREFERRED = 0x20000
+
+/**
+ * A .NET IL-only AnyCPU assembly carries machine 0x14c (i386) in its COFF
+ * header, yet the CLR JITs it to the host's native arch (x64, arm64). Its
+ * real arch lives in the CLR header flags. `readAt(offset, length)` returns
+ * the file bytes there; returns true only for IL-only, non-32-bit PEs.
+ */
+export function isAnyCpuAssembly(readAt, peOffset) {
+ const coff = readAt(peOffset, 24)
+ if (coff.length < 24) return false
+ const sections = coff.readUInt16LE(6)
+ const optionalSize = coff.readUInt16LE(20)
+ const optional = readAt(peOffset + 24, optionalSize)
+ if (optional.length < optionalSize || optionalSize < 2) return false
+ const dataDirs = { 0x10b: 96, 0x20b: 112 }[optional.readUInt16LE(0)]
+ if (dataDirs === undefined || optionalSize < dataDirs + 15 * 8) return false
+ const clrRva = optional.readUInt32LE(dataDirs + 14 * 8)
+ if (!clrRva) return false
+ const table = readAt(peOffset + 24 + optionalSize, sections * 40)
+ for (let base = 0; base + 40 <= table.length; base += 40) {
+ const virtualSize = table.readUInt32LE(base + 8)
+ const virtualAddress = table.readUInt32LE(base + 12)
+ const rawSize = table.readUInt32LE(base + 16)
+ const rawPointer = table.readUInt32LE(base + 20)
+ if (clrRva >= virtualAddress && clrRva < virtualAddress + Math.max(virtualSize, rawSize)) {
+ const cor = readAt(rawPointer + clrRva - virtualAddress, 20)
+ if (cor.length < 20) return false
+ const flags = cor.readUInt32LE(16)
+ return Boolean(flags & COMIMAGE_FLAGS_ILONLY)
+ && !(flags & COMIMAGE_FLAGS_32BITREQUIRED) && !(flags & COMIMAGE_FLAGS_32BITPREFERRED)
+ }
+ }
+ return false
}
/**
@@ -145,7 +183,13 @@ function classifyFile(filePath) {
if (m < 6 || peHead.readUInt32LE(0) !== 0x00004550) {
return null // MZ without a PE header: DOS-era stub or corrupt — not a shippable binary format we know.
}
- return { format: "pe", arches: [peArch(peHead.readUInt16LE(4))] }
+ const readAt = (offset, length) => {
+ const out = Buffer.alloc(length)
+ return out.subarray(0, fs.readSync(fd, out, 0, length, offset))
+ }
+ const arch = peArch(peHead.readUInt16LE(4))
+ if (arch === "ia32" && isAnyCpuAssembly(readAt, sniffed.peHeaderOffset)) return { format: "pe", arches: ["anycpu"] }
+ return { format: "pe", arches: [arch] }
} finally {
fs.closeSync(fd)
}
diff --git a/apps/desktop/scripts/audit-bundle-arch.test.mjs b/apps/desktop/scripts/audit-bundle-arch.test.mjs
index 4f479f56993..2ddd25eb55d 100644
--- a/apps/desktop/scripts/audit-bundle-arch.test.mjs
+++ b/apps/desktop/scripts/audit-bundle-arch.test.mjs
@@ -165,3 +165,42 @@ test('tree and CLI audit real second-read PE bytes and fail closed on absent art
assert.match(failed.stderr, /wrong-arch.dat/)
} finally { fs.rmSync(root, { recursive: true, force: true }) }
})
+
+// A .NET IL-only assembly: COFF machine 0x14c, CLR header in section .text.
+function dotnetPe(corFlags) {
+ const pe = Buffer.alloc(0x400)
+ mzStub(0x80).copy(pe)
+ pe.writeUInt32LE(0x4550, 0x80)
+ pe.writeUInt16LE(0x014c, 0x84) // machine: i386
+ pe.writeUInt16LE(1, 0x86) // one section
+ pe.writeUInt16LE(0xe0, 0x94) // PE32 optional header size
+ const optional = 0x98
+ pe.writeUInt16LE(0x10b, optional)
+ pe.writeUInt32LE(0x2008, optional + 96 + 14 * 8) // CLR runtime header RVA
+ pe.writeUInt32LE(0x48, optional + 96 + 14 * 8 + 4)
+ const section = optional + 0xe0
+ pe.write('.text', section, 'latin1')
+ pe.writeUInt32LE(0x100, section + 8) // virtual size
+ pe.writeUInt32LE(0x2000, section + 12) // virtual address
+ pe.writeUInt32LE(0x100, section + 16) // raw size
+ pe.writeUInt32LE(0x200, section + 20) // raw pointer
+ pe.writeUInt32LE(0x48, 0x208) // COR20 cb
+ pe.writeUInt32LE(corFlags, 0x208 + 16)
+ return pe
+}
+
+test('an IL-only AnyCPU .NET assembly passes every Windows arch; 32-bit-bound IL and native ia32 do not', () => {
+ const root = fs.mkdtempSync(path.join(os.tmpdir(), 'arch-clr-'))
+ try {
+ fs.writeFileSync(path.join(root, 'anycpu.exe'), dotnetPe(0x1))
+ fs.writeFileSync(path.join(root, 'x86-required.exe'), dotnetPe(0x1 | 0x2))
+ fs.writeFileSync(path.join(root, 'x86-preferred.exe'), dotnetPe(0x1 | 0x20000))
+ const native = dotnetPe(0x1)
+ native.fill(0, 0x98 + 96 + 14 * 8, 0x98 + 96 + 15 * 8) // no CLR header: a native i386 PE
+ fs.writeFileSync(path.join(root, 'native-ia32.exe'), native)
+ for (const arch of ['x64', 'arm64']) {
+ const { mismatches } = auditTree(root, arch)
+ assert.deepEqual(mismatches.map(m => m.file).sort(), ['native-ia32.exe', 'x86-preferred.exe', 'x86-required.exe'])
+ }
+ } finally { fs.rmSync(root, { recursive: true, force: true }) }
+})