linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Control over userfaultfd kernel-fault handling
@ 2020-08-22  1:40 Lokesh Gidra
  2020-08-22  1:40 ` [PATCH v2 1/2] Add UFFD_USER_MODE_ONLY Lokesh Gidra
  2020-08-22  1:40 ` [PATCH v2 2/2] Add user-mode only option to unprivileged_userfaultfd sysctl knob Lokesh Gidra
  0 siblings, 2 replies; 5+ messages in thread
From: Lokesh Gidra @ 2020-08-22  1:40 UTC (permalink / raw)
  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, linux-kernel, linux-doc, kaleshsingh, calin,
	surenb, nnk, jeffv, kernel-team, Mike Rapoport, Shaohua Li,
	Jerome Glisse, Sebastian Andrzej Siewior, Mauro Carvalho Chehab,
	Johannes Weiner, Mel Gorman, Nitin Gupta, Vlastimil Babka,
	Iurii Zaikin, Luis Chamberlain

This patch series is split from [1]. The other series enables SELinux
support for userfaultfd file descriptors so that its creation and
movement can be controlled.

It has been demonstrated on various occasions that suspending kernel
code execution for an arbitrary amount of time at any access to
userspace memory (copy_from_user()/copy_to_user()/...) can be exploited
to change the intended behavior of the kernel. For instance, handling
page faults in kernel-mode using userfaultfd has been exploited in [2, 3].
Likewise, FUSE, which is similar to userfaultfd in this respect, has been
exploited in [4, 5] for similar outcome.

This small patch series adds a new flag to userfaultfd(2) that allows
callers to give up the ability to handle kernel-mode faults with the
resulting UFFD file object. It then adds a 'user-mode only' option to
the unprivileged_userfaultfd sysctl knob to require unprivileged
callers to use this new flag.

The purpose of this new interface is to decrease the chance of an
unprivileged userfaultfd user taking advantage of userfaultfd to
enhance security vulnerabilities by lengthening the race window in
kernel code.

[1] https://lore.kernel.org/lkml/20200211225547.235083-1-dancol@google.com/
[2] https://duasynt.com/blog/linux-kernel-heap-spray
[3] https://duasynt.com/blog/cve-2016-6187-heap-off-by-one-exploit
[4] https://googleprojectzero.blogspot.com/2016/06/exploiting-recursion-in-linux-kernel_20.html
[5] https://bugs.chromium.org/p/project-zero/issues/detail?id=808

Changes since v1:

  - Added external references to the threats from allowing unprivileged
    users to handle page faults from kernel-mode.
  - Removed the new sysctl knob restricting handling of page
    faults from kernel-mode, and added an option for the same
    in the existing 'unprivileged_userfaultfd' knob.

Lokesh Gidra (2):
  Add UFFD_USER_MODE_ONLY
  Add user-mode only option to unprivileged_userfaultfd sysctl knob

 Documentation/admin-guide/sysctl/vm.rst | 10 +++++++---
 fs/userfaultfd.c                        | 17 ++++++++++++++---
 include/uapi/linux/userfaultfd.h        |  9 +++++++++
 kernel/sysctl.c                         |  2 +-
 4 files changed, 31 insertions(+), 7 deletions(-)

-- 
2.28.0.297.g1956fa8f8d-goog


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/2] Add UFFD_USER_MODE_ONLY
  2020-08-22  1:40 [PATCH v2 0/2] Control over userfaultfd kernel-fault handling Lokesh Gidra
@ 2020-08-22  1:40 ` Lokesh Gidra
  2020-08-24 12:32   ` Sebastian Andrzej Siewior
  2020-08-22  1:40 ` [PATCH v2 2/2] Add user-mode only option to unprivileged_userfaultfd sysctl knob Lokesh Gidra
  1 sibling, 1 reply; 5+ messages in thread
From: Lokesh Gidra @ 2020-08-22  1:40 UTC (permalink / raw)
  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, linux-kernel, linux-doc, kaleshsingh, calin,
	surenb, nnk, jeffv, kernel-team, 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

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 <dancol@google.com>
Signed-off-by: Lokesh Gidra <lokeshgidra@google.com>
---
 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 */
-- 
2.28.0.297.g1956fa8f8d-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/2] Add user-mode only option to unprivileged_userfaultfd sysctl knob
  2020-08-22  1:40 [PATCH v2 0/2] Control over userfaultfd kernel-fault handling Lokesh Gidra
  2020-08-22  1:40 ` [PATCH v2 1/2] Add UFFD_USER_MODE_ONLY Lokesh Gidra
@ 2020-08-22  1:40 ` Lokesh Gidra
  1 sibling, 0 replies; 5+ messages in thread
From: Lokesh Gidra @ 2020-08-22  1:40 UTC (permalink / raw)
  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, linux-kernel, linux-doc, kaleshsingh, calin,
	surenb, nnk, jeffv, kernel-team, 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

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 <dancol@google.com>
Signed-off-by: Lokesh Gidra <lokeshgidra@google.com>
---
 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
 	{ }
-- 
2.28.0.297.g1956fa8f8d-goog


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] Add UFFD_USER_MODE_ONLY
  2020-08-22  1:40 ` [PATCH v2 1/2] Add UFFD_USER_MODE_ONLY Lokesh Gidra
@ 2020-08-24 12:32   ` Sebastian Andrzej Siewior
  2020-08-26  0:24     ` Lokesh Gidra
  0 siblings, 1 reply; 5+ messages in thread
From: Sebastian Andrzej Siewior @ 2020-08-24 12:32 UTC (permalink / raw)
  To: Lokesh Gidra
  Cc: Kees Cook, Jonathan Corbet, Peter Xu, Andrea Arcangeli,
	Andrew Morton, Alexander Viro, Stephen Smalley, Eric Biggers,
	Daniel Colascione, Joel Fernandes (Google),
	linux-fsdevel, linux-kernel, linux-doc, kaleshsingh, calin,
	surenb, nnk, jeffv, kernel-team, Mike Rapoport, Shaohua Li,
	Jerome Glisse, Mauro Carvalho Chehab, Johannes Weiner,
	Mel Gorman, Nitin Gupta, Vlastimil Babka, Iurii Zaikin,
	Luis Chamberlain, Daniel Colascione

On 2020-08-21 18:40:17 [-0700], Lokesh Gidra wrote:
> --- a/fs/userfaultfd.c
> +++ b/fs/userfaultfd.c
> @@ -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;
Why?

Sebastian

^ permalink raw reply	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 1/2] Add UFFD_USER_MODE_ONLY
  2020-08-24 12:32   ` Sebastian Andrzej Siewior
@ 2020-08-26  0:24     ` Lokesh Gidra
  0 siblings, 0 replies; 5+ messages in thread
From: Lokesh Gidra @ 2020-08-26  0:24 UTC (permalink / raw)
  To: Sebastian Andrzej Siewior
  Cc: Kees Cook, Jonathan Corbet, Peter Xu, Andrea Arcangeli,
	Andrew Morton, Alexander Viro, Stephen Smalley, Eric Biggers,
	Daniel Colascione, Joel Fernandes (Google),
	Linux FS Devel, linux-kernel, linux-doc, Kalesh Singh,
	Calin Juravle, Suren Baghdasaryan, Nick Kralevich,
	Jeffrey Vander Stoep, kernel-team, Mike Rapoport, Shaohua Li,
	Jerome Glisse, Mauro Carvalho Chehab, Johannes Weiner,
	Mel Gorman, Nitin Gupta, Vlastimil Babka, Iurii Zaikin,
	Luis Chamberlain, Daniel Colascione

On Mon, Aug 24, 2020 at 5:32 AM Sebastian Andrzej Siewior
<bigeasy@linutronix.de> wrote:
>
> On 2020-08-21 18:40:17 [-0700], Lokesh Gidra wrote:
> > --- a/fs/userfaultfd.c
> > +++ b/fs/userfaultfd.c
> > @@ -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;
> Why?

Not sure! I guess Daniel didn't want to repeat the long flag name twice.

Thanks for catching that. I'll send another version fixing this.
>
> Sebastian

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-08-26  0:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-22  1:40 [PATCH v2 0/2] Control over userfaultfd kernel-fault handling Lokesh Gidra
2020-08-22  1:40 ` [PATCH v2 1/2] Add UFFD_USER_MODE_ONLY Lokesh Gidra
2020-08-24 12:32   ` Sebastian Andrzej Siewior
2020-08-26  0:24     ` Lokesh Gidra
2020-08-22  1:40 ` [PATCH v2 2/2] Add user-mode only option to unprivileged_userfaultfd sysctl knob Lokesh Gidra

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).