Repository navigation
Expand file tree
/
Copy pathphp2shell-check.sh
More file actions
executable file
·1982 lines (1872 loc) · 102 KB
/
Copy pathphp2shell-check.sh
File metadata and controls
executable file
·1982 lines (1872 loc) · 102 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/usr/bin/env bash
# php2shell-check — read-only exposure check for the "php2shell" PHP
# vulnerabilities CVE-2026-109618 and CVE-2026-109619 (GHSA-w8hw-w9fx-j79w).
#
# https://github.com/EXT-IT/php2shell-check
# Copyright (c) 2026 EXT IT GmbH. MIT License, see LICENSE.
# Provided as is, without warranty and without support.
#
# The script never changes anything and sends no exploit traffic. Local
# checks read package databases, PHP's own configuration and /proc; remote
# checks send one ordinary GET request per URL (following redirects only on
# the same host) and resolve its DNS name.
#
# Compatible with bash 3.2 (macOS) for --remote-only; local checks need Linux.
set -u
export LC_ALL=C
P2S_SCRIPT_VERSION='3.1.1'
P2S_DATA_DATE='2026-10-22'
P2S_ADVISORY='GHSA-w8hw-w9fx-j79w'
P2S_ADVISORY_URL='https://github.com/php/php-src/security/advisories/GHSA-w8hw-w9fx-j79w'
P2S_REPO_URL='https://github.com/EXT-IT/php2shell-check'
P2S_INFO_URL='https://ext-it.tech/php2shell/'
# ---------------------------------------------------------------------------
# Vulnerability data. Update these tables when new fixes are published.
# ---------------------------------------------------------------------------
# First fixed upstream release per branch (php.net, 2026-10-21).
# Backports for 7.4, 8.0 and 8.1 are announced but not released.
_p2s_upstream_fixed() {
case "$1" in
8.5) echo 8.5.12 ;;
8.4) echo 8.4.27 ;;
8.3) echo 8.3.36 ;;
8.2) echo 8.2.35 ;;
5.*|7.*|8.0|8.1) echo none ;;
*) echo unknown ;;
esac
}
# Distributions backport fixes and keep the old upstream number (Ubuntu 24.04
# still reports 8.3.6). For those, the package version decides.
# Key: <VERSION_CODENAME>:<source package>. Value: first fixed package
# version, or "unfixed" when the distribution has confirmed it is affected.
_p2s_distro_fixed() {
case "$1" in
jammy:php8.1) echo '8.1.2-1ubuntu2.28' ;; # Ubuntu 22.04
noble:php8.3) echo '8.3.6-0ubuntu0.24.04.13' ;; # Ubuntu 24.04
resolute:php8.5) echo '8.5.4-0ubuntu1.5' ;; # Ubuntu 26.04
stonking:php8.5) echo '8.5.9-0ubuntu2.1' ;; # Ubuntu 26.10
trixie:php8.4) echo '8.4.27-1~deb13u1' ;; # Debian 13, DSA-6574-1
bookworm:php8.2) echo '8.2.35-1~deb12u1' ;; # Debian 12, DLA-4848-1
# Ubuntu releases past standard support. These fixes ship through ESM,
# so they exist only on a host with Ubuntu Pro attached. Without it the
# package stays below the version named here and the verdict is vuln,
# which is the truthful answer for that host.
focal:php7.4) echo '7.4.3-4ubuntu2.29+esm5' ;; # 20.04 ESM
bionic:php7.2) echo '7.2.24-0ubuntu0.18.04.17+esm14' ;; # 18.04 ESM
xenial:php7.0) echo '7.0.33-0ubuntu0.16.04.16+esm22' ;; # 16.04 ESM
trusty:php5) echo '5.5.9+dfsg-1ubuntu4.29+esm18' ;; # 14.04 ESM
*) echo none ;;
esac
}
# Upstream numbers that distributions freeze. A remote banner showing one of
# these may belong to a patched backport build, so it cannot be judged.
_p2s_frozen_versions=' 5.5.9 7.0.33 7.2.24 7.4.3 7.4.33 8.1.2 8.3.6 8.5.4 8.5.9 '
# Cloudflare edge ranges, https://www.cloudflare.com/ips/ (2026-10-21).
_p2s_cf_v4='173.245.48.0/20 103.21.244.0/22 103.22.200.0/22 103.31.4.0/22
141.101.64.0/18 108.162.192.0/18 190.93.240.0/20 188.114.96.0/20
197.234.240.0/22 198.41.128.0/17 162.158.0.0/15 104.16.0.0/13
104.24.0.0/14 172.64.0.0/13 131.0.72.0/22'
_p2s_cf_v6='2400:cb00: 2606:4700: 2803:f800: 2405:b500: 2405:8100: 2a06:98c 2c0f:f248:'
# ---------------------------------------------------------------------------
# Argument parsing comes first: --help and --version touch nothing else.
# ---------------------------------------------------------------------------
_p2s_usage() {
cat <<'EOF'
Usage: php2shell-check.sh [options] [URL ...]
Checks a server and/or websites for exposure to the php2shell vulnerabilities
CVE-2026-109618 and CVE-2026-109619 (GHSA-w8hw-w9fx-j79w). Read-only.
Run it ON THE SERVER (as root) for a definite answer: there it reads the real
versions of every PHP installation and container. Add URLs to also check what
the sites reveal from outside and whether a WAF sits in front. A check from
outside alone can never confirm that a server is patched.
Options:
-r, --remote-only only check the given URLs (works without root, on macOS)
at most 10 URLs; redirects are followed only within the
host you named (and its www. variant)
--origin IP also test whether the sites answer directly on IP,
bypassing Cloudflare or another proxy
--no-containers skip Docker/Podman containers
--no-logs skip the memory-limit error count in logs
--json machine-readable output on stdout
--no-color plain output (also honours NO_COLOR)
-q, --quiet print the summary only
-h, --help show this help
-V, --version show version and data date
Exit codes: 0 nothing vulnerable found, 1 vulnerable, 2 could not decide,
3 usage error.
Use it only on systems you own or are authorised to check. Provided as is,
without warranty and without support; a clean result is no guarantee.
Examples:
sudo ./php2shell-check.sh
sudo ./php2shell-check.sh https://www.example.com
./php2shell-check.sh --remote-only https://www.example.com https://shop.example.com
EOF
}
_opt_remote_only=0
_opt_origin=''
_opt_containers=1
_opt_logs=1
_opt_json=0
_opt_color=1
_opt_quiet=0
_p2s_urls=()
while [ "$#" -gt 0 ]; do
case "$1" in
-h|--help) _p2s_usage; exit 0 ;;
-V|--version) echo "php2shell-check $P2S_SCRIPT_VERSION (vulnerability data as of $P2S_DATA_DATE)"; exit 0 ;;
-r|--remote-only) _opt_remote_only=1 ;;
--origin) shift; _opt_origin=${1:-}
[ -n "$_opt_origin" ] || { echo "php2shell-check: --origin needs an IP address" >&2; exit 3; } ;;
--origin=*) _opt_origin=${1#--origin=} ;;
--no-containers) _opt_containers=0 ;;
--no-logs) _opt_logs=0 ;;
--json) _opt_json=1 ;;
--no-color) _opt_color=0 ;;
-q|--quiet) _opt_quiet=1 ;;
--) shift; while [ "$#" -gt 0 ]; do _p2s_urls+=("$1"); shift; done; break ;;
-*) echo "php2shell-check: unknown option '$1' (see --help)" >&2; exit 3 ;;
*) _p2s_urls+=("$1") ;;
esac
shift
done
# This is a tool for checking your own sites, not a scanner: a handful of
# URLs per run, no address ranges.
if [ "${#_p2s_urls[@]}" -gt 10 ]; then
echo "php2shell-check: at most 10 URLs per run (this checks your own sites, it is not a scanner)" >&2
exit 3
fi
for _u in ${_p2s_urls[@]+"${_p2s_urls[@]}"}; do
if [[ "$_u" =~ ^[0-9a-fA-F.:]+/[0-9]+$ ]]; then
echo "php2shell-check: '$_u' looks like an address range; give the URLs of sites you operate" >&2
exit 3
fi
done
if [ "$_opt_remote_only" -eq 1 ] && [ "${#_p2s_urls[@]}" -eq 0 ]; then
echo "php2shell-check: --remote-only needs at least one URL" >&2
exit 3
fi
[ -n "${NO_COLOR:-}" ] && _opt_color=0
[ -t 1 ] || _opt_color=0
[ "$_opt_json" -eq 1 ] && _opt_color=0
# Test hooks: path prefix for every scanned file, and the proc filesystem to
# read process information from. Not for normal use.
_p2s_root=${P2S_TEST_ROOT:-}
_p2s_proc=${P2S_PROC_ROOT:-/proc}
# Set only while a binary that lives in another mount namespace is judged, by
# _p2s_check_ns_binary. _p2s_ns=1 points every lookup at that root's own data
# and forbids executing anything out of it; _p2s_ns_id is the dedupe identity
# and _p2s_ns_ver the version that was read without running the binary.
_p2s_ns=0
_p2s_ns_id=''
_p2s_ns_mnt=''
_p2s_ns_ver=''
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
if [ "$_opt_color" -eq 1 ]; then
_c_red=$'\033[1;31m'; _c_yel=$'\033[1;33m'; _c_grn=$'\033[1;32m'
_c_blu=$'\033[1;34m'; _c_dim=$'\033[2m'; _c_bold=$'\033[1m'; _c_off=$'\033[0m'
else
_c_red=''; _c_yel=''; _c_grn=''; _c_blu=''; _c_dim=''; _c_bold=''; _c_off=''
fi
# Anything read from a container, a web server or a file is untrusted and may
# carry terminal escape sequences. Keep a conservative character set only.
_p2s_clean() { tr -dc 'A-Za-z0-9_.,:;/@+~=()<>%# -' | head -c "${1:-200}"; }
_p2s_lower() { tr '[:upper:]' '[:lower:]'; }
_p2s_timeout() { # _p2s_timeout SECONDS cmd ...
local _s=$1; shift
if command -v timeout >/dev/null 2>&1; then timeout "$_s" "$@"
elif command -v gtimeout >/dev/null 2>&1; then gtimeout "$_s" "$@"
else "$@"
fi
}
# Numeric dotted-version compare. Prints -1, 0 or 1.
_p2s_vercmp() {
local _a=$1 _b=$2 _x _y
while [ -n "$_a" ] || [ -n "$_b" ]; do
_x=${_a%%.*}; _y=${_b%%.*}
[ "$_a" = "$_x" ] && _a='' || _a=${_a#*.}
[ "$_b" = "$_y" ] && _b='' || _b=${_b#*.}
_x=${_x:-0}; _y=${_y:-0}
if [ "$_x" -lt "$_y" ]; then echo -1; return; fi
if [ "$_x" -gt "$_y" ]; then echo 1; return; fi
done
echo 0
}
_p2s_ver_ge() { [ "$(_p2s_vercmp "$1" "$2")" != "-1" ]; }
# Compare two Debian package versions. dpkg is the authority where it exists;
# without it (a container judged from a non-Debian host, macOS) the fallback
# walks the version in digit and non-digit segments the way dpkg does, with
# "~" sorting before everything. Without this fallback a missing dpkg made
# every comparison fail, which reported a patched package as vulnerable.
_p2s_deb_ge() {
if command -v dpkg >/dev/null 2>&1; then
dpkg --compare-versions "$1" ge "$2"
return
fi
[ "$(_p2s_deb_cmp "$1" "$2")" != "-1" ]
}
_p2s_deb_cmp() {
awk -v a="$1" -v b="$2" '
function strip(s) { sub(/^[0-9]+:/, "", s); gsub(/~/, "\001", s); return s }
function tok(s, arr, n, i, j, L, c) {
n = 0; i = 1; L = length(s)
while (i <= L) {
c = substr(s, i, 1)
if (c >= "0" && c <= "9") {
j = i
while (j <= L && substr(s, j, 1) >= "0" && substr(s, j, 1) <= "9") j++
n++; arr[n] = "N"; arr[n "v"] = substr(s, i, j - i) + 0
} else {
j = i
while (j <= L && !(substr(s, j, 1) >= "0" && substr(s, j, 1) <= "9")) j++
n++; arr[n] = "S"; arr[n "v"] = substr(s, i, j - i)
}
i = j
}
return n
}
BEGIN {
na = tok(strip(a), ta); nb = tok(strip(b), tb)
m = (na > nb) ? na : nb
for (k = 1; k <= m; k++) {
# A version that continues with "~" is older than one that ends
# here: 8.4.27-1~deb13u1 comes before 8.4.27-1.
if (k > na) { print (substr(tb[k "v"], 1, 1) == "\001") ? 1 : -1; exit }
if (k > nb) { print (substr(ta[k "v"], 1, 1) == "\001") ? -1 : 1; exit }
if (ta[k] == "N" && tb[k] == "N") {
if (ta[k "v"] + 0 < tb[k "v"] + 0) { print -1; exit }
if (ta[k "v"] + 0 > tb[k "v"] + 0) { print 1; exit }
} else {
if (ta[k "v"] "" < tb[k "v"] "") { print -1; exit }
if (ta[k "v"] "" > tb[k "v"] "") { print 1; exit }
}
}
print 0
}'
}
_p2s_is_version() { [[ "$1" =~ ^[0-9]+\.[0-9]+\.[0-9]+$ ]]; }
# Size strings from php.ini ("64M", "1G", "-1") to bytes.
_p2s_bytes() {
local _v=$1 _n
case "$_v" in
-1|'') echo -1; return ;;
esac
_n=${_v%[KkMmGg]}
[[ "$_n" =~ ^[0-9]+$ ]] || { echo -1; return; }
case "$_v" in
*[Kk]) echo $(( _n * 1024 )) ;;
*[Mm]) echo $(( _n * 1024 * 1024 )) ;;
*[Gg]) echo $(( _n * 1024 * 1024 * 1024 )) ;;
*) echo "$_n" ;;
esac
}
_p2s_os_field() {
[ -r "$_p2s_root/etc/os-release" ] || return 0
# Parsed, never sourced: os-release sets VERSION and friends.
awk -F= -v k="$1" '$1==k{gsub(/"/,"",$2);print $2;exit}' "$_p2s_root/etc/os-release"
}
# ---------------------------------------------------------------------------
# Findings
# ---------------------------------------------------------------------------
_f_status=(); _f_check=(); _f_target=(); _f_detail=()
_p2s_hints_1=''; _p2s_hints_2=''; _p2s_hints_3=''
_p2s_section_open=''
_p2s_section() {
_p2s_section_open=$1
[ "$_opt_json" -eq 1 ] || [ "$_opt_quiet" -eq 1 ] && return 0
printf '\n%s%s%s\n' "$_c_bold" "$1" "$_c_off"
}
# _p2s_add STATUS CHECK TARGET DETAIL [HINT]
# STATUS: vuln | unknown | warn | ok | info
_p2s_add() {
local _st=$1 _ck=$2 _tg=$3 _dt=$4 _hn=${5:-} _tag _col
_f_status+=("$_st"); _f_check+=("$_ck"); _f_target+=("$_tg"); _f_detail+=("$_dt")
# Hints are collected by severity so the summary lists the urgent ones first.
if [ -n "$_hn" ]; then
case "$_st" in
vuln) _p2s_hints_1="$_p2s_hints_1|$_hn|" ;;
warn|unknown) _p2s_hints_2="$_p2s_hints_2|$_hn|" ;;
*) _p2s_hints_3="$_p2s_hints_3|$_hn|" ;;
esac
fi
[ "$_opt_json" -eq 1 ] || [ "$_opt_quiet" -eq 1 ] && return 0
case "$_st" in
vuln) _tag='VULN'; _col=$_c_red ;;
unknown) _tag=' ?? '; _col=$_c_yel ;;
warn) _tag='WARN'; _col=$_c_yel ;;
ok) _tag=' OK '; _col=$_c_grn ;;
*) _tag='INFO'; _col=$_c_blu ;;
esac
printf ' %s[%s]%s %-14s %s\n' "$_col" "$_tag" "$_c_off" "$_ck" "$_tg"
printf ' %s\n' "$_dt"
[ -n "$_hn" ] && printf ' %s-> %s%s\n' "$_c_dim" "$_hn" "$_c_off"
return 0
}
_p2s_json_esc() {
local _s=$1
_s=${_s//\\/\\\\}; _s=${_s//\"/\\\"}
printf '%s' "$_s"
}
# ---------------------------------------------------------------------------
# Verdicts
# ---------------------------------------------------------------------------
# Verdict for a plain upstream version (containers, own builds, Alpine, Remi).
# Prints "status|detail|hint".
_p2s_judge_upstream() {
local _v=$1 _branch _fixed
_branch=${_v%.*}
_fixed=$(_p2s_upstream_fixed "$_branch")
case "$_fixed" in
none)
echo "vuln|PHP $_v, branch $_branch has no fixed release yet|Upgrade to PHP 8.2.35, 8.3.36, 8.4.27 or 8.5.12; backports for 7.4 to 8.1 are announced but not out" ;;
unknown)
echo "unknown|PHP $_v, branch $_branch is not covered by the advisory data of $P2S_DATA_DATE|Compare with $P2S_ADVISORY_URL" ;;
*)
if _p2s_ver_ge "$_v" "$_fixed"; then
echo "ok|PHP $_v (fixed from $_fixed)|"
else
echo "vuln|PHP $_v, fixed in $_fixed|Update PHP to $_fixed or later (container: pull the new image and recreate)"
fi ;;
esac
}
# Verdict for a Debian/Ubuntu package from data that may come from the host
# or from inside a container: codename, source package, package version, the
# PHP version and a label for the report. Prints "status|detail|hint".
_p2s_judge_deb_data() {
local _cn=$1 _src=$2 _pv=$3 _v=$4 _pkg=$5 _up _fixed _dfixed
_up=${_pv#*:}; _up=${_up%%[-+~]*}
_fixed=$(_p2s_upstream_fixed "${_v%.*}")
_dfixed=$(_p2s_distro_fixed "$_cn:$_src")
if [ "$_fixed" != none ] && [ "$_fixed" != unknown ] && _p2s_is_version "$_up" && _p2s_ver_ge "$_up" "$_fixed"; then
echo "ok|package $_pkg $_pv (upstream $_up)|"
elif [ "$_dfixed" = unfixed ]; then
echo "vuln|package $_pkg $_pv, the distribution has not published a fix yet|Apply the temporary mitigations below and watch the distribution's security tracker"
elif [ "$_dfixed" != none ]; then
if _p2s_deb_ge "$_pv" "$_dfixed"; then
echo "ok|package $_pkg $_pv (distribution fix from $_dfixed)|"
else
echo "vuln|package $_pkg $_pv, fixed in $_dfixed|apt-get update && apt-get install --only-upgrade $_pkg, then restart PHP-FPM / the web server"
fi
else
echo "unknown|package $_pkg $_pv, no fix data for ${_cn:-this release}|Install security updates and compare with the distribution's advisory for CVE-2026-109618"
fi
}
# dpkg-query against the package database that belongs to the root under
# examination. --admindir lets this host's own dpkg-query read a container's
# /var/lib/dpkg: no exec into the container, no byte of it executed, and it
# works for every runtime because it is a file read. Judging a container by
# the HOST's database is the mistake v1.4.0 fixed for the Docker path, and the
# process fallback can make it just as easily.
_p2s_dpkg_query() {
if [ "$_p2s_ns" -eq 1 ] && [ -d "$_p2s_root/var/lib/dpkg" ]; then
dpkg-query --admindir="$_p2s_root/var/lib/dpkg" "$@"
else
dpkg-query "$@"
fi
}
# The same verdict for a package on this host.
_p2s_judge_deb() {
local _pkg=$1 _v=$2 _pv _src _cn
# shellcheck disable=SC2016 # dpkg format string, expanded by dpkg-query
_pv=$(_p2s_dpkg_query -W -f='${Version}' "$_pkg" 2>/dev/null | _p2s_clean 60)
# shellcheck disable=SC2016 # dpkg format string, expanded by dpkg-query
_src=$(_p2s_dpkg_query -W -f='${source:Package}' "$_pkg" 2>/dev/null | _p2s_clean 40)
_cn=$(_p2s_os_field VERSION_CODENAME)
_p2s_judge_deb_data "$_cn" "$_src" "$_pv" "$_v" "$_pkg"
}
# An RPM distribution backports the fix at an unchanged upstream number, like
# Debian does — but the deciding part is the release, and every rebuilder sets
# that scheme differently, so a version table cannot be kept correct. The
# changelog of the installed package can: it names the CVE.
#
# Measured on AlmaLinux 9.8 on 2026-10-22, all three with a changelog entry of
# 2026-10-21 naming both CVE IDs:
# php-cli-8.0.30-8.el9_8.alma.1 (branch 8.0, no upstream fix at all)
# php-cli-8.1.32-1.module_el9.8.0+326+528679fc.alma.1 (branch 8.1, likewise)
# php-cli-8.3.33-1.module_el9.8.0+328+1fdeb728.alma.1 (8.3.33, below the upstream 8.3.36)
# The reverse does not hold, which is why a missing entry decides nothing
# here: the 8.3.36 and 8.2.35 rebases carry the fix through their version and
# never mention a CVE. The plain RHEL build php-cli-8.0.30-8.el9_8 and every
# php-cli in Rocky 9.8 carried no such entry on that day.
_p2s_rpm_changelog_fixed() { # package name -> 0 when its changelog names a CVE
[ -n "$1" ] || return 1
_p2s_timeout 5 rpm -q --changelog "$1" 2>/dev/null \
| grep -qF -e 'CVE-2026-109618' -e 'CVE-2026-109619'
}
# Verdict for an RPM package. Prints "status|detail|hint".
_p2s_judge_rpm() {
local _file=$1 _v=$2 _pkg _name _vendor _fixed
_pkg=$(rpm -qf --qf '%{NAME}-%{VERSION}-%{RELEASE}' "$_file" 2>/dev/null | head -n1 | _p2s_clean 80)
_name=$(rpm -qf --qf '%{NAME}' "$_file" 2>/dev/null | head -n1 | _p2s_clean 60)
_vendor=$(rpm -qf --qf '%{VENDOR}' "$_file" 2>/dev/null | head -n1 | _p2s_clean 60)
_fixed=$(_p2s_upstream_fixed "${_v%.*}")
if [ "$_fixed" != none ] && [ "$_fixed" != unknown ] && _p2s_ver_ge "$_v" "$_fixed"; then
echo "ok|package $_pkg|"
elif _p2s_rpm_changelog_fixed "$_name"; then
echo "ok|package $_pkg, its changelog names the CVE: a backport at an unchanged upstream number|"
elif printf '%s' "$_vendor" | grep -qi remi; then
echo "vuln|package $_pkg (Remi ships upstream versions)|dnf upgrade 'php*' and restart PHP-FPM / the web server"
else
echo "unknown|package $_pkg ($_vendor), PHP $_v is below the upstream fix and the package changelog does not name the CVE|dnf upgrade, then compare with the vendor's advisory for CVE-2026-109618"
fi
}
_p2s_add_verdict() { # _p2s_add_verdict CHECK TARGET "status|detail|hint"
local _st _dt _hn _rest
_st=${3%%|*}; _rest=${3#*|}; _dt=${_rest%%|*}; _hn=${_rest#*|}
_p2s_add "$_st" "$1" "$2" "$_dt" "$_hn"
}
# ---------------------------------------------------------------------------
# Runtime configuration: the temporary measures from php.net, section
# "If you cannot update immediately" (https://www.php.net/archive/2026.php).
#
# Every judgement below follows that section and nothing else. What it asks
# for, and what this script can read:
# - memory_limit=-1, "preferably in a way applications can't override, such
# as php_admin_value in the FPM pool or Apache configuration", and a check
# "that no pool, directory or application setting puts a finite limit
# back". This is the first measure in the section.
# - a low post_max_size: 8M for applications that don't require file
# uploads, 32M if uploads are necessary, with upload_max_filesize kept
# below post_max_size.
# - session.upload_progress.enabled=0 and a restart of every worker, through
# php.ini, an FPM pool or server configuration that applies before request
# parsing -- not ini_set() and not an FPM .user.ini.
# - on PHP 8.5, fatal_error_backtraces=0 and a restart of every worker.
# - the request limits (decoded size, field count, multipart part count,
# header count, nesting depth) enforced at the web server or proxy level.
# The section names no value for max_input_nesting_level, so this script
# reports that setting as context and never as a missing measure.
# - no RLIMIT_AS or RLIMIT_DATA as a substitute for a container or cgroup
# limit, because "these can cause the vulnerable allocation failure".
#
# Settings the section does not name are not reported as its measures:
# expose_php (a disclosure question, its own line below, same as the remote
# check gives it), max_input_vars and pm.max_requests.
#
# And no result here is an "ok", because the section opens with: "while only
# an update is a complete solution, the following measures reduce exposure to
# specific attack paths but do not make an installation safe." Until 1.6.0
# this check answered "[OK] advisory mitigations in place", which said the
# opposite of its own source.
# ---------------------------------------------------------------------------
# The advisory's two absolute numbers for post_max_size, as bytes.
_P2S_POST_NO_UPLOADS=8M
_P2S_POST_UPLOADS=32M
# What php.ini says, kept for the pool and vhost checks further down: a pool
# or vhost that forces another value makes the php.ini line in this report
# untrue for that scope.
_p2s_ini_src=''
_p2s_ini_mem=''
_p2s_ini_post=''
_p2s_ini_upload=''
_p2s_ini_upprog=''
# shellcheck disable=SC2016 # PHP code, $k and $v belong to PHP
_P2S_INI_CODE='foreach (["memory_limit","post_max_size","upload_max_filesize","max_input_nesting_level","session.upload_progress.enabled","expose_php","fatal_error_backtraces"] as $k) { $v = ini_get($k); echo $k, "=", ($v === false ? "n/a" : $v), "\n"; }'
# _p2s_check_ini TARGET cmd... (cmd runs php with -r appended)
_p2s_check_ini() {
local _tg=$1 _out _k _v _mem='' _post='' _upl='' _expose='' _upprog='' \
_missing='' _note='' _mb _pb _ub _p8 _p32
shift
_out=$(_p2s_timeout 5 "$@" -r "$_P2S_INI_CODE" 2>/dev/null | head -n 20 | _p2s_clean_lines) || return 0
[ -n "$_out" ] || return 0
while IFS='=' read -r _k _v; do
# "=" is the only separator, so a value keeps any padding around it.
# Several judgements below compare it exactly ("-1", "On"), and a
# stray space would turn a satisfied measure into a missing one.
_k=${_k//[[:space:]]/}
_v=${_v//[[:space:]]/}
case "$_k" in
memory_limit) _mem=$_v ;;
post_max_size) _post=$_v ;;
upload_max_filesize) _upl=$_v ;;
expose_php) _expose=$_v ;;
session.upload_progress.enabled)
_upprog=$_v
case "$_v" in 1|On|on) _missing="$_missing session.upload_progress.enabled=0" ;; esac ;;
fatal_error_backtraces)
# ini_get answers false below PHP 8.5, which the probe prints
# as n/a. The advisory asks for this one on 8.5 only, so a
# host without the setting has nothing to set.
case "$_v" in 1|On|on) _missing="$_missing fatal_error_backtraces=0" ;; esac ;;
max_input_nesting_level)
if [[ "$_v" =~ ^[0-9]+$ ]]; then
_note="$_note max_input_nesting_level is $_v; the advisory asks for nesting depth to be limited at the web server or proxy level and names no value for this PHP setting."
fi ;;
esac
done <<EOF
$_out
EOF
# memory_limit=-1 is the section's first measure: it removes the
# controllable PHP memory limit failure. A finite value leaves it in
# place, so it is a missing measure. Until 1.6.0 this value was read and
# never judged, which is what let a badly configured host come out green.
_mb=$(_p2s_bytes "$_mem")
if [ "$_mem" != -1 ] && [ "$_mem" != n/a ] && [ -n "$_mem" ] && [ "$_mb" -ge 0 ]; then
_missing="$_missing memory_limit=-1 (now $_mem)"
fi
# post_max_size against the two numbers the advisory names. This script
# cannot know whether an installation needs file uploads, so it never
# picks one of the two for the reader: up to 8M satisfies the section
# either way, above 32M fails it either way, and in between the value
# holds only for an installation that does need uploads -- which is said
# in those words instead of being decided silently.
_pb=$(_p2s_bytes "$_post")
_p8=$(_p2s_bytes "$_P2S_POST_NO_UPLOADS")
_p32=$(_p2s_bytes "$_P2S_POST_UPLOADS")
case "$_post" in
0|-1)
# PHP reads 0 as "do not check the size at all".
_missing="$_missing a low post_max_size ($_P2S_POST_NO_UPLOADS without file uploads, $_P2S_POST_UPLOADS with; now $_post, no limit)" ;;
*)
if [ "$_pb" -gt "$_p32" ]; then
_missing="$_missing a low post_max_size ($_P2S_POST_NO_UPLOADS without file uploads, $_P2S_POST_UPLOADS with; now $_post)"
elif [ "$_pb" -gt "$_p8" ]; then
_note="$_note post_max_size is $_post: the advisory asks for $_P2S_POST_NO_UPLOADS where file uploads are not required and $_P2S_POST_UPLOADS where they are, so this value is low enough only if this installation needs uploads."
fi ;;
esac
# "Keep upload_max_filesize below post_max_size."
_ub=$(_p2s_bytes "$_upl")
if [ "$_ub" -gt 0 ] && [ "$_pb" -gt 0 ] && [ "$_ub" -ge "$_pb" ]; then
_missing="$_missing upload_max_filesize below post_max_size (now $_upl vs $_post)"
fi
# expose_php is not one of the advisory's measures. It decides whether the
# version is in the response headers, not whether the bug can be reached,
# so it gets the same kind of line the remote check gives it and must not
# decide the verdict below. Until 1.6.0 it did, and tipped hosts whose
# real measures were all in place.
case "$_expose" in
1|On|on)
_p2s_add warn disclosure "$_tg" 'expose_php is on, so the PHP version is in the response headers for everyone' \
'Set expose_php=0 so scanners cannot pick vulnerable targets by header' ;;
0|Off|off)
_p2s_add ok disclosure "$_tg" 'expose_php is off, the response headers do not reveal the PHP version' ;;
esac
if [ -n "$_missing" ]; then
_p2s_add warn mitigation "$_tg" "temporary measures from the advisory not set:$_missing.$_note" \
'Until patched: set memory_limit=-1 and session.upload_progress.enabled=0 where applications cannot override them (php_admin_value in the FPM pool or the Apache configuration), keep post_max_size low, and restart every worker. Only an update is a complete solution'
else
# Deliberately info, not ok: info is counted nowhere and changes no
# exit code, which is the point -- a mitigation is not a patch.
_p2s_add info mitigation "$_tg" "the temporary measures from the advisory that are readable here are set.$_note Only an update is a complete solution: these measures reduce exposure to specific attack paths but do not make an installation safe."
fi
# Remember php.ini for the pool and vhost checks. The FPM php.ini wins
# when several were probed: that is the one the pools build on.
case "$_tg" in
*'(fpm php.ini)'*) : ;;
*) [ -z "$_p2s_ini_src" ] || return 0 ;;
esac
_p2s_ini_src=$_tg
_p2s_ini_mem=$_mem
_p2s_ini_post=$_post
_p2s_ini_upload=$_upl
_p2s_ini_upprog=$_upprog
}
# php.ini is not the last word. An FPM pool, an Apache section or a
# fastcgi_param can force another value, and the advisory asks explicitly to
# check "that no pool, directory or application setting puts a finite limit
# back" -- that is also where it wants memory_limit=-1 to be set in the first
# place. Read line by line, without a configuration parser: no include is
# followed, no conditional section is evaluated, no precedence between files
# is computed. README says what that misses.
#
# Prints one "name=value" per forced setting.
_p2s_forced_settings() { # file kind(fpm|apache|nginx)
case "$2" in
fpm)
# php_admin_value[memory_limit] = -1
grep -E '^[[:space:]]*php_(admin_)?(value|flag)\[[A-Za-z0-9_.]+\][[:space:]]*=' "$1" 2>/dev/null \
| sed -E 's/^[[:space:]]*php_(admin_)?(value|flag)\[([A-Za-z0-9_.]+)\][[:space:]]*=[[:space:]]*/\3=/' ;;
apache)
# php_admin_value memory_limit -1
grep -E '^[[:space:]]*php_(admin_)?(value|flag)[[:space:]]+[A-Za-z0-9_.]+[[:space:]]+[^[:space:]]' "$1" 2>/dev/null \
| sed -E 's/^[[:space:]]*php_(admin_)?(value|flag)[[:space:]]+([A-Za-z0-9_.]+)[[:space:]]+"?([^"[:space:]]+)"?.*$/\3=\4/' ;;
nginx)
# fastcgi_param PHP_ADMIN_VALUE "memory_limit=-1";
# Only the single-line form. nginx can carry several settings in
# one newline-separated quoted string, and that shape is not
# readable without a parser.
grep -E 'fastcgi_param[[:space:]]+PHP_(ADMIN_)?VALUE' "$1" 2>/dev/null \
| grep -oE '[A-Za-z0-9_.]+[[:space:]]*=[[:space:]]*[^";[:space:]]+' \
| sed -E 's/[[:space:]]*=[[:space:]]*/=/' ;;
esac | head -n 40 | _p2s_clean_lines
}
# The section header for forced settings, opened only when there is one.
_p2s_forced_pending=''
_p2s_forced_open() {
[ -n "$_p2s_forced_pending" ] || return 0
_p2s_section "$_p2s_forced_pending"
_p2s_forced_pending=''
}
# Judge every setting one file forces, against the same advisory text as
# php.ini, and name the contradiction when php.ini says something else: the
# php.ini line in this report does not describe that scope.
_p2s_check_forced_file() { # file kind target
local _f=$1 _kind=$2 _tg=$3 _k _v _diff _b _p32
_p32=$(_p2s_bytes "$_P2S_POST_UPLOADS")
while IFS='=' read -r _k _v; do
_k=${_k//[[:space:]]/}
_v=${_v//[[:space:]]/}
[ -n "$_k" ] && [ -n "$_v" ] || continue
case "$_k" in
memory_limit)
_diff=''
if [ -n "$_p2s_ini_mem" ] && [ "$_p2s_ini_mem" != "$_v" ]; then
_diff=" php.ini reads memory_limit = $_p2s_ini_mem, so the php.ini line in this report does not describe this scope."
fi
if [ "$_v" = -1 ]; then
# This is where the advisory wants the setting to be, so
# it is not a warning even when php.ini disagrees.
_p2s_forced_open
_p2s_add info forced-ini "$_tg" "forces memory_limit = -1 where applications cannot override it, which is what the advisory asks for.$_diff"
else
_p2s_forced_open
_p2s_add warn forced-ini "$_tg" "forces memory_limit = $_v, a finite limit; the advisory asks to check that no pool, directory or application setting puts one back.$_diff" \
'Set memory_limit=-1 in the pool or vhost as well, or drop the line, then restart every worker'
fi ;;
post_max_size)
_b=$(_p2s_bytes "$_v")
if [ "$_v" = 0 ] || [ "$_v" = -1 ] || [ "$_b" -gt "$_p32" ]; then
_p2s_forced_open
_p2s_add warn forced-ini "$_tg" "forces post_max_size = $_v, above both values the advisory names ($_P2S_POST_NO_UPLOADS without file uploads, $_P2S_POST_UPLOADS with)" \
'Lower post_max_size in the pool or vhost as well, then restart every worker'
fi ;;
session.upload_progress.enabled)
case "$_v" in
1|On|on|true)
_diff=''
if [ -n "$_p2s_ini_upprog" ] && [ "$_p2s_ini_upprog" != "$_v" ]; then
_diff=" php.ini reads $_p2s_ini_upprog, so the php.ini line in this report does not describe this scope."
fi
_p2s_forced_open
_p2s_add warn forced-ini "$_tg" "forces session.upload_progress.enabled = $_v, the setting the advisory asks to turn off.$_diff" \
'Set session.upload_progress.enabled=0 in the pool or vhost as well, then restart every worker' ;;
esac ;;
esac
done <<EOF
$(_p2s_forced_settings "$_f" "$_kind")
EOF
}
_p2s_check_vhost_overrides() {
local _f
_p2s_forced_pending='PHP settings forced by the web server'
for _f in "$_p2s_root"/etc/apache2/apache2.conf "$_p2s_root"/etc/apache2/sites-enabled/*.conf \
"$_p2s_root"/etc/apache2/conf-enabled/*.conf "$_p2s_root"/etc/apache2/vhosts.d/*.conf \
"$_p2s_root"/etc/httpd/conf/httpd.conf "$_p2s_root"/etc/httpd/conf.d/*.conf; do
[ -f "$_f" ] && [ -r "$_f" ] || continue
_p2s_check_forced_file "$_f" apache "${_f#"$_p2s_root"}"
done
for _f in "$_p2s_root"/etc/nginx/nginx.conf "$_p2s_root"/etc/nginx/conf.d/*.conf \
"$_p2s_root"/etc/nginx/sites-enabled/*; do
[ -f "$_f" ] && [ -r "$_f" ] || continue
_p2s_check_forced_file "$_f" nginx "${_f#"$_p2s_root"}"
done
_p2s_forced_pending=''
}
# The advisory names RLIMIT_AS and RLIMIT_DATA as what NOT to use: it asks for
# "a container or cgroup limit as an availability boundary, preferably with a
# policy that kills only the affected worker" and adds "do not substitute
# RLIMIT_AS or RLIMIT_DATA, as these can cause the vulnerable allocation
# failure". So a finite one on a PHP process, or in the unit that starts it,
# is worth a warning whatever it was set for.
_p2s_check_rlimits() {
local _p _e _as _data _unit _hit _key _seen=' '
_p2s_forced_pending='Address-space limits on PHP'
# /proc, the same test hook the process fallback uses.
if [ -z "$_p2s_root" ] || [ -n "${P2S_PROC_ROOT:-}" ]; then
if [ -d "$_p2s_proc" ]; then
for _p in "$_p2s_proc"/[0-9]*; do
_e=$(readlink "$_p/exe" 2>/dev/null) || continue
_e=${_e% (deleted)}
case "$_e" in
*/php|*/php[0-9]*|*/php-fpm|*/php-fpm[0-9]*|*/lsphp|*/lsphp[0-9]* \
|*/php-cgi|*/php-cgi[0-9]*|*/cgi-bin/php*|*/frankenphp) ;;
*) continue ;;
esac
[ -r "$_p/limits" ] || continue
# Both names are three words wide, so the soft limit is field
# four. Spelled out instead of an awk interval expression:
# mawk 1.3.4 20200120 does not match those at all.
_as=$(awk '/^Max address space/ { print $4; exit }' "$_p/limits" 2>/dev/null)
_data=$(awk '/^Max data size/ { print $4; exit }' "$_p/limits" 2>/dev/null)
_hit=''
[[ "$_as" =~ ^[0-9]+$ ]] && _hit="$_hit RLIMIT_AS=$_as"
[[ "$_data" =~ ^[0-9]+$ ]] && _hit="$_hit RLIMIT_DATA=$_data"
[ -n "$_hit" ] || continue
# One line per binary and limit pair: an FPM pool has dozens
# of children that all carry the same limits.
_key=$(printf '%s%s' "$_e" "$_hit" | _p2s_clean 160 | tr -d ' ')
case "$_seen" in *" $_key "*) continue ;; esac
_seen="$_seen$_key "
_p2s_forced_open
_p2s_add warn rlimit "$(printf '%s' "$_e" | _p2s_clean 80)" \
"runs with a finite address-space limit ($_hit bytes); the advisory asks not to substitute RLIMIT_AS or RLIMIT_DATA for a container or cgroup limit, because these can cause the vulnerable allocation failure" \
'Drop the RLIMIT_AS / RLIMIT_DATA limit and bound memory with a container or cgroup limit that kills only the affected worker'
done
fi
fi
for _unit in "$_p2s_root"/etc/systemd/system/*php*.service "$_p2s_root"/etc/systemd/system/*php*.service.d/*.conf \
"$_p2s_root"/etc/systemd/system/*frankenphp*.service "$_p2s_root"/etc/systemd/system/*frankenphp*.service.d/*.conf \
"$_p2s_root"/lib/systemd/system/*php*.service "$_p2s_root"/lib/systemd/system/*frankenphp*.service \
"$_p2s_root"/usr/lib/systemd/system/*php*.service "$_p2s_root"/usr/lib/systemd/system/*frankenphp*.service; do
[ -f "$_unit" ] && [ -r "$_unit" ] || continue
_hit=$(grep -E '^[[:space:]]*Limit(AS|DATA)[[:space:]]*=' "$_unit" 2>/dev/null \
| grep -viE '=[[:space:]]*(infinity|unlimited)' | head -n 4 | tr '\n' ' ' | _p2s_clean 120)
[ -n "$_hit" ] || continue
_p2s_forced_open
_p2s_add warn rlimit "${_unit#"$_p2s_root"}" \
"sets a finite address-space limit ($_hit); the advisory asks not to substitute RLIMIT_AS or RLIMIT_DATA for a container or cgroup limit, because these can cause the vulnerable allocation failure" \
'Drop the RLIMIT_AS / RLIMIT_DATA limit and bound memory with a container or cgroup limit that kills only the affected worker'
done
_p2s_forced_pending=''
}
_p2s_clean_lines() { tr -dc 'A-Za-z0-9_.:/=<>() \n-' | head -c 1000; }
# ---------------------------------------------------------------------------
# Local checks
# ---------------------------------------------------------------------------
_p2s_seen=' '
_p2s_seen_pkg=' '
_p2s_runtime_ns=' '
_p2s_runtime_cmd=0
_p2s_ns_found=0
_p2s_php_version_of() { # binary -> x.y.z or empty
local _b=$1 _v
case "$_b" in
*frankenphp*)
# FrankenPHP links PHP into its own binary and answers with
# "FrankenPHP v1.13.2 PHP 8.5.12 Caddy v2.11.7 h1:...". The first
# version in that line is FrankenPHP's own, so anchor on "PHP ".
# "frankenphp php-cli -v" is no alternative: below PHP 8.6 it
# answers "this functionality is not available".
_v=$(_p2s_timeout 5 "$_b" version 2>/dev/null | head -n1 \
| grep -oE 'PHP [0-9]+\.[0-9]+\.[0-9]+' | head -n1)
_v=${_v#PHP } ;;
*php-fpm*|*lsphp*|*php-cgi*|*/cgi-bin/php*)
# php-cgi rejects -r ("option not found r") and prints a usage
# block instead of a version, so -v is the only probe these share.
_v=$(_p2s_timeout 5 "$_b" -v 2>/dev/null | head -n1 | grep -oE '[0-9]+\.[0-9]+\.[0-9]+' | head -n1) ;;
*) _v=$(_p2s_timeout 5 "$_b" -r 'echo PHP_VERSION;' 2>/dev/null | head -n1 | _p2s_clean 30) ;;
esac
_v=${_v%%[-+~]*}
_p2s_is_version "$_v" && echo "$_v"
}
# Judge one PHP binary. $2 is a note for the report (how it was found).
_p2s_check_one_binary() {
local _b=$1 _note=${2:-} _real _v='' _pkg _verdict _dir _sapi _qpath
[ -f "$_b" ] && [ -x "$_b" ] || return 1
if [ "$_p2s_ns" -eq 1 ]; then
# readlink -f would walk through the /proc/<pid>/root magic symlink and
# hand back a path read in the HOST namespace, which both collides with
# the host's own binaries in the dedupe list and would point the host
# package database at the wrong file. The mount namespace id plus the
# path inside it is the identity that is true for every worker of that
# container and for nothing else.
_real=$_p2s_ns_id
_v=$_p2s_ns_ver
else
_real=$(readlink -f "$_b" 2>/dev/null || echo "$_b")
fi
case "$_p2s_seen" in *" $_real "*) return 1 ;; esac
_p2s_seen="$_p2s_seen$_real "
[ -n "$_v" ] || _v=$(_p2s_php_version_of "$_b")
[ -n "$_v" ] || return 1
# No test-root guard here: a path that no package owns simply yields an
# empty answer, and that lets the tests put a dpkg-query stub in PATH and
# exercise the distribution path end to end instead of as a unit only.
_pkg=''
# A foreign root lists its files under the paths they have INSIDE that
# root, so the /proc/<pid>/root prefix has to come off before the search.
_qpath=$_real
[ "$_p2s_ns" -eq 1 ] && _qpath=${_b#"$_p2s_root"}
if command -v dpkg-query >/dev/null 2>&1; then
_pkg=$(_p2s_dpkg_query -S "$_qpath" 2>/dev/null | head -n1 | cut -d: -f1)
fi
if [ -n "$_pkg" ]; then
# Debian ships the same cgi build under two paths (/usr/bin/php-cgi8.4
# and /usr/lib/cgi-bin/php8.4). Same package, same version, same
# verdict — report it once instead of twice.
# The mount namespace belongs in the key. php8.3-cli 8.3.6 on the host
# and php8.3-cli 8.3.6 in a container are the same name at the same
# upstream number and a different patch state, so collapsing them
# would hide a vulnerable container behind a patched host, or the
# other way round.
case "$_p2s_seen_pkg" in *" $_p2s_ns_mnt$_pkg $_v "*) return 0 ;; esac
_p2s_seen_pkg="$_p2s_seen_pkg$_p2s_ns_mnt$_pkg $_v "
_verdict=$(_p2s_judge_deb "$_pkg" "$_v")
elif [ "$_p2s_ns" -eq 0 ] && command -v rpm >/dev/null 2>&1 && rpm -qf "$_real" >/dev/null 2>&1; then
_verdict=$(_p2s_judge_rpm "$_real" "$_v")
else
# An RPM root is deliberately not read through rpm --dbpath: the
# database format changed between EL releases (bdb, ndb, sqlite), so
# the host's rpm can fail on a container's database in a way that looks
# like "no package" instead of like an error. Without a package the
# upstream comparison decides, and a version only reaches this point
# when it came from the image environment, where it is an upstream
# number — see _p2s_ns_php_version.
_verdict=$(_p2s_judge_upstream "$_v")
fi
_p2s_add_verdict php-binary "${_b#"$_p2s_root"}$_note" "$_verdict"
# The ini probe below runs the binary with -r. Never do that with a binary
# out of a foreign root; _p2s_ns_php_version carries the reasoning. The
# runtime configuration of that root therefore stays unchecked, which is
# the same limit the README already states for FrankenPHP without a CLI.
[ "$_p2s_ns" -eq 1 ] && return 0
case "$_b" in
# None of these can run the ini probe: -r belongs to the CLI binary
# alone. On a Debian layout the CLI binary reads the fpm, apache2 and
# cgi php.ini below anyway; a FrankenPHP host without a php CLI keeps
# its runtime configuration unchecked, which README states.
*php-fpm*|*lsphp*|*php-cgi*|*/cgi-bin/php*|*frankenphp*) ;;
*)
_p2s_check_ini "${_b#"$_p2s_root"} (CLI)" "$_b"
# Debian layout: evaluate the FPM / Apache php.ini with this binary.
_dir="$_p2s_root/etc/php/${_v%.*}"
for _sapi in fpm apache2 cgi; do
[ -r "$_dir/$_sapi/php.ini" ] || continue
PHP_INI_SCAN_DIR="$_dir/$_sapi/conf.d" _p2s_check_ini \
"${_b#"$_p2s_root"} ($_sapi php.ini)" "$_b" -c "$_dir/$_sapi/php.ini"
done ;;
esac
return 0
}
# PHP paths taken from the running processes. A build in an unusual place
# (/opt/php-8.3, a vendor bundle) is invisible to the path list below, and
# reporting "no PHP found" on a host that is serving PHP right now is the
# worst mistake this script could make.
#
# The exe link of a containerised process names the path INSIDE that
# container, and that path does not exist in the host's namespace — so the old
# "[ -f $_e ]" filter threw away exactly the finds this fallback exists for.
# Measured on a Docker host on 2026-10-22: php:8.3.35-fpm running as pid
# 581758, [ -f /usr/local/sbin/php-fpm ] false, [ -f
# /proc/581758/root/usr/local/sbin/php-fpm ] true, and the whole run said "no
# PHP installation found" and exited 0. /proc/<pid>/root reaches every runtime
# that gives its payload its own mount namespace — rootless Docker of another
# user, containerd/nerdctl, Kubernetes, LXC/LXD, systemd-nspawn, Snap — with
# one path and without asking any of them.
#
# Prints one record per find: "kind|pid|mount-namespace|path". kind is "host"
# when the process shares our mount namespace and "ns" when it does not.
_p2s_running_php_paths() {
local _p _e _ns _self _kind _seen=' '
[ -z "$_p2s_root" ] || [ -n "${P2S_PROC_ROOT:-}" ] || return 0
[ -d "$_p2s_proc" ] || return 0
_self=$(readlink "$_p2s_proc/self/ns/mnt" 2>/dev/null | tr -dc '0-9a-z:[]')
for _p in "$_p2s_proc"/[0-9]*; do
_e=$(readlink "$_p/exe" 2>/dev/null) || continue
_e=${_e% (deleted)}
case "$_e" in
*/php|*/php[0-9]*|*/php-fpm|*/php-fpm[0-9]*|*/lsphp|*/lsphp[0-9]* \
|*/php-cgi|*/php-cgi[0-9]*|*/cgi-bin/php*|*/frankenphp) ;;
*) continue ;;
esac
# The namespace, not the file test, decides whose binary this is: an
# LXC container with /usr/bin/php8.3 on a host that also has
# /usr/bin/php8.3 would otherwise be judged by the host's package and
# then silently dropped by the dedupe list.
_ns=$(readlink "$_p/ns/mnt" 2>/dev/null | tr -dc '0-9a-z:[]')
if [ "$_ns" = "$_self" ]; then _kind=host; else _kind=ns; fi
# One line per (namespace, path): a busy FPM pool has dozens of
# workers on the same binary and they are one installation.
case "$_seen" in *" $_kind$_ns$_e "*) continue ;; esac
_seen="$_seen $_kind$_ns$_e "
printf '%s|%s|%s|%s\n' "$_kind" "${_p##*/}" "$_ns" "$_e"
done | sort -u
}
# The PHP version of a binary in another mount namespace, read from data and
# never by running it.
#
# Running it is the obvious shortcut and measurably tempting: on the Docker
# host of 2026-10-22 /proc/581758/root/usr/local/sbin/php-fpm -v did answer
# "PHP 8.3.35 (fpm-fcgi)". It is still the wrong thing to do here, for two
# reasons that each stand on their own.
#
# It would be foreign code as root. That binary comes out of an image somebody
# else built, and starting it from the host starts it OUTSIDE the container's
# namespaces, cgroups, seccomp filter and AppArmor profile, with full host
# privileges. A tool whose whole promise is "changes nothing, starts nothing"
# must not be the thing that lifts a container payload onto the host. For the
# host's own binaries the trade-off is a different one and the script keeps
# using -v there: those are already running as themselves.
#
# And the answer would not be trustworthy either. The ELF interpreter and
# every library path in that binary are absolute and resolve against the HOST
# root, not the container's: a musl image on a glibc host fails outright, and
# a glibc image of another vintage links against the host's libraries, so what
# it prints is not necessarily what the container runs.
#
# Three sources answer without execution, in this order. All three were
# measured against real containers on a Docker host on 2026-10-22, and the
# order matters more than it looks:
# 1. the container's own dpkg database, which the Debian/Ubuntu verdict needs
# anyway — a backported 8.3.6 can only be decided from the package
# version. Measured: dpkg-query --admindir on /proc/<pid>/root answers
# "php8.3-fpm 8.3.6-0ubuntu0.24.04.13" for an Ubuntu 24.04 container.
# 2. PHP_VERSION in the process environment. The official php, wordpress and
# FrankenPHP images set it, but only CLI-style processes still carry it:
# measured, php:8.3.35-cli has PHP_VERSION=8.3.35 in /proc/<pid>/environ
# while the php-fpm master has overwritten that memory with its process
# title ("master process (...)"). So this source misses exactly the SAPI
# that matters most, which is why 3 exists.
# 3. main/php_version.h of a source build, which those images ship with
# their headers. Measured: this is what answered for php:8.3.35-fpm
# (#define PHP_VERSION "8.3.35").
# Nothing found means the verdict says so, see _p2s_check_ns_binary.
_p2s_ns_php_version() { # process-dir root-prefix path-inside-that-root
local _pd=$1 _r=$2 _e=$3 _v='' _pkg _pv _h
if command -v dpkg-query >/dev/null 2>&1 && [ -r "$_r/var/lib/dpkg/status" ]; then
_pkg=$(_p2s_dpkg_query -S "$_e" 2>/dev/null | head -n1 | cut -d: -f1 | _p2s_clean 60)
if [ -n "$_pkg" ]; then
# shellcheck disable=SC2016 # dpkg format string, expanded by dpkg-query
_pv=$(_p2s_dpkg_query -W -f='${Version}' "$_pkg" 2>/dev/null | _p2s_clean 60)
_v=${_pv#*:}; _v=${_v%%[-+~]*}
fi
fi
if ! _p2s_is_version "$_v" && [ -r "$_pd/environ" ]; then
_v=$(tr '\0' '\n' < "$_pd/environ" 2>/dev/null \
| awk -F= '$1=="PHP_VERSION"{print $2;exit}' | _p2s_clean 30)
_v=${_v%%[-+~]*}
fi
if ! _p2s_is_version "$_v"; then
for _h in "$_r"/usr/local/include/php/main/php_version.h \
"$_r"/usr/include/php/main/php_version.h; do
[ -r "$_h" ] || continue
_v=$(grep -E '^#define PHP_VERSION ' "$_h" 2>/dev/null | head -n1 | tr -dc '0-9.')