Skip to contentSkip to conversation

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

PATCH Superseded Jianping Li 3 participants 5 messages Jul 16 – Jul 31

Subsystems (4)

v2 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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

DMA handles passed as invoke arguments (scalars beyond nbufs) may refer to the same dma_buf fd as an input/output buffer argument. Taking an extra reference for such DMA handle maps leads to duplicate mappings and an unbalanced reference count, since DMA handle maps are released separately when the DSP returns the fd through the fdlist.

Fix this by not taking an extra reference for DMA handle arguments (take_ref = false) and tagging them with FASTRPC_MAP_DMA_HANDLE. As these maps are borrowed references, fastrpc_get_args() re-validates the map via fastrpc_map_lookup() before dereferencing it, so it is not used after being freed. fastrpc_put_args() only releases maps flagged as FASTRPC_MAP_DMA_HANDLE and clears the flag to guarantee the map is freed exactly once.

Also reject FASTRPC_MAP_DMA_HANDLE in fastrpc_req_mem_map(), since such handles are already mapped implicitly during the remote invoke call and must not be mapped again through the explicit MEM_MAP path.

Fixes 10df039834f84 ("misc: fastrpc: Skip reference for DMA handles")

---
Patch [v1]: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Rework the commit message to describe the DMA handle reference and lifetime problem more precisely.
- Introduce a new FASTRPC_MAP_DMA_HANDLE uapi flag and a 'flags' field in struct fastrpc_map to explicitly tag DMA handle maps, instead of relying only on the nbufs boundary / take_ref.
- Plumb an mflags argument through fastrpc_map_create() and fastrpc_map_attach() so DMA handle maps are tagged at creation time.
- Re-validate the borrowed map in fastrpc_get_args() via fastrpc_map_lookup() before dereferencing it, to avoid a use-after-free when the map was created with take_ref = false.
- In fastrpc_put_args(), only release maps tagged FASTRPC_MAP_DMA_HANDLE and clear the flag afterwards, so such maps are freed exactly once.
- Reject FASTRPC_MAP_DMA_HANDLE in fastrpc_req_mem_map(), since these handles are already mapped implicitly during the remote invoke and must not be mapped again through the explicit MEM_MAP path.

---
 drivers/misc/fastrpc.c      | 56 ++++++++++++++++++++++++++-----------
 include/uapi/misc/fastrpc.h |  2 ++
 2 files changed, 42 insertions(+), 16 deletions(-)
drivers/misc/fastrpc.c +40 -16
index d86e79134c68..fa8c58a97e35 100644
@@ -223,6 +223,7 @@ struct fastrpc_map {
 	u64 len;
 	u64 raddr;
 	u32 attr;
+	u32 flags;
 	struct kref refcount;
 };
 
@@ -833,7 +834,7 @@ static dma_addr_t fastrpc_compute_dma_addr(struct fastrpc_user *fl, dma_addr_t s
 }
 
 static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
-			      u64 len, u32 attr, struct fastrpc_map **ppmap)
+			      u64 len, u32 attr, struct fastrpc_map **ppmap, int mflags)
 {
 	struct fastrpc_session_ctx *sess = fl->sctx;
 	struct fastrpc_map *map = NULL;
@@ -850,6 +851,7 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
 
 	map->fl = fl;
 	map->fd = fd;
+	map->flags = mflags;
 	map->buf = dma_buf_get(fd);
 	if (IS_ERR(map->buf)) {
 		err = PTR_ERR(map->buf);
@@ -924,13 +926,13 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
 	return err;
 }
 
-static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
-			      u64 len, u32 attr, struct fastrpc_map **ppmap)
+static int fastrpc_map_create(struct fastrpc_user *fl, int fd, u64 len, u32 attr,
+			      struct fastrpc_map **ppmap, bool take_ref, int mflags)
 {
-	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);
+	return fastrpc_map_attach(fl, fd, len, attr, ppmap, mflags);
 }
 
 /*
@@ -1000,23 +1002,25 @@ static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
 	int i, err;
 
 	for (i = 0; i < ctx->nscalars; ++i) {
+		bool take_ref = i < ctx->nbufs;
+		int mflags = 0;
 
 		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]);
+		/* Set the DMA handle mapping flag for DMA handles */
+		if (i >= ctx->nbufs)
+			mflags = FASTRPC_MAP_DMA_HANDLE;
+
+		err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, ctx->args[i].length,
+					 ctx->args[i].attr, &ctx->maps[i], take_ref, mflags);
 		if (err) {
 			dev_err(dev, "Error Creating map %d\n", err);
 			return -EINVAL;
 		}
-
 	}
+
 	return 0;
 }
 
@@ -1144,6 +1148,16 @@ static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
 		list[i].num = ctx->args[i].length ? 1 : 0;
 		list[i].pgidx = i;
 		if (ctx->maps[i]) {
+			/* It is possible that map is created with
+			 * mflags FASTRPC_MAP_DMA_HANDLE and take_ref
+			 * is false. Check if map still exists or is
+			 * being freed as take_ref is false
+			 */
+			if (fastrpc_map_lookup(ctx->fl, ctx->args[i].fd,
+					       &ctx->maps[i], false)) {
+				ctx->maps[i] = NULL;
+				return -EINVAL;
+			}
 			pages[i].addr = ctx->maps[i]->dma_addr;
 			pages[i].size = ctx->maps[i]->size;
 		}
@@ -1200,8 +1214,13 @@ static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
 	for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
 		if (!fdlist[i])
 			break;
-		if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false))
+		/* Validate the map flags for DMA handles and skip freeing map if invalid */
+		if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false) &&
+		    mmap->flags == FASTRPC_MAP_DMA_HANDLE) {
+			/* Allow DMA handle maps to free only once */
+			mmap->flags = 0;
 			fastrpc_map_put(mmap);
+		}
 	}
 
 	return ret;
@@ -1511,7 +1530,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, 0);
 		if (err)
 			goto err;
 	}
@@ -2107,9 +2126,14 @@ static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp)
 
 	if (copy_from_user(&req, argp, sizeof(req)))
 		return -EFAULT;
-
+	/*
+	 * Prevent mapping backward compatible DMA handles here, as they are
+	 * already mapped in the remote call.
+	 */
+	if (req.flags == FASTRPC_MAP_DMA_HANDLE)
+		return -EINVAL;
 	/* 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, 0);
 	if (err) {
 		dev_err(dev, "failed to map buffer, fd = %d\n", req.fd);
 		return err;
include/uapi/misc/fastrpc.h +2 -0
index c6e2925f47e6..142ddaeed85f 100644
@@ -44,6 +44,8 @@ enum fastrpc_map_flags {
 	FASTRPC_MAP_FD = 2,
 	FASTRPC_MAP_FD_DELAYED,
 	FASTRPC_MAP_FD_NOMAP = 16,
+	/* Map the DMA handle in the invoke call for backward compatibility */
+	FASTRPC_MAP_DMA_HANDLE = 0x20000,
 	FASTRPC_MAP_MAX,
 };
 
Signature · 1 line
2.43.0
#1 lore raw
Thank you for your contribution! Sashiko AI review found 5 potential issue(s) to consider:

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

New issues:
- [Critical] A data race when clearing mmap->flags in fastrpc_put_args() allows multiple concurrent threads to double-free a shared DMA handle map.
- [High] A time-of-check to time-of-use (TOCTOU) race condition in fastrpc_get_args() leads to a Use-After-Free read of a fastrpc_map object.
- [High] Bypassing reference counting for shared DMA handles leads to premature destruction of IOMMU mappings and DSP SMMU faults.

Pre-existing issues:
- [Critical] fastrpc_req_mem_unmap_impl() accesses and frees a map without holding a reference or lock, leading to Use-After-Free and Double-Free on concurrent unmap requests.
- [Critical] A compromised or malicious DSP can force the host kernel to copy arbitrary kernel memory to userspace by modifying the shared rpra buffer. --

--- Patch [1]: [PATCH v2] misc: fastrpc: avoid duplicate DMA mappings in fastrpc_create_maps() --- commit e27fd6a362ab035d376548e127d0d378dd957590
Author: Jianping Li <[email protected]>

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

This patch attempts to prevent duplicate DMA mappings by skipping extra references for DMA handles and tagging them with FASTRPC_MAP_DMA_HANDLE. It adds re-validation of borrowed maps in fastrpc_get_args() and modifies fastrpc_put_args() to only release maps tagged with this new flag.

Quoted 4 lines
drivers/misc/fastrpc.c +0 -0
index f3a49384586d1..12c82072236b2 100644

[ ... ]

Quoted 13 lines
drivers/misc/fastrpc.c +2 -2
@@ -999,23 +1001,25 @@ static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
 	int i, err;
 
 	for (i = 0; i < ctx->nscalars; ++i) {
+		bool take_ref = i < ctx->nbufs;
+		int mflags = 0;
 
 		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,

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

Quoted 10 lines
drivers/misc/fastrpc.c +6 -4
-				 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]);
+		/* Set the DMA handle mapping flag for DMA handles */
+		if (i >= ctx->nbufs)
+			mflags = FASTRPC_MAP_DMA_HANDLE;
+
+		err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, ctx->args[i].length,
+					 ctx->args[i].attr, &ctx->maps[i], take_ref, mflags);

[Severity: High]
Does bypassing reference counting (take_ref = false) for shared DMA handles lead to premature destruction of IOMMU mappings in drivers/misc/fastrpc.c:fastrpc_create_maps()?

If two concurrent threads pass the same fd to the DSP, they share the map with a single reference count. When the first thread finishes and frees the map, could this destroy the IOMMU mapping while the DSP is still actively using it for the second thread, causing an SMMU fault?

Quoted 5 lines
drivers/misc/fastrpc.c +0 -0
 		if (err) {
 			dev_err(dev, "Error Creating map %d\n", err);
 			return -EINVAL;
 		}
 	}

[ ... ]

Quoted 5 lines
drivers/misc/fastrpc.c +1 -0
@@ -1143,6 +1147,16 @@ static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
 		list[i].num = ctx->args[i].length ? 1 : 0;
 		list[i].pgidx = i;
 		if (ctx->maps[i]) {
+			/* It is possible that map is created with

drivers/misc/fastrpc.c static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx) · line 1160

Quoted 10 lines
drivers/misc/fastrpc.c +9 -0
+			 * mflags FASTRPC_MAP_DMA_HANDLE and take_ref
+			 * is false. Check if map still exists or is
+			 * being freed as take_ref is false
+			 */
+			if (fastrpc_map_lookup(ctx->fl, ctx->args[i].fd,
+					       &ctx->maps[i], false)) {
+				ctx->maps[i] = NULL;
+				return -EINVAL;
+			}
 			pages[i].addr = ctx->maps[i]->dma_addr;

[Severity: High]
Is there a time-of-check to time-of-use race condition here in drivers/misc/fastrpc.c:fastrpc_get_args()?

Because fastrpc_map_lookup() is called with take_ref = false, it sets ctx->maps[i] inside a spinlock but returns without incrementing the reference count. If a concurrent thread drops the final reference and frees the map immediately after the lock is released, does the subsequent dereference of ctx->maps[i]->dma_addr result in a use-after-free read?

drivers/misc/fastrpc.c static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx) · line 1163

Quoted 2 lines
drivers/misc/fastrpc.c +0 -0
 			pages[i].size = ctx->maps[i]->size;
 		}

[ ... ]

In drivers/misc/fastrpc.c:fastrpc_put_args(), earlier in the function:

	for (i = inbufs; i < ctx->nbufs; ++i) {
		if (!ctx->maps[i]) {
			void *src = (void *)(uintptr_t)rpra[i].buf.pv;
			void *dst = (void *)(uintptr_t)ctx->args[i].ptr;
			u64 len = rpra[i].buf.len;

			if (!kernel) {
				if (copy_to_user((void __user *)dst, src, len)) {

[Severity: Critical]
This is a pre-existing issue, but can a compromised DSP force the host kernel to copy arbitrary kernel memory to userspace here?

The src pointer (rpra[i].buf.pv) and len are read directly from DSP-modifiable shared memory without validation. If the kernel does not verify that these values match the original bounds allocated for the RPC, could the DSP overwrite them to point to arbitrary kernel memory addresses and leak sensitive data?

drivers/misc/fastrpc.c static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx, · line 1220

Quoted 10 lines
drivers/misc/fastrpc.c +5 -1
@@ -1199,8 +1213,13 @@ static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
 	for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
 		if (!fdlist[i])
 			break;
-		if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false))
+		/* Validate the map flags for DMA handles and skip freeing map if invalid */
+		if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false) &&
+		    mmap->flags == FASTRPC_MAP_DMA_HANDLE) {
+			/* Allow DMA handle maps to free only once */
+			mmap->flags = 0;

[Severity: Critical]
Does clearing mmap->flags here introduce a data race in drivers/misc/fastrpc.c:fastrpc_put_args()?

The test and subsequent modification of mmap->flags are performed completely locklessly. If two concurrent threads process the same file descriptor returned by the DSP, could both threads evaluate the flag check as true simultaneously, clear the flag, and call fastrpc_map_put(mmap), resulting in a double-free?

drivers/misc/fastrpc.c static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx, · line 1226

Quoted 5 lines
drivers/misc/fastrpc.c +1 -0
 			fastrpc_map_put(mmap);
+		}
 	}
 
 	return ret;

[ ... ]

In drivers/misc/fastrpc.c:fastrpc_req_mem_unmap_impl():

	spin_lock(&fl->lock);
	list_for_each_entry_safe(iter, m, &fl->maps, node) {
		if ((req->fd < 0 || iter->fd == req->fd) && (iter->raddr == req->vaddr)) {
			map = iter;
			break;
		}
	}
	spin_unlock(&fl->lock);
	...
	fastrpc_map_put(map);

[Severity: Critical]
This is a pre-existing issue, but does fastrpc_req_mem_unmap_impl() access and free a map without holding a reference or lock?

When searching for a map under fl->lock, the function breaks out of the loop and drops the lock without incrementing the reference count via fastrpc_map_get(). If two concurrent threads call the unmap ioctl for the same virtual address, could both threads find the map, drop the lock, and subsequently call fastrpc_map_put() causing a double-free and use-after-free?

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

DMA handles passed as invoke arguments (scalars beyond nbufs) may refer to the same dma_buf fd as an input/output buffer argument. Taking an extra reference for such DMA handle maps leads to duplicate mappings and an unbalanced reference count, since DMA handle maps are released separately when the DSP returns the fd through the fdlist.

Fix this by not taking an extra reference for DMA handle arguments (take_ref = false) and tagging them with FASTRPC_MAP_DMA_HANDLE. As these maps are borrowed references, fastrpc_get_args() re-validates the map via fastrpc_map_lookup() before dereferencing it, so it is not used after being freed. fastrpc_put_args() only releases maps flagged as FASTRPC_MAP_DMA_HANDLE and clears the flag to guarantee the map is freed exactly once.

Also reject FASTRPC_MAP_DMA_HANDLE in fastrpc_req_mem_map(), since such handles are already mapped implicitly during the remote invoke call and must not be mapped again through the explicit MEM_MAP path.

Fixes 10df039834f84 ("misc: fastrpc: Skip reference for DMA handles")

---
Patch [v1]: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Rework the commit message to describe the DMA handle reference and lifetime problem more precisely.
- Introduce a new FASTRPC_MAP_DMA_HANDLE uapi flag and a 'flags' field in struct fastrpc_map to explicitly tag DMA handle maps, instead of relying only on the nbufs boundary / take_ref.
- Plumb an mflags argument through fastrpc_map_create() and fastrpc_map_attach() so DMA handle maps are tagged at creation time.
- Re-validate the borrowed map in fastrpc_get_args() via fastrpc_map_lookup() before dereferencing it, to avoid a use-after-free when the map was created with take_ref = false.
- In fastrpc_put_args(), only release maps tagged FASTRPC_MAP_DMA_HANDLE and clear the flag afterwards, so such maps are freed exactly once.
- Reject FASTRPC_MAP_DMA_HANDLE in fastrpc_req_mem_map(), since these handles are already mapped implicitly during the remote invoke and must not be mapped again through the explicit MEM_MAP path.

---
 drivers/misc/fastrpc.c      | 56 ++++++++++++++++++++++++++-----------
 include/uapi/misc/fastrpc.h |  2 ++
 2 files changed, 42 insertions(+), 16 deletions(-)
drivers/misc/fastrpc.c +16 -12
index d86e79134c68..fa8c58a97e35 100644
@@ -223,6 +223,7 @@ struct fastrpc_map {
 	u64 len;
 	u64 raddr;
 	u32 attr;
+	u32 flags;
 	struct kref refcount;
 };
 
@@ -833,7 +834,7 @@ static dma_addr_t fastrpc_compute_dma_addr(struct fastrpc_user *fl, dma_addr_t s
 }
 
 static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
-			      u64 len, u32 attr, struct fastrpc_map **ppmap)
+			      u64 len, u32 attr, struct fastrpc_map **ppmap, int mflags)
 {
 	struct fastrpc_session_ctx *sess = fl->sctx;
 	struct fastrpc_map *map = NULL;
@@ -850,6 +851,7 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
 
 	map->fl = fl;
 	map->fd = fd;
+	map->flags = mflags;
 	map->buf = dma_buf_get(fd);
 	if (IS_ERR(map->buf)) {
 		err = PTR_ERR(map->buf);
@@ -924,13 +926,13 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
 	return err;
 }
 
-static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
-			      u64 len, u32 attr, struct fastrpc_map **ppmap)
+static int fastrpc_map_create(struct fastrpc_user *fl, int fd, u64 len, u32 attr,
+			      struct fastrpc_map **ppmap, bool take_ref, int mflags)
 {
-	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);
+	return fastrpc_map_attach(fl, fd, len, attr, ppmap, mflags);
 }
 
 /*
@@ -1000,23 +1002,25 @@ static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
 	int i, err;
 
 	for (i = 0; i < ctx->nscalars; ++i) {
+		bool take_ref = i < ctx->nbufs;
+		int mflags = 0;
 
 		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]);
+		/* Set the DMA handle mapping flag for DMA handles */
+		if (i >= ctx->nbufs)
+			mflags = FASTRPC_MAP_DMA_HANDLE;
+
+		err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, ctx->args[i].length,
+					 ctx->args[i].attr, &ctx->maps[i], take_ref, mflags);
 		if (err) {
 			dev_err(dev, "Error Creating map %d\n", err);
 			return -EINVAL;
 		}
-
 	}
+
 	return 0;
 }
 
@@ -1144,6 +1148,16 @@ static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
 		list[i].num = ctx->args[i].length ? 1 : 0;
 		list[i].pgidx = i;
 		if (ctx->maps[i]) {

drivers/misc/fastrpc.c static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx) · line 1160

Quoted 10 lines
drivers/misc/fastrpc.c +10 -0
+			/* It is possible that map is created with
+			 * mflags FASTRPC_MAP_DMA_HANDLE and take_ref
+			 * is false. Check if map still exists or is
+			 * being freed as take_ref is false
+			 */
+			if (fastrpc_map_lookup(ctx->fl, ctx->args[i].fd,
+					       &ctx->maps[i], false)) {
+				ctx->maps[i] = NULL;
+				return -EINVAL;
+			}

if map is created by R1 call with DMA handle flag, when any inbuf/outbuf carries the same fd for R2 call, wouldn't the refcount get increased as part of fastrpc_create_maps()? So if the refcount is already increased
there, why do you need to have a check here?> pages[i].addr =
ctx->maps[i]->dma_addr;

drivers/misc/fastrpc.c static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx, · line 1221

Quoted 12 lines
drivers/misc/fastrpc.c +5 -1
 			pages[i].size = ctx->maps[i]->size;
 		}
@@ -1200,8 +1214,13 @@ static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
 	for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
 		if (!fdlist[i])
 			break;
-		if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false))
+		/* Validate the map flags for DMA handles and skip freeing map if invalid */
+		if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false) &&
+		    mmap->flags == FASTRPC_MAP_DMA_HANDLE) {
+			/* Allow DMA handle maps to free only once */
+			mmap->flags = 0;

flags are getting updated without any locks, this is considering that DSP can updated a particular fd on fdlist only once. Can you add this
information here?> fastrpc_map_put(mmap);

Quoted 43 lines
drivers/misc/fastrpc.c +9 -3
+		}
 	}
 
 	return ret;
@@ -1511,7 +1530,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, 0);
 		if (err)
 			goto err;
 	}
@@ -2107,9 +2126,14 @@ static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp)
 
 	if (copy_from_user(&req, argp, sizeof(req)))
 		return -EFAULT;
-
+	/*
+	 * Prevent mapping backward compatible DMA handles here, as they are
+	 * already mapped in the remote call.
+	 */
+	if (req.flags == FASTRPC_MAP_DMA_HANDLE)
+		return -EINVAL;
 	/* 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, 0);
 	if (err) {
 		dev_err(dev, "failed to map buffer, fd = %d\n", req.fd);
 		return err;
include/uapi/misc/fastrpc.h +2 -0
index c6e2925f47e6..142ddaeed85f 100644
@@ -44,6 +44,8 @@ enum fastrpc_map_flags {
 	FASTRPC_MAP_FD = 2,
 	FASTRPC_MAP_FD_DELAYED,
 	FASTRPC_MAP_FD_NOMAP = 16,
+	/* Map the DMA handle in the invoke call for backward compatibility */
+	FASTRPC_MAP_DMA_HANDLE = 0x20000,
 	FASTRPC_MAP_MAX,
 };
 
author
#3 lore raw
Quoted 137 lines · Ekansh Gupta
Quoted 136 lines · Jianping Li

DMA handles passed as invoke arguments (scalars beyond nbufs) may refer to the same dma_buf fd as an input/output buffer argument. Taking an extra reference for such DMA handle maps leads to duplicate mappings and an unbalanced reference count, since DMA handle maps are released separately when the DSP returns the fd through the fdlist.

Fix this by not taking an extra reference for DMA handle arguments (take_ref = false) and tagging them with FASTRPC_MAP_DMA_HANDLE. As these maps are borrowed references, fastrpc_get_args() re-validates the map via fastrpc_map_lookup() before dereferencing it, so it is not used after being freed. fastrpc_put_args() only releases maps flagged as FASTRPC_MAP_DMA_HANDLE and clears the flag to guarantee the map is freed exactly once.

Also reject FASTRPC_MAP_DMA_HANDLE in fastrpc_req_mem_map(), since such handles are already mapped implicitly during the remote invoke call and must not be mapped again through the explicit MEM_MAP path.

Fixes 10df039834f84 ("misc: fastrpc: Skip reference for DMA handles")

---
Patch [v1]: https://lore.kernel.org/all/[email protected]/

Changes in v2:
- Rework the commit message to describe the DMA handle reference and lifetime problem more precisely.
- Introduce a new FASTRPC_MAP_DMA_HANDLE uapi flag and a 'flags' field in struct fastrpc_map to explicitly tag DMA handle maps, instead of relying only on the nbufs boundary / take_ref.
- Plumb an mflags argument through fastrpc_map_create() and fastrpc_map_attach() so DMA handle maps are tagged at creation time.
- Re-validate the borrowed map in fastrpc_get_args() via fastrpc_map_lookup() before dereferencing it, to avoid a use-after-free when the map was created with take_ref = false.
- In fastrpc_put_args(), only release maps tagged FASTRPC_MAP_DMA_HANDLE and clear the flag afterwards, so such maps are freed exactly once.
- Reject FASTRPC_MAP_DMA_HANDLE in fastrpc_req_mem_map(), since these handles are already mapped implicitly during the remote invoke and must not be mapped again through the explicit MEM_MAP path.

---
  drivers/misc/fastrpc.c      | 56 ++++++++++++++++++++++++++-----------
  include/uapi/misc/fastrpc.h |  2 ++
  2 files changed, 42 insertions(+), 16 deletions(-)
drivers/misc/fastrpc.c +23 -12
index d86e79134c68..fa8c58a97e35 100644
@@ -223,6 +223,7 @@ struct fastrpc_map {
  	u64 len;
  	u64 raddr;
  	u32 attr;
+	u32 flags;
  	struct kref refcount;
  };
 
@@ -833,7 +834,7 @@ static dma_addr_t fastrpc_compute_dma_addr(struct fastrpc_user *fl, dma_addr_t s
  }
 
  static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
-			      u64 len, u32 attr, struct fastrpc_map **ppmap)
+			      u64 len, u32 attr, struct fastrpc_map **ppmap, int mflags)
  {
  	struct fastrpc_session_ctx *sess = fl->sctx;
  	struct fastrpc_map *map = NULL;
@@ -850,6 +851,7 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
 
  	map->fl = fl;
  	map->fd = fd;
+	map->flags = mflags;
  	map->buf = dma_buf_get(fd);
  	if (IS_ERR(map->buf)) {
  		err = PTR_ERR(map->buf);
@@ -924,13 +926,13 @@ static int fastrpc_map_attach(struct fastrpc_user *fl, int fd,
  	return err;
  }
 
-static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
-			      u64 len, u32 attr, struct fastrpc_map **ppmap)
+static int fastrpc_map_create(struct fastrpc_user *fl, int fd, u64 len, u32 attr,
+			      struct fastrpc_map **ppmap, bool take_ref, int mflags)
  {
-	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);
+	return fastrpc_map_attach(fl, fd, len, attr, ppmap, mflags);
  }
 
  /*
@@ -1000,23 +1002,25 @@ static int fastrpc_create_maps(struct fastrpc_invoke_ctx *ctx)
  	int i, err;
 
  	for (i = 0; i < ctx->nscalars; ++i) {
+		bool take_ref = i < ctx->nbufs;
+		int mflags = 0;
 
  		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]);
+		/* Set the DMA handle mapping flag for DMA handles */
+		if (i >= ctx->nbufs)
+			mflags = FASTRPC_MAP_DMA_HANDLE;
+
+		err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, ctx->args[i].length,
+					 ctx->args[i].attr, &ctx->maps[i], take_ref, mflags);
  		if (err) {
  			dev_err(dev, "Error Creating map %d\n", err);
  			return -EINVAL;
  		}
-
  	}
+
  	return 0;
  }
 
@@ -1144,6 +1148,16 @@ static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
  		list[i].num = ctx->args[i].length ? 1 : 0;
  		list[i].pgidx = i;
  		if (ctx->maps[i]) {
+			/* It is possible that map is created with
+			 * mflags FASTRPC_MAP_DMA_HANDLE and take_ref
+			 * is false. Check if map still exists or is
+			 * being freed as take_ref is false
+			 */
+			if (fastrpc_map_lookup(ctx->fl, ctx->args[i].fd,
+					       &ctx->maps[i], false)) {
Quoted 8 lines

drivers/misc/fastrpc.c static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx) · line 1160

Quoted 3 lines
drivers/misc/fastrpc.c +3 -0
+				ctx->maps[i] = NULL;
+				return -EINVAL;
+			}

if map is created by R1 call with DMA handle flag, when any inbuf/outbuf carries the same fd for R2 call, wouldn't the refcount get increased as part of fastrpc_create_maps()? So if the refcount is already increased
there, why do you need to have a check here?> pages[i].addr =
ctx->maps[i]->dma_addr;

The check is not for the case you described (same fd reused as an in/out buffer in a later call). In that case i < nbufs so the map is created with take_ref = true and the refcount is indeed held, and the re-lookup simply passes.
The check is needed for DMA handle args themselves, which are created with take_ref = false. For those maps this context holds only a borrowed reference — the owning reference is dropped when the DSP returns the fd via fdlist in fastrpc_put_args(), which may run from a concurrent invocation. So between fastrpc_create_maps() and fastrpc_get_args() the map may already have been freed, and dereferencing ctx->maps[i] would be a use-after-free. The re-lookup re-validates that the map is still present before we dereference it. (If you believe there is no competition, I will drop this code.)

Quoted 7 lines
Quoted 7 lines
drivers/misc/fastrpc.c +0 -1
  			pages[i].size = ctx->maps[i]->size;
  		}
@@ -1200,8 +1214,13 @@ static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx,
  	for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
  		if (!fdlist[i])
  			break;
-		if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false))
Quoted 8 lines

drivers/misc/fastrpc.c static int fastrpc_put_args(struct fastrpc_invoke_ctx *ctx, · line 1221

Quoted 5 lines
drivers/misc/fastrpc.c +5 -0
+		/* Validate the map flags for DMA handles and skip freeing map if invalid */
+		if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false) &&
+		    mmap->flags == FASTRPC_MAP_DMA_HANDLE) {
+			/* Allow DMA handle maps to free only once */
+			mmap->flags = 0;

flags are getting updated without any locks, this is considering that DSP can updated a particular fd on fdlist only once. Can you add this
information here?> fastrpc_map_put(mmap);

Agreed, this relies on the DSP reporting a given fd in the fdlist only once, so no concurrent fastrpc_put_args() can update the same map's flags. I'll add a comment documenting this assumption in v3. Will send v3 with the clarified comments.

Quoted 44 lines
Quoted 43 lines
drivers/misc/fastrpc.c +9 -3
+		}
  	}
 
  	return ret;
@@ -1511,7 +1530,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, 0);
  		if (err)
  			goto err;
  	}
@@ -2107,9 +2126,14 @@ static int fastrpc_req_mem_map(struct fastrpc_user *fl, char __user *argp)
 
  	if (copy_from_user(&req, argp, sizeof(req)))
  		return -EFAULT;
-
+	/*
+	 * Prevent mapping backward compatible DMA handles here, as they are
+	 * already mapped in the remote call.
+	 */
+	if (req.flags == FASTRPC_MAP_DMA_HANDLE)
+		return -EINVAL;
  	/* 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, 0);
  	if (err) {
  		dev_err(dev, "failed to map buffer, fd = %d\n", req.fd);
  		return err;
include/uapi/misc/fastrpc.h +2 -0
index c6e2925f47e6..142ddaeed85f 100644
@@ -44,6 +44,8 @@ enum fastrpc_map_flags {
  	FASTRPC_MAP_FD = 2,
  	FASTRPC_MAP_FD_DELAYED,
  	FASTRPC_MAP_FD_NOMAP = 16,
+	/* Map the DMA handle in the invoke call for backward compatibility */
+	FASTRPC_MAP_DMA_HANDLE = 0x20000,
  	FASTRPC_MAP_MAX,
  };
 
reviewer
#4 lore raw
Quoted 152 lines · Jianping
Quoted 141 lines · Ekansh Gupta
Quoted 140 lines · Jianping Li

DMA handles passed as invoke arguments (scalars beyond nbufs) may refer to the same dma_buf fd as an input/output buffer argument. Taking an extra reference for such DMA handle maps leads to duplicate mappings and an unbalanced reference count, since DMA handle maps are released separately when the DSP returns the fd through the fdlist.

Fix this by not taking an extra reference for DMA handle arguments (take_ref = false) and tagging them with FASTRPC_MAP_DMA_HANDLE. As these maps are borrowed references, fastrpc_get_args() re-validates the map via fastrpc_map_lookup() before dereferencing it, so it is not used after being freed. fastrpc_put_args() only releases maps flagged as FASTRPC_MAP_DMA_HANDLE and clears the flag to guarantee the map is freed exactly once.

Also reject FASTRPC_MAP_DMA_HANDLE in fastrpc_req_mem_map(), since such handles are already mapped implicitly during the remote invoke call and must not be mapped again through the explicit MEM_MAP path.

Fixes 10df039834f84 ("misc: fastrpc: Skip reference for DMA handles")

---
Patch [v1]: https://lore.kernel.org/all/20260625080832.17477-1-
[email protected]/

Changes in v2:
- Rework the commit message to describe the DMA handle reference and lifetime problem more precisely.
- Introduce a new FASTRPC_MAP_DMA_HANDLE uapi flag and a 'flags' field in struct fastrpc_map to explicitly tag DMA handle maps, instead of relying only on the nbufs boundary / take_ref.
- Plumb an mflags argument through fastrpc_map_create() and fastrpc_map_attach() so DMA handle maps are tagged at creation time.
- Re-validate the borrowed map in fastrpc_get_args() via fastrpc_map_lookup() before dereferencing it, to avoid a use-after-free when the map was created with take_ref = false.
- In fastrpc_put_args(), only release maps tagged FASTRPC_MAP_DMA_HANDLE and clear the flag afterwards, so such maps are freed exactly once.
- Reject FASTRPC_MAP_DMA_HANDLE in fastrpc_req_mem_map(), since these handles are already mapped implicitly during the remote invoke and must not be mapped again through the explicit MEM_MAP path.
---
  drivers/misc/fastrpc.c      | 56 ++++++++++++++++++++++++++----------- include/uapi/misc/fastrpc.h |  2 ++

  2 files changed, 42 insertions(+), 16 deletions(-)
drivers/misc/fastrpc.c +1 -0
index d86e79134c68..fa8c58a97e35 100644
@@ -223,6 +223,7 @@ struct fastrpc_map {
       u64 len;
       u64 raddr;
       u32 attr;
+    u32 flags;
       struct kref refcount;
   };

  @@ -833,7 +834,7 @@ static dma_addr_t fastrpc_compute_dma_addr(struct fastrpc_user *fl, dma_addr_t s }
    static int fastrpc_map_attach(struct fastrpc_user *fl, int fd, -                  u64 len, u32 attr, struct fastrpc_map **ppmap) +                  u64 len, u32 attr, struct fastrpc_map **ppmap, int mflags)
  {
      struct fastrpc_session_ctx *sess = fl->sctx;
      struct fastrpc_map *map = NULL;

drivers/misc/fastrpc.c +5 -2
@@ -850,6 +851,7 @@ static int fastrpc_map_attach(struct fastrpc_user
 *fl, int fd,
         map->fl = fl;
       map->fd = fd;
+    map->flags = mflags;
       map->buf = dma_buf_get(fd);
       if (IS_ERR(map->buf)) {
           err = PTR_ERR(map->buf);
@@ -924,13 +926,13 @@ static int fastrpc_map_attach(struct
 fastrpc_user *fl, int fd,
       return err;
   }
   -static int fastrpc_map_create(struct fastrpc_user *fl, int fd,
-                  u64 len, u32 attr, struct fastrpc_map **ppmap)
+static int fastrpc_map_create(struct fastrpc_user *fl, int fd, u64
 len, u32 attr,
+                  struct fastrpc_map **ppmap, bool take_ref, int
 mflags)
   {
-    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);
+    return fastrpc_map_attach(fl, fd, len, attr, ppmap, mflags);

  }
    /*

drivers/misc/fastrpc.c +2 -0
@@ -1000,23 +1002,25 @@ static int fastrpc_create_maps(struct
 fastrpc_invoke_ctx *ctx)
       int i, err;
         for (i = 0; i < ctx->nscalars; ++i) {
+        bool take_ref = i < ctx->nbufs;
+        int mflags = 0;

            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-

Quoted 1 line

maps[i]);

-        else
-            err = fastrpc_map_attach(ctx->fl, ctx->args[i].fd, -                 ctx->args[i].length, ctx->args[i].attr, &ctx-

Quoted 1 line

maps[i]);

+        /* Set the DMA handle mapping flag for DMA handles */ +        if (i >= ctx->nbufs)
+            mflags = FASTRPC_MAP_DMA_HANDLE;
+
+        err = fastrpc_map_create(ctx->fl, ctx->args[i].fd, ctx-

Quoted 1 line

args[i].length,

+                     ctx->args[i].attr, &ctx->maps[i], take_ref, mflags);
          if (err) {
              dev_err(dev, "Error Creating map %d\n", err); return -EINVAL;
          }
-
      }
+
      return 0;
  }
  @@ -1144,6 +1148,16 @@ static int fastrpc_get_args(u32 kernel, struct fastrpc_invoke_ctx *ctx)
          list[i].num = ctx->args[i].length ? 1 : 0; list[i].pgidx = i;
          if (ctx->maps[i]) {
+            /* It is possible that map is created with +             * mflags FASTRPC_MAP_DMA_HANDLE and take_ref +             * is false. Check if map still exists or is +             * being freed as take_ref is false
+             */
+            if (fastrpc_map_lookup(ctx->fl, ctx->args[i].fd, +                           &ctx->maps[i], false)) {
+                ctx->maps[i] = NULL;

Quoted 8 lines
Quoted 2 lines

+                return -EINVAL;
+            }

if map is created by R1 call with DMA handle flag, when any inbuf/outbuf carries the same fd for R2 call, wouldn't the refcount get increased as part of fastrpc_create_maps()? So if the refcount is already increased there, why do you need to have a check here?>
pages[i].addr =
ctx->maps[i]->dma_addr;

Quoted 13 lines

The check is not for the case you described (same fd reused as an in/out buffer in a later call). In that case i < nbufs so the map is created with take_ref = true and the refcount is indeed held, and the re-lookup simply passes.
The check is needed for DMA handle args themselves, which are created with take_ref = false. For those maps this context holds only a borrowed reference — the owning reference is dropped when the DSP returns the fd via fdlist in fastrpc_put_args(), which may run from a concurrent invocation. So between fastrpc_create_maps() and fastrpc_get_args() the map may already have been freed, and dereferencing ctx->maps[i] would be a use-after-free. The re-lookup re-validates that the map is still present before we dereference it.
(If you believe there is no competition, I will drop this code.)

Okay, I see this is only for DMA handles. Thanks for the explaination.>>>               pages[i].size = ctx->maps[i]->size;

Quoted 16 lines
Quoted 8 lines
Quoted 8 lines

          }

drivers/misc/fastrpc.c +1 -1
@@ -1200,8 +1214,13 @@ static int fastrpc_put_args(struct
 fastrpc_invoke_ctx *ctx,
       for (i = 0; i < FASTRPC_MAX_FDLIST; i++) {
           if (!fdlist[i])
               break;
-        if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false))
+        /* Validate the map flags for DMA handles and skip freeing
Quoted 8 lines
Quoted 5 lines

map if invalid */
+        if (!fastrpc_map_lookup(fl, (int)fdlist[i], &mmap, false) && +            mmap->flags == FASTRPC_MAP_DMA_HANDLE) {
+            /* Allow DMA handle maps to free only once */ +            mmap->flags = 0;

flags are getting updated without any locks, this is considering that DSP can updated a particular fd on fdlist only once. Can you add this information here?>              fastrpc_map_put(mmap);

Quoted 4 lines

Agreed, this relies on the DSP reporting a given fd in the fdlist only once, so no concurrent fastrpc_put_args() can update the same map's flags. I'll add a comment documenting this assumption in v3. Will send v3 with the clarified comments.

Thanks!

//ekansh>>> +        }

Quoted 47 lines
Quoted 46 lines
Quoted 45 lines

      }
        return ret;

drivers/misc/fastrpc.c +1 -1
@@ -1511,7 +1530,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, 0);
           if (err)

              goto err;
      }

drivers/misc/fastrpc.c +2 -1
@@ -2107,9 +2126,14 @@ static int fastrpc_req_mem_map(struct
 fastrpc_user *fl, char __user *argp)
         if (copy_from_user(&req, argp, sizeof(req)))
           return -EFAULT;
-
+    /*
+     * Prevent mapping backward compatible DMA handles here, as they

are
+     * already mapped in the remote call.
+     */
+    if (req.flags == FASTRPC_MAP_DMA_HANDLE)
+        return -EINVAL;
      /* 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, 0); if (err) {
          dev_err(dev, "failed to map buffer, fd = %d\n", req.fd); return err;

include/uapi/misc/fastrpc.h +1 -0
index c6e2925f47e6..142ddaeed85f 100644
@@ -44,6 +44,8 @@ enum fastrpc_map_flags {
       FASTRPC_MAP_FD = 2,
       FASTRPC_MAP_FD_DELAYED,
       FASTRPC_MAP_FD_NOMAP = 16,
+    /* Map the DMA handle in the invoke call for backward

compatibility */
+    FASTRPC_MAP_DMA_HANDLE = 0x20000,
      FASTRPC_MAP_MAX,
  };