Received: by 10.223.185.116 with SMTP id b49csp511679wrg; Fri, 16 Feb 2018 02:45:02 -0800 (PST) X-Google-Smtp-Source: AH8x225Yijaf1z+CV02JrOLyv9jWFifvzws/GwyN+J8WdW09jc70h3DbuJKzRL4rHOx/uCQ1nil2 X-Received: by 2002:a17:902:c81:: with SMTP id 1-v6mr5500365plt.281.1518777902737; Fri, 16 Feb 2018 02:45:02 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518777902; cv=none; d=google.com; s=arc-20160816; b=LC720qPBEetCpI8k9A2KusHhUj4zNBjCrEyG1pFQm5JaA56yLiDTTq9VXTOzBQVyNn dkcY3vv7KanxGNDQtoZ1ioDTPkNiw+D9g9aRoUjBenyYX0yLZ8+zoaqd77OFC4dsMowV WTtKyhxlHwdKE2n8zl5oZfQHlSJceIxknTPEKvdtJ+S7xaYl6zRHSkX5dVF/6N5Q5mFd 7X4is4s3byYkcpqJegFho1Cf40F53s5GmlLmUweZNRyohDDcQd9aGMB6l3AxjznqfI1T DgEh2GBhDFoWHIgEKeFeeezBd2Zk5KTjcCo9xTX33ZevWR1/ej7gcaKaqD0hxpaTyMG2 fQ0w== 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=49U26Y0H5A3Aos0hHMMf1H1ml7B7Ukusu5Tf4HHDGVo=; b=N1JivF6XCoTZtm15bbUoKi8nWFteE4Pb5btWNQNvZC7sSncyc9wsm7GNpfwa1Whx9H tbNhXH6QMDbgxJ4pM7czNPcflWZpmvNSgR4+KjeIMQW6j77E9YgPCA75nbQpmndSx2gp 8sDXN2DDXlpVWiobt+ABJR/8kD9mMCbdS0cxYK/3rOvd6ByZpkWTgNAUTtBBdpZpDICP 9cRzIcitaCnf0NmEvnt84bMt75bq3zqqkfgehL7xxaboURe0xYNwfMtwLbq/K+F7oUq4 GTDOM1f6UKqGO8TdlIAYK1dwviQJoFmo4DsRU9ZyxpSIZ8JYW9v4nStCQI5qAguiuwPC SmPw== 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 m7si4163942pgp.542.2018.02.16.02.44.48; Fri, 16 Feb 2018 02:45:02 -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 S1427146AbeBORQm (ORCPT + 99 others); Thu, 15 Feb 2018 12:16:42 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:58440 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1422912AbeBOPgi (ORCPT ); Thu, 15 Feb 2018 10:36:38 -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 73E59CEE; Thu, 15 Feb 2018 15:36:31 +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.14 152/195] fs/proc/kcore.c: use probe_kernel_read() instead of memcpy() Date: Thu, 15 Feb 2018 16:17:23 +0100 Message-Id: <20180215151713.480540866@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180215151705.738773577@linuxfoundation.org> References: <20180215151705.738773577@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.14-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 @@ -512,23 +512,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 {