Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion SHA256SUMS
Original file line number Diff line number Diff line change
@@ -1 +1 @@
79780b2fba67b1aad6725f354fc273a03899edadba17348b2c9a6c66c30d47b8 php2shell-check.sh
4b075d97578adb661b2debf38f26a1cd685d624b1d1f2670c8aba34b621185c8 php2shell-check.sh
35 changes: 28 additions & 7 deletions php2shell-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -1374,16 +1374,33 @@ if command -v frankenphp >/dev/null 2>&1; then echo frankenphp; fi' 2>/dev/null
}

_p2s_check_stale_processes() {
local _p _exe _name _stale=''
[ -d /proc ] && [ "$(id -u)" -eq 0 ] || return 0
for _p in /proc/[0-9]*; do
_exe=$(readlink "$_p/exe" 2>/dev/null) || continue
local _p _exe _name _comm _state _rc _stale='' _unreadable=0
[ -d "$_p2s_proc" ] && [ "$(id -u)" -eq 0 ] || return 0
for _p in "$_p2s_proc"/[0-9]*; do
[ -d "$_p" ] || continue
if ! _exe=$(readlink "$_p/exe" 2>/dev/null); then
# Root inside a container can still lack access to workers owned
# by another uid. Do not turn an unreadable PHP process into ok.
_comm=$(cat "$_p/comm" 2>/dev/null | _p2s_clean 40)
case "$_comm" in
*php*|apache2|httpd)
_state=$(awk '/^State:/{print $2;exit}' "$_p/status" 2>/dev/null)
case "$_state" in Z|X|x) continue ;; esac
[ -d "$_p" ] && _unreadable=$(( _unreadable + 1 )) ;;
esac
continue
fi
case "$_exe" in
*php*' (deleted)')
_name=$(basename "${_exe% (deleted)}" | _p2s_clean 40) ;;
*apache2|*httpd)
grep -q 'libphp.*(deleted)' "$_p/maps" 2>/dev/null || continue
_name="$(basename "$_exe" | _p2s_clean 40) (mod_php)" ;;
grep -q 'libphp.*(deleted)' "$_p/maps" 2>/dev/null
_rc=$?
case "$_rc" in
0) _name="$(basename "$_exe" | _p2s_clean 40) (mod_php)" ;;
1) continue ;;
*) [ -d "$_p" ] && _unreadable=$(( _unreadable + 1 )); continue ;;
esac ;;
*) continue ;;
esac
case "$_stale" in *" $_name"*) ;; *) _stale="$_stale $_name" ;; esac
Expand All @@ -1392,7 +1409,11 @@ _p2s_check_stale_processes() {
if [ -n "$_stale" ]; then
_p2s_add warn process 'host' "still running the replaced PHP binary:$_stale" \
'PHP was updated on disk but not restarted: restart PHP-FPM and/or the web server'
else
fi
if [ "$_unreadable" -gt 0 ]; then
_p2s_add unknown process 'host' "cannot inspect $_unreadable visible PHP/Apache processes; their restart state is unknown" \
'Check process inspection permissions, or run inside the affected container with permission to read its workers'
elif [ -z "$_stale" ]; then
_p2s_add ok process 'host' 'no PHP process runs a replaced binary'
fi
}
Expand Down
80 changes: 80 additions & 0 deletions tests/cases/stale_processes.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# shellcheck shell=bash
# Sourced by tests/run_tests.sh. An effective uid of zero does not guarantee
# access to another user's /proc/<pid>/exe or maps in a container.
# shellcheck disable=SC2154 # _t_script and _t_tmp come from run_tests.sh

_sp_dir="${_t_tmp:?}/stale-processes"
mkdir -p "$_sp_dir"

_sp_process() { # case pid comm exe (empty means readlink fails)
local _p="$_sp_dir/$1/$2"
mkdir -p "$_p"
printf '%s\n' "$3" > "$_p/comm"
if [ -n "$4" ]; then
ln -s "$4" "$_p/exe"
else
# A regular file produces the same readlink failure even as root.
: > "$_p/exe"
fi
}

_sp_states() {
# shellcheck disable=SC2016 # Expand these variables in the child shell.
env P2S_SOURCE_ONLY=1 P2S_PROC_ROOT="$_sp_dir/$1" bash -c '
. "$1"
id() { if [ "${1:-}" = -u ]; then printf "0\n"; else command id "$@"; fi; }
_opt_json=1
_p2s_check_stale_processes
printf "%s\n" "${_f_status[@]}"
' _ "$_t_script"
}

_sp_expect() { # name case exact states
local _got
_got=$(_sp_states "$2")
if [ "$_got" = "$3" ]; then
_t_ok "$1"
else
_t_bad "$1 (expected '$3', got '$_got')"
fi
}

_sp_process current 101 php-fpm8.3 /usr/sbin/php-fpm8.3
_sp_expect 'current FPM process retains its ok finding' current ok

_sp_process deleted 102 php-fpm8.3 '/usr/sbin/php-fpm8.3 (deleted)'
_sp_expect 'deleted FPM executable retains its restart warning' deleted warn

_sp_process denied 103 php-fpm8.3 ''
_sp_expect 'unreadable FPM exe is unknown, never ok' denied unknown

_sp_process mixed 104 php-fpm8.3 '/usr/sbin/php-fpm8.3 (deleted)'
_sp_process mixed 105 php-fpm8.3 ''
_sp_expect 'stale and unreadable workers preserve both findings' mixed $'warn\nunknown'

_sp_process unrelated 106 sleep ''
_sp_expect 'unreadable unrelated process does not imply a PHP gap' unrelated ok

_sp_process apache-current 107 apache2 /usr/sbin/apache2
printf '%s\n' '00400000-00500000 r-xp 00000000 00:01 1 /usr/lib/apache2/modules/libphp8.3.so' \
> "$_sp_dir/apache-current/107/maps"
_sp_expect 'readable current Apache mapping retains ok' apache-current ok

_sp_process apache-deleted 108 apache2 /usr/sbin/apache2
printf '%s\n' '00400000-00500000 r-xp 00000000 00:01 1 /usr/lib/apache2/modules/libphp8.3.so (deleted)' \
> "$_sp_dir/apache-deleted/108/maps"
_sp_expect 'deleted Apache PHP mapping retains its restart warning' apache-deleted warn

_sp_process apache-denied 109 apache2 /usr/sbin/apache2
mkdir "$_sp_dir/apache-denied/109/maps"
_sp_expect 'Apache maps read failure is unknown, never ok' apache-denied unknown

_sp_process apache-exe-denied 110 httpd ''
_sp_expect 'unreadable httpd exe is unknown, never ok' apache-exe-denied unknown

mkdir -p "$_sp_dir/exited"
_sp_expect 'an already vanished process does not create a read failure' exited ok

_sp_process zombie 111 php-fpm8.3 ''
printf 'Name:\tphp-fpm8.3\nState:\tZ (zombie)\n' > "$_sp_dir/zombie/111/status"
_sp_expect 'a zombie PHP process has no running executable to inspect' zombie ok