Linux-mtd Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: sashiko-bot@kernel.org
To: "Richard Weinberger (sigma star gmbh)" <rw@kernel.org>,
	linux-mtd@lists.infradead.org
Cc: horms@kernel.org, pabeni@redhat.com, kuba@kernel.org,
	edumazet@google.com, davem@davemloft.net, miklos@szeredi.hu,
	vigneshr@ti.com, miquel.raynal@bootlin.com,
	david.oberhollenzer@sigma-star.at, upstream+linux@sigma-star.at,
	"Richard Weinberger" <richard@nod.at>
Subject: Re: [PATCH 09/10] mtd: Implement MUSE - MTD in userspace
Date: Mon, 30 Nov 2026 09:14:57 +0000	[thread overview]
Message-ID: <sashiko-outbox-240772@kernel.org> (raw)
In-Reply-To: <20261130082634.2750846-10-rw@kernel.org>

Thank you for your contribution! Sashiko AI review found 7 potential
issues to consider:
- [Medium] mtd: muse: 32-bit overflow in muse_process_init_reply() and
  muse_io_init()
- [Medium] mtd: muse: missing writesize check for subpage_shift in
  muse_verify_mtdreq()
- [Medium] mtd: muse: missing ops->stats update and dropped bitflips in
  muse_do_io()
- [Medium] mtd: muse: missing mtd->ecc_stats.badblocks increment in
  muse_mtd_markbad()
- [Medium] mtd: muse: module unload race between muse_mtd_put_device()
  and muse_exit()
- [Medium] mtd: muse: integer overflows from unbounded I/O limits and
  MTD size
- [Low] mtd: muse: data race on mtd->ecc_stats in muse_do_io()
--

commit fab9511930c9c6fbb7cc8a643d0853ebd17716d5
Author: Richard Weinberger <richard@nod.at>

mtd: Implement MUSE - MTD in userspace

This commit adds the MUSE driver (/dev/muse), which allows implementing an
MTD device in userspace over the FUSE transport.

> diff --git a/drivers/mtd/muse.c b/drivers/mtd/muse.c
> new file mode 100644
> index 0000000000000..aa14f987da6f3
> --- /dev/null
> +++ b/drivers/mtd/muse.c
> @@ -0,0 +1,995 @@

[ ... ]

> +static int muse_mtd_markbad(struct mtd_info *mtd, loff_t addr)
> +{
> +	struct muse_conn *mc = get_mc_from_mtd(mtd);
> +	struct muse_markbad_in inarg;
> +	FUSE_ARGS(args);
> +	ssize_t ret;
> +
> +	inarg.addr = addr;
> +
> +	args.opcode = MUSE_MARKBAD;
> +	args.nodeid = FUSE_ROOT_ID;
> +	args.in_numargs = 1;
> +	args.in_args[0].size = sizeof(inarg);
> +	args.in_args[0].value = &inarg;
> +
> +	ret = fuse_conn_send(mc->fc, &args);
> +	if (ret < 0)
> +		return ret;
> +
> +	return 0;
> +}

[Severity: Medium]
Should muse_mtd_markbad() increment mtd->ecc_stats.badblocks on success?
mtd_block_markbad() only increments ecc_stats.badblocks on partitions with
a non-NULL parent, relying on master->_block_markbad() to update the master
device's badblock count, so sysfs bad_blocks and ECCGETSTATS on the master
MTD remain stale when blocks are marked bad.

[ ... ]

> +static int muse_io_init(struct muse_conn *mc, struct mtd_oob_ops *ops,
> +			struct muse_io *io)
> +{
> +	size_t max_io = io->write ? mc->max_write : mc->max_read;
> +	struct mtd_info *mtd = &mc->mtd;
> +
> +	if (ops->mode == MTD_OPS_RAW)
> +		io->io_mode = MUSE_IO_RAW;
> +
> +	/* Without out-of-band data the kind of it does not matter. */
> +	if (io->ooblen) {
> +		io->oob_per_page = mtd_oobavail(mtd, ops);
> +		if (!io->oob_per_page || io->ooboffs >= io->oob_per_page)
> +			return -EINVAL;
> +
> +		if (ops->mode == MTD_OPS_PLACE_OOB || ops->mode == MTD_OPS_RAW)
> +			io->oob_mode = MUSE_IO_OOB_PLACE;
> +		else if (ops->mode == MTD_OPS_AUTO_OOB)
> +			io->oob_mode = MUSE_IO_OOB_AUTO;
> +		else
> +			return -EOPNOTSUPP;
> +	}
> +
> +	/*
> +	 * A full page, including its out-of-band data if requested, needs to
> +	 * fit into a single FUSE request.
> +	 */
> +	io->max_pages = max_io / (mtd->writesize + io->oob_per_page);
> +	if (!io->max_pages)
> +		return -ENOBUFS;
> +
> +	return 0;
> +}

[Severity: Medium]
Can mtd->writesize + io->oob_per_page wrap around in 32-bit unsigned
arithmetic and divide by zero?

Both mtd->writesize and io->oob_per_page are 32-bit unsigned integers, and
muse_verify_mtdreq() only checks req->oobsize < req->writesize.  If
userspace replies to MUSE_INIT with sizes whose sum wraps modulo 2^32
(such as writesize = 0x80000001 and oobsize = oobavail = 0x7fffffff),
mtd->writebufsize + mtd->oobsize in muse_process_init_reply() also wraps
in 32-bit arithmetic and bypasses the max_read/max_write check, causing an
OOB read or write to divide by zero here.

[ ... ]

> +static int muse_do_io(struct muse_conn *mc, struct mtd_oob_ops *ops,
> +		      loff_t pos, bool write)
> +{
> +	struct mtd_info *mtd = &mc->mtd;
> +	struct muse_io io = {
> +		.write = write,
> +		.pos = pos,
> +		.datbuf = ops->datbuf,
> +		.len = ops->len,
> +		.oobbuf = ops->oobbuf,
> +		.ooblen = ops->ooblen,
> +		.ooboffs = ops->ooboffs,
> +	};
> +	int ret;
> +
> +	ops->retlen = 0;
> +	ops->oobretlen = 0;
> +
> +	ret = muse_io_init(mc, ops, &io);
> +	while (!ret && (io.len || io.ooblen))
> +		ret = muse_io_one(mc, &io, ops);
> +
> +	/* A fatal error overrides -EUCLEAN and -EBADMSG. */
> +	if (ret)
> +		return ret;
> +	if (io.eccerrors) {
> +		mtd->ecc_stats.failed += io.eccerrors;
> +		return -EBADMSG;
> +	}
> +	if (io.bitflips) {
> +		mtd->ecc_stats.corrected += io.bitflips;
> +		return -EUCLEAN;
> +	}
> +
> +	return 0;
> +}

[Severity: Medium]
Does muse_do_io() fail to update ops->stats and drop io.bitflips when
io.eccerrors is non-zero?

If a multi-request read sees both -EUCLEAN and -EBADMSG across its
sub-requests, returning early on io.eccerrors skips adding io.bitflips to
mtd->ecc_stats.corrected.  In addition, ops->stats is never populated when
non-NULL, so mtdchar_read_ioctl() (MEMREAD) always reports zero ECC stats
to userspace and loses -EBADMSG or -EUCLEAN on multi-chunk reads whenever
the final chunk succeeds.

[Severity: Low]
Can concurrent reads cause a data race on mtd->ecc_stats.failed and
mtd->ecc_stats.corrected?  Unlike hardware NAND drivers that serialize
page reads under a device lock, muse_do_io() runs FUSE read requests
concurrently and updates the shared master mtd->ecc_stats without
synchronization, which can lose counter updates and skew partition ECC
deltas in mtd_update_ecc_stats().

[ ... ]

> +static void muse_mtd_put_device(struct mtd_info *mtd)
> +{
> +	struct muse_conn *mc = get_mc_from_mtd(mtd);
> +
> +	mutex_lock(&mc->ref_mutex);
> +
> +	mc->mtd_users--;
> +
> +	/*
> +	 * If this was the last reference on the MTD and userspace is gone,
> +	 * remove it now. The reference on the FUSE connection goes to the exit
> +	 * work then.
> +	 */
> +	if (mc->want_exit && mc->mtd_users == 0)
> +		muse_remove_mtd_async(mc);
> +	else
> +		fuse_conn_put(mc->fc);
> +
> +	mutex_unlock(&mc->ref_mutex);
> +}

[Severity: Medium]
Can module unload race with muse_mtd_put_device() when the last MTD user
exits after /dev/muse has already been closed?

When /dev/muse is closed while mc->mtd_users > 0, muse_ctrl_release() sets
mc->want_exit = true without queuing mtd_exit_work, and __fput() drops the
file's reference on THIS_MODULE.  When the last MTD user later closes the
MTD device, __put_mtd_device() drops the final module reference before
calling master->_put_device():

drivers/mtd/mtdcore.c:__put_mtd_device() {
    ...
	module_put(master->owner);

	/* must be the last as master can be freed in the _put_device */
	if (master->_put_device)
		master->_put_device(master);
}

That allows a concurrent module unload to destroy muse_wq and unmap the
module before muse_mtd_put_device() queues mtd_exit_work:

CPU0                              CPU1
__put_mtd_device()
  module_put(master->owner)
                                  delete_module()
                                    muse_exit()
                                      destroy_workqueue(muse_wq)
                                      rcu_barrier()
  muse_mtd_put_device()
    muse_remove_mtd_async(mc)
      queue_work(muse_wq, ...)  <-- use-after-free

> +static int muse_verify_mtdreq(struct muse_mtd_create_req *req)
> +{
> +	u64 tmp;
> +
> +	if (!req->name)
> +		return -EINVAL;
> +
> +	if (!req->size || !req->writesize || !req->erasesize)
> +		return -EINVAL;
> +
> +	tmp = req->size;
> +	if (do_div(tmp, req->erasesize))
> +		return -EINVAL;
> +
> +	if (req->erasesize % req->writesize)
> +		return -EINVAL;
> +
> +	if (req->writebufsize % req->writesize)
> +		return -EINVAL;
> +
> +	if (req->oobsize < req->oobavail || req->oobsize >= req->writesize)
> +		return -EINVAL;
> +
> +	if (req->flags & ~(MTD_WRITEABLE | MTD_BIT_WRITEABLE | MTD_NO_ERASE))
> +		return -EINVAL;
> +
> +	if (req->subpage_shift > 2)
> +		return -EINVAL;

[Severity: Medium]
Can req->writesize be smaller than (or not divisible by)
1U << req->subpage_shift?

If userspace sets WRITESIZE=1 or 2 with SUBPAGESHIFT=2,
mtd->writesize >> mtd->subpage_sft evaluates to 0.  When UBI attaches to
the device, io_init() in drivers/mtd/ubi/build.c sets
ubi->hdrs_min_io_size = 0 and divides by zero in
ubi->min_io_size % ubi->hdrs_min_io_size.

[ ... ]

> +static void muse_process_init_reply(struct fuse_args *args, int error)
> +{
> +	struct muse_init_args *mia = container_of(args, struct muse_init_args, args);
> +	struct muse_conn *mc = mia->mc;
> +	struct muse_init_out *arg = &mia->out;
> +	struct mtd_info *mtd = &mc->mtd;
> +
> +	if (error || arg->fuse_major != FUSE_KERNEL_VERSION ||
> +	    arg->fuse_minor < MUSE_MIN_MINOR)
> +		goto abort;
> +
> +	mc->max_read = max_t(unsigned int, arg->max_read, 4096);
> +	mc->max_write = max_t(unsigned int, arg->max_write, 4096);
> +
> +	if (muse_parse_mtdreq(mia->info, args->out_args[1].size, mc))
> +		goto abort;
> +
> +	/*
> +	 * We want one READ/WRITE op per MTD io. So the MTD pagesize plus
> +	 * the out-of-band area needs to fit into max_write/max_read.
> +	 */
> +	if (mc->max_write < mtd->writebufsize + mtd->oobsize ||
> +	    mc->max_read < mtd->writebufsize + mtd->oobsize)
> +		goto abort;

[Severity: Medium]
Should mc->max_read, mc->max_write, and req->size have upper bounds?

mc->max_read and mc->max_write are only clamped from below; with values
near UINT_MAX, DIV_ROUND_UP(page_offs + nbytes, mtd->writesize) in
muse_io_one() can overflow 32-bit size_t to 0 on 32-bit architectures, and
combined in-band plus OOB payloads can overflow the 32-bit request length
calculations in fuse_len_args() and fuse_copy_out_args().  Also, req->size
is parsed as an unsigned u64 without checking req->size <= LLONG_MAX, even
though MTD offsets and muse_io.pos use signed loff_t.

-- 
Sashiko AI review · https://sashiko.dev/#/patchset/20261130082634.2750846-1-rw@kernel.org?part=9

______________________________________________________
Linux MTD discussion mailing list
http://lists.infradead.org/mailman/listinfo/linux-mtd/

  reply	other threads:[~2026-11-30  9:15 UTC|newest]

Thread overview: 13+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-30  8:26 [PATCH 0/10] MUSE: Userspace backed MTD v4 Richard Weinberger (sigma star gmbh)
2026-11-30  8:26 ` [PATCH 01/10] fuse: Share cuse_parse_one() as fuse_kv_parse_one() Richard Weinberger (sigma star gmbh)
2026-11-30  8:26 ` [PATCH 02/10] fuse: Add a public header for users outside of fs/fuse/ Richard Weinberger (sigma star gmbh)
2026-11-30  9:14   ` sashiko-bot
2026-11-30  8:26 ` [PATCH 03/10] fuse: Add an interface for userspace backed devices Richard Weinberger (sigma star gmbh)
2026-11-30  8:26 ` [PATCH 04/10] fuse: Convert CUSE to the " Richard Weinberger (sigma star gmbh)
2026-11-30  8:26 ` [PATCH 05/10] fuse: Allow three output arguments per request Richard Weinberger (sigma star gmbh)
2026-11-30  8:26 ` [PATCH 06/10] fuse: Add MUSE specific defines to FUSE interface Richard Weinberger (sigma star gmbh)
2026-11-30  8:26 ` [PATCH 07/10] mtd: Add MTD_MUSE flag Richard Weinberger (sigma star gmbh)
2026-11-30  8:26 ` [PATCH 08/10] mtd: Allow passing a custom mtdparts string to the cmdline parser Richard Weinberger (sigma star gmbh)
2026-11-30  8:26 ` [PATCH 09/10] mtd: Implement MUSE - MTD in userspace Richard Weinberger (sigma star gmbh)
2026-11-30  9:14   ` sashiko-bot [this message]
2026-11-30  8:26 ` [PATCH 10/10] MAINTAINERS: Add entry for MUSE Richard Weinberger (sigma star gmbh)

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-240772@kernel.org \
    --to=sashiko-bot@kernel.org \
    --cc=davem@davemloft.net \
    --cc=david.oberhollenzer@sigma-star.at \
    --cc=edumazet@google.com \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-mtd@lists.infradead.org \
    --cc=miklos@szeredi.hu \
    --cc=miquel.raynal@bootlin.com \
    --cc=pabeni@redhat.com \
    --cc=richard@nod.at \
    --cc=rw@kernel.org \
    --cc=sashiko-reviews@lists.linux.dev \
    --cc=upstream+linux@sigma-star.at \
    --cc=vigneshr@ti.com \
    /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