Received: by 10.223.185.116 with SMTP id b49csp185745wrg; Thu, 15 Feb 2018 19:31:51 -0800 (PST) X-Google-Smtp-Source: AH8x226tMtOSTEpLhz1hMq6X1IQZ4zLSv5Ax9uPd2L3QPBsCwRNd4i5w1HXqpkVnQSqoWrR0Gek3 X-Received: by 10.101.78.12 with SMTP id r12mr3972366pgt.33.1518751910970; Thu, 15 Feb 2018 19:31:50 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1518751910; cv=none; d=google.com; s=arc-20160816; b=qoZ33xqMZfEgDHFFDoKcR71ck59KMfuq145nhvrTC2AKTZWMzxNI/5DICKpnAzTaoP wQGawzLQ6GobNSJ3I82ltRSpOGj6naNL7kRN7BJIG40NhkUl1+7G6GU5thIJDbzTp/bP v5mIb/9reoY0gQ/r2PR0uUcSpYcZXuQ7zP8grC5ZrHNbjjcXC4K041Dr8tSMbE37GaOT 68PhIwLtXJ7Wro71zFI1W9vbQ/LVFmhgEnu6O0z0UjHUqXHw6NOzT6Ifv2UP+x3YaPEX ihO/h2kknBP5DeJGT37jUrvlkrI+LaRhmak5HW7VXifFiPC5sUr2cfkEIKVk8Y7zXfyq Dh8w== 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=cAeWIass84dZ3GNW30bKJYwnIUnth1dWk3r2+XIDjyw=; b=zyaM0Olhuurig1ejXSjrYHW5lf2YoZO6PqOPsGisNvJfH7OnLd0eAqg3kOYv4+J4NZ Pu7K9xVcrHS36mpxw37Ofqen+FyOTkRDwkksizybvSQyCtRNb2kmk1eve7miediuJscM Puy2hziyzOoz1cwQKtjzv/zOot3oBT7nJZX5LKasIWmaTUCY6IhQ0Ln7q9r+rhreyIdi 3ICpwNdERPGBBlo23P72B5PmyWRkQoedh9p1yVwWwcIOHtshRhq5SOX3G2BhQ2mRDNU8 W4fy+i2S4BW9Trj8fgBPzM/tneF+Mom9WjGMIVM4H0oYBvX64QJpGGXkYwnDahFDrzv2 FoIQ== 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 k64si540455pfb.100.2018.02.15.19.31.36; Thu, 15 Feb 2018 19:31:50 -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 S1424549AbeBOPpv (ORCPT + 99 others); Thu, 15 Feb 2018 10:45:51 -0500 Received: from mail.linuxfoundation.org ([140.211.169.12]:35100 "EHLO mail.linuxfoundation.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1424535AbeBOPps (ORCPT ); Thu, 15 Feb 2018 10:45:48 -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 1272211CE; Thu, 15 Feb 2018 15:45:47 +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.15 156/202] fs/proc/kcore.c: use probe_kernel_read() instead of memcpy() Date: Thu, 15 Feb 2018 16:17:36 +0100 Message-Id: <20180215151721.082195457@linuxfoundation.org> X-Mailer: git-send-email 2.16.1 In-Reply-To: <20180215151712.768794354@linuxfoundation.org> References: <20180215151712.768794354@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.15-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 {