Skip to contentSkip to conversation

xfs_db: release ip resource before returning from get_next_unlinked()

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

v1 of 5 Superseded Latest is v5 →

lore
Share & tools
lore
#0 lore raw

PATCH 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));
---
 db/iunlink.c | 1 +
 1 file changed, 1 insertion(+)
db/iunlink.c +1 -0
index d87562e3..3b2417c5 100644
@@ -72,6 +72,7 @@ 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:
Signature · 1 line
2.45.2
#1 lore raw
Quoted 24 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));
---
 db/iunlink.c | 1 +
 1 file changed, 1 insertion(+)
db/iunlink.c +0 -0
index d87562e3..3b2417c5 100644

db/iunlink.c get_next_unlinked( · line 75

Quoted 4 lines
db/iunlink.c +1 -0
@@ -72,6 +72,7 @@ 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);

I think this needs to cover the error return for libxfs_imap_to_bp too, doesn't it?

--D

Quoted 6 lines
db/iunlink.c +0 -0
 	return ret;
 bad:
Signature · 1 line
2.45.2
#2 lore raw
Quoted 20 lines · Darrick J. Wong
Quoted 19 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));
---
 db/iunlink.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

db/iunlink.c get_next_unlinked( · line 75

Quoted 9 lines
db/iunlink.c +1 -0
index d87562e3..3b2417c5 100644
@@ -72,6 +72,7 @@ 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);

I think this needs to cover the error return for libxfs_imap_to_bp too, doesn't it?

I considered that, but there are several places in the code where the error return doesn't release the resource. Not that that's correct, but the scans didn't flag them. For example, in bmap_inflate.c, bmapinflate_f() does not release the resource and scans didn't flag it.

Thanks-
Bill

Quoted 10 lines

--D

Quoted 6 lines
db/iunlink.c +0 -0
 	return ret;
 bad:
Signature · 1 line
2.45.2
#3 lore raw
Quoted 34 lines · Bill O'Donnell
Quoted 20 lines · Darrick J. Wong
Quoted 19 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));
---
 db/iunlink.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

db/iunlink.c get_next_unlinked( · line 75

Quoted 9 lines
db/iunlink.c +1 -0
index d87562e3..3b2417c5 100644
@@ -72,6 +72,7 @@ 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);

I think this needs to cover the error return for libxfs_imap_to_bp too, doesn't it?

Quoted 4 lines

I considered that, but there are several places in the code where the error return doesn't release the resource. Not that that's correct, but the scans didn't flag them. For example, in bmap_inflate.c, bmapinflate_f() does not release the resource and scans didn't flag it.

Looking at libxfs_iget(), it seems that for error cases, the resource is released within that function.
-Bill

Quoted 17 lines

Thanks-
Bill

Quoted 10 lines

--D

Quoted 6 lines
db/iunlink.c +0 -0
 	return ret;
 bad:
Signature · 1 line
2.45.2
#4 lore raw
In reply to Bill O'Donnell
Quoted 34 lines · Bill O'Donnell
Quoted 20 lines · Darrick J. Wong
Quoted 19 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));
---
 db/iunlink.c | 1 +
 1 file changed, 1 insertion(+)
Quoted 12 lines

db/iunlink.c get_next_unlinked( · line 75

Quoted 9 lines
db/iunlink.c +1 -0
index d87562e3..3b2417c5 100644
@@ -72,6 +72,7 @@ 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);

I think this needs to cover the error return for libxfs_imap_to_bp too, doesn't it?

Quoted 4 lines

I considered that, but there are several places in the code where the error return doesn't release the resource. Not that that's correct, but the scans didn't flag them. For example, in bmap_inflate.c, bmapinflate_f() does not release the resource and scans didn't flag it.

Upstream coverity scan does flag it, CID 1554242 (that CID actually covers both instances of the leak).

-Eric

Quoted 16 lines

Thanks-
Bill

Quoted 10 lines

--D

Quoted 6 lines
db/iunlink.c +0 -0
 	return ret;
 bad:
Signature · 1 line
2.45.2
#5 lore raw
Quoted 35 lines · Eric Sandeen
Quoted 34 lines · Bill O'Donnell
Quoted 32 lines · Darrick J. Wong
Quoted 24 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));
---
 db/iunlink.c | 1 +
 1 file changed, 1 insertion(+)
db/iunlink.c +0 -0
index d87562e3..3b2417c5 100644

db/iunlink.c get_next_unlinked( · line 75

Quoted 4 lines
db/iunlink.c +1 -0
@@ -72,6 +72,7 @@ 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);

I think this needs to cover the error return for libxfs_imap_to_bp too, doesn't it?

Quoted 7 lines
Quoted 4 lines

I considered that, but there are several places in the code where the error return doesn't release the resource. Not that that's correct, but the scans didn't flag them. For example, in bmap_inflate.c, bmapinflate_f() does not release the resource and scans didn't flag it.

Upstream coverity scan does flag it, CID 1554242 (that CID actually covers both instances of the leak).

Ah, thanks. I'll send a v2.
-Bill

Quoted 10 lines
Quoted 9 lines
Quoted 7 lines
Quoted 6 lines
db/iunlink.c +0 -0
 	return ret;
 bad:
Signature · 1 line
2.45.2