BUG: unable to handle kernel paging request in n_tty_open

From: Dipanjan Das
Date: Sun Feb 12 2023 - 12:00:36 EST


Hi,

We would like to report the following bug which has been found by our
modified version of syzkaller.

======================================================
description: BUG: unable to handle kernel paging request in n_tty_open
affected file: drivers/tty/tty_ldisc.c
kernel version: 6.2.0-rc5
kernel commit: 41c66f47061608dc1fd493eebce198f0e74cc2d7
git tree: kmsan
kernel config: https://syzkaller.appspot.com/text?tag=KernelConfig&x=a9a22da1efde3af6
crash reproducer: attached
======================================================
Crash log:
======================================================
RBP: 00007f7117670c30 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 000000000000000d
R13: 00007f71165abf6c R14: 00007f71165abf60 R15: 00007f7117670d80
</TASK>
BUG: unable to handle page fault for address: ffffd10016bfa050
#PF: supervisor write access in kernel mode
#PF: error_code(0x0002) - not-present page
PGD 11410067 P4D 11410067 PUD 1157d067 PMD 31910067 PTE 0
Oops: 0002 [#1] PREEMPT SMP
CPU: 1 PID: 14496 Comm: syz-executor.4 Not tainted
6.2.0-rc5-00010-g41c66f470616 #8
Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS
1.13.0-1ubuntu1.1 04/01/2014
RIP: 0010:n_tty_open+0xa5/0x240 drivers/tty/n_tty.c:1883
Code: c8 48 c7 c7 c0 79 c0 8f e8 f8 0e ef fb 4d 8d 67 50 48 8b 18 8b
02 89 45 d0 4d 85 ed 0f 85 12 01 00 00 4c 89 e7 e8 0b 0f ef fb <48> 89
18 48 85 db 0f 85 0b 01 00 00 4d 8d a7 88 22 00 00 48 8b 45
RSP: 0018:ffff8880a72036a0 EFLAGS: 00010286
RAX: ffffd10016bfa050 RBX: 0000000000000000 RCX: ffffd10016bfa050
RDX: ffffd90016bfa050 RSI: 0000000000000001 RDI: ffffc90016bfa050
RBP: ffff8880a72036e0 R08: 0000000000000001 R09: ffff8880a6a03458
R10: 0000000000000001 R11: 0000000000000000 R12: ffffc90016bfa050
R13: 0000000000000000 R14: ffff88808b6c1000 R15: ffffc90016bfa000
FS: 00007f7117671700(0000) GS:ffff88813fc00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: ffffd10016bfa050 CR3: 00000000a7110000 CR4: 0000000000150ee0
Call Trace:
<TASK>
tty_ldisc_open drivers/tty/tty_ldisc.c:433 [inline]
tty_ldisc_setup+0xfc/0x4c0 drivers/tty/tty_ldisc.c:740
tty_init_dev+0x4b0/0xad0 drivers/tty/tty_io.c:1437
tty_open_by_driver drivers/tty/tty_io.c:2079 [inline]
tty_open+0x176f/0x20b0 drivers/tty/tty_io.c:2126
chrdev_open+0xa45/0xb80 fs/char_dev.c:414
do_dentry_open+0xf85/0x1970 fs/open.c:882
vfs_open+0x75/0xa0 fs/open.c:1013
do_open fs/namei.c:3557 [inline]
path_openat+0x4c4b/0x5a00 fs/namei.c:3714
do_filp_open+0x252/0x660 fs/namei.c:3741
do_sys_openat2+0x200/0x920 fs/open.c:1310
do_sys_open fs/open.c:1326 [inline]
__do_sys_openat fs/open.c:1342 [inline]
__se_sys_openat fs/open.c:1337 [inline]
__x64_sys_openat+0x285/0x310 fs/open.c:1337
do_syscall_x64 arch/x86/entry/common.c:50 [inline]
do_syscall_64+0x3d/0xb0 arch/x86/entry/common.c:80
entry_SYSCALL_64_after_hwframe+0x63/0xcd
RIP: 0033:0x7f711648d59d
Code: 02 b8 ff ff ff ff c3 66 0f 1f 44 00 00 f3 0f 1e fa 48 89 f8 48
89 f7 48 89 d6 48 89 ca 4d 89 c2 4d 89 c8 4c 8b 4c 24 08 0f 05 <48> 3d
01 f0 ff ff 73 01 c3 48 c7 c1 b8 ff ff ff f7 d8 64 89 01 48
RSP: 002b:00007f7117670bd8 EFLAGS: 00000246 ORIG_RAX: 0000000000000101
RAX: ffffffffffffffda RBX: 00007f71165abf60 RCX: 00007f711648d59d
RDX: 00000000000a6503 RSI: 0000000020000000 RDI: ffffffffffffff9c
RBP: 00007f7117670c30 R08: 0000000000000000 R09: 0000000000000000
R10: 0000000000000000 R11: 0000000000000246 R12: 000000000000000d
R13: 00007f71165abf6c R14: 00007f71165abf60 R15: 00007f7117670d80
</TASK>
Modules linked in:
CR2: ffffd10016bfa050
---[ end trace 0000000000000000 ]---
RIP: 0010:n_tty_open+0xa5/0x240 drivers/tty/n_tty.c:1883
Code: c8 48 c7 c7 c0 79 c0 8f e8 f8 0e ef fb 4d 8d 67 50 48 8b 18 8b
02 89 45 d0 4d 85 ed 0f 85 12 01 00 00 4c 89 e7 e8 0b 0f ef fb <48> 89
18 48 85 db 0f 85 0b 01 00 00 4d 8d a7 88 22 00 00 48 8b 45
RSP: 0018:ffff8880a72036a0 EFLAGS: 00010286
RAX: ffffd10016bfa050 RBX: 0000000000000000 RCX: ffffd10016bfa050
RDX: ffffd90016bfa050 RSI: 0000000000000001 RDI: ffffc90016bfa050
RBP: ffff8880a72036e0 R08: 0000000000000001 R09: ffff8880a6a03458
R10: 0000000000000001 R11: 0000000000000000 R12: ffffc90016bfa050
R13: 0000000000000000 R14: ffff88808b6c1000 R15: ffffc90016bfa000
FS: 00007f7117671700(0000) GS:ffff88813fc00000(0000) knlGS:0000000000000000
CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033
CR2: ffffd10016bfa050 CR3: 00000000a7110000 CR4: 0000000000150ee0
----------------
Code disassembly (best guess):
0: c8 48 c7 c7 enterq $0xc748,$0xc7
4: c0 79 c0 8f sarb $0x8f,-0x40(%rcx)
8: e8 f8 0e ef fb callq 0xfbef0f05
d: 4d 8d 67 50 lea 0x50(%r15),%r12
11: 48 8b 18 mov (%rax),%rbx
14: 8b 02 mov (%rdx),%eax
16: 89 45 d0 mov %eax,-0x30(%rbp)
19: 4d 85 ed test %r13,%r13
1c: 0f 85 12 01 00 00 jne 0x134
22: 4c 89 e7 mov %r12,%rdi
25: e8 0b 0f ef fb callq 0xfbef0f35
* 2a: 48 89 18 mov %rbx,(%rax) <-- trapping instruction
2d: 48 85 db test %rbx,%rbx
30: 0f 85 0b 01 00 00 jne 0x141
36: 4d 8d a7 88 22 00 00 lea 0x2288(%r15),%r12
3d: 48 rex.W
3e: 8b .byte 0x8b
3f: 45 rex.RB

--
Thanks and Regards,

Dipanjan
// autogenerated by syzkaller (https://github.com/google/syzkaller)

#define _GNU_SOURCE

#include <dirent.h>
#include <endian.h>
#include <errno.h>
#include <fcntl.h>
#include <signal.h>
#include <stdarg.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/prctl.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <unistd.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 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 int inject_fault(int nth)
{
int fd;
fd = open("/proc/thread-self/fail-nth", O_RDWR);
if (fd == -1)
exit(1);
char buf[16];
sprintf(buf, "%d", nth);
if (write(fd, buf, strlen(buf)) != (ssize_t)strlen(buf))
exit(1);
return fd;
}

static void kill_and_wait(int pid, int* status)
{
kill(-pid, SIGKILL);
kill(pid, SIGKILL);
for (int i = 0; i < 100; i++) {
if (waitpid(-1, status, WNOHANG | __WALL) == pid)
return;
usleep(1000);
}
DIR* dir = opendir("/sys/fs/fuse/connections");
if (dir) {
for (;;) {
struct dirent* ent = readdir(dir);
if (!ent)
break;
if (strcmp(ent->d_name, ".") == 0 || strcmp(ent->d_name, "..") == 0)
continue;
char abort[300];
snprintf(abort, sizeof(abort), "/sys/fs/fuse/connections/%s/abort", ent->d_name);
int fd = open(abort, O_WRONLY);
if (fd == -1) {
continue;
}
if (write(fd, abort, 1) < 0) {
}
close(fd);
}
closedir(dir);
} else {
}
while (waitpid(-1, status, __WALL) != pid) {
}
}

static void setup_test()
{
prctl(PR_SET_PDEATHSIG, SIGKILL, 0, 0, 0);
setpgrp();
write_file("/proc/self/oom_score_adj", "1000");
}

static void setup_fault()
{
static struct {
const char* file;
const char* val;
bool fatal;
} files[] = {
{"/sys/kernel/debug/failslab/ignore-gfp-wait", "N", true},
{"/sys/kernel/debug/fail_futex/ignore-private", "N", false},
{"/sys/kernel/debug/fail_page_alloc/ignore-gfp-highmem", "N", false},
{"/sys/kernel/debug/fail_page_alloc/ignore-gfp-wait", "N", false},
{"/sys/kernel/debug/fail_page_alloc/min-order", "0", false},
};
unsigned i;
for (i = 0; i < sizeof(files) / sizeof(files[0]); i++) {
if (!write_file(files[i].file, files[i].val)) {
if (files[i].fatal)
exit(1);
}
}
}

static void execute_one(void);

#define WAIT_FLAGS __WALL

static void loop(void)
{
int iter = 0;
for (;; iter++) {
int pid = fork();
if (pid < 0)
exit(1);
if (pid == 0) {
setup_test();
execute_one();
exit(0);
}
int status = 0;
uint64_t start = current_time_ms();
for (;;) {
if (waitpid(-1, &status, WNOHANG | WAIT_FLAGS) == pid)
break;
sleep_ms(1);
if (current_time_ms() - start < 5000)
continue;
kill_and_wait(pid, &status);
break;
}
}
}

void execute_one(void)
{
memcpy((void*)0x20000000, "/dev/ttyS3\000", 11);
inject_fault(13);
syscall(__NR_openat, 0xffffffffffffff9cul, 0x20000000ul, 0xa6503ul, 0ul);

}
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);
setup_fault();
loop();
return 0;
}

Attachment: repro.syz
Description: Binary data