Received: by 2002:ac0:a582:0:0:0:0:0 with SMTP id m2-v6csp3323137imm; Fri, 19 Oct 2018 08:44:59 -0700 (PDT) X-Google-Smtp-Source: ACcGV61l4ckhCFTL6S2PQz4i5oyEIrdvVwQxp8OrwEBGe7PLtZVYruQ3MsuOv5cVxQvkk+vXCilU X-Received: by 2002:a63:6bc2:: with SMTP id g185-v6mr33636162pgc.25.1539963899849; Fri, 19 Oct 2018 08:44:59 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1539963899; cv=none; d=google.com; s=arc-20160816; b=rsIpIZaGtLhePnHDK80d7B/m6Y8pfX/Nr9C5ujVTsYhN+ifTOZJa93SZR8TNJrGM9+ 8//6cC7XNobpKO4YRWp0M5K2E+kbZKoC/RILv+VkdGgIdS+YmmupXpmQLTwTKXKGMPby hnwgZJiAuaVOXxoTd3crwbIPLR/cj0puJwPC5q/eq4EFnAy8/q1JgyuxMBQcncpqWJLJ schaKz4X5VEWlGZ/eKLS2UBjwBtKvUKut3Sqc46gtHWkl4BAtcXDVE0efqL3hv0dnEp6 HP7l87mzlS+o4Z22dcXyTz7RVjP+QtCfrQHxYLqmciDs7NtJdA1pxMch6qP7BhyypYF6 JOzA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:cc:to:subject:message-id:date:from :references:in-reply-to:mime-version:dkim-signature; bh=x9ZKFla8glrDERS4Gs9Sgo77xKF/nEALFb2B2FNwP6w=; b=KBfXi9uEDxqKkr8lZMjsyN8aIE9dX8OuskiG56/A+Xv5Fvv8TWX2f4MMhG7DWAsVaL xGEwVkch57vSNjOUtq+4tNb3Fxr+mv6DpbuPHnAtbgKQ5cjXJwFDuW7rGJ7g0QlBwo/m V8+VDX5e5QA4PTd3a7m863nNr14fMq7t0BJejzSLRHE4zlXHsj2WuXiwNSJdb04VlgW2 SdMarWrNT7Ux9MKPU0AzwAnkmt11+3NjZnYSJJC+m8OTqwqDIoJewMlP/AqJL6IgeAUS pp1AtDCnTt2X0Wks5BJubEm+0zEZ0jaqT87+raqA/fxSD9mAYJjk2p5tYAJRAvRMME3R utng== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@chromium.org header.s=google header.b="JsQ/G5nM"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id j12-v6si24447040pgj.489.2018.10.19.08.44.44; Fri, 19 Oct 2018 08:44:59 -0700 (PDT) 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; dkim=pass header.i=@chromium.org header.s=google header.b="JsQ/G5nM"; 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=pass (p=NONE sp=NONE dis=NONE) header.from=chromium.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1727282AbeJSXsy (ORCPT + 99 others); Fri, 19 Oct 2018 19:48:54 -0400 Received: from mail-yb1-f193.google.com ([209.85.219.193]:42994 "EHLO mail-yb1-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726702AbeJSXsy (ORCPT ); Fri, 19 Oct 2018 19:48:54 -0400 Received: by mail-yb1-f193.google.com with SMTP id p74-v6so13410822ybc.9 for ; Fri, 19 Oct 2018 08:42:16 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=chromium.org; s=google; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=x9ZKFla8glrDERS4Gs9Sgo77xKF/nEALFb2B2FNwP6w=; b=JsQ/G5nMt9hQeWn2BZJqDbJ6yQLI0Xwl/sQkKSnsbMOmBF3K9vv1IjavLLHO+TqHpQ 7cl/f/pBeHwPc2G8FFjTxqqXhvsML3P+GY/gF7bM8sueAk+J8S2uWEWOq84fY14TtVtM cfo71NRrLwRF8iBE+e6Uckf12wvRKzP4oFG/8= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=x9ZKFla8glrDERS4Gs9Sgo77xKF/nEALFb2B2FNwP6w=; b=X1nQC/s+SP15wvjp7lohU/QXMacAZLgj5IOiHcfy2LUrrs0E43CL8WeHBeobCYEh/G 4DCXJOb2YaMJ/k9EhbsUGiOsMAj65cS2g0TUn2qaqHJg4hdoxs3Lf3CTB+Uh0/Co1IVs PcRuznFC+Pyb/4JeGKOqwfi5wgcgtEl6ymDKQLwRK6YPMpfG9EeMvInnlMoPGfFZqY6z 2u+a9c5hMDb/H+nQto+9yusk7dAgcK748do6pGvRrZ0cygKYrFV2Wod78SJDoE/h6MPf 1Wxh6Ivz0mNWQNF4Ru+qCZSJKDb0xrYI4fE54V1DO3q7NubXK851hjhk4/HBRUgr2e/X ufKA== X-Gm-Message-State: ABuFfog4qqVXcViuHQ2WvhgaAYSHXTasM7gcy/h0SbsqY+MyF5AwnjG2 /RCFHiOKVb72yejgdi7joWlqICv7CrA= X-Received: by 2002:a25:198a:: with SMTP id 132-v6mr23394480ybz.325.1539963735421; Fri, 19 Oct 2018 08:42:15 -0700 (PDT) Received: from mail-yw1-f54.google.com (mail-yw1-f54.google.com. [209.85.161.54]) by smtp.gmail.com with ESMTPSA id k85-v6sm5958047ywa.76.2018.10.19.08.42.14 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 19 Oct 2018 08:42:14 -0700 (PDT) Received: by mail-yw1-f54.google.com with SMTP id v1-v6so13348023ywv.6 for ; Fri, 19 Oct 2018 08:42:14 -0700 (PDT) X-Received: by 2002:a0d:d302:: with SMTP id v2-v6mr24279039ywd.124.1539963733716; Fri, 19 Oct 2018 08:42:13 -0700 (PDT) MIME-Version: 1.0 Received: by 2002:a25:d116:0:0:0:0:0 with HTTP; Fri, 19 Oct 2018 08:42:12 -0700 (PDT) In-Reply-To: <336eb81e62d6c683a69d312f533899dcb6bcf770.1539959864.git.christophe.leroy@c-s.fr> References: <336eb81e62d6c683a69d312f533899dcb6bcf770.1539959864.git.christophe.leroy@c-s.fr> From: Kees Cook Date: Fri, 19 Oct 2018 08:42:12 -0700 X-Gmail-Original-Message-ID: Message-ID: Subject: Re: [RFC PATCH] mm: add probe_user_read() and probe_user_address() To: Christophe Leroy Cc: Andrew Morton , Michael Ellerman , LKML , PowerPC , Linux-MM 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 On Fri, Oct 19, 2018 at 8:14 AM, Christophe Leroy wrote: > 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"))); Let's use #defines to deal with per-arch aliases so we can keep the inline I'm suggesting below... > + > +long __probe_user_read(void *dst, const void __user *src, size_t size) Please make this __always_inline so the "size" variable can be examined for const-ness by the check_object_size() in __copy_from_user_inatomic(). -Kees -- Kees Cook Pixel Security