Skip to contentSkip to conversation

firmware: imx: se_ctrl: roll back iobuf reservation on setup failure

PATCH New [email protected] 2 participants 2 messages Jun 11 Arm i.MX and Layerscape

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 firmware: imx: se_ctrl: roll back iobuf reservation on setup failure

From: Pankaj Gupta <[email protected]>

se_ioctl_setup_iobuf_handler() reserves space from the per-file shared memory pool before copying user data and linking a buffer descriptor.

If copy_from_user() or add_b_desc_to_pending_list() fails after the reservation, the function returns without restoring shared_mem->pos, which permanently consumes part of the shared memory pool for that file descriptor.

Also, add_b_desc_to_pending_list() failure currently falls through to the copy_to_user() path, which can return an ELE address to userspace even though setup failed.

Fix this by rolling back the reserved shared-memory position on pre-link failures and by unlinking the descriptor before rolling back the pool reservation when copy_to_user() fails after descriptor insertion.

Fixes b87b30bbdfb2 ("firmware: drivers: imx: adds miscdev")
---
 drivers/firmware/imx/se_ctrl.c | 37 +++++++++++++++++++++++++---------
 1 file changed, 27 insertions(+), 10 deletions(-)
drivers/firmware/imx/se_ctrl.c +27 -10
index 5b23485fe42c..e42e5e23f48a 100644
@@ -365,16 +365,16 @@ static void se_dev_ctx_shared_mem_cleanup(struct se_if_device_ctx *dev_ctx)
 	se_shared_mem_mgmt->non_secure_mem.pos = 0;
 }
 
-static int add_b_desc_to_pending_list(void *shared_ptr_with_pos,
-				      struct se_ioctl_setup_iobuf *io,
-				      struct se_if_device_ctx *dev_ctx)
+static struct se_buf_desc *add_b_desc_to_pending_list(void *shared_ptr_with_pos,
+						      struct se_ioctl_setup_iobuf *io,
+						      struct se_if_device_ctx *dev_ctx)
 {
 	struct se_shared_mem_mgmt_info *se_shared_mem_mgmt = &dev_ctx->se_shared_mem_mgmt;
-	struct se_buf_desc *b_desc;
+	struct se_buf_desc *b_desc = NULL;
 
 	b_desc = kzalloc_obj(*b_desc, GFP_KERNEL);
 	if (!b_desc)
-		return -ENOMEM;
+		return ERR_PTR(-ENOMEM);
 
 	b_desc->shared_buf_ptr = shared_ptr_with_pos;
 	b_desc->usr_buf_ptr = io->user_buf;
@@ -398,7 +398,7 @@ static int add_b_desc_to_pending_list(void *shared_ptr_with_pos,
 		list_add_tail(&b_desc->link, &se_shared_mem_mgmt->pending_out);
 	}
 
-	return 0;
+	return b_desc;
 }
 
 /* interface for managed res to unregister a character device */
@@ -641,7 +641,8 @@ static int se_ioctl_setup_iobuf_handler(struct se_if_device_ctx *dev_ctx,
 {
 	struct se_shared_mem *shared_mem = NULL;
 	struct se_ioctl_setup_iobuf io = {0};
-	size_t aligned_len;
+	struct se_buf_desc *b_desc = NULL;
+	size_t aligned_len = 0;
 	int err = 0;
 	u32 pos;
 
@@ -703,14 +704,18 @@ static int se_ioctl_setup_iobuf_handler(struct se_if_device_ctx *dev_ctx,
 			dev_err(dev_ctx->priv->dev,
 				"%s: Failed copy data to shared memory.",
 				dev_ctx->devname);
-			return -EFAULT;
+			err = -EFAULT;
+			goto rollback;
 		}
 	}
 
-	err = add_b_desc_to_pending_list(shared_mem->ptr + pos, &io, dev_ctx);
-	if (err < 0)
+	b_desc = add_b_desc_to_pending_list(shared_mem->ptr + pos, &io, dev_ctx);
+	if (IS_ERR(b_desc)) {
+		err = PTR_ERR(b_desc);
 		dev_err(dev_ctx->priv->dev, "%s: Failed to allocate/link b_desc.",
 			dev_ctx->devname);
+		goto rollback;
+	}
 
 copy:
 	/* Provide the EdgeLock Enclave address to user space only if success.*/
@@ -718,6 +723,18 @@ static int se_ioctl_setup_iobuf_handler(struct se_if_device_ctx *dev_ctx,
 		dev_err(dev_ctx->priv->dev, "%s: Failed to copy iobuff setup to user.",
 			dev_ctx->devname);
 		err = -EFAULT;
+		if (b_desc) {
+			list_del(&b_desc->link);
+			kfree(b_desc);
+		}
+		goto rollback;
+	}
+	return err;
+
+rollback:
+	if (aligned_len) {
+		memset(shared_mem->ptr + pos, 0, aligned_len);
+		shared_mem->pos = pos;
 	}
 
 	return err;
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:

Pre-existing issues:
- [Critical] Integer overflow in round_up() calculation bypasses bounds checks on 32-bit architectures
- [Critical] Use-after-free (UAF) due to implicit mutex_unlock on freed memory in se_if_fops_close()
- [High] Permanent resource leak if a signal is pending during se_if_fops_close()
- [High] List corruption due to improper locking of priv->dev_ctx_list
- [High] Unbounded user-controlled allocation size triggers kernel warning
--

commit 4eb035f3a88362d9a0713a5420bbd761a12dcf45
Author: Pankaj Gupta <[email protected]>

firmware: imx: se_ctrl: roll back iobuf reservation on setup failure

This commit fixes a resource leak in the iobuf setup handler where shared memory reservations were permanently consumed upon failures in user data copying or descriptor linking. It rolls back the shared memory position and unlinks the descriptor appropriately.

Quoted 4 lines
drivers/firmware/imx/se_ctrl.c +0 -0
index 8fab3b7767b7a..f3ac3487e56b1 100644

[ ... ]

drivers/firmware/imx/se_ctrl.c static int se_ioctl_setup_iobuf_handler(struct se_if_device_ctx *dev_ctx, · line 645

Quoted 7 lines
drivers/firmware/imx/se_ctrl.c +2 -1
@@ -641,7 +641,8 @@ static int se_ioctl_setup_iobuf_handler(struct se_if_device_ctx *dev_ctx,
 {
 	struct se_shared_mem *shared_mem = NULL;
 	struct se_ioctl_setup_iobuf io = {0};
-	size_t aligned_len;
+	struct se_buf_desc *b_desc = NULL;
+	size_t aligned_len = 0;

[Severity: Critical]
This isn't a bug introduced by this patch, but in se_ioctl_setup_iobuf_handler() is there a risk of integer overflow when calculating the aligned length?

drivers/firmware/imx/se_ctrl.c:se_ioctl_setup_iobuf_handler() {
    ...
    aligned_len = round_up((size_t)io.length, 8);
    ...
}

On 32-bit architectures, size_t is 32-bit. Passing a value near the maximum 32-bit integer (e.g., 0xfffffff9) causes the round_up calculation to overflow to 0. Consequently, aligned_len becomes 0, and the bounds check:

    if (shared_mem->size < shared_mem->pos ||
        aligned_len > (shared_mem->size - shared_mem->pos)) {
        ...
    }

incorrectly passes. The code then executes:

    memset(shared_mem->ptr + pos, 0, io.length);

unconditionally writing almost 4GB of zeroes and user data into kernel memory. Can this lead to severe memory corruption triggered by any unprivileged user with access to the device node?

Additional pre-existing issues found in this file:

[Severity: Critical]
This isn't a bug introduced by this patch, but in se_if_fops_close() could there be a use-after-free due to the implicit mutex unlock?

drivers/firmware/imx/se_ctrl.c:se_if_fops_close() {
    ...
    scoped_cond_guard(mutex_intr, return -EBUSY, &dev_ctx->fops_lock) {
        ...
        kfree(dev_ctx);
    }
    ...
}

Inside this scope, the dev_ctx object is freed via kfree(dev_ctx). When the scope ends, the guard's implicit destructor invokes mutex_unlock(&dev_ctx->fops_lock). Since the structure containing the mutex has already been freed, does this execute a lock operation on freed memory?

[Severity: High]
This isn't a bug introduced by this patch, but in se_if_fops_close() is there a permanent resource leak if a signal is pending?

drivers/firmware/imx/se_ctrl.c:se_if_fops_close() {
    ...
    scoped_cond_guard(mutex_intr, return -EBUSY, &dev_ctx->fops_lock) {
    ...
}

Because se_if_fops_close() is the release file operation, it attempts to acquire its lock using scoped_cond_guard which can fail if a signal is pending. If the calling process has a pending signal, the lock acquisition immediately fails and returns -EBUSY.

Since the virtual file system ignores the return value of release() and unconditionally destroys the file descriptor, is the cleanup code permanently skipped, leaving dev_ctx orphaned but still linked in the global priv->dev_ctx_list?

[Severity: High]
This isn't a bug introduced by this patch, but is there a risk of list corruption due to improper locking of priv->dev_ctx_list?

drivers/firmware/imx/se_ctrl.c:init_device_context() {
    ...
    list_add_tail(&dev_ctx->link, &priv->dev_ctx_list);
    ...
}
drivers/firmware/imx/se_ctrl.c:se_if_fops_close() {
    ...
    list_del(&dev_ctx->link);
    ...
}

In init_device_context(), the list and priv->active_devctx_count are modified without any locks. However, in se_if_fops_close(), the code modifies them while holding only the per-file dev_ctx->fops_lock.

If multiple processes open or close different file descriptors concurrently, can they execute list modifications without mutual exclusion against each other, potentially causing list corruption?

[Severity: High]
This isn't a bug introduced by this patch, but in se_ioctl_cmd_snd_rcv_rsp_handler() is it safe to use an unvalidated user size for kzalloc?

drivers/firmware/imx/se_ctrl.c:se_ioctl_cmd_snd_rcv_rsp_handler() {
    ...
    struct se_api_msg *rx_msg __free(kfree) =
        kzalloc(cmd_snd_rcv_rsp_info.rx_buf_sz, GFP_KERNEL);
    ...
}

The cmd_snd_rcv_rsp_info.rx_buf_sz is copied directly from user space without an upper bound check. If a user provides a negative value or a very large size exceeding the maximum allocation size, will passing it to kzalloc trigger a page allocation failure warning in the slab allocator?

Signature · 1 line
Sashiko AI review · https://sashiko.dev/#/patchset/[email protected]?part=1