Skip to content

bpo-34408: Prevent a null pointer derefence and resource leakage in pystate.c#8767

Merged
pablogsal merged 5 commits into
python:masterfrom
pablogsal:bpo34408
Aug 31, 2018
Merged

bpo-34408: Prevent a null pointer derefence and resource leakage in pystate.c#8767
pablogsal merged 5 commits into
python:masterfrom
pablogsal:bpo34408

Conversation

@pablogsal

@pablogsal pablogsal commented Aug 14, 2018

Copy link
Copy Markdown
Member

Comment thread Python/pystate.c
"failed to get an interpreter ID");
/* XXX deallocate! */
interp = NULL;
PyMem_RawFree(interp);

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

_PyRuntime.interpreters.head (and possibly _PyRuntime.interpreters.main) must be reverted too.

Alternately, the ID-related code could be moved to before the head/main stuff (i.e. right after the HEAD_LOCK(); line).

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think moving the ID-related code before the HEAD_LOCK is more clear.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry for the confusion here. The ID code needs to be protected by the lock, so HEAD_LOCK() should move to right above the if (_PyRuntime.interpreters.next_id < 0) { line (i.e. move line 184 of the changed file to right before line 174). Otherwise, moving the head/main stuff down after the ID stuff looks good.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FWIW, I don't recall why I originally put the ID-related code after the the head/main code. I vaguely remember there was a reason (and wish I had left a comment). Regardless, at this point I do not see any reason why putting the ID-related code would be a problem. :)

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is not going to be a problem if we return in the middle of the HEAD_LOCK() - HEAD_UNLOCK() macros?

@pablogsal pablogsal Aug 15, 2018

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In commit b48d5bd I changed the code to return outside the macro blocks and protect the ID stuff and HEAD stuff with the HEAD_LOCK().

@bedevere-bot

Copy link
Copy Markdown

When you're done making the requested changes, leave the comment: I have made the requested changes; please review again.

@pablogsal

Copy link
Copy Markdown
Member Author

I have made the requested changes; please review again

@bedevere-bot

Copy link
Copy Markdown

Thanks for making the requested changes!

@ericsnowcurrently: please review the changes made to this pull request.

@pablogsal

Copy link
Copy Markdown
Member Author

I have made the requested changes; please review again

@bedevere-bot

Copy link
Copy Markdown

Thanks for making the requested changes!

@ericsnowcurrently: please review the changes made to this pull request.

@ericsnowcurrently

Copy link
Copy Markdown
Member

LGTM

Thanks for working on this! The chance of this being a problem is super remote, but I'm glad this is resolved. :)

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For the NEWS entry, I would prefer to mention PyInterpreterState_New() than pystate.c.

@pablogsal

Copy link
Copy Markdown
Member Author

Done in 9dda7fb

@vstinner vstinner left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM.

@@ -0,0 +1 @@
Prevent a null pointer derefence and resource leakage in `PyInterpreterState_New()`.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Use double backquotes, or

:c:func:`PyInterpreterState_New`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, there's a typo: "derefence" should be "dereference".

Comment thread Python/pystate.c Outdated
}
HEAD_UNLOCK();

if (!interp){

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't comply with PEP 7.

@pablogsal pablogsal merged commit 95d630e into python:master Aug 31, 2018
@pablogsal pablogsal deleted the bpo34408 branch August 31, 2018 21:49
pablogsal added a commit to pablogsal/cpython that referenced this pull request May 10, 2019
…age in `PyInterpreterState_New()` (pythonGH-8767)

* A pointer in `PyInterpreterState_New()` could have been `NULL` when being dereferenced.

* Memory was leaked in `PyInterpreterState_New()` when taking some error-handling code path.
(cherry picked from commit 95d630e)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
pablogsal added a commit that referenced this pull request May 10, 2019
…age in `PyInterpreterState_New()` (GH-8767) (GH-13237)

* A pointer in `PyInterpreterState_New()` could have been `NULL` when being dereferenced.

* Memory was leaked in `PyInterpreterState_New()` when taking some error-handling code path.
(cherry picked from commit 95d630e)

Co-authored-by: Pablo Galindo <Pablogsal@gmail.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants