All of lore.kernel.org
 help / color / mirror / Atom feed
From: Kassey Li <quic_yingangl@quicinc.com>
To: <gregkh@google.com>, <gregkh@linuxfoundation.org>,
	<cmllamas@google.com>, <surenb@google.com>, <arve@android.com>,
	<joel@joelfernandes.org>, <brauner@kernel.org>
Cc: <tkjos@android.com>, <maco@android.com>,
	<quic_yingangl@quicinc.com>, <linux-kernel@vger.kernel.org>,
	Sherry Yang <sherryy@android.com>
Subject: [PATCH v3] binder: add mutex_lock for mmap and NULL when free
Date: Sat, 7 Oct 2023 20:28:13 +0800	[thread overview]
Message-ID: <20231007122813.84282-1-quic_yingangl@quicinc.com> (raw)

- Enforce alloc->mutex in binder_alloc_mmap_handler when add the entry to
  list.

- Assign the freed pages/page_ptr to NULL to catch possible use after free
  with NULL pointer access.

Fixes: 19c987241ca1 ("binder: separate out binder_alloc functions")
Fixes: f2517eb76f1f ("android: binder: Add global lru shrinker to binder")
CC: Todd Kjos <tkjos@android.com>
CC: Sherry Yang <sherryy@android.com>
Link: https://lore.kernel.org/all/20231007034046.2352124-1-quic_yingangl@quicinc.com/
Signed-off-by: Kassey Li <quic_yingangl@quicinc.com>
---
V1 -> V2: Add Fixes info.
V2 -> V3: Add this history.
---
 drivers/android/binder_alloc.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)

diff --git a/drivers/android/binder_alloc.c b/drivers/android/binder_alloc.c
index e3db8297095a..c7d126e04343 100644
--- a/drivers/android/binder_alloc.c
+++ b/drivers/android/binder_alloc.c
@@ -775,6 +775,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
 	}
 
 	buffer->user_data = alloc->buffer;
+	mutex_lock(&alloc->mutex);
 	list_add(&buffer->entry, &alloc->buffers);
 	buffer->free = 1;
 	binder_insert_free_buffer(alloc, buffer);
@@ -782,7 +783,7 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
 
 	/* Signal binder_alloc is fully initialized */
 	binder_alloc_set_vma(alloc, vma);
-
+	mutex_unlock(&alloc->mutex);
 	return 0;
 
 err_alloc_buf_struct_failed:
@@ -856,9 +857,11 @@ void binder_alloc_deferred_release(struct binder_alloc *alloc)
 				     __func__, alloc->pid, i, page_addr,
 				     on_lru ? "on lru" : "active");
 			__free_page(alloc->pages[i].page_ptr);
+			alloc->pages[i].page_ptr = NULL;
 			page_count++;
 		}
 		kfree(alloc->pages);
+		alloc->pages = NULL;
 	}
 	mutex_unlock(&alloc->mutex);
 	if (alloc->mm)
-- 
2.25.1


             reply	other threads:[~2023-10-07 12:28 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-10-07 12:28 Kassey Li [this message]
2023-10-07 23:16 ` [PATCH v3] binder: add mutex_lock for mmap and NULL when free Carlos Llamas
2023-10-09 12:21   ` Kassey Li

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231007122813.84282-1-quic_yingangl@quicinc.com \
    --to=quic_yingangl@quicinc.com \
    --cc=arve@android.com \
    --cc=brauner@kernel.org \
    --cc=cmllamas@google.com \
    --cc=gregkh@google.com \
    --cc=gregkh@linuxfoundation.org \
    --cc=joel@joelfernandes.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=maco@android.com \
    --cc=sherryy@android.com \
    --cc=surenb@google.com \
    --cc=tkjos@android.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.