blob: f123bdc6e3190a2a7bfba2f00a63b33cbc9263ee [file] [log] [blame]
// general protection fault in blk_get_backing_dev_info (2)
// https://syzkaller.appspot.com/bug?id=3f334eac15d01afc2d0fbe7dc6669cb1b3a492ef
// status:open
// autogenerated by syzkaller (https://github.com/google/syzkaller)
#define _GNU_SOURCE
#include <endian.h>
#include <errno.h>
#include <fcntl.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 <unistd.h>
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 int do_sandbox_none(void)
{
if (unshare(CLONE_NEWPID)) {
}
int pid = fork();
if (pid != 0)
return wait_for_loop(pid);
setup_common();
sandbox_common();
if (unshare(CLONE_NEWNET)) {
}
loop();
exit(1);
}
uint64_t r[1] = {0xffffffffffffffff};
void loop(void)
{
long res = 0;
memcpy((void*)0x20000000, "./file0\000", 8);
syscall(__NR_mknod, 0x20000000, 0, 0);
memcpy((void*)0x20000040, "/dev/fuse\000", 10);
res = syscall(__NR_openat, 0xffffffffffffff9c, 0x20000040, 2, 0);
if (res != -1)
r[0] = res;
memcpy((void*)0x20000340, "./file0\000", 8);
memcpy((void*)0x20000100, "fuse\000", 5);
memcpy((void*)0x200001c0, "fd", 2);
*(uint8_t*)0x200001c2 = 0x3d;
sprintf((char*)0x200001c3, "0x%016llx", (long long)r[0]);
*(uint8_t*)0x200001d5 = 0x2c;
memcpy((void*)0x200001d6, "rootmode", 8);
*(uint8_t*)0x200001de = 0x3d;
sprintf((char*)0x200001df, "%023llo", (long long)0x6000);
*(uint8_t*)0x200001f6 = 0x2c;
memcpy((void*)0x200001f7, "user_id", 7);
*(uint8_t*)0x200001fe = 0x3d;
sprintf((char*)0x200001ff, "%020llu", (long long)0);
*(uint8_t*)0x20000213 = 0x2c;
memcpy((void*)0x20000214, "group_id", 8);
*(uint8_t*)0x2000021c = 0x3d;
sprintf((char*)0x2000021d, "%020llu", (long long)0);
*(uint8_t*)0x20000231 = 0x2c;
*(uint8_t*)0x20000232 = 0;
syscall(__NR_mount, 0, 0x20000340, 0x20000100, 0, 0x200001c0);
memcpy((void*)0x20000080, "./file0\000", 8);
syscall(__NR_open, 0x20000080, 0x8000, 3);
*(uint32_t*)0x200000c0 = 0x29;
*(uint32_t*)0x200000c4 = 4;
*(uint64_t*)0x200000c8 = 0;
*(uint64_t*)0x200000d0 = 1;
*(uint64_t*)0x200000d8 = 0;
*(uint32_t*)0x200000e0 = 1;
*(uint32_t*)0x200000e4 = 0;
*(uint8_t*)0x200000e8 = 0;
syscall(__NR_write, r[0], 0x200000c0, 0x29);
}
int main(void)
{
syscall(__NR_mmap, 0x20000000, 0x1000000, 3, 0x32, -1, 0);
do_sandbox_none();
return 0;
}