net/ipv4: null-ptr-deref in udp_rmem_release/sk_memory_allocated_sub

From: Andrey Konovalov
Date: Thu Feb 09 2017 - 07:57:57 EST


Hi,

I've got the following error report while fuzzing the kernel with syzkaller.

On commit 926af6273fc683cd98cd0ce7bf0d04a02eed6742.

A reproducer and .config are attached.

BUG: unable to handle kernel NULL pointer dereference at (null)
IP: sk_memory_allocated_sub include/net/sock.h:1214 [inline]
IP: __sk_mem_reduce_allocated+0xa9/0x390 net/core/sock.c:2215
PGD 3aa86067
PUD 33243067
PMD 0

Oops: 0002 [#1] SMP KASAN
Dumping ftrace buffer:
(ftrace buffer empty)
Modules linked in:
CPU: 3 PID: 13215 Comm: syz-executor5 Not tainted 4.10.0-rc7+ #126
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS Bochs 01/01/2011
task: ffff88006bf59700 task.stack: ffff88005e0a8000
RIP: 0010:sk_memory_allocated_sub include/net/sock.h:1214 [inline]
RIP: 0010:__sk_mem_reduce_allocated+0xa9/0x390 net/core/sock.c:2215
RSP: 0018:ffff88005e0afa48 EFLAGS: 00010246
RAX: 0000000000000000 RBX: 1ffff1000bc15f4a RCX: 1ffffffff0bc7d8e
RDX: dffffc0000000000 RSI: ffffffff83343399 RDI: ffffffff85e3ec70
RBP: ffff88005e0afad8 R08: 0000000000000090 R09: 0000000000000000
R10: 0000000000000002 R11: 0000000000000000 R12: ffff88003dfb3c40
R13: 000000000003dfb4 R14: ffff88003dfb3c68 R15: ffffffff85e3eba0
FS: 00007f71b9ba9700(0000) GS:ffff88006df00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: 0000000000000000 CR3: 00000000393b8000 CR4: 00000000000006e0
Call Trace:
udp_rmem_release+0x14f/0x180 net/ipv4/udp.c:1199
first_packet_length+0x215/0x6e0 net/ipv4/udp.c:1380
udp_ioctl+0x13f/0x2c0 net/ipv4/udp.c:1401
inet6_ioctl+0x19e/0x1e0 net/ipv6/af_inet6.c:530
sock_do_ioctl+0x65/0xb0 net/socket.c:897
sock_ioctl+0x28f/0x440 net/socket.c:995
vfs_ioctl fs/ioctl.c:43 [inline]
do_vfs_ioctl+0x1bf/0x1780 fs/ioctl.c:683
SYSC_ioctl fs/ioctl.c:698 [inline]
SyS_ioctl+0x8f/0xc0 fs/ioctl.c:689
entry_SYSCALL_64_fastpath+0x1f/0xc2
RIP: 0033:0x4458b9
RSP: 002b:00007f71b9ba8b58 EFLAGS: 00000286 ORIG_RAX: 0000000000000010
RAX: ffffffffffffffda RBX: 0000000000000005 RCX: 00000000004458b9
RDX: 0000000020005f3e RSI: 000000000000541b RDI: 0000000000000005
RBP: 00000000006e0cc0 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000286 R12: 00000000007080a8
R13: 0000000000000000 R14: 00007f71b9ba99c0 R15: 00007f71b9ba9700
Code: 24 28 48 ba 00 00 00 00 00 fc ff df 49 8d bf d0 00 00 00 48 89
f9 48 c1 e9 03 80 3c 11 00 0f 85 8b 02 00 00 49 8b 87 d0 00 00 00 <f0>
4c 29 28 4d 8b 7c 24 28 48 ba 00 00 00 00 00 fc ff df 49 8d
RIP: sk_memory_allocated_sub include/net/sock.h:1214 [inline] RSP:
ffff88005e0afa48
RIP: __sk_mem_reduce_allocated+0xa9/0x390 net/core/sock.c:2215 RSP:
ffff88005e0afa48
CR2: 0000000000000000
---[ end trace 8b75e9ae315695b0 ]---
Kernel panic - not syncing: Fatal exception in interrupt
Dumping ftrace buffer:
(ftrace buffer empty)
Kernel Offset: disabled
Rebooting in 86400 seconds..
// autogenerated by syzkaller (http://github.com/google/syzkaller)

#ifndef __NR_ioctl
#define __NR_ioctl 16
#endif
#ifndef __NR_bind
#define __NR_bind 49
#endif
#ifndef __NR_connect
#define __NR_connect 42
#endif
#ifndef __NR_sendto
#define __NR_sendto 44
#endif
#ifndef __NR_mmap
#define __NR_mmap 9
#endif
#ifndef __NR_socket
#define __NR_socket 41
#endif

#define _GNU_SOURCE

#include <sys/ioctl.h>
#include <sys/mman.h>
#include <sys/mount.h>
#include <sys/prctl.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <linux/capability.h>
#include <linux/if.h>
#include <linux/if_tun.h>
#include <linux/kvm.h>
#include <linux/sched.h>
#include <net/if_arp.h>

#include <assert.h>
#include <dirent.h>
#include <errno.h>
#include <fcntl.h>
#include <grp.h>
#include <pthread.h>
#include <setjmp.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

const int kFailStatus = 67;
const int kErrorStatus = 68;
const int kRetryStatus = 69;

__attribute__((noreturn)) void doexit(int status)
{
volatile unsigned i;
syscall(__NR_exit_group, status);
for (i = 0;; i++) {
}
}

__attribute__((noreturn)) void fail(const char* msg, ...)
{
int e = errno;
fflush(stdout);
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fprintf(stderr, " (errno %d)\n", e);
doexit(e == ENOMEM ? kRetryStatus : kFailStatus);
}

__attribute__((noreturn)) void exitf(const char* msg, ...)
{
int e = errno;
fflush(stdout);
va_list args;
va_start(args, msg);
vfprintf(stderr, msg, args);
va_end(args);
fprintf(stderr, " (errno %d)\n", e);
doexit(kRetryStatus);
}

static int flag_debug;

void debug(const char* msg, ...)
{
if (!flag_debug)
return;
va_list args;
va_start(args, msg);
vfprintf(stdout, msg, args);
va_end(args);
fflush(stdout);
}

__thread int skip_segv;
__thread jmp_buf segv_env;

static void segv_handler(int sig, siginfo_t* info, void* uctx)
{
uintptr_t addr = (uintptr_t)info->si_addr;
const uintptr_t prog_start = 1 << 20;
const uintptr_t prog_end = 100 << 20;
if (__atomic_load_n(&skip_segv, __ATOMIC_RELAXED) &&
(addr < prog_start || addr > prog_end)) {
debug("SIGSEGV on %p, skipping\n", addr);
_longjmp(segv_env, 1);
}
debug("SIGSEGV on %p, exiting\n", addr);
doexit(sig);
for (;;) {
}
}

static void install_segv_handler()
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_sigaction = segv_handler;
sa.sa_flags = SA_NODEFER | SA_SIGINFO;
sigaction(SIGSEGV, &sa, NULL);
sigaction(SIGBUS, &sa, NULL);
}

#define NONFAILING(...) \
{ \
__atomic_fetch_add(&skip_segv, 1, __ATOMIC_SEQ_CST); \
if (_setjmp(segv_env) == 0) { \
__VA_ARGS__; \
} \
__atomic_fetch_sub(&skip_segv, 1, __ATOMIC_SEQ_CST); \
}

#define BITMASK_LEN(type, bf_len) (type)((1ull << (bf_len)) - 1)

#define BITMASK_LEN_OFF(type, bf_off, bf_len) \
(type)(BITMASK_LEN(type, (bf_len)) << (bf_off))

#define STORE_BY_BITMASK(type, addr, val, bf_off, bf_len) \
if ((bf_off) == 0 && (bf_len) == 0) { \
*(type*)(addr) = (type)(val); \
} else { \
type new_val = *(type*)(addr); \
new_val &= ~BITMASK_LEN_OFF(type, (bf_off), (bf_len)); \
new_val |= ((type)(val)&BITMASK_LEN(type, (bf_len))) << (bf_off); \
*(type*)(addr) = new_val; \
}

static uintptr_t execute_syscall(int nr, uintptr_t a0, uintptr_t a1,
uintptr_t a2, uintptr_t a3,
uintptr_t a4, uintptr_t a5,
uintptr_t a6, uintptr_t a7,
uintptr_t a8)
{
switch (nr) {
default:
return syscall(nr, a0, a1, a2, a3, a4, a5);
}
}

static void setup_main_process()
{
struct sigaction sa;
memset(&sa, 0, sizeof(sa));
sa.sa_handler = SIG_IGN;
syscall(SYS_rt_sigaction, 0x20, &sa, NULL, 8);
syscall(SYS_rt_sigaction, 0x21, &sa, NULL, 8);
install_segv_handler();

char tmpdir_template[] = "./syzkaller.XXXXXX";
char* tmpdir = mkdtemp(tmpdir_template);
if (!tmpdir)
fail("failed to mkdtemp");
if (chmod(tmpdir, 0777))
fail("failed to chmod");
if (chdir(tmpdir))
fail("failed to chdir");
}

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 = 128 << 20;
setrlimit(RLIMIT_AS, &rlim);
rlim.rlim_cur = rlim.rlim_max = 1 << 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);

unshare(CLONE_NEWNS);
unshare(CLONE_NEWIPC);
unshare(CLONE_IO);
}

static int do_sandbox_none(int executor_pid, bool enable_tun)
{
int pid = fork();
if (pid)
return pid;

sandbox_common();

loop();
doexit(1);
}

long r[19];
void* thr(void* arg)
{
switch ((long)arg) {
case 0:
r[0] =
execute_syscall(__NR_mmap, 0x20000000ul, 0x987000ul, 0x3ul,
0x32ul, 0xfffffffffffffffful, 0x0ul, 0, 0, 0);
break;
case 1:
r[1] = execute_syscall(__NR_socket, 0xaul, 0x2ul, 0x73ul, 0, 0, 0,
0, 0, 0);
break;
case 2:
r[2] = execute_syscall(__NR_ioctl, r[1], 0x541bul, 0x20005f3eul, 0,
0, 0, 0, 0, 0);
break;
case 3:
NONFAILING(*(uint16_t*)0x20000fe0 = (uint16_t)0xa);
NONFAILING(*(uint16_t*)0x20000fe2 = (uint16_t)0x234e);
NONFAILING(*(uint32_t*)0x20000fe4 = (uint32_t)0x20000000000000);
NONFAILING(*(uint64_t*)0x20000fe8 = (uint64_t)0x0);
NONFAILING(*(uint64_t*)0x20000ff0 = (uint64_t)0x100000000000000);
NONFAILING(*(uint32_t*)0x20000ff8 = (uint32_t)0x1);
r[9] = execute_syscall(__NR_bind, r[1], 0x20000fe0ul, 0x20ul, 0, 0,
0, 0, 0, 0);
break;
case 4:
NONFAILING(*(uint16_t*)0x20000000 = (uint16_t)0xa);
NONFAILING(*(uint16_t*)0x20000002 = (uint16_t)0x204e);
NONFAILING(*(uint32_t*)0x20000004 = (uint32_t)0xfd4);
NONFAILING(*(uint64_t*)0x20000008 = (uint64_t)0x0);
NONFAILING(*(uint64_t*)0x20000010 = (uint64_t)0x0);
NONFAILING(*(uint32_t*)0x20000018 = (uint32_t)0x5);
r[16] = execute_syscall(__NR_connect, r[1], 0x20000000ul, 0x20ul, 0,
0, 0, 0, 0, 0);
break;
case 5:
NONFAILING(memcpy(
(void*)0x20003f3f,
"\xc9\x00\x00\x00\x00\x00\x00\x00\x00\x14\xf7\xa4\x1c\x95\x84"
"\x6e\xac\xb3\xee\x27\xb4\x75\x68\xc5\x83\x6b\xb8\x94\x1e\x43"
"\x8c\x9a\x7b\x37\x8a\xb5\xfb\xf7\x1e\x8a\xc8\x36\xca\xf8\x82"
"\x4a\xc0\x76\x79\x35\x52\xec\x17\xcc\x77\xb2\xbb\x79\x41\x1a"
"\xae\x8c\x23\x52\xa8\x8d\x17\x90\x37\x6e\xb1\xdd\x72\x03\xa2"
"\x70\xa3\xc3\xd3\x84\xe6\xd3\x75\x95\xf4\x34\x30\x93\x0c\x0c"
"\x3b\x39\x15\x7b\x30\x4a\x44\xb1\x4c\x54\xec\x64\xf4\xc6\x71"
"\x73\x4e\x00\x9e\x23\xcf\xf8\x15\xcc\xbe\x36\x31\x5c\x92\x19"
"\x71\x8d\xe7\x2c\xaa\x3f\xbf\x08\xe6\x68\xaa\x28\x37\x0f\xee"
"\x68\xfa\xa1\xa8\xda\x01\xcd\x29\x28\xb3\xb7\x77\x04\x6b\x57"
"\xa9\x8f\xad\xb5\xf6\x3a\x82\xbb\x62\x9a\xe3\x07\x83\xde\xbc"
"\x23\x11\x29\x7f\x52\x2b\x6d\x6e\x78\x38\x6e\x49\x20\xbe\x61"
"\xfe\xeb\x81\x04\x00\x00\x00\x00\x00\x00\xae\x05\xd9",
193));
r[18] = execute_syscall(__NR_sendto, r[1], 0x20003f3ful, 0xc1ul,
0x4ul, 0x0ul, 0x0ul, 0, 0, 0);
break;
}
return 0;
}

void loop()
{
long i;
pthread_t th[12];

memset(r, -1, sizeof(r));
srand(getpid());
for (i = 0; i < 6; i++) {
pthread_create(&th[i], 0, thr, (void*)i);
usleep(10000);
}
for (i = 0; i < 6; i++) {
pthread_create(&th[6 + i], 0, thr, (void*)i);
if (rand() % 2)
usleep(rand() % 10000);
}
usleep(100000);
}

int main()
{
setup_main_process();
int pid = do_sandbox_none(0, false);
int status = 0;
while (waitpid(pid, &status, __WALL) != pid) {
}
return 0;
}

Attachment: .config
Description: Binary data