Skip to content

Commit e5473fe

Browse files
gh-143545: Add test for UAF in lsprof via re-entrant timer
1 parent 741ba14 commit e5473fe

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed

Lib/test/test_lsprof_uaf.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
"""Test for gh-143545: UAF in lsprof via re-entrant external timer."""
2+
import unittest
3+
import cProfile
4+
5+
6+
class ReentrantTimerTest(unittest.TestCase):
7+
"""Test that profiler handles re-entrant timer correctly."""
8+
9+
def test_reentrant_timer_index(self):
10+
"""Clearing profiler during timer.__index__ should not crash."""
11+
12+
class Timer:
13+
def __init__(self, profiler):
14+
self.profiler = profiler
15+
16+
def __call__(self):
17+
return self
18+
19+
def __index__(self):
20+
self.profiler.clear()
21+
return 0
22+
23+
prof = cProfile.Profile()
24+
prof.timer = Timer(prof)
25+
26+
def victim():
27+
pass
28+
29+
# Should not crash with use-after-free
30+
try:
31+
prof._pystart_callback(victim.__code__, 0)
32+
except (RuntimeError, ValueError):
33+
pass # Expected if we block the operation
34+
35+
36+
if __name__ == '__main__':
37+
unittest.main()

0 commit comments

Comments
 (0)