Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp7252729imu; Mon, 3 Dec 2018 09:56:32 -0800 (PST) X-Google-Smtp-Source: AFSGD/Xz2UhyJR64jnKDWBQoHuTEBAs2EHL9DuEEd0Z5hLnHYOFu2haSsV/37KMcZH2I6bKvcVL7 X-Received: by 2002:a63:507:: with SMTP id 7mr12611077pgf.411.1543859792173; Mon, 03 Dec 2018 09:56:32 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1543859792; cv=none; d=google.com; s=arc-20160816; b=PNmMS++HwGdtdTf5QuCZc5ovDiqJShxxZaj5uPdnIzm9ndyucfjiZZAKG6y85G/Vgc sfa5jBsjef40rNSQIrZboGY/tNuQYNgrgJB9gi7NHanv6wTi8pidpkdhY2bpvnRSqua4 OcTMhdESmiV6qF2o1HifGjYOSQpPBtecClaWN6MLUzEGRN9dPOiSSYIGiv2TPdryRU2G DmgVK6wupSsjAuTjzEGg0u2orkqweTla5ojEUNb6iBOPjhYp+LpJplpe66keA24frSWu u/l+QxwCcSw+T/xOtLGd6gCjdMJTi0K1fCyv9HwKNzP8ARplWJY0Jk7obcOSBE3v0qNX xR2g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:message-id:user-agent:in-reply-to :content-disposition:mime-version:references:subject:cc:to:from:date; bh=VPMEnfcJIrWcts/1dVLHYnrbTf3EyBxlvzl1aGuAhZg=; b=BXo4uFc2g8wHGXToGE+wJJ0FNbZcQ6KpVrFhON1CG8ecwqBKvE0L57fX6ffQCgSl8w 96zf8RBOee+M8F38IjJEtdfwSzAD9AJLydM/YlDa37RDXFaoO5rkLvNBY7/dNOBg7F9F 3U2bYZAe5SL/ST/v9JmpfsjEBSEHKscKvzUQih5K877ti182woR2aIYRMa4mU0IpnbTh b/kwQ1/DNzlt9NmGujTpkTawV3Qdz1UauRDpLeuE8OBtKuXMutvHgt0hlFRGnwxfeEmS KSCOGOvhZhKb9ZGbPzN3HjY/0xUiEj0RZaQWtTv26BUyDbnTW9wHnagqn+Qi6wzoPGM7 MkUg== 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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id e136si16255542pfh.17.2018.12.03.09.56.13; Mon, 03 Dec 2018 09:56:32 -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; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=ibm.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726896AbeLCRyL (ORCPT + 99 others); Mon, 3 Dec 2018 12:54:11 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:46525 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726014AbeLCRyL (ORCPT ); Mon, 3 Dec 2018 12:54:11 -0500 Received: from pps.filterd (m0098420.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id wB3HnblE110858 for ; Mon, 3 Dec 2018 12:54:06 -0500 Received: from e06smtp01.uk.ibm.com (e06smtp01.uk.ibm.com [195.75.94.97]) by mx0b-001b2d01.pphosted.com with ESMTP id 2p58dyjq4t-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Mon, 03 Dec 2018 12:54:05 -0500 Received: from localhost by e06smtp01.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Mon, 3 Dec 2018 17:54:04 -0000 Received: from b06cxnps3074.portsmouth.uk.ibm.com (9.149.109.194) by e06smtp01.uk.ibm.com (192.168.101.131) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Mon, 3 Dec 2018 17:53:59 -0000 Received: from d06av23.portsmouth.uk.ibm.com (d06av23.portsmouth.uk.ibm.com [9.149.105.59]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id wB3Hrwto49545460 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Mon, 3 Dec 2018 17:53:58 GMT Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 4BF5DA4051; Mon, 3 Dec 2018 17:53:58 +0000 (GMT) Received: from d06av23.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 5E0E2A4040; Mon, 3 Dec 2018 17:53:57 +0000 (GMT) Received: from rapoport-lnx (unknown [9.148.206.196]) by d06av23.portsmouth.uk.ibm.com (Postfix) with ESMTPS; Mon, 3 Dec 2018 17:53:57 +0000 (GMT) Date: Mon, 3 Dec 2018 19:53:55 +0200 From: Mike Rapoport To: Christophe Leroy Cc: Kees Cook , Andrew Morton , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , linux-kernel@vger.kernel.org, linuxppc-dev@lists.ozlabs.org, linux-mm@kvack.org Subject: Re: [PATCH 1/2] mm: add probe_user_read() and probe_user_address() References: MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/1.5.24 (2015-08-30) X-TM-AS-GCONF: 00 x-cbid: 18120317-4275-0000-0000-000002EB6E06 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18120317-4276-0000-0000-000037F8729D Message-Id: <20181203175354.GE26700@rapoport-lnx> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2018-12-03_09:,, signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=0 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=999 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1810050000 definitions=main-1812030164 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Dec 03, 2018 at 05:06:42PM +0000, Christophe Leroy wrote: > In the powerpc, there are several places implementing safe > access to user data. This is sometimes implemented using > probe_kernel_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 > --- > Changes since RFC: Made a static inline function instead of weak function as recommended by Kees. > > include/linux/uaccess.h | 42 ++++++++++++++++++++++++++++++++++++++++++ > 1 file changed, 42 insertions(+) > > diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h > index efe79c1cdd47..83ea8aefca75 100644 > --- a/include/linux/uaccess.h > +++ b/include/linux/uaccess.h > @@ -266,6 +266,48 @@ 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_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. Please add 'Returns:' description. > + */ > + > +#ifndef probe_user_read > +static __always_inline 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; > +} > +#endif > + > +/** > + * 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. This should be 'Return:' for kernel-doc to recognise it as return value description. > + */ > +#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) > -- > 2.13.3 > -- Sincerely yours, Mike.