Linux-i3c Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Sam Agazaryan" <samagazaryan@google.com>
Cc: linux-i3c@lists.infradead.org, Frank.Li@kernel.org,
	"Alexandre Belloni" <alexandre.belloni@bootlin.com>
Subject: Re: [PATCH v5 4/5] i3c: add i3cdev module to expose i3c dev in /dev
Date: Mon, 21 Sep 2026 23:21:41 +0000	[thread overview]
Message-ID: <20260921232142.56C531F000FF@smtp.kernel.org> (raw)
In-Reply-To: <20260921230603.2518652-5-samagazaryan@google.com>

Thank you for your contribution! Sashiko AI review found 4 potential issue(s) to consider:
- [High] i3c: out-of-bounds shift due to unvalidated transfer mode
- [High] i3c: global DoS via userfaultfd stalling per-device xfer_lock
- [Medium] i3c: out-of-bounds read due to unverified actual_len in copy_to_user
- [Low] cleanup: mixing goto-based error handling with scope-based cleanup
--

commit b5d90eba8091c7ed2bb03166ab238062306fafa4
Author: Vitor Soares <vitor.soares@toradex.com>

i3c: add i3cdev module to expose i3c dev in /dev
    
Add userspace character device support for I3C transfers via /dev.
This allows userspace programs to interact directly with I3C targets
that do not have a kernel driver bound to them.

> diff --git a/drivers/i3c/i3cdev.c b/drivers/i3c/i3cdev.c
> new file mode 100644
> index 0000000000000..309ce81812096
> --- /dev/null
> +++ b/drivers/i3c/i3cdev.c
> @@ -0,0 +1,491 @@
> [ ... ]
> +static ssize_t
> +i3cdev_read(struct file *file, char __user *buf, size_t count, loff_t *f_pos)
> +{
> [ ... ]
> +	ret = i3c_device_do_xfers(i3c, &xfers, 1, I3C_SDR);
> +	if (ret)
> +		return ret;
> +
> +	if (copy_to_user(buf, tmp, xfers.actual_len))

[Severity: Medium]
Could this result in an out-of-bounds read to user space? If a buggy hardware
driver returns an actual_len that is greater than the requested transfer len
(or the allocated count), actual_len is used directly in copy_to_user()
without being clamped to the dynamically allocated buffer size, which might
leak kernel memory.

> +		return -EFAULT;
> +
> +	return xfers.actual_len;
> +}
> [ ... ]
> +static int
> +i3cdev_do_xfer(struct i3c_device *dev, struct i3c_ioc_xfer *xfers,
> +	       struct i3c_ioc_xfer __user *u_xfers, unsigned int nxfers)
> +{
> +	struct i3c_xfer *k_xfers __free(kfree) = NULL;

[Severity: Low]
Is it appropriate to mix scope-based and goto-based cleanup here? The
subsystem guidelines indicate that using goto-based error handling alongside
__free() in the same function creates confusing ownership semantics. In this
function, we have __free(kfree) here and goto err_free_mem below.

> +	enum i3c_xfer_mode mode = xfers[0].mode;

[Severity: High]
Can this user-provided mode cause an out-of-bounds shift?

If a mode value greater than or equal to 64 is passed via the ioctl, it is
extracted without validation and passed down:

i3cdev_do_xfer()
    i3c_device_do_xfers(dev, k_xfers, nxfers, mode)
        i3c_dev_do_xfers_locked()
            BIT(mode)

Since BIT(mode) does a bitwise shift, this would trigger undefined behavior.
Should this value be validated against valid I3C modes before proceeding?

> +	u8 **data_ptrs;
> +	int i, j, nalloc, ret = 0;
> [ ... ]
> +	ret = i3c_device_do_xfers(dev, k_xfers, nxfers, mode);
> +	if (ret)
> +		goto err_free_mem;
> +
> +	for (i = 0; i < nxfers; i++) {
> +		bool is_read = (mode == I3C_SDR) ?
> +			       (xfers[i].rnw == I3C_DEV_DIR_READ) :
> +			       (xfers[i].cmd & 0x80);
> +
> +		if (is_read) {
> +			if (copy_to_user(u64_to_user_ptr(xfers[i].data),
> +					 data_ptrs[i], k_xfers[i].actual_len) ||

[Severity: Medium]
Similar to i3cdev_read(), is actual_len verified here? An inflated
actual_len from the hardware driver could result in an out-of-bounds read
to user space without bounds clamping.

> +			    put_user(k_xfers[i].actual_len,
> +				     &u_xfers[i].actual_len)) {
> +				ret = -EFAULT;
> +				break;
> +			}
> +		}
> +	}
> +
> +err_free_mem:
> +	for (j = 0; j < nalloc; j++)
> +		kfree(data_ptrs[j]);
> +	return ret;
> +}
> [ ... ]
> +static long
> +i3cdev_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
> +{
> +	struct i3cdev_data *i3cdev = file->private_data;
> +	struct i3c_device *i3c;
> +
> +	if (_IOC_TYPE(cmd) != I3C_DEV_IOC_MAGIC)
> +		return -ENOTTY;
> +
> +	/* Use the xfer_lock to prevent device detach during ioctl call */
> +	guard(mutex)(&i3cdev->xfer_lock);

[Severity: High]
Could holding this per-device xfer_lock throughout the ioctl create a global
DoS vulnerability?

The lock is held while calling sleepable user-memory access functions like
memdup_user() and copy_to_user() in i3cdev_do_xfer(). A userfaultfd stall
could freeze the thread here indefinitely.

Because i3cdev_detach() uses scoped_guard(mutex, &i3cdev->xfer_lock) while
also being called under the global i3cdev_attach_lock, any detach event
during this stall will block on xfer_lock, deadlocking the entire I3C
subsystem's hotplug and notification framework globally.

> +	i3c = i3cdev->i3c;
> +	if (!i3c || i3c->dev.driver)
> +		return -ENODEV;
> [ ... ]
> +static int i3cdev_attach(struct device *dev, void *dummy)
> +{
> +	struct i3cdev_data *i3cdev;
> +	struct i3c_device *i3c;
> +	int res;
> +
> +	if (dev->type == &i3c_masterdev_type)
> +		return 0;
> +
> +	i3c = dev_to_i3cdev(dev);
> +
> +	guard(mutex)(&i3cdev_attach_lock);

[Severity: Low]
Does this mix scope-based and goto-based cleanup? Like in i3cdev_do_xfer(),
this function uses guard() here but later uses goto error_put_dev for error
handling.

> +	if (dev->driver || i3cdev_get_by_i3c(i3c))
> +		return 0;
> [ ... ]

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20260921230603.2518652-1-samagazaryan@google.com?part=4

-- 
linux-i3c mailing list
linux-i3c@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/linux-i3c

  reply	other threads:[~2026-09-21 23:21 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-09-21 23:05 [PATCH v5 0/5] i3c: add i3cdev module to expose i3c dev in /dev Sam Agazaryan
2026-09-21 23:05 ` [PATCH v5 1/5] i3c: master: export i3c_masterdev_type Sam Agazaryan
2026-09-21 23:06 ` [PATCH v5 2/5] i3c: master: add i3c_for_each_dev helper Sam Agazaryan
2026-09-21 23:06 ` [PATCH v5 3/5] i3c: use actual_len for read transfers Sam Agazaryan
2026-09-21 23:16   ` sashiko-bot
2026-09-24 11:53   ` Adrian Hunter
2026-09-25 15:25   ` Frank Li
2026-09-28 10:42     ` Adrian Hunter
2026-09-21 23:06 ` [PATCH v5 4/5] i3c: add i3cdev module to expose i3c dev in /dev Sam Agazaryan
2026-09-21 23:21   ` sashiko-bot [this message]
2026-09-25 15:38   ` Frank Li
2026-09-21 23:06 ` [PATCH v5 5/5] tools: i3c: add i3ctransfer utility Sam Agazaryan
2026-09-21 23:15   ` sashiko-bot
2026-09-25  9:08 ` [PATCH v5 0/5] i3c: add i3cdev module to expose i3c dev in /dev Andy Shevchenko
2026-09-25 10:57   ` Wolfram Sang

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=20260921232142.56C531F000FF@smtp.kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=Frank.Li@kernel.org \
    --cc=alexandre.belloni@bootlin.com \
    --cc=linux-i3c@lists.infradead.org \
    --cc=samagazaryan@google.com \
    --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