linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 6.0] binder: validate alloc->mm in ->mmap() handler
@ 2022-11-23 18:08 Carlos Llamas
  2022-11-30 12:40 ` Greg Kroah-Hartman
  0 siblings, 1 reply; 3+ messages in thread
From: Carlos Llamas @ 2022-11-23 18:08 UTC (permalink / raw)
  To: Greg Kroah-Hartman, Arve Hjønnevåg, Todd Kjos,
	Martijn Coenen, Joel Fernandes, Christian Brauner, Carlos Llamas,
	Suren Baghdasaryan, Liam R. Howlett
  Cc: linux-kernel, kernel-team, Jann Horn, stable, Todd Kjos

commit 3ce00bb7e91cf57d723905371507af57182c37ef upstream.

Since commit 1da52815d5f1 ("binder: fix alloc->vma_vm_mm null-ptr
dereference") binder caches a pointer to the current->mm during open().
This fixes a null-ptr dereference reported by syzkaller. Unfortunately,
it also opens the door for a process to update its mm after the open(),
(e.g. via execve) making the cached alloc->mm pointer invalid.

Things get worse when the process continues to mmap() a vma. From this
point forward, binder will attempt to find this vma using an obsolete
alloc->mm reference. Such as in binder_update_page_range(), where the
wrong vma is obtained via vma_lookup(), yet binder proceeds to happily
insert new pages into it.

To avoid this issue fail the ->mmap() callback if we detect a mismatch
between the vma->vm_mm and the original alloc->mm pointer. This prevents
alloc->vm_addr from getting set, so that any subsequent vma_lookup()
calls fail as expected.

Fixes: 1da52815d5f1 ("binder: fix alloc->vma_vm_mm null-ptr dereference")
Reported-by: Jann Horn <jannh@google.com>
Cc: <stable@vger.kernel.org> # 5.15+
Signed-off-by: Carlos Llamas <cmllamas@google.com>
Acked-by: Todd Kjos <tkjos@google.com>
Link: https://lore.kernel.org/r/20221104231235.348958-1-cmllamas@google.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
[cmllamas: renamed alloc->mm since missing e66b77e50522]
Signed-off-by: Carlos Llamas <cmllamas@google.com>
---
 drivers/android/binder_alloc.c | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c
index 9b1778c00610..64999777e0bf 100644
--- a/drivers/android/binder_alloc.c
+++ b/drivers/android/binder_alloc.c
@@ -760,6 +760,12 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
 	const char *failure_string;
 	struct binder_buffer *buffer;
 
+	if (unlikely(vma->vm_mm != alloc->vma_vm_mm)) {
+		ret = -EINVAL;
+		failure_string = "invalid vma->vm_mm";
+		goto err_invalid_mm;
+	}
+
 	mutex_lock(&binder_alloc_mmap_lock);
 	if (alloc->buffer_size) {
 		ret = -EBUSY;
@@ -806,6 +812,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
 	alloc->buffer_size = 0;
 err_already_mapped:
 	mutex_unlock(&binder_alloc_mmap_lock);
+err_invalid_mm:
 	binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
 			   "%s: %d %lx-%lx %s failed %d\n", __func__,
 			   alloc->pid, vma->vm_start, vma->vm_end,
-- 
2.38.1.584.g0f3c55d4c2-goog


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

* Re: [PATCH 6.0] binder: validate alloc->mm in ->mmap() handler
  2022-11-23 18:08 [PATCH 6.0] binder: validate alloc->mm in ->mmap() handler Carlos Llamas
@ 2022-11-30 12:40 ` Greg Kroah-Hartman
  2022-11-30 15:01   ` Carlos Llamas
  0 siblings, 1 reply; 3+ messages in thread
From: Greg Kroah-Hartman @ 2022-11-30 12:40 UTC (permalink / raw)
  To: Carlos Llamas
  Cc: Arve Hjønnevåg, Todd Kjos, Martijn Coenen,
	Joel Fernandes, Christian Brauner, Suren Baghdasaryan,
	Liam R. Howlett, linux-kernel, kernel-team, Jann Horn, stable,
	Todd Kjos

On Wed, Nov 23, 2022 at 06:08:09PM +0000, Carlos Llamas wrote:
> commit 3ce00bb7e91cf57d723905371507af57182c37ef upstream.
> 
> Since commit 1da52815d5f1 ("binder: fix alloc->vma_vm_mm null-ptr
> dereference") binder caches a pointer to the current->mm during open().
> This fixes a null-ptr dereference reported by syzkaller. Unfortunately,
> it also opens the door for a process to update its mm after the open(),
> (e.g. via execve) making the cached alloc->mm pointer invalid.
> 
> Things get worse when the process continues to mmap() a vma. From this
> point forward, binder will attempt to find this vma using an obsolete
> alloc->mm reference. Such as in binder_update_page_range(), where the
> wrong vma is obtained via vma_lookup(), yet binder proceeds to happily
> insert new pages into it.
> 
> To avoid this issue fail the ->mmap() callback if we detect a mismatch
> between the vma->vm_mm and the original alloc->mm pointer. This prevents
> alloc->vm_addr from getting set, so that any subsequent vma_lookup()
> calls fail as expected.
> 
> Fixes: 1da52815d5f1 ("binder: fix alloc->vma_vm_mm null-ptr dereference")
> Reported-by: Jann Horn <jannh@google.com>
> Cc: <stable@vger.kernel.org> # 5.15+
> Signed-off-by: Carlos Llamas <cmllamas@google.com>
> Acked-by: Todd Kjos <tkjos@google.com>
> Link: https://lore.kernel.org/r/20221104231235.348958-1-cmllamas@google.com
> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> [cmllamas: renamed alloc->mm since missing e66b77e50522]
> Signed-off-by: Carlos Llamas <cmllamas@google.com>
> ---
>  drivers/android/binder_alloc.c | 7 +++++++
>  1 file changed, 7 insertions(+)

This is already in the 6.0 queue, is this a different version?

thanks,

greg k-h

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

* Re: [PATCH 6.0] binder: validate alloc->mm in ->mmap() handler
  2022-11-30 12:40 ` Greg Kroah-Hartman
@ 2022-11-30 15:01   ` Carlos Llamas
  0 siblings, 0 replies; 3+ messages in thread
From: Carlos Llamas @ 2022-11-30 15:01 UTC (permalink / raw)
  To: Greg Kroah-Hartman
  Cc: Arve Hjønnevåg, Todd Kjos, Martijn Coenen,
	Joel Fernandes, Christian Brauner, Suren Baghdasaryan,
	Liam R. Howlett, linux-kernel, kernel-team, Jann Horn, stable,
	Todd Kjos

On Wed, Nov 30, 2022 at 01:40:40PM +0100, Greg Kroah-Hartman wrote:
> On Wed, Nov 23, 2022 at 06:08:09PM +0000, Carlos Llamas wrote:
> > commit 3ce00bb7e91cf57d723905371507af57182c37ef upstream.
> > 
> > Since commit 1da52815d5f1 ("binder: fix alloc->vma_vm_mm null-ptr
> > dereference") binder caches a pointer to the current->mm during open().
> > This fixes a null-ptr dereference reported by syzkaller. Unfortunately,
> > it also opens the door for a process to update its mm after the open(),
> > (e.g. via execve) making the cached alloc->mm pointer invalid.
> > 
> > Things get worse when the process continues to mmap() a vma. From this
> > point forward, binder will attempt to find this vma using an obsolete
> > alloc->mm reference. Such as in binder_update_page_range(), where the
> > wrong vma is obtained via vma_lookup(), yet binder proceeds to happily
> > insert new pages into it.
> > 
> > To avoid this issue fail the ->mmap() callback if we detect a mismatch
> > between the vma->vm_mm and the original alloc->mm pointer. This prevents
> > alloc->vm_addr from getting set, so that any subsequent vma_lookup()
> > calls fail as expected.
> > 
> > Fixes: 1da52815d5f1 ("binder: fix alloc->vma_vm_mm null-ptr dereference")
> > Reported-by: Jann Horn <jannh@google.com>
> > Cc: <stable@vger.kernel.org> # 5.15+
> > Signed-off-by: Carlos Llamas <cmllamas@google.com>
> > Acked-by: Todd Kjos <tkjos@google.com>
> > Link: https://lore.kernel.org/r/20221104231235.348958-1-cmllamas@google.com
> > Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
> > [cmllamas: renamed alloc->mm since missing e66b77e50522]
> > Signed-off-by: Carlos Llamas <cmllamas@google.com>
> > ---
> >  drivers/android/binder_alloc.c | 7 +++++++
> >  1 file changed, 7 insertions(+)
> 
> This is already in the 6.0 queue, is this a different version?
> 
> thanks,
> 
> greg k-h

Oh, please ignore this. It seems Sasha backported the change correctly.

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

end of thread, other threads:[~2022-11-30 15:01 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-11-23 18:08 [PATCH 6.0] binder: validate alloc->mm in ->mmap() handler Carlos Llamas
2022-11-30 12:40 ` Greg Kroah-Hartman
2022-11-30 15:01   ` Carlos Llamas

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).