Received: by 2002:a25:1985:0:0:0:0:0 with SMTP id 127csp1279467ybz; Wed, 22 Apr 2020 17:30:49 -0700 (PDT) X-Google-Smtp-Source: APiQypIJyCfLOCTGV03+7ZmIiEKNOs+YfwDhspRrJjMVahPwh7NIIlXJ2aJnHI40DfpiuUeAdKgQ X-Received: by 2002:a17:906:5287:: with SMTP id c7mr685538ejm.141.1587601849719; Wed, 22 Apr 2020 17:30:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1587601849; cv=none; d=google.com; s=arc-20160816; b=mewC6sE873bSVe2v2I4iYCRNkBhd7iKQokxmnpKDdYu3ANpfXqyOBGftfYwd48IV7J HsI2xHyYglYnQC7ZKl4Y54JczVamQVGNZ9jySawHw7Q2WtQbWoIPDNIvcWrY+TfXlAk/ pijOuDw8+wiGULPsLDeZPfN/8OQyQdko2vexArc/gx03bY/ILlPQjENU0t0GC1o9YjHQ Rrs61g3Gr9i2mZ+70uUrGB+4gjCr6KegEAbJ3PSjYvFRTGvpHNA6HpiB11/QsSzx3Yah mAbMd2WbXGVIJpBx5+wztw84mRw8WVTl6rbUTmur2ara2nSm+JiOp0tedX7lsH5KQswk JgAg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:to:from:subject:references:mime-version :message-id:in-reply-to:date:dkim-signature; bh=cLhIPyxMn3s+VxrKXhmBoW15tCFqEcrWbg6AizvES3c=; b=Q4ZoIzpXO4kapXI5gIE8jc1jB3F3u8Wh1u8dcoUI8UFgDw+sha8zAtrAOPJq+jxb7T /WbXyEC2lHhiKhYpFg1IZ6fU6b3B9RApC9aenmQRnYFa5myra5l4Su2BA7jMrGq73zhg IhorrIxKcbD1G9awTViKC2LUgu+eH4n//ItSEuZaGPPbZxcOdqbIaomsFgffFdAO5siB 58opIkPXlEChd5Age/3DD0Knz3xaSjH5koUu8YotF4EU7AP1BjvOo09+XlXlL0ASUWrj 5BXu+u6RP7bHlByyV7pdD60VwraR5V3pSdgiDCg88E8G96/9xGAVaOOa/UZwAj7HwpHp d9kw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=DkYFt5JS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i15si422801ejc.172.2020.04.22.17.30.26; Wed, 22 Apr 2020 17:30:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@google.com header.s=20161025 header.b=DkYFt5JS; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=REJECT sp=REJECT dis=NONE) header.from=google.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726386AbgDWA1I (ORCPT + 99 others); Wed, 22 Apr 2020 20:27:08 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53886 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-FAIL-OK-FAIL) by vger.kernel.org with ESMTP id S1725846AbgDWA1E (ORCPT ); Wed, 22 Apr 2020 20:27:04 -0400 Received: from mail-pj1-x104a.google.com (mail-pj1-x104a.google.com [IPv6:2607:f8b0:4864:20::104a]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 76F8DC03C1AD for ; Wed, 22 Apr 2020 17:27:04 -0700 (PDT) Received: by mail-pj1-x104a.google.com with SMTP id i9so3220799pjs.4 for ; Wed, 22 Apr 2020 17:27:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20161025; h=date:in-reply-to:message-id:mime-version:references:subject:from:to; bh=cLhIPyxMn3s+VxrKXhmBoW15tCFqEcrWbg6AizvES3c=; b=DkYFt5JS1cxdS2DHIvHgS0H+WuG1lYhcepNLKeMUdwTbK0/X/yFbZ/ke8bST2Y1V9i N6E+lCb7I55KnDLjr1oRU4jg2YrvHExJlXCWtvVjCQFHgQSq00Iiv9CmL0r9DzgUXxIz UaaOWHLzhaaQIiVOVf2sTLoYnWf9KbR0ubRP67JDTDjSA6utYb0NYceD6RS/GQfcq1Pg Opevl3csIbN4Ha7nrW/YsexZAL2w09hSV64mfaHidynrMu826KXNcPqEVzlvbnXqPlAx 9MYkFM8ZL1iS4waU0L3c3RBioyL2YwAYvwq6T7CUT1yonxAJjXO1oukoKp6D/v74GZt0 HtlQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:in-reply-to:message-id:mime-version :references:subject:from:to; bh=cLhIPyxMn3s+VxrKXhmBoW15tCFqEcrWbg6AizvES3c=; b=LLAceGRI7LYLYAe2aMNw+MXO2TMnw7HXAZI/SfQgjOvYW/Dmuznj6kz3QBz5UG8Qim DGsYJNq16XhLVsCjl596WCrKdwXZG55Py0pVAdFV754S2E+TZAsARkFNUMI/8fyTQ293 /PW1bGQdfHkvWylqsA0FNSH1Z41N/Mi2wA9Fg/EtoVeRzwXUikGC8UswVb1QYMF0S/RO Qcs6j06SBw+cfzIvTNAPLrdYwcs8FqISkfDCWkJKe59jli6TkQr/ECRc1GTgiK8w441w vC0QmTLdvv+CrUnnE0WIth9SgIwCeqPPEatTxM/GKtn77mNAdDduSOD5dKV57ijCv6uh CktQ== X-Gm-Message-State: AGi0Pua/p2gIbtYoI3HboLyDa/W3IyEi93DW/M1b9kIipn5Qh3XADrf2 CVpckH4PROaf3i/3DY+Rs9+lw0IfphM= X-Received: by 2002:a17:90a:210b:: with SMTP id a11mr1464127pje.31.1587601623780; Wed, 22 Apr 2020 17:27:03 -0700 (PDT) Date: Wed, 22 Apr 2020 17:26:32 -0700 In-Reply-To: <20200423002632.224776-1-dancol@google.com> Message-Id: <20200423002632.224776-3-dancol@google.com> Mime-Version: 1.0 References: <20200423002632.224776-1-dancol@google.com> X-Mailer: git-send-email 2.26.2.303.gf8c07b1a785-goog Subject: [PATCH 2/2] Add a new sysctl knob: unprivileged_userfaultfd_user_mode_only From: Daniel Colascione To: Jonathan Corbet , Alexander Viro , Luis Chamberlain , Kees Cook , Iurii Zaikin , Mauro Carvalho Chehab , Andrew Morton , Andy Shevchenko , Vlastimil Babka , Mel Gorman , Sebastian Andrzej Siewior , Peter Xu , Daniel Colascione , Andrea Arcangeli , Mike Rapoport , Jerome Glisse , Shaohua Li , linux-doc@vger.kernel.org, linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, timmurray@google.com, minchan@google.com, sspatil@google.com, lokeshgidra@google.com 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 This sysctl can be set to either zero or one. When zero (the default) the system lets all users call userfaultfd with or without UFFD_USER_MODE_ONLY, modulo other access controls. When unprivileged_userfaultfd_user_mode_only is set to one, users without CAP_SYS_PTRACE must pass UFFD_USER_MODE_ONLY to userfaultfd 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 --- Documentation/admin-guide/sysctl/vm.rst | 13 +++++++++++++ fs/userfaultfd.c | 11 ++++++++++- include/linux/userfaultfd_k.h | 1 + kernel/sysctl.c | 9 +++++++++ 4 files changed, 33 insertions(+), 1 deletion(-) diff --git a/Documentation/admin-guide/sysctl/vm.rst b/Documentation/admin-guide/sysctl/vm.rst index 0329a4d3fa9e..4296b508ab74 100644 --- a/Documentation/admin-guide/sysctl/vm.rst +++ b/Documentation/admin-guide/sysctl/vm.rst @@ -850,6 +850,19 @@ privileged users (with SYS_CAP_PTRACE capability). The default value is 1. +unprivileged_userfaultfd_user_mode_only +======================================== + +This flag controls whether unprivileged users can use the userfaultfd +system calls to handle page faults in kernel mode. If set to zero, +userfaultfd works with or without UFFD_USER_MODE_ONLY, modulo +unprivileged_userfaultfd above. If set to one, 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 0. user_reserve_kbytes =================== diff --git a/fs/userfaultfd.c b/fs/userfaultfd.c index 21378abe8f7b..85cc1ab74361 100644 --- a/fs/userfaultfd.c +++ b/fs/userfaultfd.c @@ -29,6 +29,7 @@ #include int sysctl_unprivileged_userfaultfd __read_mostly = 1; +int sysctl_unprivileged_userfaultfd_user_mode_only __read_mostly = 0; static struct kmem_cache *userfaultfd_ctx_cachep __read_mostly; @@ -2009,8 +2010,16 @@ SYSCALL_DEFINE1(userfaultfd, int, flags) static const int uffd_flags = UFFD_USER_MODE_ONLY; struct userfaultfd_ctx *ctx; int fd; + bool need_cap_check = false; - if (!sysctl_unprivileged_userfaultfd && !capable(CAP_SYS_PTRACE)) + if (!sysctl_unprivileged_userfaultfd) + need_cap_check = true; + + if (sysctl_unprivileged_userfaultfd_user_mode_only && + (flags & UFFD_USER_MODE_ONLY) == 0) + need_cap_check = true; + + if (need_cap_check && !capable(CAP_SYS_PTRACE)) return -EPERM; BUG_ON(!current->mm); diff --git a/include/linux/userfaultfd_k.h b/include/linux/userfaultfd_k.h index a8e5f3ea9bb2..d81e30074bf5 100644 --- a/include/linux/userfaultfd_k.h +++ b/include/linux/userfaultfd_k.h @@ -31,6 +31,7 @@ #define UFFD_FLAGS_SET (EFD_SHARED_FCNTL_FLAGS) extern int sysctl_unprivileged_userfaultfd; +extern int sysctl_unprivileged_userfaultfd_user_mode_only; extern vm_fault_t handle_userfault(struct vm_fault *vmf, unsigned long reason); diff --git a/kernel/sysctl.c b/kernel/sysctl.c index 8a176d8727a3..9cbdf4483961 100644 --- a/kernel/sysctl.c +++ b/kernel/sysctl.c @@ -1719,6 +1719,15 @@ static struct ctl_table vm_table[] = { .extra1 = SYSCTL_ZERO, .extra2 = SYSCTL_ONE, }, + { + .procname = "unprivileged_userfaultfd_user_mode_only", + .data = &sysctl_unprivileged_userfaultfd_user_mode_only, + .maxlen = sizeof(sysctl_unprivileged_userfaultfd_user_mode_only), + .mode = 0644, + .proc_handler = proc_dointvec_minmax, + .extra1 = SYSCTL_ZERO, + .extra2 = SYSCTL_ONE, + }, #endif { } }; -- 2.26.2.303.gf8c07b1a785-goog