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

Avoid memory leak if --args is specified multiple times #426

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions bubblewrap.c
Original file line number Diff line number Diff line change
Expand Up @@ -1494,6 +1494,12 @@ parse_args_recurse (int *argcp,
if (argv[1][0] == 0 || endptr[0] != 0 || the_fd < 0)
die ("Invalid fd: %s", argv[1]);

/* Specifying --args multiple times doesn't work; this just pacifies
Copy link
Collaborator

Choose a reason for hiding this comment

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

The man page says it does, and that does seem to be true:

$ rmdir /tmp/a /tmp/b
$ printf '--dir\0/tmp/a\0' > a
$ printf '--dir\0/tmp/b\0' > b
$ bwrap --dev-bind / / --args 3 --args 4 ls /tmp 3<a 4<b
(output contains a and b)

So I think opt_args_data might need to become a linked list, or some other mechanism that keeps all of the arguments blobs referenced so that static analyzers know they are OK to "leak" once per process.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Eeek. This is why we have code review! It looks like at least flatpak (the main user of --args) is only appending it once, and for some reason I convinced myself the code was only designed to run once, but you're clearly right.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

#428
and then will rework this to be a linked list or strv after that lands.

* a static analyzer which pointed out the memory leak
*/
if (opt_args_data != NULL)
free (opt_args_data);

/* opt_args_data is essentially a recursive argv array, which we must
* keep allocated until exit time, since its argv entries get used
* by the other cases in parse_args_recurse() when we recurse. */
Expand Down