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

Conversation

cgwalters
Copy link
Collaborator

Found by a static analyzer.

bubblewrap-0.4.1/bubblewrap.c:1500: overwrite_var: Overwriting "opt_args_data" in "opt_args_data = load_file_data(the_fd, &data_len)" leaks the storage that "opt_args_data" points to.
 # 1498|              * keep allocated until exit time, since its argv entries get used
 # 1499|              * by the other cases in parse_args_recurse() when we recurse. */
 # 1500|->           opt_args_data = load_file_data (the_fd, &data_len);
 # 1501|             if (opt_args_data == NULL)
 # 1502|               die_with_error ("Can't read --args data");

Found by a static analyzer.

```
bubblewrap-0.4.1/bubblewrap.c:1500: overwrite_var: Overwriting "opt_args_data" in "opt_args_data = load_file_data(the_fd, &data_len)" leaks the storage that "opt_args_data" points to.
 # 1498|              * keep allocated until exit time, since its argv entries get used
 # 1499|              * by the other cases in parse_args_recurse() when we recurse. */
 # 1500|->           opt_args_data = load_file_data (the_fd, &data_len);
 # 1501|             if (opt_args_data == NULL)
 # 1502|               die_with_error ("Can't read --args data");
```
@@ -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.

cgwalters added a commit to cgwalters/bubblewrap that referenced this pull request Jun 10, 2021
The code supports this today, so test it.
xref containers#426
where I thought it didn't.
cgwalters added a commit to cgwalters/bubblewrap that referenced this pull request Jun 10, 2021
The code supports this today, so test it.
xref containers#426
where I thought it didn't.
@cgwalters cgwalters marked this pull request as draft June 10, 2021 17:27
brandsimon pushed a commit to brandsimon/bubblewrap that referenced this pull request Mar 19, 2023
The code supports this today, so test it.
xref containers#426
where I thought it didn't.
CrackerCat pushed a commit to CrackerCat/bubblewrap that referenced this pull request Jul 31, 2024
The code supports this today, so test it.
xref containers#426
where I thought it didn't.
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.

2 participants