Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

MacProcessAuditToken.ts

121 lines4.6 KB

1import * as NodeOS from "node:os";
2​
3// mach/task_info.h: TASK_AUDIT_TOKEN returns eight native uint32_t values.
4const TASK_AUDIT_TOKEN = 15;
5const AUDIT_TOKEN_WORDS = 8;
6const AUDIT_TOKEN_BYTES = AUDIT_TOKEN_WORDS * Uint32Array.BYTES_PER_ELEMENT;
7const AUDIT_TOKEN_PID_OFFSET = 5 * Uint32Array.BYTES_PER_ELEMENT;
8const PROC_PIDPATH_MAXSIZE = 4096;
9// sys/proc_info.h: proc_bsdshortinfo begins with the PID and parent PID.
10const PROC_PIDT_SHORTBSDINFO = 13;
11const PROC_PIDT_SHORTBSDINFO_SIZE = 64;
12​
13export const isProcessAuditToken = (value: unknown): value is string =>
14 typeof value === "string" && /^[0-9a-f]{64}$/.test(value);
15​
16export const auditTokenPid = (token: string): number =>
17 Buffer.from(token, "hex").readUInt32LE(AUDIT_TOKEN_PID_OFFSET);
18​
19export interface MacProcessAuditToken {
20 readonly read: (pid: number) => string | undefined;
21 readonly executable: (pid: number) => string | undefined;
22 readonly parent: (pid: number) => number | undefined;
23 readonly signal: (token: string, signal: "SIGTERM" | "SIGKILL") => void;
25​
26let macProcessAuditToken: Promise<MacProcessAuditToken> | undefined;
27​
28/** Signals a kernel process identity so a PID reused after verification cannot receive the signal. */
29export const loadMacProcessAuditToken = (): Promise<MacProcessAuditToken> =>
30 (macProcessAuditToken ??= import("ffi-rs").then(({ DataType, load, open }) => {
31 const library = "t3-macos-process-identity";
32 open({ library, path: "/usr/lib/libSystem.B.dylib" });
33 const self = load({
34 library,
35 funcName: "mach_task_self",
36 retType: DataType.U32,
37 paramsType: [],
38 paramsValue: [],
39 });
40​
41 return {
42 read(pid) {
43 const task = Buffer.alloc(4);
44 const nameResult = load({
45 library,
46 funcName: "task_name_for_pid",
47 retType: DataType.I32,
48 paramsType: [DataType.U32, DataType.I32, DataType.U8Array],
49 paramsValue: [self, pid, task],
50 });
51 if (nameResult !== 0) return undefined;
52​
53 const port = task.readUInt32LE();
54 try {
55 const token = Buffer.alloc(AUDIT_TOKEN_BYTES);
56 const count = Buffer.alloc(4);
57 count.writeUInt32LE(AUDIT_TOKEN_WORDS);
58 const infoResult = load({
59 library,
60 funcName: "task_info",
61 retType: DataType.I32,
62 paramsType: [DataType.U32, DataType.I32, DataType.U8Array, DataType.U8Array],
63 paramsValue: [port, TASK_AUDIT_TOKEN, token, count],
64 });
65 return infoResult === 0 &&
66 count.readUInt32LE() === AUDIT_TOKEN_WORDS &&
67 token.readUInt32LE(AUDIT_TOKEN_PID_OFFSET) === pid
68 ? token.toString("hex")
69 : undefined;
70 } finally {
71 load({
72 library,
73 funcName: "mach_port_deallocate",
74 retType: DataType.I32,
75 paramsType: [DataType.U32, DataType.U32],
76 paramsValue: [self, port],
77 });
78 }
79 },
80 executable(pid) {
81 const path = Buffer.alloc(PROC_PIDPATH_MAXSIZE);
82 const length = load({
83 library,
84 funcName: "proc_pidpath",
85 retType: DataType.I32,
86 paramsType: [DataType.I32, DataType.U8Array, DataType.U32],
87 paramsValue: [pid, path, path.byteLength],
88 });
89 return length > 0 && length <= path.byteLength
90 ? path.toString("utf8", 0, length).replace(/\0$/, "")
91 : undefined;
92 },
93 parent(pid) {
94 const info = Buffer.alloc(PROC_PIDT_SHORTBSDINFO_SIZE);
95 const length = load({
96 library,
97 funcName: "proc_pidinfo",
98 retType: DataType.I32,
99 paramsType: [DataType.I32, DataType.I32, DataType.BigInt, DataType.U8Array, DataType.I32],
100 paramsValue: [pid, PROC_PIDT_SHORTBSDINFO, 0n, info, info.byteLength],
101 });
102 return length === info.byteLength && info.readUInt32LE() === pid
103 ? info.readUInt32LE(4)
104 : undefined;
105 },
106 signal(token, name) {
107 if (!isProcessAuditToken(token)) throw new Error("Invalid macOS audit token");
108 const result = load({
109 library,
110 funcName: "proc_signal_with_audittoken",
111 retType: DataType.I32,
112 paramsType: [DataType.U8Array, DataType.I32],
113 paramsValue: [Buffer.from(token, "hex"), NodeOS.constants.signals[name]],
114 });
115 // ESRCH includes a process that exited or was replaced after the last observation.
116 if (result !== 0 && result !== NodeOS.constants.errno.ESRCH) {
117 throw new Error(`macOS could not deliver ${name} (errno ${result})`);
118 }
119 },
120 };
121 }));