Skip to contentSkip to conversation

misc: fastrpc: drop invoke context on interrupted waits

PATCH New Yousef Alhouseen 2 participants 2 messages Jun 24

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc: fastrpc: drop invoke context on interrupted waits

fastrpc_internal_invoke() skips removing and dropping the invoke context when userspace wait is interrupted or a kernel invoke times out. The context remains on the user's pending list even after a later DSP reply consumes the send reference.

Remove the context from the pending list and drop the caller reference for all exits. A late DSP reply still holds the send reference until the callback releases it, but interrupted callers no longer pin completed contexts for the lifetime of the file.

---
 drivers/misc/fastrpc.c | 15 +++++++--------
 1 file changed, 7 insertions(+), 8 deletions(-)
drivers/misc/fastrpc.c +7 -8
index 4b258dbcd..90281859a 100644
@@ -1379,14 +1379,6 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl,  u32 kernel,
 		goto bail;
 
 bail:
-	if (err != -ERESTARTSYS && err != -ETIMEDOUT) {
-		/* We are done with this compute context */
-		spin_lock(&fl->lock);
-		list_del(&ctx->node);
-		spin_unlock(&fl->lock);
-		fastrpc_context_put(ctx);
-	}
-
 	if (err == -ERESTARTSYS) {
 		list_for_each_entry_safe(buf, b, &fl->mmaps, node) {
 			list_del(&buf->node);
@@ -1394,6 +1386,13 @@ static int fastrpc_internal_invoke(struct fastrpc_user *fl,  u32 kernel,
 		}
 	}
 
+	/* We are done with this compute context */
+	spin_lock(&fl->lock);
+	if (!list_empty(&ctx->node))
+		list_del_init(&ctx->node);
+	spin_unlock(&fl->lock);
+	fastrpc_context_put(ctx);
+
 	if (err)
 		dev_dbg(fl->sctx->dev, "Error: Invoke Failed %d\n", err);
 
Signature · 1 line
2.54.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:

Thank you for your contribution! Sashiko AI review found 7 potential issue(s) to consider:

Pre-existing issues:
- [Critical] The fastrpc driver is vulnerable to a use-after-free or double free if the DSP sends multiple responses for the same context ID.
- [High] Iterating and modifying fl->mmaps and cctx->invoke_interrupted_mmaps without locks on the error path causes severe list corruption.
- [High] Buffers moved to cctx->invoke_interrupted_mmaps on -ERESTARTSYS are never freed, causing a permanent memory leak of DMA buffers and mappings.
- [High] Pending contexts and their associated fastrpc_user structures are permanently leaked if the DSP crashes or the rpmsg channel is removed.
- [High] fastrpc_invoke_send accesses cctx->rpdev->ept without synchronization, leading to a NULL pointer dereference if the rpmsg channel is removed concurrently.
- [High] fastrpc_context_free leaks fastrpc_map structures and dma-buf references for handle arguments.
- [Medium] The ctx_idr initialized for each channel context is never destroyed, leaking internal radix tree memory.
--

commit 987838af3ff819b971471dae5de97f93f9b3abb9
Author: Yousef Alhouseen <[email protected]>

misc: fastrpc: drop invoke context on interrupted waits

This patch modifies fastrpc_internal_invoke() to remove the context from the pending list and drop the caller reference on all exits, including interrupted waits and timeouts. This ensures that contexts aren't pinned for the lifetime of the file when userspace is interrupted.

Quoted 3 lines
drivers/misc/fastrpc.c +0 -0

[ ... ]

drivers/misc/fastrpc.c static int fastrpc_internal_invoke(struct fastrpc_user *fl, u32 kernel, · line 1383

Quoted 2 lines
drivers/misc/fastrpc.c +0 -0
 	if (err == -ERESTARTSYS) {
 		list_for_each_entry_safe(buf, b, &fl->mmaps, node) {

[Severity: High]
This is a pre-existing issue, but does iterating and modifying fl->mmaps without holding fl->lock race with concurrent mmap operations?

It appears another thread could be adding or removing mmaps concurrently using spin_lock(&fl->lock), which might corrupt the list during this traversal.

drivers/misc/fastrpc.c

Quoted 2 lines
drivers/misc/fastrpc.c +0 -0
 			list_del(&buf->node);
 			list_add_tail(&buf->node, &fl->cctx->invoke_interrupted_mmaps);

[Severity: High]
This is a pre-existing issue, but are the buffers moved to cctx->invoke_interrupted_mmaps ever actually freed?

Looking at fastrpc_rpmsg_remove() in drivers/misc/fastrpc.c, the nodes are deleted from the list during teardown, but fastrpc_buf_free() is never called on them:

fastrpc_rpmsg_remove() {
    ...
    list_for_each_entry_safe(buf, b, &cctx->invoke_interrupted_mmaps, node)
        list_del(&buf->node);
    ...
}

Could this permanently leak the pinned DMA buffers and mapping structures if userspace sends signals to interrupt an ongoing RPC invocation?

drivers/misc/fastrpc.c

Quoted 2 lines
drivers/misc/fastrpc.c +0 -0
 		}
 	}

[ ... ]

While reviewing the context management in this driver, I noticed a few other pre-existing issues:

[Severity: High]
This is a pre-existing issue, but how is the reference count for the remote DSP handled if the channel is removed?

In fastrpc_invoke_send(), a reference is taken before sending the message:

fastrpc_invoke_send() {
    ...
    fastrpc_context_get(ctx);
    ret = rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
    ...
}

If the DSP crashes and fastrpc_notify_users() wakes up waiters to abort:

fastrpc_notify_users() {
    ...
    ctx->retval = -EPIPE;
    complete(&ctx->work);
    ...
}

It doesn't seem to drop the reference count acquired for the remote DSP. Will this permanently pin the context and its associated fastrpc_user structures?

[Severity: Critical]
This is a pre-existing issue, but what happens if the remote DSP sends multiple responses for the same context ID?

When a reply is received, fastrpc_rpmsg_callback() queues work:

fastrpc_rpmsg_callback() {
    ...
    schedule_work(&ctx->put_work);
    ...
}

Since the context is not removed from the IDR tree upon the first reply, a duplicate response would queue put_work again. Could this artificially drop the refcount to zero and lead to a use-after-free if the context is freed while the original caller is still accessing it?

[Severity: High]
This is a pre-existing issue, but is cctx->rpdev adequately protected against concurrent removal?

During teardown, fastrpc_rpmsg_remove() clears the pointer:

fastrpc_rpmsg_remove() {
    ...
    spin_lock_irqsave(&cctx->lock, flags);
    cctx->rpdev = NULL;
    ...
}

However, fastrpc_invoke_send() accesses the endpoint without synchronization:

fastrpc_invoke_send() {
    ...
    ret = rpmsg_send(cctx->rpdev->ept, (void *)msg, sizeof(*msg));
    ...
}

Can this result in a NULL pointer dereference if the rpmsg channel is unbound concurrently?

[Severity: Medium]
This is a pre-existing issue, but is the IDR tree properly cleaned up when freeing the channel context?

fastrpc_channel_ctx_free() {
    ...
    kfree(cctx);
    ...
}

It frees the cctx structure without calling idr_destroy(&cctx->ctx_idr). Does this leave the internal radix tree memory allocated?

[Severity: High]
This is a pre-existing issue, but does fastrpc_context_free() fail to release all dma-buf references for handle arguments?

fastrpc_context_free() {
    ...
    for (i = 0; i < ctx->nbufs; i++)
        fastrpc_map_put(ctx->maps[i]);
    ...
}

The loop iterates up to ctx->nbufs. If userspace invokes an RPC call with handle arguments where ctx->nscalars is greater than ctx->nbufs, does this loop abandon the maps stored at the higher indices?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1