mirror of https://lore.kernel.org/lkml/
 help / color / mirror / Atom feed
* [PATCH v2 0/5] ubifs: harden validation of on-disk inode and node metadata
@ 2026-11-03  6:19 shao.mingyin
  2026-11-03  6:20 ` [PATCH v2 1/5] ubifs: reject inodes with an out-of-range size shao.mingyin
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: shao.mingyin @ 2026-11-03  6:19 UTC (permalink / raw)
  To: richard; +Cc: linux-mtd, chengzhihao1, linux-kernel, stable

This series tightens the checks ubifs performs on the metadata it reads
from flash.  All five patches are about size and accounting fields that
are taken from the image and later used without a range check; an image
whose CRCs and node tags are intact can therefore drive the code into
state it never expects.

Patch 1 rejects inodes whose size does not fit the signed loff_t the size
is stored in.  A 64-bit value with the top bit set becomes negative and
passes the max_inode_sz check, and the read paths then do size arithmetic
with it, for example (i_size + UBIFS_BLOCK_SIZE - 1) >> UBIFS_BLOCK_SHIFT
in do_readpage() and (i_size - 1) >> PAGE_SHIFT in populate_page().

Patch 2 rejects xattr inodes whose i_size does not match data_len.  ubifs
keeps the two equal for attribute inodes, and ubifs_xattr_get() and
remove_xattr() assert on the invariant; with assert=read-only or
assert=panic a plain getxattr() or removexattr() turns the file system
read-only or panics the kernel.

Patch 3 checks the lower bound of the master node's highest_inum.  Today
only the upper bound is validated, so a value below UBIFS_FIRST_INO is
accepted and ubifs_new_inode() allocates numbers from the range reserved
for special inodes.  With highest_inum == 0 the first new inode gets
number 1, which is UBIFS_ROOT_INO and already in use, and the index ends
up with two inodes sharing a number.

Patch 4 validates inode and data node sizes during replay.  replay_bud()
trusts them, and ubifs_recover_size() may then extend an inode to a bogus
size.  The normal read paths already reject such nodes, and truncation
nodes are checked in replay_bud() as well; do the same here.  The data
node end offset is also computed in 64 bits so that a large block number
cannot wrap.

Patch 5 refuses xattr accounting that cannot be decremented.  On an image
whose counters are already zero or smaller than the entry being removed,
the unsigned counters wrap and ubifs_jnl_delete_xattr() writes the bogus
values back to flash; the next mount then rejects the inode because
xattr_names + xattr_cnt exceeds XATTR_LIST_MAX, so a single removexattr()
makes the image unmountable.

To keep the severity claims honest: mounting such an image needs
CAP_SYS_ADMIN in the mounting namespace (or access to the flash), so none
of these is a non-root attack.  Patch 2 only turns into read-only or panic
with the assert= option; the default action is to report.  Patch 5 is the
one with a persistent effect.  Patches 1-3 are mount-time validation,
patch 4 is in replay, patch 5 is runtime accounting.

The patches are independent, have no inter-patch dependencies and can be
applied in any order or as a subset.  All five carry Fixes: and
Cc: stable.

Testing: each patch applies to next-20261030, passes checkpatch.pl
--strict with 0 errors/0 warnings/0 checks, and fs/ubifs/ builds with
CONFIG_UBIFS_FS_AUTHENTICATION, CONFIG_UBIFS_FS_XATTR and
CONFIG_UBIFS_FS_SECURITY enabled and W=1 without new warnings.  I did not
mount crafted images: this machine has no mkfs.ubifs/ubi tools and no
mtdram or nandsim setup.  I can put together an image for any of the five
if a runtime check would help the review.

Changes in v2:
- Patch 2: fold the size check into the existing ui->xattr block, as
  suggested by Zhihao Cheng.
- Patch 3: merge the lower bound into the highest_inum check; both bounds
  now report error 3, as suggested by Zhihao Cheng.
- Patches 1, 4 and 5 are unchanged from v1.

v1: https://lore.kernel.org/all/20261102214531231UAVA89ay_BDVcwFmrv8cq@zte.com.cn/

Signed-off-by: Shao Mingyin <shao.mingyin@zte.com.cn>
---
base-commit: 7649ba73f9e7e1e04d0b46213e39c79ae09dd55c

^ permalink raw reply	[flat|nested] 6+ messages in thread

* [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

end of thread, other threads:[~2026-11-03  6:27 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
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 ` [PATCH v2 3/5] ubifs: reject master nodes claiming a too small inode number 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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox

all inboxes | Powered by JetHome®