[PATCH v4 2/9] mtdchar: validate the full OOB range before splitting requests

From: James Hilliard

Date: Sun Dec 06 2026 - 04:59:26 EST


When MEMWRITE splits an OOB request past the end of the device, an early
chunk can program flash before a later chunk fails with -EINVAL. MEMREAD
can likewise copy part of the request to userspace. Checking each chunk
does not validate the original request.

Check the full data and OOB ranges before allocating buffers or starting
I/O. Share the mode-specific OOB range check with the MTD core. Count a
backing page's spare bytes only if the span reaches the end of that page,
even when the span starts partway through it. Compare page counts instead
of storing the device's total OOB capacity in a size_t.

For a concatenated MTD, use each member's OOB capacity in the requested
mode. Members with the same physical spare size can have different amounts
of free space in AUTO_OOB mode.

Fixes: 6420ac0af95d ("mtdchar: prevent unbounded allocation in MEMWRITE ioctl")
Fixes: 095bb6e44eb1 ("mtdchar: add MEMREAD ioctl")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/mtdchar.c | 15 ++++++++++----
drivers/mtd/mtdconcat.c | 17 ++++++++++++++++
drivers/mtd/mtdcore.c | 37 ++++++++++++++++++++---------------
drivers/mtd/mtdcore.h | 52 +++++++++++++++++++++++++++++++++++++++++++++----
include/linux/mtd/mtd.h | 2 ++
5 files changed, 99 insertions(+), 24 deletions(-)

diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index d385dd67f98d..a702a3a977ab 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -573,6 +573,15 @@ static int mtdchar_blkpg_ioctl(struct mtd_info *mtd,
}
}

+static bool mtdchar_req_valid(struct mtd_info *mtd, u64 start, u64 len,
+ size_t ooblen, unsigned int mode)
+{
+ struct mtd_oob_ops ops = { .mode = mode, .ooblen = ooblen };
+
+ return start <= mtd->size && len <= mtd->size - start &&
+ mtd_oob_range_valid(mtd, start, &ops);
+}
+
static void adjust_oob_length(struct mtd_info *mtd, uint64_t start,
struct mtd_oob_ops *ops)
{
@@ -598,7 +607,6 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
uint8_t *datbuf = NULL, *oobbuf = NULL;
size_t datbuf_len, oobbuf_len;
int ret = 0;
- u64 end;

if (copy_from_user(&req, argp, sizeof(req)))
return -EFAULT;
@@ -618,7 +626,7 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
req.len &= 0xffffffff;
req.ooblen &= 0xffffffff;

- if (check_add_overflow(req.start, req.len, &end) || end > mtd->size)
+ if (!mtdchar_req_valid(mtd, req.start, req.len, req.ooblen, req.mode))
return -EINVAL;

datbuf_len = min_t(size_t, req.len, mtd->erasesize);
@@ -698,7 +706,6 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
size_t datbuf_len, oobbuf_len;
size_t orig_len, orig_ooblen;
int ret = 0;
- u64 end;

if (copy_from_user(&req, argp, sizeof(req)))
return -EFAULT;
@@ -725,7 +732,7 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
req.len &= 0xffffffff;
req.ooblen &= 0xffffffff;

- if (check_add_overflow(req.start, req.len, &end) || end > mtd->size) {
+ if (!mtdchar_req_valid(mtd, req.start, req.len, req.ooblen, req.mode)) {
ret = -EINVAL;
goto out;
}
diff --git a/drivers/mtd/mtdconcat.c b/drivers/mtd/mtdconcat.c
index 354cc71cd722..51690d5a30ff 100644
--- a/drivers/mtd/mtdconcat.c
+++ b/drivers/mtd/mtdconcat.c
@@ -244,6 +244,22 @@ concat_writev(struct mtd_info *mtd, const struct kvec *vecs,
return err;
}

+static struct mtd_info *concat_oob_device(struct mtd_info *mtd, u64 *start)
+{
+ struct mtd_concat *concat = CONCAT(mtd);
+ int i;
+
+ for (i = 0; i < concat->num_subdev; i++) {
+ struct mtd_info *subdev = concat->subdev[i];
+
+ if (*start < subdev->size)
+ return subdev;
+ *start -= subdev->size;
+ }
+
+ return NULL;
+}
+
static int
concat_read_oob(struct mtd_info *mtd, loff_t from, struct mtd_oob_ops *ops)
{
@@ -667,6 +683,7 @@ struct mtd_info *mtd_concat_create(struct mtd_info *subdev[], /* subdevices to c
concat->mtd.oobavail = subdev[0]->oobavail;

subdev_master = mtd_get_master(subdev[0]);
+ concat->mtd.master.oob_device = concat_oob_device;
if (subdev_master->_writev)
concat->mtd._writev = concat_writev;
if (subdev_master->_read_oob)
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index 0d041892e173..af509dbf1f6c 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -1650,6 +1650,25 @@ int mtd_panic_write(struct mtd_info *mtd, loff_t to, size_t len, size_t *retlen,
}
EXPORT_SYMBOL_GPL(mtd_panic_write);

+bool mtd_oob_range_valid(struct mtd_info *mtd, u64 start, struct mtd_oob_ops *ops)
+{
+ struct mtd_oob_ops left = *ops;
+
+ while (left.ooblen) {
+ struct mtd_oob_span span = mtd_oob_span(mtd, start);
+ size_t n = mtd_oob_span_len(&span, &left);
+
+ if (n == left.ooblen)
+ break;
+ if (!n || mtd_oob_page_offset(span.mtd, span.start + span.len))
+ return false;
+ start += span.len;
+ left.ooblen -= n;
+ left.ooboffs = 0;
+ }
+ return true;
+}
+
static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
struct mtd_oob_ops *ops)
{
@@ -1667,22 +1686,8 @@ static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
if (offs < 0 || offs + ops->len > mtd->size)
return -EINVAL;

- if (ops->ooblen) {
- size_t maxooblen;
-
- if (mtd_div_by_ws(offs, mtd) >=
- mtd_div_by_ws(mtd->size, mtd))
- return -EINVAL;
-
- if (ops->ooboffs >= mtd_oobavail(mtd, ops))
- return -EINVAL;
-
- maxooblen = ((size_t)(mtd_div_by_ws(mtd->size, mtd) -
- mtd_div_by_ws(offs, mtd)) *
- mtd_oobavail(mtd, ops)) - ops->ooboffs;
- if (ops->ooblen > maxooblen)
- return -EINVAL;
- }
+ if (!mtd_oob_range_valid(mtd, offs, ops))
+ return -EINVAL;

return 0;
}
diff --git a/drivers/mtd/mtdcore.h b/drivers/mtd/mtdcore.h
index 3e65f85ec39a..c25b2e25cfdf 100644
--- a/drivers/mtd/mtdcore.h
+++ b/drivers/mtd/mtdcore.h
@@ -1,8 +1,5 @@
/* SPDX-License-Identifier: GPL-2.0 */
-/*
- * These are exported solely for the purpose of mtd_blkdevs.c and mtdchar.c.
- * You should not use them for _anything_ else.
- */
+/* Internal interfaces shared by the MTD core, its frontends and concat. */

extern struct mutex mtd_table_mutex;
extern struct backing_dev_info *mtd_bdi;
@@ -13,6 +10,7 @@ int del_mtd_device(struct mtd_info *mtd);
int add_mtd_partitions(struct mtd_info *, const struct mtd_partition *, int);
int del_mtd_partitions(struct mtd_info *);
void release_mtd_partition(struct mtd_info *mtd);
+bool mtd_oob_range_valid(struct mtd_info *mtd, u64 start, struct mtd_oob_ops *ops);

static inline u32 mtd_oob_page_offset(struct mtd_info *mtd, u64 addr)
{
@@ -23,6 +21,52 @@ static inline u32 mtd_oob_page_offset(struct mtd_info *mtd, u64 addr)
return mtd_mod_by_ws(addr, mtd);
}

+/* A contiguous part of a device with one backing OOB geometry and policy. */
+struct mtd_oob_span {
+ struct mtd_info *mtd;
+ u64 start;
+ u64 len;
+};
+
+static inline struct mtd_oob_span mtd_oob_span(struct mtd_info *mtd, u64 start)
+{
+ struct mtd_info *master = mtd_get_master(mtd);
+ u64 len = start < mtd->size ? mtd->size - start : 0;
+
+ while (len && master->master.oob_device) {
+ start = mtd_get_master_ofs(mtd, start);
+ mtd = master->master.oob_device(master, &start);
+ if (!mtd)
+ return (struct mtd_oob_span){};
+ len = min_t(u64, len, mtd->size - start);
+ master = mtd_get_master(mtd);
+ }
+
+ return (struct mtd_oob_span){ .mtd = mtd, .start = start, .len = len };
+}
+
+/* Number of requested spare bytes addressable before the end of the span. */
+static inline size_t mtd_oob_span_len(const struct mtd_oob_span *span,
+ struct mtd_oob_ops *ops)
+{
+ u32 avail, first, first_data;
+ u64 pages;
+
+ if (!span->len || !ops->ooblen)
+ return 0;
+ avail = mtd_oobavail(span->mtd, ops);
+ first_data = span->mtd->writesize - mtd_oob_page_offset(span->mtd, span->start);
+ if (ops->ooboffs >= avail || span->len < first_data)
+ return 0;
+
+ first = avail - ops->ooboffs;
+ pages = div_u64(span->len - first_data, span->mtd->writesize);
+ if (ops->ooblen <= first || div_u64(ops->ooblen - first - 1, avail) < pages)
+ return ops->ooblen;
+
+ return first + pages * avail;
+}
+
struct mtd_partitions;

int parse_mtd_partitions(struct mtd_info *master, const char * const *types,
diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
index 8d10d9d2e830..6dda43f1b56a 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -224,6 +224,7 @@ struct mtd_part {
* not only the master partition list, but also all
* sub-partitions.
* @suspended: set to 1 when the device is suspended, 0 otherwise
+ * @oob_device: find the member of a composite MTD and translate its data offset
*
* This struct is embedded in mtd_info and contains master-specific
* properties/fields. The master is the root MTD device from the MTD partition
@@ -233,6 +234,7 @@ struct mtd_master {
struct mutex partitions_lock;
struct mutex chrdev_lock;
unsigned int suspended : 1;
+ struct mtd_info *(*oob_device)(struct mtd_info *mtd, u64 *start);
};

struct mtd_info {

--
2.53.0