Skip to contentSkip to conversation

android: binder: Fix a possible data race in binder_alloc_mmap_handler

PATCH In review Jia-Ju Bai 2 participants 2 messages May 8, 2018 Android drivers

lore
Share & tools
lore

Jia-Ju Bai

author
#0 lore raw

PATCH 1/1 android: binder: Fix a possible data race in binder_alloc_mmap_handler

The write operations to "alloc->buffer" are protected by the lock on line 679 and 730, but the read operation to this data on line 712 is not protected by the lock. Thus, there may exist a data race for "alloc->buffer".

To fix this data race, the read operation to "alloc->buffer" should be also protected by the lock.

---
 drivers/android/binder_alloc.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/android/binder_alloc.c +2 -0
index 5a426c877dfb..596acc3a84e4 100644
@@ -709,7 +709,9 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
 		goto err_alloc_buf_struct_failed;
 	}
 
+	mutex_lock(&binder_alloc_mmap_lock);
 	buffer->data = alloc->buffer;
+	mutex_unlock(&binder_alloc_mmap_lock);
 	list_add(&buffer->entry, &alloc->buffers);
 	buffer->free = 1;
 	binder_insert_free_buffer(alloc, buffer);
Signature · 1 line
2.17.0
#1 lore raw
Quoted 4 lines · Jia-Ju Bai

The write operations to "alloc->buffer" are protected by the lock on line 679 and 730, but the read operation to this data on line 712 is not protected by the lock. Thus, there may exist a data race for "alloc->buffer".

It's read by the same thread that just wrote it, there is no data race.

The locks at line 679 and 730 protect against multiple threads calling mmap() at the same time.

Quoted 24 lines

To fix this data race, the read operation to "alloc->buffer" should be also protected by the lock.

---
 drivers/android/binder_alloc.c | 2 ++
 1 file changed, 2 insertions(+)
drivers/android/binder_alloc.c +2 -1
index 5a426c877dfb..596acc3a84e4 100644
@@ -709,7 +709,9 @@ int binder_alloc_mmap_handler(struct binder_alloc *alloc,
                goto err_alloc_buf_struct_failed;
        }
 
+       mutex_lock(&binder_alloc_mmap_lock);
        buffer->data = alloc->buffer;
+       mutex_unlock(&binder_alloc_mmap_lock);
        list_add(&buffer->entry, &alloc->buffers);
        buffer->free = 1;
        binder_insert_free_buffer(alloc, buffer);
--
Quoted 2 lines

2.17.0

_______________________________________________
devel mailing list
[email protected]
http://driverdev.linuxdriverproject.org/mailman/listinfo/driverdev-devel