misc: fastrpc: fix UAF and kernel panic during cleanup on process abort
PATCH 1/1 misc: fastrpc: fix UAF and kernel panic during cleanup on process abort
When a userspace FastRPC client is abruptly terminated, FastRPC cleanup paths can race with device and session teardown.
This results in kernel panics in different release paths:
- fastrpc_release() when using remote heap, originating from fastrpc_buf_free()
- fastrpc_device_release() when using system heap, originating from fastrpc_free_map()
In addition, fastrpc_map_put() may trigger refcount use-after-free due to concurrent cleanup without proper synchronization.
The root cause is that buffer and map cleanup paths may access map and buf resources after the associated device or session has already been released.
Fix this by:
- Introducing mutex protection for map and buf lifetime
- Serializing buffer and map cleanup against device teardown
- Skipping buffer and map operations when the device is already gone
These changes ensure cleanup paths are safe against unexpected process aborts and prevent use-after-free and kernel panic scenarios.
--- drivers/misc/fastrpc.c | 56 ++++++++++++++++++++++++++++++++++++++---- 1 file changed, 51 insertions(+), 5 deletions(-)
drivers/misc/fastrpc.c +51 -5
@@ -255,6 +255,8 @@ struct fastrpc_session_ctx {int sid;bool used;bool valid; +bool allocated; +struct mutex mutex; };struct fastrpc_soc_data {
@@ -333,9 +335,14 @@ static inline u64 fastrpc_sid_offset(struct fastrpc_channel_ctx *cctx,static void fastrpc_free_map (struct kref *ref) {struct fastrpc_map *map; +struct fastrpc_user *fl; map = container_of(ref, struct fastrpc_map, refcount); + fl = map->fl; +if (!fl) +return ; +if (map->table) {if (map->attr &FASTRPC_ATTR_SECUREMAP ) {struct qcom_scm_vmperm perm;
@@ -354,10 +361,16 @@ static void fastrpc_free_map(struct kref *ref)return ; } } +mutex_lock (&fl->sctx->mutex); +if (!fl->sctx->dev) { +mutex_unlock (&fl->sctx->mutex); +return ; + }dma_buf_unmap_attachment_unlocked (map->attach, map->table,DMA_BIDIRECTIONAL );dma_buf_detach (map->buf, map->attach);dma_buf_put (map->buf); +mutex_unlock (&fl->sctx->mutex); }if (map->fl) {
@@ -414,9 +427,17 @@ static int fastrpc_map_lookup(struct fastrpc_user *fl, int fd,static void fastrpc_buf_free (struct fastrpc_buf *buf) { -dma_free_coherent (buf->dev, buf->size, buf->virt, -fastrpc_ipa_to_dma_addr (buf->fl->cctx, buf->dma_addr)); -kfree (buf); +struct fastrpc_user *fl = buf->fl; + +if (!fl) +return ; +mutex_lock (&fl->sctx->mutex); +if (fl->sctx->dev) { +dma_free_coherent (buf->dev, buf->size, buf->virt, +FASTRPC_PHYS (buf->phys)); +kfree (buf); + } +mutex_unlock (&fl->sctx->mutex); } static int __fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev,
@@ -439,8 +460,10 @@ static int __fastrpc_buf_alloc(struct fastrpc_user *fl, struct device *dev, buf->dev = dev; buf->raddr =0 ; - buf->virt =dma_alloc_coherent (dev, buf->size, &buf->dma_addr, -GFP_KERNEL ); +mutex_lock (&fl->sctx->mutex); +if (fl->sctx->dev) + buf->virt =dma_alloc_coherent (dev, buf->size, (dma_addr_t *)&buf->phys,GFP_KERNEL ); +mutex_unlock (&fl->sctx->mutex);if (!buf->virt) {mutex_destroy (&buf->lock);kfree (buf);
@@ -483,6 +506,10 @@ static void fastrpc_channel_ctx_free(struct kref *ref)struct fastrpc_channel_ctx *cctx; cctx = container_of(ref, struct fastrpc_channel_ctx, refcount); +for (int i =0 ; i <FASTRPC_MAX_SESSIONS ; i++) { +if (cctx->session[i].allocated) +mutex_destroy (&cctx->session[i].mutex); + }kfree (cctx); }
@@ -800,19 +827,29 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd, goto get_err; } +mutex_lock (&fl->sctx->mutex); +if (!fl->sctx->dev) { + err = -ENODEV ; +mutex_unlock (&fl->sctx->mutex); + goto attach_err; + } + map->attach =dma_buf_attach (map->buf, sess->dev);if (IS_ERR (map->attach)) {dev_err (sess->dev,"Failed to attach dmabuf\n" ); err =PTR_ERR (map->attach); +mutex_unlock (&fl->sctx->mutex); goto attach_err; } table =dma_buf_map_attachment_unlocked (map->attach,DMA_BIDIRECTIONAL );if (IS_ERR (table)) { err =PTR_ERR (table); +mutex_unlock (&fl->sctx->mutex); goto map_err; } map->table = table; +mutex_unlock (&fl->sctx->mutex);if (attr &FASTRPC_ATTR_SECUREMAP ) map->dma_addr =sg_phys (map->table->sgl);
@@ -2217,6 +2254,8 @@ static int fastrpc_cb_probe(struct platform_device *pdev) sess->used = false; sess->valid = true; sess->dev = dev; +mutex_init (&sess->mutex); + sess->allocated = true;dev_set_drvdata (dev, sess);if (cctx->domain_id ==CDSP_DOMAIN_ID )
@@ -2233,6 +2272,8 @@ static int fastrpc_cb_probe(struct platform_device *pdev)break ; dup_sess = &cctx->session[cctx->sesscount++];memcpy (dup_sess, sess,sizeof (*dup_sess)); +mutex_init (&dup_sess->mutex); + dup_sess->allocated = true; } }spin_unlock_irqrestore (&cctx->lock, flags);
@@ -2255,6 +2296,11 @@ static void fastrpc_cb_remove(struct platform_device *pdev)spin_lock_irqsave (&cctx->lock, flags);for (i =0 ; i <FASTRPC_MAX_SESSIONS ; i++) {if (cctx->session[i].sid == sess->sid) { +spin_unlock_irqrestore (&cctx->lock, flags); +mutex_lock (&cctx->session[i].mutex); + cctx->session[i].dev =NULL ; +mutex_unlock (&cctx->session[i].mutex); +spin_lock_irqsave (&cctx->lock, flags); cctx->session[i].valid = false; cctx->sesscount--; }
Signature · 1 line
2.43.0
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Jianping,
kernel test robot noticed the following build errors:
[auto build test ERROR on char-misc/char-misc-testing]
[also build test ERROR on char-misc/char-misc-next char-misc/char-misc-linus linus/master v7.1-rc1 next-20260429] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jianping-Li/misc-fastrpc-fix-UAF-and-kernel-panic-during-cleanup-on-process-abort/20260428-043358 base: char-misc/char-misc-testing patch link: https://lore.kernel.org/r/20260427105310.4056-1-jianping.li%40oss.qualcomm.com patch subject: [PATCH] misc: fastrpc: fix UAF and kernel panic during cleanup on process abort config: arm64-randconfig-002-20260430 (https://download.01.org/0day-ci/archive/20260430/[email protected]/config) compiler: clang version 23.0.0git (https://github.com/llvm/llvm-project 5bac06718f502014fade905512f1d26d578a18f3) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260430/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
>> drivers/misc/fastrpc.c:437:7: error: call to undeclared function 'FASTRPC_PHYS'; ISO C99 and later do not support implicit function declarations [-Wimplicit-function-declaration]
437 | FASTRPC_PHYS(buf->phys));
| ^
>> drivers/misc/fastrpc.c:437:25: error: no member named 'phys' in 'struct fastrpc_buf' 437 | FASTRPC_PHYS(buf->phys));
| ~~~ ^
drivers/misc/fastrpc.c:465:70: error: no member named 'phys' in 'struct fastrpc_buf'
465 | buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys, GFP_KERNEL);
| ~~~ ^
3 errors generated.
vim +/FASTRPC_PHYS +437 drivers/misc/fastrpc.c
427
428 static void fastrpc_buf_free(struct fastrpc_buf *buf)
429 {
430 struct fastrpc_user *fl = buf->fl;
431
432 if (!fl)
433 return;
434 mutex_lock(&fl->sctx->mutex);
435 if (fl->sctx->dev) {
436 dma_free_coherent(buf->dev, buf->size, buf->virt,
> 437 FASTRPC_PHYS(buf->phys));
438 kfree(buf);
439 }
440 mutex_unlock(&fl->sctx->mutex);
441 }
442
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
kernel test robot
build botkernel test robot noticed the following build errors:
Hi Jianping,
kernel test robot noticed the following build errors:
[auto build test ERROR on char-misc/char-misc-testing]
[also build test ERROR on char-misc/char-misc-next char-misc/char-misc-linus linus/master v7.1-rc1 next-20260429] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Jianping-Li/misc-fastrpc-fix-UAF-and-kernel-panic-during-cleanup-on-process-abort/20260428-043358 base: char-misc/char-misc-testing patch link: https://lore.kernel.org/r/20260427105310.4056-1-jianping.li%40oss.qualcomm.com patch subject: [PATCH] misc: fastrpc: fix UAF and kernel panic during cleanup on process abort config: csky-randconfig-001-20260430 (https://download.01.org/0day-ci/archive/20260430/[email protected]/config) compiler: csky-linux-gcc (GCC) 15.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260430/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
drivers/misc/fastrpc.c: In function 'fastrpc_buf_free':
>> drivers/misc/fastrpc.c:437:35: error: implicit declaration of function 'FASTRPC_PHYS' [-Wimplicit-function-declaration]
437 | FASTRPC_PHYS(buf->phys));
| ^~~~~~~~~~~~
>> drivers/misc/fastrpc.c:437:51: error: 'struct fastrpc_buf' has no member named 'phys'
437 | FASTRPC_PHYS(buf->phys));
| ^~
drivers/misc/fastrpc.c: In function '__fastrpc_buf_alloc':
drivers/misc/fastrpc.c:465:82: error: 'struct fastrpc_buf' has no member named 'phys'
465 | buf->virt = dma_alloc_coherent(dev, buf->size, (dma_addr_t *)&buf->phys, GFP_KERNEL);
| ^~vim +/FASTRPC_PHYS +437 drivers/misc/fastrpc.c
427
428 static void fastrpc_buf_free(struct fastrpc_buf *buf)
429 {
430 struct fastrpc_user *fl = buf->fl;
431
432 if (!fl)
433 return;
434 mutex_lock(&fl->sctx->mutex);
435 if (fl->sctx->dev) {
436 dma_free_coherent(buf->dev, buf->size, buf->virt,
> 437 FASTRPC_PHYS(buf->phys));
438 kfree(buf);
439 }
440 mutex_unlock(&fl->sctx->mutex);
441 }
442
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki