blob: e09cd1821328f3b4896a1c8dda42d95f237e99f1 [file] [log] [blame]
// BUG: sleeping function called from invalid context in htb_destroy
// https://syzkaller.appspot.com/bug?id=d1e4c9e39045dc33b093078bbf9c4328f2777133
// status:open
// autogenerated by syzkaller (https://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <pthread.h>
#include <sched.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.h>
#include <linux/capability.h>
#include <linux/futex.h>
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 thread_start(void* (*fn)(void*), void* arg)
{
pthread_t th;
pthread_attr_t attr;
pthread_attr_init(&attr);
pthread_attr_setstacksize(&attr, 128 << 10);
int i;
for (i = 0; i < 100; i++) {
if (pthread_create(&th, &attr, fn, arg) == 0) {
pthread_attr_destroy(&attr);
return;
}
if (errno == EAGAIN) {
usleep(50);
continue;
}
break;
}
exit(1);
}
typedef struct {
int state;
} event_t;
static void event_init(event_t* ev)
{
ev->state = 0;
}
static void event_reset(event_t* ev)
{
ev->state = 0;
}
static void event_set(event_t* ev)
{
if (ev->state)
exit(1);
__atomic_store_n(&ev->state, 1, __ATOMIC_RELEASE);
syscall(SYS_futex, &ev->state, FUTEX_WAKE | FUTEX_PRIVATE_FLAG);
}
static void event_wait(event_t* ev)
{
while (!__atomic_load_n(&ev->state, __ATOMIC_ACQUIRE))
syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, 0);
}
static int event_isset(event_t* ev)
{
return __atomic_load_n(&ev->state, __ATOMIC_ACQUIRE);
}
static int event_timedwait(event_t* ev, uint64_t timeout)
{
uint64_t start = current_time_ms();
uint64_t now = start;
for (;;) {
uint64_t remain = timeout - (now - start);
struct timespec ts;
ts.tv_sec = remain / 1000;
ts.tv_nsec = (remain % 1000) * 1000 * 1000;
syscall(SYS_futex, &ev->state, FUTEX_WAIT | FUTEX_PRIVATE_FLAG, 0, &ts);
if (__atomic_load_n(&ev->state, __ATOMIC_RELAXED))
return 1;
now = current_time_ms();
if (now - start > timeout)
return 0;
}
}
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;
}
static void setup_common()
{
if (mount(0, "/sys/fs/fuse/connections", "fusectl", 0, 0)) {
}
}
static void loop();
static void sandbox_common()
{
prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
setpgrp();
setsid();
struct rlimit rlim;
rlim.rlim_cur = rlim.rlim_max = (200 << 20);
setrlimit(RLIMIT_AS, &rlim);
rlim.rlim_cur = rlim.rlim_max = 32 << 20;
setrlimit(RLIMIT_MEMLOCK, &rlim);
rlim.rlim_cur = rlim.rlim_max = 136 << 20;
setrlimit(RLIMIT_FSIZE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 1 << 20;
setrlimit(RLIMIT_STACK, &rlim);
rlim.rlim_cur = rlim.rlim_max = 0;
setrlimit(RLIMIT_CORE, &rlim);
rlim.rlim_cur = rlim.rlim_max = 256;
setrlimit(RLIMIT_NOFILE, &rlim);
if (unshare(CLONE_NEWNS)) {
}
if (unshare(CLONE_NEWIPC)) {
}
if (unshare(0x02000000)) {
}
if (unshare(CLONE_NEWUTS)) {
}
if (unshare(CLONE_SYSVSEM)) {
}
typedef struct {
const char* name;
const char* value;
} sysctl_t;
static const sysctl_t sysctls[] = {
{"/proc/sys/kernel/shmmax", "16777216"},
{"/proc/sys/kernel/shmall", "536870912"},
{"/proc/sys/kernel/shmmni", "1024"},
{"/proc/sys/kernel/msgmax", "8192"},
{"/proc/sys/kernel/msgmni", "1024"},
{"/proc/sys/kernel/msgmnb", "1024"},
{"/proc/sys/kernel/sem", "1024 1048576 500 1024"},
};
unsigned i;
for (i = 0; i < sizeof(sysctls) / sizeof(sysctls[0]); i++)
write_file(sysctls[i].name, sysctls[i].value);
}
int wait_for_loop(int pid)
{
if (pid < 0)
exit(1);
int status = 0;
while (waitpid(-1, &status, __WALL) != pid) {
}
return WEXITSTATUS(status);
}
static void drop_caps(void)
{
struct __user_cap_header_struct cap_hdr = {};
struct __user_cap_data_struct cap_data[2] = {};
cap_hdr.version = _LINUX_CAPABILITY_VERSION_3;
cap_hdr.pid = getpid();
if (syscall(SYS_capget, &cap_hdr, &cap_data))
exit(1);
const int drop = (1 << CAP_SYS_PTRACE) | (1 << CAP_SYS_NICE);
cap_data[0].effective &= ~drop;
cap_data[0].permitted &= ~drop;
cap_data[0].inheritable &= ~drop;
if (syscall(SYS_capset, &cap_hdr, &cap_data))
exit(1);
}
static int do_sandbox_none(void)
{
if (unshare(CLONE_NEWPID)) {
}
int pid = fork();
if (pid != 0)
return wait_for_loop(pid);
setup_common();
sandbox_common();
drop_caps();
if (unshare(CLONE_NEWNET)) {
}
loop();
exit(1);
}
static void close_fds()
{
int fd;
for (fd = 3; fd < 30; fd++)
close(fd);
}
struct thread_t {
int created, call;
event_t ready, done;
};
static struct thread_t threads[16];
static void execute_call(int call);
static int running;
static void* thr(void* arg)
{
struct thread_t* th = (struct thread_t*)arg;
for (;;) {
event_wait(&th->ready);
event_reset(&th->ready);
execute_call(th->call);
__atomic_fetch_sub(&running, 1, __ATOMIC_RELAXED);
event_set(&th->done);
}
return 0;
}
static void loop(void)
{
int i, call, thread;
int collide = 0;
again:
for (call = 0; call < 10; call++) {
for (thread = 0; thread < (int)(sizeof(threads) / sizeof(threads[0]));
thread++) {
struct thread_t* th = &threads[thread];
if (!th->created) {
th->created = 1;
event_init(&th->ready);
event_init(&th->done);
event_set(&th->done);
thread_start(thr, th);
}
if (!event_isset(&th->done))
continue;
event_reset(&th->done);
th->call = call;
__atomic_fetch_add(&running, 1, __ATOMIC_RELAXED);
event_set(&th->ready);
if (collide && (call % 2) == 0)
break;
event_timedwait(&th->done, 45);
break;
}
}
for (i = 0; i < 100 && __atomic_load_n(&running, __ATOMIC_RELAXED); i++)
sleep_ms(1);
close_fds();
if (!collide) {
collide = 1;
goto again;
}
}
uint64_t r[5] = {0xffffffffffffffff, 0xffffffffffffffff, 0xffffffffffffffff,
0xffffffffffffffff, 0x0};
void execute_call(int call)
{
intptr_t res;
switch (call) {
case 0:
res = syscall(__NR_socket, 0x10, 3, 0);
if (res != -1)
r[0] = res;
break;
case 1:
res = syscall(__NR_socket, 0x10, 3, 0);
if (res != -1)
r[1] = res;
break;
case 2:
res = syscall(__NR_socket, 0x10, 3, 0);
if (res != -1)
r[2] = res;
break;
case 3:
syscall(__NR_openat, 0xffffffffffffff9c, 0, 2, 0);
break;
case 4:
res = syscall(__NR_socket, 0x10, 0x803, 0);
if (res != -1)
r[3] = res;
break;
case 5:
*(uint64_t*)0x200001c0 = 0;
*(uint32_t*)0x200001c8 = 0;
*(uint64_t*)0x200001d0 = 0x20000180;
*(uint64_t*)0x20000180 = 0;
*(uint64_t*)0x20000188 = 0;
*(uint64_t*)0x200001d8 = 1;
*(uint64_t*)0x200001e0 = 0;
*(uint64_t*)0x200001e8 = 0;
*(uint32_t*)0x200001f0 = 0;
syscall(__NR_sendmsg, r[3], 0x200001c0, 0);
break;
case 6:
*(uint32_t*)0x20000200 = 0x14;
res = syscall(__NR_getsockname, r[3], 0x20000440, 0x20000200);
if (res != -1)
r[4] = *(uint32_t*)0x20000444;
break;
case 7:
syscall(__NR_sendmsg, r[2], 0, 0);
break;
case 8:
*(uint64_t*)0x20000240 = 0;
*(uint32_t*)0x20000248 = 0;
*(uint64_t*)0x20000250 = 0x20000080;
*(uint64_t*)0x20000080 = 0x20003e40;
*(uint32_t*)0x20003e40 = 0x148;
*(uint16_t*)0x20003e44 = 0x24;
*(uint16_t*)0x20003e46 = 0x507;
*(uint32_t*)0x20003e48 = 0;
*(uint32_t*)0x20003e4c = 0;
*(uint8_t*)0x20003e50 = 0;
*(uint32_t*)0x20003e54 = r[4];
*(uint16_t*)0x20003e58 = 0;
*(uint16_t*)0x20003e5a = 0;
*(uint16_t*)0x20003e5c = 0xfff1;
*(uint16_t*)0x20003e5e = -1;
*(uint16_t*)0x20003e60 = 0;
*(uint16_t*)0x20003e62 = 0;
*(uint16_t*)0x20003e64 = 8;
*(uint16_t*)0x20003e66 = 1;
memcpy((void*)0x20003e68, "red\000", 4);
*(uint16_t*)0x20003e6c = 0x11c;
*(uint16_t*)0x20003e6e = 2;
*(uint16_t*)0x20003e70 = 0x104;
*(uint16_t*)0x20003e72 = 2;
memcpy((void*)0x20003e74,
"\x77\x83\xaf\x7a\xa5\x52\xeb\x10\x64\x87\x31\x14\x62\xcb\x07\xa4"
"\x1b\x2d\x4a\xa6\x25\xa8\x11\xc4\xe1\xd4\xf2\x28\x59\xba\x16\x76"
"\x8b\xc2\x65\xca\xdf\xb9\x46\xf0\x47\x8d\x60\x12\x5c\x62\x0f\x5c"
"\x75\x58\x19\x93\x27\x7e\xed\xc5\xda\x9c\xe7\x08\x42\x07\x01\x87"
"\x8c\xcc\x94\xc9\xb2\xc2\xc1\x32\x55\x2d\xa7\xdb\xe8\xb2\x70\x9d"
"\x09\x3e\x52\xcb\x0d\x3f\x3b\xeb\xde\x8a\x18\xd1\x59\x2b\x8b\xf4"
"\xc5\x53\xc8\xfc\xf1\x90\x1a\x35\x83\x67\xc4\xf9\x23\x0c\xea\x8b"
"\x6b\xe9\x95\xf2\x94\x5f\x5f\xa7\xa7\x63\x55\x76\xf7\x7f\x82\x71"
"\x4d\x85\x71\x40\x6b\x73\x2d\x7a\x37\x10\x9c\xa0\x8c\x00\xcc\x02"
"\xd0\xcb\x8a\x2d\xaf\xff\xba\xa6\x2b\x8f\x1f\x7f\x69\xae\x9c\xd0"
"\x9f\x45\xb8\x3a\x0d\x21\x66\x23\x8b\x61\x01\x93\xb9\x4b\x38\x34"
"\x94\x43\xf9\x9f\xa0\x4a\x15\x20\x58\xc8\x60\xf9\xcc\xcb\xa1\xab"
"\x49\x83\x4f\xc5\x50\x7f\xa2\x3b\x46\xb5\x71\xf9\x03\xc4\x6c\x15"
"\x67\xbb\xcb\x4f\x99\x5f\x7a\x2c\x32\x83\x76\x92\x4b\x9a\x78\x98"
"\x9d\xd0\x5d\xd3\x18\x70\x95\x44\xd2\x56\xf9\x09\x33\x99\x88\x7c"
"\xde\xa8\xc5\x04\xf3\x0c\x1c\xe9\xef\x5d\x42\xa6\x84\x24\x5d\xb3",
256);
*(uint16_t*)0x20003f74 = 0x14;
*(uint16_t*)0x20003f76 = 1;
*(uint32_t*)0x20003f78 = 1;
*(uint32_t*)0x20003f7c = 0;
*(uint32_t*)0x20003f80 = 0;
*(uint8_t*)0x20003f84 = 0;
*(uint8_t*)0x20003f85 = -1;
*(uint8_t*)0x20003f86 = 0;
*(uint8_t*)0x20003f87 = 0;
*(uint64_t*)0x20000088 = 0x148;
*(uint64_t*)0x20000258 = 1;
*(uint64_t*)0x20000260 = 0;
*(uint64_t*)0x20000268 = 0;
*(uint32_t*)0x20000270 = 0;
syscall(__NR_sendmsg, r[1], 0x20000240, 0);
break;
case 9:
*(uint64_t*)0x20000240 = 0;
*(uint32_t*)0x20000248 = 0;
*(uint64_t*)0x20000250 = 0x20000080;
*(uint64_t*)0x20000080 = 0x20000180;
*(uint32_t*)0x20000180 = 0x48;
*(uint16_t*)0x20000184 = 0x24;
*(uint16_t*)0x20000186 = 0x507;
*(uint32_t*)0x20000188 = 0;
*(uint32_t*)0x2000018c = 0;
*(uint8_t*)0x20000190 = 0;
*(uint32_t*)0x20000194 = r[4];
*(uint16_t*)0x20000198 = 0;
*(uint16_t*)0x2000019a = 0;
*(uint16_t*)0x2000019c = 0;
*(uint16_t*)0x2000019e = -1;
*(uint16_t*)0x200001a0 = 0;
*(uint16_t*)0x200001a2 = 0;
*(uint16_t*)0x200001a4 = 8;
*(uint16_t*)0x200001a6 = 1;
memcpy((void*)0x200001a8, "htb\000", 4);
*(uint16_t*)0x200001ac = 0x1c;
*(uint16_t*)0x200001ae = 2;
*(uint16_t*)0x200001b0 = 0x18;
*(uint16_t*)0x200001b2 = 2;
*(uint32_t*)0x200001b4 = 3;
*(uint32_t*)0x200001b8 = 0;
*(uint32_t*)0x200001bc = 0;
*(uint32_t*)0x200001c0 = 0;
*(uint32_t*)0x200001c4 = 0;
*(uint64_t*)0x20000088 = 0x48;
*(uint64_t*)0x20000258 = 1;
*(uint64_t*)0x20000260 = 0;
*(uint64_t*)0x20000268 = 0;
*(uint32_t*)0x20000270 = 0;
syscall(__NR_sendmsg, r[0], 0x20000240, 0);
break;
}
}
int main(void)
{
syscall(__NR_mmap, 0x20000000, 0x1000000, 3, 0x32, -1, 0);
do_sandbox_none();
return 0;
}