Skip to content

Commit

Permalink
fix(pkg/rootless): avoid memleak during init() contructor.
Browse files Browse the repository at this point in the history
`argv[0]`, ie: the full buffer allocated by `get_cmd_line_args`,
was going to be freed only if `can_use_shortcut()` was called.
Since that is not always the case, add a fallback cleanup method.

Signed-off-by: Federico Di Pierro <[email protected]>
  • Loading branch information
FedeDP committed Jan 20, 2025
1 parent 0f04ba8 commit 8306053
Showing 1 changed file with 5 additions and 0 deletions.
5 changes: 5 additions & 0 deletions pkg/rootless/rootless_linux.c
Original file line number Diff line number Diff line change
Expand Up @@ -439,6 +439,7 @@ static void __attribute__((constructor)) init()
const char *listen_fds;
const char *listen_fdnames;
cleanup_free char **argv = NULL;
cleanup_free char *argv0 = NULL;
cleanup_dir DIR *d = NULL;
int argc;

Expand Down Expand Up @@ -496,6 +497,8 @@ static void __attribute__((constructor)) init()
fprintf(stderr, "cannot retrieve cmd line");
_exit (EXIT_FAILURE);
}
// Even if unused, this is needed to ensure we properly free the memory
argv0 = argv[0];

if (geteuid () != 0 || getenv ("_CONTAINERS_USERNS_CONFIGURED") == NULL)
do_preexec_hooks(argv, argc);
Expand Down Expand Up @@ -525,6 +528,8 @@ static void __attribute__((constructor)) init()
xdg_runtime_dir = getenv ("XDG_RUNTIME_DIR");
if (geteuid () != 0 && xdg_runtime_dir && xdg_runtime_dir[0] && can_use_shortcut (argv))
{
// It was freed by can_use_shortcut() call
argv0 = NULL;
cleanup_free char *cwd = NULL;
cleanup_close int userns_fd = -1;
cleanup_close int mntns_fd = -1;
Expand Down

0 comments on commit 8306053

Please sign in to comment.