Received: by 2002:a25:8b91:0:0:0:0:0 with SMTP id j17csp2299064ybl; Thu, 19 Dec 2019 11:12:18 -0800 (PST) X-Google-Smtp-Source: APXvYqxFVNfCC7AuGM4gIhlZVMDbwaPs1ye5tOE+9XzUYNkooSqbqRgbwSAV39L9eubF6ZDXCL2w X-Received: by 2002:a54:4e8d:: with SMTP id c13mr2893991oiy.27.1576782738620; Thu, 19 Dec 2019 11:12:18 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1576782738; cv=none; d=google.com; s=arc-20160816; b=PsjjmgAPP/Iv8JOUwaMIweRn4h+aACIXGS+dloMIr+U8drquI1L/Fz0bL2nivxsFBH wF+4toGOb0PLO1xtAkaIR5KMyFJKD+kh63BwmjUAtjGMtjS2Sb8ggpOF1aFF1m3bORAC 47BGaADdA6l+1ELWb8u9czXgLgt9o3cvhdWjtmWKvEYV19H9nsuhz+shAeWNaPW+qWFA CaLze6Yw8aSE5nh9qCpWotTLqPPuWKkmkuduwCZogXpu0SiVbPipI5fyKzfWjNX1UcX6 2QQcNr6w7XcjLTm3pko9fi8znxwPPAA040FbI4jH9s7cAafks8KSwlBOcl/dlUmJDhgo gJ7w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=r0qnWjvrYymIaFYyGBqC5vVo4VQlE+Y3iMdQNbLMwMo=; b=wM+SuwHjUi9kK+Bf2/YhJJ6lZZNnxS2xIcLgO7QbIegkcy+NCWAfN9FYnN2Ji18EAd yrjwJN3aS07XuzCvt8wEMjLiYi/TxrZO3z7dQW45zUmXqw51GzQu1Z5iasv69Yy0yEFM ulmB6E6n57/MWd1a7RYoRgoftVRTF+wgZd3WlqG1o0SwdeAaLRNza+inPQxWFGMpls1R 6w2PbvHmNqX8/jBzLyjCOybO70qnwPEhpy9orsc/gq07RFNDXRQKqX3jc1lZHes3Uc8H gaU4EgdNGF1JYWYOb2dyYjHZ9YPuV0M5Te4HnU8DVczlAvm8L7MsOnwMRrH5g8F9AyrQ SfEw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=SABjeKc6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m15si3034889otq.273.2019.12.19.11.12.05; Thu, 19 Dec 2019 11:12:18 -0800 (PST) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=SABjeKc6; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727905AbfLSTL0 (ORCPT + 99 others); Thu, 19 Dec 2019 14:11:26 -0500 Received: from mail.kernel.org ([198.145.29.99]:56192 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727280AbfLSSiQ (ORCPT ); Thu, 19 Dec 2019 13:38:16 -0500 Received: from localhost (83-86-89-107.cable.dynamic.v4.ziggo.nl [83.86.89.107]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 0159B2467B; Thu, 19 Dec 2019 18:38:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1576780695; bh=+h9+Fs19oWm1bKe6eOPAtfg9Z/lsZkSSW92OUu3lXdk=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=SABjeKc6+KbVBUmNvuYc/opu0zOGK23xPoFpI0aWTmh4qdKP9LN+4h8oiQ+t2iqVs 1zRWxEeYCB0lrPIqVtaR0/UciGVsbsZ3MERUHu1VG+roa4XXsqbcVSEI4e6hebr46q GfHP8CHMCPuguIMk7OTemL1hHoyShbl+UHawmqG8= 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 , "zhangyi (F)" Subject: [PATCH 4.4 080/162] proc: fix coredump vs read /proc/*/stat race Date: Thu, 19 Dec 2019 19:33:08 +0100 Message-Id: <20191219183212.664539137@linuxfoundation.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20191219183150.477687052@linuxfoundation.org> References: <20191219183150.477687052@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 Cc: Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: zhangyi (F) 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 @@ -435,8 +435,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); + } } }