Skip to content

Commit 74f5169

Browse files
committed
fix(release): enforce packaged Linux glibc requirements
1 parent 7fb54eb commit 74f5169

4 files changed

Lines changed: 239 additions & 0 deletions

File tree

‎docs/operations/development.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -304,6 +304,7 @@ C/C++ build tools, libsecret development headers, pkg-config, and ImageMagick.
304304
Bytecode preparation also runs packaged Electron and requires GTK, NSS, ALSA, and GBM runtime libraries.
305305
Install Rust 1.95 or newer through [rustup](https://rust-lang.org/install), using the stable
306306
toolchain as release CI does.
307+
The packaged ELF compatibility check uses binutils’ `readelf`, included by the C/C++ toolchain.
307308

308309
Ubuntu 24.04 and Debian 13:
309310

‎scripts/linux-after-pack.ts‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,34 @@ import * as NodeFSP from "node:fs/promises";
33
import * as NodeModule from "node:module";
44
import * as NodePath from "node:path";
55

6+
import { scanGlibcNeeds } from "./linux-glibc.ts";
7+
68
const prepareServerCompileCache: (context: AfterPackContext) => Promise<void> =
79
NodeModule.createRequire(import.meta.url)("./prepare-server-compile-cache.cjs");
810

911
interface AfterPackContext {
1012
readonly appOutDir: string;
13+
readonly electronPlatformName: string;
14+
readonly targets: ReadonlyArray<{
15+
readonly name: string;
16+
readonly options?: { depends?: string | string[] } | null;
17+
}>;
1118
}
1219

1320
// Electron's Linux zips can ship world-writable files (44.10.0 has 0777 binaries), and
1421
// electron-builder copies zip modes verbatim into the .deb and AppImage.
1522
export default async function afterPack(context: AfterPackContext): Promise<void> {
1623
await prepareServerCompileCache(context);
1724
await removeSharedWriteAccess(context.appOutDir);
25+
const { minimum } = await scanGlibcNeeds(context.appOutDir);
26+
for (const target of context.targets) {
27+
if (target.name !== "deb") continue;
28+
if (!target.options || !Array.isArray(target.options.depends)) {
29+
throw new Error("Linux deb target must configure its dependencies before afterPack");
30+
}
31+
// FPM snapshots config when targets are created; update the target's resolved options.
32+
target.options.depends = [...target.options.depends, `libc6 (>= ${minimum})`];
33+
}
1834
}
1935

2036
// Walks by hand: recursive readdir follows directory symlinks out of the app folder.

‎scripts/linux-glibc.test.ts‎

Lines changed: 131 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,131 @@
1+
// @effect-diagnostics nodeBuiltinImport:off - Exercises the packaging filesystem and readelf.
2+
import * as HostProcess from "@t3tools/shared/HostProcess";
3+
import * as NodeFSP from "node:fs/promises";
4+
import * as NodeOS from "node:os";
5+
import * as NodePath from "node:path";
6+
import { describe, expect, it } from "vite-plus/test";
7+
8+
import afterPack from "./linux-after-pack.ts";
9+
import { parseGlibcNeeds, scanGlibcNeeds } from "./linux-glibc.ts";
10+
11+
it.each([
12+
["GLIBC_2.9", "2.9"],
13+
["GLIBC_2.34", "2.34"],
14+
["GLIBC_2.2.5", "2.2.5"],
15+
["GLIBC_ABI_DT_RELR", "2.36"],
16+
])("reads required %s without counting newer definitions", (name, expected) => {
17+
expect(
18+
parseGlibcNeeds(`
19+
Version definition section '.gnu.version_d' contains 1 entry:
20+
0x0010: Rev: 1 Flags: none Index: 2 Cnt: 1 Name: GLIBC_2.99
21+
Version needs section '.gnu.version_r' contains 1 entry:
22+
000000: Version: 1 File: libc.so.6 Cnt: 3
23+
0x0010: Name: GLIBC_2.2 Flags: none Version: 3
24+
0x0020: Name: ${name} Flags: none Version: 4
25+
0x0030: Name: GLIBCXX_3.4.30 Flags: none Version: 5
26+
`),
27+
).toBe(expected);
28+
});
29+
30+
it("compares required versions numerically", () => {
31+
expect(
32+
parseGlibcNeeds(`Version needs section '.gnu.version_r' contains 1 entry:
33+
0x0010: Name: GLIBC_2.9 Flags: none Version: 3
34+
0x0020: Name: GLIBC_2.10 Flags: none Version: 4
35+
`),
36+
).toBe("2.10");
37+
});
38+
39+
it("ignores definitions, symbol tables, and unversioned/static binaries", () => {
40+
expect(parseGlibcNeeds("No version information found in this file.")).toBeUndefined();
41+
expect(
42+
parseGlibcNeeds(`
43+
Version symbols section '.gnu.version' contains 1 entry:
44+
000: 2 (GLIBC_2.99)
45+
Version definition section '.gnu.version_d' contains 1 entry:
46+
0x0010: Rev: 1 Flags: none Index: 2 Cnt: 1 Name: GLIBC_2.99
47+
`),
48+
).toBeUndefined();
49+
});
50+
51+
it.each(["GLIBC_PRIVATE", "GLIBC_ABI_UNKNOWN"])("rejects unhandled requirement %s", (name) => {
52+
expect(() =>
53+
parseGlibcNeeds(`Version needs section '.gnu.version_r' contains 1 entry:
54+
0x0010: Name: ${name} Flags: none Version: 3
55+
`),
56+
).toThrow(name);
57+
});
58+
59+
// Minimal ELF64 with a linked string table and a version NEEDS section, for both release CPUs.
60+
function elfFixture(machine: number, version: string): Buffer {
61+
const strings = Buffer.from(`\0libc.so.6\0GLIBC_${version}\0`);
62+
const names = Buffer.from("\0.shstrtab\0.dynstr\0.gnu.version_r\0");
63+
const stringsOffset = 64;
64+
const namesOffset = stringsOffset + strings.length;
65+
const needsOffset = namesOffset + names.length;
66+
const sectionsOffset = needsOffset + 32;
67+
const elf = Buffer.alloc(sectionsOffset + 4 * 64);
68+
Buffer.from([0x7f, 0x45, 0x4c, 0x46, 2, 1, 1]).copy(elf);
69+
elf.writeUInt16LE(3, 16);
70+
elf.writeUInt16LE(machine, 18);
71+
elf.writeUInt32LE(1, 20);
72+
elf.writeBigUInt64LE(BigInt(sectionsOffset), 40);
73+
elf.writeUInt16LE(64, 52);
74+
elf.writeUInt16LE(64, 58);
75+
elf.writeUInt16LE(4, 60);
76+
elf.writeUInt16LE(1, 62);
77+
strings.copy(elf, stringsOffset);
78+
names.copy(elf, namesOffset);
79+
elf.writeUInt16LE(1, needsOffset);
80+
elf.writeUInt16LE(1, needsOffset + 2);
81+
elf.writeUInt32LE(1, needsOffset + 4);
82+
elf.writeUInt32LE(16, needsOffset + 8);
83+
elf.writeUInt16LE(2, needsOffset + 16 + 6);
84+
elf.writeUInt32LE(11, needsOffset + 16 + 8);
85+
for (const [index, name, type, offset, size, link] of [
86+
[1, 1, 3, namesOffset, names.length, 0],
87+
[2, 11, 3, stringsOffset, strings.length, 0],
88+
[3, 19, 0x6ffffffe, needsOffset, 32, 2],
89+
] as const) {
90+
const section = sectionsOffset + index * 64;
91+
elf.writeUInt32LE(name, section);
92+
elf.writeUInt32LE(type, section + 4);
93+
elf.writeBigUInt64LE(BigInt(offset), section + 24);
94+
elf.writeBigUInt64LE(BigInt(size), section + 32);
95+
elf.writeUInt32LE(link, section + 40);
96+
elf.writeBigUInt64LE(1n, section + 48);
97+
}
98+
elf.writeUInt32LE(1, sectionsOffset + 3 * 64 + 44);
99+
return elf;
100+
}
101+
102+
describe.skipIf(HostProcess.Platform.defaultValue() !== "linux")("packaged ELF scan", () => {
103+
it.each([62, 183])(
104+
"checks executable, addon and extensionless ELF requirements for machine %s",
105+
async (machine) => {
106+
const root = await NodeFSP.mkdtemp(NodePath.join(NodeOS.tmpdir(), "t3-glibc-"));
107+
try {
108+
const app = NodePath.join(root, "app");
109+
const native = NodePath.join(app, "resources/app.asar.unpacked/node_modules/addon");
110+
await NodeFSP.mkdir(native, { recursive: true });
111+
await NodeFSP.writeFile(NodePath.join(app, "t3code"), elfFixture(machine, "2.25"));
112+
await NodeFSP.writeFile(NodePath.join(native, "pty.node"), elfFixture(machine, "2.34"));
113+
await NodeFSP.writeFile(NodePath.join(app, "not-elf"), "GLIBC_2.99");
114+
await NodeFSP.writeFile(NodePath.join(root, "outside"), elfFixture(machine, "2.99"));
115+
await NodeFSP.symlink("../outside", NodePath.join(app, "symlink"));
116+
expect(await scanGlibcNeeds(app)).toEqual({ minimum: "2.34", elfCount: 2 });
117+
const deb = { name: "deb", options: { depends: ["libnss3"] } };
118+
await afterPack({ appOutDir: app, electronPlatformName: "linux", targets: [deb] });
119+
expect(deb.options.depends).toEqual(["libnss3", "libc6 (>= 2.34)"]);
120+
await NodeFSP.writeFile(NodePath.join(native, "helper"), elfFixture(machine, "2.40"));
121+
await expect(
122+
afterPack({ appOutDir: app, electronPlatformName: "linux", targets: [deb] }),
123+
).rejects.toThrow(/helper.*GLIBC_2\.40.*2\.39/s);
124+
await NodeFSP.writeFile(NodePath.join(native, "helper"), Buffer.from("\x7fELFbroken"));
125+
await expect(scanGlibcNeeds(app)).rejects.toThrow(/helper/);
126+
} finally {
127+
await NodeFSP.rm(root, { recursive: true, force: true });
128+
}
129+
},
130+
);
131+
});

‎scripts/linux-glibc.ts‎

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
// @effect-diagnostics nodeBuiltinImport:off globalConsole:off - Runs in electron-builder's Node afterPack hook.
2+
import * as NodeChildProcess from "node:child_process";
3+
import * as NodeFSP from "node:fs/promises";
4+
import * as NodePath from "node:path";
5+
import * as NodeUtil from "node:util";
6+
7+
// Both Linux release jobs build on Ubuntu 24.04; this is a build ceiling, not a distro support promise.
8+
const GLIBC_BUILD_CEILING = "2.39";
9+
const readelf = NodeUtil.promisify(NodeChildProcess.execFile);
10+
11+
function compareVersions(left: string, right: string): number {
12+
const a = left.split(".").map(Number);
13+
const b = right.split(".").map(Number);
14+
for (let i = 0; i < Math.max(a.length, b.length); i++) {
15+
const difference = (a[i] ?? 0) - (b[i] ?? 0);
16+
if (difference !== 0) return difference;
17+
}
18+
return 0;
19+
}
20+
21+
export function parseGlibcNeeds(output: string): string | undefined {
22+
let inNeeds = false;
23+
let minimum: string | undefined;
24+
for (const line of output.split("\n")) {
25+
if (line.startsWith("Version ")) inNeeds = line.startsWith("Version needs section ");
26+
if (!inNeeds) continue;
27+
const name = /\bName:\s+(GLIBC_\S+)/.exec(line)?.[1];
28+
if (!name) continue;
29+
// DT_RELR's named ABI requirement was introduced in glibc 2.36.
30+
const version =
31+
name === "GLIBC_ABI_DT_RELR" ? "2.36" : /^GLIBC_(\d+(?:\.\d+)+)$/.exec(name)?.[1];
32+
if (!version) throw new Error(`Unhandled glibc requirement: ${name}`);
33+
if (!minimum || compareVersions(version, minimum) > 0) minimum = version;
34+
}
35+
return minimum;
36+
}
37+
38+
export async function scanGlibcNeeds(appOutDir: string): Promise<{
39+
minimum: string;
40+
elfCount: number;
41+
}> {
42+
let minimum: string | undefined;
43+
let elfCount = 0;
44+
async function walk(directory: string): Promise<void> {
45+
for (const entry of await NodeFSP.readdir(directory, { withFileTypes: true })) {
46+
const file = NodePath.join(directory, entry.name);
47+
if (entry.isDirectory()) {
48+
await walk(file);
49+
continue;
50+
}
51+
if (!entry.isFile()) continue;
52+
const handle = await NodeFSP.open(file, "r");
53+
let isElf: boolean;
54+
try {
55+
const magic = Buffer.alloc(4);
56+
await handle.read(magic, 0, 4, 0);
57+
isElf = magic.equals(Buffer.from([0x7f, 0x45, 0x4c, 0x46]));
58+
} finally {
59+
await handle.close();
60+
}
61+
if (!isElf) continue;
62+
elfCount++;
63+
try {
64+
// readelf inspects either release architecture without executing the binary.
65+
const { stdout, stderr } = await readelf("readelf", ["--wide", "--version-info", file], {
66+
env: { ...process.env, LC_ALL: "C" },
67+
maxBuffer: 16 * 1024 * 1024,
68+
});
69+
if (stderr.trim()) throw new Error(stderr.trim());
70+
const required = parseGlibcNeeds(stdout);
71+
if (required && compareVersions(required, GLIBC_BUILD_CEILING) > 0) {
72+
throw new Error(
73+
`requires GLIBC_${required}, exceeding build ceiling ${GLIBC_BUILD_CEILING}`,
74+
);
75+
}
76+
if (required && (!minimum || compareVersions(required, minimum) > 0)) minimum = required;
77+
} catch (cause) {
78+
throw new Error(
79+
`Cannot validate glibc compatibility of ${file}: ${cause instanceof Error ? cause.message : String(cause)}`,
80+
{ cause },
81+
);
82+
}
83+
}
84+
}
85+
await walk(appOutDir);
86+
if (elfCount === 0 || !minimum) throw new Error(`No glibc requirements found in ${appOutDir}`);
87+
console.log(
88+
`[linux-glibc] ${elfCount} ELF files; requires glibc >= ${minimum}; build ceiling ${GLIBC_BUILD_CEILING}`,
89+
);
90+
return { minimum, elfCount };
91+
}

0 commit comments

Comments
 (0)