// https://syzkaller.appspot.com/bug?id=baff5bfd0c148e6a6552e815fba942c1ac5e61b0 // autogenerated by syzkaller (https://github.com/google/syzkaller) #define _GNU_SOURCE #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include #include static unsigned long long procid; static void sleep_ms(uint64_t ms) { usleep(ms * 1000); } static uint64_t current_time_ms(void) { struct timespec ts; if (clock_gettime(CLOCK_MONOTONIC, &ts)) exit(1); return (uint64_t)ts.tv_sec * 1000 + (uint64_t)ts.tv_nsec / 1000000; } static void use_temporary_dir(void) { char tmpdir_template[] = "./syzkaller.XXXXXX"; char* tmpdir = mkdtemp(tmpdir_template); if (!tmpdir) exit(1); if (chmod(tmpdir, 0777)) exit(1); if (chdir(tmpdir)) exit(1); } static bool write_file(const char* file, const char* what, ...) { char buf[1024]; va_list args; va_start(args, what); vsnprintf(buf, sizeof(buf), what, args); va_end(args); buf[sizeof(buf) - 1] = 0; int len = strlen(buf); int fd = open(file, O_WRONLY | O_CLOEXEC); if (fd == -1) return false; if (write(fd, buf, len) != len) { int err = errno; close(fd); errno = err; return false; } close(fd); return true; } struct nlmsg { char* pos; int nesting; struct nlattr* nested[8]; char buf[4096]; }; static void netlink_init(struct nlmsg* nlmsg, int typ, int flags, const void* data, int size) { memset(nlmsg, 0, sizeof(*nlmsg)); struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf; hdr->nlmsg_type = typ; hdr->nlmsg_flags = NLM_F_REQUEST | NLM_F_ACK | flags; memcpy(hdr + 1, data, size); nlmsg->pos = (char*)(hdr + 1) + NLMSG_ALIGN(size); } static void netlink_attr(struct nlmsg* nlmsg, int typ, const void* data, int size) { struct nlattr* attr = (struct nlattr*)nlmsg->pos; attr->nla_len = sizeof(*attr) + size; attr->nla_type = typ; if (size > 0) memcpy(attr + 1, data, size); nlmsg->pos += NLMSG_ALIGN(attr->nla_len); } static void netlink_nest(struct nlmsg* nlmsg, int typ) { struct nlattr* attr = (struct nlattr*)nlmsg->pos; attr->nla_type = typ; nlmsg->pos += sizeof(*attr); nlmsg->nested[nlmsg->nesting++] = attr; } static void netlink_done(struct nlmsg* nlmsg) { struct nlattr* attr = nlmsg->nested[--nlmsg->nesting]; attr->nla_len = nlmsg->pos - (char*)attr; } static int netlink_send_ext(struct nlmsg* nlmsg, int sock, uint16_t reply_type, int* reply_len, bool dofail) { if (nlmsg->pos > nlmsg->buf + sizeof(nlmsg->buf) || nlmsg->nesting) exit(1); struct nlmsghdr* hdr = (struct nlmsghdr*)nlmsg->buf; hdr->nlmsg_len = nlmsg->pos - nlmsg->buf; struct sockaddr_nl addr; memset(&addr, 0, sizeof(addr)); addr.nl_family = AF_NETLINK; ssize_t n = sendto(sock, nlmsg->buf, hdr->nlmsg_len, 0, (struct sockaddr*)&addr, sizeof(addr)); if (n != (ssize_t)hdr->nlmsg_len) { if (dofail) exit(1); return -1; } n = recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0); if (reply_len) *reply_len = 0; if (n < 0) { if (dofail) exit(1); return -1; } if (n < (ssize_t)sizeof(struct nlmsghdr)) { errno = EINVAL; if (dofail) exit(1); return -1; } if (hdr->nlmsg_type == NLMSG_DONE) return 0; if (reply_len && hdr->nlmsg_type == reply_type) { *reply_len = n; return 0; } if (n < (ssize_t)(sizeof(struct nlmsghdr) + sizeof(struct nlmsgerr))) { errno = EINVAL; if (dofail) exit(1); return -1; } if (hdr->nlmsg_type != NLMSG_ERROR) { errno = EINVAL; if (dofail) exit(1); return -1; } errno = -((struct nlmsgerr*)(hdr + 1))->error; return -errno; } static int netlink_send(struct nlmsg* nlmsg, int sock) { return netlink_send_ext(nlmsg, sock, 0, NULL, true); } static int netlink_query_family_id(struct nlmsg* nlmsg, int sock, const char* family_name, bool dofail) { struct genlmsghdr genlhdr; memset(&genlhdr, 0, sizeof(genlhdr)); genlhdr.cmd = CTRL_CMD_GETFAMILY; netlink_init(nlmsg, GENL_ID_CTRL, 0, &genlhdr, sizeof(genlhdr)); netlink_attr(nlmsg, CTRL_ATTR_FAMILY_NAME, family_name, strnlen(family_name, GENL_NAMSIZ - 1) + 1); int n = 0; int err = netlink_send_ext(nlmsg, sock, GENL_ID_CTRL, &n, dofail); if (err < 0) { return -1; } uint16_t id = 0; struct nlattr* attr = (struct nlattr*)(nlmsg->buf + NLMSG_HDRLEN + NLMSG_ALIGN(sizeof(genlhdr))); for (; (char*)attr < nlmsg->buf + n; attr = (struct nlattr*)((char*)attr + NLMSG_ALIGN(attr->nla_len))) { if (attr->nla_type == CTRL_ATTR_FAMILY_ID) { id = *(uint16_t*)(attr + 1); break; } } if (!id) { errno = EINVAL; return -1; } recv(sock, nlmsg->buf, sizeof(nlmsg->buf), 0); return id; } static unsigned int queue_count = 2; static void netlink_add_device_impl(struct nlmsg* nlmsg, const char* type, const char* name, bool up) { struct ifinfomsg hdr; memset(&hdr, 0, sizeof(hdr)); if (up) hdr.ifi_flags = hdr.ifi_change = IFF_UP; netlink_init(nlmsg, RTM_NEWLINK, NLM_F_EXCL | NLM_F_CREATE, &hdr, sizeof(hdr)); if (name) netlink_attr(nlmsg, IFLA_IFNAME, name, strlen(name)); netlink_attr(nlmsg, IFLA_NUM_TX_QUEUES, &queue_count, sizeof(queue_count)); netlink_attr(nlmsg, IFLA_NUM_RX_QUEUES, &queue_count, sizeof(queue_count)); netlink_nest(nlmsg, IFLA_LINKINFO); netlink_attr(nlmsg, IFLA_INFO_KIND, type, strlen(type)); } static void netlink_device_change(struct nlmsg* nlmsg, int sock, const char* name, bool up, const char* master, const void* mac, int macsize, const char* new_name) { struct ifinfomsg hdr; memset(&hdr, 0, sizeof(hdr)); if (up) hdr.ifi_flags = hdr.ifi_change = IFF_UP; hdr.ifi_index = if_nametoindex(name); netlink_init(nlmsg, RTM_NEWLINK, 0, &hdr, sizeof(hdr)); if (new_name) netlink_attr(nlmsg, IFLA_IFNAME, new_name, strlen(new_name)); if (master) { int ifindex = if_nametoindex(master); netlink_attr(nlmsg, IFLA_MASTER, &ifindex, sizeof(ifindex)); } if (macsize) netlink_attr(nlmsg, IFLA_ADDRESS, mac, macsize); int err = netlink_send(nlmsg, sock); if (err < 0) { } } static struct nlmsg nlmsg; #define FS_IOC_SETFLAGS _IOW('f', 2, long) static void remove_dir(const char* dir) { int iter = 0; DIR* dp = 0; const int umount_flags = MNT_FORCE | UMOUNT_NOFOLLOW; retry: while (umount2(dir, umount_flags) == 0) { } dp = opendir(dir); if (dp == NULL) { if (errno == EMFILE) { exit(1); } exit(1); } struct dirent* ep = 0; while ((ep = readdir(dp))) { if (strcmp(ep->d_name, ".") == 0 || strcmp(ep->d_name, "..") == 0) continue; char filename[FILENAME_MAX]; snprintf(filename, sizeof(filename), "%s/%s", dir, ep->d_name); while (umount2(filename, umount_flags) == 0) { } struct stat st; if (lstat(filename, &st)) exit(1); if (S_ISDIR(st.st_mode)) { remove_dir(filename); continue; } int i; for (i = 0;; i++) { if (unlink(filename) == 0) break; if (errno == EPERM) { int fd = open(filename, O_RDONLY); if (fd != -1) { long flags = 0; if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) { } close(fd); continue; } } if (errno == EROFS) { break; } if (errno != EBUSY || i > 100) exit(1); if (umount2(filename, umount_flags)) exit(1); } } closedir(dp); for (int i = 0;; i++) { if (rmdir(dir) == 0) break; if (i < 100) { if (errno == EPERM) { int fd = open(dir, O_RDONLY); if (fd != -1) { long flags = 0; if (ioctl(fd, FS_IOC_SETFLAGS, &flags) == 0) { } close(fd); continue; } } if (errno == EROFS) { break; } if (errno == EBUSY) { if (umount2(dir, umount_flags)) exit(1); continue; } if (errno == ENOTEMPTY) { if (iter < 100) { iter++; goto retry; } } } exit(1); } } static void kill_and_wait(int pid, int* status) { kill(-pid, SIGKILL); kill(pid, SIGKILL); for (int i = 0; i < 100; i++) { if (waitpid(-1, status, WNOHANG | __WALL) == pid) return; usleep(1000); } DIR* dir = opendir("/sys/fs/fuse/connections"); if (dir) { for (;;) { struct dirent* ent = readdir(dir); if (!ent) break; if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0) continue; char abort[300]; snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort", ent->d_name); int fd = open(abort, O_WRONLY); if (fd == -1) { continue; } if (write(fd, abort, 1) < 0) { } close(fd); } closedir(dir); } else { } while (waitpid(-1, status, __WALL) != pid) { } } static void setup_test() { prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0); setpgrp(); write_file("/proc/self/oom_score_adj", "1000"); if (symlink("/dev/binderfs", "./binderfs")) { } } #define NL802154_CMD_SET_SHORT_ADDR 11 #define NL802154_ATTR_IFINDEX 3 #define NL802154_ATTR_SHORT_ADDR 10 static const char* setup_802154() { const char* error = NULL; int sock_generic = -1; int sock_route = socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE); if (sock_route == -1) { error = "socket(AF_NETLINK, SOCK_RAW, NETLINK_ROUTE) failed"; goto fail; } sock_generic = socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC); if (sock_generic == -1) { error = "socket(AF_NETLINK, SOCK_RAW, NETLINK_GENERIC) failed"; goto fail; } { int nl802154_family_id = netlink_query_family_id(&nlmsg, sock_generic, "nl802154", true); if (nl802154_family_id < 0) { error = "netlink_query_family_id failed"; goto fail; } for (int i = 0; i < 2; i++) { char devname[] = "wpan0"; devname[strlen(devname) - 1] += i; uint64_t hwaddr = 0xaaaaaaaaaaaa0002 + (i << 8); uint16_t shortaddr = 0xaaa0 + i; int ifindex = if_nametoindex(devname); struct genlmsghdr genlhdr; memset(&genlhdr, 0, sizeof(genlhdr)); genlhdr.cmd = NL802154_CMD_SET_SHORT_ADDR; netlink_init(&nlmsg, nl802154_family_id, 0, &genlhdr, sizeof(genlhdr)); netlink_attr(&nlmsg, NL802154_ATTR_IFINDEX, &ifindex, sizeof(ifindex)); netlink_attr(&nlmsg, NL802154_ATTR_SHORT_ADDR, &shortaddr, sizeof(shortaddr)); if (netlink_send(&nlmsg, sock_generic) < 0) { error = "NL802154_CMD_SET_SHORT_ADDR failed"; goto fail; } netlink_device_change(&nlmsg, sock_route, devname, true, 0, &hwaddr, sizeof(hwaddr), 0); if (i == 0) { netlink_add_device_impl(&nlmsg, "lowpan", "lowpan0", false); netlink_done(&nlmsg); netlink_attr(&nlmsg, IFLA_LINK, &ifindex, sizeof(ifindex)); if (netlink_send(&nlmsg, sock_route) < 0) { error = "netlink: adding device lowpan0 type lowpan link wpan0"; goto fail; } } } } fail: close(sock_route); close(sock_generic); return error; } static void execute_one(void); #define WAIT_FLAGS __WALL static void loop(void) { int iter = 0; for (;; iter++) { char cwdbuf[32]; sprintf(cwdbuf, "./%d", iter); if (mkdir(cwdbuf, 0777)) exit(1); int pid = fork(); if (pid < 0) exit(1); if (pid == 0) { if (chdir(cwdbuf)) exit(1); setup_test(); execute_one(); exit(0); } int status = 0; uint64_t start = current_time_ms(); for (;;) { sleep_ms(10); if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid) break; if (current_time_ms() - start < 5000) continue; kill_and_wait(pid, &status); break; } remove_dir(cwdbuf); } } uint64_t r[2] = {0xffffffffffffffff, 0xffffffffffffffff}; void execute_one(void) { intptr_t res = 0; if (write(1, "executing program\n", sizeof("executing program\n") - 1)) { } // openat$auto_kernfs_file_fops_kernfs_internal arguments: [ // fd: const = 0xffffffffffffff9c (8 bytes) // file: ptr[in, buffer] { // buffer: {2f 73 79 73 2f 64 65 76 69 63 65 73 2f 73 79 73 74 65 6d 2f // 63 70 75 2f 63 70 75 31 2f 6f 6e 6c 69 6e 65 00} (length 0x24) // } // flags: open_flags = 0x62 (4 bytes) // mode: const = 0x0 (2 bytes) // ] // returns fd_kernfs_file_fops_kernfs_internal memcpy((void*)0x200000000080, "/sys/devices/system/cpu/cpu1/online\000", 36); res = syscall(__NR_openat, /*fd=*/0xffffffffffffff9cul, /*file=*/0x200000000080ul, /*flags=O_CREAT|O_RDWR|0x20*/ 0x62, /*mode=*/0); if (res != -1) r[0] = res; // write$auto arguments: [ // fd: fd (resource) // buf: ptr[in, buffer] { // buffer: {30 00 a6 cc 0d 91 51 55 9d 49 da 1b ad b1 9e c8 54 74 a8 94 // 9c 8a e2 c7 63 4f 4d b6 a3 2c 21 6f 9e b0 ad 54 fb 52 a1 59 94 56 5b // 38 04 63 df 3a 5d d9 94 f8 46 bb a2 bb 3e ad 65 18 bd e2 1c 89 4f 4f // 5d 65 5b da ac dd 1a dd dd b9 6f 1a ab d5 ef 60 d8 9c f7 3f 3a 1a c6 // 32 91 31 65 fd 15 88 0f 9a d5 a7 14 08 7d 3b ab 74 d1 61 6b e5 98 ea // e3 7d 10 ab 0c 5f 19 9b 11 b2 35 56 55 4b 93 cd 64 17 e4 ac 41 a5 5b // 08 b8 3b 02 74 63 66 06 fb 44 91 ca 47 da 61 3a 6b 5b 72 06 eb f0 c4 // cb 10 ae c8 e9 75 9f de 4b a5 8e ff 7f d0 55 56 11 cb dd 81 be de 0c // 2f 06 28 1d a5 c5 9b b2 96 05 60 e7 d5 59 07 c1 e9 28 95 df 48 f4 0b // f3 43 52 6e 7a c2 13 3c f0 0b 1f 14 f3 d0 f2 d1 4c 21 81 ea 83 a0 0d // 7c 25 bf 02 74 72 67 9a e7 00 85 5a 06 3f 12 98 0f 29 07 f4 aa 05 c0 // a0 72 d2 85 8d 48 d0 3e ca fc 35 01 95 4f 34 ca 95 1d 83 ec 0a 44 8e // fb ce d1 77 15 3a e9 81 2f 42 23 c6 a1 fa 2d 1b 8c 72 92 6e 4d a1 3b // e4 70 64 24 d7 1b 0b 82 0d 0c d0 48 71 d9 0d 88 23 89 8d cd 1e 87 4e // ee 4f 8d 4f e9 fc 91 a1 a8 48 52 2b 07 b7 52 09 0a 2b 7f d5 48 90 47 // 3d 9a 0d b1 30 17 6e 1b f8 0b 11 0b bb c8 5e a4 e2 05 91 7c 12 33 c3 // 3a fd ee 04 61 c8 12 ce a2 12 cb 8c 87 66 eb 47 51 e9 96 d5 45 13 61 // b7 05 37 3c 26 e0 94 a7 fb 9d 3b fa b1 1b 34 61 2c 27 b2 59 6d e1 3a // bf 8c 73 06 a3 75 8d 21 0a 80 2d 9a bb 3b f4 f3 e1 97 fc 38 ff a7 5c // 8b f9 95 10 24 ef 1a 20 23 62 fb fe e9 06 66 4b 30 dd 84 54 2c fa b5 // 00 83 64 bb 61 d7 0a 92 6c df 41 4e 9d cb 96 c7 e8 e6 8b 43 eb c7 45 // 5a c8 1a 81 6e 66 09 5a 2d 73 5a 13 6e ec a9 bf d0 24 b9 d8 00 00 00 // 00 00 00 00} (length 0x1fb) // } // count: intptr = 0x5 (8 bytes) // ] memcpy((void*)0x2000000002c0, "0\000\246\314\r\221QU\235I\332\033\255\261\236\310Tt\250\224\234\212" "\342\307cOM\266\243,!o\236\260\255T\373R\241Y\224V[8\004c\337:]" "\331\224\370F\273\242\273>\255e\030\275\342\034\211OO]e[" "\332\254\335\032\335\335\271o\032\253\325\357`\330\234\367?:" "\032\3062\2211e\375\025\210\017\232\325\247\024\b};" "\253t\321ak\345\230\352\343}\020\253\f_" "\031\233\021\2625VUK\223\315d\027\344\254A\245[\b\270;" "\002tcf\006\373D\221\312G\332a:k[" "r\006\353\360\304\313\020\256\310\351u\237\336K\245\216\377\177\320UV" "\021\313\335\201\276\336\f/" "\006(\035\245\305\233\262\226\005`\347\325Y\a\301\351(" "\225\337H\364\v\363CRnz\302\023<\360\v\037\024\363\320\362\321L!" "\201\352\203\240\r|%\277\002trg\232\347\000\205Z\006?\022\230\017)" "\a\364\252\005\300\240r\322\205\215H\320>" "\312\3745\001\225O4\312\225\035\203\354\nD\216\373\316\321w\025:" "\351\201/" "B#\306\241\372-\033\214r\222nM\241;\344pd$" "\327\033\v\202\r\f\320Hq\331\r\210#" "\211\215\315\036\207N\356O\215O\351\374\221\241\250HR+\a\267R\t\n+" "\177\325H\220G=\232\r\2610\027n\033\370\v\021\v\273\310^" "\244\342\005\221|\0223\303:" "\375\356\004a\310\022\316\242\022\313\214\207f\353GQ\351\226\325E\023" "a\267\0057<&\340\224\247\373\235;\372\261\0334a,\'\262Ym\341:" "\277\214s\006\243u\215!\n\200-\232\273;" "\364\363\341\227\3748\377\247\\\213\371\225\020$\357\032 " "#b\373\376\351\006fK0\335\204T," "\372\265\000\203d\273a\327\n\222l\337AN\235\313\226\307\350\346\213C" "\353\307EZ\310\032\201nf\tZ-sZ\023n\354\251\277\320$" "\271\330\000\000\000\000\000\000\000", 507); syscall(__NR_write, /*fd=*/r[0], /*buf=*/0x2000000002c0ul, /*count=*/5ul); // mmap$auto arguments: [ // addr: intptr = 0x0 (8 bytes) // len: intptr = 0x2020009 (8 bytes) // prot: intptr = 0x3 (8 bytes) // flags: intptr = 0xeb1 (8 bytes) // fd: fd (resource) // off: intptr = 0x8000 (8 bytes) // ] syscall(__NR_mmap, /*addr=*/0ul, /*len=*/0x2020009ul, /*prot=*/3ul, /*flags=*/0xeb1ul, /*fd=*/0xfffffffa, /*off=*/0x8000ul); // openat$auto_proc_oom_adj_operations_base arguments: [ // fd: const = 0xffffffffffffff9c (8 bytes) // file: ptr[in, buffer] { // buffer: {2f 70 72 6f 63 2f 73 65 6c 66 2f 6f 6f 6d 5f 61 64 6a 00} // (length 0x13) // } // flags: open_flags = 0x48402 (4 bytes) // mode: const = 0x0 (2 bytes) // ] // returns fd_proc_oom_adj_operations_base memcpy((void*)0x200000000000, "/proc/self/oom_adj\000", 19); res = syscall( __NR_openat, /*fd=*/0xffffffffffffff9cul, /*file=*/0x200000000000ul, /*flags=O_NOATIME|O_LARGEFILE|O_APPEND|O_RDWR*/ 0x48402, /*mode=*/0); if (res != -1) r[1] = res; // read$auto arguments: [ // fd: fd (resource) // buf: nil // count: intptr = 0x1f40 (8 bytes) // ] syscall(__NR_read, /*fd=*/r[1], /*buf=*/0ul, /*count=*/0x1f40ul); // writev$auto arguments: [ // fd: fd (resource) // vec: ptr[in, iovec$auto] { // iovec$auto { // iov_base: nil // iov_len: intptr = 0x7111 (8 bytes) // } // } // vlen: intptr = 0x8 (8 bytes) // ] *(uint64_t*)0x200000000100 = 0; *(uint64_t*)0x200000000108 = 0x7111; syscall(__NR_writev, /*fd=*/3, /*vec=*/0x200000000100ul, /*vlen=*/8ul); // prctl$auto arguments: [ // option: int32 = 0x3e (4 bytes) // arg2: intptr = 0x1 (8 bytes) // arg3: pid (resource) // arg4: intptr = 0x1 (8 bytes) // arg5: intptr = 0x0 (8 bytes) // ] // returns fd syscall(__NR_prctl, /*option=*/0x3e, /*arg2=*/1ul, /*arg3=*/0, /*arg4=*/1ul, /*arg5=*/0ul); } int main(void) { syscall(__NR_mmap, /*addr=*/0x1ffffffff000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul); syscall(__NR_mmap, /*addr=*/0x200000000000ul, /*len=*/0x1000000ul, /*prot=PROT_WRITE|PROT_READ|PROT_EXEC*/ 7ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul); syscall(__NR_mmap, /*addr=*/0x200001000000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=MAP_FIXED|MAP_ANONYMOUS|MAP_PRIVATE*/ 0x32ul, /*fd=*/(intptr_t)-1, /*offset=*/0ul); const char* reason; (void)reason; if ((reason = setup_802154())) printf("the reproducer may not work as expected: 802154 injection setup " "failed: %s\n", reason); for (procid = 0; procid < 4; procid++) { if (fork() == 0) { use_temporary_dir(); loop(); } } sleep(1000000); return 0; }