Uh oh!
There was an error while loading.Please reload this page.
- Notifications
You must be signed in to change notification settings - Fork33.7k
gh-125789: fix side-effects inasyncio callback scheduling methods#125833
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
Uh oh!
There was an error while loading.Please reload this page.
Conversation
Nico-Posada commentedOct 22, 2024
You can still trigger a use after free with these changes since the tuple item never gets incref'd which allows you to abuse importasynciofut=asyncio.Future()classsetup:def__eq__(self,other):print("in setup __eq__")returnFalsedef__del__(self):print("deleting self")cb_pad=lambda: ...fut.add_done_callback(cb_pad)# sets fut->fut_callback0fut.add_done_callback(setup())# sets fut->fut_callbacks[0]# removes callback from fut->fut_callback0 setting it to NULLfut.remove_done_callback(cb_pad)# evil MUST be a subclass of setup so that the evil __eq__ gets called firstclassevil(setup):def__eq__(self,value):fut._callbacks.clear()print("in evil __eq__")returnNotImplementedfut.remove_done_callback(evil()) |
picnixz commentedOct 22, 2024
Oups, I noticed that there was already a UAF in the code but I forgot to fix it. Thanks! |
gvanrossum left a comment
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.
Are you missing a file? This seems to only change tests.
picnixz commentedOct 22, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Err... the |
gvanrossum left a comment
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.
LGTM, I just have some minor rewordings for some comments and messages to suggest.
Uh oh!
There was an error while loading.Please reload this page.
Misc/NEWS.d/next/Library/2024-10-22-12-47-46.gh-issue-125789.Hk885p.rst OutdatedShow resolvedHide resolved
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Nico-Posada commentedOct 22, 2024
One more UAF needs to be patched out, it's possible to corrupt fut_callback0 then do the NotImplemented trick to use callback0 after it has been freed. Just needs an incref before usage. cpython/Modules/_asynciomodule.c Lines 1019 to 1021 inaaed91c
POC importasynciofut=asyncio.Future()classa:def__eq__(self,other):print("in a __eq__",self)returnTruedef__del__(self):print("deleting",self)classb(a):def__eq__(self,other):print("in b __eq__")fut.remove_done_callback(None)returnNotImplementedfut.add_done_callback(a())fut.remove_done_callback(b()) |
1st1 left a comment
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.
Looks good overall.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
Uh oh!
There was an error while loading.Please reload this page.
picnixz commentedOct 23, 2024
Thanks Nico for your insight and others for the review. I appreciate you taking the time to hunt those UAF so if you want to have a look at other usages of Py_EQ, I'd be happy to review/write the corresponding PRs. I'll address the other comments in a few hours! |
bedevere-bot commentedOct 23, 2024
Modules/_asynciomodule.c Outdated
| return0; | ||
| } | ||
| if (!PyList_CheckExact(fut->fut_callbacks)) { |
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.
Is there a test for this? Is there a way to assign a non-list to fut._callbacks?
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.
Mmh, actually no. The property is read-only and even with subclassing it's not possible. I'll replace the list checks by assertions.
EDIT: NVM, the macros PyList_GET* macros already take care of the assertion.
graingert commentedOct 23, 2024
What happens if fut._callbacks is mutated from another thread on 3.13t? |
picnixz commentedOct 23, 2024
Err... I don't know. I haven't thought about this. I think it should be addressed in a follow-up PR though. |
picnixz commentedOct 24, 2024
It might be easier but I don't know how it could affect performances. Asyncio experts need to confirm whether this could be a solution or not. For instance, we could return atuple instead of a list, and this would at least avoid people using Btw, there are some inconsistencies between the Python and the C implementation for that. When we don't have any callback, the C implementation returns None but the Python implementation returns an empty list. Should it be changed? |
graingert commentedOct 24, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
It would be very rare to read fut._callbacks and it only seems to make sense in tests so performance of that branch is a non-issue. Adding extra checks to add_done_callback and schedule_callbacks is performance sensitive. I think also that it would be more consistent to always return a copy. Perhaps PyFuture could also return a copy using a I also suspect a copy is the only way to fix this on 3.13t without adding extra locks |
Uh oh!
There was an error while loading.Please reload this page.
picnixz commentedOct 24, 2024
Actually, if (fut->fut_callback0==NULL) {if (fut->fut_callbacks==NULL) {Py_RETURN_NONE; }returnPy_NewRef(fut->fut_callbacks);// here } So we could also do something like PyObject*new_list=PyList_New(PyList_GET_SIZE(fut->fut_callbacks));if (new_list==NULL) {returnNULL; }for (i=0;i<PyList_GET_SIZE(fut->fut_callbacks);i++) {PyObject*cb=PyList_GET_ITEM(fut->fut_callbacks,i);Py_INCREF(cb);PyList_SET_ITEM(new_list,i,cb); }returnnew_list; instead and the problem is likely to be gone? |
picnixz commentedOct 24, 2024
We likely still have the UAF issue on |
Nico-Posada left a comment
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.
A couple ideas.
| returnNULL; | ||
| } | ||
| Py_INCREF(cb_tup); | ||
| PyObject*cb=PyTuple_GET_ITEM(cb_tup,0); |
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.
A little nitpicky, but the incref should be oncb notcb_tup. We don't necessarily care if the tuple gets deleted in PyObject_RichCompareBool, we care ifcb gets deleted. This technically works because I don't think there's a way to deletecb at this point if the tuple can't be deleted, but it's still worth noting.
| gotofail; | ||
| } | ||
| Py_INCREF(cb_tup); | ||
| PyObject*cb=PyTuple_GET_ITEM(cb_tup,0); |
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.
Same thing here, I don't think it's possible to deletecb once the tuple has been incref'd, but if it's somehow possible to replace the first item in the tuple thencb will be freed possibly causing a UAF.
kumaraditya303 left a comment
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.
We don't need all this complexity and type checks, I proposed an alternative at#125922
A Python core developer has requested some changes be made to your pull request before we can consider merging it. If you could please address their requests along with any other requests in other reviews from core developers that would be appreciated. Once you have made the requested changes, please leave a comment on this pull request containing the phrase |
picnixz commentedOct 24, 2024
@kumaraditya303 While I understand that you're an asyncio maintainer, I would have appreciated that you first asked whether you wanted me to make the modifications here instead of plainly requesting changes and opening another PR. Nevertheless, I'm not sure that returning a copy entirely fixes the second UAF, in which case we can split the work into two (I also don't know which kind of "changes" you'd like to see here). |
kumaraditya303 commentedOct 24, 2024
Please try to fix one issue in one PR. As far as I see, this PR has several changes, it change some pure python code, it tries to fix use-after-free issue related to malicious call_soon which is unrelated to issue, it would be better to fix that separately. Also this PR takes a different route to fix the issue by adding several type checks, which bulks out the code so I proposed an alternative to this in a separate PR. Also since this bug exists in older versions, we should aim for the least invasive change and keep smaller PRs to be easily able to backport them. I would appreciate if you create an issue of UAF issues related to scheduling and create an PR specifically fixing that, which I can easily backport. Thanks |
picnixz commentedOct 24, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
Thanks. Yet, in the future, I would then appreciate that you mention those points first. For the UAF, I'll just wait that your change has been merged until creating a new one and extract the tests and PoCs. |
kumaraditya303 commentedOct 24, 2024
Okay, I'll take care of that next time, in the mean time you can review my PR |
picnixz commentedOct 24, 2024 • edited
Loading Uh oh!
There was an error while loading.Please reload this page.
edited
Uh oh!
There was an error while loading.Please reload this page.
(I'll be back on my dev env tomorrow but I'll review this evening/tomorrow) |
Uh oh!
There was an error while loading.Please reload this page.
This is a proposal for fixing the side-effects that could arise from
Py_EQ. Similar to the patch where I fixedOrderedDict.__eq__, I did not modify the pure Python implementation but I can do it (I don't think we want to align both implementations; we just don't want the interpreter to crash and I don't think we can make it crash using the pure Python implementation only).