Tang-ZW

Untitled

Mar 2nd, 2026
116
0
Never
7
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 96.54 KB | None | 0 0
  1. // autogenerated by syzkaller (https://github.com/google/syzkaller)
  2.  
  3. #define _GNU_SOURCE
  4.  
  5. #include <arpa/inet.h>
  6. #include <dirent.h>
  7. #include <endian.h>
  8. #include <errno.h>
  9. #include <fcntl.h>
  10. #include <net/if.h>
  11. #include <net/if_arp.h>
  12. #include <netinet/in.h>
  13. #include <pthread.h>
  14. #include <sched.h>
  15. #include <setjmp.h>
  16. #include <signal.h>
  17. #include <stdarg.h>
  18. #include <stdbool.h>
  19. #include <stdint.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #include <string.h>
  23. #include <sys/ioctl.h>
  24. #include <sys/mount.h>
  25. #include <sys/prctl.h>
  26. #include <sys/resource.h>
  27. #include <sys/socket.h>
  28. #include <sys/stat.h>
  29. #include <sys/syscall.h>
  30. #include <sys/time.h>
  31. #include <sys/types.h>
  32. #include <sys/uio.h>
  33. #include <sys/wait.h>
  34. #include <time.h>
  35. #include <unistd.h>
  36.  
  37. #include <linux/capability.h>
  38. #include <linux/futex.h>
  39. #include <linux/genetlink.h>
  40. #include <linux/if_addr.h>
  41. #include <linux/if_ether.h>
  42. #include <linux/if_link.h>
  43. #include <linux/if_tun.h>
  44. #include <linux/in6.h>
  45. #include <linux/ip.h>
  46. #include <linux/neighbour.h>
  47. #include <linux/net.h>
  48. #include <linux/netlink.h>
  49. #include <linux/rtnetlink.h>
  50. #include <linux/tcp.h>
  51. #include <linux/veth.h>
  52.  
  53. static unsigned long long procid;
  54.  
  55. static __thread int clone_ongoing;
  56. static __thread int skip_segv;
  57. static __thread jmp_buf segv_env;
  58.  
  59. static void segv_handler(int sig, siginfo_t* info, void* ctx)
  60. {
  61. if (__atomic_load_n(&clone_ongoing, __ATOMIC_RELAXED) != 0) {
  62. exit(sig);
  63. }
  64. uintptr_t addr = (uintptr_t)info->si_addr;
  65. const uintptr_t prog_start = 1 << 20;
  66. const uintptr_t prog_end = 100 << 20;
  67. int skip = __atomic_load_n(&skip_segv, __ATOMIC_RELAXED) != 0;
  68. int valid = addr < prog_start || addr > prog_end;
  69. if (skip && valid) {
  70. _longjmp(segv_env, 1);
  71. }
  72. exit(sig);
  73. }
  74.  
  75. static void install_segv_handler(void)
  76. {
  77. struct sigaction sa;
  78. memset(&sa, 0, sizeof(sa));
  79. sa.sa_handler = SIG_IGN;
  80. syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8);
  81. syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8);
  82. memset(&sa, 0, sizeof(sa));
  83. sa.sa_sigaction = segv_handler;
  84. sa.sa_flags = SA_NODEFER | SA_SIGINFO;
  85. sigaction(SIGSEGV, &sa, NULL);
  86. sigaction(SIGBUS, &sa, NULL);
  87. }
  88.  
  89. #define NONFAILING(...) \
  90. ({ \
  91. int ok = 1; \
  92. __atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \
  93. if (_setjmp(segv_env) == 0) { \
  94. __VA_ARGS__; \
  95. } else \
  96. ok = 0; \
  97. __atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \
  98. ok; \
  99. })
  100.  
  101. static void sleep_ms(uint64_t ms)
  102. {
  103. usleep(ms * 1000);
  104. }
  105.  
  106. static uint64_t current_time_ms(void)
  107. {
  108. struct timespec ts;
  109. if (clock_gettime(CLOCK_MONOTONIC, &ts))
  110. exit(1);
  111. return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
  112. }
  113.  
  114. static void use_temporary_dir(void)
  115. {
  116. char tmpdir_template[] = "./syzkaller.XXXXXX";
  117. char* tmpdir = mkdtemp(tmpdir_template);
  118. if (!tmpdir)
  119. exit(1);
  120. if (chmod(tmpdir, 0777))
  121. exit(1);
  122. if (chdir(tmpdir))
  123. exit(1);
  124. }
  125.  
  126. static void thread_start(void* (*fn)(void*), void* arg)
  127. {
  128. pthread_t th;
  129. pthread_attr_t attr;
  130. pthread_attr_init(&attr);
  131. pthread_attr_setstacksize(&attr, 128 << 10);
  132. int i = 0;
  133. for (; i < 100; i++) {
  134. if (pthread_create(&th, &attr, fn, arg) == 0) {
  135. pthread_attr_destroy(&attr);
  136. return;
  137. }
  138. if (errno == EAGAIN) {
  139. usleep(50);
  140. continue;
  141. }
  142. break;
  143. }
  144. exit(1);
  145. }
  146.  
  147. #define BITMASK(bf_off, bf_len) (((1ull << (bf_len)) - 1) << (bf_off))
  148. #define STORE_BY_BITMASK(type, htobe, addr, val, bf_off, bf_len) \
  149. *(type*)(addr) = \
  150. htobe((htobe(*(type*)(addr)) & ~BITMASK((bf_off), (bf_len))) | \
  151. (((type)(val) << (bf_off)) & BITMASK((bf_off), (bf_len))))
  152.  
  153. typedef struct {
  154. int state;
  155. } event_t;
  156.  
  157. static void event_init(event_t* ev)
  158. {
  159. ev->state = 0;
  160. }
  161.  
  162. static void event_reset(event_t* ev)
  163. {
  164. ev->state = 0;
  165. }
  166.  
  167. static void event_set(event_t* ev)
  168. {
  169. if (ev->state)
  170. exit(1);
  171. __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
  172. syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000);
  173. }
  174.  
  175. static void event_wait(event_t* ev)
  176. {
  177. while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
  178. syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
  179. }
  180.  
  181. static int event_isset(event_t* ev)
  182. {
  183. return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE);
  184. }
  185.  
  186. static int event_timedwait(event_t* ev, uint64_t timeout)
  187. {
  188. uint64_t start = current_time_ms();
  189. uint64_t now = start;
  190. for (;;) {
  191. uint64_t remain = timeout - (now - start);
  192. struct timespec ts;
  193. ts.tv_sec = remain / 1000;
  194. ts.tv_nsec = (remain % 1000) * 1000 * 1000;
  195. syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts);
  196. if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
  197. return 1;
  198. now = current_time_ms();
  199. if (now - start > timeout)
  200. return 0;
  201. }
  202. }
  203.  
  204. static bool write_file(const char* file, const char* what, ...)
  205. {
  206. char buf[1024];
  207. va_list args;
  208. va_start(args, what);
  209. vsnprintf(buf, sizeof(buf), what, args);
  210. va_end(args);
  211. buf[sizeof(buf) - 1] = 0;
  212. int len = strlen(buf);
  213. int fd = open(file, O_WRONLY | O_CLOEXEC);
  214. if (fd == -1)
  215. return false;
  216. if (write(fd, buf, len) != len) {
  217. int err = errno;
  218. close(fd);
  219. errno = err;
  220. return false;
  221. }
  222. close(fd);
  223. return true;
  224. }
  225.  
  226. struct nlmsg {
  227. char* pos;
  228. int nesting;
  229. struct nlattr* nested[8];
  230. char buf[4096];
  231. };
  232.  
  233. static void netlink_init(struct nlmsg* nlmsg, int typ, int flags,
  234. const void* data, int size)
  235. {
  236. memset(nlmsg, 0, sizeof(*nlmsg));
  237. struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
  238. hdr->nlmsg_type = typ;
  239. hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags;
  240. memcpy(hdr + 1, data, size);
  241. nlmsg->pos = (char*)(hdr + 1) + NLMSG_ALIGN(size);
  242. }
  243.  
  244. static void netlink_attr(struct nlmsg* nlmsg, int typ, const void* data,
  245. int size)
  246. {
  247. struct nlattr* attr = (struct nlattr*)nlmsg->pos;
  248. attr->nla_len = sizeof(*attr) + size;
  249. attr->nla_type = typ;
  250. if (size > 0)
  251. memcpy(attr + 1, data, size);
  252. nlmsg->pos += NLMSG_ALIGN(attr->nla_len);
  253. }
  254.  
  255. static void netlink_nest(struct nlmsg* nlmsg, int typ)
  256. {
  257. struct nlattr* attr = (struct nlattr*)nlmsg->pos;
  258. attr->nla_type = typ;
  259. nlmsg->pos += sizeof(*attr);
  260. nlmsg->nested[nlmsg->nesting++] = attr;
  261. }
  262.  
  263. static void netlink_done(struct nlmsg* nlmsg)
  264. {
  265. struct nlattr* attr = nlmsg->nested[--nlmsg->nesting];
  266. attr->nla_len = nlmsg->pos - (char*)attr;
  267. }
  268.  
  269. static int netlink_send_ext(struct nlmsg* nlmsg, int sock, uint16_t reply_type,
  270. int* reply_len, bool dofail)
  271. {
  272. if (nlmsg->pos > nlmsg->buf + sizeof(nlmsg->buf) || nlmsg->nesting)
  273. exit(1);
  274. struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf;
  275. hdr->nlmsg_len = nlmsg->pos - nlmsg->buf;
  276. struct sockaddr_nl addr;
  277. memset(&addr, 0, sizeof(addr));
  278. addr.nl_family = AF_NETLINK;
  279. ssize_t n = sendto(sock, nlmsg->buf, hdr->nlmsg_len, 0,
  280. (struct sockaddr*)&addr, sizeof(addr));
  281. if (n != (ssize_t)hdr->nlmsg_len) {
  282. if (dofail)
  283. exit(1);
  284. return -1;
  285. }
  286. n = recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0);
  287. if (reply_len)
  288. *reply_len = 0;
  289. if (n < 0) {
  290. if (dofail)
  291. exit(1);
  292. return -1;
  293. }
  294. if (n < (ssize_t)sizeof(struct nlmsghdr)) {
  295. errno = EINVAL;
  296. if (dofail)
  297. exit(1);
  298. return -1;
  299. }
  300. if (hdr->nlmsg_type == NLMSG_DONE)
  301. return 0;
  302. if (reply_len && hdr->nlmsg_type == reply_type) {
  303. *reply_len = n;
  304. return 0;
  305. }
  306. if (n < (ssize_t)(sizeof(struct nlmsghdr) + sizeof(struct nlmsgerr))) {
  307. errno = EINVAL;
  308. if (dofail)
  309. exit(1);
  310. return -1;
  311. }
  312. if (hdr->nlmsg_type != NLMSG_ERROR) {
  313. errno = EINVAL;
  314. if (dofail)
  315. exit(1);
  316. return -1;
  317. }
  318. errno = -((struct nlmsgerr*)(hdr + 1))->error;
  319. return -errno;
  320. }
  321.  
  322. static int netlink_send(struct nlmsg* nlmsg, int sock)
  323. {
  324. return netlink_send_ext(nlmsg, sock, 0, NULL, true);
  325. }
  326.  
  327. static int netlink_query_family_id(struct nlmsg* nlmsg, int sock,
  328. const char* family_name, bool dofail)
  329. {
  330. struct genlmsghdr genlhdr;
  331. memset(&genlhdr, 0, sizeof(genlhdr));
  332. genlhdr.cmd = CTRL_CMD_GETFAMILY;
  333. netlink_init(nlmsg, GENL_ID_CTRL, 0, &genlhdr, sizeof(genlhdr));
  334. netlink_attr(nlmsg, CTRL_ATTR_FAMILY_NAME, family_name,
  335. strnlen(family_name, GENL_NAMSIZ - 1) + 1);
  336. int n = 0;
  337. int err = netlink_send_ext(nlmsg, sock, GENL_ID_CTRL, &n, dofail);
  338. if (err < 0) {
  339. return -1;
  340. }
  341. uint16_t id = 0;
  342. struct nlattr* attr = (struct nlattr*)(nlmsg->buf + NLMSG_HDRLEN +
  343. NLMSG_ALIGN(sizeof(genlhdr)));
  344. for (; (char*)attr < nlmsg->buf + n;
  345. attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) {
  346. if (attr->nla_type == CTRL_ATTR_FAMILY_ID) {
  347. id = *(uint16_t*)(attr + 1);
  348. break;
  349. }
  350. }
  351. if (!id) {
  352. errno = EINVAL;
  353. return -1;
  354. }
  355. recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0);
  356. return id;
  357. }
  358.  
  359. static int netlink_next_msg(struct nlmsg* nlmsg, unsigned int offset,
  360. unsigned int total_len)
  361. {
  362. struct nlmsghdr* hdr = (struct nlmsghdr*)(nlmsg->buf + offset);
  363. if (offset == total_len || offset + hdr->nlmsg_len > total_len)
  364. return -1;
  365. return hdr->nlmsg_len;
  366. }
  367.  
  368. static void netlink_add_device_impl(struct nlmsg* nlmsg, const char* type,
  369. const char* name, bool up)
  370. {
  371. struct ifinfomsg hdr;
  372. memset(&hdr, 0, sizeof(hdr));
  373. if (up)
  374. hdr.ifi_flags = hdr.ifi_change = IFF_UP;
  375. netlink_init(nlmsg, RTM_NEWLINK, NLM_F_EXCL | NLM_F_CREATE, &hdr,
  376. sizeof(hdr));
  377. if (name)
  378. netlink_attr(nlmsg, IFLA_IFNAME, name, strlen(name));
  379. netlink_nest(nlmsg, IFLA_LINKINFO);
  380. netlink_attr(nlmsg, IFLA_INFO_KIND, type, strlen(type));
  381. }
  382.  
  383. static void netlink_add_device(struct nlmsg* nlmsg, int sock, const char* type,
  384. const char* name)
  385. {
  386. netlink_add_device_impl(nlmsg, type, name, false);
  387. netlink_done(nlmsg);
  388. int err = netlink_send(nlmsg, sock);
  389. if (err < 0) {
  390. }
  391. }
  392.  
  393. static void netlink_add_veth(struct nlmsg* nlmsg, int sock, const char* name,
  394. const char* peer)
  395. {
  396. netlink_add_device_impl(nlmsg, "veth", name, false);
  397. netlink_nest(nlmsg, IFLA_INFO_DATA);
  398. netlink_nest(nlmsg, VETH_INFO_PEER);
  399. nlmsg->pos += sizeof(struct ifinfomsg);
  400. netlink_attr(nlmsg, IFLA_IFNAME, peer, strlen(peer));
  401. netlink_done(nlmsg);
  402. netlink_done(nlmsg);
  403. netlink_done(nlmsg);
  404. int err = netlink_send(nlmsg, sock);
  405. if (err < 0) {
  406. }
  407. }
  408.  
  409. static void netlink_add_xfrm(struct nlmsg* nlmsg, int sock, const char* name)
  410. {
  411. netlink_add_device_impl(nlmsg, "xfrm", name, true);
  412. netlink_nest(nlmsg, IFLA_INFO_DATA);
  413. int if_id = 1;
  414. netlink_attr(nlmsg, 2, &if_id, sizeof(if_id));
  415. netlink_done(nlmsg);
  416. netlink_done(nlmsg);
  417. int err = netlink_send(nlmsg, sock);
  418. if (err < 0) {
  419. }
  420. }
  421.  
  422. static void netlink_add_hsr(struct nlmsg* nlmsg, int sock, const char* name,
  423. const char* slave1, const char* slave2)
  424. {
  425. netlink_add_device_impl(nlmsg, "hsr", name, false);
  426. netlink_nest(nlmsg, IFLA_INFO_DATA);
  427. int ifindex1 = if_nametoindex(slave1);
  428. netlink_attr(nlmsg, IFLA_HSR_SLAVE1, &ifindex1, sizeof(ifindex1));
  429. int ifindex2 = if_nametoindex(slave2);
  430. netlink_attr(nlmsg, IFLA_HSR_SLAVE2, &ifindex2, sizeof(ifindex2));
  431. netlink_done(nlmsg);
  432. netlink_done(nlmsg);
  433. int err = netlink_send(nlmsg, sock);
  434. if (err < 0) {
  435. }
  436. }
  437.  
  438. static void netlink_add_linked(struct nlmsg* nlmsg, int sock, const char* type,
  439. const char* name, const char* link)
  440. {
  441. netlink_add_device_impl(nlmsg, type, name, false);
  442. netlink_done(nlmsg);
  443. int ifindex = if_nametoindex(link);
  444. netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
  445. int err = netlink_send(nlmsg, sock);
  446. if (err < 0) {
  447. }
  448. }
  449.  
  450. static void netlink_add_vlan(struct nlmsg* nlmsg, int sock, const char* name,
  451. const char* link, uint16_t id, uint16_t proto)
  452. {
  453. netlink_add_device_impl(nlmsg, "vlan", name, false);
  454. netlink_nest(nlmsg, IFLA_INFO_DATA);
  455. netlink_attr(nlmsg, IFLA_VLAN_ID, &id, sizeof(id));
  456. netlink_attr(nlmsg, IFLA_VLAN_PROTOCOL, &proto, sizeof(proto));
  457. netlink_done(nlmsg);
  458. netlink_done(nlmsg);
  459. int ifindex = if_nametoindex(link);
  460. netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
  461. int err = netlink_send(nlmsg, sock);
  462. if (err < 0) {
  463. }
  464. }
  465.  
  466. static void netlink_add_macvlan(struct nlmsg* nlmsg, int sock, const char* name,
  467. const char* link)
  468. {
  469. netlink_add_device_impl(nlmsg, "macvlan", name, false);
  470. netlink_nest(nlmsg, IFLA_INFO_DATA);
  471. uint32_t mode = MACVLAN_MODE_BRIDGE;
  472. netlink_attr(nlmsg, IFLA_MACVLAN_MODE, &mode, sizeof(mode));
  473. netlink_done(nlmsg);
  474. netlink_done(nlmsg);
  475. int ifindex = if_nametoindex(link);
  476. netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
  477. int err = netlink_send(nlmsg, sock);
  478. if (err < 0) {
  479. }
  480. }
  481.  
  482. static void netlink_add_geneve(struct nlmsg* nlmsg, int sock, const char* name,
  483. uint32_t vni, struct in_addr* addr4,
  484. struct in6_addr* addr6)
  485. {
  486. netlink_add_device_impl(nlmsg, "geneve", name, false);
  487. netlink_nest(nlmsg, IFLA_INFO_DATA);
  488. netlink_attr(nlmsg, IFLA_GENEVE_ID, &vni, sizeof(vni));
  489. if (addr4)
  490. netlink_attr(nlmsg, IFLA_GENEVE_REMOTE, addr4, sizeof(*addr4));
  491. if (addr6)
  492. netlink_attr(nlmsg, IFLA_GENEVE_REMOTE6, addr6, sizeof(*addr6));
  493. netlink_done(nlmsg);
  494. netlink_done(nlmsg);
  495. int err = netlink_send(nlmsg, sock);
  496. if (err < 0) {
  497. }
  498. }
  499.  
  500. #define IFLA_IPVLAN_FLAGS 2
  501. #define IPVLAN_MODE_L3S 2
  502. #undef IPVLAN_F_VEPA
  503. #define IPVLAN_F_VEPA 2
  504.  
  505. static void netlink_add_ipvlan(struct nlmsg* nlmsg, int sock, const char* name,
  506. const char* link, uint16_t mode, uint16_t flags)
  507. {
  508. netlink_add_device_impl(nlmsg, "ipvlan", name, false);
  509. netlink_nest(nlmsg, IFLA_INFO_DATA);
  510. netlink_attr(nlmsg, IFLA_IPVLAN_MODE, &mode, sizeof(mode));
  511. netlink_attr(nlmsg, IFLA_IPVLAN_FLAGS, &flags, sizeof(flags));
  512. netlink_done(nlmsg);
  513. netlink_done(nlmsg);
  514. int ifindex = if_nametoindex(link);
  515. netlink_attr(nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex));
  516. int err = netlink_send(nlmsg, sock);
  517. if (err < 0) {
  518. }
  519. }
  520.  
  521. static void netlink_device_change(struct nlmsg* nlmsg, int sock,
  522. const char* name, bool up, const char* master,
  523. const void* mac, int macsize,
  524. const char* new_name)
  525. {
  526. struct ifinfomsg hdr;
  527. memset(&hdr, 0, sizeof(hdr));
  528. if (up)
  529. hdr.ifi_flags = hdr.ifi_change = IFF_UP;
  530. hdr.ifi_index = if_nametoindex(name);
  531. netlink_init(nlmsg, RTM_NEWLINK, 0, &hdr, sizeof(hdr));
  532. if (new_name)
  533. netlink_attr(nlmsg, IFLA_IFNAME, new_name, strlen(new_name));
  534. if (master) {
  535. int ifindex = if_nametoindex(master);
  536. netlink_attr(nlmsg, IFLA_MASTER, &ifindex, sizeof(ifindex));
  537. }
  538. if (macsize)
  539. netlink_attr(nlmsg, IFLA_ADDRESS, mac, macsize);
  540. int err = netlink_send(nlmsg, sock);
  541. if (err < 0) {
  542. }
  543. }
  544.  
  545. static int netlink_add_addr(struct nlmsg* nlmsg, int sock, const char* dev,
  546. const void* addr, int addrsize)
  547. {
  548. struct ifaddrmsg hdr;
  549. memset(&hdr, 0, sizeof(hdr));
  550. hdr.ifa_family = addrsize == 4 ? AF_INET : AF_INET6;
  551. hdr.ifa_prefixlen = addrsize == 4 ? 24 : 120;
  552. hdr.ifa_scope = RT_SCOPE_UNIVERSE;
  553. hdr.ifa_index = if_nametoindex(dev);
  554. netlink_init(nlmsg, RTM_NEWADDR, NLM_F_CREATE | NLM_F_REPLACE, &hdr,
  555. sizeof(hdr));
  556. netlink_attr(nlmsg, IFA_LOCAL, addr, addrsize);
  557. netlink_attr(nlmsg, IFA_ADDRESS, addr, addrsize);
  558. return netlink_send(nlmsg, sock);
  559. }
  560.  
  561. static void netlink_add_addr4(struct nlmsg* nlmsg, int sock, const char* dev,
  562. const char* addr)
  563. {
  564. struct in_addr in_addr;
  565. inet_pton(AF_INET, addr, &in_addr);
  566. int err = netlink_add_addr(nlmsg, sock, dev, &in_addr, sizeof(in_addr));
  567. if (err < 0) {
  568. }
  569. }
  570.  
  571. static void netlink_add_addr6(struct nlmsg* nlmsg, int sock, const char* dev,
  572. const char* addr)
  573. {
  574. struct in6_addr in6_addr;
  575. inet_pton(AF_INET6, addr, &in6_addr);
  576. int err = netlink_add_addr(nlmsg, sock, dev, &in6_addr, sizeof(in6_addr));
  577. if (err < 0) {
  578. }
  579. }
  580.  
  581. static struct nlmsg nlmsg;
  582.  
  583. #define DEVLINK_FAMILY_NAME "devlink"
  584.  
  585. #define DEVLINK_CMD_PORT_GET 5
  586. #define DEVLINK_ATTR_BUS_NAME 1
  587. #define DEVLINK_ATTR_DEV_NAME 2
  588. #define DEVLINK_ATTR_NETDEV_NAME 7
  589.  
  590. static struct nlmsg nlmsg2;
  591.  
  592. static void initialize_devlink_ports(const char* bus_name, const char* dev_name,
  593. const char* netdev_prefix)
  594. {
  595. struct genlmsghdr genlhdr;
  596. int len, total_len, id, err, offset;
  597. uint16_t netdev_index;
  598. int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
  599. if (sock == -1)
  600. exit(1);
  601. int rtsock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  602. if (rtsock == -1)
  603. exit(1);
  604. id = netlink_query_family_id(&nlmsg, sock, DEVLINK_FAMILY_NAME, true);
  605. if (id == -1)
  606. goto error;
  607. memset(&genlhdr, 0, sizeof(genlhdr));
  608. genlhdr.cmd = DEVLINK_CMD_PORT_GET;
  609. netlink_init(&nlmsg, id, NLM_F_DUMP, &genlhdr, sizeof(genlhdr));
  610. netlink_attr(&nlmsg, DEVLINK_ATTR_BUS_NAME, bus_name, strlen(bus_name) + 1);
  611. netlink_attr(&nlmsg, DEVLINK_ATTR_DEV_NAME, dev_name, strlen(dev_name) + 1);
  612. err = netlink_send_ext(&nlmsg, sock, id, &total_len, true);
  613. if (err < 0) {
  614. goto error;
  615. }
  616. offset = 0;
  617. netdev_index = 0;
  618. while ((len = netlink_next_msg(&nlmsg, offset, total_len)) != -1) {
  619. struct nlattr* attr = (struct nlattr*)(nlmsg.buf + offset + NLMSG_HDRLEN +
  620. NLMSG_ALIGN(sizeof(genlhdr)));
  621. for (; (char*)attr < nlmsg.buf + offset + len;
  622. attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) {
  623. if (attr->nla_type == DEVLINK_ATTR_NETDEV_NAME) {
  624. char* port_name;
  625. char netdev_name[IFNAMSIZ];
  626. port_name = (char*)(attr + 1);
  627. snprintf(netdev_name, sizeof(netdev_name), "%s%d", netdev_prefix,
  628. netdev_index);
  629. netlink_device_change(&nlmsg2, rtsock, port_name, true, 0, 0, 0,
  630. netdev_name);
  631. break;
  632. }
  633. }
  634. offset += len;
  635. netdev_index++;
  636. }
  637. error:
  638. close(rtsock);
  639. close(sock);
  640. }
  641.  
  642. #define DEV_IPV4 "172.20.20.%d"
  643. #define DEV_IPV6 "fe80::%02x"
  644. #define DEV_MAC 0x00aaaaaaaaaa
  645.  
  646. static void netdevsim_add(unsigned int addr, unsigned int port_count)
  647. {
  648. write_file("/sys/bus/netdevsim/del_device", "%u", addr);
  649. if (write_file("/sys/bus/netdevsim/new_device", "%u %u", addr, port_count)) {
  650. char buf[32];
  651. snprintf(buf, sizeof(buf), "netdevsim%d", addr);
  652. initialize_devlink_ports("netdevsim", buf, "netdevsim");
  653. }
  654. }
  655.  
  656. #define WG_GENL_NAME "wireguard"
  657. enum wg_cmd {
  658. WG_CMD_GET_DEVICE,
  659. WG_CMD_SET_DEVICE,
  660. };
  661. enum wgdevice_attribute {
  662. WGDEVICE_A_UNSPEC,
  663. WGDEVICE_A_IFINDEX,
  664. WGDEVICE_A_IFNAME,
  665. WGDEVICE_A_PRIVATE_KEY,
  666. WGDEVICE_A_PUBLIC_KEY,
  667. WGDEVICE_A_FLAGS,
  668. WGDEVICE_A_LISTEN_PORT,
  669. WGDEVICE_A_FWMARK,
  670. WGDEVICE_A_PEERS,
  671. };
  672. enum wgpeer_attribute {
  673. WGPEER_A_UNSPEC,
  674. WGPEER_A_PUBLIC_KEY,
  675. WGPEER_A_PRESHARED_KEY,
  676. WGPEER_A_FLAGS,
  677. WGPEER_A_ENDPOINT,
  678. WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
  679. WGPEER_A_LAST_HANDSHAKE_TIME,
  680. WGPEER_A_RX_BYTES,
  681. WGPEER_A_TX_BYTES,
  682. WGPEER_A_ALLOWEDIPS,
  683. WGPEER_A_PROTOCOL_VERSION,
  684. };
  685. enum wgallowedip_attribute {
  686. WGALLOWEDIP_A_UNSPEC,
  687. WGALLOWEDIP_A_FAMILY,
  688. WGALLOWEDIP_A_IPADDR,
  689. WGALLOWEDIP_A_CIDR_MASK,
  690. };
  691.  
  692. static void netlink_wireguard_setup(void)
  693. {
  694. const char ifname_a[] = "wg0";
  695. const char ifname_b[] = "wg1";
  696. const char ifname_c[] = "wg2";
  697. const char private_a[] =
  698. "\xa0\x5c\xa8\x4f\x6c\x9c\x8e\x38\x53\xe2\xfd\x7a\x70\xae\x0f\xb2\x0f\xa1"
  699. "\x52\x60\x0c\xb0\x08\x45\x17\x4f\x08\x07\x6f\x8d\x78\x43";
  700. const char private_b[] =
  701. "\xb0\x80\x73\xe8\xd4\x4e\x91\xe3\xda\x92\x2c\x22\x43\x82\x44\xbb\x88\x5c"
  702. "\x69\xe2\x69\xc8\xe9\xd8\x35\xb1\x14\x29\x3a\x4d\xdc\x6e";
  703. const char private_c[] =
  704. "\xa0\xcb\x87\x9a\x47\xf5\xbc\x64\x4c\x0e\x69\x3f\xa6\xd0\x31\xc7\x4a\x15"
  705. "\x53\xb6\xe9\x01\xb9\xff\x2f\x51\x8c\x78\x04\x2f\xb5\x42";
  706. const char public_a[] =
  707. "\x97\x5c\x9d\x81\xc9\x83\xc8\x20\x9e\xe7\x81\x25\x4b\x89\x9f\x8e\xd9\x25"
  708. "\xae\x9f\x09\x23\xc2\x3c\x62\xf5\x3c\x57\xcd\xbf\x69\x1c";
  709. const char public_b[] =
  710. "\xd1\x73\x28\x99\xf6\x11\xcd\x89\x94\x03\x4d\x7f\x41\x3d\xc9\x57\x63\x0e"
  711. "\x54\x93\xc2\x85\xac\xa4\x00\x65\xcb\x63\x11\xbe\x69\x6b";
  712. const char public_c[] =
  713. "\xf4\x4d\xa3\x67\xa8\x8e\xe6\x56\x4f\x02\x02\x11\x45\x67\x27\x08\x2f\x5c"
  714. "\xeb\xee\x8b\x1b\xf5\xeb\x73\x37\x34\x1b\x45\x9b\x39\x22";
  715. const uint16_t listen_a = 20001;
  716. const uint16_t listen_b = 20002;
  717. const uint16_t listen_c = 20003;
  718. const uint16_t af_inet = AF_INET;
  719. const uint16_t af_inet6 = AF_INET6;
  720. const struct sockaddr_in endpoint_b_v4 = {
  721. .sin_family = AF_INET,
  722. .sin_port = htons(listen_b),
  723. .sin_addr = {htonl(INADDR_LOOPBACK)}};
  724. const struct sockaddr_in endpoint_c_v4 = {
  725. .sin_family = AF_INET,
  726. .sin_port = htons(listen_c),
  727. .sin_addr = {htonl(INADDR_LOOPBACK)}};
  728. struct sockaddr_in6 endpoint_a_v6 = {.sin6_family = AF_INET6,
  729. .sin6_port = htons(listen_a)};
  730. endpoint_a_v6.sin6_addr = in6addr_loopback;
  731. struct sockaddr_in6 endpoint_c_v6 = {.sin6_family = AF_INET6,
  732. .sin6_port = htons(listen_c)};
  733. endpoint_c_v6.sin6_addr = in6addr_loopback;
  734. const struct in_addr first_half_v4 = {0};
  735. const struct in_addr second_half_v4 = {(uint32_t)htonl(128 << 24)};
  736. const struct in6_addr first_half_v6 = {{{0}}};
  737. const struct in6_addr second_half_v6 = {{{0x80}}};
  738. const uint8_t half_cidr = 1;
  739. const uint16_t persistent_keepalives[] = {1, 3, 7, 9, 14, 19};
  740. struct genlmsghdr genlhdr = {.cmd = WG_CMD_SET_DEVICE, .version = 1};
  741. int sock;
  742. int id, err;
  743. sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC);
  744. if (sock == -1) {
  745. return;
  746. }
  747. id = netlink_query_family_id(&nlmsg, sock, WG_GENL_NAME, true);
  748. if (id == -1)
  749. goto error;
  750. netlink_init(&nlmsg, id, 0, &genlhdr, sizeof(genlhdr));
  751. netlink_attr(&nlmsg, WGDEVICE_A_IFNAME, ifname_a, strlen(ifname_a) + 1);
  752. netlink_attr(&nlmsg, WGDEVICE_A_PRIVATE_KEY, private_a, 32);
  753. netlink_attr(&nlmsg, WGDEVICE_A_LISTEN_PORT, &listen_a, 2);
  754. netlink_nest(&nlmsg, NLA_F_NESTED | WGDEVICE_A_PEERS);
  755. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  756. netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_b, 32);
  757. netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_b_v4,
  758. sizeof(endpoint_b_v4));
  759. netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
  760. &persistent_keepalives[0], 2);
  761. netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
  762. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  763. netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
  764. netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v4,
  765. sizeof(first_half_v4));
  766. netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  767. netlink_done(&nlmsg);
  768. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  769. netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
  770. netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v6,
  771. sizeof(first_half_v6));
  772. netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  773. netlink_done(&nlmsg);
  774. netlink_done(&nlmsg);
  775. netlink_done(&nlmsg);
  776. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  777. netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_c, 32);
  778. netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_c_v6,
  779. sizeof(endpoint_c_v6));
  780. netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
  781. &persistent_keepalives[1], 2);
  782. netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
  783. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  784. netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
  785. netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v4,
  786. sizeof(second_half_v4));
  787. netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  788. netlink_done(&nlmsg);
  789. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  790. netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
  791. netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v6,
  792. sizeof(second_half_v6));
  793. netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  794. netlink_done(&nlmsg);
  795. netlink_done(&nlmsg);
  796. netlink_done(&nlmsg);
  797. netlink_done(&nlmsg);
  798. err = netlink_send(&nlmsg, sock);
  799. if (err < 0) {
  800. }
  801. netlink_init(&nlmsg, id, 0, &genlhdr, sizeof(genlhdr));
  802. netlink_attr(&nlmsg, WGDEVICE_A_IFNAME, ifname_b, strlen(ifname_b) + 1);
  803. netlink_attr(&nlmsg, WGDEVICE_A_PRIVATE_KEY, private_b, 32);
  804. netlink_attr(&nlmsg, WGDEVICE_A_LISTEN_PORT, &listen_b, 2);
  805. netlink_nest(&nlmsg, NLA_F_NESTED | WGDEVICE_A_PEERS);
  806. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  807. netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_a, 32);
  808. netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_a_v6,
  809. sizeof(endpoint_a_v6));
  810. netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
  811. &persistent_keepalives[2], 2);
  812. netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
  813. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  814. netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
  815. netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v4,
  816. sizeof(first_half_v4));
  817. netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  818. netlink_done(&nlmsg);
  819. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  820. netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
  821. netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v6,
  822. sizeof(first_half_v6));
  823. netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  824. netlink_done(&nlmsg);
  825. netlink_done(&nlmsg);
  826. netlink_done(&nlmsg);
  827. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  828. netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_c, 32);
  829. netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_c_v4,
  830. sizeof(endpoint_c_v4));
  831. netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
  832. &persistent_keepalives[3], 2);
  833. netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
  834. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  835. netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
  836. netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v4,
  837. sizeof(second_half_v4));
  838. netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  839. netlink_done(&nlmsg);
  840. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  841. netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
  842. netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v6,
  843. sizeof(second_half_v6));
  844. netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  845. netlink_done(&nlmsg);
  846. netlink_done(&nlmsg);
  847. netlink_done(&nlmsg);
  848. netlink_done(&nlmsg);
  849. err = netlink_send(&nlmsg, sock);
  850. if (err < 0) {
  851. }
  852. netlink_init(&nlmsg, id, 0, &genlhdr, sizeof(genlhdr));
  853. netlink_attr(&nlmsg, WGDEVICE_A_IFNAME, ifname_c, strlen(ifname_c) + 1);
  854. netlink_attr(&nlmsg, WGDEVICE_A_PRIVATE_KEY, private_c, 32);
  855. netlink_attr(&nlmsg, WGDEVICE_A_LISTEN_PORT, &listen_c, 2);
  856. netlink_nest(&nlmsg, NLA_F_NESTED | WGDEVICE_A_PEERS);
  857. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  858. netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_a, 32);
  859. netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_a_v6,
  860. sizeof(endpoint_a_v6));
  861. netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
  862. &persistent_keepalives[4], 2);
  863. netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
  864. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  865. netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
  866. netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v4,
  867. sizeof(first_half_v4));
  868. netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  869. netlink_done(&nlmsg);
  870. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  871. netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
  872. netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &first_half_v6,
  873. sizeof(first_half_v6));
  874. netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  875. netlink_done(&nlmsg);
  876. netlink_done(&nlmsg);
  877. netlink_done(&nlmsg);
  878. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  879. netlink_attr(&nlmsg, WGPEER_A_PUBLIC_KEY, public_b, 32);
  880. netlink_attr(&nlmsg, WGPEER_A_ENDPOINT, &endpoint_b_v4,
  881. sizeof(endpoint_b_v4));
  882. netlink_attr(&nlmsg, WGPEER_A_PERSISTENT_KEEPALIVE_INTERVAL,
  883. &persistent_keepalives[5], 2);
  884. netlink_nest(&nlmsg, NLA_F_NESTED | WGPEER_A_ALLOWEDIPS);
  885. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  886. netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet, 2);
  887. netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v4,
  888. sizeof(second_half_v4));
  889. netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  890. netlink_done(&nlmsg);
  891. netlink_nest(&nlmsg, NLA_F_NESTED | 0);
  892. netlink_attr(&nlmsg, WGALLOWEDIP_A_FAMILY, &af_inet6, 2);
  893. netlink_attr(&nlmsg, WGALLOWEDIP_A_IPADDR, &second_half_v6,
  894. sizeof(second_half_v6));
  895. netlink_attr(&nlmsg, WGALLOWEDIP_A_CIDR_MASK, &half_cidr, 1);
  896. netlink_done(&nlmsg);
  897. netlink_done(&nlmsg);
  898. netlink_done(&nlmsg);
  899. netlink_done(&nlmsg);
  900. err = netlink_send(&nlmsg, sock);
  901. if (err < 0) {
  902. }
  903.  
  904. error:
  905. close(sock);
  906. }
  907.  
  908. static void initialize_netdevices(void)
  909. {
  910. char netdevsim[16];
  911. sprintf(netdevsim, "netdevsim%d", (int)procid);
  912. struct {
  913. const char* type;
  914. const char* dev;
  915. } devtypes[] = {
  916. {"ip6gretap", "ip6gretap0"}, {"bridge", "bridge0"}, {"vcan", "vcan0"},
  917. {"bond", "bond0"}, {"team", "team0"}, {"dummy", "dummy0"},
  918. {"nlmon", "nlmon0"}, {"caif", "caif0"}, {"batadv", "batadv0"},
  919. {"vxcan", "vxcan1"}, {"veth", 0}, {"wireguard", "wg0"},
  920. {"wireguard", "wg1"}, {"wireguard", "wg2"},
  921. };
  922. const char* devmasters[] = {"bridge", "bond", "team", "batadv"};
  923. struct {
  924. const char* name;
  925. int macsize;
  926. bool noipv6;
  927. } devices[] = {
  928. {"lo", ETH_ALEN},
  929. {"sit0", 0},
  930. {"bridge0", ETH_ALEN},
  931. {"vcan0", 0, true},
  932. {"tunl0", 0},
  933. {"gre0", 0},
  934. {"gretap0", ETH_ALEN},
  935. {"ip_vti0", 0},
  936. {"ip6_vti0", 0},
  937. {"ip6tnl0", 0},
  938. {"ip6gre0", 0},
  939. {"ip6gretap0", ETH_ALEN},
  940. {"erspan0", ETH_ALEN},
  941. {"bond0", ETH_ALEN},
  942. {"veth0", ETH_ALEN},
  943. {"veth1", ETH_ALEN},
  944. {"team0", ETH_ALEN},
  945. {"veth0_to_bridge", ETH_ALEN},
  946. {"veth1_to_bridge", ETH_ALEN},
  947. {"veth0_to_bond", ETH_ALEN},
  948. {"veth1_to_bond", ETH_ALEN},
  949. {"veth0_to_team", ETH_ALEN},
  950. {"veth1_to_team", ETH_ALEN},
  951. {"veth0_to_hsr", ETH_ALEN},
  952. {"veth1_to_hsr", ETH_ALEN},
  953. {"hsr0", 0},
  954. {"dummy0", ETH_ALEN},
  955. {"nlmon0", 0},
  956. {"vxcan0", 0, true},
  957. {"vxcan1", 0, true},
  958. {"caif0", ETH_ALEN},
  959. {"batadv0", ETH_ALEN},
  960. {netdevsim, ETH_ALEN},
  961. {"xfrm0", ETH_ALEN},
  962. {"veth0_virt_wifi", ETH_ALEN},
  963. {"veth1_virt_wifi", ETH_ALEN},
  964. {"virt_wifi0", ETH_ALEN},
  965. {"veth0_vlan", ETH_ALEN},
  966. {"veth1_vlan", ETH_ALEN},
  967. {"vlan0", ETH_ALEN},
  968. {"vlan1", ETH_ALEN},
  969. {"macvlan0", ETH_ALEN},
  970. {"macvlan1", ETH_ALEN},
  971. {"ipvlan0", ETH_ALEN},
  972. {"ipvlan1", ETH_ALEN},
  973. {"veth0_macvtap", ETH_ALEN},
  974. {"veth1_macvtap", ETH_ALEN},
  975. {"macvtap0", ETH_ALEN},
  976. {"macsec0", ETH_ALEN},
  977. {"veth0_to_batadv", ETH_ALEN},
  978. {"veth1_to_batadv", ETH_ALEN},
  979. {"batadv_slave_0", ETH_ALEN},
  980. {"batadv_slave_1", ETH_ALEN},
  981. {"geneve0", ETH_ALEN},
  982. {"geneve1", ETH_ALEN},
  983. {"wg0", 0},
  984. {"wg1", 0},
  985. {"wg2", 0},
  986. };
  987. int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  988. if (sock == -1)
  989. exit(1);
  990. unsigned i;
  991. for (i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]); i++)
  992. netlink_add_device(&nlmsg, sock, devtypes[i].type, devtypes[i].dev);
  993. for (i = 0; i < sizeof(devmasters) / (sizeof(devmasters[0])); i++) {
  994. char master[32], slave0[32], veth0[32], slave1[32], veth1[32];
  995. sprintf(slave0, "%s_slave_0", devmasters[i]);
  996. sprintf(veth0, "veth0_to_%s", devmasters[i]);
  997. netlink_add_veth(&nlmsg, sock, slave0, veth0);
  998. sprintf(slave1, "%s_slave_1", devmasters[i]);
  999. sprintf(veth1, "veth1_to_%s", devmasters[i]);
  1000. netlink_add_veth(&nlmsg, sock, slave1, veth1);
  1001. sprintf(master, "%s0", devmasters[i]);
  1002. netlink_device_change(&nlmsg, sock, slave0, false, master, 0, 0, NULL);
  1003. netlink_device_change(&nlmsg, sock, slave1, false, master, 0, 0, NULL);
  1004. }
  1005. netlink_add_xfrm(&nlmsg, sock, "xfrm0");
  1006. netlink_device_change(&nlmsg, sock, "bridge_slave_0", true, 0, 0, 0, NULL);
  1007. netlink_device_change(&nlmsg, sock, "bridge_slave_1", true, 0, 0, 0, NULL);
  1008. netlink_add_veth(&nlmsg, sock, "hsr_slave_0", "veth0_to_hsr");
  1009. netlink_add_veth(&nlmsg, sock, "hsr_slave_1", "veth1_to_hsr");
  1010. netlink_add_hsr(&nlmsg, sock, "hsr0", "hsr_slave_0", "hsr_slave_1");
  1011. netlink_device_change(&nlmsg, sock, "hsr_slave_0", true, 0, 0, 0, NULL);
  1012. netlink_device_change(&nlmsg, sock, "hsr_slave_1", true, 0, 0, 0, NULL);
  1013. netlink_add_veth(&nlmsg, sock, "veth0_virt_wifi", "veth1_virt_wifi");
  1014. netlink_add_linked(&nlmsg, sock, "virt_wifi", "virt_wifi0",
  1015. "veth1_virt_wifi");
  1016. netlink_add_veth(&nlmsg, sock, "veth0_vlan", "veth1_vlan");
  1017. netlink_add_vlan(&nlmsg, sock, "vlan0", "veth0_vlan", 0, htons(ETH_P_8021Q));
  1018. netlink_add_vlan(&nlmsg, sock, "vlan1", "veth0_vlan", 1, htons(ETH_P_8021AD));
  1019. netlink_add_macvlan(&nlmsg, sock, "macvlan0", "veth1_vlan");
  1020. netlink_add_macvlan(&nlmsg, sock, "macvlan1", "veth1_vlan");
  1021. netlink_add_ipvlan(&nlmsg, sock, "ipvlan0", "veth0_vlan", IPVLAN_MODE_L2, 0);
  1022. netlink_add_ipvlan(&nlmsg, sock, "ipvlan1", "veth0_vlan", IPVLAN_MODE_L3S,
  1023. IPVLAN_F_VEPA);
  1024. netlink_add_veth(&nlmsg, sock, "veth0_macvtap", "veth1_macvtap");
  1025. netlink_add_linked(&nlmsg, sock, "macvtap", "macvtap0", "veth0_macvtap");
  1026. netlink_add_linked(&nlmsg, sock, "macsec", "macsec0", "veth1_macvtap");
  1027. char addr[32];
  1028. sprintf(addr, DEV_IPV4, 14 + 10);
  1029. struct in_addr geneve_addr4;
  1030. if (inet_pton(AF_INET, addr, &geneve_addr4) <= 0)
  1031. exit(1);
  1032. struct in6_addr geneve_addr6;
  1033. if (inet_pton(AF_INET6, "fc00::01", &geneve_addr6) <= 0)
  1034. exit(1);
  1035. netlink_add_geneve(&nlmsg, sock, "geneve0", 0, &geneve_addr4, 0);
  1036. netlink_add_geneve(&nlmsg, sock, "geneve1", 1, 0, &geneve_addr6);
  1037. netdevsim_add((int)procid, 4);
  1038. netlink_wireguard_setup();
  1039. for (i = 0; i < sizeof(devices) / (sizeof(devices[0])); i++) {
  1040. char addr[32];
  1041. sprintf(addr, DEV_IPV4, i + 10);
  1042. netlink_add_addr4(&nlmsg, sock, devices[i].name, addr);
  1043. if (!devices[i].noipv6) {
  1044. sprintf(addr, DEV_IPV6, i + 10);
  1045. netlink_add_addr6(&nlmsg, sock, devices[i].name, addr);
  1046. }
  1047. uint64_t macaddr = DEV_MAC + ((i + 10ull) << 40);
  1048. netlink_device_change(&nlmsg, sock, devices[i].name, true, 0, &macaddr,
  1049. devices[i].macsize, NULL);
  1050. }
  1051. close(sock);
  1052. }
  1053. static void initialize_netdevices_init(void)
  1054. {
  1055. int sock = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE);
  1056. if (sock == -1)
  1057. exit(1);
  1058. struct {
  1059. const char* type;
  1060. int macsize;
  1061. bool noipv6;
  1062. bool noup;
  1063. } devtypes[] = {
  1064. {"nr", 7, true},
  1065. {"rose", 5, true, true},
  1066. };
  1067. unsigned i;
  1068. for (i = 0; i < sizeof(devtypes) / sizeof(devtypes[0]); i++) {
  1069. char dev[32], addr[32];
  1070. sprintf(dev, "%s%d", devtypes[i].type, (int)procid);
  1071. sprintf(addr, "172.30.%d.%d", i, (int)procid + 1);
  1072. netlink_add_addr4(&nlmsg, sock, dev, addr);
  1073. if (!devtypes[i].noipv6) {
  1074. sprintf(addr, "fe88::%02x:%02x", i, (int)procid + 1);
  1075. netlink_add_addr6(&nlmsg, sock, dev, addr);
  1076. }
  1077. int macsize = devtypes[i].macsize;
  1078. uint64_t macaddr = 0xbbbbbb +
  1079. ((unsigned long long)i << (8 * (macsize - 2))) +
  1080. (procid << (8 * (macsize - 1)));
  1081. netlink_device_change(&nlmsg, sock, dev, !devtypes[i].noup, 0, &macaddr,
  1082. macsize, NULL);
  1083. }
  1084. close(sock);
  1085. }
  1086.  
  1087. #define MAX_FDS 30
  1088.  
  1089. #define XT_TABLE_SIZE 1536
  1090. #define XT_MAX_ENTRIES 10
  1091.  
  1092. struct xt_counters {
  1093. uint64_t pcnt, bcnt;
  1094. };
  1095.  
  1096. struct ipt_getinfo {
  1097. char name[32];
  1098. unsigned int valid_hooks;
  1099. unsigned int hook_entry[5];
  1100. unsigned int underflow[5];
  1101. unsigned int num_entries;
  1102. unsigned int size;
  1103. };
  1104.  
  1105. struct ipt_get_entries {
  1106. char name[32];
  1107. unsigned int size;
  1108. uint64_t entrytable[XT_TABLE_SIZE / sizeof(uint64_t)];
  1109. };
  1110.  
  1111. struct ipt_replace {
  1112. char name[32];
  1113. unsigned int valid_hooks;
  1114. unsigned int num_entries;
  1115. unsigned int size;
  1116. unsigned int hook_entry[5];
  1117. unsigned int underflow[5];
  1118. unsigned int num_counters;
  1119. struct xt_counters* counters;
  1120. uint64_t entrytable[XT_TABLE_SIZE / sizeof(uint64_t)];
  1121. };
  1122.  
  1123. struct ipt_table_desc {
  1124. const char* name;
  1125. struct ipt_getinfo info;
  1126. struct ipt_replace replace;
  1127. };
  1128.  
  1129. static struct ipt_table_desc ipv4_tables[] = {
  1130. {.name = "filter"}, {.name = "nat"}, {.name = "mangle"},
  1131. {.name = "raw"}, {.name = "security"},
  1132. };
  1133.  
  1134. static struct ipt_table_desc ipv6_tables[] = {
  1135. {.name = "filter"}, {.name = "nat"}, {.name = "mangle"},
  1136. {.name = "raw"}, {.name = "security"},
  1137. };
  1138.  
  1139. #define IPT_BASE_CTL 64
  1140. #define IPT_SO_SET_REPLACE (IPT_BASE_CTL)
  1141. #define IPT_SO_GET_INFO (IPT_BASE_CTL)
  1142. #define IPT_SO_GET_ENTRIES (IPT_BASE_CTL + 1)
  1143.  
  1144. struct arpt_getinfo {
  1145. char name[32];
  1146. unsigned int valid_hooks;
  1147. unsigned int hook_entry[3];
  1148. unsigned int underflow[3];
  1149. unsigned int num_entries;
  1150. unsigned int size;
  1151. };
  1152.  
  1153. struct arpt_get_entries {
  1154. char name[32];
  1155. unsigned int size;
  1156. uint64_t entrytable[XT_TABLE_SIZE / sizeof(uint64_t)];
  1157. };
  1158.  
  1159. struct arpt_replace {
  1160. char name[32];
  1161. unsigned int valid_hooks;
  1162. unsigned int num_entries;
  1163. unsigned int size;
  1164. unsigned int hook_entry[3];
  1165. unsigned int underflow[3];
  1166. unsigned int num_counters;
  1167. struct xt_counters* counters;
  1168. uint64_t entrytable[XT_TABLE_SIZE / sizeof(uint64_t)];
  1169. };
  1170.  
  1171. struct arpt_table_desc {
  1172. const char* name;
  1173. struct arpt_getinfo info;
  1174. struct arpt_replace replace;
  1175. };
  1176.  
  1177. static struct arpt_table_desc arpt_tables[] = {
  1178. {.name = "filter"},
  1179. };
  1180.  
  1181. #define ARPT_BASE_CTL 96
  1182. #define ARPT_SO_SET_REPLACE (ARPT_BASE_CTL)
  1183. #define ARPT_SO_GET_INFO (ARPT_BASE_CTL)
  1184. #define ARPT_SO_GET_ENTRIES (ARPT_BASE_CTL + 1)
  1185.  
  1186. static void checkpoint_iptables(struct ipt_table_desc* tables, int num_tables,
  1187. int family, int level)
  1188. {
  1189. int fd = socket(family, SOCK_STREAM, IPPROTO_TCP);
  1190. if (fd == -1) {
  1191. switch (errno) {
  1192. case EAFNOSUPPORT:
  1193. case ENOPROTOOPT:
  1194. case ENOENT:
  1195. return;
  1196. }
  1197. exit(1);
  1198. }
  1199. for (int i = 0; i < num_tables; i++) {
  1200. struct ipt_table_desc* table = &tables[i];
  1201. strcpy(table->info.name, table->name);
  1202. strcpy(table->replace.name, table->name);
  1203. socklen_t optlen = sizeof(table->info);
  1204. if (getsockopt(fd, level, IPT_SO_GET_INFO, &table->info, &optlen)) {
  1205. switch (errno) {
  1206. case EPERM:
  1207. case ENOENT:
  1208. case ENOPROTOOPT:
  1209. continue;
  1210. }
  1211. exit(1);
  1212. }
  1213. if (table->info.size > sizeof(table->replace.entrytable))
  1214. exit(1);
  1215. if (table->info.num_entries > XT_MAX_ENTRIES)
  1216. exit(1);
  1217. struct ipt_get_entries entries;
  1218. memset(&entries, 0, sizeof(entries));
  1219. strcpy(entries.name, table->name);
  1220. entries.size = table->info.size;
  1221. optlen = sizeof(entries) - sizeof(entries.entrytable) + table->info.size;
  1222. if (getsockopt(fd, level, IPT_SO_GET_ENTRIES, &entries, &optlen))
  1223. exit(1);
  1224. table->replace.valid_hooks = table->info.valid_hooks;
  1225. table->replace.num_entries = table->info.num_entries;
  1226. table->replace.size = table->info.size;
  1227. memcpy(table->replace.hook_entry, table->info.hook_entry,
  1228. sizeof(table->replace.hook_entry));
  1229. memcpy(table->replace.underflow, table->info.underflow,
  1230. sizeof(table->replace.underflow));
  1231. memcpy(table->replace.entrytable, entries.entrytable, table->info.size);
  1232. }
  1233. close(fd);
  1234. }
  1235.  
  1236. static void reset_iptables(struct ipt_table_desc* tables, int num_tables,
  1237. int family, int level)
  1238. {
  1239. int fd = socket(family, SOCK_STREAM, IPPROTO_TCP);
  1240. if (fd == -1) {
  1241. switch (errno) {
  1242. case EAFNOSUPPORT:
  1243. case ENOPROTOOPT:
  1244. case ENOENT:
  1245. return;
  1246. }
  1247. exit(1);
  1248. }
  1249. for (int i = 0; i < num_tables; i++) {
  1250. struct ipt_table_desc* table = &tables[i];
  1251. if (table->info.valid_hooks == 0)
  1252. continue;
  1253. struct ipt_getinfo info;
  1254. memset(&info, 0, sizeof(info));
  1255. strcpy(info.name, table->name);
  1256. socklen_t optlen = sizeof(info);
  1257. if (getsockopt(fd, level, IPT_SO_GET_INFO, &info, &optlen))
  1258. exit(1);
  1259. if (memcmp(&table->info, &info, sizeof(table->info)) == 0) {
  1260. struct ipt_get_entries entries;
  1261. memset(&entries, 0, sizeof(entries));
  1262. strcpy(entries.name, table->name);
  1263. entries.size = table->info.size;
  1264. optlen = sizeof(entries) - sizeof(entries.entrytable) + entries.size;
  1265. if (getsockopt(fd, level, IPT_SO_GET_ENTRIES, &entries, &optlen))
  1266. exit(1);
  1267. if (memcmp(table->replace.entrytable, entries.entrytable,
  1268. table->info.size) == 0)
  1269. continue;
  1270. }
  1271. struct xt_counters counters[XT_MAX_ENTRIES];
  1272. table->replace.num_counters = info.num_entries;
  1273. table->replace.counters = counters;
  1274. optlen = sizeof(table->replace) - sizeof(table->replace.entrytable) +
  1275. table->replace.size;
  1276. if (setsockopt(fd, level, IPT_SO_SET_REPLACE, &table->replace, optlen))
  1277. exit(1);
  1278. }
  1279. close(fd);
  1280. }
  1281.  
  1282. static void checkpoint_arptables(void)
  1283. {
  1284. int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  1285. if (fd == -1) {
  1286. switch (errno) {
  1287. case EAFNOSUPPORT:
  1288. case ENOPROTOOPT:
  1289. case ENOENT:
  1290. return;
  1291. }
  1292. exit(1);
  1293. }
  1294. for (unsigned i = 0; i < sizeof(arpt_tables) / sizeof(arpt_tables[0]); i++) {
  1295. struct arpt_table_desc* table = &arpt_tables[i];
  1296. strcpy(table->info.name, table->name);
  1297. strcpy(table->replace.name, table->name);
  1298. socklen_t optlen = sizeof(table->info);
  1299. if (getsockopt(fd, SOL_IP, ARPT_SO_GET_INFO, &table->info, &optlen)) {
  1300. switch (errno) {
  1301. case EPERM:
  1302. case ENOENT:
  1303. case ENOPROTOOPT:
  1304. continue;
  1305. }
  1306. exit(1);
  1307. }
  1308. if (table->info.size > sizeof(table->replace.entrytable))
  1309. exit(1);
  1310. if (table->info.num_entries > XT_MAX_ENTRIES)
  1311. exit(1);
  1312. struct arpt_get_entries entries;
  1313. memset(&entries, 0, sizeof(entries));
  1314. strcpy(entries.name, table->name);
  1315. entries.size = table->info.size;
  1316. optlen = sizeof(entries) - sizeof(entries.entrytable) + table->info.size;
  1317. if (getsockopt(fd, SOL_IP, ARPT_SO_GET_ENTRIES, &entries, &optlen))
  1318. exit(1);
  1319. table->replace.valid_hooks = table->info.valid_hooks;
  1320. table->replace.num_entries = table->info.num_entries;
  1321. table->replace.size = table->info.size;
  1322. memcpy(table->replace.hook_entry, table->info.hook_entry,
  1323. sizeof(table->replace.hook_entry));
  1324. memcpy(table->replace.underflow, table->info.underflow,
  1325. sizeof(table->replace.underflow));
  1326. memcpy(table->replace.entrytable, entries.entrytable, table->info.size);
  1327. }
  1328. close(fd);
  1329. }
  1330.  
  1331. static void reset_arptables()
  1332. {
  1333. int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  1334. if (fd == -1) {
  1335. switch (errno) {
  1336. case EAFNOSUPPORT:
  1337. case ENOPROTOOPT:
  1338. case ENOENT:
  1339. return;
  1340. }
  1341. exit(1);
  1342. }
  1343. for (unsigned i = 0; i < sizeof(arpt_tables) / sizeof(arpt_tables[0]); i++) {
  1344. struct arpt_table_desc* table = &arpt_tables[i];
  1345. if (table->info.valid_hooks == 0)
  1346. continue;
  1347. struct arpt_getinfo info;
  1348. memset(&info, 0, sizeof(info));
  1349. strcpy(info.name, table->name);
  1350. socklen_t optlen = sizeof(info);
  1351. if (getsockopt(fd, SOL_IP, ARPT_SO_GET_INFO, &info, &optlen))
  1352. exit(1);
  1353. if (memcmp(&table->info, &info, sizeof(table->info)) == 0) {
  1354. struct arpt_get_entries entries;
  1355. memset(&entries, 0, sizeof(entries));
  1356. strcpy(entries.name, table->name);
  1357. entries.size = table->info.size;
  1358. optlen = sizeof(entries) - sizeof(entries.entrytable) + entries.size;
  1359. if (getsockopt(fd, SOL_IP, ARPT_SO_GET_ENTRIES, &entries, &optlen))
  1360. exit(1);
  1361. if (memcmp(table->replace.entrytable, entries.entrytable,
  1362. table->info.size) == 0)
  1363. continue;
  1364. } else {
  1365. }
  1366. struct xt_counters counters[XT_MAX_ENTRIES];
  1367. table->replace.num_counters = info.num_entries;
  1368. table->replace.counters = counters;
  1369. optlen = sizeof(table->replace) - sizeof(table->replace.entrytable) +
  1370. table->replace.size;
  1371. if (setsockopt(fd, SOL_IP, ARPT_SO_SET_REPLACE, &table->replace, optlen))
  1372. exit(1);
  1373. }
  1374. close(fd);
  1375. }
  1376.  
  1377. #define NF_BR_NUMHOOKS 6
  1378. #define EBT_TABLE_MAXNAMELEN 32
  1379. #define EBT_CHAIN_MAXNAMELEN 32
  1380. #define EBT_BASE_CTL 128
  1381. #define EBT_SO_SET_ENTRIES (EBT_BASE_CTL)
  1382. #define EBT_SO_GET_INFO (EBT_BASE_CTL)
  1383. #define EBT_SO_GET_ENTRIES (EBT_SO_GET_INFO + 1)
  1384. #define EBT_SO_GET_INIT_INFO (EBT_SO_GET_ENTRIES + 1)
  1385. #define EBT_SO_GET_INIT_ENTRIES (EBT_SO_GET_INIT_INFO + 1)
  1386.  
  1387. struct ebt_replace {
  1388. char name[EBT_TABLE_MAXNAMELEN];
  1389. unsigned int valid_hooks;
  1390. unsigned int nentries;
  1391. unsigned int entries_size;
  1392. struct ebt_entries* hook_entry[NF_BR_NUMHOOKS];
  1393. unsigned int num_counters;
  1394. struct ebt_counter* counters;
  1395. char* entries;
  1396. };
  1397.  
  1398. struct ebt_entries {
  1399. unsigned int distinguisher;
  1400. char name[EBT_CHAIN_MAXNAMELEN];
  1401. unsigned int counter_offset;
  1402. int policy;
  1403. unsigned int nentries;
  1404. char data[0] __attribute__((aligned(__alignof__(struct ebt_replace))));
  1405. };
  1406.  
  1407. struct ebt_table_desc {
  1408. const char* name;
  1409. struct ebt_replace replace;
  1410. char entrytable[XT_TABLE_SIZE];
  1411. };
  1412.  
  1413. static struct ebt_table_desc ebt_tables[] = {
  1414. {.name = "filter"},
  1415. {.name = "nat"},
  1416. {.name = "broute"},
  1417. };
  1418.  
  1419. static void checkpoint_ebtables(void)
  1420. {
  1421. int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  1422. if (fd == -1) {
  1423. switch (errno) {
  1424. case EAFNOSUPPORT:
  1425. case ENOPROTOOPT:
  1426. case ENOENT:
  1427. return;
  1428. }
  1429. exit(1);
  1430. }
  1431. for (size_t i = 0; i < sizeof(ebt_tables) / sizeof(ebt_tables[0]); i++) {
  1432. struct ebt_table_desc* table = &ebt_tables[i];
  1433. strcpy(table->replace.name, table->name);
  1434. socklen_t optlen = sizeof(table->replace);
  1435. if (getsockopt(fd, SOL_IP, EBT_SO_GET_INIT_INFO, &table->replace,
  1436. &optlen)) {
  1437. switch (errno) {
  1438. case EPERM:
  1439. case ENOENT:
  1440. case ENOPROTOOPT:
  1441. continue;
  1442. }
  1443. exit(1);
  1444. }
  1445. if (table->replace.entries_size > sizeof(table->entrytable))
  1446. exit(1);
  1447. table->replace.num_counters = 0;
  1448. table->replace.entries = table->entrytable;
  1449. optlen = sizeof(table->replace) + table->replace.entries_size;
  1450. if (getsockopt(fd, SOL_IP, EBT_SO_GET_INIT_ENTRIES, &table->replace,
  1451. &optlen))
  1452. exit(1);
  1453. }
  1454. close(fd);
  1455. }
  1456.  
  1457. static void reset_ebtables()
  1458. {
  1459. int fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
  1460. if (fd == -1) {
  1461. switch (errno) {
  1462. case EAFNOSUPPORT:
  1463. case ENOPROTOOPT:
  1464. case ENOENT:
  1465. return;
  1466. }
  1467. exit(1);
  1468. }
  1469. for (unsigned i = 0; i < sizeof(ebt_tables) / sizeof(ebt_tables[0]); i++) {
  1470. struct ebt_table_desc* table = &ebt_tables[i];
  1471. if (table->replace.valid_hooks == 0)
  1472. continue;
  1473. struct ebt_replace replace;
  1474. memset(&replace, 0, sizeof(replace));
  1475. strcpy(replace.name, table->name);
  1476. socklen_t optlen = sizeof(replace);
  1477. if (getsockopt(fd, SOL_IP, EBT_SO_GET_INFO, &replace, &optlen))
  1478. exit(1);
  1479. replace.num_counters = 0;
  1480. table->replace.entries = 0;
  1481. for (unsigned h = 0; h < NF_BR_NUMHOOKS; h++)
  1482. table->replace.hook_entry[h] = 0;
  1483. if (memcmp(&table->replace, &replace, sizeof(table->replace)) == 0) {
  1484. char entrytable[XT_TABLE_SIZE];
  1485. memset(&entrytable, 0, sizeof(entrytable));
  1486. replace.entries = entrytable;
  1487. optlen = sizeof(replace) + replace.entries_size;
  1488. if (getsockopt(fd, SOL_IP, EBT_SO_GET_ENTRIES, &replace, &optlen))
  1489. exit(1);
  1490. if (memcmp(table->entrytable, entrytable, replace.entries_size) == 0)
  1491. continue;
  1492. }
  1493. for (unsigned j = 0, h = 0; h < NF_BR_NUMHOOKS; h++) {
  1494. if (table->replace.valid_hooks & (1 << h)) {
  1495. table->replace.hook_entry[h] =
  1496. (struct ebt_entries*)table->entrytable + j;
  1497. j++;
  1498. }
  1499. }
  1500. table->replace.entries = table->entrytable;
  1501. optlen = sizeof(table->replace) + table->replace.entries_size;
  1502. if (setsockopt(fd, SOL_IP, EBT_SO_SET_ENTRIES, &table->replace, optlen))
  1503. exit(1);
  1504. }
  1505. close(fd);
  1506. }
  1507.  
  1508. static void checkpoint_net_namespace(void)
  1509. {
  1510. checkpoint_ebtables();
  1511. checkpoint_arptables();
  1512. checkpoint_iptables(ipv4_tables, sizeof(ipv4_tables) / sizeof(ipv4_tables[0]),
  1513. AF_INET, SOL_IP);
  1514. checkpoint_iptables(ipv6_tables, sizeof(ipv6_tables) / sizeof(ipv6_tables[0]),
  1515. AF_INET6, SOL_IPV6);
  1516. }
  1517.  
  1518. static void reset_net_namespace(void)
  1519. {
  1520. reset_ebtables();
  1521. reset_arptables();
  1522. reset_iptables(ipv4_tables, sizeof(ipv4_tables) / sizeof(ipv4_tables[0]),
  1523. AF_INET, SOL_IP);
  1524. reset_iptables(ipv6_tables, sizeof(ipv6_tables) / sizeof(ipv6_tables[0]),
  1525. AF_INET6, SOL_IPV6);
  1526. }
  1527.  
  1528. static void setup_gadgetfs();
  1529. static void setup_binderfs();
  1530. static void setup_fusectl();
  1531. static void sandbox_common_mount_tmpfs(void)
  1532. {
  1533. write_file("/proc/sys/fs/mount-max", "100000");
  1534. if (mkdir("./syz-tmp", 0777))
  1535. exit(1);
  1536. if (mount("", "./syz-tmp", "tmpfs", 0, NULL))
  1537. exit(1);
  1538. if (mkdir("./syz-tmp/newroot", 0777))
  1539. exit(1);
  1540. if (mkdir("./syz-tmp/newroot/dev", 0700))
  1541. exit(1);
  1542. unsigned bind_mount_flags = MS_BIND | MS_REC | MS_PRIVATE;
  1543. if (mount("/dev", "./syz-tmp/newroot/dev", NULL, bind_mount_flags, NULL))
  1544. exit(1);
  1545. if (mkdir("./syz-tmp/newroot/proc", 0700))
  1546. exit(1);
  1547. if (mount("syz-proc", "./syz-tmp/newroot/proc", "proc", 0, NULL))
  1548. exit(1);
  1549. if (mkdir("./syz-tmp/newroot/selinux", 0700))
  1550. exit(1);
  1551. const char* selinux_path = "./syz-tmp/newroot/selinux";
  1552. if (mount("/selinux", selinux_path, NULL, bind_mount_flags, NULL)) {
  1553. if (errno != ENOENT)
  1554. exit(1);
  1555. if (mount("/sys/fs/selinux", selinux_path, NULL, bind_mount_flags, NULL) &&
  1556. errno != ENOENT)
  1557. exit(1);
  1558. }
  1559. if (mkdir("./syz-tmp/newroot/sys", 0700))
  1560. exit(1);
  1561. if (mount("/sys", "./syz-tmp/newroot/sys", 0, bind_mount_flags, NULL))
  1562. exit(1);
  1563. if (mount("/sys/kernel/debug", "./syz-tmp/newroot/sys/kernel/debug", NULL,
  1564. bind_mount_flags, NULL) &&
  1565. errno != ENOENT)
  1566. exit(1);
  1567. if (mount("/sys/fs/smackfs", "./syz-tmp/newroot/sys/fs/smackfs", NULL,
  1568. bind_mount_flags, NULL) &&
  1569. errno != ENOENT)
  1570. exit(1);
  1571. if (mount("/proc/sys/fs/binfmt_misc",
  1572. "./syz-tmp/newroot/proc/sys/fs/binfmt_misc", NULL, bind_mount_flags,
  1573. NULL) &&
  1574. errno != ENOENT)
  1575. exit(1);
  1576. if (mkdir("./syz-tmp/newroot/syz-inputs", 0700))
  1577. exit(1);
  1578. if (mount("/syz-inputs", "./syz-tmp/newroot/syz-inputs", NULL,
  1579. bind_mount_flags | MS_RDONLY, NULL) &&
  1580. errno != ENOENT)
  1581. exit(1);
  1582. if (mkdir("./syz-tmp/pivot", 0777))
  1583. exit(1);
  1584. if (syscall(SYS_pivot_root, "./syz-tmp", "./syz-tmp/pivot")) {
  1585. if (chdir("./syz-tmp"))
  1586. exit(1);
  1587. } else {
  1588. if (chdir("/"))
  1589. exit(1);
  1590. if (umount2("./pivot", MNT_DETACH))
  1591. exit(1);
  1592. }
  1593. if (chroot("./newroot"))
  1594. exit(1);
  1595. if (chdir("/"))
  1596. exit(1);
  1597. setup_gadgetfs();
  1598. setup_binderfs();
  1599. setup_fusectl();
  1600. }
  1601.  
  1602. static void setup_gadgetfs()
  1603. {
  1604. if (mkdir("/dev/gadgetfs", 0777)) {
  1605. }
  1606. if (mount("gadgetfs", "/dev/gadgetfs", "gadgetfs", 0, NULL)) {
  1607. }
  1608. }
  1609.  
  1610. static void setup_fusectl()
  1611. {
  1612. if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) {
  1613. }
  1614. }
  1615.  
  1616. static void setup_binderfs()
  1617. {
  1618. if (mkdir("/dev/binderfs", 0777)) {
  1619. }
  1620. if (mount("binder", "/dev/binderfs", "binder", 0, NULL)) {
  1621. }
  1622. }
  1623.  
  1624. static void loop();
  1625.  
  1626. static void sandbox_common()
  1627. {
  1628. prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  1629. if (getppid() == 1)
  1630. exit(1);
  1631. struct rlimit rlim;
  1632. rlim.rlim_cur = rlim.rlim_max = (200 << 20);
  1633. setrlimit(RLIMIT_AS, &rlim);
  1634. rlim.rlim_cur = rlim.rlim_max = 32 << 20;
  1635. setrlimit(RLIMIT_MEMLOCK, &rlim);
  1636. rlim.rlim_cur = rlim.rlim_max = 136 << 20;
  1637. setrlimit(RLIMIT_FSIZE, &rlim);
  1638. rlim.rlim_cur = rlim.rlim_max = 1 << 20;
  1639. setrlimit(RLIMIT_STACK, &rlim);
  1640. rlim.rlim_cur = rlim.rlim_max = 128 << 20;
  1641. setrlimit(RLIMIT_CORE, &rlim);
  1642. rlim.rlim_cur = rlim.rlim_max = 256;
  1643. setrlimit(RLIMIT_NOFILE, &rlim);
  1644. if (unshare(CLONE_NEWNS)) {
  1645. }
  1646. if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
  1647. }
  1648. if (unshare(CLONE_NEWIPC)) {
  1649. }
  1650. if (unshare(0x02000000)) {
  1651. }
  1652. if (unshare(CLONE_NEWUTS)) {
  1653. }
  1654. if (unshare(CLONE_SYSVSEM)) {
  1655. }
  1656. typedef struct {
  1657. const char* name;
  1658. const char* value;
  1659. } sysctl_t;
  1660. static const sysctl_t sysctls[] = {
  1661. {"/proc/sys/kernel/shmmax", "16777216"},
  1662. {"/proc/sys/kernel/shmall", "536870912"},
  1663. {"/proc/sys/kernel/shmmni", "1024"},
  1664. {"/proc/sys/kernel/msgmax", "8192"},
  1665. {"/proc/sys/kernel/msgmni", "1024"},
  1666. {"/proc/sys/kernel/msgmnb", "1024"},
  1667. {"/proc/sys/kernel/sem", "1024 1048576 500 1024"},
  1668. };
  1669. unsigned i;
  1670. for (i = 0; i < sizeof(sysctls) / sizeof(sysctls[0]); i++)
  1671. write_file(sysctls[i].name, sysctls[i].value);
  1672. }
  1673.  
  1674. static int wait_for_loop(int pid)
  1675. {
  1676. if (pid < 0)
  1677. exit(1);
  1678. int status = 0;
  1679. while (waitpid(-1, &status, __WALL) != pid) {
  1680. }
  1681. return WEXITSTATUS(status);
  1682. }
  1683.  
  1684. static void drop_caps(void)
  1685. {
  1686. struct __user_cap_header_struct cap_hdr = {};
  1687. struct __user_cap_data_struct cap_data[2] = {};
  1688. cap_hdr.version = _LINUX_CAPABILITY_VERSION_3;
  1689. cap_hdr.pid = getpid();
  1690. if (syscall(SYS_capget, &cap_hdr, &cap_data))
  1691. exit(1);
  1692. const int drop = (1 << CAP_SYS_PTRACE) | (1 << CAP_SYS_NICE);
  1693. cap_data[0].effective &= ~drop;
  1694. cap_data[0].permitted &= ~drop;
  1695. cap_data[0].inheritable &= ~drop;
  1696. if (syscall(SYS_capset, &cap_hdr, &cap_data))
  1697. exit(1);
  1698. }
  1699.  
  1700. static int do_sandbox_none(void)
  1701. {
  1702. if (unshare(CLONE_NEWPID)) {
  1703. }
  1704. int pid = fork();
  1705. if (pid != 0)
  1706. return wait_for_loop(pid);
  1707. sandbox_common();
  1708. drop_caps();
  1709. initialize_netdevices_init();
  1710. if (unshare(CLONE_NEWNET)) {
  1711. }
  1712. write_file("/proc/sys/net/ipv4/ping_group_range", "0 65535");
  1713. initialize_netdevices();
  1714. sandbox_common_mount_tmpfs();
  1715. loop();
  1716. exit(1);
  1717. }
  1718.  
  1719. #define FS_IOC_SETFLAGS _IOW('f', 2, long)
  1720. static void remove_dir(const char* dir)
  1721. {
  1722. int iter = 0;
  1723. DIR* dp = 0;
  1724. const int umount_flags = MNT_FORCE | UMOUNT_NOFOLLOW;
  1725.  
  1726. retry:
  1727. while (umount2(dir, umount_flags) == 0) {
  1728. }
  1729. dp = opendir(dir);
  1730. if (dp == NULL) {
  1731. if (errno == EMFILE) {
  1732. exit(1);
  1733. }
  1734. exit(1);
  1735. }
  1736. struct dirent* ep = 0;
  1737. while ((ep = readdir(dp))) {
  1738. if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
  1739. continue;
  1740. char filename[FILENAME_MAX];
  1741. snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
  1742. while (umount2(filename, umount_flags) == 0) {
  1743. }
  1744. struct stat st;
  1745. if (lstat(filename, &st))
  1746. exit(1);
  1747. if (S_ISDIR(st.st_mode)) {
  1748. remove_dir(filename);
  1749. continue;
  1750. }
  1751. int i;
  1752. for (i = 0;; i++) {
  1753. if (unlink(filename) == 0)
  1754. break;
  1755. if (errno == EPERM) {
  1756. int fd = open(filename, O_RDONLY);
  1757. if (fd != -1) {
  1758. long flags = 0;
  1759. if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) {
  1760. }
  1761. close(fd);
  1762. continue;
  1763. }
  1764. }
  1765. if (errno == EROFS) {
  1766. break;
  1767. }
  1768. if (errno != EBUSY || i > 100)
  1769. exit(1);
  1770. if (umount2(filename, umount_flags))
  1771. exit(1);
  1772. }
  1773. }
  1774. closedir(dp);
  1775. for (int i = 0;; i++) {
  1776. if (rmdir(dir) == 0)
  1777. break;
  1778. if (i < 100) {
  1779. if (errno == EPERM) {
  1780. int fd = open(dir, O_RDONLY);
  1781. if (fd != -1) {
  1782. long flags = 0;
  1783. if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) {
  1784. }
  1785. close(fd);
  1786. continue;
  1787. }
  1788. }
  1789. if (errno == EROFS) {
  1790. break;
  1791. }
  1792. if (errno == EBUSY) {
  1793. if (umount2(dir, umount_flags))
  1794. exit(1);
  1795. continue;
  1796. }
  1797. if (errno == ENOTEMPTY) {
  1798. if (iter < 100) {
  1799. iter++;
  1800. goto retry;
  1801. }
  1802. }
  1803. }
  1804. exit(1);
  1805. }
  1806. }
  1807.  
  1808. static void kill_and_wait(int pid, int* status)
  1809. {
  1810. kill(-pid, SIGKILL);
  1811. kill(pid, SIGKILL);
  1812. for (int i = 0; i < 100; i++) {
  1813. if (waitpid(-1, status, WNOHANG | __WALL) == pid)
  1814. return;
  1815. usleep(1000);
  1816. }
  1817. DIR* dir = opendir("/sys/fs/fuse/connections");
  1818. if (dir) {
  1819. for (;;) {
  1820. struct dirent* ent = readdir(dir);
  1821. if (!ent)
  1822. break;
  1823. if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
  1824. continue;
  1825. char abort[300];
  1826. snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
  1827. ent->d_name);
  1828. int fd = open(abort, O_WRONLY);
  1829. if (fd == -1) {
  1830. continue;
  1831. }
  1832. if (write(fd, abort, 1) < 0) {
  1833. }
  1834. close(fd);
  1835. }
  1836. closedir(dir);
  1837. } else {
  1838. }
  1839. while (waitpid(-1, status, __WALL) != pid) {
  1840. }
  1841. }
  1842.  
  1843. static void setup_loop()
  1844. {
  1845. checkpoint_net_namespace();
  1846. }
  1847.  
  1848. static void reset_loop()
  1849. {
  1850. reset_net_namespace();
  1851. }
  1852.  
  1853. static void setup_test()
  1854. {
  1855. prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  1856. setpgrp();
  1857. write_file("/proc/self/oom_score_adj", "1000");
  1858. if (symlink("/dev/binderfs", "./binderfs")) {
  1859. }
  1860. }
  1861.  
  1862. static void close_fds()
  1863. {
  1864. for (int fd = 3; fd < MAX_FDS; fd++)
  1865. close(fd);
  1866. }
  1867.  
  1868. static void setup_sysctl()
  1869. {
  1870. int cad_pid = fork();
  1871. if (cad_pid < 0)
  1872. exit(1);
  1873. if (cad_pid == 0) {
  1874. for (;;)
  1875. sleep(100);
  1876. }
  1877. char tmppid[32];
  1878. snprintf(tmppid, sizeof(tmppid), "%d", cad_pid);
  1879. struct {
  1880. const char* name;
  1881. const char* data;
  1882. } files[] = {
  1883. {"/sys/kernel/debug/x86/nmi_longest_ns", "10000000000"},
  1884. {"/proc/sys/kernel/hung_task_check_interval_secs", "20"},
  1885. {"/proc/sys/net/core/bpf_jit_kallsyms", "1"},
  1886. {"/proc/sys/net/core/bpf_jit_harden", "0"},
  1887. {"/proc/sys/kernel/kptr_restrict", "0"},
  1888. {"/proc/sys/kernel/softlockup_all_cpu_backtrace", "1"},
  1889. {"/proc/sys/fs/mount-max", "100"},
  1890. {"/proc/sys/vm/oom_dump_tasks", "0"},
  1891. {"/proc/sys/debug/exception-trace", "0"},
  1892. {"/proc/sys/kernel/printk", "7 4 1 3"},
  1893. {"/proc/sys/kernel/keys/gc_delay", "1"},
  1894. {"/proc/sys/vm/oom_kill_allocating_task", "1"},
  1895. {"/proc/sys/kernel/ctrl-alt-del", "0"},
  1896. {"/proc/sys/kernel/cad_pid", tmppid},
  1897. };
  1898. for (size_t i = 0; i < sizeof(files) / sizeof(files[0]); i++) {
  1899. if (!write_file(files[i].name, files[i].data)) {
  1900. }
  1901. }
  1902. kill(cad_pid, SIGKILL);
  1903. while (waitpid(cad_pid, NULL, 0) != cad_pid)
  1904. ;
  1905. }
  1906.  
  1907. struct thread_t {
  1908. int created, call;
  1909. event_t ready, done;
  1910. };
  1911.  
  1912. static struct thread_t threads[16];
  1913. static void execute_call(int call);
  1914. static int running;
  1915.  
  1916. static void* thr(void* arg)
  1917. {
  1918. struct thread_t* th = (struct thread_t*)arg;
  1919. for (;;) {
  1920. event_wait(&th->ready);
  1921. event_reset(&th->ready);
  1922. execute_call(th->call);
  1923. __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
  1924. event_set(&th->done);
  1925. }
  1926. return 0;
  1927. }
  1928.  
  1929. static void execute_one(void)
  1930. {
  1931. if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
  1932. }
  1933. int i, call, thread;
  1934. for (call = 0; call < 17; call++) {
  1935. for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
  1936. thread++) {
  1937. struct thread_t* th = &threads[thread];
  1938. if (!th->created) {
  1939. th->created = 1;
  1940. event_init(&th->ready);
  1941. event_init(&th->done);
  1942. event_set(&th->done);
  1943. thread_start(thr, th);
  1944. }
  1945. if (!event_isset(&th->done))
  1946. continue;
  1947. event_reset(&th->done);
  1948. th->call = call;
  1949. __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
  1950. event_set(&th->ready);
  1951. event_timedwait(&th->done, 50);
  1952. break;
  1953. }
  1954. }
  1955. for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
  1956. sleep_ms(1);
  1957. close_fds();
  1958. }
  1959.  
  1960. static void execute_one(void);
  1961.  
  1962. #define WAIT_FLAGS __WALL
  1963.  
  1964. static void loop(void)
  1965. {
  1966. setup_loop();
  1967. int iter = 0;
  1968. for (;; iter++) {
  1969. char cwdbuf[32];
  1970. sprintf(cwdbuf, "./%d", iter);
  1971. if (mkdir(cwdbuf, 0777))
  1972. exit(1);
  1973. reset_loop();
  1974. int pid = fork();
  1975. if (pid < 0)
  1976. exit(1);
  1977. if (pid == 0) {
  1978. if (chdir(cwdbuf))
  1979. exit(1);
  1980. setup_test();
  1981. execute_one();
  1982. exit(0);
  1983. }
  1984. int status = 0;
  1985. uint64_t start = current_time_ms();
  1986. for (;;) {
  1987. sleep_ms(10);
  1988. if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
  1989. break;
  1990. if (current_time_ms() - start < 5000)
  1991. continue;
  1992. kill_and_wait(pid, &status);
  1993. break;
  1994. }
  1995. remove_dir(cwdbuf);
  1996. }
  1997. }
  1998.  
  1999. uint64_t r[4] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff,
  2000. 0xffffffffffffffff};
  2001.  
  2002. void execute_call(int call)
  2003. {
  2004. intptr_t res = 0;
  2005. switch (call) {
  2006. case 0:
  2007. // perf_event_open arguments: [
  2008. // attr: ptr[in, perf_event_attr] {
  2009. // perf_event_attr {
  2010. // type: perf_event_type = 0x0 (4 bytes)
  2011. // size: len = 0x80 (4 bytes)
  2012. // config0: int8 = 0x0 (1 bytes)
  2013. // config1: int8 = 0x0 (1 bytes)
  2014. // config2: int8 = 0x0 (1 bytes)
  2015. // config3: int8 = 0x0 (1 bytes)
  2016. // config4: const = 0x0 (4 bytes)
  2017. // sample_freq: int64 = 0x18a (8 bytes)
  2018. // sample_type: perf_sample_type = 0x0 (8 bytes)
  2019. // read_format: perf_read_format = 0x0 (8 bytes)
  2020. // disabled: int64 = 0x0 (0 bytes)
  2021. // inherit: int64 = 0x0 (0 bytes)
  2022. // pinned: int64 = 0x0 (0 bytes)
  2023. // exclusive: int64 = 0x0 (0 bytes)
  2024. // exclude_user: int64 = 0x0 (0 bytes)
  2025. // exclude_kernel: int64 = 0x0 (0 bytes)
  2026. // exclude_hv: int64 = 0x0 (0 bytes)
  2027. // exclude_idle: int64 = 0x0 (0 bytes)
  2028. // mmap: int64 = 0x0 (0 bytes)
  2029. // comm: int64 = 0x0 (0 bytes)
  2030. // freq: int64 = 0x0 (0 bytes)
  2031. // inherit_stat: int64 = 0x0 (0 bytes)
  2032. // enable_on_exec: int64 = 0x0 (0 bytes)
  2033. // task: int64 = 0x0 (0 bytes)
  2034. // watermark: int64 = 0x0 (0 bytes)
  2035. // precise_ip: int64 = 0x0 (0 bytes)
  2036. // mmap_data: int64 = 0x0 (0 bytes)
  2037. // sample_id_all: int64 = 0x0 (0 bytes)
  2038. // exclude_host: int64 = 0x0 (0 bytes)
  2039. // exclude_guest: int64 = 0x0 (0 bytes)
  2040. // exclude_callchain_kernel: int64 = 0x0 (0 bytes)
  2041. // exclude_callchain_user: int64 = 0x0 (0 bytes)
  2042. // mmap2: int64 = 0x0 (0 bytes)
  2043. // comm_exec: int64 = 0x0 (0 bytes)
  2044. // use_clockid: int64 = 0x0 (0 bytes)
  2045. // context_switch: int64 = 0x0 (0 bytes)
  2046. // write_backward: int64 = 0x0 (0 bytes)
  2047. // namespaces: int64 = 0x0 (0 bytes)
  2048. // ksymbol: int64 = 0x0 (0 bytes)
  2049. // bpf_event: int64 = 0x0 (0 bytes)
  2050. // aux_output: int64 = 0x0 (0 bytes)
  2051. // cgroup: int64 = 0x0 (0 bytes)
  2052. // text_poke: int64 = 0x0 (0 bytes)
  2053. // build_id: int64 = 0x0 (0 bytes)
  2054. // inherit_thread: int64 = 0x0 (0 bytes)
  2055. // remove_on_exec: int64 = 0x0 (0 bytes)
  2056. // sigtrap: int64 = 0x0 (0 bytes)
  2057. // __reserved_1: const = 0x0 (8 bytes)
  2058. // wakeup_events: int32 = 0x0 (4 bytes)
  2059. // bp_type: perf_bp_type = 0x0 (4 bytes)
  2060. // bp_config: union perf_bp_config {
  2061. // perf_config_ext: perf_config_ext {
  2062. // config1: int64 = 0xfff (8 bytes)
  2063. // config2: int64 = 0x3 (8 bytes)
  2064. // }
  2065. // }
  2066. // branch_sample_type: perf_branch_sample_type = 0x0 (8 bytes)
  2067. // sample_regs_user: int64 = 0x8 (8 bytes)
  2068. // sample_stack_user: int32 = 0x0 (4 bytes)
  2069. // clockid: clock_type = 0x1 (4 bytes)
  2070. // sample_regs_intr: int64 = 0x7ff (8 bytes)
  2071. // aux_watermark: int32 = 0x0 (4 bytes)
  2072. // sample_max_stack: int16 = 0x0 (2 bytes)
  2073. // __reserved_2: const = 0x0 (2 bytes)
  2074. // aux_sample_size: int32 = 0x0 (4 bytes)
  2075. // __reserved_3: const = 0x0 (4 bytes)
  2076. // sig_data: int64 = 0x0 (8 bytes)
  2077. // }
  2078. // }
  2079. // pid: pid (resource)
  2080. // cpu: intptr = 0xffffffffffffffff (8 bytes)
  2081. // group: fd_perf (resource)
  2082. // flags: perf_flags = 0x0 (8 bytes)
  2083. // ]
  2084. // returns fd_perf
  2085. NONFAILING(*(uint32_t*)0x20000001d000 = 0);
  2086. NONFAILING(*(uint32_t*)0x20000001d004 = 0x80);
  2087. NONFAILING(*(uint8_t*)0x20000001d008 = 0);
  2088. NONFAILING(*(uint8_t*)0x20000001d009 = 0);
  2089. NONFAILING(*(uint8_t*)0x20000001d00a = 0);
  2090. NONFAILING(*(uint8_t*)0x20000001d00b = 0);
  2091. NONFAILING(*(uint32_t*)0x20000001d00c = 0);
  2092. NONFAILING(*(uint64_t*)0x20000001d010 = 0x18a);
  2093. NONFAILING(*(uint64_t*)0x20000001d018 = 0);
  2094. NONFAILING(*(uint64_t*)0x20000001d020 = 0);
  2095. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 0, 1));
  2096. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 1, 1));
  2097. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 2, 1));
  2098. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 3, 1));
  2099. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 4, 1));
  2100. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 5, 1));
  2101. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 6, 1));
  2102. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 7, 1));
  2103. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 8, 1));
  2104. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 9, 1));
  2105. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 10, 1));
  2106. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 11, 1));
  2107. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 12, 1));
  2108. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 13, 1));
  2109. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 14, 1));
  2110. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 15, 2));
  2111. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 17, 1));
  2112. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 18, 1));
  2113. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 19, 1));
  2114. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 20, 1));
  2115. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 21, 1));
  2116. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 22, 1));
  2117. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 23, 1));
  2118. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 24, 1));
  2119. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 25, 1));
  2120. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 26, 1));
  2121. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 27, 1));
  2122. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 28, 1));
  2123. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 29, 1));
  2124. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 30, 1));
  2125. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 31, 1));
  2126. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 32, 1));
  2127. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 33, 1));
  2128. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 34, 1));
  2129. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 35, 1));
  2130. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 36, 1));
  2131. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 37, 1));
  2132. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 38, 26));
  2133. NONFAILING(*(uint32_t*)0x20000001d030 = 0);
  2134. NONFAILING(*(uint32_t*)0x20000001d034 = 0);
  2135. NONFAILING(*(uint64_t*)0x20000001d038 = 0xfff);
  2136. NONFAILING(*(uint64_t*)0x20000001d040 = 3);
  2137. NONFAILING(*(uint64_t*)0x20000001d048 = 0);
  2138. NONFAILING(*(uint64_t*)0x20000001d050 = 8);
  2139. NONFAILING(*(uint32_t*)0x20000001d058 = 0);
  2140. NONFAILING(*(uint32_t*)0x20000001d05c = 1);
  2141. NONFAILING(*(uint64_t*)0x20000001d060 = 0x7ff);
  2142. NONFAILING(*(uint32_t*)0x20000001d068 = 0);
  2143. NONFAILING(*(uint16_t*)0x20000001d06c = 0);
  2144. NONFAILING(*(uint16_t*)0x20000001d06e = 0);
  2145. NONFAILING(*(uint32_t*)0x20000001d070 = 0);
  2146. NONFAILING(*(uint32_t*)0x20000001d074 = 0);
  2147. NONFAILING(*(uint64_t*)0x20000001d078 = 0);
  2148. res = syscall(__NR_perf_event_open, /*attr=*/0x20000001d000ul, /*pid=*/0,
  2149. /*cpu=*/(intptr_t)-1, /*group=*/(intptr_t)-1, /*flags=*/0ul);
  2150. if (res != -1)
  2151. r[0] = res;
  2152. break;
  2153. case 1:
  2154. // shmget$private arguments: [
  2155. // key: const = 0x0 (8 bytes)
  2156. // size: len = 0x13000 (8 bytes)
  2157. // flags: shmget_flags = 0x0 (8 bytes)
  2158. // unused: VMA[0x13000]
  2159. // ]
  2160. // returns ipc_shm
  2161. syscall(__NR_shmget, /*key=*/0ul, /*size=*/0x13000ul, /*flags=*/0ul,
  2162. /*unused=*/0x200000fec000ul);
  2163. break;
  2164. case 2:
  2165. // set_mempolicy arguments: [
  2166. // mode: mbind_mode = 0x4005 (8 bytes)
  2167. // nodemask: ptr[in, int64] {
  2168. // int64 = 0x103 (8 bytes)
  2169. // }
  2170. // maxnode: intptr = 0x4 (8 bytes)
  2171. // ]
  2172. NONFAILING(*(uint64_t*)0x200000000280 = 0x103);
  2173. syscall(__NR_set_mempolicy,
  2174. /*mode=MPOL_F_RELATIVE_NODES|MPOL_PREFERRED|0x4*/ 0x4005ul,
  2175. /*nodemask=*/0x200000000280ul, /*maxnode=*/4ul);
  2176. break;
  2177. case 3:
  2178. // setsockopt$packet_tx_ring arguments: [
  2179. // fd: sock_packet (resource)
  2180. // level: const = 0x107 (4 bytes)
  2181. // optname: const = 0xd (4 bytes)
  2182. // optval: nil
  2183. // optlen: len = 0x0 (8 bytes)
  2184. // ]
  2185. syscall(__NR_setsockopt, /*fd=*/(intptr_t)-1, /*level=*/0x107,
  2186. /*optname=*/0xd, /*optval=*/0ul, /*optlen=*/0ul);
  2187. break;
  2188. case 4:
  2189. // write$binfmt_elf64 arguments: [
  2190. // fd: fd_binfmt (resource)
  2191. // data: ptr[inout, array[ANYUNION]] {
  2192. // array[ANYUNION] {
  2193. // union ANYUNION {
  2194. // ANYBLOB: buffer: {7f 45 4c 46 04 03 06 00 04 00 00 00 00 00 00
  2195. // 00 02 00 3e 00 00 00 00 00 81 01 00 00 00 00 00 00 40 00 00 00
  2196. // 00 00 00 00 36 03 00 00 00} (length 0x2d)
  2197. // }
  2198. // }
  2199. // }
  2200. // len: bytesize = 0x78 (8 bytes)
  2201. // ]
  2202. NONFAILING(
  2203. memcpy((void*)0x200000000080,
  2204. "\x7f\x45\x4c\x46\x04\x03\x06\x00\x04\x00\x00\x00\x00\x00\x00"
  2205. "\x00\x02\x00\x3e\x00\x00\x00\x00\x00\x81\x01\x00\x00\x00\x00"
  2206. "\x00\x00\x40\x00\x00\x00\x00\x00\x00\x00\x36\x03\x00\x00\x00",
  2207. 45));
  2208. syscall(__NR_write, /*fd=*/(intptr_t)-1, /*data=*/0x200000000080ul,
  2209. /*len=*/0x78ul);
  2210. break;
  2211. case 5:
  2212. // perf_event_open arguments: [
  2213. // attr: ptr[in, perf_event_attr] {
  2214. // perf_event_attr {
  2215. // type: perf_event_type = 0x3 (4 bytes)
  2216. // size: len = 0x80 (4 bytes)
  2217. // config0: int8 = 0x0 (1 bytes)
  2218. // config1: int8 = 0x0 (1 bytes)
  2219. // config2: int8 = 0x0 (1 bytes)
  2220. // config3: int8 = 0x0 (1 bytes)
  2221. // config4: const = 0x0 (4 bytes)
  2222. // sample_freq: int64 = 0x18a (8 bytes)
  2223. // sample_type: perf_sample_type = 0x0 (8 bytes)
  2224. // read_format: perf_read_format = 0x2 (8 bytes)
  2225. // disabled: int64 = 0x0 (0 bytes)
  2226. // inherit: int64 = 0x0 (0 bytes)
  2227. // pinned: int64 = 0x0 (0 bytes)
  2228. // exclusive: int64 = 0x0 (0 bytes)
  2229. // exclude_user: int64 = 0x0 (0 bytes)
  2230. // exclude_kernel: int64 = 0x0 (0 bytes)
  2231. // exclude_hv: int64 = 0x0 (0 bytes)
  2232. // exclude_idle: int64 = 0x0 (0 bytes)
  2233. // mmap: int64 = 0x0 (0 bytes)
  2234. // comm: int64 = 0x0 (0 bytes)
  2235. // freq: int64 = 0x0 (0 bytes)
  2236. // inherit_stat: int64 = 0x0 (0 bytes)
  2237. // enable_on_exec: int64 = 0x0 (0 bytes)
  2238. // task: int64 = 0x0 (0 bytes)
  2239. // watermark: int64 = 0x0 (0 bytes)
  2240. // precise_ip: int64 = 0x0 (0 bytes)
  2241. // mmap_data: int64 = 0x0 (0 bytes)
  2242. // sample_id_all: int64 = 0x0 (0 bytes)
  2243. // exclude_host: int64 = 0x0 (0 bytes)
  2244. // exclude_guest: int64 = 0x0 (0 bytes)
  2245. // exclude_callchain_kernel: int64 = 0x0 (0 bytes)
  2246. // exclude_callchain_user: int64 = 0x0 (0 bytes)
  2247. // mmap2: int64 = 0x0 (0 bytes)
  2248. // comm_exec: int64 = 0x0 (0 bytes)
  2249. // use_clockid: int64 = 0x0 (0 bytes)
  2250. // context_switch: int64 = 0x0 (0 bytes)
  2251. // write_backward: int64 = 0x0 (0 bytes)
  2252. // namespaces: int64 = 0x0 (0 bytes)
  2253. // ksymbol: int64 = 0x0 (0 bytes)
  2254. // bpf_event: int64 = 0x0 (0 bytes)
  2255. // aux_output: int64 = 0x0 (0 bytes)
  2256. // cgroup: int64 = 0x0 (0 bytes)
  2257. // text_poke: int64 = 0x0 (0 bytes)
  2258. // build_id: int64 = 0x0 (0 bytes)
  2259. // inherit_thread: int64 = 0x0 (0 bytes)
  2260. // remove_on_exec: int64 = 0x0 (0 bytes)
  2261. // sigtrap: int64 = 0x0 (0 bytes)
  2262. // __reserved_1: const = 0x0 (8 bytes)
  2263. // wakeup_events: int32 = 0x0 (4 bytes)
  2264. // bp_type: perf_bp_type = 0x0 (4 bytes)
  2265. // bp_config: union perf_bp_config {
  2266. // perf_bp: perf_bp {
  2267. // bp_addr: nil
  2268. // bp_len: perf_bp_lens = 0x1 (8 bytes)
  2269. // }
  2270. // }
  2271. // branch_sample_type: perf_branch_sample_type = 0x0 (8 bytes)
  2272. // sample_regs_user: int64 = 0x0 (8 bytes)
  2273. // sample_stack_user: int32 = 0x2000 (4 bytes)
  2274. // clockid: clock_type = 0x0 (4 bytes)
  2275. // sample_regs_intr: int64 = 0x3 (8 bytes)
  2276. // aux_watermark: int32 = 0x9 (4 bytes)
  2277. // sample_max_stack: int16 = 0x0 (2 bytes)
  2278. // __reserved_2: const = 0x0 (2 bytes)
  2279. // aux_sample_size: int32 = 0x0 (4 bytes)
  2280. // __reserved_3: const = 0x0 (4 bytes)
  2281. // sig_data: int64 = 0x0 (8 bytes)
  2282. // }
  2283. // }
  2284. // pid: pid (resource)
  2285. // cpu: intptr = 0xffffffffffffffff (8 bytes)
  2286. // group: fd_perf (resource)
  2287. // flags: perf_flags = 0x0 (8 bytes)
  2288. // ]
  2289. // returns fd_perf
  2290. NONFAILING(*(uint32_t*)0x20000001d000 = 3);
  2291. NONFAILING(*(uint32_t*)0x20000001d004 = 0x80);
  2292. NONFAILING(*(uint8_t*)0x20000001d008 = 0);
  2293. NONFAILING(*(uint8_t*)0x20000001d009 = 0);
  2294. NONFAILING(*(uint8_t*)0x20000001d00a = 0);
  2295. NONFAILING(*(uint8_t*)0x20000001d00b = 0);
  2296. NONFAILING(*(uint32_t*)0x20000001d00c = 0);
  2297. NONFAILING(*(uint64_t*)0x20000001d010 = 0x18a);
  2298. NONFAILING(*(uint64_t*)0x20000001d018 = 0);
  2299. NONFAILING(*(uint64_t*)0x20000001d020 = 2);
  2300. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 0, 1));
  2301. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 1, 1));
  2302. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 2, 1));
  2303. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 3, 1));
  2304. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 4, 1));
  2305. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 5, 1));
  2306. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 6, 1));
  2307. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 7, 1));
  2308. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 8, 1));
  2309. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 9, 1));
  2310. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 10, 1));
  2311. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 11, 1));
  2312. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 12, 1));
  2313. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 13, 1));
  2314. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 14, 1));
  2315. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 15, 2));
  2316. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 17, 1));
  2317. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 18, 1));
  2318. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 19, 1));
  2319. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 20, 1));
  2320. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 21, 1));
  2321. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 22, 1));
  2322. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 23, 1));
  2323. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 24, 1));
  2324. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 25, 1));
  2325. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 26, 1));
  2326. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 27, 1));
  2327. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 28, 1));
  2328. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 29, 1));
  2329. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 30, 1));
  2330. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 31, 1));
  2331. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 32, 1));
  2332. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 33, 1));
  2333. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 34, 1));
  2334. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 35, 1));
  2335. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 36, 1));
  2336. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 37, 1));
  2337. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x20000001d028, 0, 38, 26));
  2338. NONFAILING(*(uint32_t*)0x20000001d030 = 0);
  2339. NONFAILING(*(uint32_t*)0x20000001d034 = 0);
  2340. NONFAILING(*(uint64_t*)0x20000001d038 = 0);
  2341. NONFAILING(*(uint64_t*)0x20000001d040 = 1);
  2342. NONFAILING(*(uint64_t*)0x20000001d048 = 0);
  2343. NONFAILING(*(uint64_t*)0x20000001d050 = 0);
  2344. NONFAILING(*(uint32_t*)0x20000001d058 = 0x2000);
  2345. NONFAILING(*(uint32_t*)0x20000001d05c = 0);
  2346. NONFAILING(*(uint64_t*)0x20000001d060 = 3);
  2347. NONFAILING(*(uint32_t*)0x20000001d068 = 9);
  2348. NONFAILING(*(uint16_t*)0x20000001d06c = 0);
  2349. NONFAILING(*(uint16_t*)0x20000001d06e = 0);
  2350. NONFAILING(*(uint32_t*)0x20000001d070 = 0);
  2351. NONFAILING(*(uint32_t*)0x20000001d074 = 0);
  2352. NONFAILING(*(uint64_t*)0x20000001d078 = 0);
  2353. syscall(__NR_perf_event_open, /*attr=*/0x20000001d000ul, /*pid=*/0,
  2354. /*cpu=*/(intptr_t)-1, /*group=*/(intptr_t)-1, /*flags=*/0ul);
  2355. break;
  2356. case 6:
  2357. // ioctl$int_in arguments: [
  2358. // fd: fd (resource)
  2359. // cmd: ioctl_int_in = 0x5452 (4 bytes)
  2360. // v: nil
  2361. // ]
  2362. syscall(__NR_ioctl, /*fd=*/(intptr_t)-1, /*cmd=FIOASYNC*/ 0x5452,
  2363. /*v=*/0ul);
  2364. break;
  2365. case 7:
  2366. // socket$inet6_udplite arguments: [
  2367. // domain: const = 0xa (8 bytes)
  2368. // type: const = 0x2 (8 bytes)
  2369. // proto: const = 0x88 (4 bytes)
  2370. // ]
  2371. // returns sock_udp6
  2372. res = syscall(__NR_socket, /*domain=*/0xaul, /*type=*/2ul, /*proto=*/0x88);
  2373. if (res != -1)
  2374. r[1] = res;
  2375. break;
  2376. case 8:
  2377. // connect$inet6 arguments: [
  2378. // fd: sock_in6 (resource)
  2379. // addr: nil
  2380. // addrlen: len = 0x0 (8 bytes)
  2381. // ]
  2382. syscall(__NR_connect, /*fd=*/r[1], /*addr=*/0ul, /*addrlen=*/0ul);
  2383. break;
  2384. case 9:
  2385. // connect$inet6 arguments: [
  2386. // fd: sock_in6 (resource)
  2387. // addr: ptr[in, sockaddr_in6] {
  2388. // sockaddr_in6 {
  2389. // family: const = 0xa (2 bytes)
  2390. // port: int16be = 0x4e20 (2 bytes)
  2391. // flow: int32be = 0x0 (4 bytes)
  2392. // addr: union ipv6_addr {
  2393. // ipv4: ipv6_addr_ipv4 {
  2394. // a0: buffer: {00 00 00 00 00 00 00 00 00 00} (length 0xa)
  2395. // a1: buffer: {ff ff} (length 0x2)
  2396. // a3: union ipv4_addr {
  2397. // initdev: ipv4_addr_initdev {
  2398. // a0: const = 0xac (1 bytes)
  2399. // a1: const = 0x1e (1 bytes)
  2400. // a2: int8 = 0x1 (1 bytes)
  2401. // a3: proc = 0x0 (1 bytes)
  2402. // }
  2403. // }
  2404. // }
  2405. // }
  2406. // scope: int32 = 0x0 (4 bytes)
  2407. // }
  2408. // }
  2409. // addrlen: len = 0x1c (8 bytes)
  2410. // ]
  2411. NONFAILING(*(uint16_t*)0x200000000700 = 0xa);
  2412. NONFAILING(*(uint16_t*)0x200000000702 = htobe16(0x4e20));
  2413. NONFAILING(*(uint32_t*)0x200000000704 = htobe32(0));
  2414. NONFAILING(memset((void*)0x200000000708, 0, 10));
  2415. NONFAILING(memset((void*)0x200000000712, 255, 2));
  2416. NONFAILING(*(uint8_t*)0x200000000714 = 0xac);
  2417. NONFAILING(*(uint8_t*)0x200000000715 = 0x1e);
  2418. NONFAILING(*(uint8_t*)0x200000000716 = 1);
  2419. NONFAILING(*(uint8_t*)0x200000000717 = 1 + procid * 1);
  2420. NONFAILING(*(uint32_t*)0x200000000718 = 0);
  2421. syscall(__NR_connect, /*fd=*/r[1], /*addr=*/0x200000000700ul,
  2422. /*addrlen=*/0x1cul);
  2423. break;
  2424. case 10:
  2425. // ioctl$int_in arguments: [
  2426. // fd: fd (resource)
  2427. // cmd: ioctl_int_in = 0x5452 (4 bytes)
  2428. // v: ptr[in, int64] {
  2429. // int64 = 0x1ff (8 bytes)
  2430. // }
  2431. // ]
  2432. NONFAILING(*(uint64_t*)0x200000000080 = 0x1ff);
  2433. syscall(__NR_ioctl, /*fd=*/r[0], /*cmd=FIOASYNC*/ 0x5452,
  2434. /*v=*/0x200000000080ul);
  2435. break;
  2436. case 11:
  2437. // socket$netlink arguments: [
  2438. // domain: const = 0x10 (8 bytes)
  2439. // type: const = 0x3 (8 bytes)
  2440. // proto: netlink_proto = 0x0 (4 bytes)
  2441. // ]
  2442. // returns sock_netlink
  2443. res = syscall(__NR_socket, /*domain=*/0x10ul, /*type=*/3ul, /*proto=*/0);
  2444. if (res != -1)
  2445. r[2] = res;
  2446. break;
  2447. case 12:
  2448. // sendmsg$netlink arguments: [
  2449. // fd: sock_netlink (resource)
  2450. // msg: ptr[in,
  2451. // msghdr_netlink_full[netlink_msg_t[netlink_random_msg_type, void,
  2452. // nl_generic_attr]]] {
  2453. // msghdr_netlink_full[netlink_msg_t[netlink_random_msg_type, void,
  2454. // nl_generic_attr]] {
  2455. // addr: nil
  2456. // addrlen: len = 0x0 (4 bytes)
  2457. // pad = 0x0 (4 bytes)
  2458. // vec: nil
  2459. // vlen: len = 0x0 (8 bytes)
  2460. // ctrl: nil
  2461. // ctrllen: bytesize = 0x0 (8 bytes)
  2462. // f: send_flags = 0x0 (4 bytes)
  2463. // pad = 0x0 (4 bytes)
  2464. // }
  2465. // }
  2466. // f: send_flags = 0x0 (8 bytes)
  2467. // ]
  2468. NONFAILING(*(uint64_t*)0x200000006440 = 0);
  2469. NONFAILING(*(uint32_t*)0x200000006448 = 0);
  2470. NONFAILING(*(uint64_t*)0x200000006450 = 0);
  2471. NONFAILING(*(uint64_t*)0x200000006458 = 0);
  2472. NONFAILING(*(uint64_t*)0x200000006460 = 0);
  2473. NONFAILING(*(uint64_t*)0x200000006468 = 0);
  2474. NONFAILING(*(uint32_t*)0x200000006470 = 0);
  2475. syscall(__NR_sendmsg, /*fd=*/r[2], /*msg=*/0x200000006440ul, /*f=*/0ul);
  2476. break;
  2477. case 13:
  2478. // perf_event_open arguments: [
  2479. // attr: ptr[in, perf_event_attr] {
  2480. // perf_event_attr {
  2481. // type: perf_event_type = 0x2 (4 bytes)
  2482. // size: len = 0x80 (4 bytes)
  2483. // config0: int8 = 0x80 (1 bytes)
  2484. // config1: int8 = 0x0 (1 bytes)
  2485. // config2: int8 = 0x0 (1 bytes)
  2486. // config3: int8 = 0x0 (1 bytes)
  2487. // config4: const = 0x0 (4 bytes)
  2488. // sample_freq: int64 = 0x0 (8 bytes)
  2489. // sample_type: perf_sample_type = 0x0 (8 bytes)
  2490. // read_format: perf_read_format = 0x1 (8 bytes)
  2491. // disabled: int64 = 0x0 (0 bytes)
  2492. // inherit: int64 = 0x0 (0 bytes)
  2493. // pinned: int64 = 0x0 (0 bytes)
  2494. // exclusive: int64 = 0x0 (0 bytes)
  2495. // exclude_user: int64 = 0x0 (0 bytes)
  2496. // exclude_kernel: int64 = 0x0 (0 bytes)
  2497. // exclude_hv: int64 = 0x0 (0 bytes)
  2498. // exclude_idle: int64 = 0x0 (0 bytes)
  2499. // mmap: int64 = 0x0 (0 bytes)
  2500. // comm: int64 = 0x0 (0 bytes)
  2501. // freq: int64 = 0x0 (0 bytes)
  2502. // inherit_stat: int64 = 0x0 (0 bytes)
  2503. // enable_on_exec: int64 = 0x0 (0 bytes)
  2504. // task: int64 = 0x0 (0 bytes)
  2505. // watermark: int64 = 0x0 (0 bytes)
  2506. // precise_ip: int64 = 0x0 (0 bytes)
  2507. // mmap_data: int64 = 0x0 (0 bytes)
  2508. // sample_id_all: int64 = 0x0 (0 bytes)
  2509. // exclude_host: int64 = 0x0 (0 bytes)
  2510. // exclude_guest: int64 = 0x0 (0 bytes)
  2511. // exclude_callchain_kernel: int64 = 0x0 (0 bytes)
  2512. // exclude_callchain_user: int64 = 0x0 (0 bytes)
  2513. // mmap2: int64 = 0x0 (0 bytes)
  2514. // comm_exec: int64 = 0x0 (0 bytes)
  2515. // use_clockid: int64 = 0x0 (0 bytes)
  2516. // context_switch: int64 = 0x0 (0 bytes)
  2517. // write_backward: int64 = 0x0 (0 bytes)
  2518. // namespaces: int64 = 0x0 (0 bytes)
  2519. // ksymbol: int64 = 0x0 (0 bytes)
  2520. // bpf_event: int64 = 0x0 (0 bytes)
  2521. // aux_output: int64 = 0x0 (0 bytes)
  2522. // cgroup: int64 = 0x0 (0 bytes)
  2523. // text_poke: int64 = 0x0 (0 bytes)
  2524. // build_id: int64 = 0x0 (0 bytes)
  2525. // inherit_thread: int64 = 0x0 (0 bytes)
  2526. // remove_on_exec: int64 = 0x0 (0 bytes)
  2527. // sigtrap: int64 = 0x0 (0 bytes)
  2528. // __reserved_1: const = 0x0 (8 bytes)
  2529. // wakeup_events: int32 = 0x0 (4 bytes)
  2530. // bp_type: perf_bp_type = 0x0 (4 bytes)
  2531. // bp_config: union perf_bp_config {
  2532. // perf_config_ext: perf_config_ext {
  2533. // config1: int64 = 0x0 (8 bytes)
  2534. // config2: int64 = 0x0 (8 bytes)
  2535. // }
  2536. // }
  2537. // branch_sample_type: perf_branch_sample_type = 0x0 (8 bytes)
  2538. // sample_regs_user: int64 = 0x0 (8 bytes)
  2539. // sample_stack_user: int32 = 0x0 (4 bytes)
  2540. // clockid: clock_type = 0x0 (4 bytes)
  2541. // sample_regs_intr: int64 = 0x0 (8 bytes)
  2542. // aux_watermark: int32 = 0xfffffffe (4 bytes)
  2543. // sample_max_stack: int16 = 0x0 (2 bytes)
  2544. // __reserved_2: const = 0x0 (2 bytes)
  2545. // aux_sample_size: int32 = 0x0 (4 bytes)
  2546. // __reserved_3: const = 0x0 (4 bytes)
  2547. // sig_data: int64 = 0x7 (8 bytes)
  2548. // }
  2549. // }
  2550. // pid: pid (resource)
  2551. // cpu: intptr = 0x0 (8 bytes)
  2552. // group: fd_perf (resource)
  2553. // flags: perf_flags = 0x0 (8 bytes)
  2554. // ]
  2555. // returns fd_perf
  2556. NONFAILING(*(uint32_t*)0x200000000100 = 2);
  2557. NONFAILING(*(uint32_t*)0x200000000104 = 0x80);
  2558. NONFAILING(*(uint8_t*)0x200000000108 = 0x80);
  2559. NONFAILING(*(uint8_t*)0x200000000109 = 0);
  2560. NONFAILING(*(uint8_t*)0x20000000010a = 0);
  2561. NONFAILING(*(uint8_t*)0x20000000010b = 0);
  2562. NONFAILING(*(uint32_t*)0x20000000010c = 0);
  2563. NONFAILING(*(uint64_t*)0x200000000110 = 0);
  2564. NONFAILING(*(uint64_t*)0x200000000118 = 0);
  2565. NONFAILING(*(uint64_t*)0x200000000120 = 1);
  2566. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 0, 1));
  2567. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 1, 1));
  2568. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 2, 1));
  2569. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 3, 1));
  2570. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 4, 1));
  2571. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 5, 1));
  2572. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 6, 1));
  2573. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 7, 1));
  2574. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 8, 1));
  2575. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 9, 1));
  2576. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 10, 1));
  2577. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 11, 1));
  2578. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 12, 1));
  2579. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 13, 1));
  2580. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 14, 1));
  2581. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 15, 2));
  2582. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 17, 1));
  2583. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 18, 1));
  2584. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 19, 1));
  2585. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 20, 1));
  2586. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 21, 1));
  2587. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 22, 1));
  2588. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 23, 1));
  2589. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 24, 1));
  2590. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 25, 1));
  2591. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 26, 1));
  2592. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 27, 1));
  2593. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 28, 1));
  2594. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 29, 1));
  2595. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 30, 1));
  2596. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 31, 1));
  2597. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 32, 1));
  2598. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 33, 1));
  2599. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 34, 1));
  2600. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 35, 1));
  2601. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 36, 1));
  2602. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 37, 1));
  2603. NONFAILING(STORE_BY_BITMASK(uint64_t, , 0x200000000128, 0, 38, 26));
  2604. NONFAILING(*(uint32_t*)0x200000000130 = 0);
  2605. NONFAILING(*(uint32_t*)0x200000000134 = 0);
  2606. NONFAILING(*(uint64_t*)0x200000000138 = 0);
  2607. NONFAILING(*(uint64_t*)0x200000000140 = 0);
  2608. NONFAILING(*(uint64_t*)0x200000000148 = 0);
  2609. NONFAILING(*(uint64_t*)0x200000000150 = 0);
  2610. NONFAILING(*(uint32_t*)0x200000000158 = 0);
  2611. NONFAILING(*(uint32_t*)0x20000000015c = 0);
  2612. NONFAILING(*(uint64_t*)0x200000000160 = 0);
  2613. NONFAILING(*(uint32_t*)0x200000000168 = 0xfffffffe);
  2614. NONFAILING(*(uint16_t*)0x20000000016c = 0);
  2615. NONFAILING(*(uint16_t*)0x20000000016e = 0);
  2616. NONFAILING(*(uint32_t*)0x200000000170 = 0);
  2617. NONFAILING(*(uint32_t*)0x200000000174 = 0);
  2618. NONFAILING(*(uint64_t*)0x200000000178 = 7);
  2619. syscall(__NR_perf_event_open, /*attr=*/0x200000000100ul, /*pid=*/0,
  2620. /*cpu=*/0ul, /*group=*/(intptr_t)-1, /*flags=*/0ul);
  2621. break;
  2622. case 14:
  2623. // socket$netlink arguments: [
  2624. // domain: const = 0x10 (8 bytes)
  2625. // type: const = 0x3 (8 bytes)
  2626. // proto: netlink_proto = 0x0 (4 bytes)
  2627. // ]
  2628. // returns sock_netlink
  2629. res = syscall(__NR_socket, /*domain=*/0x10ul, /*type=*/3ul, /*proto=*/0);
  2630. if (res != -1)
  2631. r[3] = res;
  2632. break;
  2633. case 15:
  2634. // socket arguments: [
  2635. // domain: socket_domain = 0x2 (8 bytes)
  2636. // type: socket_type = 0x3 (8 bytes)
  2637. // proto: int32 = 0x1 (4 bytes)
  2638. // ]
  2639. // returns sock
  2640. syscall(__NR_socket, /*domain=AF_INET*/ 2ul, /*type=SOCK_RAW*/ 3ul,
  2641. /*proto=*/1);
  2642. break;
  2643. case 16:
  2644. // sendmsg$netlink arguments: [
  2645. // fd: sock_netlink (resource)
  2646. // msg: ptr[in,
  2647. // msghdr_netlink_full[netlink_msg_t[netlink_random_msg_type, void,
  2648. // nl_generic_attr]]] {
  2649. // msghdr_netlink_full[netlink_msg_t[netlink_random_msg_type, void,
  2650. // nl_generic_attr]] {
  2651. // addr: nil
  2652. // addrlen: len = 0x0 (4 bytes)
  2653. // pad = 0x0 (4 bytes)
  2654. // vec: ptr[in, array[iovec[in,
  2655. // netlink_msg_t[netlink_random_msg_type, void, nl_generic_attr]]]] {
  2656. // array[iovec[in, netlink_msg_t[netlink_random_msg_type, void,
  2657. // nl_generic_attr]]] {
  2658. // iovec[in, netlink_msg_t[netlink_random_msg_type, void,
  2659. // nl_generic_attr]] {
  2660. // addr: ptr[inout, array[ANYUNION]] {
  2661. // array[ANYUNION] {
  2662. // union ANYUNION {
  2663. // ANYBLOB: buffer: {28 00 00 00 10 00 01 00 00 00 00 00
  2664. // 00 00 00 00 08 00 00 00 1f 04 79 5e 64 24 fc cf 2b 93
  2665. // ac 36 c5 fc 5e 46 ca 1b ed 01 91 ce b0 df b6 b9 23 ce
  2666. // 64 97 dc ce 31 43 4f 02 67 64 cb 87 2d c7 97 9e 9a 26
  2667. // dd 46 3c fd c0 b0 0e fa 93 83 b0 cd 2f 19 72 26 88 80
  2668. // de 56 35 21 6a d6 b8 27 f4 1f 12 dc 42 b9 c5 d9 a8 4c
  2669. // e8} (length 0x67)
  2670. // }
  2671. // union ANYUNION {
  2672. // ANYRES32: ANYRES32 (resource)
  2673. // }
  2674. // union ANYUNION {
  2675. // ANYBLOB: buffer: {08 00 00 00 e0 00 00 02 05 00 35 80
  2676. // e7 00 00 00} (length 0x10)
  2677. // }
  2678. // }
  2679. // }
  2680. // len: len = 0x28 (8 bytes)
  2681. // }
  2682. // }
  2683. // }
  2684. // vlen: len = 0x1 (8 bytes)
  2685. // ctrl: nil
  2686. // ctrllen: bytesize = 0x0 (8 bytes)
  2687. // f: send_flags = 0x0 (4 bytes)
  2688. // pad = 0x0 (4 bytes)
  2689. // }
  2690. // }
  2691. // f: send_flags = 0x0 (8 bytes)
  2692. // ]
  2693. NONFAILING(*(uint64_t*)0x200000006440 = 0);
  2694. NONFAILING(*(uint32_t*)0x200000006448 = 0);
  2695. NONFAILING(*(uint64_t*)0x200000006450 = 0x2000000063c0);
  2696. NONFAILING(*(uint64_t*)0x2000000063c0 = 0x200000000180);
  2697. NONFAILING(
  2698. memcpy((void*)0x200000000180,
  2699. "\x28\x00\x00\x00\x10\x00\x01\x00\x00\x00\x00\x00\x00\x00\x00"
  2700. "\x00\x08\x00\x00\x00\x1f\x04\x79\x5e\x64\x24\xfc\xcf\x2b\x93"
  2701. "\xac\x36\xc5\xfc\x5e\x46\xca\x1b\xed\x01\x91\xce\xb0\xdf\xb6"
  2702. "\xb9\x23\xce\x64\x97\xdc\xce\x31\x43\x4f\x02\x67\x64\xcb\x87"
  2703. "\x2d\xc7\x97\x9e\x9a\x26\xdd\x46\x3c\xfd\xc0\xb0\x0e\xfa\x93"
  2704. "\x83\xb0\xcd\x2f\x19\x72\x26\x88\x80\xde\x56\x35\x21\x6a\xd6"
  2705. "\xb8\x27\xf4\x1f\x12\xdc\x42\xb9\xc5\xd9\xa8\x4c\xe8",
  2706. 103));
  2707. NONFAILING(*(uint32_t*)0x2000000001e7 = 0);
  2708. NONFAILING(memcpy(
  2709. (void*)0x2000000001eb,
  2710. "\x08\x00\x00\x00\xe0\x00\x00\x02\x05\x00\x35\x80\xe7\x00\x00\x00",
  2711. 16));
  2712. NONFAILING(*(uint64_t*)0x2000000063c8 = 0x28);
  2713. NONFAILING(*(uint64_t*)0x200000006458 = 1);
  2714. NONFAILING(*(uint64_t*)0x200000006460 = 0);
  2715. NONFAILING(*(uint64_t*)0x200000006468 = 0);
  2716. NONFAILING(*(uint32_t*)0x200000006470 = 0);
  2717. syscall(__NR_sendmsg, /*fd=*/r[3], /*msg=*/0x200000006440ul, /*f=*/0ul);
  2718. break;
  2719. }
  2720. }
  2721. int main(void)
  2722. {
  2723. syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  2724. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
  2725. /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  2726. syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul,
  2727. /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
  2728. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
  2729. /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  2730. syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  2731. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
  2732. /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  2733. setup_sysctl();
  2734. const char* reason;
  2735. (void)reason;
  2736. install_segv_handler();
  2737. for (procid = 0; procid < 4; procid++) {
  2738. if (fork() == 0) {
  2739. use_temporary_dir();
  2740. do_sandbox_none();
  2741. }
  2742. }
  2743. sleep(1000000);
  2744. return 0;
  2745. }
  2746.  
Advertisement
Comments
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
  • User was banned
Add Comment
Please, Sign In to add comment