Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Arnaud Pouliquen" <arnaud.pouliquen@foss.st.com>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH v23 4/6] remoteproc: Add TEE support
Date: Wed, 02 Dec 2026 15:10:30 +0000	[thread overview]
Message-ID: <sashiko-outbox-246060@kernel.org> (raw)
In-Reply-To: <20261202-rproc_tee_v23-b4-v23-4-bac8e36eb405@foss.st.com>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Medium] remoteproc: tee: missing resource table bounds check in
  rproc_tee_parse_fw()
- [Medium] remoteproc: tee: unsafe table_sz update in
  rproc_tee_find_loaded_rsc_table()
- [Medium] remoteproc: tee: duplicate firmware release when
  rproc_tee_parse_fw() fails
- [Medium] remoteproc: tee: missing session_open checks in
  rproc_tee_register() and ops
- [Medium] remoteproc: tee: interruptible lock in rproc_tee_stop() on
  teardown
- [Medium] remoteproc: tee: out-of-bounds read in rproc_tee_parse_fw()
  on truncated table
- [Low] remoteproc: tee: unlocked rproc_tee_ctx.dev access in
  rproc_tee_parse_fw()
--

commit 8c9b0f1db642d51c414b231f4b341810c93277ab
Author: Arnaud Pouliquen <arnaud.pouliquen@foss.st.com>

remoteproc: Add TEE support

This commit adds a remoteproc TEE client driver that delegates remote
processor firmware authentication, loading, and lifecycle management to a
Trusted Application in OP-TEE.

> diff --git a/drivers/remoteproc/remoteproc_tee.c b/drivers/remoteproc/remoteproc_tee.c
> new file mode 100644
> index 0000000000000..a9e9cdb7f6579
> --- /dev/null
> +++ b/drivers/remoteproc/remoteproc_tee.c

[ ... ]

> +int rproc_tee_parse_fw(struct rproc *rproc, const struct firmware *fw)
> +{
> +	phys_addr_t rsc_table;
> +	void *rsc_va;
> +	size_t table_sz;
> +	int ret;
> +
> +	if (!rproc)
> +		return -EINVAL;
> +
> +	/* We need first to Load the firmware, to be able to get the resource table. */
> +	ret = rproc_tee_load_fw(rproc, fw);
> +	if (ret)
> +		return ret;
> +
> +	ret = rproc_tee_get_loaded_rsc_table(rproc, &rsc_table, &table_sz);
> +	if (ret)
> +		goto release_fw;
> +
> +	/* A missing resource table is valid for some firmware images. */
> +	if (!table_sz) {
> +		rproc->table_ptr = NULL;
> +		rproc->table_sz = 0;
> +		return 0;
> +	}
> +
> +	/*
> +	 * We assume here that the memory mapping is the same between the TEE
> +	 * and Linux kernel contexts. Else a new TEE remoteproc service could be
> +	 * needed to get a copy of the resource table.
> +	 */
> +	rsc_va = memremap(rsc_table, table_sz, MEMREMAP_WC);
> +	if (!rsc_va) {
> +		dev_err(rproc_tee_ctx.dev, "Unable to map memory region: %pa+%zx\n",
> +			&rsc_table, table_sz);
> +		ret = -ENOMEM;
> +		goto release_fw;
> +	}

[Severity: Low]
Is rproc_tee_ctx.dev safe to read here without holding ctx_lock?
rproc_tee_remove() clears rproc_tee_ctx.dev under ctx_lock, so logging with
&rproc->dev instead avoids a race if the TEE device unbinds concurrently.

> +
> +	/*
> +	 * Create a copy of the resource table to have the same behavior as the
> +	 * ELF loader. This cached table will be used by the remoteproc core
> +	 * after the remoteproc stops to free resources and for crash recovery
> +	 * to reapply the settings.
> +	 * The cached table will be freed by the remoteproc core.
> +	 */
> +	rproc->cached_table = kmemdup(rsc_va, table_sz, GFP_KERNEL);
> +	memunmap(rsc_va);
> +
> +	if (!rproc->cached_table) {
> +		ret = -ENOMEM;
> +		goto release_fw;
> +	}

[Severity: Medium]
Should rproc_tee_parse_fw() validate the resource table header and offset
array against table_sz the way find_table() in remoteproc_elf_loader.c does?
In addition, rproc_tee_get_loaded_rsc_table() checks pa > PHYS_ADDR_MAX and
rproc_u64_fit_in_size_t(size) separately without checking whether pa + size
overflows before memremap().

> +
> +	rproc->table_ptr = rproc->cached_table;
> +	rproc->table_sz = table_sz;

[Severity: Medium]
Can setting rproc->table_ptr and rproc->table_sz without checking that
table_sz covers sizeof(struct resource_table) and struct_size(table, offset,
table->num) cause an out-of-bounds slab read of rproc->cached_table?

When rproc_fw_boot() subsequently runs rproc_handle_resources(),
rsc_table_for_each_entry() reads table->num and table->offset[i] before
checking individual entry offsets against table_sz:

include/linux/rsc_table.h:rsc_table_for_each_entry() {
	...
	for (i = 0; i < table->num; i++) {
		u32 offset = table->offset[i];
	...
}

> +
> +	return 0;
> +
> +release_fw:
> +	rproc_tee_release_fw(rproc);
> +	return ret;
> +}

[Severity: Medium]
Does calling rproc_tee_release_fw() here release the firmware twice when
rproc_tee_parse_fw() fails?

When rproc_parse_fw() returns an error to rproc_fw_boot(), rproc_boot() also
calls rproc_release_fw(rproc) on its error path, invoking
TA_RPROC_CMD_RELEASE_FW a second time.

[ ... ]

> +struct resource_table *rproc_tee_find_loaded_rsc_table(struct rproc *rproc,
> +						       const struct firmware *fw)
> +{
> +	phys_addr_t rsc_table;
> +	size_t table_sz;
> +	int ret;
> +
> +	ret = rproc_tee_get_loaded_rsc_table(rproc, &rsc_table, &table_sz);
> +	if (ret)
> +		return NULL;
> +
> +	rproc->table_sz = table_sz;
> +	if (!table_sz)
> +		return NULL;
> +
> +	/*
> +	 * At this step the memory area that contains the resource table should
> +	 * have been registered by the remote proc platform driver and allocated
> +	 * by rproc_alloc_registered_carveouts().
> +	 */
> +	return rproc_pa_to_va(rproc, rsc_table, table_sz, NULL);
> +}

[Severity: Medium]
Can overwriting rproc->table_sz here trigger a NULL pointer dereference or
out-of-bounds read in rproc_start()?

Right after rproc_find_loaded_rsc_table() returns a non-NULL pointer,
rproc_start() runs:

drivers/remoteproc/remoteproc_core.c:rproc_start() {
	...
	loaded_table = rproc_find_loaded_rsc_table(rproc, fw);
	if (loaded_table) {
		memcpy(loaded_table, rproc->cached_table, rproc->table_sz);
		rproc->table_ptr = loaded_table;
	}
	...
}

During crash recovery, rproc_boot_recovery() reloads the firmware image and
calls rproc_start() directly without re-running rproc_parse_fw().  If the new
table_sz is larger than the rproc->cached_table allocated at initial boot (or
non-zero when initial boot had no resource table and left rproc->cached_table
NULL), memcpy() reads past rproc->cached_table or dereferences NULL.  Updating
rproc->table_sz before rproc_pa_to_va() also leaves rproc->table_sz out of
sync with rproc->cached_table if rproc_pa_to_va() returns NULL.

[ ... ]

> +int rproc_tee_stop(struct rproc *rproc)
> +{
> +	struct tee_param param[MAX_TEE_PARAM_ARRAY_MEMBER];
> +	struct rproc_tee *trproc;
> +	struct tee_ioctl_invoke_arg arg;
> +	int ret;
> +
> +	ret = mutex_lock_interruptible(&ctx_lock);
> +	if (ret)
> +		return ret;

[Severity: Medium]
Should rproc_tee_stop() use uninterruptible mutex_lock(&ctx_lock) like the
other teardown paths?

If a signal is pending when rproc_start() rolls back after a subdevice start
failure (or when rproc_del() shuts down the remote processor during
rproc_tee_unregister()), mutex_lock_interruptible() returns -EINTR without
sending TA_RPROC_CMD_STOP.  rproc_start() ignores the return value of
rproc->ops->stop(), so rproc_fw_boot() and rproc_boot() proceed to free
carveouts and call rproc_tee_release_fw() while the remote processor is still
running.

[ ... ]

> +	list_add(&trproc->node, &rproc_tee_ctx.rproc_list);
> +	mutex_unlock(&ctx_lock);
> +
> +	ret = rproc_add(rproc);
> +	if (ret)
> +		goto del_trproc;
> +
> +	return 0;

[Severity: Medium]
What happens if rproc_tee_remove() runs while ctx_lock is dropped across
rproc_add(), or if the TEE driver re-probes while a previously registered
trproc remains on rproc_tee_ctx.rproc_list?

If rproc_tee_remove() runs during rproc_add(), it closes trproc->session_id
and sets trproc->session_open = false, but rproc_tee_register() still returns
0 with a closed session.  Furthermore, because rproc_tee_remove() leaves
registered entries on rproc_tee_ctx.rproc_list with session_open = false, if
rproc_tee_probe() later repopulates rproc_tee_ctx.dev and
rproc_tee_ctx.tee_ctx, neither rproc_to_trproc() nor the TEE command helpers
check trproc->session_open before invoking tee_client_invoke_func() with the
stale session_id.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261202-rproc_tee_v23-b4-v23-0-bac8e36eb405@foss.st.com?part=4

  reply	other threads:[~2026-12-02 15:10 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-12-02 14:48 [PATCH v23 0/6] Introduction of a remoteproc tee to load signed firmware Arnaud Pouliquen
2026-12-02 14:48 ` [PATCH v23 1/6] dt-bindings: remoteproc: st,stm32-rproc: Add st,rproc-tee Arnaud Pouliquen
2026-12-02 14:48 ` [PATCH v23 2/6] remoteproc: core: Introduce rproc_pa_to_va helper Arnaud Pouliquen
2026-12-02 14:48 ` [PATCH v23 3/6] remoteproc: Introduce optional release_fw operation Arnaud Pouliquen
2026-12-02 15:00   ` sashiko-bot
2026-12-02 14:48 ` [PATCH v23 4/6] remoteproc: Add TEE support Arnaud Pouliquen
2026-12-02 15:10   ` sashiko-bot [this message]
2026-12-02 14:48 ` [PATCH v23 5/6] remoteproc: stm32: Create sub-functions to request shutdown and release Arnaud Pouliquen
2026-12-02 14:48 ` [PATCH v23 6/6] remoteproc: stm32: add OP-TEE backend support Arnaud Pouliquen

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=sashiko-outbox-246060@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=arnaud.pouliquen@foss.st.com \
    --cc=conor+dt@kernel.org \
    --cc=devicetree@vger.kernel.org \
    --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