Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

gh-130151: Fix reference leaks in_hashlib.hmac_{new,digest}#130152

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Merged
gpshead merged 9 commits intopython:mainfrompicnixz:fix/hmac/leak-130151
Feb 24, 2025

Conversation

@picnixz
Copy link
Member

@picnixzpicnixz commentedFeb 15, 2025
edited by bedevere-appbot
Loading

@encukou
Copy link
Member

Thanks, good catch!

Why reorganize the code, though?
I see no reason to remove the arg name comment/*impl*/, or renamer.

AFAICS, declaring pointers at the top and setting them toNULL, and anerror: block at the end, was deliberate; the style this function was going for is keepingctx andself non-NULL when they need to be freed. IMO, that's a good way to make complex functions maintainable: repeating a list of currently owned “resources” before eachreturn works for smaller functions, but I thinkhmac_new is past that size.
I'd expect aPy_XDECREF(self) in the error block, and a line likectx = NULL; // context is now owned by self.

Not sure about the change from_setException toPyErr_NoMemory -- the OpenSSL docs onHMAC_CTX_new andHMAC_Init_ex aren't explicit on whether it sets anERR_ error. Are they inconsistent?

serhiy-storchaka reacted with thumbs up emoji

@picnixz
Copy link
MemberAuthor

Not sure about the change from _setException to PyErr_NoMemory -- the OpenSSL docs on HMAC_CTX_new and

I think it's to align with other instances of similar code. In other cases I think we raise a MemoryError. But I need to check it.

Why reorganize the code, though?
I see no reason to remove the arg name comment /impl/, or rename r.

I think the impl comment would be implicit but it's my IDE, I'll restore it. As forr I think I wanted to reduce the kjne length :') I'll just revert this line.

AFAICS, declaring pointers at the top and setting them to NULL, and an error: block at the end, was deliberate

For me it was more an ANSI C approach where all variables are declared at the top of the function. However since I felt it was cleaner like this. But I can revert it and make it ANSI C.

@picnixz
Copy link
MemberAuthor

picnixz commentedFeb 22, 2025
edited
Loading

ConcerningPyErr_NoMemory, that's what we do inhmac.digest().NULL is returned byHMAC_CTX_new when allocation fails:https://github.com/openssl/openssl/blob/0bdd10e4078beccaa49ea015b6660f3facfab02b/crypto/hmac/hmac.c#L162.

Note that we also return NULL if the context cannot be reset correctly, but this only means that one the underlying EVP contexts couldn't be allocated either:https://github.com/openssl/openssl/blob/0bdd10e4078beccaa49ea015b6660f3facfab02b/crypto/hmac/hmac.c#L198 andhttps://github.com/openssl/openssl/blob/0bdd10e4078beccaa49ea015b6660f3facfab02b/crypto/evp/digest.c#L129.

So the NULL should only be returned byHMAC_CTX_new if either it couldn't be allocated or one of the underlying EVP contexts couldn't be allocated.

EDIT: This is assuming we are usingopenssl implementation forlibssl. Any other implementation may not necessarily do the same, but I'm not sure we're actually building against something that is notopenssl-compliant.

@gpsheadgpsheadenabled auto-merge (squash)February 23, 2025 23:21
@gpsheadgpshead added needs backport to 3.12only security fixes needs backport to 3.13bugs and security fixes labelsFeb 23, 2025
@gpsheadgpshead merged commit0718201 intopython:mainFeb 24, 2025
46 checks passed
@miss-islington-app
Copy link

Thanks@picnixz for the PR, and@gpshead for merging it 🌮🎉.. I'm working now to backport this PR to: 3.12, 3.13.
🐍🍒⛏🤖

miss-islington pushed a commit to miss-islington/cpython that referenced this pull requestFeb 24, 2025
…ythonGH-130152)* fix leak in `_hashlib.hmac_new`* fix leak in `hmac_digest`* fix exception type in `_hashlib.HMAC.copy`(cherry picked from commit0718201)Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@bedevere-app
Copy link

GH-130491 is a backport of this pull request to the3.13 branch.

@miss-islington-app
Copy link

Sorry,@picnixz and@gpshead, I could not cleanly backport this to3.12 due to a conflict.
Please backport usingcherry_picker on command line.

cherry_picker 071820113f11b8f6a21f98652d0840e10268114c 3.12

@bedevere-appbedevere-appbot removed the needs backport to 3.13bugs and security fixes labelFeb 24, 2025
@picnixz
Copy link
MemberAuthor

I'll take care of the bp tomorrow (it's 1 AM here) if no one is doing it

encukou reacted with heart emoji

@picnixzpicnixz deleted the fix/hmac/leak-130151 branchFebruary 24, 2025 00:12
gpshead pushed a commit that referenced this pull requestFeb 24, 2025
…GH-130152) (#130491)gh-130151: Fix reference leaks in `_hashlib.hmac_{new,digest}` (GH-130152)* fix leak in `_hashlib.hmac_new`* fix leak in `hmac_digest`* fix exception type in `_hashlib.HMAC.copy`(cherry picked from commit0718201)Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
@picnixz
Copy link
MemberAuthor

Huh, I forgot. I didn't have time this morning. I'll do it... well, either this night or tomorrow. Sorry :(

@picnixzpicnixz self-assigned thisFeb 24, 2025
@bedevere-app
Copy link

GH-130539 is a backport of this pull request to the3.12 branch.

@bedevere-appbedevere-appbot removed the needs backport to 3.12only security fixes labelFeb 25, 2025
picnixz added a commit that referenced this pull requestFeb 25, 2025
…GH-130152) (#130539)gh-130151: Fix reference leaks in `_hashlib.hmac_{new,digest}` (GH-130152)* fix leak in `_hashlib.hmac_new`* fix leak in `hmac_digest`* fix exception type in `_hashlib.HMAC.copy`(cherry picked from commit0718201)
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@gpsheadgpsheadAwaiting requested review from gpsheadgpshead is a code owner

@encukouencukouAwaiting requested review from encukou

@tirantiranAwaiting requested review from tiran

Assignees

@gpsheadgpshead

@picnixzpicnixz

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

3 participants

@picnixz@encukou@gpshead

[8]ページ先頭

©2009-2025 Movatter.jp