[PATCH v4 4/9] mtdchar: check each member before splitting MEMWRITE
From: James Hilliard
Date: Sun Dec 06 2026 - 04:59:28 EST
A MEMWRITE can program a writable member before reaching a read-only
one and returning -EROFS. On DiskOnChip G3, a partial last spare area
can likewise fail after an earlier page has been programmed. An OOB-only
call on that device stages spare bytes for a separate data write; it
cannot complete the OOB tail of a combined request.
Walk the entire request before the first write. Use each member's
writability and let the backing driver check its part of the request
without performing I/O. Reuse the DiskOnChip G3 write check and allow
staging only for a standalone OOB-only request on that device.
Fixes: 6420ac0af95d ("mtdchar: prevent unbounded allocation in MEMWRITE ioctl")
Cc: stable@xxxxxxxxxxxxxxx
Signed-off-by: James Hilliard <james.hilliard1@xxxxxxxxx>
---
drivers/mtd/devices/docg3.c | 11 ++++++++++
drivers/mtd/mtdchar.c | 22 +++++++++++---------
drivers/mtd/mtdcore.c | 49 +++++++++++++++++++++++++++++++--------------
drivers/mtd/mtdcore.h | 11 ++++++++--
include/linux/mtd/mtd.h | 5 +++++
5 files changed, 71 insertions(+), 27 deletions(-)
diff --git a/drivers/mtd/devices/docg3.c b/drivers/mtd/devices/docg3.c
index 4b2d635616ec..6bef2f7bbcea 100644
--- a/drivers/mtd/devices/docg3.c
+++ b/drivers/mtd/devices/docg3.c
@@ -1352,6 +1352,16 @@ static bool doc_oob_write_valid(struct mtd_info *mtd, u64 to, struct mtd_oob_ops
return ops->ooblen / avail <= ops->len / DOC_LAYOUT_PAGE_SIZE;
}
+static int doc_check_oob_write(struct mtd_info *mtd, u64 to,
+ struct mtd_oob_ops *ops, bool standalone_oob)
+{
+ /* Staging spare bytes is only valid as a separate one-page request. */
+ if ((!ops->len && !standalone_oob) || !doc_oob_write_valid(mtd, to, ops))
+ return -EINVAL;
+
+ return 0;
+}
+
/**
* doc_backup_oob - Backup OOB into docg3 structure
* @mtd: flash device
@@ -1775,6 +1785,7 @@ static int __init doc_set_driver_info(int chip_id, struct mtd_info *mtd)
mtd->_erase = doc_erase;
mtd->_read_oob = doc_read_oob;
mtd->_write_oob = doc_write_oob;
+ mtd->master.check_oob_write = doc_check_oob_write;
mtd->_block_isbad = doc_block_isbad;
mtd_set_ooblayout(mtd, &nand_ooblayout_docg3_ops);
mtd->oobavail = 8;
diff --git a/drivers/mtd/mtdchar.c b/drivers/mtd/mtdchar.c
index a702a3a977ab..2ea54fa33cd2 100644
--- a/drivers/mtd/mtdchar.c
+++ b/drivers/mtd/mtdchar.c
@@ -573,13 +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)
+static int mtdchar_check_req(struct mtd_info *mtd, u64 start, u64 len,
+ size_t ooblen, unsigned int mode, bool write)
{
- struct mtd_oob_ops ops = { .mode = mode, .ooblen = ooblen };
+ struct mtd_oob_ops ops = { .mode = mode, .len = len, .ooblen = ooblen };
- return start <= mtd->size && len <= mtd->size - start &&
- mtd_oob_range_valid(mtd, start, &ops);
+ if (start > mtd->size || len > mtd->size - start)
+ return -EINVAL;
+
+ return mtd_check_oob_range(mtd, start, &ops, write);
}
static void adjust_oob_length(struct mtd_info *mtd, uint64_t start,
@@ -626,8 +628,9 @@ mtdchar_write_ioctl(struct mtd_info *mtd, struct mtd_write_req __user *argp)
req.len &= 0xffffffff;
req.ooblen &= 0xffffffff;
- if (!mtdchar_req_valid(mtd, req.start, req.len, req.ooblen, req.mode))
- return -EINVAL;
+ ret = mtdchar_check_req(mtd, req.start, req.len, req.ooblen, req.mode, true);
+ if (ret)
+ return ret;
datbuf_len = min_t(size_t, req.len, mtd->erasesize);
if (datbuf_len > 0) {
@@ -732,10 +735,9 @@ mtdchar_read_ioctl(struct mtd_info *mtd, struct mtd_read_req __user *argp)
req.len &= 0xffffffff;
req.ooblen &= 0xffffffff;
- if (!mtdchar_req_valid(mtd, req.start, req.len, req.ooblen, req.mode)) {
- ret = -EINVAL;
+ ret = mtdchar_check_req(mtd, req.start, req.len, req.ooblen, req.mode, false);
+ if (ret)
goto out;
- }
datbuf_len = min_t(size_t, req.len, mtd->erasesize);
if (datbuf_len > 0) {
diff --git a/drivers/mtd/mtdcore.c b/drivers/mtd/mtdcore.c
index af509dbf1f6c..a61f84ed19fd 100644
--- a/drivers/mtd/mtdcore.c
+++ b/drivers/mtd/mtdcore.c
@@ -1650,23 +1650,45 @@ 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)
+int mtd_check_oob_range(struct mtd_info *mtd, u64 start, struct mtd_oob_ops *ops,
+ bool check_write)
{
- struct mtd_oob_ops left = *ops;
+ struct mtd_oob_ops part = { .mode = ops->mode, .ooboffs = ops->ooboffs };
+ size_t data = check_write ? ops->len : 0, oob = ops->ooblen;
- while (left.ooblen) {
+ while (data || oob) {
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;
+ if (!span.len)
+ return -EINVAL;
+ if (check_write && !span.writable)
+ return -EROFS;
+ part.len = min_t(u64, data, span.len);
+ part.ooblen = oob;
+ part.ooblen = mtd_oob_span_len(&span, &part);
+ if (part.ooblen < oob &&
+ (!part.ooblen || mtd_oob_page_offset(span.mtd, span.start + span.len)))
+ return -EINVAL;
+ if (check_write) {
+ struct mtd_info *master = mtd_get_master(span.mtd);
+ bool standalone = !ops->len && part.ooblen == ops->ooblen;
+
+ if (master->master.check_oob_write) {
+ u64 to = mtd_get_master_ofs(span.mtd, span.start);
+ int ret;
+
+ ret = master->master.check_oob_write(master, to, &part,
+ standalone);
+ if (ret)
+ return ret;
+ }
+ }
+ data -= part.len;
+ oob -= part.ooblen;
start += span.len;
- left.ooblen -= n;
- left.ooboffs = 0;
+ part.ooboffs = 0;
}
- return true;
+ return 0;
}
static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
@@ -1686,10 +1708,7 @@ static int mtd_check_oob_ops(struct mtd_info *mtd, loff_t offs,
if (offs < 0 || offs + ops->len > mtd->size)
return -EINVAL;
- if (!mtd_oob_range_valid(mtd, offs, ops))
- return -EINVAL;
-
- return 0;
+ return mtd_check_oob_range(mtd, offs, ops, false);
}
static int mtd_read_oob_std(struct mtd_info *mtd, loff_t from,
diff --git a/drivers/mtd/mtdcore.h b/drivers/mtd/mtdcore.h
index c25b2e25cfdf..5d3600a8abac 100644
--- a/drivers/mtd/mtdcore.h
+++ b/drivers/mtd/mtdcore.h
@@ -10,7 +10,8 @@ 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);
+int mtd_check_oob_range(struct mtd_info *mtd, u64 start, struct mtd_oob_ops *ops,
+ bool check_write);
static inline u32 mtd_oob_page_offset(struct mtd_info *mtd, u64 addr)
{
@@ -26,23 +27,29 @@ struct mtd_oob_span {
struct mtd_info *mtd;
u64 start;
u64 len;
+ bool writable;
};
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;
+ u32 flags = mtd->flags;
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){};
+ flags &= mtd->flags;
len = min_t(u64, len, mtd->size - start);
master = mtd_get_master(mtd);
}
- return (struct mtd_oob_span){ .mtd = mtd, .start = start, .len = len };
+ return (struct mtd_oob_span){
+ .mtd = mtd, .start = start, .len = len,
+ .writable = flags & MTD_WRITEABLE,
+ };
}
/* Number of requested spare bytes addressable before the end of the span. */
diff --git a/include/linux/mtd/mtd.h b/include/linux/mtd/mtd.h
index 6dda43f1b56a..2992bbe59b89 100644
--- a/include/linux/mtd/mtd.h
+++ b/include/linux/mtd/mtd.h
@@ -224,6 +224,9 @@ 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
+ * @check_oob_write: check a write's mode and geometry without performing I/O;
+ * return 0 or a negative errno. standalone_oob means the entire request
+ * is OOB-only on this device.
* @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
@@ -234,6 +237,8 @@ struct mtd_master {
struct mutex partitions_lock;
struct mutex chrdev_lock;
unsigned int suspended : 1;
+ int (*check_oob_write)(struct mtd_info *mtd, u64 to,
+ struct mtd_oob_ops *ops, bool standalone_oob);
struct mtd_info *(*oob_device)(struct mtd_info *mtd, u64 *start);
};
--
2.53.0