Tang-ZW

Untitled

Jun 29th, 2026
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 43.49 KB | None | 0 0
  1. // autogenerated by syzkaller (https://github.com/google/syzkaller)
  2.  
  3. #define _GNU_SOURCE
  4.  
  5. #include <dirent.h>
  6. #include <endian.h>
  7. #include <errno.h>
  8. #include <fcntl.h>
  9. #include <pthread.h>
  10. #include <sched.h>
  11. #include <setjmp.h>
  12. #include <signal.h>
  13. #include <stdarg.h>
  14. #include <stdbool.h>
  15. #include <stddef.h>
  16. #include <stdint.h>
  17. #include <stdio.h>
  18. #include <stdlib.h>
  19. #include <string.h>
  20. #include <sys/ioctl.h>
  21. #include <sys/mman.h>
  22. #include <sys/mount.h>
  23. #include <sys/prctl.h>
  24. #include <sys/resource.h>
  25. #include <sys/stat.h>
  26. #include <sys/syscall.h>
  27. #include <sys/time.h>
  28. #include <sys/types.h>
  29. #include <sys/wait.h>
  30. #include <time.h>
  31. #include <unistd.h>
  32.  
  33. #include <linux/capability.h>
  34. #include <linux/futex.h>
  35. #include <linux/loop.h>
  36.  
  37. #ifndef __NR_memfd_create
  38. #define __NR_memfd_create 319
  39. #endif
  40. #ifndef __NR_pwritev2
  41. #define __NR_pwritev2 328
  42. #endif
  43.  
  44. static unsigned long long procid;
  45.  
  46. static void sleep_ms(uint64_t ms)
  47. {
  48. usleep(ms * 1000);
  49. }
  50.  
  51. static uint64_t current_time_ms(void)
  52. {
  53. struct timespec ts;
  54. if (clock_gettime(CLOCK_MONOTONIC, &ts))
  55. exit(1);
  56. return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000;
  57. }
  58.  
  59. static void use_temporary_dir(void)
  60. {
  61. char tmpdir_template[] = "./syzkaller.XXXXXX";
  62. char* tmpdir = mkdtemp(tmpdir_template);
  63. if (!tmpdir)
  64. exit(1);
  65. if (chmod(tmpdir, 0777))
  66. exit(1);
  67. if (chdir(tmpdir))
  68. exit(1);
  69. }
  70.  
  71. static void thread_start(void* (*fn)(void*), void* arg)
  72. {
  73. pthread_t th;
  74. pthread_attr_t attr;
  75. pthread_attr_init(&attr);
  76. pthread_attr_setstacksize(&attr, 128 << 10);
  77. int i = 0;
  78. for (; i < 100; i++) {
  79. if (pthread_create(&th, &attr, fn, arg) == 0) {
  80. pthread_attr_destroy(&attr);
  81. return;
  82. }
  83. if (errno == EAGAIN) {
  84. usleep(50);
  85. continue;
  86. }
  87. break;
  88. }
  89. exit(1);
  90. }
  91.  
  92. typedef struct {
  93. int state;
  94. } event_t;
  95.  
  96. static void event_init(event_t* ev)
  97. {
  98. ev->state = 0;
  99. }
  100.  
  101. static void event_reset(event_t* ev)
  102. {
  103. ev->state = 0;
  104. }
  105.  
  106. static void event_set(event_t* ev)
  107. {
  108. if (ev->state)
  109. exit(1);
  110. __atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
  111. syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG, 1000000);
  112. }
  113.  
  114. static void event_wait(event_t* ev)
  115. {
  116. while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
  117. syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
  118. }
  119.  
  120. static int event_isset(event_t* ev)
  121. {
  122. return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE);
  123. }
  124.  
  125. static int event_timedwait(event_t* ev, uint64_t timeout)
  126. {
  127. uint64_t start = current_time_ms();
  128. uint64_t now = start;
  129. for (;;) {
  130. uint64_t remain = timeout - (now - start);
  131. struct timespec ts;
  132. ts.tv_sec = remain / 1000;
  133. ts.tv_nsec = (remain % 1000) * 1000 * 1000;
  134. syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts);
  135. if (__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
  136. return 1;
  137. now = current_time_ms();
  138. if (now - start > timeout)
  139. return 0;
  140. }
  141. }
  142.  
  143. static bool write_file(const char* file, const char* what, ...)
  144. {
  145. char buf[1024];
  146. va_list args;
  147. va_start(args, what);
  148. vsnprintf(buf, sizeof(buf), what, args);
  149. va_end(args);
  150. buf[sizeof(buf) - 1] = 0;
  151. int len = strlen(buf);
  152. int fd = open(file, O_WRONLY | O_CLOEXEC);
  153. if (fd == -1)
  154. return false;
  155. if (write(fd, buf, len) != len) {
  156. int err = errno;
  157. close(fd);
  158. errno = err;
  159. return false;
  160. }
  161. close(fd);
  162. return true;
  163. }
  164.  
  165. //% This code is derived from puff.{c,h}, found in the zlib development. The
  166. //% original files come with the following copyright notice:
  167.  
  168. //% Copyright (C) 2002-2013 Mark Adler, all rights reserved
  169. //% version 2.3, 21 Jan 2013
  170. //% This software is provided 'as-is', without any express or implied
  171. //% warranty. In no event will the author be held liable for any damages
  172. //% arising from the use of this software.
  173. //% Permission is granted to anyone to use this software for any purpose,
  174. //% including commercial applications, and to alter it and redistribute it
  175. //% freely, subject to the following restrictions:
  176. //% 1. The origin of this software must not be misrepresented; you must not
  177. //% claim that you wrote the original software. If you use this software
  178. //% in a product, an acknowledgment in the product documentation would be
  179. //% appreciated but is not required.
  180. //% 2. Altered source versions must be plainly marked as such, and must not be
  181. //% misrepresented as being the original software.
  182. //% 3. This notice may not be removed or altered from any source distribution.
  183. //% Mark Adler [email protected]
  184.  
  185. //% BEGIN CODE DERIVED FROM puff.{c,h}
  186.  
  187. #define MAXBITS 15
  188. #define MAXLCODES 286
  189. #define MAXDCODES 30
  190. #define MAXCODES (MAXLCODES + MAXDCODES)
  191. #define FIXLCODES 288
  192.  
  193. struct puff_state {
  194. unsigned char* out;
  195. unsigned long outlen;
  196. unsigned long outcnt;
  197. const unsigned char* in;
  198. unsigned long inlen;
  199. unsigned long incnt;
  200. int bitbuf;
  201. int bitcnt;
  202. jmp_buf env;
  203. };
  204. static int puff_bits(struct puff_state* s, int need)
  205. {
  206. long val = s->bitbuf;
  207. while (s->bitcnt < need) {
  208. if (s->incnt == s->inlen)
  209. longjmp(s->env, 1);
  210. val |= (long)(s->in[s->incnt++]) << s->bitcnt;
  211. s->bitcnt += 8;
  212. }
  213. s->bitbuf = (int)(val >> need);
  214. s->bitcnt -= need;
  215. return (int)(val & ((1L << need) - 1));
  216. }
  217. static int puff_stored(struct puff_state* s)
  218. {
  219. s->bitbuf = 0;
  220. s->bitcnt = 0;
  221. if (s->incnt + 4 > s->inlen)
  222. return 2;
  223. unsigned len = s->in[s->incnt++];
  224. len |= s->in[s->incnt++] << 8;
  225. if (s->in[s->incnt++] != (~len & 0xff) ||
  226. s->in[s->incnt++] != ((~len >> 8) & 0xff))
  227. return -2;
  228. if (s->incnt + len > s->inlen)
  229. return 2;
  230. if (s->outcnt + len > s->outlen)
  231. return 1;
  232. for (; len--; s->outcnt++, s->incnt++) {
  233. if (s->in[s->incnt])
  234. s->out[s->outcnt] = s->in[s->incnt];
  235. }
  236. return 0;
  237. }
  238. struct puff_huffman {
  239. short* count;
  240. short* symbol;
  241. };
  242. static int puff_decode(struct puff_state* s, const struct puff_huffman* h)
  243. {
  244. int first = 0;
  245. int index = 0;
  246. int bitbuf = s->bitbuf;
  247. int left = s->bitcnt;
  248. int code = first = index = 0;
  249. int len = 1;
  250. short* next = h->count + 1;
  251. while (1) {
  252. while (left--) {
  253. code |= bitbuf & 1;
  254. bitbuf >>= 1;
  255. int count = *next++;
  256. if (code - count < first) {
  257. s->bitbuf = bitbuf;
  258. s->bitcnt = (s->bitcnt - len) & 7;
  259. return h->symbol[index + (code - first)];
  260. }
  261. index += count;
  262. first += count;
  263. first <<= 1;
  264. code <<= 1;
  265. len++;
  266. }
  267. left = (MAXBITS + 1) - len;
  268. if (left == 0)
  269. break;
  270. if (s->incnt == s->inlen)
  271. longjmp(s->env, 1);
  272. bitbuf = s->in[s->incnt++];
  273. if (left > 8)
  274. left = 8;
  275. }
  276. return -10;
  277. }
  278. static int puff_construct(struct puff_huffman* h, const short* length, int n)
  279. {
  280. int len;
  281. for (len = 0; len <= MAXBITS; len++)
  282. h->count[len] = 0;
  283. int symbol;
  284. for (symbol = 0; symbol < n; symbol++)
  285. (h->count[length[symbol]])++;
  286. if (h->count[0] == n)
  287. return 0;
  288. int left = 1;
  289. for (len = 1; len <= MAXBITS; len++) {
  290. left <<= 1;
  291. left -= h->count[len];
  292. if (left < 0)
  293. return left;
  294. }
  295. short offs[MAXBITS + 1];
  296. offs[1] = 0;
  297. for (len = 1; len < MAXBITS; len++)
  298. offs[len + 1] = offs[len] + h->count[len];
  299. for (symbol = 0; symbol < n; symbol++)
  300. if (length[symbol] != 0)
  301. h->symbol[offs[length[symbol]]++] = symbol;
  302. return left;
  303. }
  304. static int puff_codes(struct puff_state* s, const struct puff_huffman* lencode,
  305. const struct puff_huffman* distcode)
  306. {
  307. static const short lens[29] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13,
  308. 15, 17, 19, 23, 27, 31, 35, 43, 51, 59,
  309. 67, 83, 99, 115, 131, 163, 195, 227, 258};
  310. static const short lext[29] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2,
  311. 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0};
  312. static const short dists[30] = {
  313. 1, 2, 3, 4, 5, 7, 9, 13, 17, 25,
  314. 33, 49, 65, 97, 129, 193, 257, 385, 513, 769,
  315. 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577};
  316. static const short dext[30] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3,
  317. 4, 4, 5, 5, 6, 6, 7, 7, 8, 8,
  318. 9, 9, 10, 10, 11, 11, 12, 12, 13, 13};
  319. int symbol;
  320. do {
  321. symbol = puff_decode(s, lencode);
  322. if (symbol < 0)
  323. return symbol;
  324. if (symbol < 256) {
  325. if (s->outcnt == s->outlen)
  326. return 1;
  327. if (symbol)
  328. s->out[s->outcnt] = symbol;
  329. s->outcnt++;
  330. } else if (symbol > 256) {
  331. symbol -= 257;
  332. if (symbol >= 29)
  333. return -10;
  334. int len = lens[symbol] + puff_bits(s, lext[symbol]);
  335. symbol = puff_decode(s, distcode);
  336. if (symbol < 0)
  337. return symbol;
  338. unsigned dist = dists[symbol] + puff_bits(s, dext[symbol]);
  339. if (dist > s->outcnt)
  340. return -11;
  341. if (s->outcnt + len > s->outlen)
  342. return 1;
  343. while (len--) {
  344. if (dist <= s->outcnt && s->out[s->outcnt - dist])
  345. s->out[s->outcnt] = s->out[s->outcnt - dist];
  346. s->outcnt++;
  347. }
  348. }
  349. } while (symbol != 256);
  350. return 0;
  351. }
  352. static int puff_fixed(struct puff_state* s)
  353. {
  354. static int virgin = 1;
  355. static short lencnt[MAXBITS + 1], lensym[FIXLCODES];
  356. static short distcnt[MAXBITS + 1], distsym[MAXDCODES];
  357. static struct puff_huffman lencode, distcode;
  358. if (virgin) {
  359. lencode.count = lencnt;
  360. lencode.symbol = lensym;
  361. distcode.count = distcnt;
  362. distcode.symbol = distsym;
  363. short lengths[FIXLCODES];
  364. int symbol;
  365. for (symbol = 0; symbol < 144; symbol++)
  366. lengths[symbol] = 8;
  367. for (; symbol < 256; symbol++)
  368. lengths[symbol] = 9;
  369. for (; symbol < 280; symbol++)
  370. lengths[symbol] = 7;
  371. for (; symbol < FIXLCODES; symbol++)
  372. lengths[symbol] = 8;
  373. puff_construct(&lencode, lengths, FIXLCODES);
  374. for (symbol = 0; symbol < MAXDCODES; symbol++)
  375. lengths[symbol] = 5;
  376. puff_construct(&distcode, lengths, MAXDCODES);
  377. virgin = 0;
  378. }
  379. return puff_codes(s, &lencode, &distcode);
  380. }
  381. static int puff_dynamic(struct puff_state* s)
  382. {
  383. static const short order[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5,
  384. 11, 4, 12, 3, 13, 2, 14, 1, 15};
  385. int nlen = puff_bits(s, 5) + 257;
  386. int ndist = puff_bits(s, 5) + 1;
  387. int ncode = puff_bits(s, 4) + 4;
  388. if (nlen > MAXLCODES || ndist > MAXDCODES)
  389. return -3;
  390. short lengths[MAXCODES];
  391. int index;
  392. for (index = 0; index < ncode; index++)
  393. lengths[order[index]] = puff_bits(s, 3);
  394. for (; index < 19; index++)
  395. lengths[order[index]] = 0;
  396. short lencnt[MAXBITS + 1], lensym[MAXLCODES];
  397. struct puff_huffman lencode = {lencnt, lensym};
  398. int err = puff_construct(&lencode, lengths, 19);
  399. if (err != 0)
  400. return -4;
  401. index = 0;
  402. while (index < nlen + ndist) {
  403. int symbol;
  404. int len;
  405. symbol = puff_decode(s, &lencode);
  406. if (symbol < 0)
  407. return symbol;
  408. if (symbol < 16)
  409. lengths[index++] = symbol;
  410. else {
  411. len = 0;
  412. if (symbol == 16) {
  413. if (index == 0)
  414. return -5;
  415. len = lengths[index - 1];
  416. symbol = 3 + puff_bits(s, 2);
  417. } else if (symbol == 17)
  418. symbol = 3 + puff_bits(s, 3);
  419. else
  420. symbol = 11 + puff_bits(s, 7);
  421. if (index + symbol > nlen + ndist)
  422. return -6;
  423. while (symbol--)
  424. lengths[index++] = len;
  425. }
  426. }
  427. if (lengths[256] == 0)
  428. return -9;
  429. err = puff_construct(&lencode, lengths, nlen);
  430. if (err && (err < 0 || nlen != lencode.count[0] + lencode.count[1]))
  431. return -7;
  432. short distcnt[MAXBITS + 1], distsym[MAXDCODES];
  433. struct puff_huffman distcode = {distcnt, distsym};
  434. err = puff_construct(&distcode, lengths + nlen, ndist);
  435. if (err && (err < 0 || ndist != distcode.count[0] + distcode.count[1]))
  436. return -8;
  437. return puff_codes(s, &lencode, &distcode);
  438. }
  439. static int puff(unsigned char* dest, unsigned long* destlen,
  440. const unsigned char* source, unsigned long sourcelen)
  441. {
  442. struct puff_state s = {
  443. .out = dest,
  444. .outlen = *destlen,
  445. .outcnt = 0,
  446. .in = source,
  447. .inlen = sourcelen,
  448. .incnt = 0,
  449. .bitbuf = 0,
  450. .bitcnt = 0,
  451. };
  452. int err;
  453. if (setjmp(s.env) != 0)
  454. err = 2;
  455. else {
  456. int last;
  457. do {
  458. last = puff_bits(&s, 1);
  459. int type = puff_bits(&s, 2);
  460. err = type == 0 ? puff_stored(&s)
  461. : (type == 1 ? puff_fixed(&s)
  462. : (type == 2 ? puff_dynamic(&s) : -1));
  463. if (err != 0)
  464. break;
  465. } while (!last);
  466. }
  467. *destlen = s.outcnt;
  468. return err;
  469. }
  470.  
  471. //% END CODE DERIVED FROM puff.{c,h}
  472.  
  473. #define ZLIB_HEADER_WIDTH 2
  474.  
  475. static int puff_zlib_to_file(const unsigned char* source,
  476. unsigned long sourcelen, int dest_fd)
  477. {
  478. if (sourcelen < ZLIB_HEADER_WIDTH)
  479. return 0;
  480. source += ZLIB_HEADER_WIDTH;
  481. sourcelen -= ZLIB_HEADER_WIDTH;
  482. const unsigned long max_destlen = 132 << 20;
  483. void* ret = mmap(0, max_destlen, PROT_WRITE | PROT_READ,
  484. MAP_PRIVATE | MAP_ANON, -1, 0);
  485. if (ret == MAP_FAILED)
  486. return -1;
  487. unsigned char* dest = (unsigned char*)ret;
  488. unsigned long destlen = max_destlen;
  489. int err = puff(dest, &destlen, source, sourcelen);
  490. if (err) {
  491. munmap(dest, max_destlen);
  492. errno = -err;
  493. return -1;
  494. }
  495. if (write(dest_fd, dest, destlen) != (ssize_t)destlen) {
  496. munmap(dest, max_destlen);
  497. return -1;
  498. }
  499. return munmap(dest, max_destlen);
  500. }
  501.  
  502. static int setup_loop_device(unsigned char* data, unsigned long size,
  503. const char* loopname, int* loopfd_p)
  504. {
  505. int err = 0, loopfd = -1;
  506. int memfd = syscall(__NR_memfd_create, "syzkaller", 0);
  507. if (memfd == -1) {
  508. err = errno;
  509. goto error;
  510. }
  511. if (puff_zlib_to_file(data, size, memfd)) {
  512. err = errno;
  513. goto error_close_memfd;
  514. }
  515. loopfd = open(loopname, O_RDWR);
  516. if (loopfd == -1) {
  517. err = errno;
  518. goto error_close_memfd;
  519. }
  520. if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
  521. if (errno != EBUSY) {
  522. err = errno;
  523. goto error_close_loop;
  524. }
  525. ioctl(loopfd, LOOP_CLR_FD, 0);
  526. usleep(1000);
  527. if (ioctl(loopfd, LOOP_SET_FD, memfd)) {
  528. err = errno;
  529. goto error_close_loop;
  530. }
  531. }
  532. close(memfd);
  533. *loopfd_p = loopfd;
  534. return 0;
  535.  
  536. error_close_loop:
  537. close(loopfd);
  538. error_close_memfd:
  539. close(memfd);
  540. error:
  541. errno = err;
  542. return -1;
  543. }
  544.  
  545. static void reset_loop_device(const char* loopname)
  546. {
  547. int loopfd = open(loopname, O_RDWR);
  548. if (loopfd == -1) {
  549. return;
  550. }
  551. if (ioctl(loopfd, LOOP_CLR_FD, 0)) {
  552. }
  553. close(loopfd);
  554. }
  555.  
  556. static long syz_mount_image(volatile long fsarg, volatile long dir,
  557. volatile long flags, volatile long optsarg,
  558. volatile long change_dir,
  559. volatile unsigned long size, volatile long image)
  560. {
  561. unsigned char* data = (unsigned char*)image;
  562. int res = -1, err = 0, need_loop_device = !!size;
  563. char* mount_opts = (char*)optsarg;
  564. char* target = (char*)dir;
  565. char* fs = (char*)fsarg;
  566. char* source = NULL;
  567. char loopname[64];
  568. if (need_loop_device) {
  569. int loopfd;
  570. memset(loopname, 0, sizeof(loopname));
  571. snprintf(loopname, sizeof(loopname), "/dev/loop%llu", procid);
  572. if (setup_loop_device(data, size, loopname, &loopfd) == -1)
  573. return -1;
  574. close(loopfd);
  575. source = loopname;
  576. }
  577. mkdir(target, 0777);
  578. char opts[256];
  579. memset(opts, 0, sizeof(opts));
  580. if (strlen(mount_opts) > (sizeof(opts) - 32)) {
  581. }
  582. strncpy(opts, mount_opts, sizeof(opts) - 32);
  583. if (strcmp(fs, "iso9660") == 0) {
  584. flags |= MS_RDONLY;
  585. } else if (strncmp(fs, "ext", 3) == 0) {
  586. bool has_remount_ro = false;
  587. char* remount_ro_start = strstr(opts, "errors=remount-ro");
  588. if (remount_ro_start != NULL) {
  589. char after = *(remount_ro_start + strlen("errors=remount-ro"));
  590. char before = remount_ro_start == opts ? '\0' : *(remount_ro_start - 1);
  591. has_remount_ro = ((before == '\0' || before == ',') &&
  592. (after == '\0' || after == ','));
  593. }
  594. if (strstr(opts, "errors=panic") || !has_remount_ro)
  595. strcat(opts, ",errors=continue");
  596. } else if (strcmp(fs, "xfs") == 0) {
  597. strcat(opts, ",nouuid");
  598. } else if (strncmp(fs, "gfs2", 4) == 0 &&
  599. (strstr(opts, "errors=panic") || strstr(opts, "debug"))) {
  600. strcat(opts, ",errors=withdraw");
  601. }
  602. res = mount(source, target, fs, flags, opts);
  603. if (res == -1) {
  604. err = errno;
  605. goto error_clear_loop;
  606. }
  607. res = open(target, O_RDONLY | O_DIRECTORY);
  608. if (res == -1) {
  609. err = errno;
  610. goto error_clear_loop;
  611. }
  612. if (change_dir) {
  613. res = chdir(target);
  614. if (res == -1) {
  615. err = errno;
  616. }
  617. }
  618.  
  619. error_clear_loop:
  620. if (need_loop_device)
  621. reset_loop_device(loopname);
  622. errno = err;
  623. return res;
  624. }
  625.  
  626. static void setup_gadgetfs();
  627. static void setup_binderfs();
  628. static void setup_fusectl();
  629. static void sandbox_common_mount_tmpfs(void)
  630. {
  631. write_file("/proc/sys/fs/mount-max", "100000");
  632. if (mkdir("./syz-tmp", 0777))
  633. exit(1);
  634. if (mount("", "./syz-tmp", "tmpfs", 0, NULL))
  635. exit(1);
  636. if (mkdir("./syz-tmp/newroot", 0777))
  637. exit(1);
  638. if (mkdir("./syz-tmp/newroot/dev", 0700))
  639. exit(1);
  640. unsigned bind_mount_flags = MS_BIND | MS_REC | MS_PRIVATE;
  641. if (mount("/dev", "./syz-tmp/newroot/dev", NULL, bind_mount_flags, NULL))
  642. exit(1);
  643. if (mkdir("./syz-tmp/newroot/proc", 0700))
  644. exit(1);
  645. if (mount("syz-proc", "./syz-tmp/newroot/proc", "proc", 0, NULL))
  646. exit(1);
  647. if (mkdir("./syz-tmp/newroot/selinux", 0700))
  648. exit(1);
  649. const char* selinux_path = "./syz-tmp/newroot/selinux";
  650. if (mount("/selinux", selinux_path, NULL, bind_mount_flags, NULL)) {
  651. if (errno != ENOENT)
  652. exit(1);
  653. if (mount("/sys/fs/selinux", selinux_path, NULL, bind_mount_flags, NULL) &&
  654. errno != ENOENT)
  655. exit(1);
  656. }
  657. if (mkdir("./syz-tmp/newroot/sys", 0700))
  658. exit(1);
  659. if (mount("/sys", "./syz-tmp/newroot/sys", 0, bind_mount_flags, NULL))
  660. exit(1);
  661. if (mount("/sys/kernel/debug", "./syz-tmp/newroot/sys/kernel/debug", NULL,
  662. bind_mount_flags, NULL) &&
  663. errno != ENOENT)
  664. exit(1);
  665. if (mount("/sys/fs/smackfs", "./syz-tmp/newroot/sys/fs/smackfs", NULL,
  666. bind_mount_flags, NULL) &&
  667. errno != ENOENT)
  668. exit(1);
  669. if (mount("/proc/sys/fs/binfmt_misc",
  670. "./syz-tmp/newroot/proc/sys/fs/binfmt_misc", NULL, bind_mount_flags,
  671. NULL) &&
  672. errno != ENOENT)
  673. exit(1);
  674. if (mkdir("./syz-tmp/newroot/syz-inputs", 0700))
  675. exit(1);
  676. if (mount("/syz-inputs", "./syz-tmp/newroot/syz-inputs", NULL,
  677. bind_mount_flags | MS_RDONLY, NULL) &&
  678. errno != ENOENT)
  679. exit(1);
  680. if (mkdir("./syz-tmp/pivot", 0777))
  681. exit(1);
  682. if (syscall(SYS_pivot_root, "./syz-tmp", "./syz-tmp/pivot")) {
  683. if (chdir("./syz-tmp"))
  684. exit(1);
  685. } else {
  686. if (chdir("/"))
  687. exit(1);
  688. if (umount2("./pivot", MNT_DETACH))
  689. exit(1);
  690. }
  691. if (chroot("./newroot"))
  692. exit(1);
  693. if (chdir("/"))
  694. exit(1);
  695. setup_gadgetfs();
  696. setup_binderfs();
  697. setup_fusectl();
  698. }
  699.  
  700. static void setup_gadgetfs()
  701. {
  702. if (mkdir("/dev/gadgetfs", 0777)) {
  703. }
  704. if (mount("gadgetfs", "/dev/gadgetfs", "gadgetfs", 0, NULL)) {
  705. }
  706. }
  707.  
  708. static void setup_fusectl()
  709. {
  710. if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) {
  711. }
  712. }
  713.  
  714. static void setup_binderfs()
  715. {
  716. if (mkdir("/dev/binderfs", 0777)) {
  717. }
  718. if (mount("binder", "/dev/binderfs", "binder", 0, NULL)) {
  719. }
  720. }
  721.  
  722. static void loop();
  723.  
  724. static void sandbox_common()
  725. {
  726. prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  727. if (getppid() == 1)
  728. exit(1);
  729. struct rlimit rlim;
  730. rlim.rlim_cur = rlim.rlim_max = (200 << 20);
  731. setrlimit(RLIMIT_AS, &rlim);
  732. rlim.rlim_cur = rlim.rlim_max = 32 << 20;
  733. setrlimit(RLIMIT_MEMLOCK, &rlim);
  734. rlim.rlim_cur = rlim.rlim_max = 136 << 20;
  735. setrlimit(RLIMIT_FSIZE, &rlim);
  736. rlim.rlim_cur = rlim.rlim_max = 1 << 20;
  737. setrlimit(RLIMIT_STACK, &rlim);
  738. rlim.rlim_cur = rlim.rlim_max = 128 << 20;
  739. setrlimit(RLIMIT_CORE, &rlim);
  740. rlim.rlim_cur = rlim.rlim_max = 256;
  741. setrlimit(RLIMIT_NOFILE, &rlim);
  742. if (unshare(CLONE_NEWNS)) {
  743. }
  744. if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL)) {
  745. }
  746. if (unshare(CLONE_NEWIPC)) {
  747. }
  748. if (unshare(0x02000000)) {
  749. }
  750. if (unshare(CLONE_NEWUTS)) {
  751. }
  752. if (unshare(CLONE_SYSVSEM)) {
  753. }
  754. typedef struct {
  755. const char* name;
  756. const char* value;
  757. } sysctl_t;
  758. static const sysctl_t sysctls[] = {
  759. {"/proc/sys/kernel/shmmax", "16777216"},
  760. {"/proc/sys/kernel/shmall", "536870912"},
  761. {"/proc/sys/kernel/shmmni", "1024"},
  762. {"/proc/sys/kernel/msgmax", "8192"},
  763. {"/proc/sys/kernel/msgmni", "1024"},
  764. {"/proc/sys/kernel/msgmnb", "1024"},
  765. {"/proc/sys/kernel/sem", "1024 1048576 500 1024"},
  766. };
  767. unsigned i;
  768. for (i = 0; i < sizeof(sysctls) / sizeof(sysctls[0]); i++)
  769. write_file(sysctls[i].name, sysctls[i].value);
  770. }
  771.  
  772. static int wait_for_loop(int pid)
  773. {
  774. if (pid < 0)
  775. exit(1);
  776. int status = 0;
  777. while (waitpid(-1, &status, __WALL) != pid) {
  778. }
  779. return WEXITSTATUS(status);
  780. }
  781.  
  782. static void drop_caps(void)
  783. {
  784. struct __user_cap_header_struct cap_hdr = {};
  785. struct __user_cap_data_struct cap_data[2] = {};
  786. cap_hdr.version = _LINUX_CAPABILITY_VERSION_3;
  787. cap_hdr.pid = getpid();
  788. if (syscall(SYS_capget, &cap_hdr, &cap_data))
  789. exit(1);
  790. const int drop = (1 << CAP_SYS_PTRACE) | (1 << CAP_SYS_NICE);
  791. cap_data[0].effective &= ~drop;
  792. cap_data[0].permitted &= ~drop;
  793. cap_data[0].inheritable &= ~drop;
  794. if (syscall(SYS_capset, &cap_hdr, &cap_data))
  795. exit(1);
  796. }
  797.  
  798. static int real_uid;
  799. static int real_gid;
  800. __attribute__((aligned(64 << 10))) static char sandbox_stack[1 << 20];
  801.  
  802. static int namespace_sandbox_proc(void* arg)
  803. {
  804. sandbox_common();
  805. write_file("/proc/self/setgroups", "deny");
  806. if (!write_file("/proc/self/uid_map", "0 %d 1\n", real_uid))
  807. exit(1);
  808. if (!write_file("/proc/self/gid_map", "0 %d 1\n", real_gid))
  809. exit(1);
  810. if (unshare(CLONE_NEWNET))
  811. exit(1);
  812. write_file("/proc/sys/net/ipv4/ping_group_range", "0 65535");
  813. sandbox_common_mount_tmpfs();
  814. drop_caps();
  815. loop();
  816. exit(1);
  817. }
  818.  
  819. static int do_sandbox_namespace(void)
  820. {
  821. real_uid = getuid();
  822. real_gid = getgid();
  823. mprotect(sandbox_stack, 4096, PROT_NONE);
  824. int pid =
  825. clone(namespace_sandbox_proc, &sandbox_stack[sizeof(sandbox_stack) - 64],
  826. CLONE_NEWUSER | CLONE_NEWPID, 0);
  827. return wait_for_loop(pid);
  828. }
  829.  
  830. #define FS_IOC_SETFLAGS _IOW('f', 2, long)
  831. static void remove_dir(const char* dir)
  832. {
  833. int iter = 0;
  834. DIR* dp = 0;
  835. const int umount_flags = MNT_FORCE | UMOUNT_NOFOLLOW;
  836.  
  837. retry:
  838. while (umount2(dir, umount_flags) == 0) {
  839. }
  840. dp = opendir(dir);
  841. if (dp == NULL) {
  842. if (errno == EMFILE) {
  843. exit(1);
  844. }
  845. exit(1);
  846. }
  847. struct dirent* ep = 0;
  848. while ((ep = readdir(dp))) {
  849. if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0)
  850. continue;
  851. char filename[FILENAME_MAX];
  852. snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name);
  853. while (umount2(filename, umount_flags) == 0) {
  854. }
  855. struct stat st;
  856. if (lstat(filename, &st))
  857. exit(1);
  858. if (S_ISDIR(st.st_mode)) {
  859. remove_dir(filename);
  860. continue;
  861. }
  862. int i;
  863. for (i = 0;; i++) {
  864. if (unlink(filename) == 0)
  865. break;
  866. if (errno == EPERM) {
  867. int fd = open(filename, O_RDONLY);
  868. if (fd != -1) {
  869. long flags = 0;
  870. if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) {
  871. }
  872. close(fd);
  873. continue;
  874. }
  875. }
  876. if (errno == EROFS) {
  877. break;
  878. }
  879. if (errno != EBUSY || i > 100)
  880. exit(1);
  881. if (umount2(filename, umount_flags))
  882. exit(1);
  883. }
  884. }
  885. closedir(dp);
  886. for (int i = 0;; i++) {
  887. if (rmdir(dir) == 0)
  888. break;
  889. if (i < 100) {
  890. if (errno == EPERM) {
  891. int fd = open(dir, O_RDONLY);
  892. if (fd != -1) {
  893. long flags = 0;
  894. if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) {
  895. }
  896. close(fd);
  897. continue;
  898. }
  899. }
  900. if (errno == EROFS) {
  901. break;
  902. }
  903. if (errno == EBUSY) {
  904. if (umount2(dir, umount_flags))
  905. exit(1);
  906. continue;
  907. }
  908. if (errno == ENOTEMPTY) {
  909. if (iter < 100) {
  910. iter++;
  911. goto retry;
  912. }
  913. }
  914. }
  915. exit(1);
  916. }
  917. }
  918.  
  919. static void kill_and_wait(int pid, int* status)
  920. {
  921. kill(-pid, SIGKILL);
  922. kill(pid, SIGKILL);
  923. for (int i = 0; i < 100; i++) {
  924. if (waitpid(-1, status, WNOHANG | __WALL) == pid)
  925. return;
  926. usleep(1000);
  927. }
  928. DIR* dir = opendir("/sys/fs/fuse/connections");
  929. if (dir) {
  930. for (;;) {
  931. struct dirent* ent = readdir(dir);
  932. if (!ent)
  933. break;
  934. if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
  935. continue;
  936. char abort[300];
  937. snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort",
  938. ent->d_name);
  939. int fd = open(abort, O_WRONLY);
  940. if (fd == -1) {
  941. continue;
  942. }
  943. if (write(fd, abort, 1) < 0) {
  944. }
  945. close(fd);
  946. }
  947. closedir(dir);
  948. } else {
  949. }
  950. while (waitpid(-1, status, __WALL) != pid) {
  951. }
  952. }
  953.  
  954. static void reset_loop()
  955. {
  956. char buf[64];
  957. snprintf(buf, sizeof(buf), "/dev/loop%llu", procid);
  958. int loopfd = open(buf, O_RDWR);
  959. if (loopfd != -1) {
  960. ioctl(loopfd, LOOP_CLR_FD, 0);
  961. close(loopfd);
  962. }
  963. }
  964.  
  965. static void setup_test()
  966. {
  967. prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
  968. setpgrp();
  969. write_file("/proc/self/oom_score_adj", "1000");
  970. if (symlink("/dev/binderfs", "./binderfs")) {
  971. }
  972. }
  973.  
  974. struct thread_t {
  975. int created, call;
  976. event_t ready, done;
  977. };
  978.  
  979. static struct thread_t threads[16];
  980. static void execute_call(int call);
  981. static int running;
  982.  
  983. static void* thr(void* arg)
  984. {
  985. struct thread_t* th = (struct thread_t*)arg;
  986. for (;;) {
  987. event_wait(&th->ready);
  988. event_reset(&th->ready);
  989. execute_call(th->call);
  990. __atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
  991. event_set(&th->done);
  992. }
  993. return 0;
  994. }
  995.  
  996. static void execute_one(void)
  997. {
  998. if (write(1, "executing program\n", sizeof("executing program\n") - 1)) {
  999. }
  1000. int i, call, thread;
  1001. for (call = 0; call < 9; call++) {
  1002. for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
  1003. thread++) {
  1004. struct thread_t* th = &threads[thread];
  1005. if (!th->created) {
  1006. th->created = 1;
  1007. event_init(&th->ready);
  1008. event_init(&th->done);
  1009. event_set(&th->done);
  1010. thread_start(thr, th);
  1011. }
  1012. if (!event_isset(&th->done))
  1013. continue;
  1014. event_reset(&th->done);
  1015. th->call = call;
  1016. __atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
  1017. event_set(&th->ready);
  1018. event_timedwait(&th->done, 50 + (call == 0 ? 4000 : 0));
  1019. break;
  1020. }
  1021. }
  1022. for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
  1023. sleep_ms(1);
  1024. }
  1025.  
  1026. static void execute_one(void);
  1027.  
  1028. #define WAIT_FLAGS __WALL
  1029.  
  1030. static void loop(void)
  1031. {
  1032. int iter = 0;
  1033. for (;; iter++) {
  1034. char cwdbuf[32];
  1035. sprintf(cwdbuf, "./%d", iter);
  1036. if (mkdir(cwdbuf, 0777))
  1037. exit(1);
  1038. reset_loop();
  1039. int pid = fork();
  1040. if (pid < 0)
  1041. exit(1);
  1042. if (pid == 0) {
  1043. if (chdir(cwdbuf))
  1044. exit(1);
  1045. setup_test();
  1046. execute_one();
  1047. exit(0);
  1048. }
  1049. int status = 0;
  1050. uint64_t start = current_time_ms();
  1051. for (;;) {
  1052. sleep_ms(10);
  1053. if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
  1054. break;
  1055. if (current_time_ms() - start < 5000)
  1056. continue;
  1057. kill_and_wait(pid, &status);
  1058. break;
  1059. }
  1060. remove_dir(cwdbuf);
  1061. }
  1062. }
  1063.  
  1064. uint64_t r[3] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff};
  1065.  
  1066. void execute_call(int call)
  1067. {
  1068. intptr_t res = 0;
  1069. switch (call) {
  1070. case 0:
  1071. // syz_mount_image$ext4 arguments: [
  1072. // fs: ptr[in, buffer] {
  1073. // buffer: {65 78 74 34 00} (length 0x5)
  1074. // }
  1075. // dir: ptr[in, buffer] {
  1076. // buffer: {2e 2f 66 69 6c 65 30 00} (length 0x8)
  1077. // }
  1078. // flags: mount_flags = 0x0 (8 bytes)
  1079. // opts: ptr[in, fs_options[ext4_options]] {
  1080. // fs_options[ext4_options] {
  1081. // elems: array[fs_opt_elem[ext4_options]] {
  1082. // }
  1083. // common: array[fs_opt_elem[fs_options_common]] {
  1084. // }
  1085. // null: const = 0x0 (1 bytes)
  1086. // }
  1087. // }
  1088. // chdir: int8 = 0x1 (1 bytes)
  1089. // size: len = 0x77f (8 bytes)
  1090. // img: ptr[in, buffer] {
  1091. // buffer: (compressed buffer with length 0x77f)
  1092. // }
  1093. // ]
  1094. // returns fd_dir
  1095. memcpy((void*)0x200000000780, "ext4\000", 5);
  1096. memcpy((void*)0x200000000240, "./file0\000", 8);
  1097. *(uint8_t*)0x200000000000 = 0;
  1098. memcpy(
  1099. (void*)0x200000000f40,
  1100. "\x78\x9c\xec\xdd\xdd\x6b\x5b\xe5\x1f\x00\xf0\xef\x49\xdf\xd6\x6e\xbf"
  1101. "\x5f\x2b\x08\x3a\xaf\x0a\x82\x16\xc6\x52\x3b\xeb\xa6\xe0\xc5\xc4\x0b"
  1102. "\x11\x1c\x0c\xf4\xda\xad\xa4\x59\x99\x4d\x9b\xd1\xa4\x63\x2d\x05\x37"
  1103. "\x44\xf0\x46\x50\xf1\x42\xd0\x9b\x5d\xfb\x32\xef\xbc\xf5\xe5\x56\xff"
  1104. "\x0b\x2f\x64\x63\x6a\x37\x9c\x78\x21\x95\x93\x9e\x74\xd9\x9a\x64\xe9"
  1105. "\xd6\x97\x8d\x7c\x3e\x70\x9a\xe7\xc9\x39\xe9\xf7\xf9\xe6\x39\x2f\xcf"
  1106. "\xc9\x39\x24\x01\x74\xad\xd1\xf4\x4f\x2e\xe2\x60\x44\x7c\x94\x44\x0c"
  1107. "\x67\xcf\x27\x11\xd1\x57\x2b\xf5\x46\x1c\x5f\x5f\xee\xd6\xea\x4a\x21"
  1108. "\x9d\x92\x58\x5b\x7b\xf3\x8f\xa4\xb6\xcc\xcd\xd5\x95\x42\x34\xbc\x26"
  1109. "\xb5\x3f\xab\x3c\x19\x11\x3f\xbe\x1f\x71\x28\xb7\x39\x6e\x65\x69\x79"
  1110. "\x76\xaa\x54\x2a\x2e\x64\xf5\xf1\xea\xdc\xb9\xf1\xca\xd2\xf2\xe1\xb3"
  1111. "\x73\x53\x33\xc5\x99\xe2\xfc\xd1\x89\xc9\xc9\x23\xc7\x5e\x38\x76\x74"
  1112. "\xfb\x72\xfd\xeb\x97\xe5\x03\xd7\x3e\x7e\xed\xd9\x6f\x8e\xff\xf3\xde"
  1113. "\x13\x57\x3e\xfc\x29\x89\xe3\x71\x20\x9b\xd7\x98\xc7\x76\x19\x8d\xd1"
  1114. "\xec\x3d\xe9\x4b\xdf\xc2\x3b\xbc\xba\xdd\xc1\xf6\x58\xb2\xd7\x0d\xe0"
  1115. "\xbe\xa4\x9b\x66\xcf\xfa\x56\x1e\x07\x63\x38\x7a\x6a\xa5\x16\x06\x77"
  1116. "\xb3\x65\x00\xc0\x4e\x79\x37\x22\xd6\x00\x80\x2e\x93\x38\xfe\x03\x40"
  1117. "\x97\xa9\x7f\x0e\x70\x73\x75\xa5\x50\x9f\xda\x7c\x5c\xd0\xe6\xe2\xc0"
  1118. "\xa3\xe9\xfa\x2b\x11\xb1\x6f\x3d\xff\xfa\xf5\xcd\xf5\x39\xbd\xd9\x35"
  1119. "\xbb\x7d\xb5\xeb\xa0\x43\x37\x93\x3b\x92\x4f\x22\x62\x64\x1b\xe2\x8f"
  1120. "\x46\xc4\x17\xdf\xbd\xfd\x55\x3a\xc5\x0e\x5d\x87\x04\x68\xe6\xe2\xa5"
  1121. "\x88\x38\x3d\x32\xba\x79\xff\x9f\x6c\xba\x67\x61\xab\x9e\xeb\x60\x99"
  1122. "\xd1\xbb\xea\x0d\xfb\xbf\xfe\x07\x0c\x0f\xdc\xc3\xf7\xe9\xf8\xe7\xc5"
  1123. "\x66\xe3\xbf\xdc\xc6\xf8\x27\x9a\x8c\x7f\x06\x9a\x6c\xbb\xf7\xa3\xcd"
  1124. "\xf6\x9f\xc9\x5d\xdd\x86\x30\x2d\xa5\xe3\xbf\x97\x1b\xee\x6d\xbb\xd5"
  1125. "\x90\x7f\x66\xa4\x27\xab\xfd\xaf\x36\xe6\xeb\x4b\xce\x9c\x2d\x15\xd3"
  1126. "\x7d\xdb\xff\x23\x62\x2c\xfa\x06\xd2\xfa\x44\x9b\x18\x63\x37\xfe\xbd"
  1127. "\xd1\x6a\x5e\xe3\xf8\xef\xcf\x4f\xde\xf9\x32\x8d\x9f\x3e\xde\x5e\x22"
  1128. "\x77\xb5\x77\xe0\xce\xd7\x4c\x4f\x55\xa7\x1e\x24\xe7\x46\xd7\x2f\x45"
  1129. "\x3c\xd5\xdb\x2c\xff\x64\xa3\xff\x93\x16\xe3\xdf\x93\x1d\xc6\x78\xfd"
  1130. "\xa5\x0f\x3e\x6f\x35\x2f\xcd\x3f\xcd\xb7\x3e\x6d\xce\x7f\x67\xad\x5d"
  1131. "\x8e\x78\xa6\x69\xff\xdf\xbe\xa3\x2d\x69\x7b\x7f\xe2\x78\x6d\x75\x18"
  1132. "\xaf\xaf\x14\x4d\x7c\xfb\xeb\x67\x43\xad\xe2\x37\xf6\x7f\x3a\xa5\xf1"
  1133. "\xeb\xe7\x02\xbb\x21\xed\xff\xa1\xf6\xf9\x8f\x24\x8d\xf7\x6b\x56\xb6"
  1134. "\x1e\xe3\xe7\xcb\xc3\x3f\xb4\x9a\x77\xef\xfc\x9b\xaf\xff\xfd\xc9\x5b"
  1135. "\xb5\x72\x7d\x90\x70\x61\xaa\x5a\x5d\x98\x88\xe8\x4f\xde\xd8\xfc\xfc"
  1136. "\x91\xdb\xaf\xad\xd7\xeb\xcb\xa7\xf9\x8f\x3d\xdd\xdf\x74\xfb\x6f\xb7"
  1137. "\xfe\xa7\xe7\x84\xa7\x3b\xcc\xbf\xf7\xda\xef\x5f\x6f\x54\xee\x3a\xf1"
  1138. "\x7e\x18\xfa\x7f\x7a\x4b\xfd\xbf\xf5\xc2\x95\x5b\xb3\x3d\xad\xe2\x77"
  1139. "\xd6\xff\x93\xb5\xd2\x58\xf6\x4c\x27\xfb\xbf\x4e\x1b\xf8\x20\xef\x1d"
  1140. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1141. "\x00\x00\x74\x2a\x17\x11\x07\x22\xc9\xe5\x37\xca\xb9\x5c\x3e\xbf\xfe"
  1142. "\x1b\xde\x8f\xc7\x50\xae\x54\xae\x54\x0f\x9d\x29\x2f\xce\x4f\x47\xed"
  1143. "\xb7\xb2\x47\xa2\x2f\x57\xff\xaa\xcb\xe1\x86\xef\x43\x9d\xc8\xbe\x0f"
  1144. "\xbf\x5e\x3f\x72\x57\xfd\xf9\x88\x78\x2c\x22\x3e\x1d\x18\xac\xd5\xf3"
  1145. "\x85\x72\x69\x7a\xaf\x93\x07\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1146. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1147. "\x00\x00\x00\x80\xcc\xfe\x16\xbf\xff\x9f\xfa\x6d\x60\xaf\x5b\x07\x00"
  1148. "\xec\x98\x7d\x7b\xdd\x00\x00\x60\xd7\x39\xfe\x03\x40\xf7\xd9\xda\xf1"
  1149. "\x7f\x70\xc7\xda\x01\x00\xec\x1e\xe7\xff\x00\xd0\x7d\x1c\xff\x01\xa0"
  1150. "\xfb\x38\xfe\x03\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1151. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1152. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1153. "\x00\x00\x00\xb0\xc3\x4e\x9e\x38\x91\x4e\x6b\x7f\xaf\xae\x14\xd2\xfa"
  1154. "\xf4\xf9\xa5\xc5\xd9\xf2\xf9\xc3\xd3\xc5\xca\x6c\x7e\x6e\xb1\x90\x2f"
  1155. "\x94\x17\xce\xe5\x67\xca\xe5\x99\x52\x31\x5f\x28\xcf\xb5\xfc\x47\x17"
  1156. "\xd7\x1f\x4a\xe5\xf2\xb9\xc9\x98\x5f\xbc\x30\x5e\x2d\x56\xaa\xe3\x95"
  1157. "\xa5\xe5\x53\x73\xe5\xc5\xf9\xea\xa9\xb3\x73\x53\x33\xc5\x53\xc5\xbe"
  1158. "\x5d\xcb\x0c\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1159. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1160. "\x3a\x57\x59\x5a\x9e\x9d\x2a\x95\x8a\x0b\x0a\xdd\x57\xe8\xc9\x56\x82"
  1161. "\x87\xa5\x3d\x0a\x0f\x51\xa1\x71\x2f\x31\xb8\x37\x3b\x27\x00\x00\x00"
  1162. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1163. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1164. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1165. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1166. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1167. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1168. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1169. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1170. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1171. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1172. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1173. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1174. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1175. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1176. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1177. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1178. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1179. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1180. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1181. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1182. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1183. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1184. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1185. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1186. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1187. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1188. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1189. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1190. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1191. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1192. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1193. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1194. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1195. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1196. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1197. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1198. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1199. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1200. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1201. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1202. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1203. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1204. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1205. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1206. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1207. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1208. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1209. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1210. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1211. "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00"
  1212. "\x00\x00\x80\x47\xc0\x7f\x01\x00\x00\xff\xff\x51\xe3\x2f\x55",
  1213. 1919);
  1214. syz_mount_image(/*fs=*/0x200000000780, /*dir=*/0x200000000240, /*flags=*/0,
  1215. /*opts=*/0x200000000000, /*chdir=*/1, /*size=*/0x77f,
  1216. /*img=*/0x200000000f40);
  1217. break;
  1218. case 1:
  1219. // creat arguments: [
  1220. // file: ptr[in, buffer] {
  1221. // buffer: {2e 2f 62 75 73 00} (length 0x6)
  1222. // }
  1223. // mode: open_mode = 0x0 (8 bytes)
  1224. // ]
  1225. // returns fd
  1226. memcpy((void*)0x200000000040, "./bus\000", 6);
  1227. res = syscall(__NR_creat, /*file=*/0x200000000040ul, /*mode=*/0ul);
  1228. if (res != -1)
  1229. r[0] = res;
  1230. break;
  1231. case 2:
  1232. // writev arguments: [
  1233. // fd: fd (resource)
  1234. // vec: ptr[in, array[iovec[in, array[int8]]]] {
  1235. // array[iovec[in, array[int8]]] {
  1236. // iovec[in, array[int8]] {
  1237. // addr: ptr[in, buffer] {
  1238. // buffer: {78} (length 0x1)
  1239. // }
  1240. // len: len = 0x1 (8 bytes)
  1241. // }
  1242. // }
  1243. // }
  1244. // vlen: len = 0x1 (8 bytes)
  1245. // ]
  1246. *(uint64_t*)0x2000000006c0 = 0x200000000340;
  1247. memset((void*)0x200000000340, 120, 1);
  1248. *(uint64_t*)0x2000000006c8 = 1;
  1249. syscall(__NR_writev, /*fd=*/r[0], /*vec=*/0x2000000006c0ul, /*vlen=*/1ul);
  1250. break;
  1251. case 3:
  1252. // creat arguments: [
  1253. // file: nil
  1254. // mode: open_mode = 0x0 (8 bytes)
  1255. // ]
  1256. // returns fd
  1257. syscall(__NR_creat, /*file=*/0ul, /*mode=*/0ul);
  1258. break;
  1259. case 4:
  1260. // mount arguments: [
  1261. // src: ptr[in, blockdev_filename] {
  1262. // union blockdev_filename {
  1263. // loop: loop_filename {
  1264. // prefix: buffer: {2f 64 65 76 2f 6c 6f 6f 70} (length 0x9)
  1265. // id: proc = 0x0 (1 bytes)
  1266. // z: const = 0x0 (1 bytes)
  1267. // }
  1268. // }
  1269. // }
  1270. // dst: ptr[in, buffer] {
  1271. // buffer: {2e 2f 62 75 73 00} (length 0x6)
  1272. // }
  1273. // type: nil
  1274. // flags: mount_flags = 0x9004 (8 bytes)
  1275. // data: nil
  1276. // ]
  1277. memcpy((void*)0x2000000000c0, "/dev/loop", 9);
  1278. *(uint8_t*)0x2000000000c9 = 0x30 + procid * 1;
  1279. *(uint8_t*)0x2000000000ca = 0;
  1280. memcpy((void*)0x200000001240, "./bus\000", 6);
  1281. syscall(__NR_mount, /*src=*/0x2000000000c0ul, /*dst=*/0x200000001240ul,
  1282. /*type=*/0ul, /*flags=MS_SILENT|MS_NODEV|MS_BIND*/ 0x9004ul,
  1283. /*data=*/0ul);
  1284. break;
  1285. case 5:
  1286. // open arguments: [
  1287. // file: ptr[in, buffer] {
  1288. // buffer: {2e 2f 62 75 73 00} (length 0x6)
  1289. // }
  1290. // flags: open_flags = 0x145842 (8 bytes)
  1291. // mode: open_mode = 0x0 (8 bytes)
  1292. // ]
  1293. // returns fd
  1294. memcpy((void*)0x2000000005c0, "./bus\000", 6);
  1295. res = syscall(
  1296. __NR_open, /*file=*/0x2000000005c0ul,
  1297. /*flags=O_SYNC|O_NONBLOCK|O_NOATIME|O_DIRECT|O_CREAT|0x2*/ 0x145842ul,
  1298. /*mode=*/0ul);
  1299. if (res != -1)
  1300. r[1] = res;
  1301. break;
  1302. case 6:
  1303. // pwritev2 arguments: [
  1304. // fd: fd (resource)
  1305. // vec: ptr[in, array[iovec[in, array[int8]]]] {
  1306. // array[iovec[in, array[int8]]] {
  1307. // iovec[in, array[int8]] {
  1308. // addr: ptr[in, buffer] {
  1309. // buffer: {85} (length 0x1)
  1310. // }
  1311. // len: len = 0xa000 (8 bytes)
  1312. // }
  1313. // }
  1314. // }
  1315. // vlen: len = 0x1 (8 bytes)
  1316. // off_low: int32 = 0x1400 (4 bytes)
  1317. // off_high: int32 = 0x0 (4 bytes)
  1318. // flags: rwf_flags = 0x3 (8 bytes)
  1319. // ]
  1320. *(uint64_t*)0x200000000240 = 0x200000000000;
  1321. memset((void*)0x200000000000, 133, 1);
  1322. *(uint64_t*)0x200000000248 = 0xa000;
  1323. syscall(__NR_pwritev2, /*fd=*/r[1], /*vec=*/0x200000000240ul, /*vlen=*/1ul,
  1324. /*off_low=*/0x1400, /*off_high=*/0,
  1325. /*flags=RWF_HIPRI|RWF_DSYNC*/ 3ul);
  1326. break;
  1327. case 7:
  1328. // open arguments: [
  1329. // file: ptr[in, buffer] {
  1330. // buffer: {2e 2f 62 75 73 00} (length 0x6)
  1331. // }
  1332. // flags: open_flags = 0x0 (8 bytes)
  1333. // mode: open_mode = 0x0 (8 bytes)
  1334. // ]
  1335. // returns fd
  1336. memcpy((void*)0x200000000480, "./bus\000", 6);
  1337. res = syscall(__NR_open, /*file=*/0x200000000480ul, /*flags=*/0ul,
  1338. /*mode=*/0ul);
  1339. if (res != -1)
  1340. r[2] = res;
  1341. break;
  1342. case 8:
  1343. // sendfile arguments: [
  1344. // fdout: fd (resource)
  1345. // fdin: fd (resource)
  1346. // off: nil
  1347. // count: intptr = 0x80001d00c0d0 (8 bytes)
  1348. // ]
  1349. syscall(__NR_sendfile, /*fdout=*/r[0], /*fdin=*/r[2], /*off=*/0ul,
  1350. /*count=*/0x80001d00c0d0ul);
  1351. break;
  1352. }
  1353. }
  1354. int main(void)
  1355. {
  1356. syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  1357. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
  1358. /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  1359. syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul,
  1360. /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul,
  1361. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
  1362. /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  1363. syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul,
  1364. /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul,
  1365. /*fd=*/(intptr_t)-1, /*offset=*/0ul);
  1366. const char* reason;
  1367. (void)reason;
  1368. for (procid = 0; procid < 4; procid++) {
  1369. if (fork() == 0) {
  1370. use_temporary_dir();
  1371. do_sandbox_namespace();
  1372. }
  1373. }
  1374. sleep(1000000);
  1375. return 0;
  1376. }
  1377.  
Add Comment
Please, Sign In to add comment