Allow systemd descriptors in PID namespace init

This commit is contained in:
Meta Vens 2026-09-28 00:24:02 +02:00
commit 71aa615d93
2 changed files with 18 additions and 17 deletions

View file

@ -588,25 +588,20 @@ function readAuthorityRecordBytes(bytes: Buffer, type: string, schemaId: string)
}
function inheritedDescriptorMap(): Map<string, number> {
if (process.env.LISTEN_PID !== currentSystemdListenPid()) throw new Error("Inherited Idunn descriptors do not belong to this process.");
if (!systemdListenPidMatches(process.env.LISTEN_PID, process.pid)) throw new Error("Inherited Idunn descriptors do not belong to this process.");
const count = Number(process.env.LISTEN_FDS);
const names = process.env.LISTEN_FDNAMES?.split(":") ?? [];
if (!Number.isInteger(count) || count < 1 || names.length !== count) throw new Error("Inherited Idunn descriptor list is malformed.");
return new Map(names.map((name, index) => [name, index + 3]));
}
function currentSystemdListenPid(): string {
try {
const status = fs.readFileSync("/proc/self/status", "utf8");
const namespacePids = /^NSpid:\s+([0-9]+)(?:\s|$)/mu.exec(status);
// systemd records LISTEN_PID in its PID namespace. In a PrivatePIDs
// service, Node's process.pid is namespace-local, while the first NSpid
// value is the same host PID systemd used for LISTEN_PID.
return namespacePids?.[1] ?? String(process.pid);
} catch (error) {
if ((error as NodeJS.ErrnoException).code === "ENOENT") return String(process.pid);
throw error;
}
export function systemdListenPidMatches(listenPid: string | undefined, processPid: number): boolean {
if (listenPid === String(processPid)) return true;
// systemd sets LISTEN_PID in the host PID namespace. With PrivatePIDs=yes,
// the service's main process is namespace PID 1 and cannot read that host
// PID through its private /proc mount. A descendant has a PID greater than
// 1 and must not reuse the inherited descriptor set.
return processPid === 1 && /^\d+$/u.test(listenPid ?? "") && Number(listenPid) > 1;
}
function readDescriptor(descriptor: number): Buffer {

View file

@ -10,6 +10,7 @@ import { decode, encode } from "@msgpack/msgpack";
import {
loadIdunnRuntimeAuthorityFromEnvironment,
signIdunnRuntimePresence,
systemdListenPidMatches,
} from "../src/idunn-runtime-authority";
import {
runtimePresenceActivationSigningMessage,
@ -21,6 +22,14 @@ const providerIdentityContext = "gamecult-provider-health-identity-v1";
const providerIdDomain = Buffer.from("gamecult.provider-health.identity.v1\0", "utf8");
const activationIdDomain = Buffer.from("idunn.runtime-activation.id.v1\0", "utf8");
test("matches systemd's host PID for the private PID namespace init only", () => {
assert.equal(systemdListenPidMatches(String(process.pid), process.pid), true);
assert.equal(systemdListenPidMatches("1725346", 1), true);
assert.equal(systemdListenPidMatches("1725346", 2), false);
assert.equal(systemdListenPidMatches("not-a-pid", 1), false);
assert.equal(systemdListenPidMatches(undefined, 1), false);
});
test("opens Idunn's machine-bound identity and signs the exact Expected incarnation twice", (context) => {
const directory = mkdtempSync(path.join(os.tmpdir(), "cultnet-runtime-authority-"));
context.after(() => rmSync(directory, { recursive: true, force: true }));
@ -86,14 +95,11 @@ test("opens Idunn's machine-bound identity and signs the exact Expected incarnat
process.env.GAMECULT_IDUNN_RUNTIME_BUNDLE = bundle;
process.env.GAMECULT_IDUNN_CANDIDATE_BIND = candidate;
process.env.GAMECULT_IDUNN_PROCESS_WRITE_LEASE = path.join(directory, "lease.cc");
// systemd reports the host PID while Node inside PrivatePIDs sees PID 1.
const listenPid = String(process.pid + 1000);
process.env.LISTEN_PID = listenPid;
process.env.LISTEN_PID = String(process.pid);
process.env.LISTEN_FDS = "2";
process.env.LISTEN_FDNAMES = "gamecult-idunn-runtime-activation-key:gamecult-runtime-presence-identity";
const realReadFileSync = fs.readFileSync.bind(fs);
mock.method(fs, "readFileSync", (filePath: fs.PathOrFileDescriptor, ...args: unknown[]) => {
if (filePath === "/proc/self/status") return `Name:\tnode\nNSpid:\t${listenPid}\t1\n`;
if (filePath === "/proc/self/fd/3") return activationSeed;
if (filePath === "/proc/self/fd/4") return providerCredential;
if (filePath === "/etc/machine-id") return machineId;