Repository navigation
Conversation
Utilise atomic_exchange and `QSBR` to ensure that accessing an old handler does not result in a potential UAF.
|
This looks like the correct fix to me. A few comments:
|
|
One other case is Would it make sense to move the Python handler table into per-interpreter state? Python signal handlers can currently only be installed and run in the main interpreter, so we’d need to decide what |
|
Another approach would be to use a mutex, rather than QSBR. In general it's good to avoid putting more things in the QSBR queue. Also, since the hander is size zero, it doesn't advance the QSBR sequence. This is not on a hot path and so the extra cost of a mutex (which is quite small anyhow) should not be significant. One complication with a mutex, we need to reset it after forking, like the import lock. A third option would be to use tryincref. That gives prompt de-allocation and nothing to do after fork. Neither the mutex or tryincref fixes the Edit: comparison of different options:
|
|
Actually, we don't need the reset after fork, just need to be careful not to be holding the mutex when fork happens. The decref of the old handler must be outside the critical section since a destructor could call fork(). Order would be for writer:
For reader: lock, load, incref, unlock, then call the handler. An incref never runs arbitrary code, so nothing inside either locked region can fork, detach or re-enter. I think I prefer the mutex approach. |
|
Apologies for my description, believe I was meaning to say For the QSBR approach one of the main benefits I saw is that it helps with races with So with the mutex believe we better handle Let me re-visit the mutex approach per your suggestion. Thanks! |
|
I have done another attempt of the mutex approach with #162137 |
Utilise atomic_exchange and
QSBRto ensure that accessing an old handler does not result in a potential UAF. This fixes 2 out of 3 race conditions identified in the issue.Swapping signal handlers can result in a UAF if another thread accesses the old handler slot that could have been freed. On swapping the old reference is given to the caller, however other accessors such as in the signal handler function do not maintain a strong reference. Taking a strong reference there could equally race too.
The proposed solution in the attached issue is to deference only at a quiescent point, this enables lock free access yet callers under most circumstances can be sure the handler won't be freed.
Happy to discuss this further on the parent issue if wider design discussion should be preferred at this stage, this PR is to demonstrate the first proposed solution from the issue.
signalmodule.cin free-threaded Python #161235