Skip to content

Commit 741ba14

Browse files
gh-143545: Fix UAF in lsprof via re-entrant external timer
1 parent 6c9f7b4 commit 741ba14

File tree

1 file changed

+10
-1
lines changed

1 file changed

+10
-1
lines changed

Modules/_lsprof.c

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ typedef struct _ProfilerContext {
4646
ProfilerEntry *ctxEntry;
4747
} ProfilerContext;
4848

49+
4950
typedef struct {
5051
PyObject_HEAD
5152
rotating_node_t *profilerEntries;
@@ -56,6 +57,7 @@ typedef struct {
5657
double externalTimerUnit;
5758
int tool_id;
5859
PyObject* missing;
60+
int inCallback;
5961
} ProfilerObject;
6062

6163
#define ProfilerObject_CAST(op) ((ProfilerObject *)(op))
@@ -289,6 +291,9 @@ static int freeEntry(rotating_node_t *header, void *arg)
289291

290292
static void clearEntries(ProfilerObject *pObj)
291293
{
294+
if (pObj->inCallback) {
295+
return;
296+
}
292297
RotatingTree_Enum(pObj->profilerEntries, freeEntry, NULL);
293298
pObj->profilerEntries = EMPTY_ROTATING_TREE;
294299
/* release the memory hold by the ProfilerContexts */
@@ -321,13 +326,17 @@ initContext(ProfilerObject *pObj, ProfilerContext *self, ProfilerEntry *entry)
321326
if (subentry)
322327
++subentry->recursionLevel;
323328
}
324-
self->t0 = call_timer(pObj);
329+
pObj->inCallback = 1;
330+
self->t0 = call_timer(pObj);
331+
pObj->inCallback = 0;
325332
}
326333

327334
static void
328335
Stop(ProfilerObject *pObj, ProfilerContext *self, ProfilerEntry *entry)
329336
{
337+
pObj->inCallback = 1;
330338
PyTime_t tt = call_timer(pObj) - self->t0;
339+
pObj->inCallback = 0;
331340
PyTime_t it = tt - self->subt;
332341
if (self->previous)
333342
self->previous->subt += tt;

0 commit comments

Comments
 (0)