Linux kernel and device drivers for NXP i.MX platforms
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: pankaj.gupta@oss.nxp.com
Cc: imx@lists.linux.dev, conor+dt@kernel.org, robh@kernel.org,
	devicetree@vger.kernel.org, Frank.Li@kernel.org
Subject: Re: [PATCH v49 5/7] firmware: imx: adds miscdev
Date: Tue, 08 Sep 2026 12:05:38 +0000	[thread overview]
Message-ID: <20260908120539.378D91F00A3A@smtp.kernel.org> (raw)
In-Reply-To: <20260908-imx-se-if-v49-5-a59529118839@nxp.com>

Thank you for your contribution! Sashiko AI review found 2 potential issue(s) to consider:
- [High] Moving `atomic_set(&fbusy_info->fw_busy, 0)` to the end of `se_clear_fw_busy()` causes all session and storage handle cleanup requests to fail, permanently leaking firmware resources.
- [High] Missing upper-bound checks for variable-length input DMA buffers in ELE_OEM_AUTH_CONTAINER_REQ and ELE_KEYSTORE_REPROV_ENABLE_REQ allow malicious userspace to trigger out-of-bounds physical memory reads, potentially crashing the secure enclave or system.
--

commit b1854f8ad905b9bef822cde3f1cab2d887c816c2
Author: Pankaj Gupta <pankaj.gupta@nxp.com>

firmware: imx: adds miscdev

This commit introduces a character device driver to facilitate communication
with the NXP secure enclave (EdgeLock Enclave) from user-space applications.
It manages the shared memory buffers, enforces command whitelisting, and
handles device context cleanup.

> diff --git a/drivers/firmware/imx/ele_common.c b/drivers/firmware/imx/ele_common.c
> --- a/drivers/firmware/imx/ele_common.c
> +++ b/drivers/firmware/imx/ele_common.c
[ ... ]
> @@ -66,13 +66,13 @@ int se_val_cmd_addrs(struct se_if_device_ctx *dev_ctx, struct se_api_msg *msg,
[ ... ]
> +		if (f->size_idx == SE_CMD_RCVR_ADDR_VAR_SIZE) {
[ ... ]
> +		} else if (f->size_idx != SE_CMD_ADDR_NO_SIZE) {
> +			u64 len;
> +
> +			if (f->size_idx >= payload_words)
> +				return -EINVAL;
> +
> +			/* size_mask == 0 with a valid size_idx is a descriptor bug. */
> +			if (!f->size_mask)
> +				return -EINVAL;
> +
> +			/*
> +			 * Widen to u64 before shifting: size_shift is u8 and
> +			 * shifting a u32 by >= 32 is undefined behaviour.
> +			 */
> +			len = ((u64)msg->data[f->size_idx] >> f->size_shift) & f->size_mask;
> +			if (len > end - addr)
> +				return -EACCES;
> +		} else if (f->buf_size) {
> +			/* buf_size: literal byte count (FW-defined constant). */
> +			if ((u64)f->buf_size > end - addr)
> +				return -EACCES;
> +		}

[Severity: High]
Does this code fail to enforce an upper bound when f->size_idx is
SE_CMD_ADDR_NO_SIZE and f->buf_size is 0?

If a malicious caller provides a DMA address very close to the end of the
window (e.g., end - 1), it seems the firmware might read past the allocated
shared memory boundary. Could this lead to an out-of-bounds physical memory
read over the DMA bus?

[ ... ]
> @@ -529,13 +529,13 @@ int ele_msg_send_rcv(struct se_if_device_ctx *dev_ctx, void *tx_msg,
[ ... ]
> +	if (atomic_read(&fbusy_info->fw_busy)) {
> +		spin_unlock_irqrestore(&priv->waiting_rsp_clbk_hdl.clbk_rx_lock, flags);
> +		dev_dbg(priv->dev, "%s: ELE became unresponsive.\n", dev_ctx->devname);
> +		return -EBUSY;
> +	}

[ ... ]
> diff --git a/drivers/firmware/imx/ele_msg_addr_field.c b/drivers/firmware/imx/ele_msg_addr_field.c
> --- a/drivers/firmware/imx/ele_msg_addr_field.c
> +++ b/drivers/firmware/imx/ele_msg_addr_field.c
> @@ -20,13 +20,13 @@ static const struct se_cmd_addr_field ele_get_info_addr_fields[] = {
[ ... ]
> +static const struct se_cmd_addr_field ele_oem_auth_cntr_addr_fields[] = {
> +	/*
> +	 * Container Header address: a 64-bit physical address split across two
> +	 * words. data[0] holds the 32-bit MSB and data[1] holds the 32-bit LSB
> +	 * (ELE API spec Table 27, word size = 0x3, so the command is header +
> +	 * MSB + LSB only). The message carries no length word for this buffer;
> +	 * the container size is variable and not communicated in the MU payload,
> +	 * and no static firmware-defined maximum is specified. Because this is a
> +	 * read-only input buffer (the ELE ROM/FW copies the container header
> +	 * into its internal memory for authentication and does not write back
> +	 * through this address), enforcing only the start-address range check is
> +	 * acceptable: a rogue caller can at most cause firmware to read within
> +	 * the shared-memory window, which is memory the caller already owns.
> +	 * Output buffers must be fully bounded; input-only buffers are safe with
> +	 * addr-only checks.
> +	 */
> +	{ .lsb_idx = 1, .msb_idx = 0, .has_msb = true, .flag_idx = SE_CMD_ADDR_ALWAYS,
> +	  .size_idx = SE_CMD_ADDR_NO_SIZE },			/* container_hdr_addr */
> +};

[Severity: High]
Is the assumption in this comment correct?

Even for an input-only buffer, if there is no length bound checked here, a
rogue caller could supply a valid start address immediately preceding an
unmapped or restricted physical memory region. The firmware reading the
variable-length container header could then cross the boundary, triggering a
bus error, IOMMU fault, or SError that might crash the system or enclave.

[ ... ]
> @@ -125,13 +125,13 @@ static const struct se_cmd_addr_field ele_pub_key_export_addr_fields[] = {
[ ... ]
> +static const struct se_cmd_addr_field ele_keystore_reprov_en_addr_fields[] = {
> +	/*
> +	 * Signed message address: a 64-bit physical address split across two
> +	 * words. data[0] holds the 32-bit MSB and data[1] holds the 32-bit LSB
> +	 * (ELE API spec Table 202, word size = 0x3, so the command is header +
> +	 * MSB + LSB only). The address points to the start of the complete
> +	 * signed message block (header + 12-byte payload from Table 204 +
> +	 * signature); the total block size depends on the signing format and is
> +	 * not carried anywhere in the MU payload words. No static
> +	 * firmware-defined maximum for the full block is specified. Because this
> +	 * is a read-only input buffer (firmware reads and verifies the signed
> +	 * block, does not write back through this address), enforcing only the
> +	 * start-address range check is acceptable: a rogue caller can at most
> +	 * cause firmware to read within the shared-memory window, which is
> +	 * memory the caller already owns. Output buffers must be fully bounded;
> +	 * input-only buffers are safe with addr-only checks.
> +	 */
> +	{ .lsb_idx = 1, .msb_idx = 0, .has_msb = true, .flag_idx = SE_CMD_ADDR_ALWAYS,
> +	  .size_idx = SE_CMD_ADDR_NO_SIZE },			/* signed_msg_addr */
> +};

[Severity: High]
Similar to the container header check in ele_oem_auth_cntr_addr_fields, could
this lack of an upper bound size check allow malicious userspace to trigger
out-of-bounds physical memory reads by the firmware during reprovisioning
operations?

[ ... ]
> diff --git a/drivers/firmware/imx/se_ctrl.c b/drivers/firmware/imx/se_ctrl.c
> --- a/drivers/firmware/imx/se_ctrl.c
> +++ b/drivers/firmware/imx/se_ctrl.c
> @@ -1070,13 +1070,13 @@ static int se_if_misc_register(struct se_if_priv *priv)
[ ... ]
> +static void se_clear_fw_busy(struct se_if_priv *priv)
> +{
[ ... ]
> +			} else if (dev_ctx->cleanup_done) {
> +				/*
> +				 * Userspace fd already closed while fw_busy was
> +				 * armed (e.g. SIGKILL). FW has now responded;
> +				 * close deferred handles and free the DMA buf.
> +				 */
> +				if (dev_ctx->strg_hdl &&
> +				    se_close_storage(priv->priv_dev_ctx,
> +						     dev_ctx->strg_hdl))
> +					dev_err(priv->dev,
> +						"%s: failed to close deferred storage handle\n",
> +						dev_ctx->devname);
> +				if (dev_ctx->sess_hdl &&
> +				    se_close_session(priv->priv_dev_ctx,
> +						     dev_ctx->sess_hdl))
> +					dev_err(priv->dev,
> +						"%s: failed to close deferred session handle\n",
> +						dev_ctx->devname);
> +				dev_ctx->strg_hdl = 0;
> +				dev_ctx->sess_hdl = 0;
> +				cleanup_se_shared_mem(dev_ctx, true);
> +			} else {
[ ... ]
> +	spin_lock_irqsave(&fbusy_info->fw_busy_lock, flags);
> +	atomic_set(&fbusy_info->fw_busy, 0);
> +	spin_unlock_irqrestore(&fbusy_info->fw_busy_lock, flags);
> +	kref_put(&dev_ctx->refcount, se_if_dev_ctx_release);
> +}

[Severity: High]
Will this cleanup path accidentally leak the handles it intends to close?

In se_clear_fw_busy(), atomic_set(&fbusy_info->fw_busy, 0) is deferred until
the very end of the function. However, the calls to se_close_storage() and
se_close_session() both rely on ele_msg_send_rcv().

Because ele_msg_send_rcv() checks atomic_read(&fbusy_info->fw_busy) and
returns -EBUSY immediately if it is set (as quoted earlier from
drivers/firmware/imx/ele_common.c), won't these close requests be rejected
without ever reaching the firmware? This could allow a userspace process
repeatedly killed by SIGKILL to exhaust the secure enclave's handles.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260908-imx-se-if-v49-0-a59529118839@nxp.com?part=5

  reply	other threads:[~2026-09-08 12:05 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-08 17:18 [PATCH v49 0/7] firmware: imx: driver for NXP secure-enclave pankaj.gupta
2026-09-08 17:18 ` [PATCH v49 1/7] Documentation/firmware: add imx/se to other_interfaces pankaj.gupta
2026-09-08 17:18 ` [PATCH v49 2/7] dt-bindings: arm: fsl: add imx-se-fw binding doc pankaj.gupta
2026-09-08 17:18 ` [PATCH v49 3/7] firmware: imx: add driver for NXP EdgeLock Enclave pankaj.gupta
2026-09-08 12:03   ` sashiko-bot
2026-09-08 17:18 ` [PATCH v49 4/7] firmware: imx: device context dedicated to priv pankaj.gupta
2026-09-08 17:18 ` [PATCH v49 5/7] firmware: imx: adds miscdev pankaj.gupta
2026-09-08 12:05   ` sashiko-bot [this message]
2026-09-08 17:18 ` [PATCH v49 6/7] arm64: dts: imx8ulp: add secure enclave node pankaj.gupta
2026-09-08 12:00   ` sashiko-bot
2026-09-08 17:18 ` [PATCH v49 7/7] arm64: dts: imx8ulp: add reserved memory for EdgeLock Enclave pankaj.gupta

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260908120539.378D91F00A3A@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Frank.Li@kernel.org \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --cc=imx@lists.linux.dev \
    --cc=pankaj.gupta@oss.nxp.com \
    --cc=robh@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox