* [PATCH v2 1/5] ubifs: reject inodes with an out-of-range size
2026-11-03 6:19 [PATCH v2 0/5] ubifs: harden validation of on-disk inode and node metadata shao.mingyin
@ 2026-11-03 6:20 ` shao.mingyin
2026-11-03 6:23 ` [PATCH v2 2/5] ubifs: reject xattr inodes whose size does not match the value shao.mingyin
` (3 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: shao.mingyin @ 2026-11-03 6:20 UTC (permalink / raw)
To: richard; +Cc: linux-mtd, chengzhihao1, linux-kernel, stable
validate_inode() compares inode->i_size with c->max_inode_sz only. The
size is read from the on-flash inode node as a 64-bit value and stored
in the signed loff_t, so a value with the top bit set becomes negative
and passes the check.
The read paths then do size arithmetic with that negative value, for
example (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT in
do_readpage() and (i_size - 1) >> PAGE_SHIFT in populate_page().
Reject such inodes like the other invalid on-flash fields.
Fixes: 1e51764a3c2a ("UBIFS: add new flash file system")
Cc: stable@vger.kernel.org
Signed-off-by: Shao Mingyin <shao.mingyin@zte.com.cn>
---
v2: no change since v1.
diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
index 38972786817e..602f0cd9713f 100644
--- a/fs/ubifs/super.c
+++ b/fs/ubifs/super.c
@@ -71,9 +71,9 @@ static int validate_inode(struct ubifs_info *c, const struct inode *inode)
int err;
const struct ubifs_inode *ui = ubifs_inode(inode);
- if (inode->i_size > c->max_inode_sz) {
- ubifs_err(c, "inode is too large (%lld)",
- (long long)inode->i_size);
+ if (inode->i_size < 0 || inode->i_size > c->max_inode_sz) {
+ ubifs_err(c, "inode size %lld is out of range (max. is %lld)",
+ (long long)inode->i_size, c->max_inode_sz);
return 1;
}
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2 2/5] ubifs: reject xattr inodes whose size does not match the value
2026-11-03 6:19 [PATCH v2 0/5] ubifs: harden validation of on-disk inode and node metadata shao.mingyin
2026-11-03 6:20 ` [PATCH v2 1/5] ubifs: reject inodes with an out-of-range size shao.mingyin
@ 2026-11-03 6:23 ` shao.mingyin
2026-11-03 6:24 ` [PATCH v2 3/5] ubifs: reject master nodes claiming a too small inode number shao.mingyin
` (2 subsequent siblings)
4 siblings, 0 replies; 6+ messages in thread
From: shao.mingyin @ 2026-11-03 6:23 UTC (permalink / raw)
To: richard; +Cc: linux-mtd, chengzhihao1, linux-kernel, stable
v2: merge the new size check into the existing ui->xattr block, as
suggested by Zhihao Cheng.
diff --git a/fs/ubifs/super.c b/fs/ubifs/super.c
index 38972786817e..d89dd76a6847 100644
--- a/fs/ubifs/super.c
+++ b/fs/ubifs/super.c
@@ -88,8 +88,22 @@ static int validate_inode(struct ubifs_info *c, const struct inode *inode)
if (ui->data_len < 0 || ui->data_len > UBIFS_MAX_INO_DATA)
return 4;
- if (ui->xattr && !S_ISREG(inode->i_mode))
- return 5;
+ if (ui->xattr) {
+ if (!S_ISREG(inode->i_mode))
+ return 5;
+
+ /*
+ * Extended attribute inodes keep the value in the inode node
+ * and maintain i_size == data_len everywhere (see
+ * create_xattr() and change_xattr()).
+ */
+ if (inode->i_size != ui->data_len) {
+ ubifs_err(c, "xattr inode %llu has size %lld, but %d bytes of data",
+ inode->i_ino, (long long)inode->i_size,
+ ui->data_len);
+ return 6;
+ }
+ }
if (!ubifs_compr_present(c, ui->compr_type)) {
ubifs_warn(c, "inode %llu uses '%s' compression, but it was not compiled in",
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2 3/5] ubifs: reject master nodes claiming a too small inode number
2026-11-03 6:19 [PATCH v2 0/5] ubifs: harden validation of on-disk inode and node metadata shao.mingyin
2026-11-03 6:20 ` [PATCH v2 1/5] ubifs: reject inodes with an out-of-range size shao.mingyin
2026-11-03 6:23 ` [PATCH v2 2/5] ubifs: reject xattr inodes whose size does not match the value shao.mingyin
@ 2026-11-03 6:24 ` shao.mingyin
2026-11-03 6:26 ` [PATCH v2 4/5] ubifs: validate inode and data node sizes during replay shao.mingyin
2026-11-03 6:27 ` [PATCH v2 5/5] ubifs: xattr: guard against xattr accounting underflow shao.mingyin
4 siblings, 0 replies; 6+ messages in thread
From: shao.mingyin @ 2026-11-03 6:24 UTC (permalink / raw)
To: richard; +Cc: linux-mtd, chengzhihao1, linux-kernel, stable
v2: merge the lower bound into the existing highest_inum check, as
suggested by Zhihao Cheng.
diff --git a/fs/ubifs/master.c b/fs/ubifs/master.c
index a148760fa49e..e180091fab8c 100644
--- a/fs/ubifs/master.c
+++ b/fs/ubifs/master.c
@@ -186,7 +186,13 @@ static int validate_master(const struct ubifs_info *c)
goto out;
}
- if (c->highest_inum >= INUM_WATERMARK) {
+ /*
+ * Inode numbers below UBIFS_FIRST_INO are reserved. If the master node
+ * claims a smaller value, newly created inodes would reuse the root
+ * inode number or another reserved one.
+ */
+ if (c->highest_inum >= INUM_WATERMARK ||
+ c->highest_inum < UBIFS_FIRST_INO) {
err = 3;
goto out;
}
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2 4/5] ubifs: validate inode and data node sizes during replay
2026-11-03 6:19 [PATCH v2 0/5] ubifs: harden validation of on-disk inode and node metadata shao.mingyin
` (2 preceding siblings ...)
2026-11-03 6:24 ` [PATCH v2 3/5] ubifs: reject master nodes claiming a too small inode number shao.mingyin
@ 2026-11-03 6:26 ` shao.mingyin
2026-11-03 6:27 ` [PATCH v2 5/5] ubifs: xattr: guard against xattr accounting underflow shao.mingyin
4 siblings, 0 replies; 6+ messages in thread
From: shao.mingyin @ 2026-11-03 6:26 UTC (permalink / raw)
To: richard; +Cc: linux-mtd, chengzhihao1, linux-kernel, stable
replay_bud() trusts the size fields of inode and data nodes from the
journal. An image that is corrupt but has a matching CRC can therefore
carry an inode node with a negative or too large size, or a data node
with a size outside 1..UBIFS_BLOCK_SIZE. During recovery
ubifs_recover_size_accum() uses these values and ubifs_recover_size()
may extend the inode to the bogus size.
The normal read paths already reject such nodes (read_block(),
populate_page()) and truncation nodes are validated in replay_bud() as
well. Do the same for inode and data nodes. While at it, compute the
data node end offset with 64-bit arithmetic so that a large block
number cannot wrap.
Fixes: 1e51764a3c2a ("UBIFS: add new flash file system")
Cc: stable@vger.kernel.org
Signed-off-by: Shao Mingyin <shao.mingyin@zte.com.cn>
---
v2: no change since v1.
diff --git a/fs/ubifs/replay.c b/fs/ubifs/replay.c
index 263045e05cf1..14cd5598ca0b 100644
--- a/fs/ubifs/replay.c
+++ b/fs/ubifs/replay.c
@@ -741,6 +741,12 @@ static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
struct ubifs_ino_node *ino = snod->node;
loff_t new_size = le64_to_cpu(ino->size);
+ /* Validate inode node */
+ if (new_size < 0 || new_size > c->max_inode_sz) {
+ ubifs_err(c, "bad inode node");
+ goto out_dump;
+ }
+
if (le32_to_cpu(ino->nlink) == 0)
deletion = 1;
err = insert_node(c, lnum, snod->offs, snod->len, hash,
@@ -751,9 +757,21 @@ static int replay_bud(struct ubifs_info *c, struct bud_entry *b)
case UBIFS_DATA_NODE:
{
struct ubifs_data_node *dn = snod->node;
- loff_t new_size = le32_to_cpu(dn->size) +
- key_block(c, &snod->key) *
- UBIFS_BLOCK_SIZE;
+ u32 dsize = le32_to_cpu(dn->size);
+ loff_t new_size;
+
+ /* Validate data node */
+ if (!dsize || dsize > UBIFS_BLOCK_SIZE) {
+ ubifs_err(c, "bad data node");
+ goto out_dump;
+ }
+
+ new_size = (loff_t)key_block(c, &snod->key) *
+ UBIFS_BLOCK_SIZE + dsize;
+ if (new_size > c->max_inode_sz) {
+ ubifs_err(c, "bad data node");
+ goto out_dump;
+ }
err = insert_node(c, lnum, snod->offs, snod->len, hash,
&snod->key, snod->sqnum, deletion,
^ permalink raw reply [flat|nested] 6+ messages in thread* [PATCH v2 5/5] ubifs: xattr: guard against xattr accounting underflow
2026-11-03 6:19 [PATCH v2 0/5] ubifs: harden validation of on-disk inode and node metadata shao.mingyin
` (3 preceding siblings ...)
2026-11-03 6:26 ` [PATCH v2 4/5] ubifs: validate inode and data node sizes during replay shao.mingyin
@ 2026-11-03 6:27 ` shao.mingyin
4 siblings, 0 replies; 6+ messages in thread
From: shao.mingyin @ 2026-11-03 6:27 UTC (permalink / raw)
To: richard; +Cc: linux-mtd, chengzhihao1, linux-kernel, stable
remove_xattr() decrements the host inode's xattr counters, which are
stored on flash. An image can contain an xattr entry for an inode
whose counters are already zero or smaller than the entry. The
unsigned counters then wrap, and ubifs_jnl_delete_xattr() writes the
bogus values back to flash. On the next mount validate_inode() rejects
the inode because xattr_names + xattr_cnt exceeds XATTR_LIST_MAX, so a
single removexattr() turns a mountable image into one that cannot be
mounted.
Refuse the operation when the accounting cannot be decremented.
Fixes: 1e51764a3c2a ("UBIFS: add new flash file system")
Cc: stable@vger.kernel.org
Signed-off-by: Shao Mingyin <shao.mingyin@zte.com.cn>
---
v2: no change since v1.
diff --git a/fs/ubifs/xattr.c b/fs/ubifs/xattr.c
index 3b0e8a270f59..8f0447de94f0 100644
--- a/fs/ubifs/xattr.c
+++ b/fs/ubifs/xattr.c
@@ -456,6 +456,21 @@ static int remove_xattr(struct ubifs_info *c, struct inode *host,
ubifs_assert(c, ui->data_len == inode->i_size);
+ /*
+ * The per-inode xattr accounting is stored on flash and can disagree
+ * with the entries found in the TNC. Do not let a corrupted image
+ * underflow the counters, because the bogus values would then be
+ * written back to flash by ubifs_jnl_delete_xattr().
+ */
+ if (!host_ui->xattr_cnt ||
+ host_ui->xattr_names < fname_len(nm) ||
+ host_ui->xattr_size < CALC_DENT_SIZE(fname_len(nm)) +
+ CALC_XATTR_BYTES(ui->data_len)) {
+ ubifs_err(c, "xattr accounting mismatch for inode %llu",
+ host->i_ino);
+ return -EUCLEAN;
+ }
+
err = ubifs_budget_space(c, &req);
if (err)
return err;
^ permalink raw reply [flat|nested] 6+ messages in thread