Debian Codegraph @

Lib/test/audit-tests.py

raw
1 """This script contains the actual auditing tests.
2
3 It should not be imported directly, but should be run by the test_audit
4 module with arguments identifying each test.
5
6 """
7
8 import contextlib
9 import os
10 import sys
11
12
13 class TestHook:
14 """Used in standard hook tests to collect any logged events.
15
16 Should be used in a with block to ensure that it has no impact
17 after the test completes.
18 """
19
20 def __init__(self, raise_on_events=None, exc_type=RuntimeError):
21 self.raise_on_events = raise_on_events or ()
22 self.exc_type = exc_type
23 self.seen = []
24 self.closed = False
25
26 def __enter__(self, *a):
27 sys.addaudithook(self)
28 return self
29
30 def __exit__(self, *a):
31 self.close()
32
33 def close(self):
34 self.closed = True
35
36 @property
37 def seen_events(self):
38 return [i[0] for i in self.seen]
39
40 def __call__(self, event, args):
41 if self.closed:
42 return
43 self.seen.append((event, args))
44 if event in self.raise_on_events:
45 raise self.exc_type("saw event " + event)
46
47
48 # Simple helpers, since we are not in unittest here
49 def assertEqual(x, y):
50 if x != y:
51 raise AssertionError(f"{x!r} should equal {y!r}")
52
53
54 def assertIn(el, series):
55 if el not in series:
56 raise AssertionError(f"{el!r} should be in {series!r}")
57
58
59 def assertNotIn(el, series):
60 if el in series:
61 raise AssertionError(f"{el!r} should not be in {series!r}")
62
63
64 def assertSequenceEqual(x, y):
65 if len(x) != len(y):
66 raise AssertionError(f"{x!r} should equal {y!r}")
67 if any(ix != iy for ix, iy in zip(x, y)):
68 raise AssertionError(f"{x!r} should equal {y!r}")
69
70
71 @contextlib.contextmanager
72 def assertRaises(ex_type):
73 try:
74 yield
75 assert False, f"expected {ex_type}"
76 except BaseException as ex:
77 if isinstance(ex, AssertionError):
78 raise
79 assert type(ex) is ex_type, f"{ex} should be {ex_type}"
80
81
82 def test_basic():
83 with TestHook() as hook:
84 sys.audit("test_event", 1, 2, 3)
85 assertEqual(hook.seen[0][0], "test_event")
86 assertEqual(hook.seen[0][1], (1, 2, 3))
87
88
89 def test_block_add_hook():
90 # Raising an exception should prevent a new hook from being added,
91 # but will not propagate out.
92 with TestHook(raise_on_events="sys.addaudithook") as hook1:
93 with TestHook() as hook2:
94 sys.audit("test_event")
95 assertIn("test_event", hook1.seen_events)
96 assertNotIn("test_event", hook2.seen_events)
97
98
99 def test_block_add_hook_baseexception():
100 # Raising BaseException will propagate out when adding a hook
101 with assertRaises(BaseException):
102 with TestHook(
103 raise_on_events="sys.addaudithook", exc_type=BaseException
104 ) as hook1:
105 # Adding this next hook should raise BaseException
106 with TestHook() as hook2:
107 pass
108
109
110 def test_marshal():
111 import marshal
112 o = ("a", "b", "c", 1, 2, 3)
113 payload = marshal.dumps(o)
114
115 with TestHook() as hook:
116 assertEqual(o, marshal.loads(marshal.dumps(o)))
117
118 try:
119 with open("test-marshal.bin", "wb") as f:
120 marshal.dump(o, f)
121 with open("test-marshal.bin", "rb") as f:
122 assertEqual(o, marshal.load(f))
123 finally:
124 os.unlink("test-marshal.bin")
125
126 actual = [(a[0], a[1]) for e, a in hook.seen if e == "marshal.dumps"]
127 assertSequenceEqual(actual, [(o, marshal.version)] * 2)
128
129 actual = [a[0] for e, a in hook.seen if e == "marshal.loads"]
130 assertSequenceEqual(actual, [payload])
131
132 actual = [e for e, a in hook.seen if e == "marshal.load"]
133 assertSequenceEqual(actual, ["marshal.load"])
134
135
136 def test_pickle():
137 import pickle
138
139 class PicklePrint:
140 def __reduce_ex__(self, p):
141 return str, ("Pwned!",)
142
143 payload_1 = pickle.dumps(PicklePrint())
144 payload_2 = pickle.dumps(("a", "b", "c", 1, 2, 3))
145
146 # Before we add the hook, ensure our malicious pickle loads
147 assertEqual("Pwned!", pickle.loads(payload_1))
148
149 with TestHook(raise_on_events="pickle.find_class") as hook:
150 with assertRaises(RuntimeError):
151 # With the hook enabled, loading globals is not allowed
152 pickle.loads(payload_1)
153 # pickles with no globals are okay
154 pickle.loads(payload_2)
155
156
157 def test_monkeypatch():
158 class A:
159 pass
160
161 class B:
162 pass
163
164 class C(A):
165 pass
166
167 a = A()
168
169 with TestHook() as hook:
170 # Catch name changes
171 C.__name__ = "X"
172 # Catch type changes
173 C.__bases__ = (B,)
174 # Ensure bypassing __setattr__ is still caught
175 type.__dict__["__bases__"].__set__(C, (B,))
176 # Catch attribute replacement
177 C.__init__ = B.__init__
178 # Catch attribute addition
179 C.new_attr = 123
180 # Catch class changes
181 a.__class__ = B
182
183 actual = [(a[0], a[1]) for e, a in hook.seen if e == "object.__setattr__"]
184 assertSequenceEqual(
185 [(C, "__name__"), (C, "__bases__"), (C, "__bases__"), (a, "__class__")], actual
186 )
187
188
189 def test_open(testfn):
190 # SSLContext.load_dh_params uses Py_fopen() rather than normal open()
191 try:
192 import ssl
193
194 load_dh_params = ssl.create_default_context().load_dh_params
195 except ImportError:
196 load_dh_params = None
197
198 try:
199 import readline
200 except ImportError:
201 readline = None
202
203 def rl(name):
204 if readline:
205 return getattr(readline, name, None)
206 else:
207 return None
208
209 # Try a range of "open" functions.
210 # All of them should fail
211 with TestHook(raise_on_events={"open"}) as hook:
212 for fn, *args in [
213 (open, testfn, "r"),
214 (open, sys.executable, "rb"),
215 (open, 3, "wb"),
216 (open, testfn, "w", -1, None, None, None, False, lambda *a: 1),
217 (load_dh_params, testfn),
218 (rl("read_history_file"), testfn),
219 (rl("read_history_file"), None),
220 (rl("write_history_file"), testfn),
221 (rl("write_history_file"), None),
222 (rl("append_history_file"), 0, testfn),
223 (rl("append_history_file"), 0, None),
224 (rl("read_init_file"), testfn),
225 (rl("read_init_file"), None),
226 ]:
227 if not fn:
228 continue
229 with assertRaises(RuntimeError):
230 try:
231 fn(*args)
232 except NotImplementedError:
233 if fn == load_dh_params:
234 # Not callable in some builds
235 load_dh_params = None
236 raise RuntimeError
237 else:
238 raise
239
240 actual_mode = [(a[0], a[1]) for e, a in hook.seen if e == "open" and a[1]]
241 actual_flag = [(a[0], a[2]) for e, a in hook.seen if e == "open" and not a[1]]
242 assertSequenceEqual(
243 [
244 i
245 for i in [
246 (testfn, "r"),
247 (sys.executable, "r"),
248 (3, "w"),
249 (testfn, "w"),
250 (testfn, "rb") if load_dh_params else None,
251 (testfn, "r") if readline else None,
252 ("~/.history", "r") if readline else None,
253 (testfn, "w") if readline else None,
254 ("~/.history", "w") if readline else None,
255 (testfn, "a") if rl("append_history_file") else None,
256 ("~/.history", "a") if rl("append_history_file") else None,
257 (testfn, "r") if readline else None,
258 ("<readline_init_file>", "r") if readline else None,
259 ]
260 if i is not None
261 ],
262 actual_mode,
263 )
264 assertSequenceEqual([], actual_flag)
265
266
267 def test_cantrace():
268 traced = []
269
270 def trace(frame, event, *args):
271 if frame.f_code == TestHook.__call__.__code__:
272 traced.append(event)
273
274 old = sys.settrace(trace)
275 try:
276 with TestHook() as hook:
277 # No traced call
278 eval("1")
279
280 # No traced call
281 hook.__cantrace__ = False
282 eval("2")
283
284 # One traced call
285 hook.__cantrace__ = True
286 eval("3")
287
288 # Two traced calls (writing to private member, eval)
289 hook.__cantrace__ = 1
290 eval("4")
291
292 # One traced call (writing to private member)
293 hook.__cantrace__ = 0
294 finally:
295 sys.settrace(old)
296
297 assertSequenceEqual(["call"] * 4, traced)
298
299
300 def test_mmap():
301 import mmap
302
303 with TestHook() as hook:
304 mmap.mmap(-1, 8)
305 assertEqual(hook.seen[0][1][:2], (-1, 8))
306
307
308 def test_ctypes_call_function():
309 import ctypes
310 import _ctypes
311
312 with TestHook() as hook:
313 _ctypes.call_function(ctypes._memmove_addr, (0, 0, 0))
314 assert ("ctypes.call_function", (ctypes._memmove_addr, (0, 0, 0))) in hook.seen, f"{ctypes._memmove_addr=} {hook.seen=}"
315
316 ctypes.CFUNCTYPE(ctypes.c_voidp)(ctypes._memset_addr)(1, 0, 0)
317 assert ("ctypes.call_function", (ctypes._memset_addr, (1, 0, 0))) in hook.seen, f"{ctypes._memset_addr=} {hook.seen=}"
318
319 with TestHook() as hook:
320 ctypes.cast(ctypes.c_voidp(0), ctypes.POINTER(ctypes.c_char))
321 assert "ctypes.call_function" in hook.seen_events
322
323 with TestHook() as hook:
324 ctypes.string_at(id("ctypes.string_at") + 40)
325 assert "ctypes.call_function" in hook.seen_events
326 assert "ctypes.string_at" in hook.seen_events
327
328
329 def test_posixsubprocess():
330 import multiprocessing.util
331
332 exe = b"xxx"
333 args = [b"yyy", b"zzz"]
334 with TestHook() as hook:
335 multiprocessing.util.spawnv_passfds(exe, args, ())
336 assert ("_posixsubprocess.fork_exec", ([exe], args, None)) in hook.seen
337
338
339 def test_excepthook():
340 def excepthook(exc_type, exc_value, exc_tb):
341 if exc_type is not RuntimeError:
342 sys.__excepthook__(exc_type, exc_value, exc_tb)
343
344 def hook(event, args):
345 if event == "sys.excepthook":
346 if not isinstance(args[2], args[1]):
347 raise TypeError(f"Expected isinstance({args[2]!r}, " f"{args[1]!r})")
348 if args[0] != excepthook:
349 raise ValueError(f"Expected {args[0]} == {excepthook}")
350 print(event, repr(args[2]))
351
352 sys.addaudithook(hook)
353 sys.excepthook = excepthook
354 raise RuntimeError("fatal-error")
355
356
357 def test_unraisablehook():
358 from _testcapi import err_formatunraisable
359
360 def unraisablehook(hookargs):
361 pass
362
363 def hook(event, args):
364 if event == "sys.unraisablehook":
365 if args[0] != unraisablehook:
366 raise ValueError(f"Expected {args[0]} == {unraisablehook}")
367 print(event, repr(args[1].exc_value), args[1].err_msg)
368
369 sys.addaudithook(hook)
370 sys.unraisablehook = unraisablehook
371 err_formatunraisable(RuntimeError("nonfatal-error"),
372 "Exception ignored for audit hook test")
373
374
375 def test_winreg():
376 from winreg import OpenKey, EnumKey, CloseKey, HKEY_LOCAL_MACHINE
377
378 def hook(event, args):
379 if not event.startswith("winreg."):
380 return
381 print(event, *args)
382
383 sys.addaudithook(hook)
384
385 k = OpenKey(HKEY_LOCAL_MACHINE, "Software")
386 EnumKey(k, 0)
387 try:
388 EnumKey(k, 10000)
389 except OSError:
390 pass
391 else:
392 raise RuntimeError("Expected EnumKey(HKLM, 10000) to fail")
393
394 kv = k.Detach()
395 CloseKey(kv)
396
397
398 def test_socket():
399 import socket
400
401 def hook(event, args):
402 if event.startswith("socket."):
403 print(event, *args)
404
405 sys.addaudithook(hook)
406
407 socket.gethostname()
408
409 # Don't care if this fails, we just want the audit message
410 sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
411 try:
412 # Don't care if this fails, we just want the audit message
413 sock.bind(('127.0.0.1', 8080))
414 except Exception:
415 pass
416 finally:
417 sock.close()
418
419
420 def test_gc():
421 import gc
422
423 def hook(event, args):
424 if event.startswith("gc."):
425 print(event, *args)
426
427 sys.addaudithook(hook)
428
429 gc.get_objects(generation=1)
430
431 x = object()
432 y = [x]
433
434 gc.get_referrers(x)
435 gc.get_referents(y)
436
437
438 def test_http_client():
439 import http.client
440
441 def hook(event, args):
442 if event.startswith("http.client."):
443 print(event, *args[1:])
444
445 sys.addaudithook(hook)
446
447 conn = http.client.HTTPConnection('www.python.org')
448 try:
449 conn.request('GET', '/')
450 except OSError:
451 print('http.client.send', '[cannot send]')
452 finally:
453 conn.close()
454
455
456 def test_sqlite3():
457 import sqlite3
458
459 def hook(event, *args):
460 if event.startswith("sqlite3."):
461 print(event, *args)
462
463 sys.addaudithook(hook)
464 cx1 = sqlite3.connect(":memory:")
465 cx2 = sqlite3.Connection(":memory:")
466
467 # Configured without --enable-loadable-sqlite-extensions
468 try:
469 if hasattr(sqlite3.Connection, "enable_load_extension"):
470 cx1.enable_load_extension(False)
471 try:
472 cx1.load_extension("test")
473 except sqlite3.OperationalError:
474 pass
475 else:
476 raise RuntimeError("Expected sqlite3.load_extension to fail")
477 finally:
478 cx1.close()
479 cx2.close()
480
481 def test_sys_getframe():
482 import sys
483
484 def hook(event, args):
485 if event.startswith("sys."):
486 print(event, args[0].f_code.co_name)
487
488 sys.addaudithook(hook)
489 sys._getframe()
490
491
492 def test_sys_getframemodulename():
493 import sys
494
495 def hook(event, args):
496 if event.startswith("sys."):
497 print(event, *args)
498
499 sys.addaudithook(hook)
500 sys._getframemodulename()
501
502
503 def test_threading():
504 import _thread
505
506 def hook(event, args):
507 if event.startswith(("_thread.", "cpython.PyThreadState", "test.")):
508 print(event, args)
509
510 sys.addaudithook(hook)
511
512 lock = _thread.allocate_lock()
513 lock.acquire()
514
515 class test_func:
516 def __repr__(self): return "<test_func>"
517 def __call__(self):
518 sys.audit("test.test_func")
519 lock.release()
520
521 i = _thread.start_new_thread(test_func(), ())
522 lock.acquire()
523
524 handle = _thread.start_joinable_thread(test_func())
525 handle.join()
526
527
528 def test_threading_abort():
529 # Ensures that aborting PyThreadState_New raises the correct exception
530 import _thread
531
532 class ThreadNewAbortError(Exception):
533 pass
534
535 def hook(event, args):
536 if event == "cpython.PyThreadState_New":
537 raise ThreadNewAbortError()
538
539 sys.addaudithook(hook)
540
541 try:
542 _thread.start_new_thread(lambda: None, ())
543 except ThreadNewAbortError:
544 # Other exceptions are raised and the test will fail
545 pass
546
547
548 def test_wmi_exec_query():
549 import _wmi
550
551 def hook(event, args):
552 if event.startswith("_wmi."):
553 print(event, args[0])
554
555 sys.addaudithook(hook)
556 try:
557 _wmi.exec_query("SELECT * FROM Win32_OperatingSystem")
558 except WindowsError as e:
559 # gh-112278: WMI may be slow response when first called, but we still
560 # get the audit event, so just ignore the timeout
561 if e.winerror != 258:
562 raise
563
564 def test_syslog():
565 import syslog
566
567 def hook(event, args):
568 if event.startswith("syslog."):
569 print(event, *args)
570
571 sys.addaudithook(hook)
572 syslog.openlog('python')
573 syslog.syslog('test')
574 syslog.setlogmask(syslog.LOG_DEBUG)
575 syslog.closelog()
576 # implicit open
577 syslog.syslog('test2')
578 # open with default ident
579 syslog.openlog(logoption=syslog.LOG_NDELAY, facility=syslog.LOG_LOCAL0)
580 sys.argv = None
581 syslog.openlog()
582 syslog.closelog()
583
584
585 def test_not_in_gc():
586 import gc
587
588 hook = lambda *a: None
589 sys.addaudithook(hook)
590
591 for o in gc.get_objects():
592 if isinstance(o, list):
593 assert hook not in o
594
595
596 def test_time(mode):
597 import time
598
599 def hook(event, args):
600 if event.startswith("time."):
601 if mode == 'print':
602 print(event, *args)
603 elif mode == 'fail':
604 raise AssertionError('hook failed')
605 sys.addaudithook(hook)
606
607 time.sleep(0)
608 time.sleep(0.0625) # 1/16, a small exact float
609 try:
610 time.sleep(-1)
611 except ValueError:
612 pass
613
614 def test_sys_monitoring_register_callback():
615 import sys
616
617 def hook(event, args):
618 if event.startswith("sys.monitoring"):
619 print(event, args)
620
621 sys.addaudithook(hook)
622 sys.monitoring.register_callback(1, 1, None)
623
624
625 def test_winapi_createnamedpipe(pipe_name):
626 import _winapi
627
628 def hook(event, args):
629 if event == "_winapi.CreateNamedPipe":
630 print(event, args)
631
632 sys.addaudithook(hook)
633 _winapi.CreateNamedPipe(pipe_name, _winapi.PIPE_ACCESS_DUPLEX, 8, 2, 0, 0, 0, 0)
634
635
636 def test_assert_unicode():
637 import sys
638 sys.addaudithook(lambda *args: None)
639 try:
640 sys.audit(9)
641 except TypeError:
642 pass
643 else:
644 raise RuntimeError("Expected sys.audit(9) to fail.")
645
646 def test_sys_remote_exec():
647 import tempfile
648
649 pid = os.getpid()
650 event_pid = -1
651 event_script_path = ""
652 remote_event_script_path = ""
653 def hook(event, args):
654 if event not in ["sys.remote_exec", "cpython.remote_debugger_script"]:
655 return
656 print(event, args)
657 match event:
658 case "sys.remote_exec":
659 nonlocal event_pid, event_script_path
660 event_pid = args[0]
661 event_script_path = args[1]
662 case "cpython.remote_debugger_script":
663 nonlocal remote_event_script_path
664 remote_event_script_path = args[0]
665
666 sys.addaudithook(hook)
667 with tempfile.NamedTemporaryFile(mode='w+', delete=True) as tmp_file:
668 tmp_file.write("a = 1+1\n")
669 tmp_file.flush()
670 sys.remote_exec(pid, tmp_file.name)
671 assertEqual(event_pid, pid)
672 assertEqual(event_script_path, tmp_file.name)
673 assertEqual(remote_event_script_path, tmp_file.name)
674
675 if __name__ == "__main__":
676 from test.support import suppress_msvcrt_asserts
677
678 suppress_msvcrt_asserts()
679
680 test = sys.argv[1]
681 globals()[test](*sys.argv[2:])