linux-mm.kvack.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2] binder_alloc: Add missing mmap_lock calls when using the VMA
@ 2022-08-10 16:02 Liam Howlett
  2022-08-10 16:27 ` Carlos Llamas
  2022-08-10 18:14 ` Ondrej Mosnacek
  0 siblings, 2 replies; 3+ messages in thread
From: Liam Howlett @ 2022-08-10 16:02 UTC (permalink / raw)
  To: linux-mm, linux-kernel, syzbot, Andrew Morton, Ondrej Mosnacek
  Cc: syzkaller-bugs, Minchan Kim, Christian Brauner,
	Greg Kroah-Hartman, Hridya Valsaraju, Joel Fernandes,
	Martijn Coenen, Suren Baghdasaryan, Todd Kjos, Matthew Wilcox,
	Arve Hjønnevåg, Carlos Llamas

Take the mmap_read_lock() when using the VMA in
binder_alloc_print_pages() and when checking for a VMA in
binder_alloc_new_buf_locked().

It is worth noting binder_alloc_new_buf_locked() drops the VMA read lock
after it verifies a VMA exists, but may be taken again deeper in the
call stack, if necessary.

Reported-by: Ondrej Mosnacek <omosnace@redhat.com>
Reported-by: syzbot+a7b60a176ec13cafb793@syzkaller.appspotmail.com
Fixes: a43cfc87caaf (android: binder: stop saving a pointer to the VMA)
Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
---
 drivers/android/binder_alloc.c | 31 +++++++++++++++++++++----------
 1 file changed, 21 insertions(+), 10 deletions(-)

diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c
index f555eebceef6..c5e7c6d3a844 100644
--- a/drivers/android/binder_alloc.c
+++ b/drivers/android/binder_alloc.c
@@ -395,12 +395,15 @@ static struct binder_buffer *binder_alloc_new_buf_locked(
 	size_t size, data_offsets_size;
 	int ret;
 
+	mmap_read_lock(alloc->vma_vm_mm);
 	if (!binder_alloc_get_vma(alloc)) {
+		mmap_read_unlock(alloc->vma_vm_mm);
 		binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
 				   "%d: binder_alloc_buf, no vma\n",
 				   alloc->pid);
 		return ERR_PTR(-ESRCH);
 	}
+	mmap_read_unlock(alloc->vma_vm_mm);
 
 	data_offsets_size = ALIGN(data_size, sizeof(void *)) +
 		ALIGN(offsets_size, sizeof(void *));
@@ -922,17 +925,25 @@ void binder_alloc_print_pages(struct seq_file *m,
 	 * Make sure the binder_alloc is fully initialized, otherwise we might
 	 * read inconsistent state.
 	 */
-	if (binder_alloc_get_vma(alloc) != NULL) {
-		for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
-			page = &alloc->pages[i];
-			if (!page->page_ptr)
-				free++;
-			else if (list_empty(&page->lru))
-				active++;
-			else
-				lru++;
-		}
+
+	mmap_read_lock(alloc->vma_vm_mm);
+	if (binder_alloc_get_vma(alloc) == NULL) {
+		mmap_read_unlock(alloc->vma_vm_mm);
+		goto uninitialized;
 	}
+
+	mmap_read_unlock(alloc->vma_vm_mm);
+	for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
+		page = &alloc->pages[i];
+		if (!page->page_ptr)
+			free++;
+		else if (list_empty(&page->lru))
+			active++;
+		else
+			lru++;
+	}
+
+uninitialized:
 	mutex_unlock(&alloc->mutex);
 	seq_printf(m, "  pages: %d:%d:%d\n", active, lru, free);
 	seq_printf(m, "  pages high watermark: %zu\n", alloc->pages_high);
-- 
2.35.1


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

* Re: [PATCH v2] binder_alloc: Add missing mmap_lock calls when using the VMA
  2022-08-10 16:02 [PATCH v2] binder_alloc: Add missing mmap_lock calls when using the VMA Liam Howlett
@ 2022-08-10 16:27 ` Carlos Llamas
  2022-08-10 18:14 ` Ondrej Mosnacek
  1 sibling, 0 replies; 3+ messages in thread
From: Carlos Llamas @ 2022-08-10 16:27 UTC (permalink / raw)
  To: Liam Howlett
  Cc: linux-mm, linux-kernel, syzbot, Andrew Morton, Ondrej Mosnacek,
	syzkaller-bugs, Minchan Kim, Christian Brauner,
	Greg Kroah-Hartman, Hridya Valsaraju, Joel Fernandes,
	Martijn Coenen, Suren Baghdasaryan, Todd Kjos, Matthew Wilcox,
	Arve Hjønnevåg

On Wed, Aug 10, 2022 at 04:02:25PM +0000, Liam Howlett wrote:
> Take the mmap_read_lock() when using the VMA in
> binder_alloc_print_pages() and when checking for a VMA in
> binder_alloc_new_buf_locked().
> 
> It is worth noting binder_alloc_new_buf_locked() drops the VMA read lock
> after it verifies a VMA exists, but may be taken again deeper in the
> call stack, if necessary.
> 
> Reported-by: Ondrej Mosnacek <omosnace@redhat.com>
> Reported-by: syzbot+a7b60a176ec13cafb793@syzkaller.appspotmail.com
> Fixes: a43cfc87caaf (android: binder: stop saving a pointer to the VMA)
> Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
> ---
>  drivers/android/binder_alloc.c | 31 +++++++++++++++++++++----------
>  1 file changed, 21 insertions(+), 10 deletions(-)
> 
> diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c
> index f555eebceef6..c5e7c6d3a844 100644
> --- a/drivers/android/binder_alloc.c
> +++ b/drivers/android/binder_alloc.c
> @@ -395,12 +395,15 @@ static struct binder_buffer *binder_alloc_new_buf_locked(
>  	size_t size, data_offsets_size;
>  	int ret;
>  
> +	mmap_read_lock(alloc->vma_vm_mm);
>  	if (!binder_alloc_get_vma(alloc)) {
> +		mmap_read_unlock(alloc->vma_vm_mm);
>  		binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
>  				   "%d: binder_alloc_buf, no vma\n",
>  				   alloc->pid);
>  		return ERR_PTR(-ESRCH);
>  	}
> +	mmap_read_unlock(alloc->vma_vm_mm);
>  
>  	data_offsets_size = ALIGN(data_size, sizeof(void *)) +
>  		ALIGN(offsets_size, sizeof(void *));
> @@ -922,17 +925,25 @@ void binder_alloc_print_pages(struct seq_file *m,
>  	 * Make sure the binder_alloc is fully initialized, otherwise we might
>  	 * read inconsistent state.
>  	 */
> -	if (binder_alloc_get_vma(alloc) != NULL) {
> -		for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
> -			page = &alloc->pages[i];
> -			if (!page->page_ptr)
> -				free++;
> -			else if (list_empty(&page->lru))
> -				active++;
> -			else
> -				lru++;
> -		}
> +
> +	mmap_read_lock(alloc->vma_vm_mm);
> +	if (binder_alloc_get_vma(alloc) == NULL) {
> +		mmap_read_unlock(alloc->vma_vm_mm);
> +		goto uninitialized;
>  	}
> +
> +	mmap_read_unlock(alloc->vma_vm_mm);
> +	for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
> +		page = &alloc->pages[i];
> +		if (!page->page_ptr)
> +			free++;
> +		else if (list_empty(&page->lru))
> +			active++;
> +		else
> +			lru++;
> +	}
> +
> +uninitialized:
>  	mutex_unlock(&alloc->mutex);
>  	seq_printf(m, "  pages: %d:%d:%d\n", active, lru, free);
>  	seq_printf(m, "  pages high watermark: %zu\n", alloc->pages_high);
> -- 
> 2.35.1

Thanks Liam, this looks good.

Acked-by: Carlos Llamas <cmllamas@google.com>


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

* Re: [PATCH v2] binder_alloc: Add missing mmap_lock calls when using the VMA
  2022-08-10 16:02 [PATCH v2] binder_alloc: Add missing mmap_lock calls when using the VMA Liam Howlett
  2022-08-10 16:27 ` Carlos Llamas
@ 2022-08-10 18:14 ` Ondrej Mosnacek
  1 sibling, 0 replies; 3+ messages in thread
From: Ondrej Mosnacek @ 2022-08-10 18:14 UTC (permalink / raw)
  To: Liam Howlett
  Cc: linux-mm, linux-kernel, syzbot, Andrew Morton, syzkaller-bugs,
	Minchan Kim, Christian Brauner, Greg Kroah-Hartman,
	Hridya Valsaraju, Joel Fernandes, Martijn Coenen,
	Suren Baghdasaryan, Todd Kjos, Matthew Wilcox,
	Arve Hjønnevåg, Carlos Llamas

On Wed, Aug 10, 2022 at 6:02 PM Liam Howlett <liam.howlett@oracle.com> wrote:
> Take the mmap_read_lock() when using the VMA in
> binder_alloc_print_pages() and when checking for a VMA in
> binder_alloc_new_buf_locked().
>
> It is worth noting binder_alloc_new_buf_locked() drops the VMA read lock
> after it verifies a VMA exists, but may be taken again deeper in the
> call stack, if necessary.
>
> Reported-by: Ondrej Mosnacek <omosnace@redhat.com>
> Reported-by: syzbot+a7b60a176ec13cafb793@syzkaller.appspotmail.com
> Fixes: a43cfc87caaf (android: binder: stop saving a pointer to the VMA)
> Signed-off-by: Liam R. Howlett <Liam.Howlett@oracle.com>
> ---
>  drivers/android/binder_alloc.c | 31 +++++++++++++++++++++----------
>  1 file changed, 21 insertions(+), 10 deletions(-)
>
> diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c
> index f555eebceef6..c5e7c6d3a844 100644
> --- a/drivers/android/binder_alloc.c
> +++ b/drivers/android/binder_alloc.c
> @@ -395,12 +395,15 @@ static struct binder_buffer *binder_alloc_new_buf_locked(
>         size_t size, data_offsets_size;
>         int ret;
>
> +       mmap_read_lock(alloc->vma_vm_mm);
>         if (!binder_alloc_get_vma(alloc)) {
> +               mmap_read_unlock(alloc->vma_vm_mm);
>                 binder_alloc_debug(BINDER_DEBUG_USER_ERROR,
>                                    "%d: binder_alloc_buf, no vma\n",
>                                    alloc->pid);
>                 return ERR_PTR(-ESRCH);
>         }
> +       mmap_read_unlock(alloc->vma_vm_mm);
>
>         data_offsets_size = ALIGN(data_size, sizeof(void *)) +
>                 ALIGN(offsets_size, sizeof(void *));
> @@ -922,17 +925,25 @@ void binder_alloc_print_pages(struct seq_file *m,
>          * Make sure the binder_alloc is fully initialized, otherwise we might
>          * read inconsistent state.
>          */
> -       if (binder_alloc_get_vma(alloc) != NULL) {
> -               for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
> -                       page = &alloc->pages[i];
> -                       if (!page->page_ptr)
> -                               free++;
> -                       else if (list_empty(&page->lru))
> -                               active++;
> -                       else
> -                               lru++;
> -               }
> +
> +       mmap_read_lock(alloc->vma_vm_mm);
> +       if (binder_alloc_get_vma(alloc) == NULL) {
> +               mmap_read_unlock(alloc->vma_vm_mm);
> +               goto uninitialized;
>         }
> +
> +       mmap_read_unlock(alloc->vma_vm_mm);
> +       for (i = 0; i < alloc->buffer_size / PAGE_SIZE; i++) {
> +               page = &alloc->pages[i];
> +               if (!page->page_ptr)
> +                       free++;
> +               else if (list_empty(&page->lru))
> +                       active++;
> +               else
> +                       lru++;
> +       }
> +
> +uninitialized:
>         mutex_unlock(&alloc->mutex);
>         seq_printf(m, "  pages: %d:%d:%d\n", active, lru, free);
>         seq_printf(m, "  pages high watermark: %zu\n", alloc->pages_high);
> --
> 2.35.1

Can confirm that with this patch the selinux-testsuite's binder test
passes again with no warnings in dmesg.

Tested-by: Ondrej Mosnacek <omosnace@redhat.com>

Thanks!

-- 
Ondrej Mosnacek
Senior Software Engineer, Linux Security - SELinux kernel
Red Hat, Inc.



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

end of thread, other threads:[~2022-08-10 18:14 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-08-10 16:02 [PATCH v2] binder_alloc: Add missing mmap_lock calls when using the VMA Liam Howlett
2022-08-10 16:27 ` Carlos Llamas
2022-08-10 18:14 ` Ondrej Mosnacek

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox