fix(pkg/rootless): avoid memleak during init() contructor.

`argv[0]`, ie: the full buffer allocated by `get_cmd_line_args`,
was going to be freed only if `can_use_shortcut()` was called.
Instead, let `init()` always manage `argv0` lifecycle.

Signed-off-by: Federico Di Pierro <nierro92@gmail.com>
This commit is contained in:
Federico Di Pierro
2025-01-20 09:15:31 +01:00
parent 0f04ba87bb
commit 51fd6e906c

View File

@ -364,7 +364,6 @@ get_cmd_line_args (int *argc_out)
static bool static bool
can_use_shortcut (char **argv) can_use_shortcut (char **argv)
{ {
cleanup_free char *argv0 = NULL;
bool ret = true; bool ret = true;
int argc; int argc;
@ -372,8 +371,6 @@ can_use_shortcut (char **argv)
return false; return false;
#endif #endif
argv0 = argv[0];
if (strstr (argv[0], "podman") == NULL) if (strstr (argv[0], "podman") == NULL)
return false; return false;
@ -439,6 +436,7 @@ static void __attribute__((constructor)) init()
const char *listen_fds; const char *listen_fds;
const char *listen_fdnames; const char *listen_fdnames;
cleanup_free char **argv = NULL; cleanup_free char **argv = NULL;
cleanup_free char *argv0 = NULL;
cleanup_dir DIR *d = NULL; cleanup_dir DIR *d = NULL;
int argc; int argc;
@ -496,6 +494,8 @@ static void __attribute__((constructor)) init()
fprintf(stderr, "cannot retrieve cmd line"); fprintf(stderr, "cannot retrieve cmd line");
_exit (EXIT_FAILURE); _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) if (geteuid () != 0 || getenv ("_CONTAINERS_USERNS_CONFIGURED") == NULL)
do_preexec_hooks(argv, argc); do_preexec_hooks(argv, argc);