Find processes on old code after a security update
Ubuntu 24.04 restarts PHP-FPM from the package, but processes outside services, or servers with auto-restart off, keep old code. Find them with needrestart.
USN-8948-1, the notice behind the PHP remote code execution fix, gives no restart instruction; it only says a standard system update makes the necessary changes. On a default Ubuntu 24.04 that is mostly true. Below, the evidence comes first, then how to use needrestart to find what still runs old files.
[1] See whether the package restarts the service itself. Fetch the php8.3-fpm package and extract its install script (postinst). When it is upgrading an installed version ($2 holds the previous version), it calls restart.
$ dpkg-deb -e php8.3-fpm_8.3.6-0ubuntu0.24.04.13_amd64.deb ctl$ grep -n invoke-rc.d ctl/postinst$ sed -n 91,100p ctl/postinst25: invoke-rc.d php8.3-fpm restart100: invoke-rc.d php8.3-fpm $_dh_action || exit 1# Automatically added by dh_installinit/13.14.1ubuntu5if [ "$1" = "configure" ] || [ "$1" = "abort-upgrade" ] || [ "$1" = "abort-deconfigure" ] || [ "$1" = "abort-remove" ] ; then if [ -z "${DPKG_ROOT:-}" ] && [ -x "/etc/init.d/php8.3-fpm" ]; then update-rc.d php8.3-fpm defaults >/dev/null if [ -n "$2" ]; then _dh_action=restart else _dh_action=start fi invoke-rc.d php8.3-fpm $_dh_action || exit 1[2] The second mechanism is needrestart's APT hook, which runs after every dpkg run with -m u (Ubuntu mode).
$ tail -1 /etc/apt/apt.conf.d/99needrestartDPkg::Post-Invoke {"test -x /usr/lib/needrestart/apt-pinvoke && /usr/lib/needrestart/apt-pinvoke -m u || true"; };According to the package changelog (/usr/share/doc/needrestart/changelog.Debian.gz), this mode was introduced as a specific mode for running as an APT hook: by default needrestart is quieter and restarts the services that need it. Version 3.6-7ubuntu4.1 added "disable it if restart mode has been explicitly set". So a server whose needrestart.conf sets $nrconf{restart} explicitly should not be expected to restart automatically.
[3] What can still be left: processes running outside a service, servers where the hook is off or a restart mode is set, and services needrestart defers. The test server had no PHP service, so the unpacked php8.3 was copied and started outside any service, then the copy was deleted, which reproduces the state a package replacement leaves. The deleted executable shows in the process's exe link.
$ ls -l /proc/1471007/exe$ rm /opt/lab/phpold$ ls -l /proc/1471007/exelrwxrwxrwx 1 root root 0 Oct 23 22:30 /proc/1471007/exe -> /opt/lab/phpoldlrwxrwxrwx 1 root root 0 Oct 23 22:30 /proc/1471007/exe -> /opt/lab/phpold (deleted)[4] Get a list in machine-readable form. -r l lists without restarting anything, and -b is batch mode, which prefixes each line with NEEDRESTART-…: (man page). This output was taken while the step 3 process was running.
# needrestart -r l -b 2>/dev/nullNEEDRESTART-VER: 3.6NEEDRESTART-KCUR: 6.8.0-147-genericNEEDRESTART-KEXP: 6.8.0-147-genericNEEDRESTART-KSTA: 1NEEDRESTART-UCSTA: 1NEEDRESTART-UCCUR: NEEDRESTART-UCEXP: NEEDRESTART-SVC: dbus.serviceNEEDRESTART-SVC: systemd-logind.serviceNEEDRESTART-SVC: unattended-upgrades.serviceNEEDRESTART-SESS: root @ session #9911NEEDRESTART-SESS: root @ user manager serviceFor a PHP check, look at two prefixes only: NEEDRESTART-SVC: for systemd services and NEEDRESTART-SESS: for user sessions. If PHP-FPM were left over, its unit would appear on an SVC line; the test server had no PHP service, so that was not observed. A process started outside a service appears under SESS, as above.
[5] The human-readable output also names the process and PID. -b gives only the session, so read -r l when you need the name.
# needrestart -r l 2>&1Running kernel seems to be up-to-date. The processor microcode seems to be up-to-date. Services to be restarted: Service restarts being deferred: /etc/needrestart/restart.d/dbus.service systemctl restart systemd-logind.service systemctl restart unattended-upgrades.service No containers need to be restarted. User sessions running outdated binaries: root @ session #9911: phpold[1471007] root @ user manager service: systemd[479387] No VM guests are running outdated hypervisor (qemu) binaries on this host.The phpold[1471007] line is the process running an old file. "Service restarts being deferred" lists services needrestart deliberately held back; if a PHP service is there, restart it yourself.
[6] Restart what is left. For PHP-FPM that is systemctl restart php8.3-fpm; for Apache mod_php it is systemctl restart apache2. Both drop in-flight requests, so pick a quiet hour and check the config syntax first (php-fpm8.3 -t, apachectl configtest); restarting with a broken config leaves the service down. To drop fewer requests, read systemctl cat to see what systemctl reload does before relying on it. The restart commands were not run in this test.
[7] Run -b again and confirm the line is gone. In the test, after the process ended, only one session line remained.
# needrestart -r l -b 2>/dev/null | grep SESSNEEDRESTART-SESS: root @ user manager serviceWhere checks go wrong
- The human-readable output of
needrestart -r lwent to standard error, not standard output. Add2>&1when piping it to a file. For-b,2>/dev/nullwas needed: this host printedUse of uninitialized value $ucode_vars{"CURRENT"} …warnings on standard error. - The
user manager serviceline (systemd[479387]) stayed in the list regardless of PHP, and was still there after the process ended. - A process started from the same SSH session as
needrestartdid not appear in this test; it did whenneedrestartran from another session.