Skip to contentSkip to conversation

Userfaultfd: Avoid double free of userfault_ctx and remove O_CLOEXEC

PATCH In review Lokesh Gidra 3 participants 9 messages Aug 4, 2020 – Aug 6, 2020 VFS

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Userfaultfd: Avoid double free of userfault_ctx and remove O_CLOEXEC

when get_unused_fd_flags returns error, ctx will be freed by userfaultfd's release function, which is indirectly called by fput(). Also, if anon_inode_getfile_secure() returns an error, then userfaultfd_ctx_put() is called, which calls mmdrop() and frees ctx.

Also, the O_CLOEXEC was inadvertently added to the call to get_unused_fd_flags() [1].

Adding Al Viro's suggested-by, based on [2].

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

Fixes d08ac70b1e0d (Wire UFFD up to SELinux)
Reported-by [email protected]
---
 fs/userfaultfd.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
fs/userfaultfd.c +4 -10
index ae859161908f..e15eb8fdc083 100644
@@ -2042,24 +2042,18 @@ SYSCALL_DEFINE1(userfaultfd, int, flags)
 		O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS),
 		NULL);
 	if (IS_ERR(file)) {
-		fd = PTR_ERR(file);
-		goto out;
+		userfaultfd_ctx_put(ctx);
+		return PTR_ERR(file);
 	}
 
-	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
+	fd = get_unused_fd_flags(O_RDONLY);
 	if (fd < 0) {
 		fput(file);
-		goto out;
+		return fd;
 	}
 
 	ctx->owner = file_inode(file);
 	fd_install(fd, file);
-
-out:
-	if (fd < 0) {
-		mmdrop(ctx->mm);
-		kmem_cache_free(userfaultfd_ctx_cachep, ctx);
-	}
 	return fd;
 }
 
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
#1 lore raw
Quoted 10 lines · Lokesh Gidra

when get_unused_fd_flags returns error, ctx will be freed by userfaultfd's release function, which is indirectly called by fput(). Also, if anon_inode_getfile_secure() returns an error, then userfaultfd_ctx_put() is called, which calls mmdrop() and frees ctx.

Also, the O_CLOEXEC was inadvertently added to the call to get_unused_fd_flags() [1].

Adding Al Viro's suggested-by, based on [2].

Quoted 7 lines

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

Fixes d08ac70b1e0d (Wire UFFD up to SELinux)
Reported-by [email protected]

What branch does this patch apply to? Neither mainline nor linux-next works.

- Eric

#2 lore raw
Quoted 15 lines · Eric Biggers
Quoted 13 lines · Lokesh Gidra

when get_unused_fd_flags returns error, ctx will be freed by userfaultfd's release function, which is indirectly called by fput(). Also, if anon_inode_getfile_secure() returns an error, then userfaultfd_ctx_put() is called, which calls mmdrop() and frees ctx.

Also, the O_CLOEXEC was inadvertently added to the call to get_unused_fd_flags() [1].

Adding Al Viro's suggested-by, based on [2].

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

Quoted 7 lines
Quoted 4 lines
Fixes d08ac70b1e0d (Wire UFFD up to SELinux)
Reported-by [email protected]

What branch does this patch apply to? Neither mainline nor linux-next works.

On James Morris' tree (secure_uffd_v5.9 branch).

Quoted 1 line

- Eric

#3 lore raw
Quoted 16 lines · Lokesh Gidra
Quoted 15 lines · Eric Biggers
Quoted 13 lines · Lokesh Gidra

when get_unused_fd_flags returns error, ctx will be freed by userfaultfd's release function, which is indirectly called by fput(). Also, if anon_inode_getfile_secure() returns an error, then userfaultfd_ctx_put() is called, which calls mmdrop() and frees ctx.

Also, the O_CLOEXEC was inadvertently added to the call to get_unused_fd_flags() [1].

Adding Al Viro's suggested-by, based on [2].

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

Quoted 9 lines
Quoted 7 lines
Quoted 4 lines
Fixes d08ac70b1e0d (Wire UFFD up to SELinux)
Reported-by [email protected]

What branch does this patch apply to? Neither mainline nor linux-next works.

On James Morris' tree (secure_uffd_v5.9 branch).

For those of us not "in the know", that apparently means branch secure_uffd_v5.9 of https://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security.git

Perhaps it would make more sense to resend your original patch series with this fix folded in?

Quoted 21 lines
fs/userfaultfd.c +4 -4
index ae859161908f..e15eb8fdc083 100644
@@ -2042,24 +2042,18 @@ SYSCALL_DEFINE1(userfaultfd, int, flags)
 		O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS),
 		NULL);
 	if (IS_ERR(file)) {
-		fd = PTR_ERR(file);
-		goto out;
+		userfaultfd_ctx_put(ctx);
+		return PTR_ERR(file);
 	}
 
-	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
+	fd = get_unused_fd_flags(O_RDONLY);
 	if (fd < 0) {
 		fput(file);
-		goto out;
+		return fd;
 	}

fs/userfaultfd.c SYSCALL_DEFINE1(userfaultfd, int, flags) · line 2057

Quoted 10 lines
fs/userfaultfd.c +0 -6
 
 	ctx->owner = file_inode(file);
 	fd_install(fd, file);
-
-out:
-	if (fd < 0) {
-		mmdrop(ctx->mm);
-		kmem_cache_free(userfaultfd_ctx_cachep, ctx);
-	}
 	return fd;

This introduces the opposite bug: now it's hardcoded to *not* use O_CLOEXEC, instead of using the flag the user passed in the flags argument to the syscall.

- Eric

#4 lore raw
Quoted 25 lines · Eric Biggers
Quoted 23 lines · Lokesh Gidra
Quoted 22 lines · Eric Biggers
Quoted 10 lines · Lokesh Gidra

when get_unused_fd_flags returns error, ctx will be freed by userfaultfd's release function, which is indirectly called by fput(). Also, if anon_inode_getfile_secure() returns an error, then userfaultfd_ctx_put() is called, which calls mmdrop() and frees ctx.

Also, the O_CLOEXEC was inadvertently added to the call to get_unused_fd_flags() [1].

Adding Al Viro's suggested-by, based on [2].

Quoted 7 lines

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

Fixes d08ac70b1e0d (Wire UFFD up to SELinux)
Reported-by [email protected]

What branch does this patch apply to? Neither mainline nor linux-next works.

Quoted 9 lines
Quoted 2 lines

On James Morris' tree (secure_uffd_v5.9 branch).

For those of us not "in the know", that apparently means branch secure_uffd_v5.9 of https://git.kernel.org/pub/scm/linux/kernel/git/jmorris/linux-security.git

Perhaps it would make more sense to resend your original patch series with this fix folded in?

OK. I'll resend the whole patch series with the fixes soon.

Quoted 22 lines
Quoted 22 lines
fs/userfaultfd.c +4 -4
index ae859161908f..e15eb8fdc083 100644
@@ -2042,24 +2042,18 @@ SYSCALL_DEFINE1(userfaultfd, int, flags)
              O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS),
              NULL);
      if (IS_ERR(file)) {
-             fd = PTR_ERR(file);
-             goto out;
+             userfaultfd_ctx_put(ctx);
+             return PTR_ERR(file);
      }
 
-     fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
+     fd = get_unused_fd_flags(O_RDONLY);
      if (fd < 0) {
              fput(file);
-             goto out;
+             return fd;
      }
 
Quoted 12 lines

fs/userfaultfd.c SYSCALL_DEFINE1(userfaultfd, int, flags) · line 2057

Quoted 9 lines
fs/userfaultfd.c +0 -6
      ctx->owner = file_inode(file);
      fd_install(fd, file);
-
-out:
-     if (fd < 0) {
-             mmdrop(ctx->mm);
-             kmem_cache_free(userfaultfd_ctx_cachep, ctx);
-     }
      return fd;

This introduces the opposite bug: now it's hardcoded to *not* use O_CLOEXEC, instead of using the flag the user passed in the flags argument to the syscall.

I get your point. I agree the flags passed in to the syscall should be used.

Quoted 2 lines

- Eric

#5 lore raw
In reply to Lokesh Gidra
Quoted 7 lines · Lokesh Gidra

when get_unused_fd_flags returns error, ctx will be freed by userfaultfd's release function, which is indirectly called by fput(). Also, if anon_inode_getfile_secure() returns an error, then userfaultfd_ctx_put() is called, which calls mmdrop() and frees ctx.

Also, the O_CLOEXEC was inadvertently added to the call to get_unused_fd_flags() [1].

I disagree that it is "wrong" to do O_CLOEXEC-by-default (after all, it's trivial to disable O_CLOEXEC, but it's non-trivial to enable it on an existing file descriptor because it's possible for another thread to exec() before you set the flag). Several new syscalls and fd-returning facilities are O_CLOEXEC-by-default now (the most obvious being pidfds and seccomp notifier fds).

At the very least there should be a new flag added that sets O_CLOEXEC.

Quoted 49 lines

Adding Al Viro's suggested-by, based on [2].

[1] https://lore.kernel.org/lkml/[email protected]/
[2] https://lore.kernel.org/lkml/[email protected]/

Fixes d08ac70b1e0d (Wire UFFD up to SELinux)
Reported-by [email protected]
---
 fs/userfaultfd.c | 14 ++++----------
 1 file changed, 4 insertions(+), 10 deletions(-)
fs/userfaultfd.c +4 -10
index ae859161908f..e15eb8fdc083 100644
@@ -2042,24 +2042,18 @@ SYSCALL_DEFINE1(userfaultfd, int, flags)
 		O_RDWR | (flags & UFFD_SHARED_FCNTL_FLAGS),
 		NULL);
 	if (IS_ERR(file)) {
-		fd = PTR_ERR(file);
-		goto out;
+		userfaultfd_ctx_put(ctx);
+		return PTR_ERR(file);
 	}
 
-	fd = get_unused_fd_flags(O_RDONLY | O_CLOEXEC);
+	fd = get_unused_fd_flags(O_RDONLY);
 	if (fd < 0) {
 		fput(file);
-		goto out;
+		return fd;
 	}
 
 	ctx->owner = file_inode(file);
 	fd_install(fd, file);
-
-out:
-	if (fd < 0) {
-		mmdrop(ctx->mm);
-		kmem_cache_free(userfaultfd_ctx_cachep, ctx);
-	}
 	return fd;
 }
 
Signature · 1 line
2.28.0.163.g6104cc2f0b6-goog
Signature · 4 lines
Aleksa Sarai
Senior Software Engineer (Containers)
SUSE Linux GmbH
<https://www.cyphar.com/>
#6 lore raw
Quoted 9 lines · Aleksa Sarai
Quoted 7 lines · Lokesh Gidra

when get_unused_fd_flags returns error, ctx will be freed by userfaultfd's release function, which is indirectly called by fput(). Also, if anon_inode_getfile_secure() returns an error, then userfaultfd_ctx_put() is called, which calls mmdrop() and frees ctx.

Also, the O_CLOEXEC was inadvertently added to the call to get_unused_fd_flags() [1].

Quoted 6 lines

I disagree that it is "wrong" to do O_CLOEXEC-by-default (after all, it's trivial to disable O_CLOEXEC, but it's non-trivial to enable it on an existing file descriptor because it's possible for another thread to exec() before you set the flag). Several new syscalls and fd-returning facilities are O_CLOEXEC-by-default now (the most obvious being pidfds and seccomp notifier fds).

Sure, O_CLOEXEC *should* be the default, but this is an existing syscall so it has to keep the existing behavior.

Quoted 1 line

At the very least there should be a new flag added that sets O_CLOEXEC.

There already is one (but these patches broke it).

- Eric

#7 lore raw
Quoted 18 lines · Eric Biggers
Quoted 9 lines · Aleksa Sarai
Quoted 7 lines · Lokesh Gidra

when get_unused_fd_flags returns error, ctx will be freed by userfaultfd's release function, which is indirectly called by fput(). Also, if anon_inode_getfile_secure() returns an error, then userfaultfd_ctx_put() is called, which calls mmdrop() and frees ctx.

Also, the O_CLOEXEC was inadvertently added to the call to get_unused_fd_flags() [1].

Quoted 6 lines

I disagree that it is "wrong" to do O_CLOEXEC-by-default (after all, it's trivial to disable O_CLOEXEC, but it's non-trivial to enable it on an existing file descriptor because it's possible for another thread to exec() before you set the flag). Several new syscalls and fd-returning facilities are O_CLOEXEC-by-default now (the most obvious being pidfds and seccomp notifier fds).

Quoted 7 lines

Sure, O_CLOEXEC *should* be the default, but this is an existing syscall so it has to keep the existing behavior.

Quoted 1 line

At the very least there should be a new flag added that sets O_CLOEXEC.

There already is one (but these patches broke it).

I looked at the existing implementation, and the right thing is to pass on the 'flags' (that is passed in to the syscall) to fetch 'fd'.

Besides, as you said in the other email thread, anon_inode_getfile_secure() should be replaced with anon_inode_getfd_secure(), which will remove this ambiguity.

I'll resend the patch series soon with all the changes that you proposed.

Quoted 1 line

- Eric

#8 lore raw
In reply to Eric Biggers
Quoted 17 lines · Eric Biggers
Quoted 9 lines · Aleksa Sarai
Quoted 7 lines · Lokesh Gidra

when get_unused_fd_flags returns error, ctx will be freed by userfaultfd's release function, which is indirectly called by fput(). Also, if anon_inode_getfile_secure() returns an error, then userfaultfd_ctx_put() is called, which calls mmdrop() and frees ctx.

Also, the O_CLOEXEC was inadvertently added to the call to get_unused_fd_flags() [1].

Quoted 6 lines

I disagree that it is "wrong" to do O_CLOEXEC-by-default (after all, it's trivial to disable O_CLOEXEC, but it's non-trivial to enable it on an existing file descriptor because it's possible for another thread to exec() before you set the flag). Several new syscalls and fd-returning facilities are O_CLOEXEC-by-default now (the most obvious being pidfds and seccomp notifier fds).

Quoted 2 lines

Sure, O_CLOEXEC *should* be the default, but this is an existing syscall so it has to keep the existing behavior.

Ah, I missed that this was a UAPI breakage. :P

Quoted 3 lines
Quoted 1 line

At the very least there should be a new flag added that sets O_CLOEXEC.

There already is one (but these patches broke it).

Signature · 4 lines
Aleksa Sarai
Senior Software Engineer (Containers)
SUSE Linux GmbH
<https://www.cyphar.com/>