user-util: return malloc'ed strings in get_user_creds

get_user_creds would use getpwnam() and then returns strings pointing
into the static buffer. This seems very iffy. Let's duplicate the
strings properly
This commit is contained in:
Zbigniew Jędrzejewski-Szmek
2026-05-21 18:02:05 +02:00
parent 3ecd6b69cf
commit c83ff0e58d
21 changed files with 176 additions and 203 deletions
+5 -6
View File
@@ -2581,19 +2581,18 @@ static int start_transient_scope(sd_bus *bus) {
}
if (arg_exec_user) {
const char *un = arg_exec_user, *home, *shell;
_cleanup_free_ char *user = NULL, *home = NULL, *shell = NULL;
uid_t uid;
gid_t gid;
r = get_user_creds(&un, &uid, &gid, &home, &shell,
USER_CREDS_CLEAN|USER_CREDS_SUPPRESS_PLACEHOLDER|USER_CREDS_PREFER_NSS);
r = get_user_creds(arg_exec_user,
USER_CREDS_CLEAN|USER_CREDS_SUPPRESS_PLACEHOLDER|USER_CREDS_PREFER_NSS,
&user, &uid, &gid, &home, &shell);
if (r < 0)
return log_error_errno(r, "Failed to resolve user '%s': %s",
arg_exec_user, STRERROR_USER(r));
r = free_and_strdup_warn(&arg_exec_user, un);
if (r < 0)
return r;
free_and_replace(arg_exec_user, user);
if (home) {
r = strv_extendf(&user_env, "HOME=%s", home);