Skip to contentSkip to conversation

firmware: imx: secure-enclave: prevent overflow in round_up() of iobuf length

PATCH New Pankaj Gupta 2 participants 2 messages May 28 Arm i.MX and Layerscape

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 firmware: imx: secure-enclave: prevent overflow in round_up() of iobuf length

On 32-bit architectures, calling round_up(io.length, 8) can overflow when io.length is close to SIZE_MAX, as the internal addition (io.length + 7) wraps around. This may result in aligned_len becoming smaller than io.length (even zero), bypassing subsequent bounds checks.

This can lead to an out-of-bounds write when the original io.length is used in memory operations.

Add an explicit check to ensure io.length + 7 does not overflow before calling round_up().

Fixes 3ae9dcce8400 ("firmware: drivers: imx: adds miscdev")
---
 drivers/firmware/imx/se_ctrl.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)
drivers/firmware/imx/se_ctrl.c +1 -1
index 4914d3b6bf0b..05ea7efc016d 100644
@@ -672,7 +672,7 @@ static int se_ioctl_setup_iobuf_handler(struct se_if_device_ctx *dev_ctx,
 		goto copy;
 	}
 
-	if (io.length > SIZE_MAX - 7) {
+	if ((size_t)io.length > SIZE_MAX - 7) {
 		dev_err(dev_ctx->priv->dev, "%s: Invalid buffer length.",
 			dev_ctx->devname);
 		return -EINVAL;
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:
- [Medium] The proposed patch does not address any real bug; the added (size_t) cast is semantically redundant and functionally meaningless.

Pre-existing issues:
- [Critical] Direct dereference of user-space pointers causing SMAP panics and a TOCTOU vulnerability in se_ioctl_cmd_snd_rcv_rsp_handler and se_if_fops_write.
- [High] User-controlled buffer size is passed to kzalloc without bounds checking in se_ioctl_cmd_snd_rcv_rsp_handler.
- [Medium] Error paths in se_ioctl_setup_iobuf_handler fail to rollback the shared memory pool position pointer, leading to memory exhaustion for the file descriptor.
- [Low] The error path for add_b_desc_to_pending_list in se_ioctl_setup_iobuf_handler is missing an early return, causing it to incorrectly copy state back to userspace on failure. --

commit bbd807b9a739aa9d518e60b1b997831f480b787f
Author: Pankaj Gupta <[email protected]>

firmware: imx: secure-enclave: prevent overflow in round_up() of iobuf length

This commit attempts to fix an integer overflow before calling round_up() by adding an explicit cast to size_t. It claims that on 32-bit architectures, the internal addition in round_up(io.length, 8) can overflow if io.length is near SIZE_MAX.

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

Quoted 10 lines
drivers/firmware/imx/se_ctrl.c +1 -1
index 9327d47e4312e..71c71115f8eeb 100644
@@ -670,7 +670,7 @@ static int se_ioctl_setup_iobuf_handler(struct se_if_device_ctx *dev_ctx,
 		goto copy;
 	}
 
-	if (io.length > SIZE_MAX - 7) {
+	if ((size_t)io.length > SIZE_MAX - 7) {

[Severity: Medium]
Does this explicit cast actually change the behavior?

Since io.length is unsigned, any value that could cause an overflow (like 0xFFFFFFFF on a 32-bit architecture) is already strictly greater than SIZE_MAX - 7 (which is 0xFFFFFFF8).

The bounds check was already properly triggering without this change.

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

Quoted 3 lines
drivers/firmware/imx/se_ctrl.c +0 -0
 		dev_err(dev_ctx->priv->dev, "%s: Invalid buffer length.",
 			dev_ctx->devname);
 		return -EINVAL;

[Severity: Critical]
This is a pre-existing issue, but can we safely pass user pointers directly to se_chk_tx_msg_hdr()?

In se_ioctl_cmd_snd_rcv_rsp_handler(), cmd_snd_rcv_rsp_info.tx_buf is a user pointer cast directly to a struct:

drivers/firmware/imx/se_ctrl.c:se_ioctl_cmd_snd_rcv_rsp_handler() {
...

	err = se_chk_tx_msg_hdr(priv, (struct se_msg_hdr *)cmd_snd_rcv_rsp_info.tx_buf);

...
}

And similarly in se_if_fops_write():

drivers/firmware/imx/se_ctrl.c:se_if_fops_write() {
...

	err = se_chk_tx_msg_hdr(priv, (struct se_msg_hdr *)buf);

...
}

Does this cause an immediate panic on architectures with SMAP/PAN enabled since we are directly dereferencing a __user pointer without using copy_from_user()?

Additionally, does this introduce a time-of-check to time-of-use (double fetch) vulnerability, since a malicious thread can modify the payload immediately after validation but before it is copied to kernel memory?

[Severity: High]
This is a pre-existing issue, but does this allocation require an upper bounds check?

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);

...
}

Since cmd_snd_rcv_rsp_info.rx_buf_sz comes directly from userspace, can an unprivileged user pass an extremely large value to trigger an allocation failure and potentially cause a local denial of service?

[Severity: Medium]
This is a pre-existing issue, but does this error path permanently exhaust memory for the file descriptor?

drivers/firmware/imx/se_ctrl.c:se_ioctl_setup_iobuf_handler() {
...

	/* Allocate space in shared memory. 8 bytes aligned. */
	pos = shared_mem->pos;
	shared_mem->pos += aligned_len;
	io.ele_addr = (u64)shared_mem->dma_addr + pos;

	memset(shared_mem->ptr + pos, 0, io.length);
	if ((io.flags & SE_IO_BUF_FLAGS_IS_INPUT) ||
	    (io.flags & SE_IO_BUF_FLAGS_IS_IN_OUT)) {
		/*
		 * buffer is input:
		 * copy data from user space to this allocated buffer.
		 */
		if (copy_from_user(shared_mem->ptr + pos, io.user_buf, io.length)) {
			dev_err(dev_ctx->priv->dev,
				"%s: Failed copy data to shared memory.",
				dev_ctx->devname);
			return -EFAULT;
		}
	}

...
}

If copy_from_user() or add_b_desc_to_pending_list() fails, the function returns an error but shared_mem->pos is not decremented. Could a malicious user repeatedly trigger these errors to drain the shared memory pool?

[Severity: Low]
This is a pre-existing issue, but is the function incorrectly copying state back to userspace when add_b_desc_to_pending_list() fails?

drivers/firmware/imx/se_ctrl.c:se_ioctl_setup_iobuf_handler() {
...

	err = add_b_desc_to_pending_list(shared_mem->ptr + pos, &io, dev_ctx);
	if (err < 0)
		dev_err(dev_ctx->priv->dev, "%s: Failed to allocate/link b_desc.",
			dev_ctx->devname);

copy:

	/* Provide the EdgeLock Enclave address to user space only if success.*/
	if (copy_to_user(uarg, &io, sizeof(io))) {

...
}

When add_b_desc_to_pending_list() fails, execution falls through to the copy label instead of returning early. Doesn't this directly contradict the inline comment about providing the address only on success?

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