Skip to content
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

gh-128974: Fix UnicodeError.__str__ when custom attributes have side-effects #128975

Merged
merged 10 commits into from
Mar 1, 2025

Conversation

picnixz
Copy link
Member

@picnixz picnixz commented Jan 18, 2025

@picnixz
Copy link
Member Author

picnixz commented Jan 20, 2025

@vstinner I plan to merge this one with no commit message and the PR's title as the commit title. Do you want me to expand on the issue itself in the commit message or do you think it's fine to leave it in the issue's description? (we can both do a UAF and an invalid type assertion)

@picnixz picnixz marked this pull request as draft January 21, 2025 14:45
@@ -3379,7 +3399,10 @@ UnicodeEncodeError_str(PyObject *self)
if (encoding_str == NULL) {
goto done;
}

// calls to PyObject_Str(...) above might mutate 'exc->object'
if (check_unicode_error_attribute(exc->object, "object", false) < 0) {
Copy link
Member

Choose a reason for hiding this comment

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

IMO this check is done too late. I would prefer to disallow creating inconsistent exception object, and so implement such check in UnicodeEncodeError_init() instead.

Copy link
Member

Choose a reason for hiding this comment

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

The problem is that it's possible to override exc.object in Python. A setter function is needed here to also check when the attribute is set.

Copy link
Member Author

Choose a reason for hiding this comment

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

IMO this check is done too late

Agreed, but I was more worried about the crash itself rather than whether the API was correct in the first place or not. I can add a setter function if you want but I haven't looked at whether this would introduce other corner cases or not and how it would interact with the rest.

@picnixz picnixz marked this pull request as ready for review January 22, 2025 11:50
Copy link
Member

@encukou encukou left a comment

Choose a reason for hiding this comment

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

This is an improvement. I'd merge this PR, and perhaps follow up with an API change later.

@picnixz
Copy link
Member Author

picnixz commented Mar 1, 2025

I'm planning to merge this one, considering I had Petr's approval. While I understand the concern of the API itself, I think this should be addressed separately.

@picnixz picnixz merged commit ddc27f9 into python:main Mar 1, 2025
42 checks passed
@picnixz picnixz deleted the fix/evil/unicode-error-str-128974 branch March 1, 2025 11:01
@picnixz
Copy link
Member Author

picnixz commented Mar 1, 2025

I don't think I'll backport the change because the structure of exceptions.c::UnicodeError* changed in 3.14 as I've refactored them. I can however only backport that fix but I don't think I've backported the other changes.

@encukou
Copy link
Member

encukou commented Mar 3, 2025

For an old bug that was found by code inspection, rather than by real-world use, I think it's fine to not backport.

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.

3 participants