From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Google-Smtp-Source: AH8x225+FYndGdEXSVQXHgEDbrlcdprkVvjd45chiS38MusTPDmX/r1SGMzwBtd1iMMahqMvh3jl ARC-Seal: i=1; a=rsa-sha256; t=1516611185; cv=none; d=google.com; s=arc-20160816; b=CiVUdywSzhSD61QjVwnGVFMo2OVW1RvyQZxSBBYA11QzdGDVzfrwi1e7+5K10YaptE 7WzF0qlIzqgb+jvXhNhja003tAcRC3aYBRbe9zDFsDi3+QnicWtFvIM6t4+zSLE/hmvI qMM2fDaikUavgtsMuewzMBCSFvhhNOXS7L1/KuXI2s98i6AJ7Ncm1g0IecRNYODW8kLT Bc4yhbUiWQPLi4mV9Z0SM9ZMYzKkKBXwtsvY/w5gNhGJ7b61riHfUD6sJDGN7o+YIhv4 zRjeU6IZwgy6e33Rb+hn5qxnrNlMUTqLljIj7Aa3/wSE/gEhryQ/gOJ3/3t9McbsYCs1 vgAw== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=mime-version:user-agent:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=GveX3dglFXCfX1hhjBePb9LvH0wrELtpN3IGbeLGoLE=; b=Ep3M+/JYO6xhEeZZ95N3CvAUG/u9Zil9D629CXQrT9ojsMIwsxDea+1vK4mgOFhbik Ss1JcfMBglpfbkSzmLd4Rr9M/vCRW9vr8lClKh3MpfhNyW0xJsQCuDmugwV0vK6PsTB6 CWXLUq3tOLzMVwV803vE492qpNewOkbZ/qKwsQeL3pu1w2T8X4V6JhuAAIXW3CfvUPhU X/9rQNMOXr2rn3PPjFwe4ZE4c3qe8SVUsUqa89YVu1JmRw6kDfcOaM2FhuKGKt0nrh5v sjm9ZiFjizM8bQqMPGQEG2va9e1W0UOCb7ZFnJtvl9e7IuMoAeiSAmrgK00CdUsUdBPS y5TA== ARC-Authentication-Results: i=1; mx.google.com; spf=softfail (google.com: domain of transitioning gregkh@linuxfoundation.org does not designate 90.92.71.90 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org Authentication-Results: mx.google.com; spf=softfail (google.com: domain of transitioning gregkh@linuxfoundation.org does not designate 90.92.71.90 as permitted sender) smtp.mailfrom=gregkh@linuxfoundation.org From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Alexey Dobriyan , "Kohli, Gaurav" , John Ogness , Peter Zijlstra , Ingo Molnar , Oleg Nesterov , Andrew Morton , Linus Torvalds Subject: [PATCH 4.14 65/89] proc: fix coredump vs read /proc/*/stat race Date: Mon, 22 Jan 2018 09:45:45 +0100 Message-Id: <20180122084001.037904053@linuxfoundation.org> X-Mailer: git-send-email 2.16.0 In-Reply-To: <20180122083954.683903493@linuxfoundation.org> References: <20180122083954.683903493@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-LABELS: =?utf-8?b?IlxcU2VudCI=?= X-GMAIL-THRID: =?utf-8?q?1590281827062060471?= X-GMAIL-MSGID: =?utf-8?q?1590282090097430642?= X-Mailing-List: linux-kernel@vger.kernel.org List-ID: 4.14-stable review patch. If anyone has any objections, please let me know. ------------------ From: Alexey Dobriyan commit 8bb2ee192e482c5d500df9f2b1b26a560bd3026f upstream. do_task_stat() accesses IP and SP of a task without bumping reference count of a stack (which became an entity with independent lifetime at some point). Steps to reproduce: #include #include #include #include #include #include #include #include int main(void) { setrlimit(RLIMIT_CORE, &(struct rlimit){}); while (1) { char buf[64]; char buf2[4096]; pid_t pid; int fd; pid = fork(); if (pid == 0) { *(volatile int *)0 = 0; } snprintf(buf, sizeof(buf), "/proc/%u/stat", pid); fd = open(buf, O_RDONLY); read(fd, buf2, sizeof(buf2)); close(fd); waitpid(pid, NULL, 0); } return 0; } BUG: unable to handle kernel paging request at 0000000000003fd8 IP: do_task_stat+0x8b4/0xaf0 PGD 800000003d73e067 P4D 800000003d73e067 PUD 3d558067 PMD 0 Oops: 0000 [#1] PREEMPT SMP PTI CPU: 0 PID: 1417 Comm: a.out Not tainted 4.15.0-rc8-dirty #2 Hardware name: QEMU Standard PC (i440FX + PIIX, 1996), BIOS 1.10.2-1.fc27 04/01/2014 RIP: 0010:do_task_stat+0x8b4/0xaf0 Call Trace: proc_single_show+0x43/0x70 seq_read+0xe6/0x3b0 __vfs_read+0x1e/0x120 vfs_read+0x84/0x110 SyS_read+0x3d/0xa0 entry_SYSCALL_64_fastpath+0x13/0x6c RIP: 0033:0x7f4d7928cba0 RSP: 002b:00007ffddb245158 EFLAGS: 00000246 Code: 03 b7 a0 01 00 00 4c 8b 4c 24 70 4c 8b 44 24 78 4c 89 74 24 18 e9 91 f9 ff ff f6 45 4d 02 0f 84 fd f7 ff ff 48 8b 45 40 48 89 ef <48> 8b 80 d8 3f 00 00 48 89 44 24 20 e8 9b 97 eb ff 48 89 44 24 RIP: do_task_stat+0x8b4/0xaf0 RSP: ffffc90000607cc8 CR2: 0000000000003fd8 John Ogness said: for my tests I added an else case to verify that the race is hit and correctly mitigated. Link: http://lkml.kernel.org/r/20180116175054.GA11513@avx2 Signed-off-by: Alexey Dobriyan Reported-by: "Kohli, Gaurav" Tested-by: John Ogness Cc: Peter Zijlstra Cc: Ingo Molnar Cc: Oleg Nesterov Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- fs/proc/array.c | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) --- a/fs/proc/array.c +++ b/fs/proc/array.c @@ -424,8 +424,11 @@ static int do_task_stat(struct seq_file * safe because the task has stopped executing permanently. */ if (permitted && (task->flags & PF_DUMPCORE)) { - eip = KSTK_EIP(task); - esp = KSTK_ESP(task); + if (try_get_task_stack(task)) { + eip = KSTK_EIP(task); + esp = KSTK_ESP(task); + put_task_stack(task); + } } }