Skip to contentSkip to conversation

misc: fastrpc: Add reference counting for fastrpc_user structure

PATCH In review Anandu Krishnan E 3 participants 8 messages Feb 26 – Apr 22

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

PATCH 1/1 misc: fastrpc: Add reference counting for fastrpc_user structure

Add reference counting using kref to the fastrpc_user structure to prevent use-after-free issues when contexts are freed from workqueue after device release.

The issue occurs when fastrpc_device_release() frees the user structure while invoke contexts are still pending in the workqueue. When the workqueue later calls fastrpc_context_free(), it attempts to access buf->fl->cctx in fastrpc_buf_free(), leading to a use-after-free:

  pc : fastrpc_buf_free+0x38/0x80 [fastrpc]
  lr : fastrpc_context_free+0xa8/0x1b0 [fastrpc]
  ...
  fastrpc_context_free+0xa8/0x1b0 [fastrpc]
  fastrpc_context_put_wq+0x78/0xa0 [fastrpc]
  process_one_work+0x180/0x450
  worker_thread+0x26c/0x388

Implement proper reference counting to fix this:
- Initialize kref in fastrpc_device_open()
- Take a reference in fastrpc_context_alloc() for each context
- Release the reference in fastrpc_context_free() when context is freed
- Release the initial reference in fastrpc_device_release()

This ensures the user structure remains valid as long as there are contexts holding references to it, preventing the race condition.

Fixes 6cffd79504ce ("misc: fastrpc: Add support for dmabuf exporter")
---
 drivers/misc/fastrpc.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
drivers/misc/fastrpc.c +31 -4
index 47356a5d5804..3ababcf327d7 100644
@@ -310,6 +310,8 @@ struct fastrpc_user {
 	spinlock_t lock;
 	/* lock for allocations */
 	struct mutex mutex;
+	/* Reference count */
+	struct kref refcount;
 };
 
 /* Extract SMMU PA from consolidated IOVA */
@@ -497,15 +499,36 @@ static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
 	kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
 }
 
+static void fastrpc_user_free(struct kref *ref)
+{
+	struct fastrpc_user *fl = container_of(ref, struct fastrpc_user, refcount);
+
+	fastrpc_channel_ctx_put(fl->cctx);
+	mutex_destroy(&fl->mutex);
+	kfree(fl);
+}
+
+static void fastrpc_user_get(struct fastrpc_user *fl)
+{
+	kref_get(&fl->refcount);
+}
+
+static void fastrpc_user_put(struct fastrpc_user *fl)
+{
+	kref_put(&fl->refcount, fastrpc_user_free);
+}
+
 static void fastrpc_context_free(struct kref *ref)
 {
 	struct fastrpc_invoke_ctx *ctx;
 	struct fastrpc_channel_ctx *cctx;
+	struct fastrpc_user *fl;
 	unsigned long flags;
 	int i;
 
 	ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
 	cctx = ctx->cctx;
+	fl = ctx->fl;
 
 	for (i = 0; i < ctx->nbufs; i++)
 		fastrpc_map_put(ctx->maps[i]);
@@ -521,6 +544,8 @@ static void fastrpc_context_free(struct kref *ref)
 	kfree(ctx->olaps);
 	kfree(ctx);
 
+	/* Release the reference taken in fastrpc_context_alloc() */
+	fastrpc_user_put(fl);
 	fastrpc_channel_ctx_put(cctx);
 }
 
@@ -628,6 +653,8 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
 
 	/* Released in fastrpc_context_put() */
 	fastrpc_channel_ctx_get(cctx);
+	/* Take a reference to user, released in fastrpc_context_free() */
+	fastrpc_user_get(user);
 
 	ctx->sc = sc;
 	ctx->retval = -1;
@@ -658,6 +685,7 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
 	spin_lock(&user->lock);
 	list_del(&ctx->node);
 	spin_unlock(&user->lock);
+	fastrpc_user_put(user);
 	fastrpc_channel_ctx_put(cctx);
 	kfree(ctx->maps);
 	kfree(ctx->olaps);
@@ -1606,11 +1634,9 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)
 	}
 
 	fastrpc_session_free(cctx, fl->sctx);
-	fastrpc_channel_ctx_put(cctx);
-
-	mutex_destroy(&fl->mutex);
-	kfree(fl);
 	file->private_data = NULL;
+	/* Release the reference taken in fastrpc_device_open */
+	fastrpc_user_put(fl);
 
 	return 0;
 }
@@ -1654,6 +1680,7 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp)
 	spin_lock_irqsave(&cctx->lock, flags);
 	list_add_tail(&fl->user, &cctx->users);
 	spin_unlock_irqrestore(&cctx->lock, flags);
+	kref_init(&fl->refcount);
 
 	return 0;
 }
Signature · 1 line
2.34.1
#1 lore raw
Quoted 3 lines · Anandu Krishnan E

Add reference counting using kref to the fastrpc_user structure to prevent use-after-free issues when contexts are freed from workqueue after device release.

Please follow
https://docs.kernel.org/process/submitting-patches.html#describe-your-changes
and start your commit message by clearly establishing the problem, once that's done you can describe the technical solution.

Quoted 5 lines

The issue occurs when fastrpc_device_release() frees the user structure while invoke contexts are still pending in the workqueue. When the workqueue later calls fastrpc_context_free(), it attempts to access buf->fl->cctx in fastrpc_buf_free(), leading to a use-after-free:

But why does it do that?

The reason why we need buf->fl->cctx in this context is because we need to mask out the DMA address from the buf->dma_addr (remove the SID bits).

If we instead split "dma_addr" into two separate members of struct fastrpc_buf, one dma_addr_t dma_addr and one u64 iova_with_sid (?), then it would be clear throughout the driver which address space we're talking about (is it the SID-adjusted iova space or the dma_addr_t in the particular DMA context).

In addition to making this aspect of the driver easier to follow, you no longer need to call fastrpc_ipa_to_dma_addr() in fastrpc_buf_free() (or anywhere else for that matter).

You can just pass buf->dma_addr directly to dma_free_coherent().

You're isolating the fact that the firmware needs to see "SID | dma_addr" to just two places in the driver.

Quoted 9 lines
  pc : fastrpc_buf_free+0x38/0x80 [fastrpc]
  lr : fastrpc_context_free+0xa8/0x1b0 [fastrpc]
  ...
  fastrpc_context_free+0xa8/0x1b0 [fastrpc]
  fastrpc_context_put_wq+0x78/0xa0 [fastrpc]
  process_one_work+0x180/0x450
  worker_thread+0x26c/0x388
Quoted 5 lines

Implement proper reference counting to fix this:
- Initialize kref in fastrpc_device_open()
- Take a reference in fastrpc_context_alloc() for each context
- Release the reference in fastrpc_context_free() when context is freed
- Release the initial reference in fastrpc_device_release()

There's no reason to include a checklist of pseudo-code in the commit message, describe why and the overall design if this isn't obvious.

Quoted 4 lines

This ensures the user structure remains valid as long as there are contexts holding references to it, preventing the race condition.

The life cycles at play in this driver is already very hard to reason about.

Quoted 21 lines
Fixes 6cffd79504ce ("misc: fastrpc: Add support for dmabuf exporter")
---
 drivers/misc/fastrpc.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
drivers/misc/fastrpc.c +2 -0
index 47356a5d5804..3ababcf327d7 100644
@@ -310,6 +310,8 @@ struct fastrpc_user {
 	spinlock_t lock;
 	/* lock for allocations */
 	struct mutex mutex;
+	/* Reference count */
+	struct kref refcount;
 };
 
 /* Extract SMMU PA from consolidated IOVA */

drivers/misc/fastrpc.c static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx) · line 504

Quoted 6 lines
drivers/misc/fastrpc.c +3 -0
@@ -497,15 +499,36 @@ static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
 	kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
 }
 
+static void fastrpc_user_free(struct kref *ref)
+{
+	struct fastrpc_user *fl = container_of(ref, struct fastrpc_user, refcount);

Unrelated question, what does the "fl" abbreviation actually mean? I presume 'f' is for "fastrpc", but what is 'l'?

Regards,
Bjorn

Quoted 82 lines
drivers/misc/fastrpc.c +26 -4
+
+	fastrpc_channel_ctx_put(fl->cctx);
+	mutex_destroy(&fl->mutex);
+	kfree(fl);
+}
+
+static void fastrpc_user_get(struct fastrpc_user *fl)
+{
+	kref_get(&fl->refcount);
+}
+
+static void fastrpc_user_put(struct fastrpc_user *fl)
+{
+	kref_put(&fl->refcount, fastrpc_user_free);
+}
+
 static void fastrpc_context_free(struct kref *ref)
 {
 	struct fastrpc_invoke_ctx *ctx;
 	struct fastrpc_channel_ctx *cctx;
+	struct fastrpc_user *fl;
 	unsigned long flags;
 	int i;
 
 	ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
 	cctx = ctx->cctx;
+	fl = ctx->fl;
 
 	for (i = 0; i < ctx->nbufs; i++)
 		fastrpc_map_put(ctx->maps[i]);
@@ -521,6 +544,8 @@ static void fastrpc_context_free(struct kref *ref)
 	kfree(ctx->olaps);
 	kfree(ctx);
 
+	/* Release the reference taken in fastrpc_context_alloc() */
+	fastrpc_user_put(fl);
 	fastrpc_channel_ctx_put(cctx);
 }
 
@@ -628,6 +653,8 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
 
 	/* Released in fastrpc_context_put() */
 	fastrpc_channel_ctx_get(cctx);
+	/* Take a reference to user, released in fastrpc_context_free() */
+	fastrpc_user_get(user);
 
 	ctx->sc = sc;
 	ctx->retval = -1;
@@ -658,6 +685,7 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
 	spin_lock(&user->lock);
 	list_del(&ctx->node);
 	spin_unlock(&user->lock);
+	fastrpc_user_put(user);
 	fastrpc_channel_ctx_put(cctx);
 	kfree(ctx->maps);
 	kfree(ctx->olaps);
@@ -1606,11 +1634,9 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)
 	}
 
 	fastrpc_session_free(cctx, fl->sctx);
-	fastrpc_channel_ctx_put(cctx);
-
-	mutex_destroy(&fl->mutex);
-	kfree(fl);
 	file->private_data = NULL;
+	/* Release the reference taken in fastrpc_device_open */
+	fastrpc_user_put(fl);
 
 	return 0;
 }
@@ -1654,6 +1680,7 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp)
 	spin_lock_irqsave(&cctx->lock, flags);
 	list_add_tail(&fl->user, &cctx->users);
 	spin_unlock_irqrestore(&cctx->lock, flags);
+	kref_init(&fl->refcount);
 
 	return 0;
 }
Signature · 1 line
2.34.1
#2 lore raw
Quoted 8 lines · Bjorn Andersson
Quoted 3 lines · Anandu Krishnan E

Add reference counting using kref to the fastrpc_user structure to prevent use-after-free issues when contexts are freed from workqueue after device release.

Please follow
https://docs.kernel.org/process/submitting-patches.html#describe-your-changes
and start your commit message by clearly establishing the problem, once that's done you can describe the technical solution.

sure,  will update the commit message and send as patch v2.

Quoted 16 lines
Quoted 5 lines

The issue occurs when fastrpc_device_release() frees the user structure while invoke contexts are still pending in the workqueue. When the workqueue later calls fastrpc_context_free(), it attempts to access buf->fl->cctx in fastrpc_buf_free(), leading to a use-after-free:

But why does it do that?

The reason why we need buf->fl->cctx in this context is because we need to mask out the DMA address from the buf->dma_addr (remove the SID bits).

If we instead split "dma_addr" into two separate members of struct fastrpc_buf, one dma_addr_t dma_addr and one u64 iova_with_sid (?), then it would be clear throughout the driver which address space we're talking about (is it the SID-adjusted iova space or the dma_addr_t in the particular DMA context).

Quoted 8 lines

In addition to making this aspect of the driver easier to follow, you no longer need to call fastrpc_ipa_to_dma_addr() in fastrpc_buf_free() (or anywhere else for that matter).

You can just pass buf->dma_addr directly to dma_free_coherent().

You're isolating the fact that the firmware needs to see "SID | dma_addr" to just two places in the driver.

I agree with the refactoring direction you’re suggesting, and separating the address spaces does make the driver easier to reason about.

That said, the UAF isn’t limited to the buffer
free path. fastrpc_context_free() also calls fastrpc_map_put(), which reaches fastrpc_free_map() and still dereferences fl, so addressing only the buffer side doesn’t fully resolve the lifetime issue. So the reference counting is still needed. I will update the scenario in commit message as well.

If you think it makes sense, I can take the address‑space refactoring as a separate follow‑up patch and keep this change focused on fixing the lifetime issue.

Quoted 9 lines
Quoted 9 lines
   pc : fastrpc_buf_free+0x38/0x80 [fastrpc]
   lr : fastrpc_context_free+0xa8/0x1b0 [fastrpc]
   ...
   fastrpc_context_free+0xa8/0x1b0 [fastrpc]
   fastrpc_context_put_wq+0x78/0xa0 [fastrpc]
   process_one_work+0x180/0x450
   worker_thread+0x26c/0x388
Quoted 7 lines
Quoted 5 lines

Implement proper reference counting to fix this:
- Initialize kref in fastrpc_device_open()
- Take a reference in fastrpc_context_alloc() for each context
- Release the reference in fastrpc_context_free() when context is freed
- Release the initial reference in fastrpc_device_release()

There's no reason to include a checklist of pseudo-code in the commit message, describe why and the overall design if this isn't obvious.

sure, will remove.

Quoted 31 lines
Quoted 4 lines

This ensures the user structure remains valid as long as there are contexts holding references to it, preventing the race condition.

The life cycles at play in this driver is already very hard to reason about.

Quoted 24 lines
Fixes 6cffd79504ce ("misc: fastrpc: Add support for dmabuf exporter")
---
  drivers/misc/fastrpc.c | 35 +++++++++++++++++++++++++++++++----
  1 file changed, 31 insertions(+), 4 deletions(-)
drivers/misc/fastrpc.c +2 -0
index 47356a5d5804..3ababcf327d7 100644
@@ -310,6 +310,8 @@ struct fastrpc_user {
  	spinlock_t lock;
  	/* lock for allocations */
  	struct mutex mutex;
+	/* Reference count */
+	struct kref refcount;
  };
 
  /* Extract SMMU PA from consolidated IOVA */
@@ -497,15 +499,36 @@ static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
  	kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
  }
 
Quoted 5 lines

drivers/misc/fastrpc.c static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx) · line 504

Quoted 3 lines
drivers/misc/fastrpc.c +3 -0
+static void fastrpc_user_free(struct kref *ref)
+{
+	struct fastrpc_user *fl = container_of(ref, struct fastrpc_user, refcount);

Unrelated question, what does the "fl" abbreviation actually mean? I presume 'f' is for "fastrpc", but what is 'l'?

fl is short for fastrpc file.

Regards,
Anandu

Quoted 85 lines

Regards,
Bjorn

Quoted 82 lines
drivers/misc/fastrpc.c +26 -4
+
+	fastrpc_channel_ctx_put(fl->cctx);
+	mutex_destroy(&fl->mutex);
+	kfree(fl);
+}
+
+static void fastrpc_user_get(struct fastrpc_user *fl)
+{
+	kref_get(&fl->refcount);
+}
+
+static void fastrpc_user_put(struct fastrpc_user *fl)
+{
+	kref_put(&fl->refcount, fastrpc_user_free);
+}
+
  static void fastrpc_context_free(struct kref *ref)
  {
  	struct fastrpc_invoke_ctx *ctx;
  	struct fastrpc_channel_ctx *cctx;
+	struct fastrpc_user *fl;
  	unsigned long flags;
  	int i;
 
  	ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
  	cctx = ctx->cctx;
+	fl = ctx->fl;
 
  	for (i = 0; i < ctx->nbufs; i++)
  		fastrpc_map_put(ctx->maps[i]);
@@ -521,6 +544,8 @@ static void fastrpc_context_free(struct kref *ref)
  	kfree(ctx->olaps);
  	kfree(ctx);
 
+	/* Release the reference taken in fastrpc_context_alloc() */
+	fastrpc_user_put(fl);
  	fastrpc_channel_ctx_put(cctx);
  }
 
@@ -628,6 +653,8 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
 
  	/* Released in fastrpc_context_put() */
  	fastrpc_channel_ctx_get(cctx);
+	/* Take a reference to user, released in fastrpc_context_free() */
+	fastrpc_user_get(user);
 
  	ctx->sc = sc;
  	ctx->retval = -1;
@@ -658,6 +685,7 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
  	spin_lock(&user->lock);
  	list_del(&ctx->node);
  	spin_unlock(&user->lock);
+	fastrpc_user_put(user);
  	fastrpc_channel_ctx_put(cctx);
  	kfree(ctx->maps);
  	kfree(ctx->olaps);
@@ -1606,11 +1634,9 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)
  	}
 
  	fastrpc_session_free(cctx, fl->sctx);
-	fastrpc_channel_ctx_put(cctx);
-
-	mutex_destroy(&fl->mutex);
-	kfree(fl);
  	file->private_data = NULL;
+	/* Release the reference taken in fastrpc_device_open */
+	fastrpc_user_put(fl);
 
  	return 0;
  }
@@ -1654,6 +1680,7 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp)
  	spin_lock_irqsave(&cctx->lock, flags);
  	list_add_tail(&fl->user, &cctx->users);
  	spin_unlock_irqrestore(&cctx->lock, flags);
+	kref_init(&fl->refcount);
 
  	return 0;
  }
Signature · 1 line
2.34.1
#3 lore raw
Quoted 40 lines · Anandu Krishnan E
Quoted 8 lines · Bjorn Andersson
Quoted 3 lines · Anandu Krishnan E

Add reference counting using kref to the fastrpc_user structure to prevent use-after-free issues when contexts are freed from workqueue after device release.

Please follow
https://docs.kernel.org/process/submitting-patches.html#describe-your-changes
and start your commit message by clearly establishing the problem, once that's done you can describe the technical solution.

sure,  will update the commit message and send as patch v2.

Quoted 16 lines
Quoted 5 lines

The issue occurs when fastrpc_device_release() frees the user structure while invoke contexts are still pending in the workqueue. When the workqueue later calls fastrpc_context_free(), it attempts to access buf->fl->cctx in fastrpc_buf_free(), leading to a use-after-free:

But why does it do that?

The reason why we need buf->fl->cctx in this context is because we need to mask out the DMA address from the buf->dma_addr (remove the SID bits).

If we instead split "dma_addr" into two separate members of struct fastrpc_buf, one dma_addr_t dma_addr and one u64 iova_with_sid (?), then it would be clear throughout the driver which address space we're talking about (is it the SID-adjusted iova space or the dma_addr_t in the particular DMA context).

Quoted 8 lines

In addition to making this aspect of the driver easier to follow, you no longer need to call fastrpc_ipa_to_dma_addr() in fastrpc_buf_free() (or anywhere else for that matter).

You can just pass buf->dma_addr directly to dma_free_coherent().

You're isolating the fact that the firmware needs to see "SID | dma_addr" to just two places in the driver.

I agree with the refactoring direction you’re suggesting, and separating the address spaces does make the driver easier to reason about.

Quoted 7 lines

That said, the UAF isn’t limited to the buffer
free path. fastrpc_context_free() also calls fastrpc_map_put(), which reaches fastrpc_free_map() and still dereferences fl, so addressing only the buffer side doesn’t fully resolve the lifetime issue. So the reference counting is still needed. I will update the scenario in commit message as well.

I presume you're referring to the "vmid" we need to rebuild the src_perms for use in fastrpc_free_map()?

I think the relevant question to ask there is if it's really a property of the "fastrpc file context". It seems to me that we could solve that by storing the src_perms in the fastrpc_map once we've done the qcom_scm_assign_mem() call in fastrpc_map_attach() - so that we can free that object without having to reach out to objects of other lifetimes.

Quoted 3 lines

If you think it makes sense, I can take the address‑space refactoring as a separate follow‑up patch and keep this change focused on fixing the lifetime issue.

The chance of you fixing one lifetime issue by introducing one or more worries me, I'm only familiar with the driver, so I wouldn't be able to say with confidence without investing more time fully understand the various lifetimes. So if we're going that path, I'd like someone else to step up and tell me that it's good.

On the other hand, the two changes I presented above are logically simple to make, follow, and review - and they don't complicate the driver further. So that would still be _my_ preferred choice.

[..]

Quoted 20 lines
Quoted 12 lines
Quoted 12 lines
drivers/misc/fastrpc.c +2 -0
index 47356a5d5804..3ababcf327d7 100644
@@ -310,6 +310,8 @@ struct fastrpc_user {
  	spinlock_t lock;
  	/* lock for allocations */
  	struct mutex mutex;
+	/* Reference count */
+	struct kref refcount;
  };
  /* Extract SMMU PA from consolidated IOVA */
Quoted 8 lines

drivers/misc/fastrpc.c static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx) · line 503

Quoted 6 lines
drivers/misc/fastrpc.c +3 -0
@@ -497,15 +499,36 @@ static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
  	kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
  }
+static void fastrpc_user_free(struct kref *ref)
+{
+	struct fastrpc_user *fl = container_of(ref, struct fastrpc_user, refcount);

Unrelated question, what does the "fl" abbreviation actually mean? I presume 'f' is for "fastrpc", but what is 'l'?

Quoted 2 lines

fl is short for fastrpc file.

Hmm, okay. Thank you for clarifying.

Regards,
Bjorn

Quoted 79 lines

Regards,
Anandu

Quoted 76 lines

Regards,
Bjorn

Quoted 73 lines
drivers/misc/fastrpc.c +26 -4
+
+	fastrpc_channel_ctx_put(fl->cctx);
+	mutex_destroy(&fl->mutex);
+	kfree(fl);
+}
+
+static void fastrpc_user_get(struct fastrpc_user *fl)
+{
+	kref_get(&fl->refcount);
+}
+
+static void fastrpc_user_put(struct fastrpc_user *fl)
+{
+	kref_put(&fl->refcount, fastrpc_user_free);
+}
+
  static void fastrpc_context_free(struct kref *ref)
  {
  	struct fastrpc_invoke_ctx *ctx;
  	struct fastrpc_channel_ctx *cctx;
+	struct fastrpc_user *fl;
  	unsigned long flags;
  	int i;
  	ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
  	cctx = ctx->cctx;
+	fl = ctx->fl;
  	for (i = 0; i < ctx->nbufs; i++)
  		fastrpc_map_put(ctx->maps[i]);
@@ -521,6 +544,8 @@ static void fastrpc_context_free(struct kref *ref)
  	kfree(ctx->olaps);
  	kfree(ctx);
+	/* Release the reference taken in fastrpc_context_alloc() */
+	fastrpc_user_put(fl);
  	fastrpc_channel_ctx_put(cctx);
  }
@@ -628,6 +653,8 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
  	/* Released in fastrpc_context_put() */
  	fastrpc_channel_ctx_get(cctx);
+	/* Take a reference to user, released in fastrpc_context_free() */
+	fastrpc_user_get(user);
  	ctx->sc = sc;
  	ctx->retval = -1;
@@ -658,6 +685,7 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
  	spin_lock(&user->lock);
  	list_del(&ctx->node);
  	spin_unlock(&user->lock);
+	fastrpc_user_put(user);
  	fastrpc_channel_ctx_put(cctx);
  	kfree(ctx->maps);
  	kfree(ctx->olaps);
@@ -1606,11 +1634,9 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)
  	}
  	fastrpc_session_free(cctx, fl->sctx);
-	fastrpc_channel_ctx_put(cctx);
-
-	mutex_destroy(&fl->mutex);
-	kfree(fl);
  	file->private_data = NULL;
+	/* Release the reference taken in fastrpc_device_open */
+	fastrpc_user_put(fl);
  	return 0;
  }
@@ -1654,6 +1680,7 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp)
  	spin_lock_irqsave(&cctx->lock, flags);
  	list_add_tail(&fl->user, &cctx->users);
  	spin_unlock_irqrestore(&cctx->lock, flags);
+	kref_init(&fl->refcount);
  	return 0;
  }
Signature · 1 line
2.34.1
#4 lore raw
Quoted 64 lines · Bjorn Andersson
Quoted 38 lines · Anandu Krishnan E
Quoted 8 lines · Bjorn Andersson
Quoted 3 lines · Anandu Krishnan E

Add reference counting using kref to the fastrpc_user structure to prevent use-after-free issues when contexts are freed from workqueue after device release.

Please follow
https://docs.kernel.org/process/submitting-patches.html#describe-your-changes
and start your commit message by clearly establishing the problem, once that's done you can describe the technical solution.

sure,  will update the commit message and send as patch v2.

Quoted 15 lines
Quoted 4 lines

The issue occurs when fastrpc_device_release() frees the user structure while invoke contexts are still pending in the workqueue. When the workqueue later calls fastrpc_context_free(), it attempts to access buf->fl->cctx in fastrpc_buf_free(), leading to a use-after-free:

But why does it do that?

The reason why we need buf->fl->cctx in this context is because we need to mask out the DMA address from the buf->dma_addr (remove the SID bits).

If we instead split "dma_addr" into two separate members of struct fastrpc_buf, one dma_addr_t dma_addr and one u64 iova_with_sid (?), then it would be clear throughout the driver which address space we're talking about (is it the SID-adjusted iova space or the dma_addr_t in the particular DMA context).

Quoted 8 lines

In addition to making this aspect of the driver easier to follow, you no longer need to call fastrpc_ipa_to_dma_addr() in fastrpc_buf_free() (or anywhere else for that matter).

You can just pass buf->dma_addr directly to dma_free_coherent().

You're isolating the fact that the firmware needs to see "SID | dma_addr" to just two places in the driver.

I agree with the refactoring direction you’re suggesting, and separating the address spaces does make the driver easier to reason about.

Quoted 7 lines

That said, the UAF isn’t limited to the buffer
free path. fastrpc_context_free() also calls fastrpc_map_put(), which reaches fastrpc_free_map() and still dereferences fl, so addressing only the buffer side doesn’t fully resolve the lifetime issue. So the reference counting is still needed. I will update the scenario in commit message as well.

I presume you're referring to the "vmid" we need to rebuild the src_perms for use in fastrpc_free_map()?

I think the relevant question to ask there is if it's really a property of the "fastrpc file context". It seems to me that we could solve that by storing the src_perms in the fastrpc_map once we've done the qcom_scm_assign_mem() call in fastrpc_map_attach() - so that we can free that object without having to reach out to objects of other lifetimes.

Quoted 3 lines

If you think it makes sense, I can take the address‑space refactoring as a separate follow‑up patch and keep this change focused on fixing the lifetime issue.

The chance of you fixing one lifetime issue by introducing one or more worries me, I'm only familiar with the driver, so I wouldn't be able to say with confidence without investing more time fully understand the various lifetimes. So if we're going that path, I'd like someone else to step up and tell me that it's good.

Quoted 3 lines

On the other hand, the two changes I presented above are logically simple to make, follow, and review - and they don't complicate the driver further. So that would still be _my_ preferred choice.

I understand your concerns about the ref count approach. But with the two above changes also UAF issue will still persist. In fastrpc_free_map() :

if (map->fl) {
    spin_lock(&map->fl->lock);
    list_del(&map->node);
    spin_unlock(&map->fl->lock);
    map->fl = NULL;
}

we are using above logic to remove map node from the list. Here also we are using fl->lock to manage the map list and this map is part of fl->maps list as well.

I suggested ref count change because it was handling all the scenarios we discussed above. I am open to any alternative suggestions as well.

Regards,
Anandu

Quoted 106 lines

[..]

Quoted 20 lines
Quoted 12 lines
Quoted 12 lines
drivers/misc/fastrpc.c +2 -0
index 47356a5d5804..3ababcf327d7 100644
@@ -310,6 +310,8 @@ struct fastrpc_user {
   	spinlock_t lock;
   	/* lock for allocations */
   	struct mutex mutex;
+	/* Reference count */
+	struct kref refcount;
   };
   /* Extract SMMU PA from consolidated IOVA */
Quoted 8 lines

drivers/misc/fastrpc.c static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx) · line 503

Quoted 6 lines
drivers/misc/fastrpc.c +3 -0
@@ -497,15 +499,36 @@ static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
   	kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
   }
+static void fastrpc_user_free(struct kref *ref)
+{
+	struct fastrpc_user *fl = container_of(ref, struct fastrpc_user, refcount);

Unrelated question, what does the "fl" abbreviation actually mean? I presume 'f' is for "fastrpc", but what is 'l'?

Quoted 2 lines

fl is short for fastrpc file.

Hmm, okay. Thank you for clarifying.

Regards,
Bjorn

Quoted 77 lines

Regards,
Anandu

Quoted 75 lines

Regards,
Bjorn

Quoted 73 lines
drivers/misc/fastrpc.c +26 -4
+
+	fastrpc_channel_ctx_put(fl->cctx);
+	mutex_destroy(&fl->mutex);
+	kfree(fl);
+}
+
+static void fastrpc_user_get(struct fastrpc_user *fl)
+{
+	kref_get(&fl->refcount);
+}
+
+static void fastrpc_user_put(struct fastrpc_user *fl)
+{
+	kref_put(&fl->refcount, fastrpc_user_free);
+}
+
   static void fastrpc_context_free(struct kref *ref)
   {
   	struct fastrpc_invoke_ctx *ctx;
   	struct fastrpc_channel_ctx *cctx;
+	struct fastrpc_user *fl;
   	unsigned long flags;
   	int i;
   	ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
   	cctx = ctx->cctx;
+	fl = ctx->fl;
   	for (i = 0; i < ctx->nbufs; i++)
   		fastrpc_map_put(ctx->maps[i]);
@@ -521,6 +544,8 @@ static void fastrpc_context_free(struct kref *ref)
   	kfree(ctx->olaps);
   	kfree(ctx);
+	/* Release the reference taken in fastrpc_context_alloc() */
+	fastrpc_user_put(fl);
   	fastrpc_channel_ctx_put(cctx);
   }
@@ -628,6 +653,8 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
   	/* Released in fastrpc_context_put() */
   	fastrpc_channel_ctx_get(cctx);
+	/* Take a reference to user, released in fastrpc_context_free() */
+	fastrpc_user_get(user);
   	ctx->sc = sc;
   	ctx->retval = -1;
@@ -658,6 +685,7 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
   	spin_lock(&user->lock);
   	list_del(&ctx->node);
   	spin_unlock(&user->lock);
+	fastrpc_user_put(user);
   	fastrpc_channel_ctx_put(cctx);
   	kfree(ctx->maps);
   	kfree(ctx->olaps);
@@ -1606,11 +1634,9 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)
   	}
   	fastrpc_session_free(cctx, fl->sctx);
-	fastrpc_channel_ctx_put(cctx);
-
-	mutex_destroy(&fl->mutex);
-	kfree(fl);
   	file->private_data = NULL;
+	/* Release the reference taken in fastrpc_device_open */
+	fastrpc_user_put(fl);
   	return 0;
   }
@@ -1654,6 +1680,7 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp)
   	spin_lock_irqsave(&cctx->lock, flags);
   	list_add_tail(&fl->user, &cctx->users);
   	spin_unlock_irqrestore(&cctx->lock, flags);
+	kref_init(&fl->refcount);
   	return 0;
   }
Signature · 1 line
2.34.1
#5 lore raw
Quoted 87 lines · Anandu Krishnan E
Quoted 65 lines · Bjorn Andersson
Quoted 39 lines · Anandu Krishnan E
Quoted 9 lines · Bjorn Andersson
Quoted 3 lines · Anandu Krishnan E

Add reference counting using kref to the fastrpc_user structure to prevent use-after-free issues when contexts are freed from workqueue after device release.

Please follow
https://docs.kernel.org/process/submitting-patches.html#describe-your-changes

and start your commit message by clearly establishing the problem, once that's done you can describe the technical solution.

sure,  will update the commit message and send as patch v2.

Quoted 15 lines
Quoted 4 lines

The issue occurs when fastrpc_device_release() frees the user structure while invoke contexts are still pending in the workqueue. When the workqueue later calls fastrpc_context_free(), it attempts to access buf->fl->cctx in fastrpc_buf_free(), leading to a use-after-free:

But why does it do that?

The reason why we need buf->fl->cctx in this context is because we need to mask out the DMA address from the buf->dma_addr (remove the SID bits).

If we instead split "dma_addr" into two separate members of struct fastrpc_buf, one dma_addr_t dma_addr and one u64 iova_with_sid (?), then it would be clear throughout the driver which address space we're talking about (is it the SID-adjusted iova space or the dma_addr_t in the particular DMA context).

Quoted 8 lines

In addition to making this aspect of the driver easier to follow, you no longer need to call fastrpc_ipa_to_dma_addr() in fastrpc_buf_free() (or anywhere else for that matter).

You can just pass buf->dma_addr directly to dma_free_coherent().

You're isolating the fact that the firmware needs to see "SID | dma_addr" to just two places in the driver.

I agree with the refactoring direction you’re suggesting, and separating the address spaces does make the driver easier to reason about.

Quoted 7 lines

That said, the UAF isn’t limited to the buffer
free path. fastrpc_context_free() also calls fastrpc_map_put(), which reaches fastrpc_free_map() and still dereferences fl, so addressing only the buffer side doesn’t fully resolve the lifetime issue. So the reference counting is still needed. I will update the scenario in commit message as well.

I presume you're referring to the "vmid" we need to rebuild the src_perms for use in fastrpc_free_map()?

I think the relevant question to ask there is if it's really a property of the "fastrpc file context". It seems to me that we could solve that by storing the src_perms in the fastrpc_map once we've done the qcom_scm_assign_mem() call in fastrpc_map_attach() - so that we can free that object without having to reach out to objects of other lifetimes.

Quoted 3 lines

If you think it makes sense, I can take the address‑space refactoring as a separate follow‑up patch and keep this change focused on fixing the lifetime issue.

The chance of you fixing one lifetime issue by introducing one or more worries me, I'm only familiar with the driver, so I wouldn't be able to say with confidence without investing more time fully understand the various lifetimes. So if we're going that path, I'd like someone else to step up and tell me that it's good.

Quoted 3 lines

On the other hand, the two changes I presented above are logically simple to make, follow, and review - and they don't complicate the driver further. So that would still be _my_ preferred choice.

I understand your concerns about the ref count approach. But with the two above changes also UAF issue will still persist. In fastrpc_free_map() :

if (map->fl) {
    spin_lock(&map->fl->lock);
    list_del(&map->node);
    spin_unlock(&map->fl->lock);
    map->fl = NULL;
}

we are using above logic to remove map node from the list. Here also we are using fl->lock to manage the map list and this map is part of fl->maps list as well.

Quoted 6 lines

I suggested ref count change because it was handling all the scenarios we discussed above. I am open to any alternative suggestions as well.

Regards,
Anandu

Just a quick follow-up on my earlier reply.

My concern was that even with the two changes you suggested, we still end up dereferencing fl->lock in fastrpc_free_map() when removing the map from fl->maps, which could still result in a UAF.

Since the reference-count approach addressed all the scenarios we discussed, I wanted to check whether you’re okay with proceeding in that direction. If you have an alternative suggestion, I’d be happy to take that up. Alternatively, if there are specific issues you see with the refcount approach, I can review and address them from my side.

- Anandu

Quoted 116 lines
Quoted 115 lines

[..]

Quoted 22 lines
Quoted 14 lines
Quoted 14 lines
drivers/misc/fastrpc.c +2 -0
index 47356a5d5804..3ababcf327d7 100644
@@ -310,6 +310,8 @@ struct fastrpc_user {
        spinlock_t lock;
        /* lock for allocations */
        struct mutex mutex;
+    /* Reference count */
+    struct kref refcount;
    };
    /* Extract SMMU PA from consolidated IOVA */
@@ -497,15 +499,36 @@ static void fastrpc_channel_ctx_put(struct 
 fastrpc_channel_ctx *cctx)
Quoted 8 lines

drivers/misc/fastrpc.c static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)

Quoted 6 lines
drivers/misc/fastrpc.c +3 -0
        kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
    }
+static void fastrpc_user_free(struct kref *ref)
+{
+    struct fastrpc_user *fl = container_of(ref, struct 
 fastrpc_user, refcount);

Unrelated question, what does the "fl" abbreviation actually mean? I presume 'f' is for "fastrpc", but what is 'l'?

Quoted 2 lines

fl is short for fastrpc file.

Hmm, okay. Thank you for clarifying.

Regards,
Bjorn

Quoted 83 lines

Regards,
Anandu

Quoted 81 lines

Regards,
Bjorn

Quoted 79 lines
drivers/misc/fastrpc.c +21 -0
+
+    fastrpc_channel_ctx_put(fl->cctx);
+    mutex_destroy(&fl->mutex);
+    kfree(fl);
+}
+
+static void fastrpc_user_get(struct fastrpc_user *fl)
+{
+    kref_get(&fl->refcount);
+}
+
+static void fastrpc_user_put(struct fastrpc_user *fl)
+{
+    kref_put(&fl->refcount, fastrpc_user_free);
+}
+
    static void fastrpc_context_free(struct kref *ref)
    {
        struct fastrpc_invoke_ctx *ctx;
        struct fastrpc_channel_ctx *cctx;
+    struct fastrpc_user *fl;
        unsigned long flags;
        int i;
        ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
        cctx = ctx->cctx;
+    fl = ctx->fl;
        for (i = 0; i < ctx->nbufs; i++)
            fastrpc_map_put(ctx->maps[i]);
@@ -521,6 +544,8 @@ static void fastrpc_context_free(struct kref 
 *ref)
        kfree(ctx->olaps);
        kfree(ctx);
+    /* Release the reference taken in fastrpc_context_alloc() */
+    fastrpc_user_put(fl);
        fastrpc_channel_ctx_put(cctx);
    }
@@ -628,6 +653,8 @@ static struct fastrpc_invoke_ctx 
 *fastrpc_context_alloc(
        /* Released in fastrpc_context_put() */
        fastrpc_channel_ctx_get(cctx);
+    /* Take a reference to user, released in 

fastrpc_context_free() */
+    fastrpc_user_get(user);
       ctx->sc = sc;
       ctx->retval = -1;

drivers/misc/fastrpc.c +1 -0
@@ -658,6 +685,7 @@ static struct fastrpc_invoke_ctx 
 *fastrpc_context_alloc(
        spin_lock(&user->lock);
        list_del(&ctx->node);
        spin_unlock(&user->lock);
+    fastrpc_user_put(user);
        fastrpc_channel_ctx_put(cctx);
        kfree(ctx->maps);

       kfree(ctx->olaps);

drivers/misc/fastrpc.c +3 -4
@@ -1606,11 +1634,9 @@ static int fastrpc_device_release(struct 
 inode *inode, struct file *file)
        }
        fastrpc_session_free(cctx, fl->sctx);
-    fastrpc_channel_ctx_put(cctx);
-
-    mutex_destroy(&fl->mutex);
-    kfree(fl);
        file->private_data = NULL;
+    /* Release the reference taken in fastrpc_device_open */
+    fastrpc_user_put(fl);
        return 0;
    }
@@ -1654,6 +1680,7 @@ static int fastrpc_device_open(struct inode 
 *inode, struct file *filp)
        spin_lock_irqsave(&cctx->lock, flags);
        list_add_tail(&fl->user, &cctx->users);
        spin_unlock_irqrestore(&cctx->lock, flags);
+    kref_init(&fl->refcount);
        return 0;
    }
Signature · 1 line
2.34.1
#6 lore raw
In reply to Anandu Krishnan E
Quoted 8 lines · Anandu Krishnan E

Add reference counting using kref to the fastrpc_user structure to prevent use-after-free issues when contexts are freed from workqueue after device release.

The issue occurs when fastrpc_device_release() frees the user structure while invoke contexts are still pending in the workqueue. When the workqueue later calls fastrpc_context_free(), it attempts to access buf->fl->cctx in fastrpc_buf_free(), leading to a use-after-free:

I guess these are some of the pending invoke requests that are still processing and receive callback after user closes the device.

Quoted 102 lines
  pc : fastrpc_buf_free+0x38/0x80 [fastrpc]
  lr : fastrpc_context_free+0xa8/0x1b0 [fastrpc]
  ...
  fastrpc_context_free+0xa8/0x1b0 [fastrpc]
  fastrpc_context_put_wq+0x78/0xa0 [fastrpc]
  process_one_work+0x180/0x450
  worker_thread+0x26c/0x388

Implement proper reference counting to fix this:
- Initialize kref in fastrpc_device_open()
- Take a reference in fastrpc_context_alloc() for each context
- Release the reference in fastrpc_context_free() when context is freed
- Release the initial reference in fastrpc_device_release()

This ensures the user structure remains valid as long as there are contexts holding references to it, preventing the race condition.

Fixes 6cffd79504ce ("misc: fastrpc: Add support for dmabuf exporter")
---
 drivers/misc/fastrpc.c | 35 +++++++++++++++++++++++++++++++----
 1 file changed, 31 insertions(+), 4 deletions(-)
drivers/misc/fastrpc.c +28 -0
index 47356a5d5804..3ababcf327d7 100644
@@ -310,6 +310,8 @@ struct fastrpc_user {
 	spinlock_t lock;
 	/* lock for allocations */
 	struct mutex mutex;
+	/* Reference count */
+	struct kref refcount;
 };
 
 /* Extract SMMU PA from consolidated IOVA */
@@ -497,15 +499,36 @@ static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
 	kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
 }
 
+static void fastrpc_user_free(struct kref *ref)
+{
+	struct fastrpc_user *fl = container_of(ref, struct fastrpc_user, refcount);
+
+	fastrpc_channel_ctx_put(fl->cctx);
+	mutex_destroy(&fl->mutex);
+	kfree(fl);
+}
+
+static void fastrpc_user_get(struct fastrpc_user *fl)
+{
+	kref_get(&fl->refcount);
+}
+
+static void fastrpc_user_put(struct fastrpc_user *fl)
+{
+	kref_put(&fl->refcount, fastrpc_user_free);
+}
+
 static void fastrpc_context_free(struct kref *ref)
 {
 	struct fastrpc_invoke_ctx *ctx;
 	struct fastrpc_channel_ctx *cctx;
+	struct fastrpc_user *fl;
 	unsigned long flags;
 	int i;
 
 	ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
 	cctx = ctx->cctx;
+	fl = ctx->fl;
 
 	for (i = 0; i < ctx->nbufs; i++)
 		fastrpc_map_put(ctx->maps[i]);
@@ -521,6 +544,8 @@ static void fastrpc_context_free(struct kref *ref)
 	kfree(ctx->olaps);
 	kfree(ctx);
 
+	/* Release the reference taken in fastrpc_context_alloc() */
+	fastrpc_user_put(fl);
 	fastrpc_channel_ctx_put(cctx);
 }
 
@@ -628,6 +653,8 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
 
 	/* Released in fastrpc_context_put() */
 	fastrpc_channel_ctx_get(cctx);
+	/* Take a reference to user, released in fastrpc_context_free() */
+	fastrpc_user_get(user);
 
 	ctx->sc = sc;
 	ctx->retval = -1;
@@ -658,6 +685,7 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
 	spin_lock(&user->lock);
 	list_del(&ctx->node);
 	spin_unlock(&user->lock);
+	fastrpc_user_put(user);
 	fastrpc_channel_ctx_put(cctx);
 	kfree(ctx->maps);
 	kfree(ctx->olaps);

drivers/misc/fastrpc.c static int fastrpc_device_release(struct inode *inode, struct file *file) · line 1639

Quoted 10 lines
drivers/misc/fastrpc.c +2 -4
@@ -1606,11 +1634,9 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)
 	}
 
 	fastrpc_session_free(cctx, fl->sctx);
-	fastrpc_channel_ctx_put(cctx);
-
-	mutex_destroy(&fl->mutex);
-	kfree(fl);
 	file->private_data = NULL;
+	/* Release the reference taken in fastrpc_device_open */
+	fastrpc_user_put(fl);

Now that we are trying to handle(wait) for the cases where wq will get callbacks after the fd is closed, how are we making sure that memory associated with these invoke, init memory and mmaps are not released?

Also now that we have a refcount for the fastrpc_user, is it possible to
remove the refcount on the context, as this seems redundant now, we
could just use the user refcount instead. May be other patch..

--srini

Quoted 11 lines
drivers/misc/fastrpc.c +1 -0
 	return 0;
 }
@@ -1654,6 +1680,7 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp)
 	spin_lock_irqsave(&cctx->lock, flags);
 	list_add_tail(&fl->user, &cctx->users);
 	spin_unlock_irqrestore(&cctx->lock, flags);
+	kref_init(&fl->refcount);
 
 	return 0;
 }
#7 lore raw
Quoted 116 lines · Srinivas Kandagatla
Quoted 8 lines · Anandu Krishnan E

Add reference counting using kref to the fastrpc_user structure to prevent use-after-free issues when contexts are freed from workqueue after device release.

The issue occurs when fastrpc_device_release() frees the user structure while invoke contexts are still pending in the workqueue. When the workqueue later calls fastrpc_context_free(), it attempts to access buf->fl->cctx in fastrpc_buf_free(), leading to a use-after-free:

I guess these are some of the pending invoke requests that are still processing and receive callback after user closes the device.

Quoted 103 lines
   pc : fastrpc_buf_free+0x38/0x80 [fastrpc]
   lr : fastrpc_context_free+0xa8/0x1b0 [fastrpc]
   ...
   fastrpc_context_free+0xa8/0x1b0 [fastrpc]
   fastrpc_context_put_wq+0x78/0xa0 [fastrpc]
   process_one_work+0x180/0x450
   worker_thread+0x26c/0x388

Implement proper reference counting to fix this:
- Initialize kref in fastrpc_device_open()
- Take a reference in fastrpc_context_alloc() for each context
- Release the reference in fastrpc_context_free() when context is freed
- Release the initial reference in fastrpc_device_release()

This ensures the user structure remains valid as long as there are contexts holding references to it, preventing the race condition.

Fixes 6cffd79504ce ("misc: fastrpc: Add support for dmabuf exporter")
---
  drivers/misc/fastrpc.c | 35 +++++++++++++++++++++++++++++++----
  1 file changed, 31 insertions(+), 4 deletions(-)
drivers/misc/fastrpc.c +28 -0
index 47356a5d5804..3ababcf327d7 100644
@@ -310,6 +310,8 @@ struct fastrpc_user {
  	spinlock_t lock;
  	/* lock for allocations */
  	struct mutex mutex;
+	/* Reference count */
+	struct kref refcount;
  };
 
  /* Extract SMMU PA from consolidated IOVA */
@@ -497,15 +499,36 @@ static void fastrpc_channel_ctx_put(struct fastrpc_channel_ctx *cctx)
  	kref_put(&cctx->refcount, fastrpc_channel_ctx_free);
  }
 
+static void fastrpc_user_free(struct kref *ref)
+{
+	struct fastrpc_user *fl = container_of(ref, struct fastrpc_user, refcount);
+
+	fastrpc_channel_ctx_put(fl->cctx);
+	mutex_destroy(&fl->mutex);
+	kfree(fl);
+}
+
+static void fastrpc_user_get(struct fastrpc_user *fl)
+{
+	kref_get(&fl->refcount);
+}
+
+static void fastrpc_user_put(struct fastrpc_user *fl)
+{
+	kref_put(&fl->refcount, fastrpc_user_free);
+}
+
  static void fastrpc_context_free(struct kref *ref)
  {
  	struct fastrpc_invoke_ctx *ctx;
  	struct fastrpc_channel_ctx *cctx;
+	struct fastrpc_user *fl;
  	unsigned long flags;
  	int i;
 
  	ctx = container_of(ref, struct fastrpc_invoke_ctx, refcount);
  	cctx = ctx->cctx;
+	fl = ctx->fl;
 
  	for (i = 0; i < ctx->nbufs; i++)
  		fastrpc_map_put(ctx->maps[i]);
@@ -521,6 +544,8 @@ static void fastrpc_context_free(struct kref *ref)
  	kfree(ctx->olaps);
  	kfree(ctx);
 
+	/* Release the reference taken in fastrpc_context_alloc() */
+	fastrpc_user_put(fl);
  	fastrpc_channel_ctx_put(cctx);
  }
 
@@ -628,6 +653,8 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
 
  	/* Released in fastrpc_context_put() */
  	fastrpc_channel_ctx_get(cctx);
+	/* Take a reference to user, released in fastrpc_context_free() */
+	fastrpc_user_get(user);
 
  	ctx->sc = sc;
  	ctx->retval = -1;
@@ -658,6 +685,7 @@ static struct fastrpc_invoke_ctx *fastrpc_context_alloc(
  	spin_lock(&user->lock);
  	list_del(&ctx->node);
  	spin_unlock(&user->lock);
+	fastrpc_user_put(user);
  	fastrpc_channel_ctx_put(cctx);
  	kfree(ctx->maps);
  	kfree(ctx->olaps);
@@ -1606,11 +1634,9 @@ static int fastrpc_device_release(struct inode *inode, struct file *file)
  	}
 
Quoted 11 lines

drivers/misc/fastrpc.c static int fastrpc_device_release(struct inode *inode, struct file *file) · line 1639

Quoted 8 lines
drivers/misc/fastrpc.c +2 -4
  	fastrpc_session_free(cctx, fl->sctx);
-	fastrpc_channel_ctx_put(cctx);
-
-	mutex_destroy(&fl->mutex);
-	kfree(fl);
  	file->private_data = NULL;
+	/* Release the reference taken in fastrpc_device_open */
+	fastrpc_user_put(fl);

Now that we are trying to handle(wait) for the cases where wq will get callbacks after the fd is closed, how are we making sure that memory associated with these invoke, init memory and mmaps are not released?

I will move the cleanups to fastrpc_user_free so that cleanup wont happen until

all user references are released

Quoted 5 lines

Also now that we have a refcount for the fastrpc_user, is it possible to
remove the refcount on the context, as this seems redundant now, we
could just use the user refcount instead. May be other patch..

I will check on this and if possible to remove, i will share a separate patch

-Anandu

Quoted 14 lines

--srini

Quoted 11 lines
drivers/misc/fastrpc.c +1 -0
  	return 0;
  }
@@ -1654,6 +1680,7 @@ static int fastrpc_device_open(struct inode *inode, struct file *filp)
  	spin_lock_irqsave(&cctx->lock, flags);
  	list_add_tail(&fl->user, &cctx->users);
  	spin_unlock_irqrestore(&cctx->lock, flags);
+	kref_init(&fl->refcount);
 
  	return 0;
  }