@mrrlin-dev/mcp 0.4.223 → 0.4.225

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.
@@ -0,0 +1,185 @@
1
+ /*
2
+ * mrrlin-drain-ns — Linux drain helper for one supervised attempt tree
3
+ * (openspec change f54-trusted-attempt-drain, design §2.4).
4
+ *
5
+ * Usage: mrrlin-drain-ns <uid> <gid> <user> <deadline-ms> -- <worker> [args...]
6
+ *
7
+ * fd 0/1/2 the attempt worker's stdin/stdout/stderr (the service relays them)
8
+ * fd 3 control pipe from the service: EOF or the byte 'S' means "drain now"
9
+ * fd 4 result pipe to the service: one line "DRAINED" or "TIMEOUT" or "ERROR <step>"
10
+ *
11
+ * The helper clones a new PID namespace. Its init (PID 1) drops to the provider account, clears every
12
+ * capability set, sets no_new_privs, forks the worker and reaps forever. Draining means SIGKILL to
13
+ * PID 1 through a pidfd: the kernel then kills every member of the namespace (and of any nested one)
14
+ * and only lets PID 1 become waitable after all of them are reaped (zap_pid_ns_processes). So when
15
+ * waitid() on the pidfd returns, every process the tree ever contained is gone. A member stuck in
16
+ * uninterruptible sleep delays that; the deadline turns the delay into TIMEOUT, never into DRAINED.
17
+ */
18
+ #define _GNU_SOURCE
19
+ #include <errno.h>
20
+ #include <grp.h>
21
+ #include <linux/sched.h>
22
+ #include <poll.h>
23
+ #include <pwd.h>
24
+ #include <signal.h>
25
+ #include <stdint.h>
26
+ #include <stdio.h>
27
+ #include <stdlib.h>
28
+ #include <string.h>
29
+ #include <sys/prctl.h>
30
+ #include <sys/syscall.h>
31
+ #include <sys/types.h>
32
+ #include <sys/wait.h>
33
+ #include <time.h>
34
+ #include <unistd.h>
35
+ #include <linux/capability.h>
36
+
37
+ #ifndef SYS_clone3
38
+ #define SYS_clone3 435
39
+ #endif
40
+ #ifndef SYS_pidfd_send_signal
41
+ #define SYS_pidfd_send_signal 424
42
+ #endif
43
+ #ifndef P_PIDFD
44
+ #define P_PIDFD 3
45
+ #endif
46
+
47
+ #define CONTROL_FD 3
48
+ #define RESULT_FD 4
49
+
50
+ static void result(const char *line) {
51
+ size_t length = strlen(line);
52
+ ssize_t written = write(RESULT_FD, line, length);
53
+ (void)written;
54
+ }
55
+
56
+ static int in_namespace_init = 0;
57
+
58
+ static void fail(const char *step) {
59
+ char line[128];
60
+ snprintf(line, sizeof line, "ERROR %s %d\n", step, errno);
61
+ if (in_namespace_init) {
62
+ /* The result pipe is closed in the init; its stderr is the worker's stderr. */
63
+ ssize_t written = write(2, line, strlen(line));
64
+ (void)written;
65
+ _exit(3);
66
+ }
67
+ result(line);
68
+ _exit(3);
69
+ }
70
+
71
+ static long long now_ms(void) {
72
+ struct timespec ts;
73
+ clock_gettime(CLOCK_MONOTONIC, &ts);
74
+ return (long long)ts.tv_sec * 1000 + ts.tv_nsec / 1000000;
75
+ }
76
+
77
+ static void drop_privileges(uid_t uid, gid_t gid, const char *user) {
78
+ /* The bounding set can only shrink while CAP_SETPCAP is still held, i.e. before the id change. */
79
+ for (int cap = 0; cap <= 63; cap++) {
80
+ if (prctl(PR_CAPBSET_READ, cap, 0, 0, 0) == 1 && prctl(PR_CAPBSET_DROP, cap, 0, 0, 0) != 0) fail("capbset");
81
+ }
82
+ if (prctl(PR_CAP_AMBIENT, PR_CAP_AMBIENT_CLEAR_ALL, 0, 0, 0) != 0 && errno != EINVAL) fail("ambient");
83
+ if (initgroups(user, gid) != 0) fail("initgroups");
84
+ if (setresgid(gid, gid, gid) != 0) fail("setresgid");
85
+ if (setresuid(uid, uid, uid) != 0) fail("setresuid");
86
+ struct __user_cap_header_struct header = { _LINUX_CAPABILITY_VERSION_3, 0 };
87
+ struct __user_cap_data_struct data[2];
88
+ memset(data, 0, sizeof data);
89
+ if (syscall(SYS_capset, &header, data) != 0) fail("capset");
90
+ if (prctl(PR_SET_NO_NEW_PRIVS, 1, 0, 0, 0) != 0) fail("no_new_privs");
91
+ if (getuid() != uid || geteuid() != uid || getgid() != gid) fail("verify_ids");
92
+ /* Changing credentials clears the parent-death signal: set it again, or a helper killed with SIGKILL
93
+ would leave this init (and the tree) running. */
94
+ if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) != 0) fail("init_pdeathsig_after_drop");
95
+ for (int cap = 0; cap <= 63; cap++) {
96
+ if (prctl(PR_CAPBSET_READ, cap, 0, 0, 0) == 1) fail("verify_capbset");
97
+ }
98
+ }
99
+
100
+ /* PID 1 of the new namespace: fork the worker, then reap until killed. */
101
+ static int namespace_init(uid_t uid, gid_t gid, const char *user, char **worker) {
102
+ in_namespace_init = 1;
103
+ if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) != 0) fail("init_pdeathsig");
104
+ /* getppid() is 0 for a namespace init, so it cannot detect a dead helper; PDEATHSIG above and the
105
+ helper's own pidfd kill on control-pipe EOF cover that case. */
106
+ close(CONTROL_FD);
107
+ close(RESULT_FD);
108
+ drop_privileges(uid, gid, user);
109
+ pid_t child = fork();
110
+ if (child < 0) _exit(5);
111
+ if (child == 0) {
112
+ execvp(worker[0], worker);
113
+ _exit(127);
114
+ }
115
+ for (;;) {
116
+ if (waitpid(-1, NULL, 0) < 0 && errno == ECHILD) pause();
117
+ }
118
+ }
119
+
120
+ int main(int argc, char **argv) {
121
+ if (argc < 7 || strcmp(argv[5], "--") != 0) {
122
+ fprintf(stderr, "usage: mrrlin-drain-ns <uid> <gid> <user> <deadline-ms> -- <worker> [args...]\n");
123
+ return 2;
124
+ }
125
+ char *end = NULL;
126
+ uid_t uid = (uid_t)strtoul(argv[1], &end, 10);
127
+ if (*end != '\0' || uid == 0) return 2;
128
+ gid_t gid = (gid_t)strtoul(argv[2], &end, 10);
129
+ if (*end != '\0' || gid == 0) return 2;
130
+ const char *user = argv[3];
131
+ long long deadline_ms = strtoll(argv[4], &end, 10);
132
+ if (*end != '\0' || deadline_ms <= 0) return 2;
133
+
134
+ /* Secondary backstop only: the primary death signal is EOF on the control pipe. */
135
+ pid_t parent = getppid();
136
+ if (prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0) != 0) fail("pdeathsig");
137
+ if (getppid() != parent) fail("parent_gone");
138
+
139
+ int pidfd = -1;
140
+ struct clone_args args;
141
+ memset(&args, 0, sizeof args);
142
+ args.flags = CLONE_NEWPID | CLONE_PIDFD;
143
+ args.pidfd = (uint64_t)(uintptr_t)&pidfd;
144
+ args.exit_signal = SIGCHLD;
145
+ long child = syscall(SYS_clone3, &args, sizeof args);
146
+ if (child < 0) fail("clone3");
147
+ if (child == 0) return namespace_init(uid, gid, user, &argv[6]);
148
+
149
+ /* The worker's stdio belongs to the tree now. */
150
+ close(0);
151
+ close(1);
152
+
153
+ struct pollfd fds[2] = { { CONTROL_FD, POLLIN, 0 }, { pidfd, POLLIN, 0 } };
154
+ for (;;) {
155
+ if (poll(fds, 2, -1) < 0) {
156
+ if (errno == EINTR) continue;
157
+ fail("poll");
158
+ }
159
+ if (fds[1].revents) break; /* the init died on its own: the kernel drained the namespace */
160
+ if (fds[0].revents) {
161
+ char byte;
162
+ ssize_t n = read(CONTROL_FD, &byte, 1);
163
+ if (n <= 0 || byte == 'S') break; /* EOF (service gone) or an explicit stop */
164
+ }
165
+ }
166
+
167
+ if (syscall(SYS_pidfd_send_signal, pidfd, SIGKILL, NULL, 0) != 0 && errno != ESRCH) fail("pidfd_send_signal");
168
+ long long deadline = now_ms() + deadline_ms;
169
+ struct pollfd exitfd = { pidfd, POLLIN, 0 };
170
+ for (;;) {
171
+ long long remaining = deadline - now_ms();
172
+ if (remaining <= 0) {
173
+ result("TIMEOUT\n");
174
+ return 1;
175
+ }
176
+ int ready = poll(&exitfd, 1, (int)(remaining > 1000 ? 1000 : remaining));
177
+ if (ready < 0 && errno != EINTR) fail("poll_exit");
178
+ if (ready > 0) break;
179
+ }
180
+ siginfo_t info;
181
+ memset(&info, 0, sizeof info);
182
+ if (waitid((idtype_t)P_PIDFD, (id_t)pidfd, &info, WEXITED) != 0) fail("waitid");
183
+ result("DRAINED\n");
184
+ return 0;
185
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@mrrlin-dev/mcp",
3
- "version": "0.4.223",
3
+ "version": "0.4.225",
4
4
  "type": "module",
5
5
  "bin": {
6
6
  "mrrlin-mcp": "dist/bin.cjs"
@@ -11,6 +11,7 @@
11
11
  "files": [
12
12
  "dist/bin.cjs",
13
13
  "dist/consensus/personas",
14
+ "native",
14
15
  "scripts/postinstall-restart.cjs"
15
16
  ],
16
17
  "dependencies": {