Skip to content

Commit 028cbca

Browse files
bebarinogregkh
authored andcommitted
soc: qcom: rpmh: Avoid accessing freed memory from batch API
commit baef1c9 upstream. Using the batch API from the interconnect driver sometimes leads to a KASAN error due to an access to freed memory. This is easier to trigger with threadirqs on the kernel commandline. BUG: KASAN: use-after-free in rpmh_tx_done+0x114/0x12c Read of size 1 at addr fffffff51414ad84 by task irq/110-apps_rs/57 CPU: 0 PID: 57 Comm: irq/110-apps_rs Tainted: G W 4.19.10 #72 Call trace: dump_backtrace+0x0/0x2f8 show_stack+0x20/0x2c __dump_stack+0x20/0x28 dump_stack+0xcc/0x10c print_address_description+0x74/0x240 kasan_report+0x250/0x26c __asan_report_load1_noabort+0x20/0x2c rpmh_tx_done+0x114/0x12c tcs_tx_done+0x450/0x768 irq_forced_thread_fn+0x58/0x9c irq_thread+0x120/0x1dc kthread+0x248/0x260 ret_from_fork+0x10/0x18 Allocated by task 385: kasan_kmalloc+0xac/0x148 __kmalloc+0x170/0x1e4 rpmh_write_batch+0x174/0x540 qcom_icc_set+0x8dc/0x9ac icc_set+0x288/0x2e8 a6xx_gmu_stop+0x320/0x3c0 a6xx_pm_suspend+0x108/0x124 adreno_suspend+0x50/0x60 pm_generic_runtime_suspend+0x60/0x78 __rpm_callback+0x214/0x32c rpm_callback+0x54/0x184 rpm_suspend+0x3f8/0xa90 pm_runtime_work+0xb4/0x178 process_one_work+0x544/0xbc0 worker_thread+0x514/0x7d0 kthread+0x248/0x260 ret_from_fork+0x10/0x18 Freed by task 385: __kasan_slab_free+0x12c/0x1e0 kasan_slab_free+0x10/0x1c kfree+0x134/0x588 rpmh_write_batch+0x49c/0x540 qcom_icc_set+0x8dc/0x9ac icc_set+0x288/0x2e8 a6xx_gmu_stop+0x320/0x3c0 a6xx_pm_suspend+0x108/0x124 adreno_suspend+0x50/0x60 cr50_spi spi5.0: SPI transfer timed out pm_generic_runtime_suspend+0x60/0x78 __rpm_callback+0x214/0x32c rpm_callback+0x54/0x184 rpm_suspend+0x3f8/0xa90 pm_runtime_work+0xb4/0x178 process_one_work+0x544/0xbc0 worker_thread+0x514/0x7d0 kthread+0x248/0x260 ret_from_fork+0x10/0x18 The buggy address belongs to the object at fffffff51414ac80 which belongs to the cache kmalloc-512 of size 512 The buggy address is located 260 bytes inside of 512-byte region [fffffff51414ac80, fffffff51414ae80) The buggy address belongs to the page: page:ffffffbfd4505200 count:1 mapcount:0 mapping:fffffff51e00c680 index:0x0 compound_mapcount: 0 flags: 0x4000000000008100(slab|head) raw: 4000000000008100 ffffffbfd4529008 ffffffbfd44f9208 fffffff51e00c680 raw: 0000000000000000 0000000000200020 00000001ffffffff 0000000000000000 page dumped because: kasan: bad access detected Memory state around the buggy address: fffffff51414ac80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fffffff51414ad00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb >fffffff51414ad80: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb ^ fffffff51414ae00: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fffffff51414ae80: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc The batch API sets the same completion for each rpmh message that's sent and then loops through all the messages and waits for that single completion declared on the stack to be completed before returning from the function and freeing the message structures. Unfortunately, some messages may still be in process and 'stuck' in the TCS. At some later point, the tcs_tx_done() interrupt will run and try to process messages that have already been freed at the end of rpmh_write_batch(). This will in turn access the 'needs_free' member of the rpmh_request structure and cause KASAN to complain. Furthermore, if there's a message that's completed in rpmh_tx_done() and freed immediately after the complete() call is made we'll be racing with potentially freed memory when accessing the 'needs_free' member: CPU0 CPU1 ---- ---- rpmh_tx_done() complete(&compl) wait_for_completion(&compl) kfree(rpm_msg) if (rpm_msg->needs_free) <KASAN warning splat> Let's fix this by allocating a chunk of completions for each message and waiting for all of them to be completed before returning from the batch API. Alternatively, we could wait for the last message in the batch, but that may be a more complicated change because it looks like tcs_tx_done() just iterates through the indices of the queue and completes each message instead of tracking the last inserted message and completing that first. Fixes: c8790cb ("drivers: qcom: rpmh: add support for batch RPMH request") Cc: Lina Iyer <[email protected]> Cc: "Raju P.L.S.S.S.N" <[email protected]> Cc: Matthias Kaehlcke <[email protected]> Cc: Evan Green <[email protected]> Cc: [email protected] Reviewed-by: Lina Iyer <[email protected]> Reviewed-by: Evan Green <[email protected]> Signed-off-by: Stephen Boyd <[email protected]> Signed-off-by: Bjorn Andersson <[email protected]> Signed-off-by: Andy Gross <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 1098803 commit 028cbca

File tree

1 file changed

+21
-13
lines changed

1 file changed

+21
-13
lines changed

drivers/soc/qcom/rpmh.c

Lines changed: 21 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,7 @@ void rpmh_tx_done(const struct tcs_request *msg, int r)
8080
struct rpmh_request *rpm_msg = container_of(msg, struct rpmh_request,
8181
msg);
8282
struct completion *compl = rpm_msg->completion;
83+
bool free = rpm_msg->needs_free;
8384

8485
rpm_msg->err = r;
8586

@@ -94,7 +95,7 @@ void rpmh_tx_done(const struct tcs_request *msg, int r)
9495
complete(compl);
9596

9697
exit:
97-
if (rpm_msg->needs_free)
98+
if (free)
9899
kfree(rpm_msg);
99100
}
100101

@@ -348,11 +349,12 @@ int rpmh_write_batch(const struct device *dev, enum rpmh_state state,
348349
{
349350
struct batch_cache_req *req;
350351
struct rpmh_request *rpm_msgs;
351-
DECLARE_COMPLETION_ONSTACK(compl);
352+
struct completion *compls;
352353
struct rpmh_ctrlr *ctrlr = get_rpmh_ctrlr(dev);
353354
unsigned long time_left;
354355
int count = 0;
355-
int ret, i, j;
356+
int ret, i;
357+
void *ptr;
356358

357359
if (!cmd || !n)
358360
return -EINVAL;
@@ -362,10 +364,15 @@ int rpmh_write_batch(const struct device *dev, enum rpmh_state state,
362364
if (!count)
363365
return -EINVAL;
364366

365-
req = kzalloc(sizeof(*req) + count * sizeof(req->rpm_msgs[0]),
367+
ptr = kzalloc(sizeof(*req) +
368+
count * (sizeof(req->rpm_msgs[0]) + sizeof(*compls)),
366369
GFP_ATOMIC);
367-
if (!req)
370+
if (!ptr)
368371
return -ENOMEM;
372+
373+
req = ptr;
374+
compls = ptr + sizeof(*req) + count * sizeof(*rpm_msgs);
375+
369376
req->count = count;
370377
rpm_msgs = req->rpm_msgs;
371378

@@ -380,25 +387,26 @@ int rpmh_write_batch(const struct device *dev, enum rpmh_state state,
380387
}
381388

382389
for (i = 0; i < count; i++) {
383-
rpm_msgs[i].completion = &compl;
390+
struct completion *compl = &compls[i];
391+
392+
init_completion(compl);
393+
rpm_msgs[i].completion = compl;
384394
ret = rpmh_rsc_send_data(ctrlr_to_drv(ctrlr), &rpm_msgs[i].msg);
385395
if (ret) {
386396
pr_err("Error(%d) sending RPMH message addr=%#x\n",
387397
ret, rpm_msgs[i].msg.cmds[0].addr);
388-
for (j = i; j < count; j++)
389-
rpmh_tx_done(&rpm_msgs[j].msg, ret);
390398
break;
391399
}
392400
}
393401

394402
time_left = RPMH_TIMEOUT_MS;
395-
for (i = 0; i < count; i++) {
396-
time_left = wait_for_completion_timeout(&compl, time_left);
403+
while (i--) {
404+
time_left = wait_for_completion_timeout(&compls[i], time_left);
397405
if (!time_left) {
398406
/*
399407
* Better hope they never finish because they'll signal
400-
* the completion on our stack and that's bad once
401-
* we've returned from the function.
408+
* the completion that we're going to free once
409+
* we've returned from this function.
402410
*/
403411
WARN_ON(1);
404412
ret = -ETIMEDOUT;
@@ -407,7 +415,7 @@ int rpmh_write_batch(const struct device *dev, enum rpmh_state state,
407415
}
408416

409417
exit:
410-
kfree(req);
418+
kfree(ptr);
411419

412420
return ret;
413421
}

0 commit comments

Comments
 (0)