From patchwork Sat Aug 22 01:40:17 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lokesh Gidra X-Patchwork-Id: 11730801 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 0E84E1575 for ; Sat, 22 Aug 2020 01:40:37 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id E052A20720 for ; Sat, 22 Aug 2020 01:40:36 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="ucn8M6gY" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726627AbgHVBkf (ORCPT ); Fri, 21 Aug 2020 21:40:35 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35160 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726483AbgHVBk2 (ORCPT ); Fri, 21 Aug 2020 21:40:28 -0400 Received: from mail-yb1-xb49.google.com (mail-yb1-xb49.google.com [IPv6:2607:f8b0:4864:20::b49]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 8322EC061575 for ; Fri, 21 Aug 2020 18:40:27 -0700 (PDT) Received: by mail-yb1-xb49.google.com with SMTP id e1so4054172ybk.14 for ; Fri, 21 Aug 2020 18:40:27 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=tKITLEyWnWCi9IJ4HMcscwuSv2AmZTmql/ok3Wo7ZvQ=; b=ucn8M6gYfR4WBFzw8eeNOL6MRtLncDDzaiGDLI6u21NlKZXLjmVIM6kCYQFIxVdxSJ zdsSL8sRuhAY02v8EPbLClsbB0hu7jJeZNfVgz2NwSv1KCveVJooy4+EBftCUaaGKJJw cc0qLfj32PQHBoSrbsvLv6ke8gSbrx/SedtQAYLjKRti5v3LdgVFIdmSGSWVviicG1/D OB7LLKlB6f+VXc1/QEm2z+HDWzzNrpQga3HUi3OjTldhS5rFl6ZI9PXBVWmJ2feZKBiW 0FqU6fHiUHVDFPOeLhfxiOUK4oGWMNOO6Z8a7ewewzlpFXDGeRIjH056qHfnde08RZKq WNFQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=tKITLEyWnWCi9IJ4HMcscwuSv2AmZTmql/ok3Wo7ZvQ=; b=rdV2JMaeDPJShj/Auhc3Q11aIZvGdIcgh1K44SsSaHcpf9ZLoDV0YDzFbCskoLs/6V 4q81GttrzHd1O83N0PEazseakwd4142UkKAREmCZA+x0ZAJnitIqbean+Td11dJ18oQ0 6ju37jbiJLhyt+nw/9M6WK9P3bEpZfia+9fApoNqgqArk4iLpHM/ydFg82qz+iW3Px4u vD7O0uuvI3/dKqH1tyDKpR5UjzQHuTxrTmSnoX+F37iTbOebIQMOo9stcZJf8TinP1OF 71KxzvqKSnbook+gf6r+wiLz0q43ysW0hlrs3cXoCijCxhaMUmIX+SCRQkR9QlVvb14m +tKg== X-Gm-Message-State: AOAM5311LVkSmplQAPJD+Mq+oCoT+pXizFncYfC9+0HEh6ndlBuxrWTy U7PAzxKHzZoM3md27VJpKU3nvAUlwfhwRBCcAg== X-Google-Smtp-Source: ABdhPJyDjyQrzQvudTYj4/X7uhGm0k2D6vJ9FL3QpJpxNbUXIBUfhsFQ4o0as7SBR1h8V3D/ko8jO98VYXcUtxCLwQ== X-Received: from lg.mtv.corp.google.com ([2620:15c:211:202:f693:9fff:fef4:29dd]) (user=lokeshgidra job=sendgmr) by 2002:a25:c582:: with SMTP id v124mr7843055ybe.456.1598060426970; Fri, 21 Aug 2020 18:40:26 -0700 (PDT) Date: Fri, 21 Aug 2020 18:40:17 -0700 In-Reply-To: <20200822014018.913868-1-lokeshgidra@google.com> Message-Id: <20200822014018.913868-2-lokeshgidra@google.com> Mime-Version: 1.0 References: <20200822014018.913868-1-lokeshgidra@google.com> X-Mailer: git-send-email 2.28.0.297.g1956fa8f8d-goog Subject: [PATCH v2 1/2] Add UFFD_USER_MODE_ONLY From: Lokesh Gidra To: Kees Cook , Jonathan Corbet , Peter Xu , Andrea Arcangeli , Andrew Morton Cc: Alexander Viro , Stephen Smalley , Eric Biggers , Lokesh Gidra , Daniel Colascione , "Joel Fernandes (Google)" , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kaleshsingh@google.com, calin@google.com, surenb@google.com, nnk@google.com, jeffv@google.com, kernel-team@android.com, Mike Rapoport , Shaohua Li , Jerome Glisse , Sebastian Andrzej Siewior , Mauro Carvalho Chehab , Johannes Weiner , Mel Gorman , Nitin Gupta , Vlastimil Babka , Iurii Zaikin , Luis Chamberlain , Daniel Colascione Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org userfaultfd handles page faults from both user and kernel code. Add a new UFFD_USER_MODE_ONLY flag for userfaultfd(2) that makes the resulting userfaultfd object refuse to handle faults from kernel mode, treating these faults as if SIGBUS were always raised, causing the kernel code to fail with EFAULT. A future patch adds a knob allowing administrators to give some processes the ability to create userfaultfd file objects only if they pass UFFD_USER_MODE_ONLY, reducing the likelihood that these processes will exploit userfaultfd's ability to delay kernel page faults to open timing windows for future exploits. Signed-off-by: Daniel Colascione Signed-off-by: Lokesh Gidra --- fs/userfaultfd.c | 7 ++++++- include/uapi/linux/userfaultfd.h | 9 +++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 0e4a3837da52..3e4ae6145112 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -405,6 +405,9 @@ vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason) if (ctx->features & UFFD_FEATURE_SIGBUS) goto out; + if ((vmf->flags & FAULT_FLAG_USER) == 0 && + ctx->flags & UFFD_USER_MODE_ONLY) + goto out; /* * If it's already released don't get it. This avoids to loop @@ -1966,6 +1969,7 @@ static void init_once_userfaultfd_ctx(void *mem) SYSCALL_DEFINE1(userfaultfd, int, flags) { + static const int uffd_flags = UFFD_USER_MODE_ONLY; struct userfaultfd_ctx *ctx; int fd; @@ -1975,10 +1979,11 @@ SYSCALL_DEFINE1(userfaultfd, int, flags) BUG_ON(!current->mm); /* Check the UFFD_* constants for consistency. */ + BUILD_BUG_ON(uffd_flags & UFFD_SHARED_FCNTL_FLAGS); BUILD_BUG_ON(UFFD_CLOEXEC != O_CLOEXEC); BUILD_BUG_ON(UFFD_NONBLOCK != O_NONBLOCK); - if (flags & ~UFFD_SHARED_FCNTL_FLAGS) + if (flags & ~(UFFD_SHARED_FCNTL_FLAGS | uffd_flags)) return -EINVAL; ctx = kmem_cache_alloc(userfaultfd_ctx_cachep, GFP_KERNEL); diff --git a/include/uapi/linux/userfaultfd.h b/include/uapi/linux/userfaultfd.h index e7e98bde221f..5f2d88212f7c 100644 --- a/include/uapi/linux/userfaultfd.h +++ b/include/uapi/linux/userfaultfd.h @@ -257,4 +257,13 @@ struct uffdio_writeprotect { __u64 mode; }; +/* + * Flags for the userfaultfd(2) system call itself. + */ + +/* + * Create a userfaultfd that can handle page faults only in user mode. + */ +#define UFFD_USER_MODE_ONLY 1 + #endif /* _LINUX_USERFAULTFD_H */ From patchwork Sat Aug 22 01:40:18 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Lokesh Gidra X-Patchwork-Id: 11730803 Return-Path: Received: from mail.kernel.org (pdx-korg-mail-1.web.codeaurora.org [172.30.200.123]) by pdx-korg-patchwork-2.web.codeaurora.org (Postfix) with ESMTP id 79C84913 for ; Sat, 22 Aug 2020 01:40:48 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by mail.kernel.org (Postfix) with ESMTP id 5B72E2072D for ; Sat, 22 Aug 2020 01:40:48 +0000 (UTC) Authentication-Results: mail.kernel.org; dkim=fail reason="signature verification failed" (2048-bit key) header.d=google.com header.i=@google.com header.b="Fjb47DU5" Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726676AbgHVBkl (ORCPT ); Fri, 21 Aug 2020 21:40:41 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35172 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726585AbgHVBkd (ORCPT ); Fri, 21 Aug 2020 21:40:33 -0400 Received: from mail-qv1-xf4a.google.com (mail-qv1-xf4a.google.com [IPv6:2607:f8b0:4864:20::f4a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 3F054C061574 for ; Fri, 21 Aug 2020 18:40:31 -0700 (PDT) Received: by mail-qv1-xf4a.google.com with SMTP id q4so2437969qvu.6 for ; Fri, 21 Aug 2020 18:40:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=sender:date:in-reply-to:message-id:mime-version:references:subject :from:to:cc; bh=MZt6zWa+RNv5nFf3tGacIvcIKi4HOMw7I8ZdHhGadw8=; b=Fjb47DU5UAmSrK+favah3JR3SaEeIHpvEskXHgYrtYJ3n/Bq3bJrOY1YznTe99CW9w DFFIxz48eB5rPJvbwZ2WmLcr2udtejpyE5PqXM8EcPnXgeTz0iF5sYdswDmwwMS9HZgw AekKXEkBi+3/lLdRxCUTx8u+CUn1noNVk8XXftvGOqPgK+jMFzLQgKigImeF0kzHlWqL Z1Rvj6co0gfQGg9GPvRp4AyBe9tkZ/yVC0BCmF6i0CwLfxSSQnm8UzGsE0uvZj9QkjSa cnB+JCWlCT8nJW88RKFw1sv86Du/hRSoJqDwnHSeAgdx3gajxoWK8DAZ67q75sM4G4mX Ujmg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:in-reply-to:message-id:mime-version :references:subject:from:to:cc; bh=MZt6zWa+RNv5nFf3tGacIvcIKi4HOMw7I8ZdHhGadw8=; b=OOp4Jt/IlUqUsQlY9on+sH5pMeyZZO8NCU9wjlB0dDHpBj+Q7CjkgAUXUsC8MhNdzw XJ1NV7nzTTTA3qLgYgXK3SeSedOKDRVcmuLia2unvHxPRIcAXYw1uos+frqoyND/Ppjz uO7U10rjaZ3xdCI6Uf4xZpOBjrJI32W4h5lgK36Ns0DuRzDLmzLtrIEY9wARnDIMZ850 EKKdwxJ/R0B9SdHfcMB5yR/OiZ9rY0TisDv/Hw4latVDB0lL4X27INk2rkJlR/sXsvvc QAaL0ukGuxTmwPRI4mKPmYqSd60RYtMkFkAfMfCUkifHaXDFQhPLgp9xip+HkXy2FdW3 bINQ== X-Gm-Message-State: AOAM5324Yz3QvMZQnMsSVsPjidpwbVlUtsz8fFIq6j0968wbR36IMF/U YassMvw+b7tNGtNCkOx6Y+amrKUoG7vZz9WBYg== X-Google-Smtp-Source: ABdhPJylWPWmpbtUgK/VlQmDrGjkTVQmIPagoBhVfweXeFSEuVo3aeNUXXPJ4rkw3klaFazqhLHnatwJe2pNyCOagQ== X-Received: from lg.mtv.corp.google.com ([2620:15c:211:202:f693:9fff:fef4:29dd]) (user=lokeshgidra job=sendgmr) by 2002:a0c:d44e:: with SMTP id r14mr5036474qvh.105.1598060430375; Fri, 21 Aug 2020 18:40:30 -0700 (PDT) Date: Fri, 21 Aug 2020 18:40:18 -0700 In-Reply-To: <20200822014018.913868-1-lokeshgidra@google.com> Message-Id: <20200822014018.913868-3-lokeshgidra@google.com> Mime-Version: 1.0 References: <20200822014018.913868-1-lokeshgidra@google.com> X-Mailer: git-send-email 2.28.0.297.g1956fa8f8d-goog Subject: [PATCH v2 2/2] Add user-mode only option to unprivileged_userfaultfd sysctl knob From: Lokesh Gidra To: Kees Cook , Jonathan Corbet , Peter Xu , Andrea Arcangeli , Andrew Morton Cc: Alexander Viro , Stephen Smalley , Eric Biggers , Lokesh Gidra , Daniel Colascione , "Joel Fernandes (Google)" , linux-fsdevel@vger.kernel.org, linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, kaleshsingh@google.com, calin@google.com, surenb@google.com, nnk@google.com, jeffv@google.com, kernel-team@android.com, Mike Rapoport , Shaohua Li , Jerome Glisse , Sebastian Andrzej Siewior , Mauro Carvalho Chehab , Johannes Weiner , Mel Gorman , Nitin Gupta , Vlastimil Babka , Iurii Zaikin , Luis Chamberlain , Daniel Colascione Sender: linux-fsdevel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-fsdevel@vger.kernel.org A third option is added to 'unprivileged_userfaultfd' sysctl knob. When the knob is set to 2, it allows unprivileged users to call userfaultfd, like when it is set to 1, but with the restriction that page faults from only user-mode can be handled. In this mode, an unprivileged user (without SYS_CAP_PTRACE capability) must pass UFFD_USER_MODE_ONLY to userfaultd or the API will fail with EPERM. This facility allows administrators to reduce the likelihood that an attacker with access to userfaultfd can delay faulting kernel code to widen timing windows for other exploits. Signed-off-by: Daniel Colascione Signed-off-by: Lokesh Gidra --- Documentation/admin-guide/sysctl/vm.rst | 10 +++++++--- fs/userfaultfd.c | 10 ++++++++-- kernel/sysctl.c | 2 +- 3 files changed, 16 insertions(+), 6 deletions(-) diff --git a/Documentation/admin-guide/sysctl/vm.rst b/Documentation/admin-guide/sysctl/vm.rst index 4b9d2e8e9142..23d6feb79f5c 100644 --- a/Documentation/admin-guide/sysctl/vm.rst +++ b/Documentation/admin-guide/sysctl/vm.rst @@ -872,9 +872,13 @@ unprivileged_userfaultfd ======================== This flag controls whether unprivileged users can use the userfaultfd -system calls. Set this to 1 to allow unprivileged users to use the -userfaultfd system calls, or set this to 0 to restrict userfaultfd to only -privileged users (with SYS_CAP_PTRACE capability). +system calls. Set this to 0 to restrict userfaultfd to only privileged +users (with SYS_CAP_PTRACE capability), set this to 1 to allow unprivileged +users to use the userfaultfd system calls, or set this to 2 to restrict +unprivileged users to handle page faults in user mode only. In the last case, +users without SYS_CAP_PTRACE must pass UFFD_USER_MODE_ONLY in order for +userfaultfd to succeed. Prohibiting use of userfaultfd for handling faults +from kernel mode may make certain vulnerabilities more difficult to exploit. The default value is 1. diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 3e4ae6145112..2fcdeb28c960 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -1973,8 +1973,14 @@ SYSCALL_DEFINE1(userfaultfd, int, flags) struct userfaultfd_ctx *ctx; int fd; - if (!sysctl_unprivileged_userfaultfd && !capable(CAP_SYS_PTRACE)) - return -EPERM; + switch (sysctl_unprivileged_userfaultfd) { + case 2: + if (flags & UFFD_USER_MODE_ONLY) + break; + case 0: + if (!capable(CAP_SYS_PTRACE)) + return -EPERM; + } BUG_ON(!current->mm); diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 287862f91717..7e94215dfff5 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -3119,7 +3119,7 @@ static struct ctl_table vm_table[] = { .mode = 0644, .proc_handler = proc_dointvec_minmax, .extra1 = SYSCTL_ZERO, - .extra2 = SYSCTL_ONE, + .extra2 = &two, }, #endif { }