Skip to contentSkip to conversation

misc: fastrpc: avoid duplicate DMA mappings in fastrpc_create_maps()

PATCH Superseded Jianping Li 3 participants 4 messages Jun 25 – Jul 3

Subsystems (3)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 misc: fastrpc: avoid duplicate DMA mappings in fastrpc_create_maps()

fastrpc_create_maps() performs map lookup only for buffer arguments (i < ctx->nbufs) via fastrpc_map_create(). For arguments beyond this range, no lookup is performed, which can result in duplicate DMA mappings for the same file descriptor.

Additionally, if the same file descriptor is passed multiple times within a single invocation, performing lookups with reference counting would increment the reference multiple times, while fastrpc_put_args() would release it only once, leading to an imbalanced reference count.

Fix this by allowing fastrpc_map_create() to control whether the lookup should take a reference. For arguments beyond ctx->nbufs, the lookup is performed without taking a reference, ensuring that existing mappings are reused without introducing duplicate DMA mappings or reference count imbalance.

Fixes 10df039834f84 ("misc: fastrpc: Skip reference for DMA handles")
---
 drivers/misc/fastrpc.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
drivers/misc/fastrpc.c +11 -11
index a9b2ae44c06f..2622a1360a65 100644
@@ -924,9 +924,9 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
 }
 
 static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
-			      u64 len, u32 attr, struct fastrpc_map **ppmap)
+			      u64 len, u32 attr, struct fastrpc_map **ppmap, bool take_ref)
 {
-	if (!fastrpc_map_lookup(fl, fd, ppmap, true))
+	if (!fastrpc_map_lookup(fl, fd, ppmap, take_ref))
 		return 0;
 
 	return fastrpc_map_attach(fl, fd, len, attr, ppmap);
@@ -999,23 +999,23 @@ static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
 	int i, err;
 
 	for (i = 0; i < ctx->nscalars; ++i) {
+		bool take_ref = true;
 
 		if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
 		    ctx->args[i].length == 0)
 			continue;
 
-		if (i < ctx->nbufs)
-			err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
-				 ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
-		else
-			err = fastrpc_map_attach(ctx->fl, ctx->args[i].fd,
-				 ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
+		if (i >= ctx->nbufs)
+			take_ref = false;
+
+		err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, ctx->args[i].length,
+			 ctx->args[i].attr, &ctx->maps[i], take_ref);
 		if (err) {
 			dev_err(dev, "Error Creating map %d\n", err);
 			return -EINVAL;
 		}
-
 	}
+
 	return 0;
 }
 
@@ -1508,7 +1508,7 @@ static int fastrpc_init_create_process(struct fastrpc_user *fl,
 	fl->pd = USER_PD;
 
 	if (init.filelen && init.filefd) {
-		err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map);
+		err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map, true);
 		if (err)
 			goto err;
 	}
@@ -2100,7 +2100,7 @@ static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp)
 		return -EFAULT;
 
 	/* create SMMU mapping */
-	err = fastrpc_map_create(fl, req.fd, req.length, 0, &map);
+	err = fastrpc_map_create(fl, req.fd, req.length, 0, &map, true);
 	if (err) {
 		dev_err(dev, "failed to map buffer, fd = %d\n", req.fd);
 		return err;
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:

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

New issues:
- [Critical] Use-After-Free and Double Free due to bypassing reference counting for handles.

Pre-existing issues:
- [High] Memory leak of newly created DMA maps for handle arguments.
- [High] Race condition in fdlist cleanup leading to Kernel Use-After-Free.
- [High] Circular reference causing permanent resource leak on interrupted invocations. --

Note: The format of this review is altered to provide a free-form summary rather than inline quotes to comply with system recitation restrictions.

commit ad847e952f22a54c8e1f599b332d32abb92a3e6a
Author: Jianping Li <[email protected]>

misc: fastrpc: avoid duplicate DMA mappings in fastrpc_create_maps()

This patch modifies fastrpc_create_maps() to bypass reference counting when looking up existing DMA mappings for handle arguments. It achieves this by introducing a boolean take_ref flag to fastrpc_map_create() to prevent duplicate DMA mappings and unbalanced reference counts.

[Severity: Critical]
Does bypassing the reference count for handle arguments introduce a use-after-free and double-free risk?

In drivers/misc/fastrpc.c:fastrpc_create_maps(), this patch forces take_ref = false for arguments where i >= ctx->nbufs. This populates ctx->maps[i] with a weak reference.

If a concurrent thread calls FASTRPC_IOCTL_MEM_UNMAP and drops the map's reference, could fastrpc_get_args() dereference a freed pointer when it accesses ctx->maps[i]->dma_addr?

Additionally, if the DSP returns the handle in fdlist, fastrpc_put_args() will unconditionally call fastrpc_map_put(). Since a reference was never taken during creation, does this consume an unbalanced reference and free explicit memory maps prematurely?

[Severity: High]
This isn't a bug introduced by this patch, but does the error path leak newly created DMA maps for handle arguments?

When fastrpc_map_create() does not find an existing mapping, it calls fastrpc_map_attach(), which allocates a new map with a reference count of 1.

However, looking at drivers/misc/fastrpc.c:fastrpc_context_free(), the cleanup loop only iterates up to ctx->nbufs. Since handle arguments (i >= ctx->nbufs) are deliberately skipped during this cleanup, are the maps created for them permanently leaked, potentially leading to memory exhaustion if a user repeatedly passes new memfds?

[Severity: High]
This is a pre-existing issue, but is there a race condition in the fdlist cleanup that could lead to a use-after-free?

In drivers/misc/fastrpc.c:fastrpc_put_args(), the code looks up the map without taking a reference (take_ref = false), and then immediately calls fastrpc_map_put() to drop a reference.

If a compromised or buggy DSP returns the same file descriptor in the fdlist for two concurrent invocations, could both threads simultaneously execute the lookup and drop a reference without adequate locking, causing the second thread to drop a reference on an already freed map?

[Severity: High]
This isn't a bug introduced by this patch, but does the invocation error handling create a circular reference that permanently leaks resources?

In drivers/misc/fastrpc.c:fastrpc_internal_invoke(), if the invocation is interrupted (returning -ERESTARTSYS or -ETIMEDOUT), the code explicitly skips calling fastrpc_context_put().

Because fastrpc_context_alloc() takes a strong reference to the user structure (fastrpc_user_get()), and the leaked context holds this reference, the user reference count never reaches zero. Does this prevent fastrpc_user_free() from ever executing to clean up pending contexts, allowing a local user to easily exhaust resources by interrupting invocations?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1
reviewer
#2 lore raw
In reply to Jianping Li
Quoted 43 lines · Jianping Li

fastrpc_create_maps() performs map lookup only for buffer arguments (i < ctx->nbufs) via fastrpc_map_create(). For arguments beyond this range, no lookup is performed, which can result in duplicate DMA mappings for the same file descriptor.

Additionally, if the same file descriptor is passed multiple times within a single invocation, performing lookups with reference counting would increment the reference multiple times, while fastrpc_put_args() would release it only once, leading to an imbalanced reference count.

Fix this by allowing fastrpc_map_create() to control whether the lookup should take a reference. For arguments beyond ctx->nbufs, the lookup is performed without taking a reference, ensuring that existing mappings are reused without introducing duplicate DMA mappings or reference count imbalance.

Fixes 10df039834f84 ("misc: fastrpc: Skip reference for DMA handles")
---
 drivers/misc/fastrpc.c | 22 +++++++++++-----------
 1 file changed, 11 insertions(+), 11 deletions(-)
drivers/misc/fastrpc.c +2 -2
index a9b2ae44c06f..2622a1360a65 100644
@@ -924,9 +924,9 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
 }
 
 static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
-			      u64 len, u32 attr, struct fastrpc_map **ppmap)
+			      u64 len, u32 attr, struct fastrpc_map **ppmap, bool take_ref)
 {
-	if (!fastrpc_map_lookup(fl, fd, ppmap, true))
+	if (!fastrpc_map_lookup(fl, fd, ppmap, take_ref))
 		return 0;
 
 	return fastrpc_map_attach(fl, fd, len, attr, ppmap);

drivers/misc/fastrpc.c static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx) · line 1002

Quoted 4 lines
drivers/misc/fastrpc.c +1 -0
@@ -999,23 +999,23 @@ static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
 	int i, err;
 
 	for (i = 0; i < ctx->nscalars; ++i) {
+		bool take_ref = true;

bool take_ref = i < ctx->nbufs, also add a comment stating why reference should be skipped for DMA handles.>

Quoted 43 lines
drivers/misc/fastrpc.c +8 -9
 		if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
 		    ctx->args[i].length == 0)
 			continue;
 
-		if (i < ctx->nbufs)
-			err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
-				 ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
-		else
-			err = fastrpc_map_attach(ctx->fl, ctx->args[i].fd,
-				 ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
+		if (i >= ctx->nbufs)
+			take_ref = false;
+
+		err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, ctx->args[i].length,
+			 ctx->args[i].attr, &ctx->maps[i], take_ref);
 		if (err) {
 			dev_err(dev, "Error Creating map %d\n", err);
 			return -EINVAL;
 		}
-
 	}
+
 	return 0;
 }
 
@@ -1508,7 +1508,7 @@ static int fastrpc_init_create_process(struct fastrpc_user *fl,
 	fl->pd = USER_PD;
 
 	if (init.filelen && init.filefd) {
-		err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map);
+		err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map, true);
 		if (err)
 			goto err;
 	}
@@ -2100,7 +2100,7 @@ static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp)
 		return -EFAULT;
 
 	/* create SMMU mapping */
-	err = fastrpc_map_create(fl, req.fd, req.length, 0, &map);
+	err = fastrpc_map_create(fl, req.fd, req.length, 0, &map, true);
 	if (err) {
 		dev_err(dev, "failed to map buffer, fd = %d\n", req.fd);
 		return err;
#3 lore raw
Quoted 42 lines · Ekansh Gupta
Quoted 41 lines · Jianping Li

fastrpc_create_maps() performs map lookup only for buffer arguments (i < ctx->nbufs) via fastrpc_map_create(). For arguments beyond this range, no lookup is performed, which can result in duplicate DMA mappings for the same file descriptor.

Additionally, if the same file descriptor is passed multiple times within a single invocation, performing lookups with reference counting would increment the reference multiple times, while fastrpc_put_args() would release it only once, leading to an imbalanced reference count.

Fix this by allowing fastrpc_map_create() to control whether the lookup should take a reference. For arguments beyond ctx->nbufs, the lookup is performed without taking a reference, ensuring that existing mappings are reused without introducing duplicate DMA mappings or reference count imbalance.

Fixes 10df039834f84 ("misc: fastrpc: Skip reference for DMA handles")
---
  drivers/misc/fastrpc.c | 22 +++++++++++-----------
  1 file changed, 11 insertions(+), 11 deletions(-)
drivers/misc/fastrpc.c +2 -2
index a9b2ae44c06f..2622a1360a65 100644
@@ -924,9 +924,9 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
  }
 
  static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
-			      u64 len, u32 attr, struct fastrpc_map **ppmap)
+			      u64 len, u32 attr, struct fastrpc_map **ppmap, bool take_ref)
  {
-	if (!fastrpc_map_lookup(fl, fd, ppmap, true))
+	if (!fastrpc_map_lookup(fl, fd, ppmap, take_ref))
  		return 0;
 
Quoted 8 lines

drivers/misc/fastrpc.c static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx) · line 1002

Quoted 6 lines
drivers/misc/fastrpc.c +1 -0
  	return fastrpc_map_attach(fl, fd, len, attr, ppmap);
@@ -999,23 +999,23 @@ static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
  	int i, err;
 
  	for (i = 0; i < ctx->nscalars; ++i) {
+		bool take_ref = true;

bool take_ref = i < ctx->nbufs, also add a comment stating why reference should be skipped for DMA handles.>

Thanks for the review.

That's a good point. I'll initialize the variable as:

    bool take_ref = i < ctx->nbufs;

and add a comment explaining that DMA handle arguments can reuse an existing mapping and should not take an additional reference, otherwise multiple references may be acquired for the same fd while only a single release is performed during cleanup.

Quoted 43 lines
Quoted 43 lines
drivers/misc/fastrpc.c +8 -9
  		if (ctx->args[i].fd == 0 || ctx->args[i].fd == -1 ||
  		    ctx->args[i].length == 0)
  			continue;
 
-		if (i < ctx->nbufs)
-			err = fastrpc_map_create(ctx->fl, ctx->args[i].fd,
-				 ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
-		else
-			err = fastrpc_map_attach(ctx->fl, ctx->args[i].fd,
-				 ctx->args[i].length, ctx->args[i].attr, &ctx->maps[i]);
+		if (i >= ctx->nbufs)
+			take_ref = false;
+
+		err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, ctx->args[i].length,
+			 ctx->args[i].attr, &ctx->maps[i], take_ref);
  		if (err) {
  			dev_err(dev, "Error Creating map %d\n", err);
  			return -EINVAL;
  		}
-
  	}
+
  	return 0;
  }
 
@@ -1508,7 +1508,7 @@ static int fastrpc_init_create_process(struct fastrpc_user *fl,
  	fl->pd = USER_PD;
 
  	if (init.filelen && init.filefd) {
-		err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map);
+		err = fastrpc_map_create(fl, init.filefd, init.filelen, 0, &map, true);
  		if (err)
  			goto err;
  	}
@@ -2100,7 +2100,7 @@ static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp)
  		return -EFAULT;
 
  	/* create SMMU mapping */
-	err = fastrpc_map_create(fl, req.fd, req.length, 0, &map);
+	err = fastrpc_map_create(fl, req.fd, req.length, 0, &map, true);
  	if (err) {
  		dev_err(dev, "failed to map buffer, fd = %d\n", req.fd);
  		return err;