Skip to content

Commit 2a9edd0

Browse files
thejhgregkh
authored andcommitted
binder: Handle start==NULL in binder_update_page_range()
The old loop wouldn't stop when reaching `start` if `start==NULL`, instead continuing backwards to index -1 and crashing. Luckily you need to be highly privileged to map things at NULL, so it's not a big problem. Fix it by adjusting the loop so that the loop variable is always in bounds. This patch is deliberately minimal to simplify backporting, but IMO this function could use a refactor. The jump labels in the second loop body are horrible (the error gotos should be jumping to free_range instead), and both loops would look nicer if they just iterated upwards through indices. And the up_read()+mmput() shouldn't be duplicated like that. Cc: stable@vger.kernel.org Fixes: 457b9a6 ("Staging: android: add binder driver") Signed-off-by: Jann Horn <jannh@google.com> Acked-by: Christian Brauner <christian.brauner@ubuntu.com> Link: https://lore.kernel.org/r/20191018205631.248274-3-jannh@google.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent a7a74d7 commit 2a9edd0

1 file changed

Lines changed: 5 additions & 3 deletions

File tree

‎drivers/android/binder_alloc.c‎

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -276,8 +276,7 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
276276
return 0;
277277

278278
free_range:
279-
for (page_addr = end - PAGE_SIZE; page_addr >= start;
280-
page_addr -= PAGE_SIZE) {
279+
for (page_addr = end - PAGE_SIZE; 1; page_addr -= PAGE_SIZE) {
281280
bool ret;
282281
size_t index;
283282

@@ -290,14 +289,17 @@ static int binder_update_page_range(struct binder_alloc *alloc, int allocate,
290289
WARN_ON(!ret);
291290

292291
trace_binder_free_lru_end(alloc, index);
292+
if (page_addr == start)
293+
break;
293294
continue;
294295

295296
err_vm_insert_page_failed:
296297
__free_page(page->page_ptr);
297298
page->page_ptr = NULL;
298299
err_alloc_page_failed:
299300
err_page_ptr_cleared:
300-
;
301+
if (page_addr == start)
302+
break;
301303
}
302304
err_no_vma:
303305
if (mm) {

0 commit comments

Comments
 (0)