core/exec: do not crash with UtmpMode=user without User= setting

Fixes https://bugzilla.redhat.com/show_bug.cgi?id=2264404.

Replaces #31356.
This commit is contained in:
Yu Watanabe 2024-02-19 13:04:28 +09:00 committed by Zbigniew Jędrzejewski-Szmek
parent 190ff0d0a8
commit d42b81f93f
2 changed files with 12 additions and 1 deletions

View file

@ -4451,6 +4451,16 @@ int exec_invoke(
#if ENABLE_UTMP
if (context->utmp_id) {
_cleanup_free_ char *username_alloc = NULL;
if (!username && context->utmp_mode == EXEC_UTMP_USER) {
username_alloc = uid_to_name(uid_is_valid(uid) ? uid : saved_uid);
if (!username_alloc) {
*exit_status = EXIT_USER;
return log_oom();
}
}
const char *line = context->tty_path ?
(path_startswith(context->tty_path, "/dev/") ?: context->tty_path) :
NULL;
@ -4459,7 +4469,7 @@ int exec_invoke(
context->utmp_mode == EXEC_UTMP_INIT ? INIT_PROCESS :
context->utmp_mode == EXEC_UTMP_LOGIN ? LOGIN_PROCESS :
USER_PROCESS,
username);
username ?: username_alloc);
}
#endif

View file

@ -179,6 +179,7 @@ int utmp_put_init_process(const char *id, pid_t pid, pid_t sid, const char *line
int r;
assert(id);
assert(ut_type != USER_PROCESS || user);
init_timestamp(&store, 0);