Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

kernel: fix memory leaks in lantiq #2678

Closed
wants to merge 1 commit into from

Conversation

draane
Copy link
Contributor

@draane draane commented Jan 6, 2020

Add calls to free for the variables buf_orig and buf.
Those two variables were never freed, causing possible
memory leaks.

@adschm adschm added kernel pull request/issue with Linux kernel related changes target/lantiq pull request/issue for lantiq target labels Jan 6, 2020
@neheb
Copy link
Contributor

neheb commented Jan 7, 2020

To simplify, you could use GCC's cleanup attribute: http://echorand.me/site/notes/articles/c_cleanup/cleanup_attribute_c.html

@draane
Copy link
Contributor Author

draane commented Jan 7, 2020

To simplify, you could use GCC's cleanup attribute: http://echorand.me/site/notes/articles/c_cleanup/cleanup_attribute_c.html

Oh that is nice, I didn't know about it. So, should I put a single free in the cleanup attribute and remove all the other ones?

@ynezz ynezz added the fix pull request contains fix for some issue label Jan 7, 2020
@ynezz
Copy link
Member

ynezz commented Jan 7, 2020

To simplify, you could use GCC's cleanup attribute

@neheb In kernel?

So, should I put a single free in the cleanup attribute

I would rather use common kernel goto idiom:

  if (!foo)
    goto free_buf;

  if (!bar)
    goto free_buf_orig;

  return 0;

free_buf_orig:
  free(buf_orig);
free_buf:
  free(buf);

  return -1;

@neheb
Copy link
Contributor

neheb commented Jan 8, 2020

To simplify, you could use GCC's cleanup attribute

@neheb In kernel?

This looks like a standalone utility.

@neheb
Copy link
Contributor

neheb commented Jan 8, 2020

To simplify, you could use GCC's cleanup attribute: http://echorand.me/site/notes/articles/c_cleanup/cleanup_attribute_c.html

Oh that is nice, I didn't know about it. So, should I put a single free in the cleanup attribute and remove all the other ones?

IIRC, you create a static function that takes a double pointer as an argument that just calls free. It makes all those if statements useless as GCC handles cleaning up automatically.

It's a way to do C++ destructors in C (similar but not quite the same). systemd does this all over the codebase.

@draane
Copy link
Contributor Author

draane commented Jan 11, 2020

@neheb I changed the file as you suggested, adding the cleanup attribute.
@ynezz are the changes ok for you?

@@ -83,8 +88,13 @@ int main(int argc, char **argv)
}

buf_orig = malloc(s.st_size);
if (!buf_orig) {
printf("Failed to alloc %d bytes\n", s.st_size);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That should be %zu I believe.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The original code used %d but I can change that.
Also should I keep the new if I created? Or just keep it as it was originally?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why not.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok 👍
I'm gonna change the string format

@adschm
Copy link
Member

adschm commented May 7, 2020

@neheb Can you have another look at this?

@neheb
Copy link
Contributor

neheb commented May 7, 2020

Seems fine.

@adschm
Copy link
Member

adschm commented May 7, 2020

@ynezz Would you review this again?

@adschm
Copy link
Member

adschm commented Jun 23, 2020

@ynezz Ping.

@neheb
Copy link
Contributor

neheb commented Sep 4, 2020

ping @hauke

Copy link
Contributor

@CodeFetch CodeFetch left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me.

@hauke @ynezz Is it okay?

@adschm
Copy link
Member

adschm commented Jun 6, 2021

Commit title prefix should be "ltq-vdsl-fw:" ...

@CodeFetch
Copy link
Contributor

@draane Ping...

Add `cleanup` attribute for the variables `buf_orig` and `buf`.
Those two variables were never freed, causing possible
memory leaks. Now gcc will automatically free the two
variables once they go out of scope.

Signed-off-by: Andrea Dalla Costa <andrea@dallacosta.me>
@draane
Copy link
Contributor Author

draane commented Jun 13, 2021

@adschm @CodeFetch I just changed the commit title

@tomjnixon
Copy link
Contributor

tomjnixon commented Jul 11, 2021

I could be wrong, but I think this patch introduces undefined behaviour. If main returns before the buffers are allocated, then free_buffer will be call free with an uninitialised pointer, which could point anywhere. Running this in valgrind shows the problem:

#include <stdlib.h>

void free_mem(void **mem) { free(*mem); }

int main(int argc, char **argv) {
  void *mem __attribute__((__cleanup__(free_mem)));

  return 0;
}

This is fixable by either initialising the pointers to NULL when they are declared, or declare and allocate on the same line.

Personally I don't think this is a memory leak, as the memory is freed when the program exits.

@mkresin
Copy link
Member

mkresin commented Nov 15, 2021

The author of the code seconds the last comment:

imho #2678 is a non-problem. the extractor runs once and all memory is freed anyway when the execution ends.

Unlikely that the code is required or will be ever merged. Closing here as we are talking about "possible memory leaks" without a proof.

@mkresin mkresin closed this Nov 15, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
fix pull request contains fix for some issue kernel pull request/issue with Linux kernel related changes needs reviewer target/lantiq pull request/issue for lantiq target
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

7 participants