Received: by 2002:ac0:8c9a:0:0:0:0:0 with SMTP id r26csp5331579ima; Tue, 5 Feb 2019 09:58:14 -0800 (PST) X-Google-Smtp-Source: AHgI3IYtMmIXnoj1VZkWyl7kU3njKqrkL9aXWY3G4Re2n3e34UB3L3lsUyuNcH6of8DqJtwYP35k X-Received: by 2002:a17:902:8607:: with SMTP id f7mr6165293plo.123.1549389494191; Tue, 05 Feb 2019 09:58:14 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1549389494; cv=none; d=google.com; s=arc-20160816; b=IJjzAOap2qPYFSHwktFH062ToAqh8eUfeBvBPmRFAr/pDZTypFA/suOsRlCb1LQTi5 QNdVZcRx5hEdFiGOn4n3wt/CazXR6JC0nIr9DaOHn6vz5huoFjG3f4rAlfOBYDiSObag ABibXi5qFHkHf4ec829S3PSIDcWI8s+UY0b6orwwxWwv8P5uMtN/NNd4ckS8kQD1OiIA htzrjLFVd/ucR1gISnxsa+C3l5PjqYhCKLRbZcM153pj2cGL3t078M7dOYKLIykdOFUP Bc5eez4Ij95TrmBjh8GIIPaadndPQjV6RUf25M2E/1i7JOatynXCU++fWZhu0L61I+k/ ZcRQ== 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=ys9/8iZI2+AZ770JEgmBUcU9oubRnjFWNVLnWXmDC5A=; b=fJOeJYwB0JaFCkbTfKI5vUNd6XdkbysQFCFAOkQuByzl9JIB8/HQuq9sKzn5xqUZfA d+GNtTHHGIAwojK8O4zZeL63mHw4IxnqmoA1KXx6vYJD+l6b3KGN/AE5R8SU1A6IhD2L zCv76LP5nhy9FVXJfgzKBnHg6HkQr/okE/68WRPqRKQVujy8GBhyVkG0v+511Kk/wEvy cS1iihLYDpgPP8fUm0x9ov+V16ZyUWJSGMDYhAl9eiZJ+goVSRofUvPiQFdGE5l03+D+ H/GjWatXFDgW2Mroho0b0NvgDJ3Qa4GL8bJjqhtkcfoSynJDmEk27BiHrsOSrAU9RrjD /icA== 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 e7si3455939pgv.499.2019.02.05.09.57.58; Tue, 05 Feb 2019 09:58:14 -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 S1727523AbfBERmu (ORCPT + 99 others); Tue, 5 Feb 2019 12:42:50 -0500 Received: from mx0b-001b2d01.pphosted.com ([148.163.158.5]:36906 "EHLO mx0a-001b2d01.pphosted.com" rhost-flags-OK-OK-OK-FAIL) by vger.kernel.org with ESMTP id S1726534AbfBERmu (ORCPT ); Tue, 5 Feb 2019 12:42:50 -0500 Received: from pps.filterd (m0098413.ppops.net [127.0.0.1]) by mx0b-001b2d01.pphosted.com (8.16.0.27/8.16.0.27) with SMTP id x15Hc9Sx037774 for ; Tue, 5 Feb 2019 12:42:48 -0500 Received: from e11.ny.us.ibm.com (e11.ny.us.ibm.com [129.33.205.201]) by mx0b-001b2d01.pphosted.com with ESMTP id 2qff2bg6yu-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 05 Feb 2019 12:42:48 -0500 Received: from localhost by e11.ny.us.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 5 Feb 2019 17:42:47 -0000 Received: from b01cxnp22036.gho.pok.ibm.com (9.57.198.26) by e11.ny.us.ibm.com (146.89.104.198) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 5 Feb 2019 17:42:45 -0000 Received: from b01ledav006.gho.pok.ibm.com (b01ledav006.gho.pok.ibm.com [9.57.199.111]) by b01cxnp22036.gho.pok.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id x15HgiNh16908464 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL); Tue, 5 Feb 2019 17:42:44 GMT Received: from b01ledav006.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 1D718AC05B; Tue, 5 Feb 2019 17:42:44 +0000 (GMT) Received: from b01ledav006.gho.pok.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id D0933AC05F; Tue, 5 Feb 2019 17:42:43 +0000 (GMT) Received: from localhost (unknown [9.18.235.42]) by b01ledav006.gho.pok.ibm.com (Postfix) with ESMTPS; Tue, 5 Feb 2019 17:42:43 +0000 (GMT) Date: Tue, 5 Feb 2019 15:42:42 -0200 From: Murilo Opsfelder Araujo To: Christophe Leroy Cc: Kees Cook , Andrew Morton , Benjamin Herrenschmidt , Paul Mackerras , Michael Ellerman , Mike Rapoport , linux-mm@kvack.org, linuxppc-dev@lists.ozlabs.org, linux-kernel@vger.kernel.org Subject: Re: [PATCH v3 1/2] mm: add probe_user_read() References: <39fb6c5a191025378676492e140dc012915ecaeb.1547652372.git.christophe.leroy@c-s.fr> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <39fb6c5a191025378676492e140dc012915ecaeb.1547652372.git.christophe.leroy@c-s.fr> User-Agent: Mutt/1.10.1 (2018-07-13) X-TM-AS-GCONF: 00 x-cbid: 19020517-2213-0000-0000-0000034AA0F5 X-IBM-SpamModules-Scores: X-IBM-SpamModules-Versions: BY=3.00010542; HX=3.00000242; KW=3.00000007; PH=3.00000004; SC=3.00000279; SDB=6.01156741; UDB=6.00603425; IPR=6.00937266; MB=3.00025447; MTD=3.00000008; XFM=3.00000015; UTC=2019-02-05 17:42:47 X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 19020517-2214-0000-0000-00005D3E444D Message-Id: <20190205174242.GA24427@kermit.br.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:,, definitions=2019-02-05_07:,, 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-1902050135 Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Hi, Christophe. On Wed, Jan 16, 2019 at 04:59:27PM +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. > > The patch defines this function as a static inline so the "size" > variable can be examined for const-ness by the check_object_size() > in __copy_from_user_inatomic() > > Signed-off-by: Christophe Leroy > --- > v3: Moved 'Returns:" comment after description. > Explained in the commit log why the function is defined static inline > > v2: Added "Returns:" comment and removed probe_user_address() > > include/linux/uaccess.h | 34 ++++++++++++++++++++++++++++++++++ > 1 file changed, 34 insertions(+) > > diff --git a/include/linux/uaccess.h b/include/linux/uaccess.h > index 37b226e8df13..ef99edd63da3 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 > + * > + * 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. > + * > + * Returns: 0 on success, -EFAULT on error. > + */ > + > +#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; Hopefully, there is still time for a minor comment. Do we need to differentiate the returned error here, e.g.: return -EACCES? I wonder if there will be situations where callers need to know why probe_user_read() failed. Besides that: Acked-by: Murilo Opsfelder Araujo > + > + 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 > -- Murilo