optee: Fix memory leak when failing to register shm pages

Free the previously allocated pages when we encounter an error condition
while attempting to register the pages with the secure world.

Fixes: a249dd200d ("tee: optee: Fix dynamic shm pool allocations")
Fixes: 5a769f6ff4 ("optee: Fix multi page dynamic shm pool alloc")
Cc: stable@vger.kernel.org
Signed-off-by: Tyler Hicks <tyhicks@linux.microsoft.com>
Reviewed-by: Jens Wiklander <jens.wiklander@linaro.org>
Reviewed-by: Sumit Garg <sumit.garg@linaro.org>
Signed-off-by: Jens Wiklander <jens.wiklander@linaro.org>
This commit is contained in:
Tyler Hicks 2021-06-14 17:33:10 -05:00 committed by Jens Wiklander
parent 2734d6c1b1
commit ec185dd3ab

View File

@ -32,8 +32,10 @@ static int pool_op_alloc(struct tee_shm_pool_mgr *poolm,
struct page **pages; struct page **pages;
pages = kcalloc(nr_pages, sizeof(pages), GFP_KERNEL); pages = kcalloc(nr_pages, sizeof(pages), GFP_KERNEL);
if (!pages) if (!pages) {
return -ENOMEM; rc = -ENOMEM;
goto err;
}
for (i = 0; i < nr_pages; i++) { for (i = 0; i < nr_pages; i++) {
pages[i] = page; pages[i] = page;
@ -44,8 +46,14 @@ static int pool_op_alloc(struct tee_shm_pool_mgr *poolm,
rc = optee_shm_register(shm->ctx, shm, pages, nr_pages, rc = optee_shm_register(shm->ctx, shm, pages, nr_pages,
(unsigned long)shm->kaddr); (unsigned long)shm->kaddr);
kfree(pages); kfree(pages);
if (rc)
goto err;
} }
return 0;
err:
__free_pages(page, order);
return rc; return rc;
} }