@indigoai-us/hq-cli 5.345.214 → 5.345.215-beta.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +29 -0
- package/dist/command-catalog.generated.d.ts +134 -7
- package/dist/command-catalog.generated.js +168 -7
- package/dist/commands/agents.d.ts +51 -1
- package/dist/commands/agents.js +154 -2
- package/dist/commands/bot-jobs.d.ts +2 -15
- package/dist/commands/bot-jobs.js +2 -94
- package/dist/commands/bot-runtime.d.ts +46 -5
- package/dist/commands/bot-runtime.js +228 -18
- package/dist/commands/bot.d.ts +16 -9
- package/dist/commands/bot.js +169 -51
- package/dist/commands/cloud.d.ts +66 -13
- package/dist/commands/cloud.js +120 -39
- package/dist/commands/company-restore.d.ts +23 -0
- package/dist/commands/company-restore.js +128 -0
- package/dist/commands/company.js +2 -0
- package/dist/commands/integrations-connect.d.ts +10 -3
- package/dist/commands/integrations-connect.js +87 -13
- package/dist/commands/lanes.js +26 -9
- package/dist/commands/sources.js +6 -0
- package/dist/commands/storage-offload.d.ts +12 -0
- package/dist/commands/storage-offload.js +41 -3
- package/dist/commands/sync-mode.js +5 -0
- package/dist/commands/whoami.js +4 -0
- package/dist/lib/bot/config.d.ts +1 -0
- package/dist/lib/bot/config.js +10 -0
- package/dist/lib/bot/eventbridge-cron.d.ts +22 -0
- package/dist/lib/bot/eventbridge-cron.js +104 -0
- package/dist/lib/bot/fleet-local.d.ts +33 -1
- package/dist/lib/bot/fleet-local.js +215 -31
- package/dist/lib/bot/fleet-python.d.ts +3 -0
- package/dist/lib/bot/fleet-python.js +34 -4
- package/dist/lib/bot/fleet-runtime-sources.d.ts +15 -0
- package/dist/lib/bot/fleet-runtime-sources.js +30 -0
- package/dist/lib/bot/legacy-provider-login.d.ts +8 -0
- package/dist/lib/bot/legacy-provider-login.js +19 -2
- package/dist/lib/bot/prompt.js +1 -1
- package/dist/lib/bot/readiness.d.ts +8 -0
- package/dist/lib/bot/readiness.js +17 -3
- package/dist/lib/bot/runtime-auth.d.ts +30 -0
- package/dist/lib/bot/runtime-auth.js +94 -3
- package/dist/lib/bot/status.d.ts +2 -0
- package/dist/lib/capacity/collectors/grok-refresh.d.ts +80 -0
- package/dist/lib/capacity/collectors/grok-refresh.js +189 -0
- package/dist/lib/capacity/collectors/grok.d.ts +5 -0
- package/dist/lib/capacity/collectors/grok.js +56 -5
- package/dist/lib/daemon/catalog.js +11 -1
- package/dist/lib/db/sqlite-binding.d.ts +1 -0
- package/dist/lib/db/sqlite-binding.js +1 -1
- package/dist/lib/delivery/artifacts.d.ts +99 -0
- package/dist/lib/delivery/artifacts.js +548 -0
- package/dist/lib/delivery/backup.d.ts +78 -0
- package/dist/lib/delivery/backup.js +343 -0
- package/dist/lib/delivery/cache.d.ts +29 -0
- package/dist/lib/delivery/cache.js +33 -0
- package/dist/lib/delivery/contracts.d.ts +142 -0
- package/dist/lib/delivery/contracts.js +289 -0
- package/dist/lib/delivery/credentials.d.ts +54 -0
- package/dist/lib/delivery/credentials.js +119 -0
- package/dist/lib/delivery/enrollment.d.ts +142 -0
- package/dist/lib/delivery/enrollment.js +382 -0
- package/dist/lib/delivery/identity.d.ts +25 -0
- package/dist/lib/delivery/identity.js +82 -0
- package/dist/lib/delivery/inbox.d.ts +116 -0
- package/dist/lib/delivery/inbox.js +260 -0
- package/dist/lib/delivery/migrations.d.ts +139 -0
- package/dist/lib/delivery/migrations.js +623 -0
- package/dist/lib/delivery/mqtt-adapter.d.ts +50 -0
- package/dist/lib/delivery/mqtt-adapter.js +242 -0
- package/dist/lib/delivery/mqtt.d.ts +61 -0
- package/dist/lib/delivery/mqtt.js +112 -0
- package/dist/lib/delivery/outbox.d.ts +95 -0
- package/dist/lib/delivery/outbox.js +187 -0
- package/dist/lib/delivery/provenance.d.ts +129 -0
- package/dist/lib/delivery/provenance.js +93 -0
- package/dist/lib/delivery/reconciler.d.ts +151 -0
- package/dist/lib/delivery/reconciler.js +492 -0
- package/dist/lib/delivery/restore.d.ts +35 -0
- package/dist/lib/delivery/restore.js +134 -0
- package/dist/lib/delivery/store.d.ts +161 -0
- package/dist/lib/delivery/store.js +940 -0
- package/dist/lib/delivery/workers/macos-job-helper.d.ts +103 -0
- package/dist/lib/delivery/workers/macos-job-helper.js +927 -0
- package/dist/lib/delivery/workers/macos.d.ts +176 -0
- package/dist/lib/delivery/workers/macos.js +901 -0
- package/dist/lib/delivery/workers/windows-boundary.d.ts +453 -0
- package/dist/lib/delivery/workers/windows-boundary.js +693 -0
- package/dist/lib/delivery/workers/windows-job-helper.d.ts +35 -0
- package/dist/lib/delivery/workers/windows-job-helper.js +264 -0
- package/dist/lib/delivery/workers/windows.d.ts +241 -0
- package/dist/lib/delivery/workers/windows.js +608 -0
- package/dist/lib/install/provider-wrappers.d.ts +2 -0
- package/dist/lib/install/provider-wrappers.js +68 -3
- package/dist/utils/cognito-callback-port.d.ts +14 -0
- package/dist/utils/cognito-callback-port.js +24 -0
- package/dist/utils/cognito-session.js +3 -3
- package/dist/utils/deleted-company-notice.d.ts +59 -0
- package/dist/utils/deleted-company-notice.js +156 -0
- package/dist/utils/login-browser.d.ts +1 -1
- package/dist/utils/login-browser.js +7 -5
- package/dist/utils/resident-package-preload.js +30 -15
- package/dist/utils/vault-api.js +7 -0
- package/package.json +7 -7
|
@@ -0,0 +1,927 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The native macOS containment supervisor (US-007), as C source.
|
|
3
|
+
*
|
|
4
|
+
* Node cannot put a process tree in a sandbox or tell which processes belong
|
|
5
|
+
* to one job once they leave the process group, so the worker compiles this
|
|
6
|
+
* source once with the system C compiler (/usr/bin/cc, from the Command Line
|
|
7
|
+
* Tools) and runs the result as a small supervisor per job. No package or
|
|
8
|
+
* system software is installed and nothing is registered with launchd.
|
|
9
|
+
*
|
|
10
|
+
* Protocol. The worker writes the spec to the helper's stdin as
|
|
11
|
+
* "key<TAB>base64(utf8 value)" lines ended by "end". Keys: app, arg (one line
|
|
12
|
+
* per argument, argv[0] first), cwd, log, env (one line per KEY=VALUE), mem
|
|
13
|
+
* (bytes), procs, cpu (ms), profile (the Seatbelt profile), mark and ctrl
|
|
14
|
+
* (the job's marker and control files), win (the cleanup window in ms,
|
|
15
|
+
* default 2000), seen (how many distinct processes the helper tracks,
|
|
16
|
+
* default 8192), fsize and nofile (RLIMIT_FSIZE and RLIMIT_NOFILE, unset when
|
|
17
|
+
* absent), room (the RLIMIT_NPROC room above procs) and sample ("0" turns the
|
|
18
|
+
* sampled limits off; tests use it to prove the kernel limits alone). The
|
|
19
|
+
* helper writes one event per stdout line:
|
|
20
|
+
* reaper <pid> the reaper (see below) is running
|
|
21
|
+
* watchdog <pid> the watchdog (see below) is running in the sandbox
|
|
22
|
+
* started <pid> the root process runs inside the job's sandbox
|
|
23
|
+
* process <pid> a process was seen in the job's sandbox
|
|
24
|
+
* exit <reason> <code> the tree is gone; reason is exited, cancelled,
|
|
25
|
+
* memory_limit, process_limit or cpu_limit (also
|
|
26
|
+
* when the kernel stopped the root with SIGXCPU)
|
|
27
|
+
* exit kill_incomplete -1
|
|
28
|
+
* members were still alive when the cleanup window
|
|
29
|
+
* ran out; the worker leaves the job directory for
|
|
30
|
+
* the next sweep
|
|
31
|
+
* exit protocol -1 the worker sent a stdin line longer than the
|
|
32
|
+
* helper's buffer; the tree was killed
|
|
33
|
+
* error <stage> <errno> the job could not be set up; nothing ran
|
|
34
|
+
* After "end", a stdin line "cancel", or stdin closing, kills the tree.
|
|
35
|
+
*
|
|
36
|
+
* Containment. The root process enters a per-job Seatbelt profile
|
|
37
|
+
* (sandbox_init) before exec. The profile denies reading the job's marker
|
|
38
|
+
* file, so every descendant, including ones that call setsid, are started
|
|
39
|
+
* detached, or are reparented to launchd, inherits it. The helper finds the
|
|
40
|
+
* tree by asking the kernel, for every process of the same user, whether that
|
|
41
|
+
* process may read the marker (it may not) and the control file next to it
|
|
42
|
+
* (it may): sandbox_check answers for a process that is not the caller. A
|
|
43
|
+
* process that passes both checks is in this job. The rest of the profile
|
|
44
|
+
* (reads, network, mach services, process inspection, signals, writes and
|
|
45
|
+
* exec) is built by the worker; see jobProfile in macos.ts.
|
|
46
|
+
*
|
|
47
|
+
* Kills. Each member is signalled only after its start time is read again and
|
|
48
|
+
* matches the scan that found it, and it still passes the membership checks,
|
|
49
|
+
* so a pid reused by an unrelated process is never signalled. Kills repeat
|
|
50
|
+
* until two scans in a row find nobody or the cleanup window ends; a window
|
|
51
|
+
* that ends with members alive is reported as kill_incomplete.
|
|
52
|
+
*
|
|
53
|
+
* Reaper. Before the job starts the helper forks a reaper outside the
|
|
54
|
+
* sandbox in its own session. The reaper waits on the helper's exit (kqueue
|
|
55
|
+
* NOTE_EXIT) and, if the helper dies without stopping it first, kills every
|
|
56
|
+
* member within the cleanup window and exits. So a tree whose helper and
|
|
57
|
+
* worker host both die is gone within the window, without a later prepare.
|
|
58
|
+
* The helper kills and waits for the reaper before every exit line.
|
|
59
|
+
*
|
|
60
|
+
* Watchdog. The reaper and the helper can both be killed at once. So the
|
|
61
|
+
* pre-exec child forks a watchdog after it enters the sandbox: it is in the
|
|
62
|
+
* job's sandbox but is not counted toward the job's limits. It holds the read
|
|
63
|
+
* end of a pipe whose only write ends the helper and the reaper hold, and
|
|
64
|
+
* blocks on it. When both are gone, however they died, the read returns EOF
|
|
65
|
+
* and the watchdog kills every process it can see and exits. The sandbox lets
|
|
66
|
+
* it see and signal only processes of its own sandbox, which is this job's
|
|
67
|
+
* tree. A normal exit kills the watchdog with the rest of the tree. Only the
|
|
68
|
+
* job itself can kill its watchdog early; then the prepare sweep is the
|
|
69
|
+
* fallback.
|
|
70
|
+
*
|
|
71
|
+
* Limits. The helper waits on kqueue for stdin, and for NOTE_FORK and
|
|
72
|
+
* NOTE_EXIT on every member it has seen, so a fork wakes it to scan at once.
|
|
73
|
+
* Otherwise it samples every 10 ms when a memory limit is set and every 50 ms
|
|
74
|
+
* when not. Each 10 ms memory sample sums resident memory over the members the
|
|
75
|
+
* helper already knows: the root, children reported by fork notes, and members
|
|
76
|
+
* found by earlier scans. A member the fork notes missed is counted only after
|
|
77
|
+
* the next 50 ms discovery scan finds it. Each sample reads the kernel's task
|
|
78
|
+
* info: resident memory summed over live members, user plus system CPU time
|
|
79
|
+
* summed over every member seen (exited ones keep their last reading), and the
|
|
80
|
+
* count of live members. A full seen table is a process-limit breach.
|
|
81
|
+
*
|
|
82
|
+
* Kernel limits, set in the pre-exec child and inherited by every
|
|
83
|
+
* descendant: RLIMIT_CPU per process at ceil(cpu / 1000) s (SIGXCPU, then
|
|
84
|
+
* SIGKILL a second later); RLIMIT_NPROC at the user's process count now plus
|
|
85
|
+
* procs, the watchdog and room, a ceiling on the user's processes that a fork
|
|
86
|
+
* bomb cannot pass; RLIMIT_FSIZE and RLIMIT_NOFILE when declared; RLIMIT_CORE
|
|
87
|
+
* 0. RLIMIT_AS and RLIMIT_DATA return EINVAL on macOS and RLIMIT_RSS is not
|
|
88
|
+
* enforced, so memory has the sampled limit only, and the per-job process
|
|
89
|
+
* count is sampled because RLIMIT_NPROC counts every process of the user.
|
|
90
|
+
*
|
|
91
|
+
* Modes.
|
|
92
|
+
* --selftest <dir> checks membership, non-membership, the identity
|
|
93
|
+
* check before a signal and fork notes; prints one
|
|
94
|
+
* "check <name> <0|1>" line each, then "selftest ok",
|
|
95
|
+
* or "selftest failed" with exit 1
|
|
96
|
+
* --reap <marker> <control>
|
|
97
|
+
* kills every member of a job whose helper died
|
|
98
|
+
* --start <pid> prints "<sec> <usec>", the process start time
|
|
99
|
+
* --alive <pid> <sec> <usec>
|
|
100
|
+
* exits 0 only when that pid is alive with that start time
|
|
101
|
+
*/
|
|
102
|
+
export const HELPER_SOURCE = String.raw `
|
|
103
|
+
#include <errno.h>
|
|
104
|
+
#include <fcntl.h>
|
|
105
|
+
#include <libproc.h>
|
|
106
|
+
#include <mach/mach_time.h>
|
|
107
|
+
#include <sandbox.h>
|
|
108
|
+
#include <signal.h>
|
|
109
|
+
#include <stdarg.h>
|
|
110
|
+
#include <stdio.h>
|
|
111
|
+
#include <stdlib.h>
|
|
112
|
+
#include <string.h>
|
|
113
|
+
#include <sys/event.h>
|
|
114
|
+
#include <sys/proc.h>
|
|
115
|
+
#include <sys/proc_info.h>
|
|
116
|
+
#include <sys/resource.h>
|
|
117
|
+
#include <sys/sysctl.h>
|
|
118
|
+
#include <sys/wait.h>
|
|
119
|
+
#include <unistd.h>
|
|
120
|
+
|
|
121
|
+
extern int sandbox_check(pid_t pid, const char *operation, int type, ...);
|
|
122
|
+
#define SANDBOX_FILTER_PATH 1
|
|
123
|
+
#define SANDBOX_CHECK_NO_REPORT 0x40000000
|
|
124
|
+
#define DEFAULT_SEEN 8192
|
|
125
|
+
/* The backstop interval of the full process scan, in ms. */
|
|
126
|
+
#define SCAN_MS 50
|
|
127
|
+
#define DEFAULT_WINDOW_MS 2000
|
|
128
|
+
|
|
129
|
+
typedef struct { pid_t pid; unsigned long long sec, usec; } ident_t;
|
|
130
|
+
typedef struct { pid_t pid; unsigned long long cpu_ns; int alive; } seen_t;
|
|
131
|
+
|
|
132
|
+
static const char *marker, *control;
|
|
133
|
+
static pid_t root = -1, reaper = -1, wdpid = -1;
|
|
134
|
+
static int root_done = 0, root_status = 0, root_sig = 0;
|
|
135
|
+
/* 0 turns the sampled limits off, so only the kernel limits act; tests use it to prove that layer alone. */
|
|
136
|
+
static int sampling = 1;
|
|
137
|
+
static seen_t *seen = NULL;
|
|
138
|
+
static int nseen = 0, seen_cap = DEFAULT_SEEN;
|
|
139
|
+
static int kq = -1;
|
|
140
|
+
static mach_timebase_info_data_t tb;
|
|
141
|
+
|
|
142
|
+
static void out(const char *fmt, ...) {
|
|
143
|
+
va_list ap;
|
|
144
|
+
va_start(ap, fmt);
|
|
145
|
+
vfprintf(stdout, fmt, ap);
|
|
146
|
+
va_end(ap);
|
|
147
|
+
fputc('\n', stdout);
|
|
148
|
+
fflush(stdout);
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
static unsigned long long now_ms(void) {
|
|
152
|
+
return mach_absolute_time() * tb.numer / tb.denom / 1000000ULL;
|
|
153
|
+
}
|
|
154
|
+
|
|
155
|
+
static int bsdinfo(pid_t pid, struct proc_bsdinfo *bi) {
|
|
156
|
+
return proc_pidinfo(pid, PROC_PIDTBSDINFO, 0, bi, sizeof *bi) == (int)sizeof *bi;
|
|
157
|
+
}
|
|
158
|
+
|
|
159
|
+
static int member(pid_t pid) {
|
|
160
|
+
struct proc_bsdinfo bi;
|
|
161
|
+
if (pid <= 1 || pid == getpid()) return 0;
|
|
162
|
+
if (!bsdinfo(pid, &bi)) return 0;
|
|
163
|
+
if (bi.pbi_uid != getuid() || bi.pbi_status == SZOMB) return 0;
|
|
164
|
+
if (sandbox_check(pid, "file-read-data", SANDBOX_FILTER_PATH | SANDBOX_CHECK_NO_REPORT, marker) != 1) return 0;
|
|
165
|
+
if (sandbox_check(pid, "file-read-data", SANDBOX_FILTER_PATH | SANDBOX_CHECK_NO_REPORT, control) != 0) return 0;
|
|
166
|
+
return 1;
|
|
167
|
+
}
|
|
168
|
+
|
|
169
|
+
static void poll_forks(void);
|
|
170
|
+
static int scanning = 0;
|
|
171
|
+
|
|
172
|
+
/* Every live member with its start time; returns the count, or -1 when memory runs out. The caller frees *list. */
|
|
173
|
+
static int members(ident_t **list) {
|
|
174
|
+
*list = NULL;
|
|
175
|
+
int n = proc_listallpids(NULL, 0);
|
|
176
|
+
if (n < 0) n = 0;
|
|
177
|
+
int cap = n + 256;
|
|
178
|
+
pid_t *all = calloc((size_t)cap, sizeof(pid_t));
|
|
179
|
+
ident_t *mine = calloc((size_t)cap, sizeof(ident_t));
|
|
180
|
+
if (!all || !mine) {
|
|
181
|
+
free(all);
|
|
182
|
+
free(mine);
|
|
183
|
+
return -1;
|
|
184
|
+
}
|
|
185
|
+
int got = proc_listallpids(all, cap * (int)sizeof(pid_t));
|
|
186
|
+
int m = 0;
|
|
187
|
+
for (int i = 0; i < got && m < cap; i++) {
|
|
188
|
+
struct proc_bsdinfo bi;
|
|
189
|
+
/* A full scan is slow; fork notes that arrive during it are handled as it goes. */
|
|
190
|
+
if (scanning && (i & 31) == 0) poll_forks();
|
|
191
|
+
if (!member(all[i]) || !bsdinfo(all[i], &bi)) continue;
|
|
192
|
+
mine[m].pid = all[i];
|
|
193
|
+
mine[m].sec = bi.pbi_start_tvsec;
|
|
194
|
+
mine[m].usec = bi.pbi_start_tvusec;
|
|
195
|
+
m++;
|
|
196
|
+
}
|
|
197
|
+
free(all);
|
|
198
|
+
*list = mine;
|
|
199
|
+
return m;
|
|
200
|
+
}
|
|
201
|
+
|
|
202
|
+
/* Signal pid only if it still has the start time it was found with and is still a member. 0 when signalled. */
|
|
203
|
+
static int signal_if_same(pid_t pid, unsigned long long sec, unsigned long long usec, int sig) {
|
|
204
|
+
struct proc_bsdinfo bi;
|
|
205
|
+
if (!bsdinfo(pid, &bi)) return -1;
|
|
206
|
+
if (bi.pbi_start_tvsec != sec || bi.pbi_start_tvusec != usec) return -1;
|
|
207
|
+
if (!member(pid)) return -1;
|
|
208
|
+
return kill(pid, sig) == 0 ? 0 : -1;
|
|
209
|
+
}
|
|
210
|
+
|
|
211
|
+
static void reap_root(int block) {
|
|
212
|
+
if (root <= 0 || root_done) return;
|
|
213
|
+
int st;
|
|
214
|
+
pid_t r;
|
|
215
|
+
do r = waitpid(root, &st, block ? 0 : WNOHANG);
|
|
216
|
+
while (r < 0 && errno == EINTR);
|
|
217
|
+
if (r == root) {
|
|
218
|
+
root_done = 1;
|
|
219
|
+
root_status = WIFEXITED(st) ? WEXITSTATUS(st) : WIFSIGNALED(st) ? 128 + WTERMSIG(st) : 1;
|
|
220
|
+
root_sig = WIFSIGNALED(st) ? WTERMSIG(st) : 0;
|
|
221
|
+
} else if (r < 0) {
|
|
222
|
+
root_done = 1;
|
|
223
|
+
root_status = 1;
|
|
224
|
+
}
|
|
225
|
+
}
|
|
226
|
+
|
|
227
|
+
/*
|
|
228
|
+
* Kill every member until two scans in a row find nobody or window_ms runs
|
|
229
|
+
* out. A window of 0 makes no kill pass. Returns 1 only when no member is
|
|
230
|
+
* left.
|
|
231
|
+
*/
|
|
232
|
+
static int kill_tree(long long window_ms) {
|
|
233
|
+
unsigned long long deadline = now_ms() + (unsigned long long)(window_ms > 0 ? window_ms : 0);
|
|
234
|
+
int quiet = 0, killed_root = 0;
|
|
235
|
+
while (window_ms > 0 && quiet < 2 && now_ms() < deadline) {
|
|
236
|
+
ident_t *list;
|
|
237
|
+
int n = members(&list);
|
|
238
|
+
for (int j = 0; j < n; j++) signal_if_same(list[j].pid, list[j].sec, list[j].usec, SIGKILL);
|
|
239
|
+
free(list);
|
|
240
|
+
/* The root is this helper's unreaped child, so its pid cannot be reused yet. */
|
|
241
|
+
if (root > 0 && !root_done) {
|
|
242
|
+
kill(root, SIGKILL);
|
|
243
|
+
killed_root = 1;
|
|
244
|
+
reap_root(0);
|
|
245
|
+
}
|
|
246
|
+
quiet = n == 0 ? quiet + 1 : 0;
|
|
247
|
+
usleep(n == 0 ? 2000 : 5000);
|
|
248
|
+
}
|
|
249
|
+
reap_root(killed_root);
|
|
250
|
+
ident_t *left;
|
|
251
|
+
int n = members(&left);
|
|
252
|
+
free(left);
|
|
253
|
+
return n == 0;
|
|
254
|
+
}
|
|
255
|
+
|
|
256
|
+
static void stop_reaper(void) {
|
|
257
|
+
if (reaper <= 0) return;
|
|
258
|
+
kill(reaper, SIGKILL);
|
|
259
|
+
while (waitpid(reaper, NULL, 0) < 0 && errno == EINTR) {}
|
|
260
|
+
reaper = -1;
|
|
261
|
+
}
|
|
262
|
+
|
|
263
|
+
/* Watch pid for forks and exit. Errors are ignored: the timed scans still find the process. */
|
|
264
|
+
static void watch(pid_t pid) {
|
|
265
|
+
if (kq < 0) return;
|
|
266
|
+
struct kevent ev;
|
|
267
|
+
EV_SET(&ev, pid, EVFILT_PROC, EV_ADD, NOTE_FORK | NOTE_EXIT, 0, NULL);
|
|
268
|
+
kevent(kq, &ev, 1, NULL, 0, NULL);
|
|
269
|
+
}
|
|
270
|
+
|
|
271
|
+
/* The seen-table slot for pid, adding it when new; -1 when the table is full. */
|
|
272
|
+
static int seen_index(pid_t pid) {
|
|
273
|
+
/* The watchdog is part of the sandbox but not of the job's work: it is never counted. */
|
|
274
|
+
if (pid == wdpid) return -2;
|
|
275
|
+
for (int i = 0; i < nseen; i++)
|
|
276
|
+
if (seen[i].pid == pid) return i;
|
|
277
|
+
if (nseen >= seen_cap) return -1;
|
|
278
|
+
seen[nseen].pid = pid;
|
|
279
|
+
seen[nseen].cpu_ns = 0;
|
|
280
|
+
seen[nseen].alive = 1;
|
|
281
|
+
out("process %d", pid);
|
|
282
|
+
watch(pid);
|
|
283
|
+
return nseen++;
|
|
284
|
+
}
|
|
285
|
+
|
|
286
|
+
/* A NOTE_EXIT for pid: it no longer counts toward the process limit. */
|
|
287
|
+
static void mark_dead(pid_t pid) {
|
|
288
|
+
for (int i = 0; i < nseen; i++)
|
|
289
|
+
if (seen[i].pid == pid) seen[i].alive = 0;
|
|
290
|
+
}
|
|
291
|
+
|
|
292
|
+
/*
|
|
293
|
+
* A NOTE_FORK for parent. The note carries no child pid, so the parent's
|
|
294
|
+
* children are listed and every new member is recorded and watched. This is
|
|
295
|
+
* much cheaper than a full scan, so the process limit holds at the fork
|
|
296
|
+
* itself. Returns a breach reason or NULL.
|
|
297
|
+
*/
|
|
298
|
+
static const char *on_fork(pid_t parent, long long procs_max) {
|
|
299
|
+
pid_t kids[1024];
|
|
300
|
+
int n = proc_listchildpids(parent, kids, (int)sizeof kids);
|
|
301
|
+
if (n < 0) n = 0;
|
|
302
|
+
if (n > 1024) n = 1024;
|
|
303
|
+
int full = 0;
|
|
304
|
+
for (int i = 0; i < n; i++) {
|
|
305
|
+
int known = 0;
|
|
306
|
+
for (int j = 0; j < nseen && !known; j++) known = seen[j].pid == kids[i];
|
|
307
|
+
if (!known && member(kids[i]) && seen_index(kids[i]) == -1) full = 1;
|
|
308
|
+
}
|
|
309
|
+
if (full) return "process_limit";
|
|
310
|
+
if (procs_max <= 0) return NULL;
|
|
311
|
+
int live = 0;
|
|
312
|
+
for (int i = 0; i < nseen; i++) live += seen[i].alive;
|
|
313
|
+
return live > procs_max ? "process_limit" : NULL;
|
|
314
|
+
}
|
|
315
|
+
|
|
316
|
+
static long long procs_limit = 0;
|
|
317
|
+
static const char *fork_breach = NULL;
|
|
318
|
+
|
|
319
|
+
/* Handle the fork and exit notes that are queued now, without waiting. A breach is kept in fork_breach. */
|
|
320
|
+
static void poll_forks(void) {
|
|
321
|
+
struct kevent evs[64];
|
|
322
|
+
struct timespec zero = {0, 0};
|
|
323
|
+
int ne = kevent(kq, NULL, 0, evs, 64, &zero);
|
|
324
|
+
for (int i = 0; i < ne; i++) {
|
|
325
|
+
if (evs[i].filter != EVFILT_PROC) continue;
|
|
326
|
+
if (evs[i].fflags & NOTE_EXIT) mark_dead((pid_t)evs[i].ident);
|
|
327
|
+
if ((evs[i].fflags & NOTE_FORK) && !fork_breach) fork_breach = on_fork((pid_t)evs[i].ident, procs_limit);
|
|
328
|
+
}
|
|
329
|
+
if (fork_breach && root > 0 && !root_done) kill(root, SIGKILL);
|
|
330
|
+
}
|
|
331
|
+
|
|
332
|
+
/* The tracked members that are still alive, without a scan of every process. */
|
|
333
|
+
static int tracked(ident_t **list) {
|
|
334
|
+
*list = calloc((size_t)nseen + 1, sizeof(ident_t));
|
|
335
|
+
if (!*list) return -1;
|
|
336
|
+
int m = 0;
|
|
337
|
+
for (int i = 0; i < nseen; i++)
|
|
338
|
+
if (seen[i].alive && member(seen[i].pid)) (*list)[m++].pid = seen[i].pid;
|
|
339
|
+
return m;
|
|
340
|
+
}
|
|
341
|
+
|
|
342
|
+
/*
|
|
343
|
+
* One sample: record new members and read the limits. A full scan of every
|
|
344
|
+
* process (discover) finds members the fork notes missed; it is slow, so it
|
|
345
|
+
* runs on the backstop tick only, and the faster ticks read the tracked
|
|
346
|
+
* members. Returns a breach reason or NULL.
|
|
347
|
+
*/
|
|
348
|
+
static const char *sample(long long mem_max, long long procs_max, long long cpu_max_ms, int discover) {
|
|
349
|
+
ident_t *list;
|
|
350
|
+
int n = discover ? members(&list) : tracked(&list);
|
|
351
|
+
if (n < 0) return NULL;
|
|
352
|
+
unsigned long long resident = 0;
|
|
353
|
+
int full = 0, live = 0;
|
|
354
|
+
for (int j = 0; j < n; j++) {
|
|
355
|
+
int k = seen_index(list[j].pid);
|
|
356
|
+
if (k == -2) continue;
|
|
357
|
+
live++;
|
|
358
|
+
if (k < 0) full = 1;
|
|
359
|
+
else seen[k].alive = 1;
|
|
360
|
+
struct proc_taskinfo ti;
|
|
361
|
+
if (proc_pidinfo(list[j].pid, PROC_PIDTASKINFO, 0, &ti, sizeof ti) == (int)sizeof ti) {
|
|
362
|
+
resident += ti.pti_resident_size;
|
|
363
|
+
unsigned long long ns = (ti.pti_total_user + ti.pti_total_system) * tb.numer / tb.denom;
|
|
364
|
+
if (k >= 0 && ns > seen[k].cpu_ns) seen[k].cpu_ns = ns;
|
|
365
|
+
}
|
|
366
|
+
}
|
|
367
|
+
free(list);
|
|
368
|
+
unsigned long long cpu = 0;
|
|
369
|
+
for (int i = 0; i < nseen; i++) cpu += seen[i].cpu_ns;
|
|
370
|
+
if (mem_max > 0 && resident > (unsigned long long)mem_max) return "memory_limit";
|
|
371
|
+
if (full || (procs_max > 0 && live > procs_max)) return "process_limit";
|
|
372
|
+
if (cpu_max_ms > 0 && cpu / 1000000ULL > (unsigned long long)cpu_max_ms) return "cpu_limit";
|
|
373
|
+
return NULL;
|
|
374
|
+
}
|
|
375
|
+
|
|
376
|
+
static int b64val(int c) {
|
|
377
|
+
if (c >= 'A' && c <= 'Z') return c - 'A';
|
|
378
|
+
if (c >= 'a' && c <= 'z') return c - 'a' + 26;
|
|
379
|
+
if (c >= '0' && c <= '9') return c - '0' + 52;
|
|
380
|
+
if (c == '+') return 62;
|
|
381
|
+
if (c == '/') return 63;
|
|
382
|
+
return -1;
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
static char *b64dec(const char *s) {
|
|
386
|
+
size_t len = strlen(s);
|
|
387
|
+
char *o = malloc(len * 3 / 4 + 4);
|
|
388
|
+
if (!o) return NULL;
|
|
389
|
+
size_t k = 0;
|
|
390
|
+
int acc = 0, bits = 0;
|
|
391
|
+
for (size_t i = 0; i < len; i++) {
|
|
392
|
+
int v = b64val((unsigned char)s[i]);
|
|
393
|
+
if (v < 0) continue;
|
|
394
|
+
acc = (acc << 6) | v;
|
|
395
|
+
bits += 6;
|
|
396
|
+
if (bits >= 8) {
|
|
397
|
+
bits -= 8;
|
|
398
|
+
o[k++] = (char)((acc >> bits) & 0xff);
|
|
399
|
+
}
|
|
400
|
+
}
|
|
401
|
+
o[k] = 0;
|
|
402
|
+
return o;
|
|
403
|
+
}
|
|
404
|
+
|
|
405
|
+
static char inbuf[1 << 16];
|
|
406
|
+
static size_t inlen = 0;
|
|
407
|
+
|
|
408
|
+
/* The next complete line already in the buffer, without the newline; NULL when there is none or memory runs out. */
|
|
409
|
+
static char *take_line(void) {
|
|
410
|
+
char *nl = memchr(inbuf, '\n', inlen);
|
|
411
|
+
if (!nl) return NULL;
|
|
412
|
+
size_t n = (size_t)(nl - inbuf);
|
|
413
|
+
char *line = malloc(n + 1);
|
|
414
|
+
if (!line) return NULL;
|
|
415
|
+
memcpy(line, inbuf, n);
|
|
416
|
+
line[n] = 0;
|
|
417
|
+
memmove(inbuf, nl + 1, inlen - n - 1);
|
|
418
|
+
inlen -= n + 1;
|
|
419
|
+
return line;
|
|
420
|
+
}
|
|
421
|
+
|
|
422
|
+
/* Next stdin line without the newline, or NULL at EOF, on a line longer than the buffer, or when memory runs out. */
|
|
423
|
+
static char *next_line(void) {
|
|
424
|
+
for (;;) {
|
|
425
|
+
if (memchr(inbuf, '\n', inlen)) return take_line();
|
|
426
|
+
if (inlen == sizeof inbuf) return NULL;
|
|
427
|
+
ssize_t r = read(0, inbuf + inlen, sizeof inbuf - inlen);
|
|
428
|
+
if (r < 0 && errno == EINTR) continue;
|
|
429
|
+
if (r <= 0) return NULL;
|
|
430
|
+
inlen += (size_t)r;
|
|
431
|
+
}
|
|
432
|
+
}
|
|
433
|
+
|
|
434
|
+
static void report(int fd, int stage, int err) {
|
|
435
|
+
int v[2] = {stage, err};
|
|
436
|
+
ssize_t w = write(fd, v, sizeof v);
|
|
437
|
+
(void)w;
|
|
438
|
+
_exit(127);
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
static const char *STAGES[] = {"none", "log", "cwd", "stdin", "sandbox", "exec", "limits", "watchdog"};
|
|
442
|
+
#define NSTAGES 7
|
|
443
|
+
/* The pre-exec child sends {WD_MSG, watchdog pid} on the error pipe before exec. */
|
|
444
|
+
#define WD_MSG 100
|
|
445
|
+
#define PID_LIMIT 99999
|
|
446
|
+
|
|
447
|
+
/* Close every descriptor above 2 except keep (-1 for none). */
|
|
448
|
+
static void close_others(int keep) {
|
|
449
|
+
int nfd = proc_pidinfo(getpid(), PROC_PIDLISTFDS, 0, NULL, 0);
|
|
450
|
+
struct proc_fdinfo *fds = nfd > 0 ? malloc((size_t)nfd) : NULL;
|
|
451
|
+
if (fds) {
|
|
452
|
+
int got = proc_pidinfo(getpid(), PROC_PIDLISTFDS, 0, fds, nfd) / (int)sizeof(struct proc_fdinfo);
|
|
453
|
+
for (int i = 0; i < got; i++)
|
|
454
|
+
if (fds[i].proc_fd > 2 && fds[i].proc_fd != keep) close(fds[i].proc_fd);
|
|
455
|
+
free(fds);
|
|
456
|
+
} else {
|
|
457
|
+
for (int fd = 3; fd < 4096; fd++)
|
|
458
|
+
if (fd != keep) close(fd);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
|
|
462
|
+
/* How many processes the kernel counts against this real uid, from kern.proc; -1 when it cannot be read. */
|
|
463
|
+
static long long user_procs(void) {
|
|
464
|
+
int mib[4] = {CTL_KERN, KERN_PROC, KERN_PROC_RUID, (int)getuid()};
|
|
465
|
+
size_t len = 0;
|
|
466
|
+
/* The size query alone over-estimates, so the table is read and its real length used. */
|
|
467
|
+
if (sysctl(mib, 4, NULL, &len, NULL, 0) != 0) return -1;
|
|
468
|
+
len += 64 * sizeof(struct kinfo_proc);
|
|
469
|
+
void *buf = malloc(len);
|
|
470
|
+
if (!buf) return -1;
|
|
471
|
+
int rc = sysctl(mib, 4, buf, &len, NULL, 0);
|
|
472
|
+
free(buf);
|
|
473
|
+
if (rc != 0) return -1;
|
|
474
|
+
return (long long)(len / sizeof(struct kinfo_proc));
|
|
475
|
+
}
|
|
476
|
+
|
|
477
|
+
/*
|
|
478
|
+
* The watchdog. It runs inside the job's sandbox and holds only the read end
|
|
479
|
+
* of a pipe whose write ends the helper and the reaper hold. When both are
|
|
480
|
+
* gone, for any reason, the read returns EOF and the watchdog kills every
|
|
481
|
+
* process it can see. Inside the sandbox it can see and signal only
|
|
482
|
+
* processes of this same sandbox (process-info and signal are allowed for
|
|
483
|
+
* same-sandbox targets only; proc_listallpids is denied, so it probes each
|
|
484
|
+
* pid), which is exactly this job's tree.
|
|
485
|
+
*/
|
|
486
|
+
static void run_watchdog(int rd, long long window_ms) {
|
|
487
|
+
setsid();
|
|
488
|
+
int nul = open("/dev/null", O_RDWR);
|
|
489
|
+
if (nul >= 0) {
|
|
490
|
+
dup2(nul, 0);
|
|
491
|
+
dup2(nul, 1);
|
|
492
|
+
dup2(nul, 2);
|
|
493
|
+
if (nul > 2) close(nul);
|
|
494
|
+
}
|
|
495
|
+
close_others(rd);
|
|
496
|
+
signal(SIGPIPE, SIG_IGN);
|
|
497
|
+
char b[64];
|
|
498
|
+
for (;;) {
|
|
499
|
+
ssize_t r = read(rd, b, sizeof b);
|
|
500
|
+
if (r == 0 || (r < 0 && errno != EINTR)) break;
|
|
501
|
+
}
|
|
502
|
+
pid_t self = getpid();
|
|
503
|
+
uid_t uid = getuid();
|
|
504
|
+
unsigned long long deadline = now_ms() + (unsigned long long)(window_ms > 0 ? window_ms : DEFAULT_WINDOW_MS);
|
|
505
|
+
int quiet = 0;
|
|
506
|
+
while (quiet < 2 && now_ms() < deadline) {
|
|
507
|
+
int n = 0;
|
|
508
|
+
for (pid_t p = 2; p <= PID_LIMIT; p++) {
|
|
509
|
+
struct proc_bsdinfo bi;
|
|
510
|
+
if (p == self || !bsdinfo(p, &bi)) continue;
|
|
511
|
+
if (bi.pbi_uid != uid || bi.pbi_status == SZOMB) continue;
|
|
512
|
+
if (kill(p, SIGKILL) == 0) n++;
|
|
513
|
+
}
|
|
514
|
+
quiet = n == 0 ? quiet + 1 : 0;
|
|
515
|
+
usleep(n == 0 ? 2000 : 5000);
|
|
516
|
+
}
|
|
517
|
+
_exit(0);
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
/* The reaper: wait for the helper to exit, then kill whatever is left of the job within the window. */
|
|
521
|
+
static void run_reaper(pid_t parent, long long window_ms, int keep) {
|
|
522
|
+
setsid();
|
|
523
|
+
int nul = open("/dev/null", O_RDWR);
|
|
524
|
+
if (nul >= 0) {
|
|
525
|
+
dup2(nul, 0);
|
|
526
|
+
dup2(nul, 1);
|
|
527
|
+
dup2(nul, 2);
|
|
528
|
+
if (nul > 2) close(nul);
|
|
529
|
+
}
|
|
530
|
+
/* keep is the reaper's write end of the watchdog pipe: while it is open the watchdog waits. */
|
|
531
|
+
close_others(keep);
|
|
532
|
+
int q = kqueue();
|
|
533
|
+
int gone = 0;
|
|
534
|
+
if (q >= 0) {
|
|
535
|
+
struct kevent ev;
|
|
536
|
+
EV_SET(&ev, parent, EVFILT_PROC, EV_ADD, NOTE_EXIT, 0, NULL);
|
|
537
|
+
if (kevent(q, &ev, 1, NULL, 0, NULL) < 0) gone = errno == ESRCH;
|
|
538
|
+
else if (getppid() != parent) gone = 1;
|
|
539
|
+
else {
|
|
540
|
+
struct kevent got;
|
|
541
|
+
while (kevent(q, NULL, 0, &got, 1, NULL) < 0 && errno == EINTR) {}
|
|
542
|
+
gone = 1;
|
|
543
|
+
}
|
|
544
|
+
}
|
|
545
|
+
while (!gone) {
|
|
546
|
+
if (getppid() != parent) gone = 1;
|
|
547
|
+
else usleep(20000);
|
|
548
|
+
}
|
|
549
|
+
root = -1;
|
|
550
|
+
kill_tree(window_ms);
|
|
551
|
+
_exit(0);
|
|
552
|
+
}
|
|
553
|
+
|
|
554
|
+
/* Every exit after the job started: stop the reaper, then report. */
|
|
555
|
+
static int finish(const char *why, int code, int complete) {
|
|
556
|
+
stop_reaper();
|
|
557
|
+
if (!complete) out("exit kill_incomplete -1");
|
|
558
|
+
else out("exit %s %d", why, code);
|
|
559
|
+
return 0;
|
|
560
|
+
}
|
|
561
|
+
|
|
562
|
+
static int selftest(const char *dir) {
|
|
563
|
+
char mark[4096], ctrl[4096], profile[8192];
|
|
564
|
+
snprintf(mark, sizeof mark, "%s/selftest-%d.marker", dir, getpid());
|
|
565
|
+
snprintf(ctrl, sizeof ctrl, "%s/selftest-%d.control", dir, getpid());
|
|
566
|
+
int fm = open(mark, O_WRONLY | O_CREAT | O_TRUNC, 0600), fc = open(ctrl, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
|
567
|
+
if (fm < 0 || fc < 0) {
|
|
568
|
+
out("selftest files %d", errno);
|
|
569
|
+
out("selftest failed");
|
|
570
|
+
return 1;
|
|
571
|
+
}
|
|
572
|
+
close(fm);
|
|
573
|
+
close(fc);
|
|
574
|
+
marker = mark;
|
|
575
|
+
control = ctrl;
|
|
576
|
+
snprintf(profile, sizeof profile, "(version 1)(allow default)(deny file-read* (literal \"%s\"))", mark);
|
|
577
|
+
int ready[2], go[2];
|
|
578
|
+
if (pipe(ready) != 0 || pipe(go) != 0) {
|
|
579
|
+
out("selftest failed");
|
|
580
|
+
return 1;
|
|
581
|
+
}
|
|
582
|
+
pid_t c = fork();
|
|
583
|
+
if (c == 0) {
|
|
584
|
+
close(ready[0]);
|
|
585
|
+
close(go[1]);
|
|
586
|
+
char *err = NULL;
|
|
587
|
+
char ok = sandbox_init(profile, 0, &err) == 0 ? 1 : 0;
|
|
588
|
+
ssize_t w = write(ready[1], &ok, 1);
|
|
589
|
+
(void)w;
|
|
590
|
+
char b;
|
|
591
|
+
if (read(go[0], &b, 1) == 1) {
|
|
592
|
+
pid_t g = fork();
|
|
593
|
+
if (g == 0) _exit(0);
|
|
594
|
+
if (g > 0) waitpid(g, NULL, 0);
|
|
595
|
+
}
|
|
596
|
+
pause();
|
|
597
|
+
_exit(0);
|
|
598
|
+
}
|
|
599
|
+
close(ready[1]);
|
|
600
|
+
close(go[0]);
|
|
601
|
+
char ok = 0;
|
|
602
|
+
ssize_t r = c > 0 ? read(ready[0], &ok, 1) : -1;
|
|
603
|
+
int sandboxed = r == 1 && ok;
|
|
604
|
+
|
|
605
|
+
int is_member = sandboxed && member(c) == 1;
|
|
606
|
+
out("check member %d", is_member);
|
|
607
|
+
|
|
608
|
+
pid_t plain = fork();
|
|
609
|
+
if (plain == 0) {
|
|
610
|
+
pause();
|
|
611
|
+
_exit(0);
|
|
612
|
+
}
|
|
613
|
+
int is_nonmember = plain > 0 && member(plain) == 0 &&
|
|
614
|
+
sandbox_check(getpid(), "file-read-data", SANDBOX_FILTER_PATH | SANDBOX_CHECK_NO_REPORT, mark) == 0;
|
|
615
|
+
if (plain > 0) {
|
|
616
|
+
kill(plain, SIGKILL);
|
|
617
|
+
waitpid(plain, NULL, 0);
|
|
618
|
+
}
|
|
619
|
+
out("check nonmember %d", is_nonmember);
|
|
620
|
+
|
|
621
|
+
struct proc_bsdinfo bi;
|
|
622
|
+
int have = sandboxed && bsdinfo(c, &bi);
|
|
623
|
+
int match = have && signal_if_same(c, bi.pbi_start_tvsec, bi.pbi_start_tvusec, 0) == 0;
|
|
624
|
+
out("check identity_match %d", match);
|
|
625
|
+
int stale = have && signal_if_same(c, bi.pbi_start_tvsec + 1, bi.pbi_start_tvusec, 0) == -1;
|
|
626
|
+
out("check identity_stale %d", stale);
|
|
627
|
+
|
|
628
|
+
int forked = 0;
|
|
629
|
+
int q = kqueue();
|
|
630
|
+
if (sandboxed && q >= 0) {
|
|
631
|
+
struct kevent ev;
|
|
632
|
+
EV_SET(&ev, c, EVFILT_PROC, EV_ADD, NOTE_FORK, 0, NULL);
|
|
633
|
+
if (kevent(q, &ev, 1, NULL, 0, NULL) == 0) {
|
|
634
|
+
char b = 1;
|
|
635
|
+
if (write(go[1], &b, 1) == 1) {
|
|
636
|
+
struct timespec ts = {2, 0};
|
|
637
|
+
struct kevent got;
|
|
638
|
+
int n;
|
|
639
|
+
while ((n = kevent(q, NULL, 0, &got, 1, &ts)) < 0 && errno == EINTR) {}
|
|
640
|
+
forked = n == 1 && got.filter == EVFILT_PROC && (got.fflags & NOTE_FORK) != 0;
|
|
641
|
+
}
|
|
642
|
+
}
|
|
643
|
+
}
|
|
644
|
+
if (q >= 0) close(q);
|
|
645
|
+
out("check fork %d", forked);
|
|
646
|
+
|
|
647
|
+
if (c > 0) {
|
|
648
|
+
kill(c, SIGKILL);
|
|
649
|
+
waitpid(c, NULL, 0);
|
|
650
|
+
}
|
|
651
|
+
unlink(mark);
|
|
652
|
+
unlink(ctrl);
|
|
653
|
+
if (is_member && is_nonmember && match && stale && forked) {
|
|
654
|
+
out("selftest ok");
|
|
655
|
+
return 0;
|
|
656
|
+
}
|
|
657
|
+
out("selftest failed");
|
|
658
|
+
return 1;
|
|
659
|
+
}
|
|
660
|
+
|
|
661
|
+
int main(int argc, char **argv) {
|
|
662
|
+
signal(SIGPIPE, SIG_IGN);
|
|
663
|
+
mach_timebase_info(&tb);
|
|
664
|
+
if (argc == 3 && strcmp(argv[1], "--selftest") == 0) return selftest(argv[2]);
|
|
665
|
+
if (argc == 4 && strcmp(argv[1], "--reap") == 0) {
|
|
666
|
+
marker = argv[2];
|
|
667
|
+
control = argv[3];
|
|
668
|
+
return kill_tree(DEFAULT_WINDOW_MS) ? 0 : 3;
|
|
669
|
+
}
|
|
670
|
+
if (argc == 3 && strcmp(argv[1], "--start") == 0) {
|
|
671
|
+
struct proc_bsdinfo bi;
|
|
672
|
+
pid_t p = (pid_t)atoi(argv[2]);
|
|
673
|
+
if (p <= 0 || !bsdinfo(p, &bi)) return 1;
|
|
674
|
+
out("%llu %llu", (unsigned long long)bi.pbi_start_tvsec, (unsigned long long)bi.pbi_start_tvusec);
|
|
675
|
+
return 0;
|
|
676
|
+
}
|
|
677
|
+
if (argc == 5 && strcmp(argv[1], "--alive") == 0) {
|
|
678
|
+
struct proc_bsdinfo bi;
|
|
679
|
+
pid_t p = (pid_t)atoi(argv[2]);
|
|
680
|
+
if (p <= 0 || !bsdinfo(p, &bi) || bi.pbi_status == SZOMB) return 1;
|
|
681
|
+
return bi.pbi_start_tvsec == strtoull(argv[3], NULL, 10) && bi.pbi_start_tvusec == strtoull(argv[4], NULL, 10) ? 0 : 1;
|
|
682
|
+
}
|
|
683
|
+
|
|
684
|
+
char *app = NULL, *cwd = NULL, *logp = NULL, *profile = NULL;
|
|
685
|
+
char *args[1024], *envs[1024];
|
|
686
|
+
int nargs = 0, nenv = 0;
|
|
687
|
+
long long mem = 0, procs = 0, cpu = 0, win = DEFAULT_WINDOW_MS, fsize = 0, nofile = 0, room = 0;
|
|
688
|
+
for (;;) {
|
|
689
|
+
char *line = next_line();
|
|
690
|
+
if (!line) {
|
|
691
|
+
out("error spec %d", EINVAL);
|
|
692
|
+
return 2;
|
|
693
|
+
}
|
|
694
|
+
if (strcmp(line, "end") == 0) {
|
|
695
|
+
free(line);
|
|
696
|
+
break;
|
|
697
|
+
}
|
|
698
|
+
char *tab = strchr(line, '\t');
|
|
699
|
+
if (!tab) {
|
|
700
|
+
free(line);
|
|
701
|
+
continue;
|
|
702
|
+
}
|
|
703
|
+
*tab = 0;
|
|
704
|
+
char *v = b64dec(tab + 1);
|
|
705
|
+
if (!v) {
|
|
706
|
+
out("error spec %d", ENOMEM);
|
|
707
|
+
return 2;
|
|
708
|
+
}
|
|
709
|
+
if (strcmp(line, "app") == 0) app = v;
|
|
710
|
+
else if (strcmp(line, "arg") == 0 && nargs < 1023) args[nargs++] = v;
|
|
711
|
+
else if (strcmp(line, "env") == 0 && nenv < 1023) envs[nenv++] = v;
|
|
712
|
+
else if (strcmp(line, "cwd") == 0) cwd = v;
|
|
713
|
+
else if (strcmp(line, "log") == 0) logp = v;
|
|
714
|
+
else if (strcmp(line, "profile") == 0) profile = v;
|
|
715
|
+
else if (strcmp(line, "mark") == 0) marker = v;
|
|
716
|
+
else if (strcmp(line, "ctrl") == 0) control = v;
|
|
717
|
+
else if (strcmp(line, "mem") == 0) mem = atoll(v);
|
|
718
|
+
else if (strcmp(line, "procs") == 0) procs = atoll(v);
|
|
719
|
+
else if (strcmp(line, "cpu") == 0) cpu = atoll(v);
|
|
720
|
+
else if (strcmp(line, "win") == 0) win = atoll(v);
|
|
721
|
+
else if (strcmp(line, "seen") == 0) seen_cap = atoi(v);
|
|
722
|
+
else if (strcmp(line, "fsize") == 0) fsize = atoll(v);
|
|
723
|
+
else if (strcmp(line, "nofile") == 0) nofile = atoll(v);
|
|
724
|
+
else if (strcmp(line, "room") == 0) room = atoll(v);
|
|
725
|
+
else if (strcmp(line, "sample") == 0) sampling = atoi(v) != 0;
|
|
726
|
+
else free(v);
|
|
727
|
+
free(line);
|
|
728
|
+
}
|
|
729
|
+
args[nargs] = NULL;
|
|
730
|
+
envs[nenv] = NULL;
|
|
731
|
+
if (win < 0) win = 0;
|
|
732
|
+
if (seen_cap < 1) seen_cap = 1;
|
|
733
|
+
if (!app || !cwd || !logp || !profile || !marker || !control || nargs == 0) {
|
|
734
|
+
out("error spec %d", EINVAL);
|
|
735
|
+
return 2;
|
|
736
|
+
}
|
|
737
|
+
seen = calloc((size_t)seen_cap, sizeof(seen_t));
|
|
738
|
+
if (!seen) {
|
|
739
|
+
out("error spec %d", ENOMEM);
|
|
740
|
+
return 2;
|
|
741
|
+
}
|
|
742
|
+
|
|
743
|
+
/* The watchdog pipe: the helper and the reaper hold the write end, the watchdog the read end. */
|
|
744
|
+
int wd[2];
|
|
745
|
+
if (pipe(wd) != 0) {
|
|
746
|
+
out("error pipe %d", errno);
|
|
747
|
+
return 1;
|
|
748
|
+
}
|
|
749
|
+
fcntl(wd[0], F_SETFD, FD_CLOEXEC);
|
|
750
|
+
fcntl(wd[1], F_SETFD, FD_CLOEXEC);
|
|
751
|
+
pid_t helper = getpid();
|
|
752
|
+
fflush(stdout);
|
|
753
|
+
reaper = fork();
|
|
754
|
+
if (reaper < 0) {
|
|
755
|
+
out("error fork %d", errno);
|
|
756
|
+
return 1;
|
|
757
|
+
}
|
|
758
|
+
if (reaper == 0) run_reaper(helper, win, wd[1]);
|
|
759
|
+
out("reaper %d", reaper);
|
|
760
|
+
|
|
761
|
+
kq = kqueue();
|
|
762
|
+
if (kq < 0) {
|
|
763
|
+
int e = errno;
|
|
764
|
+
stop_reaper();
|
|
765
|
+
out("error kqueue %d", e);
|
|
766
|
+
return 1;
|
|
767
|
+
}
|
|
768
|
+
struct kevent in;
|
|
769
|
+
EV_SET(&in, 0, EVFILT_READ, EV_ADD, 0, 0, NULL);
|
|
770
|
+
kevent(kq, &in, 1, NULL, 0, NULL);
|
|
771
|
+
|
|
772
|
+
int ep[2];
|
|
773
|
+
if (pipe(ep) != 0) {
|
|
774
|
+
int e = errno;
|
|
775
|
+
stop_reaper();
|
|
776
|
+
out("error pipe %d", e);
|
|
777
|
+
return 1;
|
|
778
|
+
}
|
|
779
|
+
fcntl(ep[0], F_SETFD, FD_CLOEXEC);
|
|
780
|
+
fcntl(ep[1], F_SETFD, FD_CLOEXEC);
|
|
781
|
+
pid_t pid = fork();
|
|
782
|
+
if (pid < 0) {
|
|
783
|
+
int e = errno;
|
|
784
|
+
stop_reaper();
|
|
785
|
+
out("error fork %d", e);
|
|
786
|
+
return 1;
|
|
787
|
+
}
|
|
788
|
+
if (pid == 0) {
|
|
789
|
+
close(ep[0]);
|
|
790
|
+
signal(SIGPIPE, SIG_DFL);
|
|
791
|
+
int fd = open(logp, O_WRONLY | O_CREAT | O_TRUNC, 0600);
|
|
792
|
+
if (fd < 0) report(ep[1], 1, errno);
|
|
793
|
+
dup2(fd, 1);
|
|
794
|
+
dup2(fd, 2);
|
|
795
|
+
close(fd);
|
|
796
|
+
int nul = open("/dev/null", O_RDONLY);
|
|
797
|
+
if (nul < 0) report(ep[1], 3, errno);
|
|
798
|
+
dup2(nul, 0);
|
|
799
|
+
close(nul);
|
|
800
|
+
if (chdir(cwd) != 0) report(ep[1], 2, errno);
|
|
801
|
+
close(wd[1]);
|
|
802
|
+
/*
|
|
803
|
+
* Kernel limits, set before the sandbox (which denies the kern.proc sysctl)
|
|
804
|
+
* and inherited by every descendant. RLIMIT_CPU is per process: SIGXCPU at
|
|
805
|
+
* the soft limit, SIGKILL one second later. RLIMIT_NPROC counts every
|
|
806
|
+
* process of this uid, so it is set to the uid's count now plus the job's
|
|
807
|
+
* limit, the watchdog and room: a ceiling no fork bomb can pass.
|
|
808
|
+
*/
|
|
809
|
+
struct rlimit rl;
|
|
810
|
+
if (cpu > 0) {
|
|
811
|
+
rl.rlim_cur = (rlim_t)((cpu + 999) / 1000);
|
|
812
|
+
rl.rlim_max = rl.rlim_cur + 1;
|
|
813
|
+
if (setrlimit(RLIMIT_CPU, &rl) != 0) report(ep[1], 6, errno);
|
|
814
|
+
}
|
|
815
|
+
if (procs > 0) {
|
|
816
|
+
long long have = user_procs();
|
|
817
|
+
if (have < 0) report(ep[1], 6, errno);
|
|
818
|
+
rl.rlim_cur = rl.rlim_max = (rlim_t)(have + procs + 1 + (room > 0 ? room : 0));
|
|
819
|
+
if (setrlimit(RLIMIT_NPROC, &rl) != 0) report(ep[1], 6, errno);
|
|
820
|
+
}
|
|
821
|
+
if (fsize > 0) {
|
|
822
|
+
rl.rlim_cur = rl.rlim_max = (rlim_t)fsize;
|
|
823
|
+
if (setrlimit(RLIMIT_FSIZE, &rl) != 0) report(ep[1], 6, errno);
|
|
824
|
+
}
|
|
825
|
+
if (nofile > 0) {
|
|
826
|
+
rl.rlim_cur = rl.rlim_max = (rlim_t)nofile;
|
|
827
|
+
if (setrlimit(RLIMIT_NOFILE, &rl) != 0) report(ep[1], 6, errno);
|
|
828
|
+
}
|
|
829
|
+
rl.rlim_cur = rl.rlim_max = 0;
|
|
830
|
+
setrlimit(RLIMIT_CORE, &rl);
|
|
831
|
+
char *err = NULL;
|
|
832
|
+
if (sandbox_init(profile, 0, &err) != 0) report(ep[1], 4, EPERM);
|
|
833
|
+
pid_t w = fork();
|
|
834
|
+
if (w < 0) report(ep[1], 7, errno);
|
|
835
|
+
if (w == 0) run_watchdog(wd[0], win);
|
|
836
|
+
close(wd[0]);
|
|
837
|
+
int msg[2] = {WD_MSG, (int)w};
|
|
838
|
+
ssize_t sent = write(ep[1], msg, sizeof msg);
|
|
839
|
+
(void)sent;
|
|
840
|
+
execve(app, args, envs);
|
|
841
|
+
report(ep[1], 5, errno);
|
|
842
|
+
}
|
|
843
|
+
root = pid;
|
|
844
|
+
close(ep[1]);
|
|
845
|
+
close(wd[0]);
|
|
846
|
+
int v[2] = {0, 0};
|
|
847
|
+
ssize_t r;
|
|
848
|
+
for (;;) {
|
|
849
|
+
do r = read(ep[0], v, sizeof v);
|
|
850
|
+
while (r < 0 && errno == EINTR);
|
|
851
|
+
if (r == (ssize_t)sizeof v && v[0] == WD_MSG) {
|
|
852
|
+
wdpid = (pid_t)v[1];
|
|
853
|
+
continue;
|
|
854
|
+
}
|
|
855
|
+
break;
|
|
856
|
+
}
|
|
857
|
+
close(ep[0]);
|
|
858
|
+
if (r == (ssize_t)sizeof v) {
|
|
859
|
+
reap_root(1);
|
|
860
|
+
if (wdpid > 0 && member(wdpid)) kill(wdpid, SIGKILL);
|
|
861
|
+
stop_reaper();
|
|
862
|
+
out("error %s %d", STAGES[v[0] >= 0 && v[0] <= NSTAGES ? v[0] : 0], v[1]);
|
|
863
|
+
return 1;
|
|
864
|
+
}
|
|
865
|
+
if (wdpid <= 0) {
|
|
866
|
+
kill(root, SIGKILL);
|
|
867
|
+
reap_root(1);
|
|
868
|
+
stop_reaper();
|
|
869
|
+
out("error watchdog %d", ESRCH);
|
|
870
|
+
return 1;
|
|
871
|
+
}
|
|
872
|
+
out("watchdog %d", wdpid);
|
|
873
|
+
out("started %d", pid);
|
|
874
|
+
seen_index(pid);
|
|
875
|
+
|
|
876
|
+
long long tick_ms = mem > 0 ? 10 : 50;
|
|
877
|
+
struct timespec tick = {0, tick_ms * 1000000L};
|
|
878
|
+
unsigned long long next_sample = 0, next_scan = 0;
|
|
879
|
+
for (;;) {
|
|
880
|
+
struct kevent evs[64];
|
|
881
|
+
int ne = kevent(kq, NULL, 0, evs, 64, &tick);
|
|
882
|
+
int readable = 0;
|
|
883
|
+
const char *breach = NULL;
|
|
884
|
+
for (int i = 0; i < ne; i++) {
|
|
885
|
+
if (evs[i].filter == EVFILT_READ) readable = 1;
|
|
886
|
+
if (evs[i].filter != EVFILT_PROC) continue;
|
|
887
|
+
if (evs[i].fflags & NOTE_EXIT) mark_dead((pid_t)evs[i].ident);
|
|
888
|
+
if ((evs[i].fflags & NOTE_FORK) && !breach && sampling) breach = on_fork((pid_t)evs[i].ident, procs);
|
|
889
|
+
}
|
|
890
|
+
if (breach) {
|
|
891
|
+
if (!root_done) kill(root, SIGKILL);
|
|
892
|
+
return finish(breach, root_status, kill_tree(win));
|
|
893
|
+
}
|
|
894
|
+
int stop = 0;
|
|
895
|
+
if (readable && inlen < sizeof inbuf) {
|
|
896
|
+
ssize_t n = read(0, inbuf + inlen, sizeof inbuf - inlen);
|
|
897
|
+
if (n == 0 || (n < 0 && errno != EINTR && errno != EAGAIN)) stop = 1;
|
|
898
|
+
else if (n > 0) inlen += (size_t)n;
|
|
899
|
+
}
|
|
900
|
+
/* Lines left over from the spec read are handled here too, not only after a new read. */
|
|
901
|
+
char *line;
|
|
902
|
+
while (!stop && memchr(inbuf, '\n', inlen) && (line = take_line())) {
|
|
903
|
+
if (strcmp(line, "cancel") == 0) stop = 1;
|
|
904
|
+
free(line);
|
|
905
|
+
}
|
|
906
|
+
if (stop) return finish("cancelled", root_status, kill_tree(win));
|
|
907
|
+
if (inlen == sizeof inbuf) return finish("protocol", -1, kill_tree(win));
|
|
908
|
+
reap_root(0);
|
|
909
|
+
/* A root the kernel stopped at its RLIMIT_CPU soft limit is a CPU limit breach, not an ordinary exit. */
|
|
910
|
+
if (root_done) return finish(root_sig == SIGXCPU && cpu > 0 ? "cpu_limit" : "exited", root_status, kill_tree(win));
|
|
911
|
+
if (!sampling || now_ms() < next_sample) continue;
|
|
912
|
+
next_sample = now_ms() + tick_ms;
|
|
913
|
+
int discover = now_ms() >= next_scan;
|
|
914
|
+
if (discover) next_scan = now_ms() + SCAN_MS;
|
|
915
|
+
procs_limit = procs;
|
|
916
|
+
scanning = 1;
|
|
917
|
+
breach = sample(mem, procs, cpu, discover);
|
|
918
|
+
scanning = 0;
|
|
919
|
+
if (fork_breach) breach = fork_breach;
|
|
920
|
+
if (breach) {
|
|
921
|
+
if (!root_done) kill(root, SIGKILL);
|
|
922
|
+
return finish(breach, root_status, kill_tree(win));
|
|
923
|
+
}
|
|
924
|
+
}
|
|
925
|
+
}
|
|
926
|
+
`;
|
|
927
|
+
//# sourceMappingURL=macos-job-helper.js.map
|