From patchwork Fri Oct 19 15:14:54 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Christophe Leroy X-Patchwork-Id: 10649625 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 4F7E513A4 for ; Fri, 19 Oct 2018 15:15:01 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 3ACFD24603 for ; Fri, 19 Oct 2018 15:15:01 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 2A44F26246; Fri, 19 Oct 2018 15:15:01 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-2.9 required=2.0 tests=BAYES_00,MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=ham version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 1AE7A24603 for ; Fri, 19 Oct 2018 15:14:59 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 07C666B0007; Fri, 19 Oct 2018 11:14:58 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id 02B4A6B0008; Fri, 19 Oct 2018 11:14:57 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id E36AC6B000A; Fri, 19 Oct 2018 11:14:57 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by kanga.kvack.org (Postfix) with ESMTP id 8F17A6B0007 for ; Fri, 19 Oct 2018 11:14:57 -0400 (EDT) Received: by mail-wr1-f72.google.com with SMTP id 88-v6so26999309wrp.21 for ; Fri, 19 Oct 2018 08:14:57 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:message-id :from:subject:to:cc:date; bh=dM0+a0zwvlgqUmeoZcB6HrfmOkZLHS742m3F9Ark+go=; b=QRnFeGg1mUhltbBNVAKRWKdWNvr2vaPnRZW2bR0+sKX833ND5uq1Wk5uIrVelPLQJp rQo4EunmfyoZJ3KosjBQGnOrx6ixKy+McHyrlhYyq6xJBo5P3lPiiBRUfgB0wa1tDrRD uVHsi0a+ZeOM86weLiV/rftgo8xKDV2WSNJ+LZk6HR++qkCo6I0D+WTb1ubaZ+Zivkja Wg7F5VzsIbswT530qMhLwuzFy2NECmJa6b6eKrYuhhvtW9SmgckMdvL9z3i+7iJCqZU6 S17/LrZ3umboKKBJDta2JgZNnFD+RLim6KAgUOay6H8+SQ9B0pSUyFC2Ts8OMeK+XhGz 2M3w== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of christophe.leroy@c-s.fr designates 93.17.236.30 as permitted sender) smtp.mailfrom=christophe.leroy@c-s.fr X-Gm-Message-State: ABuFfoippBokf4/STWDrpuNJEAUt9Htofbatvz1XvPcrTe/4LOJl+ndZ j+pRyqPJix1/uEeYFxQQ0zBiZEvExFgT1+Yy7CtHaXso4DkQLz4PrnhAXMAXz5BXcG57R3GcKgd 92Ph0AU2IzuzgKbErvewubCx3Iszrcn4M+DGd087VYe0bfjkpOjXn2aC1oroGmJyXGg== X-Received: by 2002:adf:916a:: with SMTP id j97-v6mr35714068wrj.179.1539962096980; Fri, 19 Oct 2018 08:14:56 -0700 (PDT) X-Google-Smtp-Source: ACcGV61sftv1lKmGG+NwLFY90fkv4frb82aYzZheTSqiSDzVIDGGh3fnj2ZazFwluuJa34Av16um X-Received: by 2002:adf:916a:: with SMTP id j97-v6mr35713980wrj.179.1539962095729; Fri, 19 Oct 2018 08:14:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539962095; cv=none; d=google.com; s=arc-20160816; b=GxbMUxSayKXnmREDHleR9/OLUMD+6JbxDJxEibCfwWXtyz0App98zgrf0IlNDeBR/Z aJ6gQRxcoEJOJi3FSaJBUTiKgH0MJvM38t953/B+TtgMKxbfVrigIAO4JhzscU2X+cAk YL9Ypcw0DgSLD09uds/rQcItcVTg24iAIJaMuYUT+naTmFO5GzShBQTNaHvWdfdqEFbY gJSltDWme1i+X3EK/cHnWnevOUnNX35XhMYGi/UrDQR1opagW5HNNpiq8UJVQ/vfy1ob tp6Rl4vMsFz9O6bjyXx/p6Jfv82mjzUXKUmtmEQg3aq2yp+ZUBUgRmwrLsAjorRFBRod EpOA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=date:cc:to:subject:from:message-id; bh=dM0+a0zwvlgqUmeoZcB6HrfmOkZLHS742m3F9Ark+go=; b=xpMM1O6Uhs+kzUCKZgwR5lnRS963o82JyTJ8BbJPJJFU9oo6baDhSsgUtKdxsfWkzF Y+YkVdFw44sAN2E+HQvBN3Jnxtm4vbfDbQOpDAhdqV2yRdRWc/XpKmOA1BO4DAra7Non cYW4IGPCV0RbBa6Cfh6qT/D9bzeufkN0tZMuQgaNw529XnseZtUjVXzI/9kouS2nc15q BlUGTGIGeQXXQJDHwGBzowMQJUaE1nl9ydXvbC5SL8V+6seDYemCy5ZSoa08SQ/bkVhT wUFNuh0ejwaYRKZHvbt0EsK3BKs7TTUU7O2oI+Q9dSbF6DpZeX9Tr9eIx0ts9285WTHb dBrw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of christophe.leroy@c-s.fr designates 93.17.236.30 as permitted sender) smtp.mailfrom=christophe.leroy@c-s.fr Received: from pegase1.c-s.fr (pegase1.c-s.fr. [93.17.236.30]) by mx.google.com with ESMTPS id p5si4242058wrx.116.2018.10.19.08.14.55 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Oct 2018 08:14:55 -0700 (PDT) Received-SPF: pass (google.com: domain of christophe.leroy@c-s.fr designates 93.17.236.30 as permitted sender) client-ip=93.17.236.30; Authentication-Results: mx.google.com; spf=pass (google.com: domain of christophe.leroy@c-s.fr designates 93.17.236.30 as permitted sender) smtp.mailfrom=christophe.leroy@c-s.fr Received: from localhost (mailhub1-int [192.168.12.234]) by localhost (Postfix) with ESMTP id 42c8bg0gMsz9ttS0; Fri, 19 Oct 2018 17:14:55 +0200 (CEST) X-Virus-Scanned: Debian amavisd-new at c-s.fr Received: from pegase1.c-s.fr ([192.168.12.234]) by localhost (pegase1.c-s.fr [192.168.12.234]) (amavisd-new, port 10024) with ESMTP id 5yksk3zlssSd; Fri, 19 Oct 2018 17:14:55 +0200 (CEST) Received: from messagerie.si.c-s.fr (messagerie.si.c-s.fr [192.168.25.192]) by pegase1.c-s.fr (Postfix) with ESMTP id 42c8bf6rrWz9ttRV; Fri, 19 Oct 2018 17:14:54 +0200 (CEST) Received: from localhost (localhost [127.0.0.1]) by messagerie.si.c-s.fr (Postfix) with ESMTP id D382B8B93C; Fri, 19 Oct 2018 17:14:54 +0200 (CEST) X-Virus-Scanned: amavisd-new at c-s.fr Received: from messagerie.si.c-s.fr ([127.0.0.1]) by localhost (messagerie.si.c-s.fr [127.0.0.1]) (amavisd-new, port 10023) with ESMTP id PDDxes12-UzN; Fri, 19 Oct 2018 17:14:54 +0200 (CEST) Received: from pc13168vm.idsi0.si.c-s.fr (po15451.idsi0.si.c-s.fr [172.25.231.2]) by messagerie.si.c-s.fr (Postfix) with ESMTP id 9A7118B935; Fri, 19 Oct 2018 17:14:54 +0200 (CEST) Received: by pc13168vm.idsi0.si.c-s.fr (Postfix, from userid 0) id 5FF53674CC; Fri, 19 Oct 2018 15:14:54 +0000 (UTC) Message-Id: <336eb81e62d6c683a69d312f533899dcb6bcf770.1539959864.git.christophe.leroy@c-s.fr> From: Christophe Leroy Subject: [RFC PATCH] mm: add probe_user_read() and probe_user_address() To: Kees Cook , Andrew Morton , Michael Ellerman Cc: linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org Date: Fri, 19 Oct 2018 15:14:54 +0000 (UTC) X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP In the powerpc, there are several places implementing safe access to user data. This is sometimes implemented using probe_kerne_address() with additional access_ok() verification, sometimes with get_user() enclosed in a pagefault_disable()/enable() pair, etc... : show_user_instructions() bad_stack_expansion() p9_hmi_special_emu() fsl_pci_mcheck_exception() read_user_stack_64() read_user_stack_32() on PPC64 read_user_stack_32() on PPC32 power_pmu_bhrb_to() In the same spirit as probe_kernel_read() and probe_kernel_address(), this patch adds probe_user_read() and probe_user_address(). probe_user_read() does the same as probe_kernel_read() but first checks that it is really a user address. probe_user_address() is a shortcut to probe_user_read() Signed-off-by: Christophe Leroy --- include/linux/uaccess.h | 10 ++++++++++ mm/maccess.c | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h index efe79c1cdd47..fb00e3f847d7 100644 --- a/include/linux/uaccess.h +++ b/include/linux/uaccess.h @@ -266,6 +266,16 @@ extern long strncpy_from_unsafe(char *dst, const void *unsafe_addr, long count); #define probe_kernel_address(addr, retval) \ probe_kernel_read(&retval, addr, sizeof(retval)) +/** + * probe_user_address(): safely attempt to read from a user location + * @addr: address to read from + * @retval: read into this variable + * + * Returns 0 on success, or -EFAULT. + */ +#define probe_user_address(addr, retval) \ + probe_user_read(&(retval), addr, sizeof(retval)) + #ifndef user_access_begin #define user_access_begin() do { } while (0) #define user_access_end() do { } while (0) diff --git a/mm/maccess.c b/mm/maccess.c index ec00be51a24f..85d4a88a6917 100644 --- a/mm/maccess.c +++ b/mm/maccess.c @@ -67,6 +67,39 @@ long __probe_kernel_write(void *dst, const void *src, size_t size) EXPORT_SYMBOL_GPL(probe_kernel_write); /** + * probe_user_read(): safely attempt to read from a user location + * @dst: pointer to the buffer that shall take the data + * @src: address to read from + * @size: size of the data chunk + * + * Safely read from address @src to the buffer at @dst. If a kernel fault + * happens, handle that and return -EFAULT. + * + * We ensure that the copy_from_user is executed in atomic context so that + * do_page_fault() doesn't attempt to take mmap_sem. This makes + * probe_user_read() suitable for use within regions where the caller + * already holds mmap_sem, or other locks which nest inside mmap_sem. + */ + +long __weak probe_user_read(void *dst, const void *src, size_t size) + __attribute__((alias("__probe_user_read"))); + +long __probe_user_read(void *dst, const void __user *src, size_t size) +{ + long ret; + + if (!access_ok(VERIFY_READ, src, size)) + return -EFAULT; + + pagefault_disable(); + ret = __copy_from_user_inatomic(dst, src, size); + pagefault_enable(); + + return ret ? -EFAULT : 0; +} +EXPORT_SYMBOL_GPL(probe_user_read); + +/** * strncpy_from_unsafe: - Copy a NUL terminated string from unsafe address. * @dst: Destination address, in kernel space. This buffer must be at * least @count bytes long.