Skip to contentSkip to conversation

mdrestore: fix extent length overflow in v2 restore path

PATCH Superseded Manognya Singuru 2 participants 3 messages Jul 9 – Jul 13

v1 of 2 Superseded Latest is v2 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 mdrestore: fix extent length overflow in v2 restore path

Aisle Research reported that a crafted metadump v2 extent length can overflow the signed int used for length, leading to incorrect size calculations and a potential heap buffer over-read in restore_meta_extent().

Change the length type to uint64_t and ensure all size arithmetic in 64-bit before converting to size_t for I/O calls.

---
 mdrestore/xfs_mdrestore.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
mdrestore/xfs_mdrestore.c +5 -7
index f10c4bef..9c7f77e0 100644
@@ -395,11 +395,9 @@ restore_meta_extent(
 	char		*device,
 	void		*buf,
 	uint64_t	offset,
-	int		len)
+	uint64_t		len)
 {
-	int		io_size;
-
-	io_size = min(len, MDR_IO_BUF_SIZE);
+	size_t io_size = min(len, MDR_IO_BUF_SIZE);
 
 	do {
 		if (fread(buf, io_size, 1, md_fp) != 1)
@@ -428,7 +426,7 @@ restore_v2(
 	int64_t			mb_read = 0;
 	int64_t			bytes_read;
 	uint64_t		offset;
-	int			len;
+	uint64_t			len;
 
 	block_buffer = malloc(MDR_IO_BUF_SIZE);
 	if (block_buffer == NULL)
@@ -442,7 +440,7 @@ restore_v2(
 			XME_ADDR_DATA_DEVICE)
 		fatal("Invalid superblock disk address/length\n");
 
-	len = BBTOB(be32_to_cpu(xme.xme_len));
+	len = BBTOB((uint64_t)be32_to_cpu(xme.xme_len));
 
 	if (fread(block_buffer, len, 1, md_fp) != 1)
 		fatal("error reading from metadump file\n");
@@ -503,7 +501,7 @@ restore_v2(
 			break;
 		}
 
-		len = BBTOB(be32_to_cpu(xme.xme_len));
+		len = BBTOB((uint64_t)be32_to_cpu(xme.xme_len));
 
 		restore_meta_extent(md_fp, fd, device, block_buffer, offset,
 				len);
Signature · 1 line
2.54.0
Reviewed-by on v1 1/1
#1 lore raw
Quoted 19 lines · Manognya Singuru

Aisle Research reported that a crafted metadump v2 extent length can overflow the signed int used for length, leading to incorrect size calculations and a potential heap buffer over-read in restore_meta_extent().

Change the length type to uint64_t and ensure all size arithmetic in 64-bit before converting to size_t for I/O calls.

---
 mdrestore/xfs_mdrestore.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
mdrestore/xfs_mdrestore.c +0 -0
index f10c4bef..9c7f77e0 100644

mdrestore/xfs_mdrestore.c restore_meta_extent( · line 398

Quoted 5 lines
mdrestore/xfs_mdrestore.c +1 -1
@@ -395,11 +395,9 @@ restore_meta_extent(
 	char		*device,
 	void		*buf,
 	uint64_t	offset,
-	int		len)
+	uint64_t		len)

Please make the indentation consistent with the existing code here and elsewhere in the patch.

Quoted 18 lines
mdrestore/xfs_mdrestore.c +2 -4
 {
-	int		io_size;
-
-	io_size = min(len, MDR_IO_BUF_SIZE);
+	size_t io_size = min(len, MDR_IO_BUF_SIZE);
 
 	do {
 		if (fread(buf, io_size, 1, md_fp) != 1)
@@ -428,7 +426,7 @@ restore_v2(
 	int64_t			mb_read = 0;
 	int64_t			bytes_read;
 	uint64_t		offset;
-	int			len;
+	uint64_t			len;
 
 	block_buffer = malloc(MDR_IO_BUF_SIZE);
 	if (block_buffer == NULL)

mdrestore/xfs_mdrestore.c restore_v2( · line 443

Quoted 5 lines
mdrestore/xfs_mdrestore.c +1 -1
@@ -442,7 +440,7 @@ restore_v2(
 			XME_ADDR_DATA_DEVICE)
 		fatal("Invalid superblock disk address/length\n");
 
-	len = BBTOB(be32_to_cpu(xme.xme_len));
+	len = BBTOB((uint64_t)be32_to_cpu(xme.xme_len));

/me wonders if BBTOB ought to cast its parameter to uint64_t but this change here avoids shift truncation if xme_len is large, so

--D

Quoted 16 lines
mdrestore/xfs_mdrestore.c +1 -1
 	if (fread(block_buffer, len, 1, md_fp) != 1)
 		fatal("error reading from metadump file\n");
@@ -503,7 +501,7 @@ restore_v2(
 			break;
 		}
 
-		len = BBTOB(be32_to_cpu(xme.xme_len));
+		len = BBTOB((uint64_t)be32_to_cpu(xme.xme_len));
 
 		restore_meta_extent(md_fp, fd, device, block_buffer, offset,
 				len);
Signature · 1 line
2.54.0
#2 lore raw
Quoted 16 lines · Darrick J. Wong
Quoted 14 lines · Manognya Singuru

Aisle Research reported that a crafted metadump v2 extent length can overflow the signed int used for length, leading to incorrect size calculations and a potential heap buffer over-read in restore_meta_extent().

Change the length type to uint64_t and ensure all size arithmetic in 64-bit before converting to size_t for I/O calls.

---
 mdrestore/xfs_mdrestore.c | 12 +++++-------
 1 file changed, 5 insertions(+), 7 deletions(-)
Quoted 14 lines

mdrestore/xfs_mdrestore.c restore_meta_extent( · line 398

Quoted 10 lines
mdrestore/xfs_mdrestore.c +1 -1
index f10c4bef..9c7f77e0 100644
@@ -395,11 +395,9 @@ restore_meta_extent(
      char            *device,
      void            *buf,
      uint64_t        offset,
-     int             len)
+     uint64_t                len)

Please make the indentation consistent with the existing code here and elsewhere in the patch.

Sure, I have made the indentation consistent and sent PATCH v2.

Quoted 21 lines
Quoted 21 lines
mdrestore/xfs_mdrestore.c +2 -4
 {
-     int             io_size;
-
-     io_size = min(len, MDR_IO_BUF_SIZE);
+     size_t io_size = min(len, MDR_IO_BUF_SIZE);
 
      do {
              if (fread(buf, io_size, 1, md_fp) != 1)
@@ -428,7 +426,7 @@ restore_v2(
      int64_t                 mb_read = 0;
      int64_t                 bytes_read;
      uint64_t                offset;
-     int                     len;
+     uint64_t                        len;
 
      block_buffer = malloc(MDR_IO_BUF_SIZE);
      if (block_buffer == NULL)
@@ -442,7 +440,7 @@ restore_v2(
                      XME_ADDR_DATA_DEVICE)
              fatal("Invalid superblock disk address/length\n");
 
Quoted 6 lines

mdrestore/xfs_mdrestore.c restore_v2( · line 443

Quoted 2 lines
mdrestore/xfs_mdrestore.c +1 -1
-     len = BBTOB(be32_to_cpu(xme.xme_len));
+     len = BBTOB((uint64_t)be32_to_cpu(xme.xme_len));

/me wonders if BBTOB ought to cast its parameter to uint64_t but this change here avoids shift truncation if xme_len is large, so

Yes, my thinking was that casting to uint64_t would force BBTOB() to perform the shift in 64-bit arithmetic and avoid shift truncation.

Quoted 21 lines

--D

Quoted 16 lines
mdrestore/xfs_mdrestore.c +1 -2
      if (fread(block_buffer, len, 1, md_fp) != 1)
              fatal("error reading from metadump file\n");
@@ -503,7 +501,7 @@ restore_v2(
                      break;
              }
 
-             len = BBTOB(be32_to_cpu(xme.xme_len));
+             len = BBTOB((uint64_t)be32_to_cpu(xme.xme_len));
 
              restore_meta_extent(md_fp, fd, device, block_buffer, offset,
                              len);
--

2.54.0