This is a note to let you know that I've just added the patch titled binder: add lockless binder_alloc_(set|get)_vma() to the 5.15-stable tree which can be found at: http://www.kernel.org/git/?p=linux/kernel/git/stable/stable-queue.git;a=summary The filename of the patch is: binder-add-lockless-binder_alloc_-set-get-_vma.patch and it can be found in the queue-5.15 subdirectory. If you, or anyone else, feels it should not be added to the stable tree, please let <stable@xxxxxxxxxxxxxxx> know about it. >From stable-owner@xxxxxxxxxxxxxxx Tue May 30 20:44:04 2023 From: Carlos Llamas <cmllamas@xxxxxxxxxx> Date: Tue, 30 May 2023 19:43:37 +0000 Subject: binder: add lockless binder_alloc_(set|get)_vma() To: stable@xxxxxxxxxxxxxxx Cc: Carlos Llamas <cmllamas@xxxxxxxxxx>, Liam Howlett <liam.howlett@xxxxxxxxxx>, Suren Baghdasaryan <surenb@xxxxxxxxxx>, Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> Message-ID: <20230530194338.1683009-4-cmllamas@xxxxxxxxxx> From: Carlos Llamas <cmllamas@xxxxxxxxxx> commit 0fa53349c3acba0239369ba4cd133740a408d246 upstream. Bring back the original lockless design in binder_alloc to determine whether the buffer setup has been completed by the ->mmap() handler. However, this time use smp_load_acquire() and smp_store_release() to wrap all the ordering in a single macro call. Also, add comments to make it evident that binder uses alloc->vma to determine when the binder_alloc has been fully initialized. In these scenarios acquiring the mmap_lock is not required. Fixes: a43cfc87caaf ("android: binder: stop saving a pointer to the VMA") Cc: Liam Howlett <liam.howlett@xxxxxxxxxx> Cc: Suren Baghdasaryan <surenb@xxxxxxxxxx> Cc: stable@xxxxxxxxxxxxxxx Signed-off-by: Carlos Llamas <cmllamas@xxxxxxxxxx> Link: https://lore.kernel.org/r/20230502201220.1756319-3-cmllamas@xxxxxxxxxx Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> [cmllamas: fixed minor merge conflict in binder_alloc_set_vma()] Signed-off-by: Carlos Llamas <cmllamas@xxxxxxxxxx> Signed-off-by: Greg Kroah-Hartman <gregkh@xxxxxxxxxxxxxxxxxxx> --- drivers/android/binder_alloc.c | 22 +++++++--------------- 1 file changed, 7 insertions(+), 15 deletions(-) --- a/drivers/android/binder_alloc.c +++ b/drivers/android/binder_alloc.c @@ -309,29 +309,18 @@ err_no_vma: return vma ? -ENOMEM : -ESRCH; } - static inline void binder_alloc_set_vma(struct binder_alloc *alloc, struct vm_area_struct *vma) { - /* - * If we see alloc->vma is not NULL, buffer data structures set up - * completely. Look at smp_rmb side binder_alloc_get_vma. - */ - smp_wmb(); - alloc->vma = vma; + /* pairs with smp_load_acquire in binder_alloc_get_vma() */ + smp_store_release(&alloc->vma, vma); } static inline struct vm_area_struct *binder_alloc_get_vma( struct binder_alloc *alloc) { - struct vm_area_struct *vma = NULL; - - if (alloc->vma) { - /* Look at description in binder_alloc_set_vma */ - smp_rmb(); - vma = alloc->vma; - } - return vma; + /* pairs with smp_store_release in binder_alloc_set_vma() */ + return smp_load_acquire(&alloc->vma); } static bool debug_low_async_space_locked(struct binder_alloc *alloc, int pid) @@ -394,6 +383,7 @@ static struct binder_buffer *binder_allo size_t size, data_offsets_size; int ret; + /* Check binder_alloc is fully initialized */ if (!binder_alloc_get_vma(alloc)) { binder_alloc_debug(BINDER_DEBUG_USER_ERROR, "%d: binder_alloc_buf, no vma\n", @@ -789,6 +779,8 @@ int binder_alloc_mmap_handler(struct bin buffer->free = 1; binder_insert_free_buffer(alloc, buffer); alloc->free_async_space = alloc->buffer_size / 2; + + /* Signal binder_alloc is fully initialized */ binder_alloc_set_vma(alloc, vma); return 0; Patches currently in stable-queue which might be from stable-owner@xxxxxxxxxxxxxxx are queue-5.15/revert-android-binder-stop-saving-a-pointer-to-the-vma.patch queue-5.15/binder-fix-uaf-of-alloc-vma-in-race-with-munmap.patch queue-5.15/revert-binder_alloc-add-missing-mmap_lock-calls-when-using-the-vma.patch queue-5.15/binder-add-lockless-binder_alloc_-set-get-_vma.patch