Fedora Account System
Red Hat Associate
Red Hat Customer
I would like to report a local privilege escalation in needrestart, as packaged in Debian bookworm (3.6-4+deb12u3) and Ubuntu 24.04 (3.6-7ubuntu4.5). Debian trixie (3.11-1) is partly affected: the session forgery works, and the packaged notify hook runs notify-send as root with attacker-chosen DISPLAY and text, but I did not get command execution there. needrestart decides which user session a process belongs to by matching the text of its cgroup path, and the match is not anchored. A local user can create a matching directory inside their own user cgroup and make their process look like root's session. The notify hook (400-notify-send) then uses that process's name, which the process sets itself. On bookworm and noble the name is put into an su -c shell string, so it runs as root. I confirmed command execution on the packaged builds in containers, run as root over the host's cgroups and systemd, on noble and bookworm. The trigger is any root apt or dpkg run with the stock hook. I tested with apt-get only, not unattended-upgrades. Upstream (Thomas Liske, CC'd) has reviewed the three-patch fix and agreed to the date below. The three patches are attached. They apply with offsets on needrestart 3.11. On 3.6 the second patch is rejected because of rewording, so bookworm and noble need a backport of that patch. I have not built or tested a 3.6 backport. * Patches ** Patch 1 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Armaan Sandhu <armaan.sandhu0504> Date: Fri, 23 Oct 2026 18:16:55 -0400 Subject: [PATCH v2 1/3] notify.d: pass the notification text to su through the environment The 400-notify-send hook built the su -c command string from the notification title and body. The body contains the process names that needrestart reports, and a process sets its own 15 byte comm name with prctl(PR_SET_NAME). A name that closes the quote and adds shell syntax was therefore executed by the shell that su starts, instead of being shown as text. The title and body are now stored in NR_NOTIFY_TITLE and NR_NOTIFY_BODY, exported, and expanded by the inner shell as quoted data, so the command string no longer contains any notification text. The existing MSGTITLE and MSGBODY variables are renamed instead of being copied into new ones, so there is no second copy of the text to keep in sync. Found and reported privately by Armaan Sandhu. #+begin_src diff --- Changes since v1: MSGTITLE and MSGBODY are renamed and exported directly instead of being copied into new variables. ex/notify.d/400-notify-send | 10 +++++++--- 1 file changed, 7 insertions(+), 3 deletions(-) diff --git a/ex/notify.d/400-notify-send b/ex/notify.d/400-notify-send index 800bc75..af5e6bc 100755 --- a/ex/notify.d/400-notify-send +++ b/ex/notify.d/400-notify-send @@ -48,11 +48,15 @@ case "$NR_SESSION" in echo "[$0] notify user $NR_USERNAME on $DISPLAY" 1>&2 - MSGTITLE=$(gettext 'Relogin or restarts required!') - MSGBODY=$(gettext 'Your session is running obsolete binaries or libraries as listed below. + NR_NOTIFY_TITLE=$(gettext 'Relogin or restarts required!') + NR_NOTIFY_BODY=$(gettext 'Your session is running obsolete binaries or libraries as listed below. <i><b>Please consider a relogin or restart of the affected processes!</b></i>')'\n'$(cat) - su -p -s /bin/sh -c "$NSEND -a needrestart -u critical -i dialog-warning \"$MSGTITLE\" \"$MSGBODY\"" "$NR_USERNAME" + # Pass the text to su through the environment: the inner shell expands it as + # data, so process names in the body are never parsed as shell code. + export NR_NOTIFY_TITLE NR_NOTIFY_BODY + + su -p -s /bin/sh -c "$NSEND -a needrestart -u critical -i dialog-warning \"\$NR_NOTIFY_TITLE\" \"\$NR_NOTIFY_BODY\"" "$NR_USERNAME" ;; *) echo "[$0] skip session '$NR_SESSION'" 1>&2 -- 2.56.0 #+end_src ** Patch 2 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Armaan Sandhu <armaan.sandhu0504> Date: Fri, 23 Oct 2026 18:16:55 -0400 Subject: [PATCH v2 2/3] needrestart: anchor the systemd cgroup matches needrestart files a process under a user session, a user manager or a user service by matching the path in /proc/PID/cgroup. Those matches were not anchored at the start, so a directory with the right name created inside a user's own delegated user cgroup could give a process a different uid. A process started by one user could be filed under root's session, and the notification hook then ran su as root with that process name. The session, user manager and user service matches now only accept paths that begin with systemd's slice components before the user-N.slice, user or session scope. Paths that match the systemd layout are classified as before, and paths that only contain the same text inside a user-owned subtree no longer match. This was tested against systemd layouts only. Hosts whose cgroup paths have a different prefix, such as some container setups, may classify those processes differently. Found and reported privately by Armaan Sandhu. #+begin_src diff --- Changes since v1: none. needrestart | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/needrestart b/needrestart index 7fb1adf..bfb3348 100755 --- a/needrestart +++ b/needrestart @@ -723,24 +723,24 @@ if(defined($opt_l)) { next; } # `systemd --user` manager - if($cgroup =~ m@/user\@(\d+)\.service/init\.scope$@ && $ptable->{$pid}->{fname} ne "(sd-pam)") { + if($cgroup =~ m@^(?:/[^/]+\.slice)*/user\@(\d+)\.service/init\.scope$@ && $ptable->{$pid}->{fname} ne "(sd-pam)") { print STDERR "$LOGPREF #$pid is user systemd manager: uid=$1\n" if($nrconf{verbosity} > 1); # TODO: restart specific `systemd --user` instance? $restart{q(systemd-user)}++; next; } - if($cgroup =~ m@/user-(\d+)\.slice/session-(\d+)\.scope@) { + if($cgroup =~ m@^(?:/[^/]+\.slice)*/user-(\d+)\.slice/session-(\d+)\.scope@) { print STDERR "$LOGPREF #$pid part of user session: uid=$1 sess=$2\n" if($nrconf{verbosity} > 1); push(@{ $sessions{$1}->{"session #$2"}->{ $ptable->{$pid}->{fname} } }, $pid); next; } - if($cgroup =~ m@/user\@(\d+)\.service(/.+\.slice)*/([^/]+\.service)$@) { + if($cgroup =~ m@^(?:/[^/]+\.slice)*/user\@(\d+)\.service(/.+\.slice)*/([^/]+\.service)$@) { print STDERR "$LOGPREF #$pid part of user service: uid=$1 unit=$3\n" if($nrconf{verbosity} > 1); push(@{ $sessions{$1}->{'user service'}->{ $3 } }, $pid); next; } - if($cgroup =~ m@/user\@(\d+)\.service@) { + if($cgroup =~ m@^(?:/[^/]+\.slice)*/user\@(\d+)\.service@) { print STDERR "$LOGPREF #$pid part of user manager: uid=$1\n" if($nrconf{verbosity} > 1); push(@{ $sessions{$1}->{'user manager'}->{ $ptable->{$pid}->{fname} } }, $pid); next; -- 2.56.0 #+end_src ** Patch 3 From 0000000000000000000000000000000000000000 Mon Sep 17 00:00:00 2001 From: Armaan Sandhu <armaan.sandhu0504> Date: Fri, 23 Oct 2026 18:16:55 -0400 Subject: [PATCH v2 3/3] needrestart: replace non-ASCII and control characters in process names Process names come from /proc and can contain escape sequences and newlines. They were printed to the terminal unchanged, which allowed terminal escape injection in the process list shown to the administrator. A newline in a name could also split a line of the machine readable NEEDRESTART-PID output. Only printable ASCII characters (0x20 to 0x7e) are now kept in the displayed copies and in the text passed to notification hooks. Every other byte is replaced with ?, so control characters and the bytes of UTF-8 encoded names are covered by one simple rule. A name with non-ASCII letters therefore shows one ? per byte. That is a deliberate trade-off: the rule is easy to read and cannot miss an escape that is encoded as UTF-8. The stored process names are not changed. Found and reported privately by Armaan Sandhu. #+begin_src diff --- Changes since v1: the control character ranges are replaced by a whitelist of printable ASCII, as suggested in review. needrestart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/needrestart b/needrestart index bfb3348..7d644b3 100755 --- a/needrestart +++ b/needrestart @@ -879,7 +879,7 @@ if(defined($opt_l)) { if($uid && scalar %stage2) { my %fnames; foreach my $pid (keys %stage2) { - push(@{$fnames{ $ptable->{$pid}->{fname} }}, $pid); + push(@{$fnames{ $ptable->{$pid}->{fname} =~ s/[^\x20-\x7e]/?/gr }}, $pid); } if($opt_b) { @@ -1372,7 +1372,7 @@ if(defined($opt_l) && !$uid) { unless($opt_p || $opt_b) { foreach my $uid (sort { ncmp(uid2name($a), uid2name($b)); } keys %sessions) { foreach my $sess (sort keys %{ $sessions{$uid} }) { - my $fnames = join(', ',map { $_.'['.join(',', @{ $sessions{$uid}->{$sess}->{$_} }).']'; } nsort keys %{ $sessions{$uid}->{$sess} }); + my $fnames = join(', ',map { (s/[^\x20-\x7e]/?/gr).'['.join(',', @{ $sessions{$uid}->{$sess}->{$_} }).']'; } nsort keys %{ $sessions{$uid}->{$sess} }); $ui->notice(' '.uid2name($uid)." @ $sess: $fnames") unless($opt_m eq 'e'); if($nrconf{sendnotify}) { local %ENV; -- 2.56.0 #+end_src