Commit 7bada55a authored by Todd Kjos's avatar Todd Kjos Committed by Greg Kroah-Hartman

binder: fix race that allows malicious free of live buffer

Malicious code can attempt to free buffers using the BC_FREE_BUFFER
ioctl to binder. There are protections against a user freeing a buffer
while in use by the kernel, however there was a window where
BC_FREE_BUFFER could be used to free a recently allocated buffer that
was not completely initialized. This resulted in a use-after-free
detected by KASAN with a malicious test program.

This window is closed by setting the buffer's allow_user_free attribute
to 0 when the buffer is allocated or when the user has previously freed
it instead of waiting for the caller to set it. The problem was that
when the struct buffer was recycled, allow_user_free was stale and set
to 1 allowing a free to go through.
Signed-off-by: default avatarTodd Kjos <tkjos@google.com>
Acked-by: default avatarArve Hjønnevåg <arve@android.com>
Cc: stable <stable@vger.kernel.org> # 4.14
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 032371a1
...@@ -2974,7 +2974,6 @@ static void binder_transaction(struct binder_proc *proc, ...@@ -2974,7 +2974,6 @@ static void binder_transaction(struct binder_proc *proc,
t->buffer = NULL; t->buffer = NULL;
goto err_binder_alloc_buf_failed; goto err_binder_alloc_buf_failed;
} }
t->buffer->allow_user_free = 0;
t->buffer->debug_id = t->debug_id; t->buffer->debug_id = t->debug_id;
t->buffer->transaction = t; t->buffer->transaction = t;
t->buffer->target_node = target_node; t->buffer->target_node = target_node;
...@@ -3510,14 +3509,18 @@ static int binder_thread_write(struct binder_proc *proc, ...@@ -3510,14 +3509,18 @@ static int binder_thread_write(struct binder_proc *proc,
buffer = binder_alloc_prepare_to_free(&proc->alloc, buffer = binder_alloc_prepare_to_free(&proc->alloc,
data_ptr); data_ptr);
if (buffer == NULL) { if (IS_ERR_OR_NULL(buffer)) {
binder_user_error("%d:%d BC_FREE_BUFFER u%016llx no match\n", if (PTR_ERR(buffer) == -EPERM) {
proc->pid, thread->pid, (u64)data_ptr); binder_user_error(
break; "%d:%d BC_FREE_BUFFER u%016llx matched unreturned or currently freeing buffer\n",
} proc->pid, thread->pid,
if (!buffer->allow_user_free) { (u64)data_ptr);
binder_user_error("%d:%d BC_FREE_BUFFER u%016llx matched unreturned buffer\n", } else {
proc->pid, thread->pid, (u64)data_ptr); binder_user_error(
"%d:%d BC_FREE_BUFFER u%016llx no match\n",
proc->pid, thread->pid,
(u64)data_ptr);
}
break; break;
} }
binder_debug(BINDER_DEBUG_FREE_BUFFER, binder_debug(BINDER_DEBUG_FREE_BUFFER,
......
...@@ -151,16 +151,12 @@ static struct binder_buffer *binder_alloc_prepare_to_free_locked( ...@@ -151,16 +151,12 @@ static struct binder_buffer *binder_alloc_prepare_to_free_locked(
else { else {
/* /*
* Guard against user threads attempting to * Guard against user threads attempting to
* free the buffer twice * free the buffer when in use by kernel or
* after it's already been freed.
*/ */
if (buffer->free_in_progress) { if (!buffer->allow_user_free)
binder_alloc_debug(BINDER_DEBUG_USER_ERROR, return ERR_PTR(-EPERM);
"%d:%d FREE_BUFFER u%016llx user freed buffer twice\n", buffer->allow_user_free = 0;
alloc->pid, current->pid,
(u64)user_ptr);
return NULL;
}
buffer->free_in_progress = 1;
return buffer; return buffer;
} }
} }
...@@ -500,7 +496,7 @@ static struct binder_buffer *binder_alloc_new_buf_locked( ...@@ -500,7 +496,7 @@ static struct binder_buffer *binder_alloc_new_buf_locked(
rb_erase(best_fit, &alloc->free_buffers); rb_erase(best_fit, &alloc->free_buffers);
buffer->free = 0; buffer->free = 0;
buffer->free_in_progress = 0; buffer->allow_user_free = 0;
binder_insert_allocated_buffer_locked(alloc, buffer); binder_insert_allocated_buffer_locked(alloc, buffer);
binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC, binder_alloc_debug(BINDER_DEBUG_BUFFER_ALLOC,
"%d: binder_alloc_buf size %zd got %pK\n", "%d: binder_alloc_buf size %zd got %pK\n",
......
...@@ -50,8 +50,7 @@ struct binder_buffer { ...@@ -50,8 +50,7 @@ struct binder_buffer {
unsigned free:1; unsigned free:1;
unsigned allow_user_free:1; unsigned allow_user_free:1;
unsigned async_transaction:1; unsigned async_transaction:1;
unsigned free_in_progress:1; unsigned debug_id:29;
unsigned debug_id:28;
struct binder_transaction *transaction; struct binder_transaction *transaction;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment