On Thu, Sep 25, 2025 at 07:36:50PM +0100, Catalin Marinas wrote:
On Thu, Sep 25, 2025 at 06:01:07PM +0100, Mark Brown wrote:
On Thu, Sep 25, 2025 at 05:46:46PM +0100, Catalin Marinas wrote:
On Sun, Sep 21, 2025 at 02:21:35PM +0100, Mark Brown wrote:
We can't have scheduled? That's actually a pleasant surprise, that was the main hole I was thinking of in the cover letter.
Well, double-check. AFAICT, gcs_free() is only called on the exit_mm() path when a thread dies.
I think gcs_free() may have been called in other contexts before the cleanups you had in 6.16 (there were two more call sites for gcs_free()). If that's the case, we could turn these checks into WARN_ON_ONCE().
Yeah, just I need to convince myself that we're always running the exit_mm() path in the context of the exiting thread. Like you say it needs checking but hopefully you're right and the current code is more correct than I had thought.
The only downside is that, if the thread did not unwind properly, we don't write the token where it was initially. We could save the token address from clone3() and restore it there instead.
If we do that and the thread pivots away to another GCS and exits from there then we'll write the token onto a different stack. Writing onto the location that userspace provided when creating the thread should be fine for glibc's needs but it feels like the wrong assumption to bake in, to me it feels less bad to have to map a new GCS in the case where we didn't unwind properly. There will be overhead in doing that but the thread is already exiting uncleanly so imposing a cost doesn't seem disproportionate.
You are right, that's the safest. glibc can always unmap the shadow stack if the thread did not exit properly.
That said, does glibc ensure the thread unwinds its stack (and shadow stack) on pthread_exit()? IIUC, it does, at least for the normal stack, but I'm not familiar with the codebase.
Florian indicated that it did in:
https://marc.info/?l=glibc-alpha&m=175733266913483&w=2
I did look at the code to check, though I'm not at all familiar with the codebase either so I'm not sure how much that check was worth. If the unwinder doesn't handle the shadow stack then userspace will be having a very bad time anyway whenever it tries to run on an unwound stack so doing so shouldn't be an additional cost there.