-
-
Notifications
You must be signed in to change notification settings - Fork 33.9k
gh-143545: Fix UAF in lsprof via re-entrant external timer #143550
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
741ba14
e5473fe
cbab259
34b7430
ffc48d5
ac0c035
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| """Test for gh-143545: UAF in lsprof via re-entrant external timer.""" | ||
| import unittest | ||
| import cProfile | ||
|
|
||
|
|
||
| class ReentrantTimerTest(unittest.TestCase): | ||
| """Test that profiler handles re-entrant timer correctly.""" | ||
|
|
||
| def test_reentrant_timer_index(self): | ||
| """Clearing profiler during timer.__index__ should not crash.""" | ||
|
|
||
| class Timer: | ||
| def __init__(self, profiler): | ||
| self.profiler = profiler | ||
|
|
||
| def __call__(self): | ||
| return self | ||
|
|
||
| def __index__(self): | ||
| self.profiler.clear() | ||
| return 0 | ||
|
|
||
| prof = cProfile.Profile() | ||
| prof.timer = Timer(prof) | ||
|
|
||
| def victim(): | ||
| pass | ||
|
|
||
| # Should not crash with use-after-free | ||
| try: | ||
| prof._pystart_callback(victim.__code__, 0) | ||
| except (RuntimeError, ValueError): | ||
| pass # Expected if we block the operation | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If this is expected - maybe we should use |
||
|
|
||
|
|
||
| if __name__ == '__main__': | ||
| unittest.main() | ||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
| @@ -0,0 +1 @@ | ||||||
| Fix use-after-free in the ``_lsprof`` module when external timer's ``__index__`` method clears the profiler during callback. | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'm not sure whether we should mention |
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -46,6 +46,7 @@ typedef struct _ProfilerContext { | |
| ProfilerEntry *ctxEntry; | ||
| } ProfilerContext; | ||
|
|
||
|
|
||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please avoid changing the formatting of code that you do not touch, as this makes review and backporting more difficult. |
||
| typedef struct { | ||
| PyObject_HEAD | ||
| rotating_node_t *profilerEntries; | ||
|
|
@@ -56,6 +57,7 @@ typedef struct { | |
| double externalTimerUnit; | ||
| int tool_id; | ||
| PyObject* missing; | ||
| int inCallback; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since we are using C11, we can use |
||
| } ProfilerObject; | ||
|
|
||
| #define ProfilerObject_CAST(op) ((ProfilerObject *)(op)) | ||
|
|
@@ -289,6 +291,9 @@ static int freeEntry(rotating_node_t *header, void *arg) | |
|
|
||
| static void clearEntries(ProfilerObject *pObj) | ||
| { | ||
| if (pObj->inCallback) { | ||
| return; | ||
| } | ||
| RotatingTree_Enum(pObj->profilerEntries, freeEntry, NULL); | ||
| pObj->profilerEntries = EMPTY_ROTATING_TREE; | ||
| /* release the memory hold by the ProfilerContexts */ | ||
|
|
@@ -321,13 +326,17 @@ initContext(ProfilerObject *pObj, ProfilerContext *self, ProfilerEntry *entry) | |
| if (subentry) | ||
| ++subentry->recursionLevel; | ||
| } | ||
| self->t0 = call_timer(pObj); | ||
| pObj->inCallback = 1; | ||
| self->t0 = call_timer(pObj); | ||
| pObj->inCallback = 0; | ||
|
Comment on lines
+329
to
+331
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The indentation is inaccurate. |
||
| } | ||
|
|
||
| static void | ||
| Stop(ProfilerObject *pObj, ProfilerContext *self, ProfilerEntry *entry) | ||
| { | ||
| pObj->inCallback = 1; | ||
| PyTime_t tt = call_timer(pObj) - self->t0; | ||
| pObj->inCallback = 0; | ||
| PyTime_t it = tt - self->subt; | ||
| if (self->previous) | ||
| self->previous->subt += tt; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Although this happens in the
_lsprofmodule, the user interface and the test code are related tocProfile(andprofile), so I think we should move the test case to the test files for those modules.