public inbox for linux-xfs@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH] xfs_db: release ip resource before returning from get_next_unlinked()
@ 2024-08-02 22:25 Bill O'Donnell
  2024-08-02 23:23 ` Darrick J. Wong
  0 siblings, 1 reply; 6+ messages in thread
From: Bill O'Donnell @ 2024-08-02 22:25 UTC (permalink / raw)
  To: linux-xfs; +Cc: cem, djwong, 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));

Signed-off-by: Bill O'Donnell <bodonnel@redhat.com>
---
 db/iunlink.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/db/iunlink.c b/db/iunlink.c
index d87562e3..3b2417c5 100644
--- a/db/iunlink.c
+++ b/db/iunlink.c
@@ -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:
-- 
2.45.2


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

* Re: [PATCH] xfs_db: release ip resource before returning from get_next_unlinked()
  2024-08-02 22:25 [PATCH] xfs_db: release ip resource before returning from get_next_unlinked() Bill O'Donnell
@ 2024-08-02 23:23 ` Darrick J. Wong
  2024-08-05 14:42   ` Bill O'Donnell
  0 siblings, 1 reply; 6+ messages in thread
From: Darrick J. Wong @ 2024-08-02 23:23 UTC (permalink / raw)
  To: Bill O'Donnell; +Cc: linux-xfs, cem

On Fri, Aug 02, 2024 at 05:25:52PM -0500, Bill O'Donnell wrote:
> 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));
> 
> Signed-off-by: Bill O'Donnell <bodonnel@redhat.com>
> ---
>  db/iunlink.c | 1 +
>  1 file changed, 1 insertion(+)
> 
> diff --git a/db/iunlink.c b/db/iunlink.c
> index d87562e3..3b2417c5 100644
> --- a/db/iunlink.c
> +++ b/db/iunlink.c
> @@ -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

>  
>  	return ret;
>  bad:
> -- 
> 2.45.2
> 

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

* Re: [PATCH] xfs_db: release ip resource before returning from get_next_unlinked()
  2024-08-02 23:23 ` Darrick J. Wong
@ 2024-08-05 14:42   ` Bill O'Donnell
  2024-08-05 15:48     ` Bill O'Donnell
  2024-08-07 17:07     ` Eric Sandeen
  0 siblings, 2 replies; 6+ messages in thread
From: Bill O'Donnell @ 2024-08-05 14:42 UTC (permalink / raw)
  To: Darrick J. Wong; +Cc: linux-xfs, cem

On Fri, Aug 02, 2024 at 04:23:00PM -0700, Darrick J. Wong wrote:
> On Fri, Aug 02, 2024 at 05:25:52PM -0500, Bill O'Donnell wrote:
> > 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));
> > 
> > Signed-off-by: Bill O'Donnell <bodonnel@redhat.com>
> > ---
> >  db/iunlink.c | 1 +
> >  1 file changed, 1 insertion(+)
> > 
> > diff --git a/db/iunlink.c b/db/iunlink.c
> > index d87562e3..3b2417c5 100644
> > --- a/db/iunlink.c
> > +++ b/db/iunlink.c
> > @@ -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


> 
> --D
> 
> >  
> >  	return ret;
> >  bad:
> > -- 
> > 2.45.2
> > 
> 


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

* Re: [PATCH] xfs_db: release ip resource before returning from get_next_unlinked()
  2024-08-05 14:42   ` Bill O'Donnell
@ 2024-08-05 15:48     ` Bill O'Donnell
  2024-08-07 17:07     ` Eric Sandeen
  1 sibling, 0 replies; 6+ messages in thread
From: Bill O'Donnell @ 2024-08-05 15:48 UTC (permalink / raw)
  To: Bill O'Donnell; +Cc: Darrick J. Wong, linux-xfs, cem

On Mon, Aug 05, 2024 at 09:42:15AM -0500, Bill O'Donnell wrote:
> On Fri, Aug 02, 2024 at 04:23:00PM -0700, Darrick J. Wong wrote:
> > On Fri, Aug 02, 2024 at 05:25:52PM -0500, Bill O'Donnell wrote:
> > > 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));
> > > 
> > > Signed-off-by: Bill O'Donnell <bodonnel@redhat.com>
> > > ---
> > >  db/iunlink.c | 1 +
> > >  1 file changed, 1 insertion(+)
> > > 
> > > diff --git a/db/iunlink.c b/db/iunlink.c
> > > index d87562e3..3b2417c5 100644
> > > --- a/db/iunlink.c
> > > +++ b/db/iunlink.c
> > > @@ -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.

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

> 
> Thanks-
> Bill
> 
> 
> > 
> > --D
> > 
> > >  
> > >  	return ret;
> > >  bad:
> > > -- 
> > > 2.45.2
> > > 
> > 
> 
> 


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

* Re: [PATCH] xfs_db: release ip resource before returning from get_next_unlinked()
  2024-08-05 14:42   ` Bill O'Donnell
  2024-08-05 15:48     ` Bill O'Donnell
@ 2024-08-07 17:07     ` Eric Sandeen
  2024-08-07 17:35       ` Bill O'Donnell
  1 sibling, 1 reply; 6+ messages in thread
From: Eric Sandeen @ 2024-08-07 17:07 UTC (permalink / raw)
  To: Bill O'Donnell, Darrick J. Wong; +Cc: linux-xfs, cem

On 8/5/24 9:42 AM, Bill O'Donnell wrote:
> On Fri, Aug 02, 2024 at 04:23:00PM -0700, Darrick J. Wong wrote:
>> On Fri, Aug 02, 2024 at 05:25:52PM -0500, Bill O'Donnell wrote:
>>> 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));
>>>
>>> Signed-off-by: Bill O'Donnell <bodonnel@redhat.com>
>>> ---
>>>  db/iunlink.c | 1 +
>>>  1 file changed, 1 insertion(+)
>>>
>>> diff --git a/db/iunlink.c b/db/iunlink.c
>>> index d87562e3..3b2417c5 100644
>>> --- a/db/iunlink.c
>>> +++ b/db/iunlink.c
>>> @@ -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.

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

-Eric

> Thanks-
> Bill
> 
> 
>>
>> --D
>>
>>>  
>>>  	return ret;
>>>  bad:
>>> -- 
>>> 2.45.2
>>>
>>
> 
> 


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

* Re: [PATCH] xfs_db: release ip resource before returning from get_next_unlinked()
  2024-08-07 17:07     ` Eric Sandeen
@ 2024-08-07 17:35       ` Bill O'Donnell
  0 siblings, 0 replies; 6+ messages in thread
From: Bill O'Donnell @ 2024-08-07 17:35 UTC (permalink / raw)
  To: Eric Sandeen; +Cc: Darrick J. Wong, linux-xfs, cem

On Wed, Aug 07, 2024 at 12:07:37PM -0500, Eric Sandeen wrote:
> On 8/5/24 9:42 AM, Bill O'Donnell wrote:
> > On Fri, Aug 02, 2024 at 04:23:00PM -0700, Darrick J. Wong wrote:
> >> On Fri, Aug 02, 2024 at 05:25:52PM -0500, Bill O'Donnell wrote:
> >>> 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));
> >>>
> >>> Signed-off-by: Bill O'Donnell <bodonnel@redhat.com>
> >>> ---
> >>>  db/iunlink.c | 1 +
> >>>  1 file changed, 1 insertion(+)
> >>>
> >>> diff --git a/db/iunlink.c b/db/iunlink.c
> >>> index d87562e3..3b2417c5 100644
> >>> --- a/db/iunlink.c
> >>> +++ b/db/iunlink.c
> >>> @@ -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.
> 
> 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


> >>>  
> >>>  	return ret;
> >>>  bad:
> >>> -- 
> >>> 2.45.2
> >>>
> >>
> > 
> > 
> 


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

end of thread, other threads:[~2024-08-07 17:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-08-02 22:25 [PATCH] xfs_db: release ip resource before returning from get_next_unlinked() Bill O'Donnell
2024-08-02 23:23 ` Darrick J. Wong
2024-08-05 14:42   ` Bill O'Donnell
2024-08-05 15:48     ` Bill O'Donnell
2024-08-07 17:07     ` Eric Sandeen
2024-08-07 17:35       ` Bill O'Donnell

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