From fa972e8681159a34ffc9c114e4fe3538a7f69046 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Mon, 19 Feb 2024 13:04:28 +0900 Subject: [PATCH] core/exec: do not crash with UtmpMode=user without User= setting Fixes https://bugzilla.redhat.com/show_bug.cgi?id=2264404. Replaces #31356. (cherry picked from commit d42b81f93f81e45f7a4053c6522ec3a2145ff136) (cherry picked from commit cba1060f8854fd9a11dac8e2b02126d2f3bb14ba) (cherry picked from commit 74ffb11efc851ecc291766abce8a1052746e715e) Conflict:add saved_uid Reference:https://github.com/systemd/systemd-stable/commit/fa972e8681159a34ffc9c114e4fe3538a7f69046 --- src/core/execute.c | 13 ++++++++++++- src/shared/utmp-wtmp.c | 1 + 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/src/core/execute.c b/src/core/execute.c index f6e2142..f4b4642 100644 --- a/src/core/execute.c +++ b/src/core/execute.c @@ -2962,6 +2962,7 @@ static int exec_child( #if HAVE_APPARMOR bool use_apparmor = false; #endif + uid_t saved_uid = getuid(); uid_t uid = UID_INVALID; gid_t gid = GID_INVALID; size_t n_fds; @@ -3252,6 +3253,16 @@ static int exec_child( } 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; @@ -3260,7 +3271,7 @@ static int exec_child( context->utmp_mode == EXEC_UTMP_INIT ? INIT_PROCESS : context->utmp_mode == EXEC_UTMP_LOGIN ? LOGIN_PROCESS : USER_PROCESS, - username); + username ?: username_alloc); } if (uid_is_valid(uid)) { diff --git a/src/shared/utmp-wtmp.c b/src/shared/utmp-wtmp.c index 646f449..4c1d479 100644 --- a/src/shared/utmp-wtmp.c +++ b/src/shared/utmp-wtmp.c @@ -202,6 +202,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); -- 2.33.0