Received: by 2002:ad5:474a:0:0:0:0:0 with SMTP id i10csp4447874imu; Mon, 7 Jan 2019 23:52:35 -0800 (PST) X-Google-Smtp-Source: ALg8bN6SWqLQ2/lv3vHaXDlYkaTGcTxThKy3/zAWMVrOsisp6l0eROxfnsdIGlBONi8hwbWhRoCC X-Received: by 2002:a63:ee4c:: with SMTP id n12mr604010pgk.21.1546933955216; Mon, 07 Jan 2019 23:52:35 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1546933955; cv=none; d=google.com; s=arc-20160816; b=MK87NQZtEhYDPJxlNEbJrUCmzcdgPeOuCYdulj4KZcBxUsUYtEdNcJYUzjwSUGVE6U f5DvQGIEc3nrNO9VZEFrWgJaEOiKMGBIOiW49eij8jpYceAINaBWqHc9KP3KIgtDo1K9 CrWlPUe0//AYIIVXP7rKFfq0e01+R8css5ZBXdbV4zc2SbJG3i9BE3lLkQcp9mrPqQkh WnzEqerT20oFkF4bMtFhQ+f/oPqVEmKvK3RH6YjI/WyfzjInIdXMeMPkSzceW6bXN5Vx INcK9MfC9Y5K/sAWwOivIJElREyaDVZQBn6/jUaLDpQYfa0u+HwKXcAlKFzVA+L9cQRW FAOQ== 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=59ORtEOclccJkolhz/GI6ktm32n9H3Yn7+JkzCgXg04=; b=hJaAGGAV2iMp2Q9V2sWRzGCRsQuW5xlRLC+fIHzDPUkHpeh7ocMWAp+dAmDb+Vfud/ HZXp+ctYzW6JgZmpYkVT5Cfs2IcquxJkXyshy7NSxqfKyY5XyqNK0kynm8dt56v6CUgV vg64ouF1okGPLFbKyceQ7RB0Y3/eJd5ACstooosMvbSQjENfg+3qxzLVTYkw4ZMPfvb7 sxNDwuKyUwQrV7jCzIIHNJRW1W9jrqEmWw8UXVX5kWVcRoLosBR5kIlrTenU52hji0Wx K8DQ5FjMT09ool9J26prY+hCVWnNI+CMOiherpvsJXFwK8z7wTbJ+8JmYExdoXCRZ+Md UXAA== 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 v13si18693640pgn.355.2019.01.07.23.52.19; Mon, 07 Jan 2019 23:52:35 -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 S1727820AbfAHHvO (ORCPT + 99 others); Tue, 8 Jan 2019 02:51:14 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:59608 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1727473AbfAHHvO (ORCPT ); Tue, 8 Jan 2019 02:51:14 -0500 Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id x087nJTC001298 for ; Tue, 8 Jan 2019 02:51:13 -0500 Received: from e06smtp01.uk.ibm.com (e06smtp01.uk.ibm.com [195.75.94.97]) by mx0a-001b2d01.pphosted.com with ESMTP id 2pvpham1fc-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 08 Jan 2019 02:51:13 -0500 Received: from localhost by e06smtp01.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 8 Jan 2019 07:51:10 -0000 Received: from b06cxnps4076.portsmouth.uk.ibm.com (9.149.109.198) 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) Tue, 8 Jan 2019 07:51:08 -0000 Received: from d06av21.portsmouth.uk.ibm.com (d06av21.portsmouth.uk.ibm.com [9.149.105.232]) by b06cxnps4076.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x087p77S7405828 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 8 Jan 2019 07:51:07 GMT Received: from d06av21.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 323015204F; Tue, 8 Jan 2019 07:51:07 +0000 (GMT) Received: from rapoport-lnx (unknown [9.148.8.241]) by d06av21.portsmouth.uk.ibm.com (Postfix) with ESMTPS id 755895204E; Tue, 8 Jan 2019 07:51:06 +0000 (GMT) Date: Tue, 8 Jan 2019 09:51:04 +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 v2 1/2] mm: add probe_user_read() References: <0b0db24e18063076e9d9f4e376994af83da05456.1546932949.git.christophe.leroy@c-s.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <0b0db24e18063076e9d9f4e376994af83da05456.1546932949.git.christophe.leroy@c-s.fr> User-Agent: Mutt/1.5.24 (2015-08-30) X-TM-AS-GCONF: 00 x-cbid: 19010807-4275-0000-0000-000002FBC587 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19010807-4276-0000-0000-00003809CF5E Message-Id: <20190108075104.GA4396@rapoport-lnx> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-01-08_05:,, 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-1901080064 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Tue, Jan 08, 2019 at 07:37:44AM +0000, Christophe Leroy wrote: > In powerpc code, 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(), this patch adds > probe_user_read(). > > probe_user_read() does the same as probe_kernel_read() but > first checks that it is really a user address. > > Signed-off-by: Christophe Leroy > --- > v2: Added "Returns:" comment and removed probe_user_address() > > Changes since RFC: Made a static inline function instead of weak function as recommended by Kees. > > include/linux/uaccess.h | 34 ++++++++++++++++++++++++++++++++++ > 1 file changed, 34 insertions(+) > > diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h > index 37b226e8df13..07f4f0ed69bc 100644 > --- a/include/linux/uaccess.h > +++ b/include/linux/uaccess.h > @@ -263,6 +263,40 @@ 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 > + * > + * Returns: 0 on success, -EFAULT on error. Nit: please put the "Returns:" comment after the description, otherwise kernel-doc considers it a part of the elaborate description. > + * > + * 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. > + */ > + > +#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(src, size)) > + return -EFAULT; > + > + pagefault_disable(); > + ret = __copy_from_user_inatomic(dst, src, size); > + pagefault_enable(); > + > + return ret ? -EFAULT : 0; > +} > +#endif > + > #ifndef user_access_begin > #define user_access_begin(ptr,len) access_ok(ptr, len) > #define user_access_end() do { } while (0) > -- > 2.13.3 > -- Sincerely yours, Mike.