// https://syzkaller.appspot.com/bug?id=67dbafe7987e8ed784ca4d29a2465ee9ae18d2f1 // 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 #ifndef __NR_memfd_create #define __NR_memfd_create 319 #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) { errno = EMSGSIZE; return -1; } 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) { if (strstr(opts, "errors=panic") || strstr(opts, "errors=remount-ro") == 0) 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, 0x1ffff000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul); syscall(__NR_mmap, 0x20000000ul, 0x1000000ul, 7ul, 0x32ul, -1, 0ul); syscall(__NR_mmap, 0x21000000ul, 0x1000ul, 0ul, 0x32ul, -1, 0ul); memcpy((void*)0x20000440, "ext4\000", 5); memcpy((void*)0x20000480, "./file0\000", 8); memcpy( (void*)0x20000000, "\x6e\x6f\x75\x73\x65\x72\x5f\x78\x61\x74\x74\x72\x2c\x61\x63\x6c\x2c\x64" "\x65\x62\x75\x67\x5f\x77\x61\x6e\x74\x5f\x65\x78\x74\x72\x61\x5f\x69\x73" "\x69\x7a\x65\x3d\x30\x78\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30\x30" "\x30\x30\x38\x30\x2c\x6c\x61\x7a\x79\x74\x69\x6d\x65\x2c\x6e\x6f\x62\x68" "\x2c\x71\x75\x6f\x74\x61\x2c\x00\x3d\x93\x09\x61\x36\x5d\x73\x58\x9c", 89); memcpy( (void*)0x200004c0, "\x78\x9c\xec\xdb\xcf\x6b\x1c\x55\x1c\x00\xf0\xef\xcc\x26\xad\xfd\x65\x62" "\xad\x3f\x9a\x56\x8d\x56\x31\xf8\x23\x69\xd2\xaa\x3d\x78\x50\x51\xf0\xa0" "\x20\xe8\xa1\x1e\x63\x92\x96\xd8\x6d\x23\x4d\x04\x5b\x82\x46\x91\x7a\x94" "\x82\x77\xf1\x28\xf8\x17\x78\xd2\x8b\xa8\x27\xc1\xab\xde\xa5\x50\x24\x97" "\x56\x4f\x2b\xb3\x3b\x93\x6c\xb6\xbb\x31\x9b\x6c\xb2\xd5\xfd\x7c\x60\xda" "\xf7\x66\xde\xf2\xde\x77\xde\xbc\xdd\x37\xf3\x32\x01\xf4\xac\xe1\xec\x9f" "\x24\x62\x7f\x44\xfc\x16\x11\x03\xb5\xec\xda\x02\xc3\xb5\xff\x6e\x2e\x2f" "\x4e\xfd\xb5\xbc\x38\x95\x44\xa5\xf2\xe6\x9f\x49\xb5\xdc\x8d\xe5\xc5\xa9" "\xa2\x68\xf1\xb9\x7d\x79\x66\x24\x8d\x48\x3f\x4d\xe2\x48\x93\x7a\xe7\x2f" "\x5d\x3e\x37\x59\x2e\xcf\x5c\xcc\xf3\x63\x0b\xe7\xdf\x1b\x9b\xbf\x74\xf9" "\xe9\xd9\xf3\x93\x67\x67\xce\xce\x5c\x98\x38\x75\xea\xe4\x89\xf1\xe7\x9e" "\x9d\x78\xa6\x23\x71\x66\x71\xdd\x18\xfa\x70\xee\xe8\xe1\x57\xdf\xbe\xfa" "\xfa\xd4\xe9\xab\xef\xfc\xf4\x4d\x52\xc4\xdf\x10\x47\x87\x0c\xaf\x77\xf0" "\xb1\x0e\x57\xd6\x6d\x07\xea\xd2\x49\x5f\x17\x1b\x42\x5b\x4a\x11\x91\x75" "\x57\x7f\x75\xfc\x0f\x44\x29\x56\x3b\x6f\x20\x5e\xf9\xa4\xab\x8d\x03\xb6" "\x55\xa5\x52\xa9\xec\x6e\x7d\x78\xa9\x02\xfc\x8f\x25\xd1\xed\x16\x00\xdd" "\x51\xfc\xd0\x67\xf7\xbf\xc5\xb6\x43\x53\x8f\xdb\xc2\xf5\x17\x6b\x37\x40" "\x59\xdc\x37\xf3\xad\x76\xa4\x2f\xd2\xbc\x4c\x7f\xc3\xfd\x6d\x27\x0d\x47" "\xc4\xe9\xa5\xbf\xbf\xcc\xb6\xd8\x9e\xe7\x10\x00\x00\x6b\x7c\x97\xcd\x7f" "\x9e\x6a\x36\xff\x4b\xe3\xde\xba\x72\x77\xe6\x6b\x28\x83\x11\x71\x57\x44" "\x1c\x8c\x88\xbb\x23\xe2\x50\x44\xdc\x13\x51\x2d\x7b\x5f\x44\xdc\xdf\x66" "\xfd\x8d\x8b\x24\xb7\xce\x7f\xd2\x6b\x9b\x0a\x6c\x83\xb2\xf9\xdf\xf3\xf9" "\xda\xd6\xda\xf9\x5f\x31\xfb\x8b\xc1\x52\x9e\x3b\x50\x8d\xbf\x3f\x39\x33" "\x5b\x9e\x39\x9e\x9f\x93\x91\xe8\xdf\x9d\xe5\xc7\xd7\xa9\xe3\xfb\x97\x7f" "\xfd\xbc\xd5\xb1\xfa\xf9\x5f\xb6\x65\xf5\x17\x73\xc1\xbc\x1d\xd7\xfa\x1a" "\x1e\xd0\x4d\x4f\x2e\x4c\x6e\x25\xe6\x7a\xd7\x3f\x8e\x18\xea\x6b\x16\x7f" "\xb2\xb2\x12\x90\x44\xc4\xe1\x88\x18\xda\x64\x1d\xb3\x4f\x7c\x7d\xb4\xd5" "\xb1\x7f\x8f\x7f\x1d\x1d\x58\x67\xaa\x7c\x15\xf1\x78\xad\xff\x97\xa2\x21" "\xfe\x42\xb2\xfe\xfa\xe4\xd8\x1d\x51\x9e\x39\x3e\x56\x5c\x15\xb7\xfa\xf9" "\x97\x2b\x6f\xb4\xaa\x7f\x4b\xf1\x77\x40\xd6\xff\x7b\x9b\x5e\xff\x2b\xf1" "\x0f\x26\xf5\xeb\xb5\xf3\xed\xd7\x71\xe5\xf7\xcf\x5a\xde\xd3\x6c\xf6\xfa" "\xdf\x95\xbc\x55\x4d\xef\xca\xf7\x7d\x30\xb9\xb0\x70\x71\x3c\x62\x57\xf2" "\x5a\xbe\xff\x85\xd5\xfd\x13\xab\x9f\x2d\xf2\x45\xf9\x2c\xfe\x91\x63\xcd" "\xc7\xff\xc1\x58\x3d\x13\x47\x22\x22\xbb\x88\x1f\x88\x88\x07\x23\xe2\xa1" "\xbc\xed\x0f\x47\xc4\x23\x11\x71\x6c\x9d\xf8\x7f\x7c\xe9\xd1\x77\x37\x1f" "\xff\xf6\xca\xe2\x9f\x6e\xab\xff\xdb\x4f\x94\xce\xfd\xf0\xed\x9a\x4a\x07" "\xdb\x89\x3f\xeb\xff\x93\xd5\xd4\x48\xbe\x67\x23\xdf\x7f\x1b\x6d\xe0\x56" "\xcf\x1f\x00\x00\x00\xfc\x17\xa4\x11\xb1\x3f\x92\x74\x74\x25\x9d\xa6\xa3" "\xa3\xb5\xbf\xe1\x3f\x14\x7b\xd3\xf2\xdc\xfc\xc2\x93\x67\xe6\xde\xbf\x30" "\x5d\x7b\x47\x60\x30\xfa\xd3\xe2\x49\xd7\x40\xdd\xf3\xd0\xf1\xfc\xb6\xbe" "\xc8\x4f\x34\xe4\x4f\xe4\xcf\x8d\xbf\x28\xed\xa9\xe6\x47\xa7\xe6\xca\xd3" "\xdd\x0e\x1e\x7a\xdc\xbe\x16\xe3\x3f\xf3\x47\xa9\xdb\xad\x03\xb6\x9d\xf7" "\xb5\xa0\x77\x19\xff\xd0\xbb\x8c\x7f\xe8\x5d\xc6\x3f\xf4\xae\x26\xe3\x7f" "\x4f\x37\xda\x01\xec\xbc\x66\xbf\xff\x1f\x75\xa1\x1d\xc0\xce\x33\xff\x87" "\xde\x65\xfc\x43\xef\x32\xfe\xa1\x77\x19\xff\xd0\x93\xb6\xf2\x5e\xbf\x44" "\x2f\x27\x22\xbd\x2d\x9a\x21\xb1\x4d\x89\x6e\x7f\x33\x01\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00\x00" "\x00\x00\x00\x00\x00\x00\x00\x74\xc6\x3f\x01\x00\x00\xff\xff\x38\x0b\xe6" "\xcd", 1063); syz_mount_image(0x20000440, 0x20000480, 0x1e, 0x20000000, 2, 0x427, 0x200004c0); memcpy((void*)0x20000040, "\351\037q\211Y\036\2223aK\000", 11); syscall(__NR_mknodat, 0xffffff9c, 0x20000040ul, 0ul, 0); return 0; }