Skip to contentSkip to conversation

mkfs: fixup memory leak on exit

xfsprogs Wilfred Mallawa 6 participants 11 messages Oct 6 – Nov 2

lore
Share & tools
lore
#0 lore raw

From: Wilfred Mallawa <[email protected]>

When running mkfs.xfs with addrsan enabled, the following is reported:

```
==27832==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 9 byte(s) in 1 object(s) allocated from:
    #0 0x7fe6db2e8ac4 in strdup (/lib64/libasan.so.8+0xe8ac4) (BuildId: 5395ec74f54d9ec7bf97c06583dd39a96c230822)
    #1 0x0000004175ce in getstr <blah>/mkfs/xfs_mkfs.c:1767

SUMMARY: AddressSanitizer: 9 byte(s) leaked in 1 allocation(s).
```

Thus, gracefully clean up these string allocation on exit to address the above.

---
 mkfs/xfs_mkfs.c | 3 +++
 1 file changed, 3 insertions(+)
mkfs/xfs_mkfs.c +3 -0
index 334367b8..fec43987 100644
@@ -6503,5 +6503,8 @@ main(
 		exit(1);
 
 	libxfs_destroy(&xi);
+	free(cli.xi->data.name);
+	free(cli.xi->log.name);
+	free(cli.xi->rt.name);
 	return 0;
 }
Signature · 1 line
2.55.0
#1 lore raw
Quoted 26 lines · Wilfred Mallawa

From: Wilfred Mallawa <[email protected]>

When running mkfs.xfs with addrsan enabled, the following is reported:

```
==27832==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 9 byte(s) in 1 object(s) allocated from:
    #0 0x7fe6db2e8ac4 in strdup (/lib64/libasan.so.8+0xe8ac4) (BuildId: 5395ec74f54d9ec7bf97c06583dd39a96c230822)
    #1 0x0000004175ce in getstr <blah>/mkfs/xfs_mkfs.c:1767

SUMMARY: AddressSanitizer: 9 byte(s) leaked in 1 allocation(s).
```

Thus, gracefully clean up these string allocation on exit to address the above.

---
 mkfs/xfs_mkfs.c | 3 +++
 1 file changed, 3 insertions(+)
mkfs/xfs_mkfs.c +0 -0
index 334367b8..fec43987 100644

mkfs/xfs_mkfs.c main( · line 6510

Quoted 8 lines
mkfs/xfs_mkfs.c +3 -0
@@ -6503,5 +6503,8 @@ main(
 		exit(1);
 
 	libxfs_destroy(&xi);
+	free(cli.xi->data.name);
+	free(cli.xi->log.name);
+	free(cli.xi->rt.name);
 	return 0;
 }

Hm, wouldn't perhaps be better to hook this into libxfs_destroy? Although this would likely need some other extra surgery on xfs_copy.c.

It just sounds weird to me to free the memory here 'after' we already called libxfs_destroy() here. IIUC: cli.xi == &xi in this place, so freeing up memory after destroying the struct using it sounds like a time bomb.

#2 lore raw
Quoted 21 lines · Carlos Maiolino
Quoted 14 lines

 mkfs/xfs_mkfs.c | 3 +++

 1 file changed, 3 insertions(+)
mkfs/xfs_mkfs.c +0 -0
index 334367b8..fec43987 100644
@@ -6503,5 +6503,8 @@ main(
  		exit(1);


  libxfs_destroy(&xi);

mkfs/xfs_mkfs.c +3 -0
+	free(cli.xi->data.name);
+	free(cli.xi->log.name);
+	free(cli.xi->rt.name);
Quoted 2 lines

  return 0;
 }

Hm, wouldn't perhaps be better to hook this into libxfs_destroy? Although this would likely need some other extra surgery on xfs_copy.c.

Quoted 3 lines

It just sounds weird to me to free the memory here 'after' we already called
libxfs_destroy() here.

Ah yeah.... I back-ported this off the xraid tree, where we have a bit more cleanup after libxfs_destroy() and I think it makes sense to do there but maybe not here...

Quoted 2 lines

IIUC: cli.xi == &xi in this place, so freeing up
memory after destroying the struct using it sounds like a time bomb.

I can plumb this into libxfs_destroy() but as you mentioned, it will need changes in xfs_copy, xfs_db and xfs_repair because they track the argv pointers, but we can just strdup(). Does that seem okay?

Thanks!

#3 lore raw
Quoted 30 lines · Wilfred Mallawa
Quoted 21 lines · Carlos Maiolino
Quoted 14 lines

 mkfs/xfs_mkfs.c | 3 +++

 1 file changed, 3 insertions(+)
mkfs/xfs_mkfs.c +0 -0
index 334367b8..fec43987 100644
@@ -6503,5 +6503,8 @@ main(
  		exit(1);


  libxfs_destroy(&xi);

mkfs/xfs_mkfs.c +3 -0
+	free(cli.xi->data.name);
+	free(cli.xi->log.name);
+	free(cli.xi->rt.name);
Quoted 2 lines

  return 0;
 }

Hm, wouldn't perhaps be better to hook this into libxfs_destroy? Although this would likely need some other extra surgery on xfs_copy.c.

Quoted 3 lines

It just sounds weird to me to free the memory here 'after' we already called
libxfs_destroy() here.

Ah yeah.... I back-ported this off the xraid tree, where we have a bit more cleanup after libxfs_destroy() and I think it makes sense to do there but maybe not here...

Quoted 6 lines
Quoted 2 lines

IIUC: cli.xi == &xi in this place, so freeing up
memory after destroying the struct using it sounds like a time bomb.

I can plumb this into libxfs_destroy() but as you mentioned, it will need changes in xfs_copy, xfs_db and xfs_repair because they track the argv pointers, but we can just strdup(). Does that seem okay?

I don't think this is better. libxfs' init functions do not create or assign the libxfs_init.{data,log,rt}.name fields, they merely reuse whatever pointer the calling program set. Only the caller knows where that pointed to memory came from (stack, optarg, dynamic allocation) so only the caller knows how to drop the pointer.

The original patch is more appropriate IMO but seeing as we're about to return from main and terminate the process, why do we care? And if we do, shouldn't mkfs also free the other getstr() strings attached to the cli struct? And shouldn't the exit(1) paths do likewise?

(To be clear, my answers are "I don't", "no", and "no" but I could be convinced.)

--D

#4 lore raw
Quoted 53 lines · Darrick J. Wong
Quoted 37 lines · Wilfred Mallawa
Quoted 21 lines · Carlos Maiolino
Quoted 14 lines

 mkfs/xfs_mkfs.c | 3 +++

 1 file changed, 3 insertions(+)
mkfs/xfs_mkfs.c +0 -0
index 334367b8..fec43987 100644
@@ -6503,5 +6503,8 @@ main(
  		exit(1);


  libxfs_destroy(&xi);

mkfs/xfs_mkfs.c +3 -0
+	free(cli.xi->data.name);
+	free(cli.xi->log.name);
+	free(cli.xi->rt.name);
Quoted 2 lines

  return 0;
 }

Hm, wouldn't perhaps be better to hook this into libxfs_destroy? Although this would likely need some other extra surgery on xfs_copy.c.

Quoted 4 lines

It just sounds weird to me to free the memory here 'after' we already
called
libxfs_destroy() here.

Ah yeah.... I back-ported this off the xraid tree, where we have a bit
more cleanup after libxfs_destroy() and I think it makes sense to do
there but maybe not here...

Quoted 3 lines

IIUC: cli.xi == &xi in this place, so freeing up memory after destroying the struct using it sounds like a time bomb.

Quoted 5 lines

I can plumb this into libxfs_destroy() but as you mentioned, it will
need changes in xfs_copy, xfs_db and xfs_repair because they track the
argv pointers, but we can just strdup(). Does that seem okay?

I don't think this is better.  libxfs' init functions do not create or
assign the libxfs_init.{data,log,rt}.name fields, they merely reuse whatever pointer the calling program set.  Only the caller knows where
that pointed to memory came from (stack, optarg, dynamic allocation) so
only the caller knows how to drop the pointer.

Quoted 3 lines

The original patch is more appropriate IMO but seeing as we're about to
return from main and terminate the process, why do we care?

I agree, I don't think there's any actual harm. Only that addrsan was complaining when running mkfs.xfs (see below) and it was just annoying to filter through a bunch of actual leaks that needed to be fixed.

```
==100203==ERROR: LeakSanitizer: detected memory leaks

Direct leak of 9 byte(s) in 1 object(s) allocated from:
    #0 0x7f6207ae8ac4 in strdup (/lib64/libasan.so.8+0xe8ac4) (BuildId:
5395ec74f54d9ec7bf97c06583dd39a96c230822)
    #1 0x0000004175ce in getstr /home/twilfred/wdc/xfs/xfsprogs-
dev/mkfs/xfs_mkfs.c:1767

SUMMARY: AddressSanitizer: 9 byte(s) leaked in 1 allocation(s).
```
Quoted 7 lines

And if we
do, shouldn't mkfs also free the other getstr() strings attached to the
cli struct?  And shouldn't the exit(1) paths do likewise? (To be clear, my answers are "I don't", "no", and "no" but I could be convinced.)

Okay, fair enough :)

Wilfred

#5 lore raw
In reply to Darrick J. Wong
Quoted 6 lines · Darrick J. Wong

I don't think this is better. libxfs' init functions do not create or assign the libxfs_init.{data,log,rt}.name fields, they merely reuse whatever pointer the calling program set. Only the caller knows where that pointed to memory came from (stack, optarg, dynamic allocation) so only the caller knows how to drop the pointer.

Exactly.

Quoted 4 lines

The original patch is more appropriate IMO but seeing as we're about to return from main and terminate the process, why do we care? And if we do, shouldn't mkfs also free the other getstr() strings attached to the cli struct?

We probably want that, as having a clean xfstests run with addrsan is actually really nice to have, as without that the addrsan support is only if limited use.

Quoted 1 line

And shouldn't the exit(1) paths do likewise?

Maybe. Although it doesn't really matter all that much.

#6 lore raw
In reply to Darrick J. Wong
Quoted 38 lines · Darrick J. Wong
Quoted 30 lines · Wilfred Mallawa
Quoted 21 lines · Carlos Maiolino
Quoted 14 lines

 mkfs/xfs_mkfs.c | 3 +++

 1 file changed, 3 insertions(+)
mkfs/xfs_mkfs.c +0 -0
index 334367b8..fec43987 100644
@@ -6503,5 +6503,8 @@ main(
  		exit(1);


  libxfs_destroy(&xi);

mkfs/xfs_mkfs.c +3 -0
+	free(cli.xi->data.name);
+	free(cli.xi->log.name);
+	free(cli.xi->rt.name);
Quoted 2 lines

  return 0;
 }

Hm, wouldn't perhaps be better to hook this into libxfs_destroy? Although this would likely need some other extra surgery on xfs_copy.c.

Quoted 3 lines

It just sounds weird to me to free the memory here 'after' we already called
libxfs_destroy() here.

Ah yeah.... I back-ported this off the xraid tree, where we have a bit more cleanup after libxfs_destroy() and I think it makes sense to do there but maybe not here...

Quoted 6 lines
Quoted 2 lines

IIUC: cli.xi == &xi in this place, so freeing up
memory after destroying the struct using it sounds like a time bomb.

I can plumb this into libxfs_destroy() but as you mentioned, it will need changes in xfs_copy, xfs_db and xfs_repair because they track the argv pointers, but we can just strdup(). Does that seem okay?

Quoted 5 lines

I don't think this is better. libxfs' init functions do not create or assign the libxfs_init.{data,log,rt}.name fields, they merely reuse whatever pointer the calling program set. Only the caller knows where that pointed to memory came from (stack, optarg, dynamic allocation) so only the caller knows how to drop the pointer.

Yeah, that's mostly why I thought we'd need to re-wire a bunch of stuff which is probably not important.

Quoted 8 lines

The original patch is more appropriate IMO but seeing as we're about to return from main and terminate the process, why do we care? And if we do, shouldn't mkfs also free the other getstr() strings attached to the cli struct? And shouldn't the exit(1) paths do likewise?

(To be clear, my answers are "I don't", "no", and "no" but I could be convinced.)

Leaving them as-is is fine for now, might not be fine in the future, I'd say if we are leaking them, we should fix it, even if it's falling to the end of the program.

Quoted 3 lines

--D

#7 lore raw

[snip]

Quoted 9 lines
Quoted 9 lines

The original patch is more appropriate IMO but seeing as we're about to
return from main and terminate the process, why do we care?  And if we
do, shouldn't mkfs also free the other getstr() strings attached to the
cli struct?  And shouldn't the exit(1) paths do likewise?

Quoted 9 lines
Quoted 3 lines

(To be clear, my answers are "I don't", "no", and "no" but I could be
convinced.)

Leaving them as-is is fine for now, might not be fine in the future, I'd
say if we are leaking them, we should fix it, even if it's falling to the end of the program.

Just to clarify, should I can spin up a V2 that frees all of the getstr() allocations in xfs_mkfs? or shall we drop this for now?

Quoted 1 line
#8 lore raw
Quoted 15 lines · Wilfred Mallawa

[snip]

Quoted 13 lines
Quoted 12 lines

The original patch is more appropriate IMO but seeing as we're about to
return from main and terminate the process, why do we care?  And if we
do, shouldn't mkfs also free the other getstr() strings attached to the
cli struct?  And shouldn't the exit(1) paths do likewise?

(To be clear, my answers are "I don't", "no", and "no" but I could be
convinced.)

Quoted 8 lines
Quoted 5 lines

Leaving them as-is is fine for now, might not be fine in the future, I'd
say if we are leaking them, we should fix it, even if it's falling to the end of the program.

Just to clarify, should I can spin up a V2 that frees all of the getstr() allocations in xfs_mkfs? or shall we drop this for now?

I'm happy getting rid of leaks even if they are just before we exit(). Final decision is from the maintainer thought. Andrey has been OOO past few days, so perhaps wait for him to come back and decide?!

Cheers.

#9 lore raw
Quoted 5 lines · Carlos Maiolino
Quoted 4 lines
Quoted 1 line

Just to clarify, should I can spin up a V2 that frees all of the getstr() allocations in xfs_mkfs? or shall we drop this for now?

Quoted 7 lines

I'm happy getting rid of leaks even if they are just before we exit().
Final decision is from the maintainer thought. Andrey has been OOO past
few days, so perhaps wait for him to come back and decide?!

Cheers.

Okay, sounds good!

#10 lore raw
Quoted 12 lines · Wilfred Mallawa
Quoted 11 lines · Carlos Maiolino
Quoted 4 lines
Quoted 1 line

Just to clarify, should I can spin up a V2 that frees all of the getstr() allocations in xfs_mkfs? or shall we drop this for now?

I'm happy getting rid of leaks even if they are just before we exit().
Final decision is from the maintainer thought. Andrey has been OOO past
few days, so perhaps wait for him to come back and decide?!

Quoted 3 lines
Quoted 1 line

Cheers.

Okay, sounds good!

Sound good to me, to have it freed.

Signature · 1 line
- Andrey