Received: by 10.223.185.116 with SMTP id b49csp618522wrg; Fri, 16 Feb 2018 04:35:42 -0800 (PST) X-Google-Smtp-Source: AH8x225RVhG4hcMgj8Qfh0oEpQQI4W9bFrLcIhbH+g2HW+TQYawYRDXaT13h3GKiqIoj+Eif2ccJ X-Received: by 10.101.93.142 with SMTP id f14mr4915031pgt.82.1518784542025; Fri, 16 Feb 2018 04:35:42 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518784541; cv=none; d=google.com; s=arc-20160816; b=0gE8bnirMlN6qF7adTXeGBwRWZy2qVvlKv+TghnPuOUnNPejCUgAAUZBIm7bFHsBuA WzsdmAhKw1+AXVcBeRggeEMQFHwYXxnD0+5D6vH6V3r5S2YDO/LFBD+/6oUBBjhLoIet w2IvOLPvWbZzxK0U6PwtgJBUd9xxjctDWNNkTBOqx1fb2IqDZYa9fC3oN8sGa8rXBnz9 KaXKlny3N1UHVaYeXacFOybGDlbOtA/An0b6Sx2cKbXwyoXEDMLI3o0rgRat6/H9dw6B IJwnPZvCrryfUfQaTbT1ux9tx4EVoBDPUZDHUWoeR+D3zWhFYZUnxuJ4KltHPU25ffaY vRNQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:mime-version:user-agent:references :in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=1g088M/YuyaKb3LiEL0dvgbg0f8gCA23Z9mF8CEZCGw=; b=JqMPxyDHwKQV4dVhqY/ct7358i/OH8aC/FlcuXtxzgb+sX4comq9Hd8hsjiASa1sGK QTMb1jYUGQymHHMaZ8whWURmXtm1FsYfe6MDgukz8x+viWTZ/m0caSmt8MG/s0aPAcMl 8E7+npL/pHZggrNPpMa+/tFU9QaHNq9Z1ljtUZyE/gN+qVgAVnXQ7SvlTkP6NWM9F1oq 8TGGzdR7ff7OB2QxAXkvnafITuY1X+H0x1se19Z84dpJ94ZL9PgfnbyAe6Iq328RL79W AWoImukE27USOrI8SeL7JaVu0XiSVC3Cs5kKztPUQ2VMP0YDoHNyV8DDoek17fYrfSUN pGYQ== ARC-Authentication-Results: i=1; mx.google.com; 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 m11-v6si1393979pla.639.2018.02.16.04.35.27; Fri, 16 Feb 2018 04:35:41 -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; 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 S1946285AbeBOSE5 (ORCPT + 99 others); Thu, 15 Feb 2018 13:04:57 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:54436 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1164046AbeBOP3b (ORCPT ); Thu, 15 Feb 2018 10:29:31 -0500 Received: from localhost (LFbn-1-12258-90.w90-92.abo.wanadoo.fr [90.92.71.90]) by mail.linuxfoundation.org (Postfix) with ESMTPSA id 643FC10EC; Thu, 15 Feb 2018 15:29:30 +0000 (UTC) From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Heiko Carstens , Kees Cook , Jiri Olsa , Al Viro , Andrew Morton , Linus Torvalds Subject: [PATCH 4.9 61/88] fs/proc/kcore.c: use probe_kernel_read() instead of memcpy() Date: Thu, 15 Feb 2018 16:17:28 +0100 Message-Id: <20180215151231.108665950@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180215151222.437136975@linuxfoundation.org> References: <20180215151222.437136975@linuxfoundation.org> User-Agent: quilt/0.65 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 4.9-stable review patch. If anyone has any objections, please let me know. ------------------ From: Heiko Carstens commit d0290bc20d4739b7a900ae37eb5d4cc3be2b393f upstream. Commit df04abfd181a ("fs/proc/kcore.c: Add bounce buffer for ktext data") added a bounce buffer to avoid hardened usercopy checks. Copying to the bounce buffer was implemented with a simple memcpy() assuming that it is always valid to read from kernel memory iff the kern_addr_valid() check passed. A simple, but pointless, test case like "dd if=/proc/kcore of=/dev/null" now can easily crash the kernel, since the former execption handling on invalid kernel addresses now doesn't work anymore. Also adding a kern_addr_valid() implementation wouldn't help here. Most architectures simply return 1 here, while a couple implemented a page table walk to figure out if something is mapped at the address in question. With DEBUG_PAGEALLOC active mappings are established and removed all the time, so that relying on the result of kern_addr_valid() before executing the memcpy() also doesn't work. Therefore simply use probe_kernel_read() to copy to the bounce buffer. This also allows to simplify read_kcore(). At least on s390 this fixes the observed crashes and doesn't introduce warnings that were removed with df04abfd181a ("fs/proc/kcore.c: Add bounce buffer for ktext data"), even though the generic probe_kernel_read() implementation uses uaccess functions. While looking into this I'm also wondering if kern_addr_valid() could be completely removed...(?) Link: http://lkml.kernel.org/r/20171202132739.99971-1-heiko.carstens@de.ibm.com Fixes: df04abfd181a ("fs/proc/kcore.c: Add bounce buffer for ktext data") Fixes: f5509cc18daa ("mm: Hardened usercopy") Signed-off-by: Heiko Carstens Acked-by: Kees Cook Cc: Jiri Olsa Cc: Al Viro Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Greg Kroah-Hartman --- fs/proc/kcore.c | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) --- a/fs/proc/kcore.c +++ b/fs/proc/kcore.c @@ -507,23 +507,15 @@ read_kcore(struct file *file, char __use return -EFAULT; } else { if (kern_addr_valid(start)) { - unsigned long n; - /* * Using bounce buffer to bypass the * hardened user copy kernel text checks. */ - memcpy(buf, (char *) start, tsz); - n = copy_to_user(buffer, buf, tsz); - /* - * We cannot distinguish between fault on source - * and fault on destination. When this happens - * we clear too and hope it will trigger the - * EFAULT again. - */ - if (n) { - if (clear_user(buffer + tsz - n, - n)) + if (probe_kernel_read(buf, (void *) start, tsz)) { + if (clear_user(buffer, tsz)) + return -EFAULT; + } else { + if (copy_to_user(buffer, buf, tsz)) return -EFAULT; } } else {