Skip to contentSkip to conversation

xfsprogs: add missing bounds checks for on-disk fields

PATCH New Tristan Madani 1 participant 6 messages Nov 1

lore
Share & tools
lore
#0 lore raw

cover xfsprogs: add missing bounds checks for on-disk fields

Hi Andrey,

These five patches add missing bounds checks for on-disk fields that are used as loop counts or copy lengths in xfs_repair, xfs_copy, xfs_db, and xfs_logprint. In each case, sibling code paths in the same codebase already validate these fields correctly -- these patches close the gaps.

Tristan Madani (5):
repair: validate MAC label attribute length before stack copy
copy: validate bb_numrecs before iterating alloc btree records
repair: cap extent record count at physical fork capacity
db: validate symlink target length in rdump_symlink
logprint: bounds-check continued log record copies

 copy/xfs_copy.c      |  8 ++++++++
 db/rdump.c           |  6 ++++++
 logprint/log_redo.c  | 15 ++++++++++-----
 repair/attr_repair.c |  6 ++++++
 repair/dinode.c      |  5 +++++
 repair/prefetch.c    |  3 ++-
 6 files changed, 37 insertions(+), 6 deletions(-)

--
2.53.0

#1 lore raw

PATCH 1/5 repair: validate MAC label attribute length before stack copy

valuecheck() copies on-disk MAC label attribute values into a stack-allocated xfs_mac_label_t buffer (508 bytes) using memmove() with the on-disk valuelen field as the copy length. For leaf-format attributes, valuelen is a 16-bit value read directly from disk and can be up to 65535 bytes, overflowing the stack buffer.

The ACL path in the same function already handles this correctly by using malloc(valuelen) for a heap allocation. Add a bounds check for the MAC label path to reject oversized values before the copy.

Found by code audit.

---
 repair/attr_repair.c | 6 ++++++
 1 file changed, 6 insertions(+)
repair/attr_repair.c +6 -0
index 982dc838..876f4240 100644
@@ -164,6 +164,12 @@ valuecheck(
 			free(valuep);
 
 	} else if (strncmp(namevalue, SGI_MAC_FILE, SGI_MAC_FILE_SIZE) == 0) {
+		if (valuelen > sizeof(xfs_mac_label_t)) {
+			do_warn(
+	_("entry contains oversized value (%d bytes) in attribute named SGI_MAC_LABEL\n"),
+				valuelen);
+			return 1;
+		}
 		if (value == NULL) {
 			memset(&macl, 0, sizeof(xfs_mac_label_t));
 			memmove(&macl, namevalue+namelen, valuelen);

--
2.53.0

#2 lore raw

PATCH 2/5 copy: validate bb_numrecs before iterating alloc btree records

In reply to Tristan Madani

The alloc btree leaf record loop in xfs_copy uses the on-disk bb_numrecs field directly as the iteration count without checking that it does not exceed the maximum number of records that fit in a single btree block. A crafted filesystem image with an inflated bb_numrecs causes reads past the btree block buffer.

Add a check against mp->m_alloc_mxr[0] (the precomputed maximum leaf records per block) before the loop, consistent with the validation already present in repair/scan.c for the same btree type.

Found by code audit.

---
 copy/xfs_copy.c | 8 ++++++++
 1 file changed, 8 insertions(+)
copy/xfs_copy.c +8 -0
index 4692a4ff..9658aea3 100644
@@ -1083,6 +1083,14 @@ main(int argc, char **argv)
 			}
 
 			rec_ptr = XFS_ALLOC_REC_ADDR(mp, block, 1);
+			if (be16_to_cpu(block->bb_numrecs) >
+						mp->m_alloc_mxr[0]) {
+				do_log(
+			_("WARNING:  bb_numrecs %u exceeds max %u in AG %u.  Aborting.\n"),
+					be16_to_cpu(block->bb_numrecs),
+					mp->m_alloc_mxr[0], agno);
+				exit(1);
+			}
 			for (i = 0; i < be16_to_cpu(block->bb_numrecs);
 							i++, rec_ptr++)  {
 				/* calculate in daddr's */

--
2.53.0

#3 lore raw

PATCH 3/5 repair: cap extent record count at physical fork capacity

In reply to Tristan Madani

process_exinode() uses the on-disk di_nextents count as the loop bound for iterating extent records, capped only at the format maximum (2^31 or 2^48 for large extent counts) rather than the physical inode fork size. A corrupt inode with an inflated di_nextents causes reads past the inode fork buffer.

Add a check against XFS_DFORK_MAXEXT(), which computes the actual number of extent records that fit in the fork, consistent with the fork size validation already done in the btree-format path in process_btinode().

Apply the same cap in the prefetch path (pf_read_exinode), which passes xfs_dfork_data_extents() directly to pf_read_bmbt_reclist() without any fork size validation.

Found by code audit.

---
 repair/dinode.c   | 5 +++++
 repair/prefetch.c | 3 ++-
 2 files changed, 7 insertions(+), 1 deletion(-)
repair/dinode.c +5 -0
index 243fcf7a..2240720a 100644
@@ -1529,6 +1529,11 @@ process_exinode(
 	if (numrecs > max_numrecs)
 		numrecs = *nex;
 
+	/* Also cap at the physical fork capacity to avoid reading past
+	 * the inode fork buffer when on-disk nextents is corrupt. */
+	if (numrecs > XFS_DFORK_MAXEXT(dip, mp, whichfork))
+		numrecs = XFS_DFORK_MAXEXT(dip, mp, whichfork);
+
 	/*
 	 * XXX - if we were going to fix up the btree record,
 	 * we'd do it right here.  For now, if there's a problem,
repair/prefetch.c +2 -1
index 3e03ae7f..df0e8be5 100644
@@ -393,7 +393,8 @@ pf_read_exinode(
 	struct xfs_dinode	*dino)
 {
 	pf_read_bmbt_reclist(args, (xfs_bmbt_rec_t *)XFS_DFORK_DPTR(dino),
-			xfs_dfork_data_extents(dino));
+			min(xfs_dfork_data_extents(dino),
+			    XFS_DFORK_MAXEXT(dino, mp, XFS_DATA_FORK)));
 }
 
 static void

--
2.53.0

#4 lore raw

PATCH 4/5 db: validate symlink target length in rdump_symlink

In reply to Tristan Madani

rdump_symlink() reads i_disk_size directly from the on-disk inode and uses it as the copy length into a 1025-byte stack buffer (XFS_SYMLINK_MAXLEN + 1) without checking bounds. For both the local (memcpy) and remote (libxfs_symlink_remote_read) paths, a corrupt inode with an oversized di_size overflows the stack buffer.

Add a check against XFS_SYMLINK_MAXLEN before the copy, consistent with the validation in repair/dinode.c which already checks di_size for symlink inodes.

Found by code audit.

---
 db/rdump.c | 6 ++++++
 1 file changed, 6 insertions(+)
db/rdump.c +6 -0
index a0177e5b..1884b2ff 100644
@@ -766,6 +766,12 @@ rdump_symlink(
 	unsigned int		targetlen = ip->i_disk_size;
 	int			ret;
 
+	if (targetlen > XFS_SYMLINK_MAXLEN) {
+		dbprintf(_("%s: symlink target length %u exceeds maximum %u\n"),
+				pbuf->path, targetlen, XFS_SYMLINK_MAXLEN);
+		return strict_errors ? 1 : 0;
+	}
+
 	if (ifp->if_format == XFS_DINODE_FMT_LOCAL) {
 		memcpy(target, ifp->if_data, targetlen);
 	} else {

--
2.53.0

#5 lore raw

PATCH 5/5 logprint: bounds-check continued log record copies

In reply to Tristan Madani

Five log redo format copy functions (xfs_efi_copy_format, xfs_rui_copy_format, xfs_cui_copy_format, xfs_bui_copy_format, xfs_xmi_copy_format) bypass the record length validation when the XLOG_WAS_CONT_TRANS flag is set. In that case, memcpy copies the full source length into a destination buffer sized by the nextents field, without verifying that the source length fits. A crafted log image can trigger a heap buffer overflow via an oversized continued record.

Fix all five functions to check that the source length does not exceed the destination buffer size even for continued records.

Found by code audit.

---
 logprint/log_redo.c | 15 ++++++++++-----
 1 file changed, 10 insertions(+), 5 deletions(-)
logprint/log_redo.c +10 -5
index 38caf5a2..df24b77f 100644
@@ -24,9 +24,13 @@ xfs_efi_copy_format(
 	uint len32 = xfs_efi_log_format32_sizeof(nextents);
 	uint len64 = xfs_efi_log_format64_sizeof(nextents);
 
-	if (len == dst_len || continued) {
+	if (len == dst_len || (continued && len <= dst_len)) {
 		memcpy((char *)dst_efi_fmt, buf, len);
 		return 0;
+	} else if (continued) {
+		fprintf(stderr, _("%s: continued efi record too large: %u; max %u\n"),
+			progname, len, dst_len);
+		return 1;
 	} else if (len == len32) {
 		struct xfs_efi_log_format_32 *src_efi_fmt_32 =
 			(struct xfs_efi_log_format_32 *)buf;
@@ -264,7 +268,7 @@ xfs_rui_copy_format(
 	uint nextents = ((struct xfs_rui_log_format *)buf)->rui_nextents;
 	uint dst_len = xfs_rui_log_format_sizeof(nextents);
 
-	if (len == dst_len || continued) {
+	if (len == dst_len || (continued && len <= dst_len)) {
 		memcpy((char *)dst_fmt, buf, len);
 		return 0;
 	}
@@ -430,7 +434,7 @@ xfs_cui_copy_format(
 	nextents = cui->cui_nextents;
 	dst_len = xfs_cui_log_format_sizeof(nextents);
 
-	if (len == dst_len || continued) {
+	if (len == dst_len || (continued && len <= dst_len)) {
 		memcpy(dst_fmt, cui, len);
 		return 0;
 	}
@@ -587,7 +591,7 @@ xfs_bui_copy_format(
 	nextents = bui->bui_nextents;
 	dst_len = xfs_bui_log_format_sizeof(nextents);
 
-	if (len == dst_len || continued) {
+	if (len == dst_len || (continued && len <= dst_len)) {
 		memcpy(dst_fmt, bui, len);
 		return 0;
 	}
@@ -1086,7 +1090,8 @@ xfs_xmi_copy_format(
 	struct xfs_xmi_log_format *dst_fmt,
 	int			  continued)
 {
-	if (len == sizeof(struct xfs_xmi_log_format) || continued) {
+	if (len == sizeof(struct xfs_xmi_log_format) ||
+	    (continued && len <= sizeof(struct xfs_xmi_log_format))) {
 		memcpy(dst_fmt, xmi, len);
 		return 0;
 	}

--
2.53.0