Free on address which was not realloc()-ed in js::GetNativeStackBaseImpl after realloc failure at pthread_getattr_np
Categories
(Core :: JavaScript Engine, defect, P3)
Tracking
()
People
(Reporter: sourc7, Assigned: sfink)
References
(Blocks 1 open bug)
Details
(Keywords: csectype-uninitialized, reporter-external, sec-moderate, Whiteboard: [client-bounty-form][adv-main142+][adv-esr140.2+][adv-esr128.14+])
Attachments
(4 files)
|
2.45 KB,
text/plain
|
Details | |
|
871 bytes,
patch
|
Details | Diff | Splinter Review | |
|
129 bytes,
text/html
|
Details | |
|
48 bytes,
text/x-phabricator-request
|
diannaS
:
approval-mozilla-beta+
RyanVM
:
approval-mozilla-esr128+
RyanVM
:
approval-mozilla-esr140+
|
Details | Review |
While fuzzing on Firefox Nightly 32-bit (Official Build) (BuildID=20250707093546) on Arch Linux (with original glibc), I found from minidump.txt it crash at SIGSEGV / SEGV_ACCERR during execution of js::newContext to free.
However, it still hard to reproduce the same, even after multiple tries.
After looking the code, the crash occurs at NativeStack.cpp in the function js::GetNativeStackBaseImpl:
void* js::GetNativeStackBaseImpl() {
pthread_getattr_np(thread, &sattr); // 1. realloc failure for &sattr inside pthread_getattr_np
...
...
pthread_attr_destroy(&sattr); // 2. free on address which was not realloc()-ed on &sattr
To reliably reproduce this bug, we can use patched glibc to simulate the realloc failure inside pthread_getattr_np(thread, &sattr) to return ENOMEM, leaving the &sattr address not realloc()-ed, then on pthread_attr_destroy it free on &sattr address (which not realloc()-ed) which cause the crash.
Updated•1 year ago
|
Updated•1 year ago
|
| Reporter | ||
Comment 2•1 year ago
|
||
| Reporter | ||
Comment 3•1 year ago
|
||
| Reporter | ||
Comment 4•1 year ago
|
||
Here are the steps for patching glibc to simulate pthread_attr_set_affinity to return ENOMEM, to apply the pthread_attr_setaffinity_simulate_enomem.patch and compile glibc 2.4.1, I'm following the Arch Linux PKGBUILD process.
Steps to reproduce
wget https://ftp.gnu.org/gnu/glibc/glibc-2.41.tar.xztar -xvf glibc-2.41.tar.xz -C /tmpcd /tmp/glibc-2.41- (Download pthread_attr_setaffinity_simulate_enomem.patch to
/tmp/glibc-2.41) patch nptl/pthread_attr_setaffinity.c < pthread_attr_setaffinity_simulate_enomem.patchmkdir build32cd build32export CC="gcc -m32 -mstackrealign" && export CXX="g++ -m32 -mstackrealign"- cat <<EOF > configparms
slibdir=/usr/lib32
rtlddir=/usr/lib32
sbindir=/usr/bin
rootsbindir=/usr/bin
EOF ../configure --prefix=/usr --enable-bind-now --with-headers=/usr/include --enable-kernel=4.4 --enable-multi-arch --enable-systemtap --disable-werror --disable-profile --disable-nscd --host=i686-pc-linux-gnu --libdir=/usr/lib32 --libexecdir=/usr/lib32sudo make -j32 DESTDIR=/var/target-chroot install
(Change /var/target-chroot/ directory to Arch Linux root installation e.g. Virtual Machine .qcow2/.img disk root or Arch Linux pacstrap chroot installation folder)
I recommend to running this directly on Arch Linux virtual machine (e.g. QEMU for simplicity and avoid host OS breakages), so we can install it directly, by changing the DESTDIR=/var/target-chroot to DESTDIR=/
After patched glibc was installed on the system, then visit set-interval-new-worker.forpatch.html on Firefox 32-bit, it will crash at js::NewContext to free
| Reporter | ||
Comment 5•1 year ago
|
||
This similar to my bug 1895081, which realloc failure inside pthread_getattr_np, that cause free on address (which not realloc()-ed) by pthread_attr_destroy
The fix would be add MOZ_RELEASE_ASSERT to check the return value of pthread_getattr_np. If the function fails (e.g., returns ENOMEM due to a memory allocation failure), then it will trigger the MOZ_RELEASE_ASSERT assertion.
| Reporter | ||
Comment 6•1 year ago
|
||
For fix references D212313 - Add extra assertions to nsThread initialization
Updated•1 year ago
|
Updated•1 year ago
|
| Assignee | ||
Comment 7•1 year ago
|
||
Notes to self: pthread_getattr_np calls __pthread_attr_copy calls __pthread_attr_setaffinity_np which does the realloc. But only if the extension field is set and has affinity mask info in extension->cpusetsize.
...but wait, what is setting affinity? I see some pthread_setaffinity_np calls, but they're in dav1d and glsl, neither of which is on a JS-running thread. Perhaps it's different in glibc 2.41? I have glibc 2.35.
Oh... yes, 2.41 is wildly different. It calls __pthread_attr_setaffnity_np unconditionally.
But you're right, given your diagnosis it seems like the fix is pretty straightforward. Now to see who's ok with forced crashes in their code...
| Assignee | ||
Comment 8•1 year ago
|
||
Updated•1 year ago
|
Comment 10•1 year ago
|
||
Updated•1 year ago
|
Comment 11•1 year ago
|
||
Please nominate this for Beta/ESR140/ESR128 approval when you get a chance.
Comment 12•1 year ago
|
||
As a reminder we are in the last week of beta for uplifts!
| Assignee | ||
Comment 13•1 year ago
|
||
Comment on attachment 9502818 [details]
(secure)
Beta/Release Uplift Approval Request
- User impact if declined/Reason for urgency: Honestly, it's hard for me to see a path to user impact. It seems very hard to trigger and about as hard to exploit. It's probably more useful as bulletproofing to aid fuzzers. That said, if I were to try to make use of this, I'd play around with Web Workers since they do the relevant actions while the main thread is sitting around and potentially making use of them.
- Is this code covered by automated tests?: No
- Has the fix been verified in Nightly?: Yes
- Needs manual test from QE?: No
- If yes, steps to reproduce:
- List of other uplifts needed: None
- Risk to taking this patch: Low
- Why is the change risky/not risky? (and alternatives if risky): The change itself is very safe. It forces a crash when the problematic operation fails, and in normal practice, it should never fail (unless your system is severely messed up already).
- String changes made/needed: none
- Is Android affected?: Yes
| Assignee | ||
Comment 14•1 year ago
|
||
Comment on attachment 9502818 [details]
(secure)
ESR Uplift Approval Request
- If this is not a sec:{high,crit} bug, please state case for ESR consideration: Mainly to reduce fuzzing noise (the type of fuzzing that caught this -- fault injection -- is generally useful). It is also just barely possible that this might be exploitable via Web Workers (or some other worker type).
- User impact if declined: see above
- Fix Landed on Version: 143
- Risk to taking this patch: Low
- Why is the change risky/not risky? (and alternatives if risky): It forces a safe crash in the very unlikely event that this fails during regular usage.
Updated•1 year ago
|
Comment 15•1 year ago
|
||
Comment on attachment 9502818 [details]
(secure)
Approved for 142.0b8
Comment 16•1 year ago
|
||
| uplift | ||
Updated•1 year ago
|
Comment 17•1 year ago
|
||
Comment on attachment 9502818 [details]
(secure)
Approved for 140.2esr and 128.14esr.
Updated•1 year ago
|
Comment 18•1 year ago
|
||
| uplift | ||
Updated•1 year ago
|
Comment 19•1 year ago
|
||
| uplift | ||
Updated•1 year ago
|
Updated•1 year ago
|
Updated•11 months ago
|
Updated•3 months ago
|
Description
•