From: John Stultz Date: Tue, 17 Dec 2013 05:07:52 +0000 (-0800) Subject: staging: ion: Fix possible null pointer dereference X-Git-Tag: firefly_0821_release~4090^2~422 X-Git-Url: http://plrg.eecs.uci.edu/git/?a=commitdiff_plain;h=60e11dfc002962181fe4f0db54b33c65fee02c52;p=firefly-linux-kernel-4.4.55.git staging: ion: Fix possible null pointer dereference The kbuild test robot reported: drivers/staging/android/ion/ion_system_heap.c:122 alloc_largest_available() error: potential null dereference 'info'. (kmalloc returns null) Where the pointer returned from kmalloc goes unchecked for failure. This patch checks the return for NULL, and reworks the logic, as suggested by Colin, so we allocate the page_info structure first. Acked-by: Colin Cross Cc: Android Kernel Team Reported-by: kbuild test robot Signed-off-by: John Stultz Signed-off-by: Greg Kroah-Hartman --- diff --git a/drivers/staging/android/ion/ion_system_heap.c b/drivers/staging/android/ion/ion_system_heap.c index 05e9dc93980f..5bccf9e2c93c 100644 --- a/drivers/staging/android/ion/ion_system_heap.c +++ b/drivers/staging/android/ion/ion_system_heap.c @@ -108,6 +108,10 @@ static struct page_info *alloc_largest_available(struct ion_system_heap *heap, struct page_info *info; int i; + info = kmalloc(sizeof(struct page_info), GFP_KERNEL); + if (!info) + return NULL; + for (i = 0; i < num_orders; i++) { if (size < order_to_size(orders[i])) continue; @@ -118,11 +122,12 @@ static struct page_info *alloc_largest_available(struct ion_system_heap *heap, if (!page) continue; - info = kmalloc(sizeof(struct page_info), GFP_KERNEL); info->page = page; info->order = orders[i]; return info; } + kfree(info); + return NULL; }