From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 38DC6C00140 for ; Thu, 11 Aug 2022 00:42:53 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232539AbiHKAmw (ORCPT ); Wed, 10 Aug 2022 20:42:52 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:53820 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232964AbiHKAms (ORCPT ); Wed, 10 Aug 2022 20:42:48 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 1D7F4844CB for ; Wed, 10 Aug 2022 17:42:47 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id B068561068 for ; Thu, 11 Aug 2022 00:42:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 0C3DAC433D6; Thu, 11 Aug 2022 00:42:46 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1660178566; bh=ZjlsjmAEiTWz5AOzjuVe9ZrHbjPQiHyFPcK31aJcxg0=; h=Date:To:From:Subject:From; b=iKDHeg5FeDomZQeFm9HzervK+GQBgMzzEkidqtABbms4oBcbz6sf16hEn4EJlPuPN MPZLXGjF1Ls+Gfg8kJIvw7RTpBtn9oZk/G8WBTUO8qv3s5qGnXvCu3pUX7EL+4HA1m dFqSxnIn+66t76lcIDgqqDc2+aO7cK9Mwbrt8xYY= Date: Wed, 10 Aug 2022 17:42:45 -0700 To: mm-commits@vger.kernel.org, yi.zhang@huawei.com, viro@zeniv.linux.org.uk, vbabka@suse.cz, surenb@google.com, skhan@linuxfoundation.org, shuah@kernel.org, rppt@kernel.org, peterx@redhat.com, namit@vmware.com, mike.kravetz@oracle.com, mgorman@techsingularity.net, ldv@altlinux.org, jack@suse.cz, hughd@google.com, glebfm@altlinux.org, dave.hansen@linux.intel.com, corbet@lwn.net, axelrasmussen@google.com, akpm@linux-foundation.org From: Andrew Morton Subject: + userfaultfd-update-documentation-to-describe-dev-userfaultfd.patch added to mm-unstable branch Message-Id: <20220811004246.0C3DAC433D6@smtp.kernel.org> Precedence: bulk Reply-To: linux-kernel@vger.kernel.org List-ID: X-Mailing-List: mm-commits@vger.kernel.org The patch titled Subject: userfaultfd: update documentation to describe /dev/userfaultfd has been added to the -mm mm-unstable branch. Its filename is userfaultfd-update-documentation-to-describe-dev-userfaultfd.patch This patch will shortly appear at https://git.kernel.org/pub/scm/linux/kernel/git/akpm/25-new.git/tree/patches/userfaultfd-update-documentation-to-describe-dev-userfaultfd.patch This patch will later appear in the mm-unstable branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm Before you just go and hit "reply", please: a) Consider who else should be cc'ed b) Prefer to cc a suitable mailing list as well c) Ideally: find the original patch on the mailing list and do a reply-to-all to that, adding suitable additional cc's *** Remember to use Documentation/process/submit-checklist.rst when testing your code *** The -mm tree is included into linux-next via the mm-everything branch at git://git.kernel.org/pub/scm/linux/kernel/git/akpm/mm and is updated there every 2-3 working days ------------------------------------------------------ From: Axel Rasmussen Subject: userfaultfd: update documentation to describe /dev/userfaultfd Date: Mon, 8 Aug 2022 10:56:13 -0700 Explain the different ways to create a new userfaultfd, and how access control works for each way. Link: https://lkml.kernel.org/r/20220808175614.3885028-5-axelrasmussen@google.com Signed-off-by: Axel Rasmussen Acked-by: Peter Xu Cc: Al Viro Cc: Dave Hansen Cc: Dmitry V. Levin Cc: Gleb Fotengauer-Malinovskiy Cc: Hugh Dickins Cc: Jan Kara Cc: Jonathan Corbet Cc: Mel Gorman Cc: Mike Kravetz Cc: Mike Rapoport Cc: Nadav Amit Cc: Shuah Khan Cc: Shuah Khan Cc: Suren Baghdasaryan Cc: Vlastimil Babka Cc: Zhang Yi Signed-off-by: Andrew Morton --- Documentation/admin-guide/mm/userfaultfd.rst | 41 +++++++++++++++-- Documentation/admin-guide/sysctl/vm.rst | 3 + 2 files changed, 41 insertions(+), 3 deletions(-) --- a/Documentation/admin-guide/mm/userfaultfd.rst~userfaultfd-update-documentation-to-describe-dev-userfaultfd +++ a/Documentation/admin-guide/mm/userfaultfd.rst @@ -17,7 +17,10 @@ of the ``PROT_NONE+SIGSEGV`` trick. Design ====== -Userfaults are delivered and resolved through the ``userfaultfd`` syscall. +Userspace creates a new userfaultfd, initializes it, and registers one or more +regions of virtual memory with it. Then, any page faults which occur within the +region(s) result in a message being delivered to the userfaultfd, notifying +userspace of the fault. The ``userfaultfd`` (aside from registering and unregistering virtual memory ranges) provides two primary functionalities: @@ -34,12 +37,11 @@ The real advantage of userfaults if comp management of mremap/mprotect is that the userfaults in all their operations never involve heavyweight structures like vmas (in fact the ``userfaultfd`` runtime load never takes the mmap_lock for writing). - Vmas are not suitable for page- (or hugepage) granular fault tracking when dealing with virtual address spaces that could span Terabytes. Too many vmas would be needed for that. -The ``userfaultfd`` once opened by invoking the syscall, can also be +The ``userfaultfd``, once created, can also be passed using unix domain sockets to a manager process, so the same manager process could handle the userfaults of a multitude of different processes without them being aware about what is going on @@ -50,6 +52,39 @@ is a corner case that would currently re API === +Creating a userfaultfd +---------------------- + +There are two ways to create a new userfaultfd, each of which provide ways to +restrict access to this functionality (since historically userfaultfds which +handle kernel page faults have been a useful tool for exploiting the kernel). + +The first way, supported since userfaultfd was introduced, is the +userfaultfd(2) syscall. Access to this is controlled in several ways: + +- Any user can always create a userfaultfd which traps userspace page faults + only. Such a userfaultfd can be created using the userfaultfd(2) syscall + with the flag UFFD_USER_MODE_ONLY. + +- In order to also trap kernel page faults for the address space, then either + the process needs the CAP_SYS_PTRACE capability, or the system must have + vm.unprivileged_userfaultfd set to 1. By default, vm.unprivileged_userfaultfd + is set to 0. + +The second way, added to the kernel more recently, is by opening and issuing a +USERFAULTFD_IOC_NEW ioctl to /dev/userfaultfd. This method yields equivalent +userfaultfds to the userfaultfd(2) syscall. + +Unlike userfaultfd(2), access to /dev/userfaultfd is controlled via normal +filesystem permissions (user/group/mode), which gives fine grained access to +userfaultfd specifically, without also granting other unrelated privileges at +the same time (as e.g. granting CAP_SYS_PTRACE would do). Users who have access +to /dev/userfaultfd can always create userfaultfds that trap kernel page faults; +vm.unprivileged_userfaultfd is not considered. + +Initializing a userfaultfd +-------------------------- + When first opened the ``userfaultfd`` must be enabled invoking the ``UFFDIO_API`` ioctl specifying a ``uffdio_api.api`` value set to ``UFFD_API`` (or a later API version) which will specify the ``read/POLLIN`` protocol --- a/Documentation/admin-guide/sysctl/vm.rst~userfaultfd-update-documentation-to-describe-dev-userfaultfd +++ a/Documentation/admin-guide/sysctl/vm.rst @@ -926,6 +926,9 @@ calls without any restrictions. The default value is 0. +Another way to control permissions for userfaultfd is to use +/dev/userfaultfd instead of userfaultfd(2). See +Documentation/admin-guide/mm/userfaultfd.rst. user_reserve_kbytes =================== _ Patches currently in -mm which might be from axelrasmussen@google.com are selftests-vm-add-hugetlb_shared-userfaultfd-test-to-run_vmtestssh.patch userfaultfd-add-dev-userfaultfd-for-fine-grained-access-control.patch userfaultfd-selftests-modify-selftest-to-use-dev-userfaultfd.patch userfaultfd-update-documentation-to-describe-dev-userfaultfd.patch selftests-vm-add-dev-userfaultfd-test-cases-to-run_vmtestssh.patch