// https://syzkaller.appspot.com/bug?id=c2fd07ae5213991a1b95eadee9205b41ac1aa6e2 // 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 #ifndef __NR_memfd_create #define __NR_memfd_create 279 #endif #ifndef __NR_mmap #define __NR_mmap 222 #endif static unsigned long long procid; //% This code is derived from puff.{c,h}, found in the zlib development. The //% original files come with the following copyright notice: //% Copyright (C) 2002-2013 Mark Adler, all rights reserved //% version 2.3, 21 Jan 2013 //% This software is provided 'as-is', without any express or implied //% warranty. In no event will the author be held liable for any damages //% arising from the use of this software. //% Permission is granted to anyone to use this software for any purpose, //% including commercial applications, and to alter it and redistribute it //% freely, subject to the following restrictions: //% 1. The origin of this software must not be misrepresented; you must not //% claim that you wrote the original software. If you use this software //% in a product, an acknowledgment in the product documentation would be //% appreciated but is not required. //% 2. Altered source versions must be plainly marked as such, and must not be //% misrepresented as being the original software. //% 3. This notice may not be removed or altered from any source distribution. //% Mark Adler madler@alumni.caltech.edu //% BEGIN CODE DERIVED FROM puff.{c,h} #define MAXBITS 15 #define MAXLCODES 286 #define MAXDCODES 30 #define MAXCODES (MAXLCODES + MAXDCODES) #define FIXLCODES 288 struct puff_state { unsigned char* out; unsigned long outlen; unsigned long outcnt; const unsigned char* in; unsigned long inlen; unsigned long incnt; int bitbuf; int bitcnt; jmp_buf env; }; static int puff_bits(struct puff_state* s, int need) { long val = s->bitbuf; while (s->bitcnt < need) { if (s->incnt == s->inlen) longjmp(s->env, 1); val |= (long)(s->in[s->incnt++]) << s->bitcnt; s->bitcnt += 8; } s->bitbuf = (int)(val >> need); s->bitcnt -= need; return (int)(val & ((1L << need) - 1)); } static int puff_stored(struct puff_state* s) { s->bitbuf = 0; s->bitcnt = 0; if (s->incnt + 4 > s->inlen) return 2; unsigned len = s->in[s->incnt++]; len |= s->in[s->incnt++] << 8; if (s->in[s->incnt++] != (~len & 0xff) || s->in[s->incnt++] != ((~len >> 8) & 0xff)) return -2; if (s->incnt + len > s->inlen) return 2; if (s->outcnt + len > s->outlen) return 1; for (; len--; s->outcnt++, s->incnt++) { if (s->in[s->incnt]) s->out[s->outcnt] = s->in[s->incnt]; } return 0; } struct puff_huffman { short* count; short* symbol; }; static int puff_decode(struct puff_state* s, const struct puff_huffman* h) { int first = 0; int index = 0; int bitbuf = s->bitbuf; int left = s->bitcnt; int code = first = index = 0; int len = 1; short* next = h->count + 1; while (1) { while (left--) { code |= bitbuf & 1; bitbuf >>= 1; int count = *next++; if (code - count < first) { s->bitbuf = bitbuf; s->bitcnt = (s->bitcnt - len) & 7; return h->symbol[index + (code - first)]; } index += count; first += count; first <<= 1; code <<= 1; len++; } left = (MAXBITS + 1) - len; if (left == 0) break; if (s->incnt == s->inlen) longjmp(s->env, 1); bitbuf = s->in[s->incnt++]; if (left > 8) left = 8; } return -10; } static int puff_construct(struct puff_huffman* h, const short* length, int n) { int len; for (len = 0; len <= MAXBITS; len++) h->count[len] = 0; int symbol; for (symbol = 0; symbol < n; symbol++) (h->count[length[symbol]])++; if (h->count[0] == n) return 0; int left = 1; for (len = 1; len <= MAXBITS; len++) { left <<= 1; left -= h->count[len]; if (left < 0) return left; } short offs[MAXBITS + 1]; offs[1] = 0; for (len = 1; len < MAXBITS; len++) offs[len + 1] = offs[len] + h->count[len]; for (symbol = 0; symbol < n; symbol++) if (length[symbol] != 0) h->symbol[offs[length[symbol]]++] = symbol; return left; } static int puff_codes(struct puff_state* s, const struct puff_huffman* lencode, const struct puff_huffman* distcode) { static const short lens[29] = {3, 4, 5, 6, 7, 8, 9, 10, 11, 13, 15, 17, 19, 23, 27, 31, 35, 43, 51, 59, 67, 83, 99, 115, 131, 163, 195, 227, 258}; static const short lext[29] = {0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5, 0}; static const short dists[30] = { 1, 2, 3, 4, 5, 7, 9, 13, 17, 25, 33, 49, 65, 97, 129, 193, 257, 385, 513, 769, 1025, 1537, 2049, 3073, 4097, 6145, 8193, 12289, 16385, 24577}; static const short dext[30] = {0, 0, 0, 0, 1, 1, 2, 2, 3, 3, 4, 4, 5, 5, 6, 6, 7, 7, 8, 8, 9, 9, 10, 10, 11, 11, 12, 12, 13, 13}; int symbol; do { symbol = puff_decode(s, lencode); if (symbol < 0) return symbol; if (symbol < 256) { if (s->outcnt == s->outlen) return 1; if (symbol) s->out[s->outcnt] = symbol; s->outcnt++; } else if (symbol > 256) { symbol -= 257; if (symbol >= 29) return -10; int len = lens[symbol] + puff_bits(s, lext[symbol]); symbol = puff_decode(s, distcode); if (symbol < 0) return symbol; unsigned dist = dists[symbol] + puff_bits(s, dext[symbol]); if (dist > s->outcnt) return -11; if (s->outcnt + len > s->outlen) return 1; while (len--) { if (dist <= s->outcnt && s->out[s->outcnt - dist]) s->out[s->outcnt] = s->out[s->outcnt - dist]; s->outcnt++; } } } while (symbol != 256); return 0; } static int puff_fixed(struct puff_state* s) { static int virgin = 1; static short lencnt[MAXBITS + 1], lensym[FIXLCODES]; static short distcnt[MAXBITS + 1], distsym[MAXDCODES]; static struct puff_huffman lencode, distcode; if (virgin) { lencode.count = lencnt; lencode.symbol = lensym; distcode.count = distcnt; distcode.symbol = distsym; short lengths[FIXLCODES]; int symbol; for (symbol = 0; symbol < 144; symbol++) lengths[symbol] = 8; for (; symbol < 256; symbol++) lengths[symbol] = 9; for (; symbol < 280; symbol++) lengths[symbol] = 7; for (; symbol < FIXLCODES; symbol++) lengths[symbol] = 8; puff_construct(&lencode, lengths, FIXLCODES); for (symbol = 0; symbol < MAXDCODES; symbol++) lengths[symbol] = 5; puff_construct(&distcode, lengths, MAXDCODES); virgin = 0; } return puff_codes(s, &lencode, &distcode); } static int puff_dynamic(struct puff_state* s) { static const short order[19] = {16, 17, 18, 0, 8, 7, 9, 6, 10, 5, 11, 4, 12, 3, 13, 2, 14, 1, 15}; int nlen = puff_bits(s, 5) + 257; int ndist = puff_bits(s, 5) + 1; int ncode = puff_bits(s, 4) + 4; if (nlen > MAXLCODES || ndist > MAXDCODES) return -3; short lengths[MAXCODES]; int index; for (index = 0; index < ncode; index++) lengths[order[index]] = puff_bits(s, 3); for (; index < 19; index++) lengths[order[index]] = 0; short lencnt[MAXBITS + 1], lensym[MAXLCODES]; struct puff_huffman lencode = {lencnt, lensym}; int err = puff_construct(&lencode, lengths, 19); if (err != 0) return -4; index = 0; while (index < nlen + ndist) { int symbol; int len; symbol = puff_decode(s, &lencode); if (symbol < 0) return symbol; if (symbol < 16) lengths[index++] = symbol; else { len = 0; if (symbol == 16) { if (index == 0) return -5; len = lengths[index - 1]; symbol = 3 + puff_bits(s, 2); } else if (symbol == 17) symbol = 3 + puff_bits(s, 3); else symbol = 11 + puff_bits(s, 7); if (index + symbol > nlen + ndist) return -6; while (symbol--) lengths[index++] = len; } } if (lengths[256] == 0) return -9; err = puff_construct(&lencode, lengths, nlen); if (err && (err < 0 || nlen != lencode.count[0] + lencode.count[1])) return -7; short distcnt[MAXBITS + 1], distsym[MAXDCODES]; struct puff_huffman distcode = {distcnt, distsym}; err = puff_construct(&distcode, lengths + nlen, ndist); if (err && (err < 0 || ndist != distcode.count[0] + distcode.count[1])) return -8; return puff_codes(s, &lencode, &distcode); } static int puff(unsigned char* dest, unsigned long* destlen, const unsigned char* source, unsigned long sourcelen) { struct puff_state s = { .out = dest, .outlen = *destlen, .outcnt = 0, .in = source, .inlen = sourcelen, .incnt = 0, .bitbuf = 0, .bitcnt = 0, }; int err; if (setjmp(s.env) != 0) err = 2; else { int last; do { last = puff_bits(&s, 1); int type = puff_bits(&s, 2); err = type == 0 ? puff_stored(&s) : (type == 1 ? puff_fixed(&s) : (type == 2 ? puff_dynamic(&s) : -1)); if (err != 0) break; } while (!last); } *destlen = s.outcnt; return err; } //% END CODE DERIVED FROM puff.{c,h} #define ZLIB_HEADER_WIDTH 2 static int puff_zlib_to_file(const unsigned char* source, unsigned long sourcelen, int dest_fd) { if (sourcelen < ZLIB_HEADER_WIDTH) return 0; source += ZLIB_HEADER_WIDTH; sourcelen -= ZLIB_HEADER_WIDTH; const unsigned long max_destlen = 132 << 20; void* ret = mmap(0, max_destlen, PROT_WRITE | PROT_READ, MAP_PRIVATE | MAP_ANON, -1, 0); if (ret == MAP_FAILED) return -1; unsigned char* dest = (unsigned char*)ret; unsigned long destlen = max_destlen; int err = puff(dest, &destlen, source, sourcelen); if (err) { munmap(dest, max_destlen); errno = -err; return -1; } if (write(dest_fd, dest, destlen) != (ssize_t)destlen) { munmap(dest, max_destlen); return -1; } return munmap(dest, destlen); } static int setup_loop_device(unsigned char* data, unsigned long size, const char* loopname, int* loopfd_p) { int err = 0, loopfd = -1; int memfd = syscall(__NR_memfd_create, "syzkaller", 0); if (memfd == -1) { err = errno; goto error; } if (puff_zlib_to_file(data, size, memfd)) { err = errno; goto error_close_memfd; } loopfd = open(loopname, O_RDWR); if (loopfd == -1) { err = errno; goto error_close_memfd; } if (ioctl(loopfd, LOOP_SET_FD, memfd)) { if (errno != EBUSY) { err = errno; goto error_close_loop; } ioctl(loopfd, LOOP_CLR_FD, 0); usleep(1000); if (ioctl(loopfd, LOOP_SET_FD, memfd)) { err = errno; goto error_close_loop; } } close(memfd); *loopfd_p = loopfd; return 0; error_close_loop: close(loopfd); error_close_memfd: close(memfd); error: errno = err; return -1; } static long syz_mount_image(volatile long fsarg, volatile long dir, volatile long flags, volatile long optsarg, volatile long change_dir, volatile unsigned long size, volatile long image) { unsigned char* data = (unsigned char*)image; int res = -1, err = 0, loopfd = -1, need_loop_device = !!size; char* mount_opts = (char*)optsarg; char* target = (char*)dir; char* fs = (char*)fsarg; char* source = NULL; char loopname[64]; if (need_loop_device) { memset(loopname, 0, sizeof(loopname)); snprintf(loopname, sizeof(loopname), "/dev/loop%llu", procid); if (setup_loop_device(data, size, loopname, &loopfd) == -1) return -1; source = loopname; } mkdir(target, 0777); char opts[256]; memset(opts, 0, sizeof(opts)); if (strlen(mount_opts) > (sizeof(opts) - 32)) { } strncpy(opts, mount_opts, sizeof(opts) - 32); if (strcmp(fs, "iso9660") == 0) { flags |= MS_RDONLY; } else if (strncmp(fs, "ext", 3) == 0) { bool has_remount_ro = false; char* remount_ro_start = strstr(opts, "errors=remount-ro"); if (remount_ro_start != NULL) { char after = *(remount_ro_start + strlen("errors=remount-ro")); char before = remount_ro_start == opts ? '\0' : *(remount_ro_start - 1); has_remount_ro = ((before == '\0' || before == ',') && (after == '\0' || after == ',')); } if (strstr(opts, "errors=panic") || !has_remount_ro) strcat(opts, ",errors=continue"); } else if (strcmp(fs, "xfs") == 0) { strcat(opts, ",nouuid"); } res = mount(source, target, fs, flags, opts); if (res == -1) { err = errno; goto error_clear_loop; } res = open(target, O_RDONLY | O_DIRECTORY); if (res == -1) { err = errno; goto error_clear_loop; } if (change_dir) { res = chdir(target); if (res == -1) { err = errno; } } error_clear_loop: if (need_loop_device) { ioctl(loopfd, LOOP_CLR_FD, 0); close(loopfd); } errno = err; return res; } int main(void) { syscall(__NR_mmap, /*addr=*/0x1ffff000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul); syscall(__NR_mmap, /*addr=*/0x20000000ul, /*len=*/0x1000000ul, /*prot=*/7ul, /*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul); syscall(__NR_mmap, /*addr=*/0x21000000ul, /*len=*/0x1000ul, /*prot=*/0ul, /*flags=*/0x32ul, /*fd=*/-1, /*offset=*/0ul); memcpy((void*)0x20000000, "hfs\000", 4); memcpy((void*)0x200001c0, "\023\023w\305\3745\324\024T\325\324\035)\255\032`)" "Y\201F\346\276\026nA\255\r\275@T\003<\2373\273\332\202$" "\242\363\327r\347cnH\263<\277p\203r\350\361\271\223>" "\305\022wC\276\"\006 \236\360-\371\313\362\366\350\200\3238/\000", 78); *(uint8_t*)0x20000100 = 0; memcpy( (void*)0x20000540, "\x78\x9c\xec\xdd\xcf\x6b\x13\x4d\x1c\xc7\xf1\xcf\x6c\xd2\x34\x7d\x9e\xd2" "\xe6\x69\xfb\x50\x10\x4f\xd5\x82\x27\x69\xed\x45\x3c\x68\x91\xfe\x11\x1e" "\x44\xac\x4d\x84\xd2\x50\x41\x2b\xd8\x5e\x6c\x3d\x8b\x78\x13\xbc\xf7\x26" "\x78\x13\xf1\x4f\xf0\x24\xfe\x03\xf5\xd4\x93\x20\xf4\xd4\xdb\xc8\x4e\x26" "\xc9\x36\xc9\x26\x9b\xb4\x9b\x6d\xe9\xfb\x05\x0d\x4b\x77\x7e\x7c\x27\x33" "\xe9\xce\x37\x50\x46\x00\x2e\xad\xfb\x2b\x07\xfb\x4b\x3a\xd8\x5f\x3a\x34" "\x52\x4e\x39\x49\x77\xa4\xa0\x76\x33\x2f\xe9\x7f\xcd\x16\x5f\x6e\x6e\xad" "\x6f\x55\x2b\xe5\x6e\x0d\xe5\xa4\xa2\xdc\x8f\xf1\x35\x4d\x5b\x99\xb5\xcd" "\x4a\x7b\xc5\x6f\x6d\xbf\x29\x85\xf5\xf3\x1a\x77\xad\x20\x55\xd6\x5a\xfb" "\xab\x67\xa9\xa3\xa1\xc4\x82\xec\xb8\x4f\x7f\x07\x81\x34\xea\x3f\xcf\xee" "\x7e\x71\xe8\x91\xa5\x63\x37\xeb\x00\xce\xd6\x48\xbf\x15\xcc\xb1\x8e\xf5" "\x4a\x13\xe9\x84\x03\x00\xb8\x28\xfc\xf3\x3f\xf0\xcf\xf9\x71\xbf\x7f\x0f" "\x02\x69\xde\x3f\xf6\xdb\x9f\xff\x7b\x99\x85\x7b\x6a\xc7\x59\x07\x90\xb1" "\xc8\xf3\xdf\x65\x59\xd6\x84\xf3\x3b\xe9\x6e\x35\xf3\x3d\x97\xc2\x85\xf7" "\x83\x30\x43\x0c\x7f\x06\xe9\xab\xa0\xda\xca\x3a\xb1\xc1\x34\xbd\xb2\x4a" "\x17\x4b\x30\xf6\x74\xbd\x5a\xb9\xb9\xf6\xac\x5a\x0e\xf4\x46\xf7\xbc\x48" "\xb1\x19\xf7\x5a\x6e\xe4\xac\x4e\x4c\xb4\xf5\x22\x73\x1d\x72\xd3\x2e\xfa" "\x18\x7b\xcb\xf6\xf8\x5f\xd7\xeb\x48\x38\x86\x5b\x31\xf1\x4f\x0f\xd2\xe3" "\x87\xc4\xb1\xb7\x33\xdf\xcd\x0f\xf3\xc8\x94\xf4\x51\xe5\xc6\xfe\x2f\x6f" "\x4d\x38\x4d\x6e\xa6\x4a\x2d\x33\x55\x8b\x7f\x21\xbe\x45\x37\xca\xd2\x9e" "\x2b\x15\x33\xca\xff\x5c\x27\x57\x7c\x0f\xfa\xfa\x29\xc1\x28\x8b\xca\x85" "\xb1\x74\x52\xf0\x6d\x9e\xf8\x82\xa0\x54\x8f\xf3\x7d\xa1\x4b\xad\xa9\x5a" "\x2d\x6b\xeb\xa3\x53\x58\x6b\x31\x7e\x74\xae\xd6\xb4\xab\xd5\x5c\x35\xd1" "\xf5\x73\xd0\xb8\xba\xdb\x52\x6b\xa6\x25\xc2\xc8\x6a\x8e\xef\x2f\x6d\xe6" "\x9d\x79\x60\xe6\xf4\x5b\x5f\xb4\x12\xd9\xff\x07\xe1\xa4\xcf\x2b\xf2\xc9" "\xbc\x7a\xd4\x78\x93\xd4\xba\xb4\x8d\x2b\xe9\x57\x46\xd7\xf1\xe4\x5d\xc9" "\x98\x79\x3c\x21\x88\xf9\xfd\xe7\x04\x75\xd1\xaf\xb7\x7a\xa2\xdb\x9a\x78" "\xb1\xbd\xb3\xb1\x5a\xad\x56\x9e\xf7\x71\x61\x27\x6b\x4d\xf4\x57\xeb\x1c" "\x5c\x18\x19\x0d\x5c\xdd\x2a\xc5\x08\x8b\xdd\xdf\xcc\xbc\xb6\x77\x36\x76" "\xd3\x7e\x7f\xea\x5f\x22\x6c\xef\x2c\xff\xa9\x5d\x65\x33\x4d\x61\xcf\xcb" "\x99\xf5\x3e\xf0\xc5\x98\x4e\xd9\x8e\xb5\xf6\x75\xe2\xc2\x39\x8d\xa6\xb9" "\x20\x57\xab\x59\xfd\x61\xc2\x30\x35\x27\x5d\xb3\x0f\xb3\x0e\x06\x59\x08" "\xf7\x5d\xa6\x96\xff\x45\xf2\x95\x05\xb7\xc5\x0d\x5f\x4a\x8d\x7d\x7a\x64" "\x3f\xe8\xf7\xb5\xb6\x57\xe3\xf5\x16\x0b\xaa\x2c\xc6\xe4\x06\x53\xee\xf5" "\x9f\x44\x19\x5c\xb3\xd9\xd8\x0c\x2e\x69\xce\x75\xed\x86\x74\x3d\x79\x8f" "\x25\x1f\xe7\x79\x66\xad\xed\x39\x21\x9e\x59\xd1\x4f\x3d\xe6\xfb\x7f\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x80\x8b\xe6\x2c\xfe" "\x43\xa0\x57\xe1\xf8\xde\x6d\x5f\xff\x87\x09\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\xc0\x65\xe5\xce\xff\x3d\xec\x74\xfe" "\x6f\x31\xee\xfc\xdf\xce\x67\x4d\x0e\x7c\xfe\xaf\x3f\x59\x26\x72\x50\x0e" "\xe7\xff\x02\xc3\xf1\x37\x00\x00\xff\xff\x15\xaf\x7e\xaa", 734); syz_mount_image(/*fs=*/0x20000000, /*dir=*/0x200001c0, /*flags=*/0x300041c, /*opts=*/0x20000100, /*chdir=*/2, /*size=*/0x2de, /*img=*/0x20000540); return 0; }