Skip to contentSkip to conversation

xfs_db: release ip resource before returning from get_next_unlinked()

PATCH Superseded Bill O'Donnell 2 participants 3 messages Aug 7, 2024

v2 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 xfs_db: release ip resource before returning from get_next_unlinked()

Fix potential memory leak in function get_next_unlinked(). Call libxfs_irele(ip) before exiting.

Details:
Error: RESOURCE_LEAK (CWE-772):
xfsprogs-6.5.0/db/iunlink.c:51:2: alloc_arg: "libxfs_iget" allocates memory that is stored into "ip". xfsprogs-6.5.0/db/iunlink.c:68:2: noescape: Resource "&ip->i_imap" is not freed or pointed-to in "libxfs_imap_to_bp". xfsprogs-6.5.0/db/iunlink.c:76:2: leaked_storage: Variable "ip" going out of scope leaks the storage it points to.

#   74|   	libxfs_buf_relse(ino_bp);
#   75|
#   76|-> 	return ret;
#   77|   bad:
#   78|   	dbprintf(_("AG %u agino %u: %s\n"), agno, agino, strerror(error));

---
v2: cover the error case.

---
 db/iunlink.c | 3 +++
 1 file changed, 3 insertions(+)
db/iunlink.c +3 -0
index d87562e3..bd973600 100644
@@ -72,9 +72,12 @@ get_next_unlinked(
 	dip = xfs_buf_offset(ino_bp, ip->i_imap.im_boffset);
 	ret = be32_to_cpu(dip->di_next_unlinked);
 	libxfs_buf_relse(ino_bp);
+	libxfs_irele(ip);
 
 	return ret;
 bad:
+	libxfs_buf_relse(ino_bp);
+	libxfs_irele(ip);
 	dbprintf(_("AG %u agino %u: %s\n"), agno, agino, strerror(error));
 	return NULLAGINO;
 }
Signature · 1 line
2.45.2
#1 lore raw
Quoted 26 lines · Bill O'Donnell

Fix potential memory leak in function get_next_unlinked(). Call libxfs_irele(ip) before exiting.

Details:
Error: RESOURCE_LEAK (CWE-772):
xfsprogs-6.5.0/db/iunlink.c:51:2: alloc_arg: "libxfs_iget" allocates memory that is stored into "ip". xfsprogs-6.5.0/db/iunlink.c:68:2: noescape: Resource "&ip->i_imap" is not freed or pointed-to in "libxfs_imap_to_bp". xfsprogs-6.5.0/db/iunlink.c:76:2: leaked_storage: Variable "ip" going out of scope leaks the storage it points to.

#   74|   	libxfs_buf_relse(ino_bp);
#   75|
#   76|-> 	return ret;
#   77|   bad:
#   78|   	dbprintf(_("AG %u agino %u: %s\n"), agno, agino, strerror(error));

---
v2: cover the error case.

---
 db/iunlink.c | 3 +++
 1 file changed, 3 insertions(+)
db/iunlink.c +0 -0
index d87562e3..bd973600 100644

db/iunlink.c get_next_unlinked( · line 79

Quoted 8 lines
db/iunlink.c +2 -0
@@ -72,9 +72,12 @@ get_next_unlinked(
 	dip = xfs_buf_offset(ino_bp, ip->i_imap.im_boffset);
 	ret = be32_to_cpu(dip->di_next_unlinked);
 	libxfs_buf_relse(ino_bp);
+	libxfs_irele(ip);
 
 	return ret;
 bad:
+	libxfs_buf_relse(ino_bp);

What if we got here via the first 'goto bad' in this function? We'll be feeding an uninitialized variable into libxfs_buf_relse.

--D

Quoted 8 lines
db/iunlink.c +1 -0
+	libxfs_irele(ip);
 	dbprintf(_("AG %u agino %u: %s\n"), agno, agino, strerror(error));
 	return NULLAGINO;
 }
Signature · 1 line
2.45.2
#2 lore raw
Quoted 32 lines · Darrick J. Wong
Quoted 31 lines · Bill O'Donnell

Fix potential memory leak in function get_next_unlinked(). Call libxfs_irele(ip) before exiting.

Details:
Error: RESOURCE_LEAK (CWE-772):
xfsprogs-6.5.0/db/iunlink.c:51:2: alloc_arg: "libxfs_iget" allocates memory that is stored into "ip". xfsprogs-6.5.0/db/iunlink.c:68:2: noescape: Resource "&ip->i_imap" is not freed or pointed-to in "libxfs_imap_to_bp". xfsprogs-6.5.0/db/iunlink.c:76:2: leaked_storage: Variable "ip" going out of scope leaks the storage it points to.

#   74|   	libxfs_buf_relse(ino_bp);
#   75|
#   76|-> 	return ret;
#   77|   bad:
#   78|   	dbprintf(_("AG %u agino %u: %s\n"), agno, agino, strerror(error));

---
v2: cover the error case.

---
 db/iunlink.c | 3 +++
 1 file changed, 3 insertions(+)
db/iunlink.c +1 -0
index d87562e3..bd973600 100644
@@ -72,9 +72,12 @@ get_next_unlinked(
 	dip = xfs_buf_offset(ino_bp, ip->i_imap.im_boffset);
 	ret = be32_to_cpu(dip->di_next_unlinked);
 	libxfs_buf_relse(ino_bp);
+	libxfs_irele(ip);
 
Quoted 6 lines

db/iunlink.c get_next_unlinked( · line 79

Quoted 3 lines
db/iunlink.c +1 -0
 	return ret;
 bad:
+	libxfs_buf_relse(ino_bp);

What if we got here via the first 'goto bad' in this function? We'll be feeding an uninitialized variable into libxfs_buf_relse.

ugh. Missed that, thanks.

-Bill

Quoted 12 lines

--D

Quoted 8 lines
db/iunlink.c +1 -0
+	libxfs_irele(ip);
 	dbprintf(_("AG %u agino %u: %s\n"), agno, agino, strerror(error));
 	return NULLAGINO;
 }
Signature · 1 line
2.45.2