// https://syzkaller.appspot.com/bug?id=7dae7f386fd87762441d6466a7e5f3cbd9d048d2 // 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) 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) { 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*)0x200000c0, "grpjquota=", 10); *(uint8_t*)0x200000ca = 0x4a; memcpy((void*)0x200000cb, "noinit_itable", 13); *(uint8_t*)0x200000d8 = 0x28; memcpy((void*)0x200000d9, "errors=remount-ro", 17); *(uint8_t*)0x200000ea = 0x2c; memcpy((void*)0x200000eb, "minixdf", 7); *(uint8_t*)0x200000f2 = 0x2c; memcpy((void*)0x200000f3, "jqfmt=vfsv0", 11); *(uint8_t*)0x200000fe = 0x2c; memcpy((void*)0x200000ff, "usrjquota=", 10); *(uint8_t*)0x20000109 = 0x2e; *(uint8_t*)0x2000010a = 0x22; memcpy( (void*)0x20000940, "\x78\x9c\xec\xdc\x4b\x6f\xdc\x54\x1b\x00\xe0\xd7\x9e\xa4\x97\xaf\xcd\x97" "\x50\xae\xbd\x00\x81\x82\x88\xb8\x24\x4d\x7a\x5d\xb0\x01\x81\xd4\x0d\x12" "\x12\x2c\xca\x32\x24\x69\x55\x9a\xb6\xa8\x09\x12\xad\x22\x1a\x10\x2a\x4b" "\xd4\x5f\x00\x2c\x91\xf8\x05\xac\x60\x83\x80\x15\x88\x2d\x88\x2d\x42\xaa" "\x50\x36\x2d\x2c\x90\x91\x67\xec\x74\x9a\x64\x92\x4c\x32\xc9\xa4\x9d\xe7" "\x91\xdc\x9e\x63\x7b\x7c\xde\xd7\xf6\x69\x8f\x7d\x32\x09\xa0\x63\xf5\xe7" "\x7f\x24\x11\xbb\x23\xe2\xd7\x88\xe8\xad\x55\xef\xdc\xa1\xbf\xf6\xd7\xad" "\xb9\x99\xb1\xbf\xe7\x66\xc6\x92\xc8\xb2\x37\xfe\x4a\xa2\x12\x11\x37\xe7" "\x66\xc6\xca\x5d\xcb\xcf\xed\x2a\x2a\x03\x69\x44\xfa\x71\x12\xfb\x97\x68" "\x77\xea\xf2\x95\x73\xa3\x93\x93\x13\x97\x8a\xfa\xd0\xf4\xf9\x77\x87\xa6" "\x2e\x5f\x79\xe1\xec\xf9\xd1\x33\x13\x67\x26\x2e\x8c\x9c\x38\x71\xe4\xf0" "\xce\xe3\xc7\x46\x8e\xb6\x24\xcf\x9e\x3c\xd6\x7d\x1f\x5c\x3c\xb0\xf7\xe4" "\x5b\xd7\x5f\x1b\x3b\x75\xfd\xed\x1f\xbe\xca\xe3\xdd\x5d\x6c\xaf\xcf\xa3" "\xa6\x6f\xdd\x6d\xf6\x47\xff\x9d\xe7\xb2\xce\xd3\xeb\x3e\xfa\xd6\xd2\x53" "\x57\x4e\xba\xda\x18\x08\x4d\xc9\xfb\x70\x7e\xb9\xba\xab\xfd\xbf\x37\x2a" "\x71\xfb\xe2\xf5\xc6\xab\x1f\xb5\x35\x38\x60\x43\x65\x59\x96\x6d\x5f\xb4" "\xb6\x52\x16\x66\x33\xe0\x1e\x96\x44\xbb\x23\x00\xda\xa3\xfc\x8f\xfe\xe6" "\x5c\xfe\xa4\x3a\x33\xb6\xf8\x39\xf8\xde\x76\xe3\xa5\xda\x03\x50\x9e\xf7" "\xad\x62\xa9\x6d\xe9\x8a\x34\x7f\x86\xef\xab\x3d\x1b\xa5\x1b\xd4\x7e\x7f" "\x44\x9c\x9a\xfd\xe7\xb3\x7c\x89\x25\xdf\x43\x00\x00\xb4\xd6\x37\xf9\xf8" "\xe7\xf9\xda\xb8\xa3\x5c\x6a\x5b\xd2\x78\xa8\x6e\xbf\xff\x17\x73\x43\x7d" "\x11\x71\x5f\x44\xec\x89\x88\xfb\x23\xe2\x81\x88\x78\x30\xa2\xba\xef\xc3" "\x11\xf1\x48\x93\xed\xf7\x2f\xa8\x2f\x1e\xff\xfc\xbc\x73\x4d\x89\xad\x52" "\x3e\xfe\x7b\xb1\x98\xdb\xba\x73\xfc\x37\x3f\xe2\xeb\xab\x14\xb5\x9e\x6a" "\xfe\xdd\xc9\xe9\xb3\x93\x13\x87\x8a\x73\x32\x10\xdd\xdb\xf3\xfa\xf0\x32" "\x6d\x7c\xfb\xca\x2f\x9f\x36\xda\x56\x3f\xfe\xcb\x97\xbc\xfd\x72\x2c\x58" "\xc4\xf1\x67\xd7\x82\x17\x74\xe3\xa3\xd3\xa3\xeb\xc9\xb9\xde\x8d\x0f\x23" "\xf6\x75\x2d\x95\x7f\x32\x3f\x13\x90\x3f\x19\xec\x8d\x88\x7d\x6b\x38\x7e" "\x7e\xce\xce\x3e\xfb\xe5\x81\x46\xdb\x57\xce\x7f\x19\x2d\x98\x67\xca\xbe" "\x88\x78\xa6\x76\xfd\x67\x63\x41\xfe\xa5\xa4\xe1\xfc\xe4\xf0\xf1\x63\x23" "\x47\x87\x76\xc4\xe4\xc4\xa1\xa1\xf2\xae\x58\xec\xc7\x9f\xae\xbd\xde\xa8" "\xfd\x75\xe5\xdf\x02\xf9\xf5\xff\xdf\x92\xf7\xff\x7c\xfe\x7d\x49\xfd\x7c" "\xed\x54\xf3\x6d\x5c\xfb\xed\x93\x86\xcf\x34\x6b\xbd\xff\xb7\x25\x6f\x56" "\xcb\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x68\xcd\x01\x2b\x9b\x1e\xfc\x8e\x68\xc5\x71\x22\x5d\x71\x9f\xae\x26\x7e" "\x11\xc3\xe6\x16\xd2\xad\x11\x46\xad\xb0\x3d\x22\x56\xbb\xf3\xd5\x16\xdd" "\x75\x2b\x17\xda\xfd\x2f\x13\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" "\x40\x6b\xfc\x17\x00\x00\xff\xff\x95\x74\xe2\x7b", 1146); syz_mount_image(0x20000440, 0x20000480, 0, 0x200000c0, 0x84, 0x47a, 0x20000940); return 0; }