Repository navigation
Expand file tree
/
Copy pathnode_runtime_audit.py
More file actions
1365 lines (1221 loc) · 60.2 KB
/
Copy pathnode_runtime_audit.py
File metadata and controls
1365 lines (1221 loc) · 60.2 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 python3
"""node-runtime-audit: find every Node.js a Linux host really runs and check
it against a Node.js security release.
It reads only these:
1. /proc: the real binary of each running process (/proc/<pid>/exe), read
even when it was deleted or lives in a container, and its version
2. `pm2 jlist`: live entries with interpreter, script and PATH pinned to
one Node install, and the Node processes running under each entry
3. `crontab -l` and systemd unit files: absolute Node paths
4. `docker exec <id> node --version` (and podman) in running containers
Node binaries found in pm2 entries, crontabs and units are not read: their
version comes from a running process that uses the same file, or from the
path as a hint (an unfixed version in the path counts as vulnerable, a fixed
one stays unknown).
It only reads. It never restarts, edits or upgrades anything, and it does not
run the binaries it finds unless you pass --exec-version. Exit code: 1 if
anything is vulnerable, else 2 if anything is unknown, else 0. Unknown is never
reported as patched.
Standard library only, Python 3.8+. MIT licensed.
"""
import argparse
import errno
import glob
import json
import os
import pwd
import re
import shlex
import shutil
import stat
import subprocess
import sys
RELEASE = "Node.js security release of 2026-10-21"
FIXED_DEFAULT = "22=22.23.4,24=24.22.1,26=26.12.1"
NODE_EXE_RE = re.compile(r"^(node|nodejs)(\d+)?$")
NODE_TOOL_NAMES = {"node", "nodejs", "npm", "npx", "corepack"}
VERSION_RE = re.compile(r"^v?(\d+)\.(\d+)\.(\d+)(?:[-+]([0-9A-Za-z.\-]+))?$")
# process.release.headersUrl is compiled into official builds:
# https://nodejs.org/download/release/v24.22.1/node-v24.22.1-headers.tar.gz
EMBEDDED_RE = re.compile(rb"node-v(\d+\.\d+\.\d+(?:-[0-9A-Za-z.]+)?)-headers\.tar\.gz")
# A path component that names one install: nvm, fnm, n, volta, asdf, mise,
# nodenv (".../v24.14.0/..." or ".../24.14.0/...") or a tarball dir
# (".../node-v24.14.0-linux-x64/...").
PINNED_RE = re.compile(r"(?:^|/)(?:node-)?v?(\d+\.\d+\.\d+)(?:-[a-z0-9_-]+)?(?:/|$)")
ABS_PATH_RE = re.compile(r"/[A-Za-z0-9._@+%~/-]+")
FNM_MULTISHELL_RE = re.compile(r"/fnm_multishells/")
# C0 controls, DEL and C1 controls. Process names, command lines, file names
# and container names come from other users and containers; printed raw they
# could move the cursor, rewrite the screen or set the terminal title.
CONTROL_RE = re.compile("[\x00-\x1f\x7f-\x9f]")
STATUS_ORDER = {"vulnerable": 3, "unknown": 2, "missing": 2, "patched": 1, "none": 0}
EXIT_FOR = {"vulnerable": 1, "unknown": 2, "missing": 2, "patched": 0, "none": 0}
# ---------------------------------------------------------------- output safety
def clean(text):
"""Escape control characters as \\xNN so nothing can reach the terminal raw."""
return CONTROL_RE.sub(lambda m: "\\x%02x" % ord(m.group(0)), text)
def clean_tree(obj):
"""clean() every string in a report, keys included."""
if isinstance(obj, str):
return clean(obj)
if isinstance(obj, dict):
return {clean_tree(k): clean_tree(v) for k, v in obj.items()}
if isinstance(obj, list):
return [clean_tree(x) for x in obj]
if isinstance(obj, tuple):
return tuple(clean_tree(x) for x in obj)
return obj
# ---------------------------------------------------------------- safe reads
MAX_READ = 4 << 20 # config files, unit files, scripts
MAX_BINARY = 512 << 20 # Node binaries are about 100 to 130 MB
def open_pinned(path, follow=True, allow_unlinked=False):
"""Open a file someone else may control, for reading, without blocking.
The path is first opened with O_PATH, which never runs a device's open
and never blocks on a FIFO. fstat on that handle must show a regular file
with exactly one link (or none, for a deleted binary seen through
/proc/<pid>/exe, when allow_unlinked). Only then is the same inode
reopened for reading through /proc/self/fd/N with O_RDONLY, O_NONBLOCK
and O_CLOEXEC, so nothing can be swapped in between. follow=False adds
O_NOFOLLOW: a symlink as the last path component is refused.
Returns (fd, stat)."""
pfd = os.open(path, os.O_PATH | os.O_CLOEXEC | (0 if follow else os.O_NOFOLLOW))
try:
st = os.fstat(pfd)
if stat.S_ISLNK(st.st_mode):
raise OSError(errno.ELOOP, "is a symlink", path)
if not stat.S_ISREG(st.st_mode):
raise OSError(errno.EINVAL, "not a regular file", path)
if st.st_nlink != 1 and not (allow_unlinked and st.st_nlink == 0):
raise OSError(errno.EMLINK, "has %d hard links" % st.st_nlink, path)
fd = os.open("/proc/self/fd/%d" % pfd, os.O_RDONLY | os.O_NONBLOCK | os.O_CLOEXEC)
finally:
os.close(pfd)
st2 = os.fstat(fd)
if (st2.st_dev, st2.st_ino) != (st.st_dev, st.st_ino):
os.close(fd)
raise OSError(errno.EAGAIN, "file changed while opening", path)
return fd, st2
def safe_read(path, limit=MAX_READ, binary=False):
"""Read at most `limit` bytes of a regular, singly linked file."""
fd, _ = open_pinned(path)
try:
chunks, size = [], 0
while size < limit:
chunk = os.read(fd, min(65536, limit - size))
if not chunk:
break
chunks.append(chunk)
size += len(chunk)
finally:
os.close(fd)
data = b"".join(chunks)
return data if binary else data.decode("utf-8", "replace")
# ---------------------------------------------------------------- versions
def parse_fixed(spec):
fixed = {}
for part in spec.split(","):
part = part.strip()
if not part:
continue
line, _, ver = part.partition("=")
m = VERSION_RE.match(ver.strip())
if not line.strip().isdigit() or not m or m.group(4):
raise ValueError("bad --fixed entry %r (want 24=24.22.1)" % part)
v = tuple(int(x) for x in m.groups()[:3])
if int(line) != v[0]:
raise ValueError("bad --fixed entry %r: the version is not on line %s" % (part, line))
fixed[int(line)] = v
if not fixed:
raise ValueError("--fixed is empty")
return fixed
def fmt(v):
return "v%d.%d.%d" % v
def classify(version, fixed):
"""Return (status, reason) for a version string such as 'v24.14.0'."""
if not version:
return "unknown", "version not found"
m = VERSION_RE.match(version.strip())
if not m:
return "unknown", "cannot parse version %r" % version
if m.group(4):
return "unknown", "pre-release or custom build %s" % version
v = tuple(int(x) for x in m.groups()[:3])
major = v[0]
if major in fixed:
if v >= fixed[major]:
return "patched", "%s >= %s" % (fmt(v), fmt(fixed[major]))
return "vulnerable", "%s < %s" % (fmt(v), fmt(fixed[major]))
if major < max(fixed):
return "vulnerable", "Node %d is end-of-life: no fixed release" % major
return "unknown", "Node %d is newer than the fixed-release table" % major
def worst(statuses):
best = "none"
for s in statuses:
if STATUS_ORDER.get(s, 2) > STATUS_ORDER[best]:
best = s
return best
def pinned_version(path):
"""The version named in a path component, or None."""
m = PINNED_RE.search(path or "")
return "v" + m.group(1) if m else None
# ---------------------------------------------------------------- binaries
class BinaryInspector:
"""Reads the version of a Node binary without running it.
The only source is the binary itself: official builds compile in their
headers URL (node-vX.Y.Z-headers.tar.gz). For a process the file is opened
through /proc/<pid>/exe, the inode the process runs, even when it was
deleted or lives in a container. A version that only a directory name
gives ("from path") is a hint and never counts as patched. --exec-version
adds a `--version` cross-check through exec_version_hardened().
"""
def __init__(self, exec_version=False):
self.exec_version = exec_version
self.cache = {}
def inspect(self, path, exec_path=None, magic=False):
"""path: the file to read. magic=True for /proc/<pid>/exe, which has to
be followed; any other path is opened with O_NOFOLLOW. exec_path: the
file to run with --exec-version."""
try:
fd, st = open_pinned(path, follow=magic, allow_unlinked=magic)
except OSError as e:
return {"version": None, "method": None, "shared": [],
"error": "cannot read the binary: %s" % (e.strerror or e)}
key = (st.st_dev, st.st_ino, st.st_size, st.st_mtime_ns, bool(exec_path and self.exec_version))
try:
if key not in self.cache:
self.cache[key] = self._inspect(fd, st, exec_path)
self.cache[key]["inode"] = [st.st_dev, st.st_ino]
finally:
os.close(fd)
return self.cache[key]
def _inspect(self, fd, st, exec_path):
info = {"version": None, "method": None, "error": None, "shared": []}
embedded = set()
if st.st_size > MAX_BINARY:
info["error"] = "file larger than %d MB, not read" % (MAX_BINARY >> 20)
return info
try:
# pread in chunks with an overlap, not mmap: a file truncated
# while we read it ends the loop instead of raising SIGBUS.
pos, tail, shared = 0, b"", set()
while pos < st.st_size:
chunk = os.pread(fd, min(1 << 20, st.st_size - pos), pos)
if not chunk:
break
buf = tail + chunk
for m in EMBEDDED_RE.finditer(buf):
embedded.add("v" + m.group(1).decode())
for lib in (b"libnode.so", b"libbrotlidec.so"):
if lib in buf:
shared.add(lib.decode())
tail = buf[-256:]
pos += len(chunk)
info["shared"] = sorted(shared)
head = os.pread(fd, 4, 0)
after = os.fstat(fd)
except (OSError, ValueError) as e:
info["error"] = getattr(e, "strerror", None) or str(e)
return info
if pos < st.st_size or (after.st_size, after.st_mtime_ns) != (st.st_size, st.st_mtime_ns):
info["error"] = "the file changed or ended early while it was read"
return info
if head != b"\x7fELF":
# A shell wrapper can mention any version and start another Node.
info["error"] = "not an ELF binary: an embedded version is only taken from one"
return info
if len(embedded) > 1:
info["error"] = "several versions embedded: %s" % ", ".join(sorted(embedded))
return info
found = embedded.pop() if embedded else None
executed = None
if exec_path and self.exec_version:
executed, why = exec_version_hardened(exec_path, (st.st_dev, st.st_ino))
if executed is None:
info["exec_refused"] = why
if found and executed and found != executed:
info["error"] = "embedded %s but --version says %s" % (found, executed)
return info
if found:
info["version"], info["method"] = found, "embedded" + ("+--version" if executed else "")
if info.get("exec_refused"):
info["note"] = "--exec-version refused: %s" % info["exec_refused"]
elif executed:
info["version"], info["method"] = executed, "--version"
return info
def trusted_owner_ok(st, me):
"""Root, or us when we are not root; never writable by group or others."""
return not untrusted_reason(st, me)
def untrusted_reason(st, me):
"""Why a file or directory could have been changed by someone else, or ''."""
owners = {0} if me == 0 else {0, me}
if st.st_uid not in owners:
return "owned by uid %d" % st.st_uid
if st.st_mode & (stat.S_IWGRP | stat.S_IWOTH):
return "writable by group or others (mode %o)" % stat.S_IMODE(st.st_mode)
return ""
def exec_version_hardened(path, expect=None, timeout=10):
"""Run `<node> --version` only when nobody but root (or us, when not root)
can have changed the file or any directory above it. The file is pinned
with an O_PATH descriptor, must be the inode that was read, and is
executed through /proc/self/fd/N, so it cannot be swapped between the
checks and the exec. Returns (version, why)."""
me = os.geteuid()
try:
real = os.path.realpath(path)
fd = os.open(real, os.O_PATH | os.O_NOFOLLOW | os.O_CLOEXEC)
except (OSError, AttributeError) as e:
return None, "cannot open: %s" % (getattr(e, "strerror", None) or e)
try:
st = os.fstat(fd)
if not stat.S_ISREG(st.st_mode) or st.st_nlink != 1:
return None, "not a regular file with one link"
if expect and (st.st_dev, st.st_ino) != tuple(expect):
return None, "file changed since it was found"
if me == 0 and st.st_uid != 0:
return None, "running as root: refusing a binary not owned by root"
if not trusted_owner_ok(st, me):
return None, "binary %s" % untrusted_reason(st, me)
d = os.path.dirname(real)
while True:
try:
dst = os.stat(d)
except OSError as e:
return None, "cannot stat %s: %s" % (d, e.strerror)
if not trusted_owner_ok(dst, me):
return None, "directory %s is %s" % (d, untrusted_reason(dst, me))
if d == "/":
break
d = os.path.dirname(d)
try:
out = subprocess.run(["/proc/self/fd/%d" % fd, "--version"], pass_fds=(fd,),
capture_output=True, text=True, timeout=timeout,
env={"PATH": "/usr/bin:/bin"}, cwd="/")
except (OSError, subprocess.SubprocessError) as e:
return None, "exec failed: %s" % e
line = (out.stdout or "").strip().splitlines()
if out.returncode == 0 and line and VERSION_RE.match(line[0].strip()):
return line[0].strip(), None
return None, "--version gave no version"
finally:
os.close(fd)
def judge_binary(info, fixed, name_hint=None):
status, reason = classify(info.get("version"), fixed)
if info.get("error") and not info.get("version"):
reason = info["error"]
if not info.get("version") and name_hint:
reason += "; from path: %s (a hint, not proof)" % name_hint
if info.get("exec_refused"):
reason += "; --exec-version refused: %s" % info["exec_refused"]
if info.get("shared"):
# Distribution builds link system libraries (Debian: libnode and
# libbrotli) and backport fixes, so the upstream version decides nothing.
status = "unknown"
reason = ("distribution build linked to system %s (upstream says: %s); "
"check your distribution's nodejs and brotli advisories"
% (" and ".join(info["shared"]), reason))
return status, reason
# ---------------------------------------------------------------- processes
def read_proc(path, mode="r"):
"""Read a procfs file (kernel-generated; never user-controlled)."""
with open(path, mode) as f:
return f.read()
def proc_table(proc_root):
"""pid -> {ppid, comm, uid} for every process we can see."""
table = {}
for name in os.listdir(proc_root):
if not name.isdigit():
continue
base = os.path.join(proc_root, name)
try:
raw = read_proc(os.path.join(base, "stat"))
close = raw.rfind(")")
comm = raw[raw.find("(") + 1:close]
ppid = int(raw[close + 2:].split()[1])
except (OSError, ValueError, IndexError):
continue
uid = None
try:
for line in read_proc(os.path.join(base, "status")).splitlines():
if line.startswith("Uid:"):
uid = int(line.split()[1])
break
except (OSError, ValueError):
pass
table[int(name)] = {"ppid": ppid, "comm": comm, "uid": uid}
return table
def short_cmd(argv):
"""argv[0] and up to two arguments, never values that look like settings."""
if not argv:
return ""
parts = [os.path.basename(argv[0])] if "/" in argv[0] and " " not in argv[0] else [argv[0]]
for a in argv[1:3]:
parts.append("<arg>" if ("=" in a or len(a) > 80) else a)
s = " ".join(parts)
return s if len(s) <= 90 else s[:87] + "..."
def username(uid):
if uid is None:
return "?"
try:
return pwd.getpwuid(uid).pw_name
except KeyError:
return str(uid)
def scan_processes(proc_root, inspector, fixed, table):
try:
my_mnt = os.readlink(os.path.join(proc_root, "self", "ns", "mnt"))
except OSError:
my_mnt = None
found, hidden = [], 0
for pid in sorted(table):
base = os.path.join(proc_root, str(pid))
try:
argv = [a.decode("utf-8", "replace")
for a in read_proc(os.path.join(base, "cmdline"), "rb").split(b"\0") if a]
except OSError:
argv = []
try:
exe = os.readlink(os.path.join(base, "exe"))
except OSError as e:
looks_node = argv and NODE_EXE_RE.match(os.path.basename(argv[0]))
if looks_node and e.errno in (1, 13):
found.append({"pid": pid, "status": "unknown", "reason":
"cannot read /proc/%d/exe (%s): run the script as its owner" % (pid, e.strerror),
"exe": None, "version": None, "cmd": short_cmd(argv),
"user": username(table[pid]["uid"]), "ppid": table[pid]["ppid"]})
elif e.errno in (1, 13):
hidden += 1
continue
deleted = exe.endswith(" (deleted)")
path = exe[:-len(" (deleted)")] if deleted else exe
if not NODE_EXE_RE.match(os.path.basename(path)):
continue
try:
mnt = os.readlink(os.path.join(base, "ns", "mnt"))
except OSError:
mnt = None
foreign = mnt is not None and my_mnt is not None and mnt != my_mnt
# Read the binary through /proc/<pid>/exe, which is the inode the
# process runs even if the file was replaced or lives in a container.
# Run --version only on the same file in our own mount namespace.
# Read the binary through /proc/<pid>/exe: the inode the process
# runs. --exec-version only applies to the same file in our own mount
# namespace, never to a replaced (deleted) binary.
own = not deleted and not foreign and same_file(path, os.path.join(base, "exe"))
info = dict(inspector.inspect(os.path.join(base, "exe"),
exec_path=path if own else None, magic=True))
deleted_libs, shared_libs = mapped_libs(base)
info["shared"] = sorted(set(info.get("shared") or []) | set(shared_libs))
status, reason = judge_binary(info, fixed, pinned_version(path))
notes = []
if deleted:
notes.append("binary was replaced on disk: this process still runs the old file")
if foreign:
notes.append("other mount namespace (container): path is inside the container")
if deleted_libs:
notes.append("maps replaced libraries (restart needed): %s" % ", ".join(deleted_libs))
found.append({"pid": pid, "ppid": table[pid]["ppid"], "user": username(table[pid]["uid"]),
"exe": path, "deleted": deleted, "container": foreign, "inode": info.get("inode"),
"version": info.get("version"), "method": info.get("method"),
"status": status, "reason": reason, "notes": notes,
"cmd": short_cmd(argv)})
return found, hidden
def same_file(a, b):
try:
sa, sb = os.stat(a), os.stat(b)
except OSError:
return False
return (sa.st_dev, sa.st_ino) == (sb.st_dev, sb.st_ino)
def mapped_libs(base):
"""(replaced libraries still mapped, system libnode/libbrotlidec mapped)."""
try:
lines = read_proc(os.path.join(base, "maps")).splitlines()
except OSError:
return [], []
deleted, shared = set(), set()
for line in lines:
fields = line.split(None, 5)
if len(fields) < 6:
continue
path = fields[5]
name = os.path.basename(path.replace(" (deleted)", ""))
if path.endswith("(deleted)") and re.match(r"lib(node|brotli|ssl|crypto|z|cares)", name):
deleted.add(name)
for lib in ("libnode.so", "libbrotlidec.so"):
if name.startswith(lib):
shared.add(lib)
return sorted(deleted), sorted(shared)
def ancestry(pid, table, procs_by_pid, limit=4):
chain = []
cur = table.get(pid, {}).get("ppid")
while cur and cur in table and len(chain) < limit:
# Node names its main thread "MainThread", which says nothing here.
name = "node" if cur in procs_by_pid else table[cur]["comm"]
chain.append("%s(%d)" % (name, cur))
cur = table[cur]["ppid"]
return chain
def descendants(pid, table):
kids = {}
for p, row in table.items():
kids.setdefault(row["ppid"], []).append(p)
out, todo = [], [pid]
while todo:
p = todo.pop()
for k in kids.get(p, []):
out.append(k)
todo.append(k)
return set(out)
# ---------------------------------------------------------------- node paths
def node_for(path):
"""The node binary a path implies: itself, the node next to npm/npx, the
node inside a bin directory, or the node of the versioned install that
contains the path (a global package under lib/node_modules)."""
base = os.path.basename(path)
if base in ("node", "nodejs"):
return path
if base in NODE_TOOL_NAMES:
return os.path.join(os.path.dirname(path), "node")
if base == "bin":
return os.path.join(path, "node")
m = PINNED_RE.search(path)
if m:
root = path[:m.end()].rstrip("/")
for rel in ("bin/node", "installation/bin/node"):
cand = os.path.join(root, rel)
if os.path.exists(cand):
return cand
return os.path.join(root, "bin", "node")
return os.path.join(path, "node")
def hint_status(hint, fixed, why="binaries outside /proc are not read"):
"""Judge a version taken from a path name. A path that names an unfixed
version counts as vulnerable; one that names a fixed version stays
unknown, because the binary in it was not checked."""
if not hint:
return "unknown", "version not known: %s" % why
status, reason = classify(hint, fixed)
if status == "vulnerable":
return "vulnerable", "from path: %s, %s (%s)" % (hint, reason, why)
return "unknown", "from path: %s (a hint, not proof: %s)" % (hint, why)
class Context:
"""What judging a config path needs: the fixed table, the running
binaries by inode, and whether --exec-version is on."""
def __init__(self, fixed, procs=(), exec_version=False):
self.fixed = fixed
self.exec_version = exec_version
self.known = {}
for p in procs:
if p.get("inode") and p.get("version") and not p.get("deleted"):
self.known.setdefault(tuple(p["inode"]), p)
def judge_path(path, ctx, tool_uses_path=True):
"""Judge a Node path found in a pm2 entry, crontab or unit. The binary is
not read: a running process that uses the same file gives its version,
otherwise the path is a hint. Pinned means the path itself names one
install, so it will not follow the next upgrade. npm, npx and corepack
start with `#!/usr/bin/env node`, so run directly they use the first node
on PATH, not the one next to them (tool_uses_path=False when pm2 passes
the interpreter itself)."""
node = node_for(path)
pin = pinned_version(path)
try:
st = os.stat(node)
except OSError:
reason = "%s does not exist" % node
if pin:
reason += "; the path names %s (%s)" % (pin, classify(pin, ctx.fixed)[0])
return {"path": path, "node": node, "pinned": bool(pin), "version": pin,
"status": "missing", "reason": reason}
hint = pin or pinned_version(os.path.realpath(node))
version = None
running = ctx.known.get((st.st_dev, st.st_ino))
if running:
version = running["version"]
status, reason = running["status"], "%s (same file as running pid %d)" % (
running["reason"], running["pid"])
elif ctx.exec_version:
version, why = exec_version_hardened(node, (st.st_dev, st.st_ino))
if version:
status, reason = classify(version, ctx.fixed)
else:
status, reason = hint_status(hint, ctx.fixed, "--exec-version refused: %s" % why)
else:
status, reason = hint_status(hint, ctx.fixed)
if tool_uses_path and os.path.basename(path) in ("npm", "npx", "corepack") \
and status == "patched":
status = "unknown"
reason += ("; %s runs on the first `node` on the PATH it is started with, "
"which this line does not show" % os.path.basename(path))
return {"path": path, "node": node, "pinned": bool(pin), "version": version or hint,
"from_path": not version, "status": status, "reason": reason}
def find_node_paths(text):
"""Yield (line_no, path) for absolute paths to Node binaries or to bin
directories inside a versioned Node install. Comments are skipped."""
for no, line in enumerate(text.splitlines(), 1):
stripped = line.strip()
if not stripped or stripped.startswith(("#", ";")):
continue
seen = set()
for m in ABS_PATH_RE.finditer(line):
p = m.group(0).rstrip("/")
if p.startswith("//") or line[max(0, m.start() - 1):m.start()] == ":" and \
line[max(0, m.start() - 6):m.start()].endswith(("http:", "https:")):
continue # a URL, not a path
base = os.path.basename(p)
# Node tools anywhere, and anything inside a versioned Node
# install: its bin dir on a PATH line, or a global package such
# as .../v24.14.0/lib/node_modules/pm2/bin/pm2.
interesting = base in NODE_TOOL_NAMES or (
pinned_version(p) and re.search(r"node", p, re.I))
if interesting and p not in seen:
seen.add(p)
yield no, p
def scan_text_sources(sources, ctx):
"""sources: (label, text) or (label, None, error) for a file that should
have been read but could not be: that is unknown, not clean."""
results = []
for src in sources:
label, text = src[0], src[1]
if text is None:
results.append({"where": label, "path": None, "node": None, "pinned": False,
"version": None, "status": "unknown",
"reason": "cannot read: %s" % src[2]})
continue
for no, p in find_node_paths(text):
r = judge_path(p, ctx)
r["where"] = "%s:%d" % (label, no)
results.append(r)
# EnvironmentFile= is never followed. For units that run Node, list it
# as not read, so the operator can check it for PATH or node paths.
if NODEISH_RE.search(text):
for m in ENV_FILE_RE.finditer(text):
no = text.count("\n", 0, m.start()) + 1
results.append({"where": "%s:%d" % (label, no), "path": m.group(1).strip(),
"node": None, "pinned": False, "version": None,
"status": "unknown", "reason": "EnvironmentFile not read: check "
"it for PATH or node paths"})
return results
def read_unit(path, sources, limit=MAX_READ):
"""Append (path, text) for a unit file owned by root or by us, or
(path, None, error) when it cannot be read. Units owned by anyone else
are skipped: as root, reading them would let their owner point us at
files. A file above the limit is an error, never silently cut short."""
try:
fd, st = open_pinned(path)
except OSError as e:
sources.append((path, None, e.strerror or str(e)))
return
try:
if st.st_uid not in (0, os.geteuid()):
return
data = os.read(fd, limit + 1)
finally:
os.close(fd)
if len(data) > limit:
sources.append((path, None, "larger than %d MB" % (limit >> 20)))
else:
sources.append((path, data.decode("utf-8", "replace")))
def crontab_sources(args):
"""`crontab -l` of the user running the script. (--crontab-file reads a
copy instead, for tests and offline review.)"""
sources = []
if args.crontab_file:
for f in args.crontab_file:
try:
sources.append((f, safe_read(f)))
except OSError as e:
sources.append((f, None, e.strerror or str(e)))
return sources
if shutil.which("crontab"):
try:
out = subprocess.run(["crontab", "-l"], capture_output=True, text=True, timeout=10)
label = "crontab -l (%s)" % username(os.geteuid())
if out.returncode == 0:
sources.append((label, out.stdout))
elif "no crontab" not in (out.stderr or "").lower():
sources.append((label, None, "exit %d: %s" % (out.returncode, " ".join(
(out.stderr or "").strip().splitlines()[:1]))))
except (OSError, subprocess.SubprocessError) as e:
sources.append(("crontab -l", None, str(e)))
return sources
ENV_FILE_RE = re.compile(r"^\s*EnvironmentFile\s*=\s*(.+?)\s*$", re.M)
# A unit runs Node when an Exec*= or Environment= line names node, npm, npx
# or pm2 as a command or path (a Description that says "node" does not count).
NODEISH_RE = re.compile(r"^\s*(Exec\w*|Environment)\s*=.*(^|[\s/=\"'])(node|nodejs|npm|npx|pm2)"
r"([\s\"';]|$)", re.M)
SYSTEMD_DIRS = ["/etc/systemd/system", "/run/systemd/system", "/usr/local/lib/systemd/system",
"/lib/systemd/system", "/usr/lib/systemd/system", "/etc/systemd/user",
"~/.config/systemd/user"]
UNIT_PATTERNS = ("*.service", "*.service.d/*.conf", "*.timer")
def systemd_sources(dirs):
"""Unit files and drop-ins in the usual directories. Nothing they refer
to (EnvironmentFile=, scripts) is read."""
sources, seen = [], set()
for d in dirs:
d = os.path.expanduser(d)
for pat in UNIT_PATTERNS:
for f in sorted(glob.glob(os.path.join(d, pat))):
real = os.path.realpath(f)
if real in seen or os.path.islink(f) or not os.path.isfile(f):
# Symlinked units are skipped (an enabled unit's real file
# sits in one of these directories); so is anything that
# is not a regular file.
continue
seen.add(real)
read_unit(f, sources)
return sources
def pm2_unit_state(unit_path):
"""For a pm2 systemd unit, whether it is the thing running the daemon."""
unit = os.path.basename(unit_path)
if not shutil.which("systemctl"):
return None
try:
out = subprocess.run(["systemctl", "show", unit, "-p", "ActiveState", "-p", "MainPID"],
capture_output=True, text=True, timeout=10).stdout
except (OSError, subprocess.SubprocessError):
return None
vals = dict(l.split("=", 1) for l in out.splitlines() if "=" in l)
return {"unit": unit, "active": vals.get("ActiveState"), "main_pid": vals.get("MainPID")}
# ---------------------------------------------------------------- installs
MANAGERS = [
# (name, glob for each installed node binary, default alias file or link)
("nvm", "{nvm}/versions/node/*/bin/node", "{nvm}/alias/default"),
("fnm", "{fnm}/node-versions/*/installation/bin/node", "{fnm}/aliases/default"),
("volta", "{home}/.volta/tools/image/node/*/bin/node", None),
("asdf", "{home}/.asdf/installs/nodejs/*/bin/node", None),
("mise", "{home}/.local/share/mise/installs/node/*/bin/node", None),
("nodenv", "{home}/.nodenv/versions/*/bin/node", None),
("n", "/usr/local/n/versions/node/*/bin/node", None),
]
def installed_versions(homes, ctx):
"""Node installs kept by version managers, for information: nothing is
read, versions come from directory names or from a running process that
uses the same binary. They do not affect the exit code."""
def label(path):
try:
st = os.stat(path)
except OSError:
return {"version": pinned_version(path), "source": "missing", "status": "missing"}
running = ctx.known.get((st.st_dev, st.st_ino))
if running:
return {"version": running["version"], "source": "running", "status": running["status"]}
hint = pinned_version(path) or pinned_version(os.path.realpath(path))
return {"version": hint, "source": "from path", "status": hint_status(hint, ctx.fixed)[0]}
out, seen = [], set()
for home in homes:
nvm = os.environ.get("NVM_DIR") if home == os.path.expanduser("~") else None
fnms = [os.environ.get("FNM_DIR")] if home == os.path.expanduser("~") else []
fnms += [os.path.join(home, ".local/share/fnm"), os.path.join(home, ".fnm")]
for name, pattern, alias in MANAGERS:
for fnm in ([d for d in fnms if d] if name == "fnm" else [None]):
ctx_dirs = {"home": home, "nvm": nvm or os.path.join(home, ".nvm"), "fnm": fnm}
pat = pattern.format(**ctx_dirs)
if pat in seen:
continue
seen.add(pat)
bins = sorted(glob.glob(pat))
if not bins:
continue
default = None
if alias:
a = alias.format(**ctx_dirs)
if os.path.islink(a):
target = os.path.realpath(a)
node = next((c for c in (os.path.join(target, "bin", "node"),
os.path.join(target, "installation", "bin", "node"))
if os.path.exists(c)), os.path.join(target, "bin", "node"))
default = label(node)
elif os.path.isfile(a):
default = {"version": None, "source": "alias file %s not read" % a,
"status": "unknown"}
out.append({"manager": name, "where": os.path.dirname(pat.split("*")[0].rstrip("/")),
"versions": [label(b) for b in bins], "default": default})
shell = None
which = shutil.which("node")
if which:
shell = dict(label(which), path=which, real=os.path.realpath(which))
return out, shell
# ---------------------------------------------------------------- pm2
def resolve_on_path(path_value, cwd=None, skip_ephemeral=False, name="node"):
"""First executable `name` on a saved PATH, like execvp would find it.
Empty and relative entries are resolved against the app's cwd. With
skip_ephemeral, fnm multishell dirs (gone after a reboot) are skipped,
because that is what a resurrected app will see. Returns (dir, notes)."""
notes = []
for d in (path_value or "").split(":"):
if not d or not d.startswith("/"):
if not cwd:
notes.append("relative PATH entry %r skipped: no cwd" % d)
continue
d = os.path.join(cwd, d)
if skip_ephemeral and FNM_MULTISHELL_RE.search(d):
notes.append("skipped %s: an fnm multishell dir does not survive a reboot" % d)
continue
cand = os.path.join(d, name)
if os.path.isfile(cand) and os.access(cand, os.X_OK):
return d, notes
return None, notes
def pm2_entry(entry, ctx, procs_by_pid, table, daemon=None, saved=False):
env = entry.get("pm2_env", entry) if isinstance(entry, dict) else None
if not isinstance(env, dict):
return {"name": "?", "status_pm2": None, "pid": None, "cluster": False, "interpreter": "",
"script": "", "running": [], "status": "unknown",
"findings": [{"what": "entry", "path": None, "pinned": False, "version": None,
"status": "unknown", "reason": "malformed pm2 entry"}]}
text = lambda v: v if isinstance(v, str) else "" # noqa: E731
name = text(env.get("name")) or text(entry.get("name")) or "?"
interp = text(env.get("exec_interpreter"))
script = text(env.get("pm_exec_path"))
sub_env = env.get("env") if isinstance(env.get("env"), dict) else {}
path_value = text(env.get("PATH")) or text(sub_env.get("PATH"))
cwd = text(env.get("pm_cwd")) or text(env.get("cwd")) or None
cluster = env.get("exec_mode") == "cluster_mode"
bare = interp in ("node", "nodejs")
findings = []
if cluster:
# Cluster workers are forked by the daemon with its own
# process.execPath: interpreter and PATH do not choose their binary.
if daemon and daemon.get("version"):
reason = "cluster mode runs on the daemon's Node: %s" % daemon.get("reason")
if saved:
reason += "; after a reboot, the Node that starts pm2 (startup unit PATH) decides"
findings.append({"what": "daemon node", "path": daemon.get("exe"), "pinned": False,
"version": daemon.get("version"), "status": daemon["status"],
"reason": reason})
else:
findings.append({"what": "daemon node", "path": None, "pinned": False, "version": None,
"status": "unknown", "reason": "cluster mode runs on the Node that "
"starts the pm2 daemon, which could not be determined"})
elif interp.startswith("/"):
r = judge_path(interp, ctx, tool_uses_path=False)
r["what"] = "interpreter"
findings.append(r)
if script.startswith("/") and pinned_version(script) and re.search(r"node", script, re.I):
r = judge_path(script, ctx, tool_uses_path=False)
r["what"] = "script"
findings.append(r)
running = []
pid = entry.get("pid") or env.get("pid")
if isinstance(pid, int) and pid:
tree = {pid} | descendants(pid, table)
running = [procs_by_pid[p] for p in sorted(tree) if p in procs_by_pid]
else:
pid = None
# The saved PATH decides which node a bare `node` interpreter, a
# `#!/usr/bin/env node` script or a child process gets. It does not
# matter for an entry that runs Python or bash and starts no Node.
other_interp = (interp.startswith("/") and os.path.basename(interp) not in ("node", "nodejs")) \
or (interp and not interp.startswith("/") and not bare and interp != "none")
d, notes = resolve_on_path(path_value, cwd, skip_ephemeral=saved,
name="nodejs" if interp == "nodejs" else "node")
if other_interp and not running:
pass
elif d:
r = judge_path(os.path.join(d, "nodejs" if interp == "nodejs" else "node"), ctx)
r["what"] = "PATH node"
if FNM_MULTISHELL_RE.search(d):
r["reason"] += "; fnm multishell dir: gone after a reboot"
if notes:
r["reason"] += "; " + "; ".join(notes)
if any("no cwd" in n for n in notes) and r["status"] == "patched":
r["status"] = "unknown"
findings.append(r)
elif bare and not cluster:
findings.append({"what": "PATH node", "path": None, "pinned": False, "version": None,
"status": "unknown",
"reason": "no node on the saved PATH" + ("; " + "; ".join(notes) if notes else "")})
statuses = [f["status"] for f in findings] + [p["status"] for p in running]
return {"name": name, "status_pm2": text(env.get("status")), "pid": pid, "cluster": cluster,
"interpreter": interp, "script": script, "findings": findings, "running": running,
"status": worst(statuses) if statuses else "none"}
def pm2_homes(args):
if args.pm2_home:
return [os.path.expanduser(h) for h in args.pm2_home]
homes = [os.environ.get("PM2_HOME") or os.path.expanduser("~/.pm2")]
out = []
for h in homes:
if os.path.isdir(h) and os.path.realpath(h) not in [os.path.realpath(x) for x in out]:
out.append(h)
return out
def load_json_list(raw):
raw = raw or ""
start = raw.find("[")
if start == -1:
return None
try:
data = json.loads(raw[start:])
except ValueError:
return None
return data if isinstance(data, list) else None
def find_pm2_daemon(home, proc_root, table):
"""The pid of the pm2 daemon for PM2_HOME, from /proc: a process of ours
whose title is `PM2 vX.Y.Z: God Daemon (<PM2_HOME>)`."""
want = b"God Daemon (" + os.fsencode(home.rstrip("/")) + b")"
for pid, row in sorted(table.items()):
if row.get("uid") != os.geteuid():
continue
try:
title = read_proc(os.path.join(proc_root, str(pid), "cmdline"), "rb").rstrip(b"\0")
except OSError:
continue
if title.startswith(b"PM2 v") and title.endswith(want):
return pid
return None
def pm2_live(home, args, proc_root, table):
"""`pm2 jlist` for our own PM2_HOME, only when its daemon is running
(jlist would otherwise start one). The pm2 CLI is the one thing on the
host the script runs: your own pm2, as you, and only when it is owned by
you or root and not writable by group or others."""
if args.pm2_jlist_file:
try:
data = load_json_list(safe_read(args.pm2_jlist_file, limit=64 << 20))
except OSError as e:
return None, "FAIL: cannot read %s: %s" % (args.pm2_jlist_file, e.strerror or e)
return data, (None if data is not None else "FAIL: no JSON list in %s" % args.pm2_jlist_file)
if find_pm2_daemon(home, proc_root, table) is None:
return None, "no pm2 daemon running for this PM2_HOME"
pm2 = args.pm2_bin or shutil.which("pm2")
if not pm2:
return None, "FAIL: pm2 daemon runs but no pm2 CLI was found"
try:
cli = os.stat(os.path.realpath(pm2))
except OSError as e:
return None, "FAIL: cannot stat the pm2 CLI: %s" % e.strerror
if not trusted_owner_ok(cli, os.geteuid()):
return None, "FAIL: pm2 CLI %s is %s: not run, live list skipped" % (
pm2, untrusted_reason(cli, os.geteuid()))
env = dict(os.environ, PM2_HOME=home)
try:
out = subprocess.run([pm2, "jlist"], capture_output=True, text=True, timeout=60, env=env)
except (OSError, subprocess.SubprocessError) as e:
return None, "FAIL: pm2 jlist failed: %s" % e
if out.returncode != 0:
return None, "FAIL: pm2 jlist exited %d" % out.returncode