Skip to contentSkip to conversation

fastrpc: fix probe-time races between rpmsg and userspace

PATCH Applied Vinayak Katoch 3 participants 6 messages Nov 16

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

v3 cover fastrpc: fix probe-time races between rpmsg and userspace

This series fixes two races in fastrpc_rpmsg_probe() that can be triggered when userspace opens a fastrpc device node concurrently with the rpmsg probe sequence.

fastrpc_device_register() calls misc_register() before the channel context is fully initialised. A concurrent open() in that window hits kref_get() on a zero refcount, and because of_platform_populate() has not yet run, sesscount is 0. The error path then calls kref_put() on the saturated refcount, which kfree()s the channel context. A subsequent IRQ-path fastrpc_rpmsg_callback() dereferences the freed spinlock, causing a kernel panic.

A NULL dereference in the fastrpc_device_open() error path when cctx->rpdev is concurrently cleared by fastrpc_rpmsg_remove() is also addressed.

---
Changes in v3:
- Drop the dependency on the dup-sessions series; use of_platform_populate()/of_platform_depopulate() directly in the init
ordering patch
- Fix Fixes: tag of the init ordering patch to 965602eabb57
- Rebase onto next-20261113
- Collect Tested-by on the NULL rpdev patch (unchanged from v2)
- Link to v2: https://lore.kernel.org/r/[email protected]

Changes in v2:
- Add Cc: stable to both fixes
- Free cctx via fastrpc_channel_ctx_put() in the probe error path
- Clear rpdev and wake pending users there, as remove() does
- Move fastrpc_notify_users() above probe
- Link to v1: https://lore.kernel.org/r/[email protected]

---
Vinayak Katoch (2):
misc: fastrpc: fix NULL rpdev dereference when session alloc fails
misc: fastrpc: fix init ordering race in rpmsg probe

 drivers/misc/fastrpc.c | 94 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 56 insertions(+), 38 deletions(-)

---
base-commit: 84abb955b1015477aa11a96e2f3ac1b938b77f48
change-id: 20260923-fastrpc-probe-fixes-30c1980b50e6

Best regards,

Signature · 1 line
Vinayak Katoch <[email protected]>
#1 lore raw

PATCH v3 1/2 misc: fastrpc: fix NULL rpdev dereference when session alloc fails

When a fastrpc client process dies abnormally without closing its file descriptor, fastrpc_device_release() is never called and the compute-cb session slot is never freed. The abnormal client death on the DSP side triggers a glink channel teardown which propagates up through the rpmsg bus and calls fastrpc_rpmsg_remove().

fastrpc_rpmsg_remove() nulls cctx->rpdev before calling misc_deregister(). If a concurrent open() enters fastrpc_device_open() in this window and fastrpc_session_alloc() returns NULL (sessions exhausted by the leaked slot), the error path dereferences cctx->rpdev->dev causing a NULL pointer dereference:

  Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000
  pc : fastrpc_device_open+0x1b8/0x258 [fastrpc]
  Call trace:
   fastrpc_device_open+0x1b8/0x258 [fastrpc] (P)
   misc_open+0xd8/0x1a0

Fix by moving misc_deregister() before rpdev = NULL. misc_open() and misc_deregister() both take misc_mtx, so misc_deregister() will either block until fastrpc_device_open() completes or prevent new opens from entering it entirely.

Fixes 7c11df42d0c7 ("misc: fastrpc: Fix device_open when no session is available")
---
 drivers/misc/fastrpc.c | 12 ++++++------
 1 file changed, 6 insertions(+), 6 deletions(-)
drivers/misc/fastrpc.c +6 -6
index cdc2627bdaf2..da0b2333a3ca 100644
@@ -2675,6 +2675,12 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
 	struct fastrpc_user *user;
 	unsigned long flags;
 
+	if (cctx->fdevice)
+		misc_deregister(&cctx->fdevice->miscdev);
+
+	if (cctx->secure_fdevice)
+		misc_deregister(&cctx->secure_fdevice->miscdev);
+
 	/* No invocations past this point */
 	spin_lock_irqsave(&cctx->lock, flags);
 	cctx->rpdev = NULL;
@@ -2682,12 +2688,6 @@ static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
 		fastrpc_notify_users(user);
 	spin_unlock_irqrestore(&cctx->lock, flags);
 
-	if (cctx->fdevice)
-		misc_deregister(&cctx->fdevice->miscdev);
-
-	if (cctx->secure_fdevice)
-		misc_deregister(&cctx->secure_fdevice->miscdev);
-
 	list_for_each_entry_safe(buf, b, &cctx->invoke_interrupted_mmaps, node)
 		list_del(&buf->node);
 
Signature · 1 line
2.34.1
#2 lore raw

Re: [PATCH v3 1/2] misc: fastrpc: fix NULL rpdev dereference when session alloc fails

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider: - [Low] misc: fastrpc: inaccurate Fixes trailer in fastrpc_rpmsg_remove() fix --

Thank you for your contribution! Sashiko AI review found 1 potential issue to consider:
- [Low] misc: fastrpc: inaccurate Fixes trailer in fastrpc_rpmsg_remove() fix
--

commit 29dfa1f04039615ed133d75259b0e1c7af30e157
Author: Vinayak Katoch <[email protected]>

Quoted 25 lines

misc: fastrpc: fix NULL rpdev dereference when session alloc fails

When a fastrpc client process dies abnormally without closing its file descriptor, fastrpc_device_release() is never called and the compute-cb session slot is never freed. The abnormal client death on the DSP side triggers a glink channel teardown which propagates up through the rpmsg bus and calls fastrpc_rpmsg_remove().

fastrpc_rpmsg_remove() nulls cctx->rpdev before calling misc_deregister(). If a concurrent open() enters fastrpc_device_open() in this window and fastrpc_session_alloc() returns NULL (sessions exhausted by the leaked slot), the error path dereferences cctx->rpdev->dev causing a NULL pointer dereference:

  Unable to handle kernel NULL pointer dereference at virtual address 0000000000000000
  pc : fastrpc_device_open+0x1b8/0x258 [fastrpc]
  Call trace:
   fastrpc_device_open+0x1b8/0x258 [fastrpc] (P)
   misc_open+0xd8/0x1a0

Fix by moving misc_deregister() before rpdev = NULL. misc_open() and misc_deregister() both take misc_mtx, so misc_deregister() will either block until fastrpc_device_open() completes or prevent new opens from entering it entirely.

Quoted 5 lines
Fixes 7c11df42d0c7 ("misc: fastrpc: Fix device_open when no session is available")

[Severity: Low]
In commit 7c11df42d0c7 ("misc: fastrpc: Fix device_open when no session is available"), fastrpc_rpmsg_remove() did not clear cctx->rpdev at all, and when 2e369878bd43 ("misc: fastrpc: Don't reference rpmsg_device after remove") added cctx->rpdev = NULL, it placed it after misc_deregister().

46248400d81e ("misc: fastrpc: reject new invocations during device removal") moved cctx->rpdev = NULL ahead of misc_deregister(), introducing the race window where fastrpc_device_open() can see a NULL cctx->rpdev.

Should the Fixes tag be:

Fixes 46248400d81e ("misc: fastrpc: reject new invocations during device removal")
Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
#3 lore raw

PATCH v3 2/2 misc: fastrpc: fix init ordering race in rpmsg probe

In reply to Vinayak Katoch

fastrpc_device_register() calls misc_register() which makes the device node visible to userspace. However, kref_init(), spin_lock_init(), INIT_LIST_HEAD(), dev_set_drvdata() and of_platform_populate() were all called after the switch/misc_register block, leaving a window where userspace can open the device and call fastrpc_device_open() on a partially initialised channel context.

On a concurrent open(), fastrpc_channel_ctx_get() calls kref_get() on a kzalloc-zeroed, never kref_init()'d refcount of 0, triggering:

refcount_t: addition on 0; use-after-free.

  Call trace:
   refcount_warn_saturate+0x120/0x148 (P)
   fastrpc_device_open+0x204/0x258 [fastrpc]
   misc_open+0xd8/0x1a0

Additionally, of_platform_populate() not having run means sesscount is still 0, so fastrpc_session_alloc() returns NULL and fastrpc_device_open() hits the error path which calls fastrpc_channel_ctx_put() on the already-bogus refcount. This triggers the free callback, kfree()-ing the channel context. A subsequent IRQ-path fastrpc_rpmsg_callback() then dereferences the freed spinlock:

  Unable to handle kernel paging request at virtual address ffffcd5841510ca0
  Internal error: Oops: 0000000096000047 [#1] SMP
  Call trace:
   queued_spin_lock_slowpath+0x414/0x5e0 (P)
   _raw_spin_lock_irqsave+0x74/0x90
   fastrpc_rpmsg_callback+0x4c/0xf0 [fastrpc]
   qcom_glink_native_rx+0x6c4/0xf10
   qcom_glink_smem_intr+0x1c/0x38 [qcom_glink_smem]
  Kernel panic - not syncing: Oops: Fatal exception in interrupt

Fix by moving all channel context initialisation (kref_init, spin_lock_init, INIT_LIST_HEAD, idr_init, dev_set_drvdata) and of_platform_populate() before fastrpc_device_register(), ensuring the struct is fully ready before the device node is visible to userspace.

Add err_depopulate label in the error path since of_platform_populate() now runs before misc_register(); a fastrpc_device_register() failure must call of_platform_depopulate() to clean up the compute-cb children.

On CDSP/GDSP the secure node is registered before the non-secure one, so an application may already have opened the first node and hold a channel context reference when the second registration fails. Freeing the context with kfree() there would leave that user with a dangling pointer. Handle it like fastrpc_rpmsg_remove(): after the nodes are deregistered, clear rpdev, wake pending invocations with -EPIPE and drop only the probe reference with fastrpc_channel_ctx_put(), so the last holder frees the context. Move fastrpc_notify_users() above probe so it can be used there.

Fixes 965602eabb57 ("misc: fastrpc: separate fastrpc device from channel context")
---
 drivers/misc/fastrpc.c | 82 ++++++++++++++++++++++++++++++--------------------
 1 file changed, 50 insertions(+), 32 deletions(-)
drivers/misc/fastrpc.c +50 -32
index da0b2333a3ca..0bbc1e00001c 100644
@@ -2542,6 +2542,18 @@ static int fastrpc_init_reserved_mem(struct fastrpc_channel_ctx *cctx,
 				  cctx->vmperms, cctx->vmcount);
 }
 
+static void fastrpc_notify_users(struct fastrpc_user *user)
+{
+	struct fastrpc_invoke_ctx *ctx;
+
+	spin_lock(&user->lock);
+	list_for_each_entry(ctx, &user->pending, node) {
+		ctx->retval = -EPIPE;
+		complete(&ctx->work);
+	}
+	spin_unlock(&user->lock);
+}
+
 static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 {
 	struct device *rdev = &rpdev->dev;
@@ -2551,6 +2563,8 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 	bool secure_dsp;
 	unsigned int vmids[FASTRPC_MAX_VMIDS];
 	const struct fastrpc_soc_data *soc_data;
+	struct fastrpc_user *user;
+	unsigned long flags;
 
 	soc_data = device_get_match_data(rdev);
 
@@ -2599,6 +2613,23 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 	data->poll_mode_supported = soc_data->poll_mode_supported ||
 		of_machine_get_match(fastrpc_poll_supported_machines);
 
+	kref_init(&data->refcount);
+	atomic_set(&data->ctx_seq, 0);
+
+	rdev->dma_mask = &data->dma_mask;
+	dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
+	INIT_LIST_HEAD(&data->users);
+	INIT_LIST_HEAD(&data->invoke_interrupted_mmaps);
+	spin_lock_init(&data->lock);
+	idr_init(&data->ctx_idr);
+	data->domain_id = domain_id;
+	data->rpdev = rpdev;
+	dev_set_drvdata(&rpdev->dev, data);
+
+	err = of_platform_populate(rdev->of_node, NULL, NULL, rdev);
+	if (err)
+		goto err_free_data;
+
 	switch (domain_id) {
 	case ADSP_DOMAIN_ID:
 	case MDSP_DOMAIN_ID:
@@ -2607,7 +2638,7 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 		data->unsigned_support = false;
 		err = fastrpc_device_register(rdev, data, secure_dsp, domain);
 		if (err)
-			goto err_free_data;
+			goto err_depopulate;
 		break;
 	case CDSP_DOMAIN_ID:
 	case GDSP_DOMAIN_ID:
@@ -2615,7 +2646,7 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 		/* Create both device nodes so that we can allow both Signed and Unsigned PD */
 		err = fastrpc_device_register(rdev, data, true, domain);
 		if (err)
-			goto err_free_data;
+			goto err_depopulate;
 
 		err = fastrpc_device_register(rdev, data, false, domain);
 		if (err)
@@ -2623,26 +2654,9 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 		break;
 	default:
 		err = -EINVAL;
-		goto err_free_data;
+		goto err_depopulate;
 	}
 
-	kref_init(&data->refcount);
-	atomic_set(&data->ctx_seq, 0);
-
-	rdev->dma_mask = &data->dma_mask;
-	dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
-	INIT_LIST_HEAD(&data->users);
-	INIT_LIST_HEAD(&data->invoke_interrupted_mmaps);
-	spin_lock_init(&data->lock);
-	idr_init(&data->ctx_idr);
-	data->domain_id = domain_id;
-	data->rpdev = rpdev;
-	dev_set_drvdata(&rpdev->dev, data);
-
-	err = of_platform_populate(rdev->of_node, NULL, NULL, rdev);
-	if (err)
-		goto err_deregister_fdev;
-
 	return 0;
 
 err_deregister_fdev:
@@ -2651,23 +2665,27 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 	if (data->secure_fdevice)
 		misc_deregister(&data->secure_fdevice->miscdev);
 
+err_depopulate:
+	/*
+	 * A registered device node may already have been opened, so a user
+	 * can hold a cctx reference. Drop only our reference instead of
+	 * freeing the context directly.
+	 */
+	spin_lock_irqsave(&data->lock, flags);
+	data->rpdev = NULL;
+	list_for_each_entry(user, &data->users, user)
+		fastrpc_notify_users(user);
+	spin_unlock_irqrestore(&data->lock, flags);
+
+	of_platform_depopulate(rdev);
+	fastrpc_channel_ctx_put(data);
+	return err;
+
 err_free_data:
 	kfree(data);
 	return err;
 }
 
-static void fastrpc_notify_users(struct fastrpc_user *user)
-{
-	struct fastrpc_invoke_ctx *ctx;
-
-	spin_lock(&user->lock);
-	list_for_each_entry(ctx, &user->pending, node) {
-		ctx->retval = -EPIPE;
-		complete(&ctx->work);
-	}
-	spin_unlock(&user->lock);
-}
-
 static void fastrpc_rpmsg_remove(struct rpmsg_device *rpdev)
 {
 	struct fastrpc_channel_ctx *cctx = dev_get_drvdata(&rpdev->dev);
Signature · 1 line
2.34.1
#4 lore raw

Re: [PATCH v3 2/2] misc: fastrpc: fix init ordering race in rpmsg probe

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider: - [High] misc: fastrpc: dangling drvdata and dma_mask on fastrpc_rpmsg_probe() error - [Low] misc: fastrpc…

Thank you for your contribution! Sashiko AI review found 2 potential issues to consider:
- [High] misc: fastrpc: dangling drvdata and dma_mask on fastrpc_rpmsg_probe() error
- [Low] misc: fastrpc: inaccurate Fixes trailer in fastrpc_rpmsg_probe() commit
--

commit 4a1cfc3ddafb9d12682d2f0bc78e07af7648cf89
Author: Vinayak Katoch <[email protected]>

Quoted 57 lines

misc: fastrpc: fix init ordering race in rpmsg probe

fastrpc_device_register() calls misc_register() which makes the device node visible to userspace. However, kref_init(), spin_lock_init(), INIT_LIST_HEAD(), dev_set_drvdata() and of_platform_populate() were all called after the switch/misc_register block, leaving a window where userspace can open the device and call fastrpc_device_open() on a partially initialised channel context.

On a concurrent open(), fastrpc_channel_ctx_get() calls kref_get() on a kzalloc-zeroed, never kref_init()'d refcount of 0, triggering:

refcount_t: addition on 0; use-after-free.

  Call trace:
   refcount_warn_saturate+0x120/0x148 (P)
   fastrpc_device_open+0x204/0x258 [fastrpc]
   misc_open+0xd8/0x1a0

Additionally, of_platform_populate() not having run means sesscount is still 0, so fastrpc_session_alloc() returns NULL and fastrpc_device_open() hits the error path which calls fastrpc_channel_ctx_put() on the already-bogus refcount. This triggers the free callback, kfree()-ing the channel context. A subsequent IRQ-path fastrpc_rpmsg_callback() then dereferences the freed spinlock:

  Unable to handle kernel paging request at virtual address ffffcd5841510ca0
  Internal error: Oops: 0000000096000047 [#1] SMP
  Call trace:
   queued_spin_lock_slowpath+0x414/0x5e0 (P)
   _raw_spin_lock_irqsave+0x74/0x90
   fastrpc_rpmsg_callback+0x4c/0xf0 [fastrpc]
   qcom_glink_native_rx+0x6c4/0xf10
   qcom_glink_smem_intr+0x1c/0x38 [qcom_glink_smem]
  Kernel panic - not syncing: Oops: Fatal exception in interrupt

Fix by moving all channel context initialisation (kref_init, spin_lock_init, INIT_LIST_HEAD, idr_init, dev_set_drvdata) and of_platform_populate() before fastrpc_device_register(), ensuring the struct is fully ready before the device node is visible to userspace.

Add err_depopulate label in the error path since of_platform_populate() now runs before misc_register(); a fastrpc_device_register() failure must call of_platform_depopulate() to clean up the compute-cb children.

On CDSP/GDSP the secure node is registered before the non-secure one, so an application may already have opened the first node and hold a channel context reference when the second registration fails. Freeing the context with kfree() there would leave that user with a dangling pointer. Handle it like fastrpc_rpmsg_remove(): after the nodes are deregistered, clear rpdev, wake pending invocations with -EPIPE and drop only the probe reference with fastrpc_channel_ctx_put(), so the last holder frees the context. Move fastrpc_notify_users() above probe so it can be used there.

Quoted 3 lines
Fixes 965602eabb57 ("misc: fastrpc: separate fastrpc device from channel context")

[Severity: Low]
Is 965602eabb57 ("misc: fastrpc: separate fastrpc device from channel context") the right commit for the Fixes tag?

That commit only factored misc_register() into fastrpc_device_register() at the same position in fastrpc_rpmsg_probe(). Calling misc_register() before spin_lock_init(), INIT_LIST_HEAD(), idr_init() and of_platform_populate() dates back to f6f9279f2bf0 ("misc: fastrpc: Add Qualcomm fastrpc basic driver model"), and the kref_init() ordering issue was introduced in 278d56f970ae ("misc: fastrpc: Reference count channel context").

Quoted 4 lines
drivers/misc/fastrpc.c +0 -0
index da0b2333a3ca3..0bbc1e00001ca 100644

[ ... ]

Quoted 84 lines
drivers/misc/fastrpc.c +30 -20
@@ -2599,6 +2613,23 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 	data->poll_mode_supported = soc_data->poll_mode_supported ||
 		of_machine_get_match(fastrpc_poll_supported_machines);
 
+	kref_init(&data->refcount);
+	atomic_set(&data->ctx_seq, 0);
+
+	rdev->dma_mask = &data->dma_mask;
+	dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
+	INIT_LIST_HEAD(&data->users);
+	INIT_LIST_HEAD(&data->invoke_interrupted_mmaps);
+	spin_lock_init(&data->lock);
+	idr_init(&data->ctx_idr);
+	data->domain_id = domain_id;
+	data->rpdev = rpdev;
+	dev_set_drvdata(&rpdev->dev, data);
+
+	err = of_platform_populate(rdev->of_node, NULL, NULL, rdev);
+	if (err)
+		goto err_free_data;
+
 	switch (domain_id) {
 	case ADSP_DOMAIN_ID:
 	case MDSP_DOMAIN_ID:
@@ -2607,7 +2638,7 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 		data->unsigned_support = false;
 		err = fastrpc_device_register(rdev, data, secure_dsp, domain);
 		if (err)
-			goto err_free_data;
+			goto err_depopulate;
 		break;
 	case CDSP_DOMAIN_ID:
 	case GDSP_DOMAIN_ID:
@@ -2615,7 +2646,7 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 		/* Create both device nodes so that we can allow both Signed and Unsigned PD */
 		err = fastrpc_device_register(rdev, data, true, domain);
 		if (err)
-			goto err_free_data;
+			goto err_depopulate;
 
 		err = fastrpc_device_register(rdev, data, false, domain);
 		if (err)
@@ -2623,26 +2654,9 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 		break;
 	default:
 		err = -EINVAL;
-		goto err_free_data;
+		goto err_depopulate;
 	}
 
-	kref_init(&data->refcount);
-	atomic_set(&data->ctx_seq, 0);
-
-	rdev->dma_mask = &data->dma_mask;
-	dma_set_mask_and_coherent(rdev, DMA_BIT_MASK(32));
-	INIT_LIST_HEAD(&data->users);
-	INIT_LIST_HEAD(&data->invoke_interrupted_mmaps);
-	spin_lock_init(&data->lock);
-	idr_init(&data->ctx_idr);
-	data->domain_id = domain_id;
-	data->rpdev = rpdev;
-	dev_set_drvdata(&rpdev->dev, data);
-
-	err = of_platform_populate(rdev->of_node, NULL, NULL, rdev);
-	if (err)
-		goto err_deregister_fdev;
-
 	return 0;
 
 err_deregister_fdev:
@@ -2651,23 +2665,27 @@ static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev)
 	if (data->secure_fdevice)
 		misc_deregister(&data->secure_fdevice->miscdev);
 
+err_depopulate:
+	/*
+	 * A registered device node may already have been opened, so a user
+	 * can hold a cctx reference. Drop only our reference instead of
+	 * freeing the context directly.
+	 */
+	spin_lock_irqsave(&data->lock, flags);
+	data->rpdev = NULL;
+	list_for_each_entry(user, &data->users, user)
+		fastrpc_notify_users(user);

drivers/misc/fastrpc.c static int fastrpc_rpmsg_probe(struct rpmsg_device *rpdev) · line 2687

Quoted 10 lines
drivers/misc/fastrpc.c +6 -0
+	spin_unlock_irqrestore(&data->lock, flags);
+
+	of_platform_depopulate(rdev);
+	fastrpc_channel_ctx_put(data);
+	return err;
+
 err_free_data:
 	kfree(data);
 	return err;
 }

[Severity: High]
Does moving dev_set_drvdata() and rdev->dma_mask = &data->dma_mask earlier leave dangling pointers on rpdev->dev when of_platform_populate() or fastrpc_device_register() fails?

rpmsg_dev_probe() creates rpdev->ept with fastrpc_rpmsg_callback() before calling fastrpc_rpmsg_probe(), and only destroys the endpoint after probe returns:

CPU0 CPU1 (<hardirq>)
rpmsg_dev_probe()
rpmsg_create_ept()
fastrpc_rpmsg_probe()

    dev_set_drvdata(&rpdev->dev, data)
    of_platform_populate() -> fails
    kfree(data)
                                      fastrpc_rpmsg_callback()
                                        cctx = dev_get_drvdata(&rpdev->dev)
                                        spin_lock_irqsave(&cctx->lock, ...)
                                          <-- use-after-free

rpmsg_destroy_ept()

Because fastrpc_rpmsg_callback() only checks whether cctx is NULL, any incoming rpmsg interrupt before rpmsg_destroy_ept() finishes will dereference freed data at &cctx->lock and &cctx->ctx_idr. In addition, device_unbind_cleanup() does not reset rdev->dma_mask, leaving it pointing into the freed struct fastrpc_channel_ctx.

Clearing drvdata with dev_set_drvdata(&rpdev->dev, NULL) and resetting rdev->dma_mask = NULL before releasing data in the error paths (or setting drvdata only after fastrpc_device_register() succeeds) would avoid this regression.

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=2
maintainer Applied
#5 lore raw
In reply to Vinayak Katoch
Quoted 12 lines · Vinayak Katoch

This series fixes two races in fastrpc_rpmsg_probe() that can be triggered when userspace opens a fastrpc device node concurrently with the rpmsg probe sequence.

fastrpc_device_register() calls misc_register() before the channel context is fully initialised. A concurrent open() in that window hits kref_get() on a zero refcount, and because of_platform_populate() has not yet run, sesscount is 0. The error path then calls kref_put() on the saturated refcount, which kfree()s the channel context. A subsequent IRQ-path fastrpc_rpmsg_callback() dereferences the freed spinlock, causing a kernel panic.

Quoted 1 line

[...]

Applied, thanks!

[1/2] misc: fastrpc: fix NULL rpdev dereference when session alloc fails

      commit: 2b22e3266386190d2027d9f12f317e0662a40711
[2/2] misc: fastrpc: fix init ordering race in rpmsg probe
      commit: 1bf78aac760fd25b684153f8c477aede9b9dd634

Best regards,

Signature · 1 line
Srinivas Kandagatla <[email protected]>