Closed Bug 1940326 Opened 1 year ago Closed 1 year ago

Fedora 42 shutdown Crash in [@ __nptl_deallocate_tsd]

Categories

(Core :: Security: Process Sandboxing, defect, P1)

Unspecified
Linux
defect

Tracking

()

RESOLVED FIXED
136 Branch
Tracking Status
firefox-esr115 --- unaffected
firefox-esr128 --- unaffected
firefox134 + wontfix
firefox135 + wontfix
firefox136 + fixed

People

(Reporter: aryx, Assigned: jld)

References

(Regression)

Details

(Keywords: crash, regression, sec-moderate, Whiteboard: [adv-main136+r])

Crash Data

Attachments

(2 external files, 1 obsolete file)

This is the most frequent crash signature submitted for Firefox 134.0 at the moment but submissions started before the release time today. The comments explain this is a shutdown crash. Only Fedora 42 is affected based on the reports.

Crash report: https://crash-stats.mozilla.org/report/index/ba059f9e-a334-471e-a94a-88fc30250107

Reason:

SIGSEGV / SEGV_MAPERR

Top 4 frames:

0  libc.so.6  __GI___nptl_deallocate_tsd  /usr/src/debug/glibc-2.40.9000-26.fc42.x86_64/nptl/nptl_deallocate_tsd.c:54
0  libc.so.6  __nptl_deallocate_tsd  /usr/src/debug/glibc-2.40.9000-26.fc42.x86_64/nptl/nptl_deallocate_tsd.c:22
1  libc.so.6  start_thread  /usr/src/debug/glibc-2.40.9000-26.fc42.x86_64/nptl/pthread_create.c:456
2  libc.so.6  __clone3  /usr/src/debug/glibc-2.40.9000-26.fc42.x86_64/sysdeps/unix/sysv/linux/x86_64/clone3.S:78
OS: Unspecified → Linux
Summary: Fedora 42 startup Crash in [@ __nptl_deallocate_tsd] → Fedora 42 shutdown Crash in [@ __nptl_deallocate_tsd]

My initial reaction was “this looks… bad” and then, as I investigated, it got worse. Here's the line we're crashing on:

void *data = level2[inner].data;

The translation of that line is the crashing instruction, mov rdi, qword [rbx]; strength reduction has been applied such that rbx holds the address of level2[inner].data (data is a void*, so 8-byte aligned), but the value of rbx is 0x00007f0b00002e6f which is not aligned. That value comes from:

level2 = THREAD_GETMEM_NC (self, specific, cnt);

which is defined using inline asm but it's equivalent to self->specific[cnt] (the struct pthread has an array of pointers to arrays of records of pthread_getspecific keys). So, corruption of the contents of struct pthread.

That made me wonder if maybe something went wrong with the tid cache hack we borrowed from Chromium for the sandbox, and 0x2e67 is the tid and we wrote it onto one of those pointers because the layout of struct pthread had changed from what the code expects. (That struct is private to glibc and in theory subject to change without notice, but in practice Chrome has been using this code for many years, and there's also a comment in the glibc source that AddressSanitizer depends on it). The CHECK_EQ calls in that imported function should crash if the layout isn't as expected (CHECK is a release assertion in Chromium code) but we seem to be using a debug assertion instead, so on a release build we might not catch a layout mismatch until glibc tries to use the field we corrupted.

At first I thought I was being too paranoid, but then I tried running gdb on Fedora Rawhide's glibc debuginfo:

(gdb) p sizeof(tcbhead_t)
$1 = 128           ← this used to be 704
(gdb) p/x &(((struct pthread*)0)->tid)
$2 = 0x90         ← this used to be 0x2d0
(gdb) p/x &(((struct pthread*)0)->specific[0])
$3 = 0x2d0        ← this would be where we wrote the tid

(EDIT: the numbers above were previously incorrect; I accidentally used an aarch64 package. These are now correct for x86_64. In particular, the field we're corrupting is (by a remarkable coincidence) specific[0], the only element of the top-level specific array which is typically used, and not specific[15] which would have suggested a TSD key leak.)

The Fedora source RPM has a changelog entry dated 2025-01-02 with an ominous reference to x86_64: Remove unused padding from tcbhead_t, which suggests checking the upstream glibc commit log, and I will quote these two commit messages in full:

commit 30d3fd7f4f4bc8f767d73ad4e4b005c1bd234310
Author: Florian Weimer <fweimer@redhat.com>
Date:   Thu Dec 19 20:56:44 2024 +0100

    x86_64: Remove unused padding from tcbhead_t
    
    This padding is difficult to use for preserving the internal
    GLIBC_PRIVATE ABI.  The comment is misleading.  Current Address
    Sanitizer uses heuristics to determine struct pthread size.
    It does not depend on its precise layout.  It merely scans for
    pointers allocated using malloc.
    
    Due to the removal of the padding, the assert for its start
    is no longer required.

commit a257f201dd557aa82c7efb7528277afe675c0468
Author: Florian Weimer <fweimer@redhat.com>
Date:   Tue Jan 7 08:34:13 2025 +0100

    Revert "x86_64: Remove unused padding from tcbhead_t"
    
    This reverts commit 30d3fd7f4f4bc8f767d73ad4e4b005c1bd234310.
    
    The padding is required by Chromium's MaybeUpdateGlibcTidCache
    in sandbox/linux/services/namespace_sandbox.cc.

I'll investigate more to find out what we need to do here.

Assignee: nobody → jld
Component: Widget: Gtk → Security: Process Sandboxing
Keywords: regression
Priority: -- → P1
Regressed by: 1896054

From the Fedora bug: there's a glibc patch under discussion which would avoid the need for the tid cache hack by updating the tid cache automatically if it's safe to do so (if the VM space isn't shared with any other task, which covers our/Chromium's “fork with flags” use case). This wouldn't require us (or Chromium) to change anything, because the code already reads the cached tid in a way that doesn't depend on the struct pthread layout, and doesn't do anything else if the cached and real tids already match. As proposed, the patch is for x86_64 only.

There's also a thread on chromium-dev. There are some mentions of glibc possibly adding a clone_fork in the future, which I would gladly use instead of what we're currently doing (we'd need to dynamically detect it, but that should be doable). Also, we are on notice that longjmping out of the clone callback may be desupported in the future (clone_fork would also remove the need for that).

It gets worse. I didn't understand why we were seeing this bug with glibc-2.40.9000-26.fc42.x86_64 which should have the fix, so I disassembled some binaries, and it's as if the body of GetGlibcCachedTid is being optimized to return 0, but only on non-debug builds.

But of course it is: with CHECK_EQ(…) defined to nothing, the calls to pthread_mutex_{lock,unlock,destroy} are deleted, the tid is never written to to the lock, and the compiler is entirely correct that the value returned would always be 0.

Yet another complication: if I fix the definition of CHECK_EQ, and then intentionally use incorrect offsets to verify the fix, it does (as expected) crash all child processes at launch… but it doesn't produce crash reports. In fact, we may not be able to produce crash reports at this point. And the assertion failure doesn't write to stderr, either. And the first tab opened is just blank, but additional tabs do show a tab-crashed page (minus the option to submit a report) so at least the user is likely to understand that it is a crash. We can probably live with that?

And the assertion failure doesn't write to stderr, either

Probably another instance of https://bugzilla.mozilla.org/show_bug.cgi?id=1911044 which could use a slightly reworded title.

See Also: → 1911044

This might exploitable. If the attacker can heap-spray the target of the corrupt pointer, they only have to guess the low 3 bits of the tid (⅛ chance) to control the arguments of the TSD destructors — only the arguments and not the function pointers, but in several cases the argument is a heap-allocated pointer which is freed or deleted, so it may be possible to escalate to code execution (or at least to a controlled memory write).

Group: dom-core-security

I notice that the crash report in comment #0 is for the parent process, which is correct and reflects another bug: the tid cache hack is also called in the parent process, on the thread that called clone. Because of the issue in comment #3, it doesn't matter that the real cached tid is correct; the value of pthread_self()->specific[0] is corrupted unconditionally.

So to break down the various cases:

  1. Fork server child process main threads are affected, but on a non-debug build they _exit during shutdown and never run TSD destructors. (The fork server is preffed off on non-Nightly builds, but some downstream distributions enable it, e.g. Pop! OS.)
  2. The parent process's IPC Launch thread is affected. If the fork server is enabled at compile time, which it is by default, then currently that is a dedicated thread which doesn't exit until browser shutdown.
  3. If the fork server is disabled in the build config, then IPC Launch is a thread pool and in practice this causes the browser to crash on startup. I don't fully understand what's going on there, but an unusable browser isn't likely to be exploited, and it's not clear if any downstreams even build in that configuration.
  4. In the non-fork-server launch path, the child process and the chroot helper process are single-threaded and that thread's is TCB corrupted, but they don't use TSD (or, in theory, anything that's non-signal-safe) and terminate either by exec or _exit so TSD destruction never happens.

I believe this affects only the code that runs TSD destructors when a thread exits: the storage is arranged as 32 blocks of 32 keys each, with the first block stored inline in the TCB and the others heap-allocated if needed, so specific[0] points back into the TCB, and because there are almost always ≤32 keys in typical usage, pthread_{get,set}specific optimize that case by accessing specific_1stblock directly to avoid an extra dependent load, which means they never see the bad pointer.

So, I think this affects only the parent process, and only at browser shutdown. That seems difficult to properly exploit from Web content, but maybe not impossible. An attacker with control over a sandboxed content process might be able to exploit it more reliably, as a sandbox escape.

For clarity, this only impacts an unreleased development version of glibc (and Fedora). I should be able to revert the glibc in Fedora rawhide soon enough.

I don't know what this means for future glibc changes in this area because of the unconditional nature of the TCB patching in the Firefox variant. Extracting the cached TID with a recursive mutex is a bit brittle, too. Using pthread_getcpuclockid could make this completely reliable (the TID→clockid_t encoding is reversible and proscribed by the kernel ABI).

Pushed by jedavis@mozilla.com: https://hg.mozilla.org/integration/autoland/rev/37a22978fa22 Fix the glue for some imported Chromium code. r=gcp https://hg.mozilla.org/integration/autoland/rev/bd666f49dd1b Clean up the control flow in ForkWithFlags. r=gcp https://hg.mozilla.org/integration/autoland/rev/cfc6b506c476 apply code formatting via Lando

(In reply to Florian Weimer from comment #10)

I don't know what this means for future glibc changes in this area because of the unconditional nature of the TCB patching in the Firefox variant.

That was unintentional. Once the patches on this bug land, Firefox should behave the same way as Chromium.

(In reply to Jed Davis [:jld] ⟨⏰|UTC-8⟩ ⟦he/him⟧ from comment #12)

(In reply to Florian Weimer from comment #10)

I don't know what this means for future glibc changes in this area because of the unconditional nature of the TCB patching in the Firefox variant.

That was unintentional. Once the patches on this bug land, Firefox should behave the same way as Chromium.

I understood that. However, if we clean this up on the glibc side, older Firefox binaries will stop working. But maybe this is something that can be covered by snaps/flatpaks these days, and we can make the switch on the glibc side once the change has been in an ESR release or two.

Group: dom-core-security → core-security-release
Status: NEW → RESOLVED
Closed: 1 year ago
Resolution: --- → FIXED
Target Milestone: --- → 136 Branch

The patch landed in nightly and beta is affected.
:jld, is this bug important enough to require an uplift?

  • If yes, please nominate the patch for beta approval.
  • If no, please set status-firefox135 to wontfix.

For more information, please visit BugBot documentation.

Flags: needinfo?(jld)

(In reply to Florian Weimer from comment #13)

I understood that. However, if we clean this up on the glibc side, older Firefox binaries will stop working. But maybe this is something that can be covered by snaps/flatpaks these days, and we can make the switch on the glibc side once the change has been in an ESR release or two.

Ah, I see. This regressed in 130, the fix is in 136 (and hopefully 135), and the ESRs are 128 and 140, so this bug won't be in any ESR. There is some usage of out-of-date release versions that I've seen in crash stats and telemetry, even though it's officially not supported. I could do some research to see how quickly that tends to drop off over time, but it's possible that those users aren't updating their OSes promptly either, in which case they also wouldn't have a new glibc.

The more distant history: From 60 through 129 we just had processes with the wrong cached tid for their main thread; they did longjmp out of the clone callback but they did not attempt to touch struct pthread internals. Originally those processes would exec or _exit without doing anything pthreads-related (including raise) so the tid mismatch didn't matter and I didn't know it was happening, but recent progress towards shipping something similar to Chromium's zygote has complicated the situation.

I'll request beta uplift once this has had another day or two on Nightly.

Attachment #9459304 - Flags: approval-mozilla-beta?
Attachment #9459304 - Attachment is obsolete: true
Attachment #9459304 - Flags: approval-mozilla-beta?

I decided not the file for uplift, because at the moment (now that glibc backed out the change to tcbhead_t and Fedora Rawhide shipped the backout last Thursday in 2.40.9000-27) approximately nobody is actually affected by the bug, as I understand it.

If it would help glibc development to get the fix out to users 4 weeks sooner I could try to ask for uplift, but it's hard to make a case for it under the current circumstances.

Flags: needinfo?(jld)
See Also: → 1941981
QA Whiteboard: [post-critsmash-triage]
Flags: qe-verify-
Whiteboard: [adv-main136+r]
Group: core-security-release
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: