From: Yu Watanabe Date: Mon, 19 Feb 2024 04:04:28 +0000 (+0900) Subject: core/exec: do not crash with UtmpMode=user without User= setting X-Git-Tag: v256-rc1~821 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=d42b81f93f81e45f7a4053c6522ec3a2145ff136;p=thirdparty%2Fsystemd.git core/exec: do not crash with UtmpMode=user without User= setting Fixes https://bugzilla.redhat.com/show_bug.cgi?id=2264404. Replaces #31356. --- diff --git a/src/core/exec-invoke.c b/src/core/exec-invoke.c index c62b7385cb3..c012a359a0b 100644 --- a/src/core/exec-invoke.c +++ b/src/core/exec-invoke.c @@ -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 diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c index 6c3238a9c6a..267b350276c 100644 --- a/src/shared/utmp-wtmp.c +++ b/src/shared/utmp-wtmp.c @@ -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);