Devicetree
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Armandas Kvietkus" <armandas.kvietkus@proton.me>
Cc: devicetree@vger.kernel.org, robh@kernel.org, conor+dt@kernel.org
Subject: Re: [PATCH 4/4] misc: Add FPC1540 fingerprint sensor driver for QSEE apps
Date: Tue, 24 Nov 2026 18:01:52 +0000	[thread overview]
Message-ID: <sashiko-outbox-231762@kernel.org> (raw)
In-Reply-To: <20261124-fpc1540-tee-v1-4-6c6985f05b06@proton.me>

Thank you for your contribution! Sashiko AI review found 9 potential
issues to consider:
- [High] misc: fpc1540-tee: missing qcom_scm_is_available() check in
  fpc_tee_probe()
- [High] misc: fpc1540-tee: fpc_tee_read() returns 0 events instead of
  -EAGAIN
- [Medium] misc: fpc1540-tee: unbalanced wakeup and IRQ wake on
  fpc_tee_probe() error
- [Medium] misc: fpc1540-tee: unbalanced wakeup and IRQ wake in
  fpc_tee_remove()
- [Medium] misc: fpc1540-tee: ELF header overflow in
  fpc_tee_read_image()
- [Medium] misc: fpc1540-tee: unhandled GPIO error and race in IRQ
  handling
- [Medium] misc: fpc1540-tee: unlocked teardown and UAF race in
  fpc_tee_remove()
- [Medium] misc: fpc1540-tee: unverified size in second pass of
  fpc_tee_read_image()
- [Low] misc: fpc1540-tee: double firmware load and bounce buffer in
  fpc_tee_load()
--

commit 23089f4b85a359ddf10f0fa7ac60bd5d6f60b0d1
Author: Armandas Kvietkus <armandas.kvietkus@proton.me>

misc: Add FPC1540 fingerprint sensor driver for QSEE apps

This commit adds a misc driver for the FPC1540 fingerprint sensor on
Qualcomm platforms, handling power, reset, interrupt reporting, and QSEE
trusted application loading and commands via /dev/fpc_tee.

> diff --git a/drivers/misc/fpc1540-tee.c b/drivers/misc/fpc1540-tee.c
> new file mode 100644
> index 0000000000000..37229190c46af
> --- /dev/null
> +++ b/drivers/misc/fpc1540-tee.c
[ ... ]
> +static void *fpc_tee_read_image(struct fpc_tee *fpc, const char *name,
> +				size_t *mdt_len, size_t *img_len,
> +				size_t *map_len)
> +{
[ ... ]
> +	*map_len = 0;
> +	if (ehdr->e_ident[EI_CLASS] == ELFCLASS64) {
> +		const struct elf64_hdr *eh = (const struct elf64_hdr *)fw->data;
> +		const struct elf64_phdr *ph = (const void *)fw->data + eh->e_phoff;
> +
> +		phnum = eh->e_phnum;
> +		if (eh->e_phoff + phnum * sizeof(*ph) <= fw->size)
> +			for (i = 0; i < phnum; i++)
> +				*map_len = max_t(size_t, *map_len,
> +						 ph[i].p_offset + ph[i].p_filesz);
> +	} else {
> +		const struct elf32_phdr *ph = (const void *)fw->data + ehdr->e_phoff;
> +
> +		phnum = ehdr->e_phnum;
> +		if (ehdr->e_phoff + phnum * sizeof(*ph) <= fw->size)
> +			for (i = 0; i < phnum; i++)
> +				*map_len = max_t(size_t, *map_len,
> +						 ph[i].p_offset + ph[i].p_filesz);
> +	}

[Severity: Medium]
Can a malformed .mdt header wrap eh->e_phoff + phnum * sizeof(*ph) and
read ph[i] out of bounds?

In addition, ph[i].p_offset + ph[i].p_filesz can overflow (or produce a
*map_len that wraps PAGE_ALIGN(map_len) to 0 in fpc_tee_load() before
memcpy(buf, image, img_len)), and when e_phoff + phnum * sizeof(*ph) >
fw->size without wrapping, the function continues with *map_len == 0
instead of releasing fw and returning ERR_PTR(-EINVAL).

> +
> +	*mdt_len = fw->size;
> +	total = fw->size;
> +	release_firmware(fw);
> +
> +	for (i = 0; i < phnum; i++) {
> +		snprintf(fw_name, sizeof(fw_name), "%s.b%02u", name, i);
> +		ret = request_firmware(&fw, fw_name, fpc->dev);
> +		if (ret)
> +			return ERR_PTR(ret);
> +		total += fw->size;
> +		release_firmware(fw);
> +	}

[Severity: Low]
This isn't a bug, but fpc_tee_read_image() calls request_firmware() and
release_firmware() on the .mdt and every .bNN segment once just to sum
their sizes, then loads all of them a second time into a vmalloc() buffer
that fpc_tee_load() immediately copies into a dma_alloc_coherent() buffer
and frees.

> +
> +	buf = vmalloc(total);
> +	if (!buf)
> +		return ERR_PTR(-ENOMEM);
> +
> +	off = 0;
> +	for (i = 0; i <= phnum; i++) {
> +		if (i)
> +			snprintf(fw_name, sizeof(fw_name), "%s.b%02u", name, i - 1);
> +		else
> +			snprintf(fw_name, sizeof(fw_name), "%s.mdt", name);
> +		ret = request_firmware(&fw, fw_name, fpc->dev);
> +		if (ret || off + fw->size > total) {
> +			if (!ret)
> +				release_firmware(fw);
> +			vfree(buf);
> +			return ERR_PTR(ret ?: -EINVAL);
> +		}
> +		memcpy(buf + off, fw->data, fw->size);
> +		off += fw->size;
> +		release_firmware(fw);
> +	}
> +
> +	*img_len = total;
> +	*map_len = max(*map_len, total);
> +	return buf;
> +}

[Severity: Medium]
What happens if any firmware file shrinks between the two
request_firmware() passes?

The second loop checks off + fw->size > total, but does not verify that
the reloaded .mdt size still equals *mdt_len or that off == total after
the loop.  If a file is smaller on the second read, buf[off .. total - 1]
is left with uninitialized vmalloc() memory and copied to TrustZone with
*img_len = total.

[ ... ]
> +static long fpc_tee_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
> +{
[ ... ]
> +	case FPC_TEE_IOC_IRQ_CLEAR:
> +		atomic_set(&fpc->irq_events, fpc->irq_gpio &&
> +			   gpiod_get_value_cansleep(fpc->irq_gpio) ? 1 : 0);
> +		ret = 0;
> +		break;

[Severity: Medium]
Does gpiod_get_value_cansleep() returning a negative errno get treated as
an asserted interrupt line both here and in fpc_tee_irq()?

Also, when FPC_TEE_IOC_IRQ_CLEAR sets fpc->irq_events to 1, it does not
call wake_up_interruptible(&fpc->irq_wait), and if fpc->irq_gpio reads 0
right before fpc_tee_irq() runs, atomic_set(&fpc->irq_events, 0) can
overwrite the new increment and drop the interrupt:

CPU0                              CPU1
fpc_tee_ioctl()
  gpiod_get_value_cansleep() -> 0
                                  fpc_tee_irq()
                                    atomic_inc(&fpc->irq_events)
  atomic_set(&fpc->irq_events, 0)  <-- loses interrupt event

> +	default:
> +		ret = -ENOTTY;
> +	}
> +	mutex_unlock(&fpc->lock);
> +
> +	return ret;
> +}
[ ... ]
> +static ssize_t fpc_tee_read(struct file *file, char __user *buf, size_t len,
> +			    loff_t *ppos)
> +{
> +	struct fpc_tee *fpc = fpc_tee_from_file(file);
> +	u32 events;
> +	int ret;
> +
> +	if (len < sizeof(events))
> +		return -EINVAL;
> +
> +	if (!(file->f_flags & O_NONBLOCK)) {
> +		ret = wait_event_interruptible(fpc->irq_wait,
> +					       atomic_read(&fpc->irq_events));
> +		if (ret)
> +			return ret;
> +	}
> +
> +	events = atomic_xchg(&fpc->irq_events, 0);
> +	if (copy_to_user(buf, &events, sizeof(events)))
> +		return -EFAULT;
> +
> +	return sizeof(events);
> +}

[Severity: High]
Does a non-blocking read() return 4 bytes with events == 0 instead of
-EAGAIN when no interrupts are pending?  A userspace poll/read loop
draining a non-blocking descriptor until -EAGAIN will spin forever.

Even for blocking reads, atomic_xchg(&fpc->irq_events, 0) runs outside the
wait condition, so a concurrent reader or FPC_TEE_IOC_IRQ_CLEAR can consume
the count first and cause a blocking read() to return events == 0.
Zeroing fpc->irq_events before copy_to_user() also discards the pending
events if copy_to_user() faults.

[ ... ]
> +static int fpc_tee_probe(struct platform_device *pdev)
> +{
[ ... ]
> +	ret = dma_set_mask_and_coherent(dev, DMA_BIT_MASK(32));
> +	if (!ret)
> +		ret = qcom_tzmem_shm_bridge_create(rmem->base, rmem->size,
> +						   &fpc->bridge);
> +	if (ret) {
> +		of_reserved_mem_device_release(dev);
> +		return dev_err_probe(dev, ret, "no shm bridge\n");
> +	}

[Severity: High]
What happens if fpc_tee_probe() runs before qcom_scm_probe() finishes?

Without checking qcom_scm_is_available() and returning -EPROBE_DEFER,
qcom_tzmem_shm_bridge_create() runs while qcom_tzmem_using_shm_bridge is
still false and silently returns 0 without creating the bridge:

drivers/firmware/qcom/qcom_tzmem.c:qcom_tzmem_shm_bridge_create() {
	if (!qcom_tzmem_using_shm_bridge)
		return 0;
	...
}

If /dev/fpc_tee is also opened before qcom_scm_probe() completes,
fpc_tee_open() -> ... -> qcom_scm_qseecom_app_start() ->
__qcom_scm_qseecom_call() dereferences __scm->dev while __scm is NULL.

[ ... ]
> +	device_init_wakeup(dev, true);
> +	enable_irq_wake(irq);
> +
> +	fpc->misc.minor = MISC_DYNAMIC_MINOR;
> +	fpc->misc.name = "fpc_tee";
> +	fpc->misc.fops = &fpc_tee_fops;
> +	fpc->misc.parent = dev;
> +
> +	platform_set_drvdata(pdev, fpc);
> +
> +	return misc_register(&fpc->misc);
> +}

[Severity: Medium]
If misc_register() fails here, does fpc_tee_probe() leave IRQ wake enabled
and the wakeup source registered on dev?

> +
> +static void fpc_tee_remove(struct platform_device *pdev)
> +{
> +	struct fpc_tee *fpc = platform_get_drvdata(pdev);
> +
> +	misc_deregister(&fpc->misc);
> +	if (fpc->loaded)
> +		qcom_scm_qseecom_app_shutdown(fpc->app_id);
> +	fpc_tee_power(fpc, false);
> +}

[Severity: Medium]
Should fpc_tee_remove() (or a devm action) also call disable_irq_wake()
and device_init_wakeup(dev, false) to balance fpc_tee_probe()?

[Severity: Medium]
Can fpc_tee_remove() race with an open file descriptor that is still in
fpc_tee_ioctl() or sleeping in fpc_tee_read()?

misc_deregister() does not wait for already-open file descriptors to close,
and fpc_tee_remove() inspects fpc->loaded, shuts down fpc->app_id, and
calls fpc_tee_power() without taking fpc->lock:

CPU0                              CPU1
fpc_tee_ioctl(FPC_TEE_IOC_POWER)
  mutex_lock(&fpc->lock)
  fpc_tee_power(fpc, true)
    regulator_enable(fpc->vdd)
    usleep_range(1000, 1100)
                                  fpc_tee_remove()
                                    misc_deregister(&fpc->misc)
                                    fpc_tee_power(fpc, false)
                                      if (on == fpc->powered)
                                        return 0;  <-- vdd left enabled
    fpc->powered = true

In addition, any reader still sleeping on fpc->irq_wait or holding an open
file descriptor can access fpc after fpc_tee_remove() returns and devres
frees it.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261124-fpc1540-tee-v1-0-6c6985f05b06@proton.me?part=4

      reply	other threads:[~2026-11-24 18:01 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-24 17:42 [PATCH 0/4] Add FPC1540 fingerprint sensor support through QSEE Armandas Kvietkus via B4 Relay
2026-11-24 17:43 ` [PATCH 1/4] firmware: qcom: scm: Add QSEE app start, shutdown and services image load Armandas Kvietkus via B4 Relay
2026-11-24 17:43 ` [PATCH 2/4] dt-bindings: vendor-prefixes: Add Fingerprint Cards AB Armandas Kvietkus via B4 Relay
2026-11-24 17:43 ` [PATCH 3/4] dt-bindings: misc: Add FPC1540 fingerprint sensor driven by a QSEE app Armandas Kvietkus via B4 Relay
2026-11-24 17:51   ` sashiko-bot
2026-11-24 17:43 ` [PATCH 4/4] misc: Add FPC1540 fingerprint sensor driver for QSEE apps Armandas Kvietkus via B4 Relay
2026-11-24 18:01   ` sashiko-bot [this message]

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-231762@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=armandas.kvietkus@proton.me \
    --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