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

libkmod and depmod: Release memory on error paths #228

Closed
wants to merge 4 commits into from

Conversation

stoeckmann
Copy link
Contributor

The libkmod library as well as depmod leaked memory on error paths related to kmod_lists:

  1. On kmod_list_append failure (out of memory), the soon to be list->data was not freed
  2. Not all modules were unreferenced in case of alias lookup failures

To make it a lot easier to review, I have added kmod_list_release which is a macro and abstracts the explicit while-loops from the code. I use it for case 2 to handle error cases more smoothly. This in turn made function kmod_list_remove_n_latest obsolete, so I removed it.

Proof of Concept (for case 2):

  1. Either compile kmod with address sanitizer (for leak detection) or use valgrind for modprobe call later on

  2. Create a modules config with an alias resolving to two modules (first one can be created in memory, second one is too long)

echo 'alias poc shortenough' > poc.conf
python -c 'print("alias poc " + 4096 * "A")' >> poc.conf
  1. Try to insert modules through alias
modprobe -aC poc.conf poc

The option -a is important to not trigger a FATAL log, which would abort the modprobe call without trying to release all resources. Since the leak occurs in library code, it should be fixed so allocated memory can be freed when requested.

libkmod/libkmod-config.c Show resolved Hide resolved
tools/depmod.c Outdated Show resolved Hide resolved
@lucasdemarchi
Copy link
Contributor

Sorry, we've got cconflicts and this now needs a rebase.

@stoeckmann
Copy link
Contributor Author

Sorry, we've got cconflicts and this now needs a rebase.

Rebased.

Copy link

codecov bot commented Nov 12, 2024

Codecov Report

Attention: Patch coverage is 38.09524% with 39 lines in your changes missing coverage. Please review.

Files with missing lines Patch % Lines
libkmod/libkmod.c 23.68% 22 Missing and 7 partials ⚠️
tools/depmod.c 44.44% 4 Missing and 1 partial ⚠️
libkmod/libkmod-config.c 70.00% 2 Missing and 1 partial ⚠️
libkmod/libkmod-module.c 66.66% 2 Missing ⚠️
Files with missing lines Coverage Δ
libkmod/libkmod-list.c 91.91% <ø> (ø)
testsuite/test-list.c 72.72% <ø> (ø)
libkmod/libkmod-module.c 53.56% <66.66%> (ø)
libkmod/libkmod-config.c 61.73% <70.00%> (ø)
tools/depmod.c 57.33% <44.44%> (ø)
libkmod/libkmod.c 49.75% <23.68%> (ø)

@evelikov
Copy link
Collaborator

@stoeckmann can we drop the libkmod: Introduce kmod_list_release commit? As mentioned before it is not required for addressing the memory leaks and is more importantly it's a fairly error prone construct.

The last commit uses it although we can live with an 2 extra lines of code.

libkmod/libkmod.c Outdated Show resolved Hide resolved
libkmod/libkmod.c Outdated Show resolved Hide resolved
libkmod/libkmod-internal.h Show resolved Hide resolved
libkmod/libkmod-config.c Show resolved Hide resolved
libkmod/libkmod-config.c Show resolved Hide resolved
@stoeckmann stoeckmann force-pushed the list_leak branch 2 times, most recently from 6d205c9 to 4355524 Compare November 13, 2024 17:28
libkmod/libkmod.c Show resolved Hide resolved
libkmod/libkmod.c Show resolved Hide resolved
Add a macro to reduce amount of explicit while-loops for removal of
nodes and release of their associated data in code base.

Signed-off-by: Tobias Stoeckmann <[email protected]>
If a list node could not be added, release already allocated data which
was supposed to end up in list.

Signed-off-by: Tobias Stoeckmann <[email protected]>
Check malloc return value and clean up resources if allocation fails.

Signed-off-by: Tobias Stoeckmann <[email protected]>
Do not override pointers to first list nodes if appending failed,
otherwise it's impossible to release already existing nodes of these
lists afterwards.

Remove the now unused function kmod_list_remove_n_latest as well.

Signed-off-by: Tobias Stoeckmann <[email protected]>
lucasdemarchi pushed a commit that referenced this pull request Nov 15, 2024
Add a macro to reduce amount of explicit while-loops for removal of
nodes and release of their associated data in code base.

Signed-off-by: Tobias Stoeckmann <[email protected]>
Link: #228
Signed-off-by: Lucas De Marchi <[email protected]>
lucasdemarchi pushed a commit that referenced this pull request Nov 15, 2024
If a list node could not be added, release already allocated data which
was supposed to end up in list.

Signed-off-by: Tobias Stoeckmann <[email protected]>
Link: #228
Signed-off-by: Lucas De Marchi <[email protected]>
lucasdemarchi pushed a commit that referenced this pull request Nov 15, 2024
Check malloc return value and clean up resources if allocation fails.

Signed-off-by: Tobias Stoeckmann <[email protected]>
Link: #228
Signed-off-by: Lucas De Marchi <[email protected]>
lucasdemarchi pushed a commit that referenced this pull request Nov 15, 2024
Do not override pointers to first list nodes if appending failed,
otherwise it's impossible to release already existing nodes of these
lists afterwards.

Remove the now unused function kmod_list_remove_n_latest as well.

Signed-off-by: Tobias Stoeckmann <[email protected]>
Link: #228
Signed-off-by: Lucas De Marchi <[email protected]>
@lucasdemarchi
Copy link
Contributor

Applied, thanks

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants