Closed Bug 2042730 Opened 5 months ago Closed 5 months ago

Unhandled Woken futex state in [@ js::FutexThread::wait] precondition assertion via re-entrant Atomics.wait from an interrupt handler

Categories

(Core :: JavaScript: Standard Library, defect)

defect

Tracking

()

RESOLVED DUPLICATE of bug 2042727

People

(Reporter: bugmon, Unassigned)

References

Details

(4 keywords)

Attachments

(3 files)

js::FutexThread::wait in js/src/builtin/AtomicsObject.cpp begins with the precondition MOZ_ASSERT(state_ == Idle || state_ == WaitingInterrupted) (line 1702) and a re-entrancy guard (lines 1707-1712) that only special-cases the WaitingInterrupted state. The FutexState enum, however, also contains a Woken state, which the precondition does not allow.

The engine can re-enter wait() with state_ == Woken: a thread blocked in Atomics.wait is interrupted by an urgent JS interrupt (JSContext::requestInterrupt(CallbackUrgent) calls fx.notify(NotifyForJSInterrupt) in Runtime.cpp), which drives the futex state Waiting -> WaitingNotifiedForInterrupt -> WaitingInterrupted and then runs the JS interrupt callback via cx->handleInterrupt() at AtomicsObject.cpp:1810. If that callback calls Atomics.notify on the same memory location the outer thread is waiting on, atomics_notify_impl finds the still-listed SyncFutexWaiter and calls FutexThread::notify(NotifyExplicit), which (because state_ == WaitingInterrupted) sets state_ = Woken (lines 1828-1832). If the interrupt callback then re-enters Atomics.wait (a nested wait), FutexThread::wait is entered again with state_ == Woken, which is not handled by the guard and trips the assertion at line 1702.

In debug builds this is a deterministic assertion failure / crash. In release/ASAN builds (assertion compiled out) the nested wait overwrites state_ (-> Waiting, then -> Idle via the MakeScopeExit at line 1715), destroying the outer wait's Woken signal, so the outer Atomics.wait loses its wakeup and returns "timed-out" instead of "ok" -- an observable corruption of the futex synchronization primitive.

Build Info

Affected Code

File: js/src/builtin/AtomicsObject.cpp, line 1697-1712

FutexThread::WaitResult js::FutexThread::wait(
    JSContext* cx, js::UniqueLock<js::Mutex>& locked,
    const mozilla::Maybe<mozilla::TimeDuration>& timeout) {
  MOZ_ASSERT(&cx->fx == this);
  MOZ_ASSERT(cx->fx.canWait());
  MOZ_ASSERT(state_ == Idle || state_ == WaitingInterrupted);  // <-- fails when state_ == Woken

  // Disallow waiting when a runtime is processing an interrupt.
  // See explanation below.

  if (state_ == WaitingInterrupted) {       // <-- guard only handles WaitingInterrupted, not Woken
    UnlockGuard unlock(locked);
    JS_ReportErrorNumberASCII(cx, GetErrorMessage, nullptr,
                              JSMSG_ATOMICS_WAIT_NOT_ALLOWED);
    return WaitResult::Error;
  }

File: js/src/builtin/AtomicsObject.cpp, line 1807-1817

        state_ = WaitingInterrupted;
        {
          UnlockGuard unlock(locked);
          if (!cx->handleInterrupt()) {   // re-enters JS; interrupt handler can notify + nested-wait
            return WaitResult::Error;
          }
        }
        if (state_ == Woken) {
          return WaitResult::OK;
        }
        break;

File: js/src/builtin/AtomicsObject.cpp, line 1825-1846

void js::FutexThread::notify(NotifyReason reason) {
  MOZ_ASSERT(isWaiting());

  if ((state_ == WaitingInterrupted || state_ == WaitingNotifiedForInterrupt) &&
      reason == NotifyExplicit) {
    state_ = Woken;   // <-- transitions an interrupted waiter to Woken
    return;
  }
  switch (reason) {
    case NotifyExplicit:
      state_ = Woken;
      break;
    ...
  }
  cond_->notify_all();
}

The wait() precondition and re-entrancy guard do not account for the Woken state, while notify() can legitimately set Woken during interrupt handling, and the interrupt handler can re-enter wait().

Exploit Chain

  1. Main thread calls Atomics.wait(ta, 0, 0, longTimeout) on a SharedArrayBuffer and blocks; FutexThread state_ = Waiting and a stack-allocated SyncFutexWaiter is linked into the buffer's waiter list.
  2. An urgent JS interrupt is requested (shell watchdog timeout(...) -> JS_RequestInterruptCallback -> JSContext::requestInterrupt(CallbackUrgent) -> fx.notify(NotifyForJSInterrupt)); the blocked futex wakes and state_ transitions Waiting -> WaitingNotifiedForInterrupt -> WaitingInterrupted.
  3. FutexThread::wait calls cx->handleInterrupt() (AtomicsObject.cpp:1810), which runs the JS interrupt callback (in --fuzzing-safe this is a string evaluated in a fresh global).
  4. The interrupt callback calls Atomics.notify on the same location; atomics_notify_impl finds the still-listed SyncFutexWaiter and calls fx.notify(NotifyExplicit), which sets state_ = Woken because state_ == WaitingInterrupted.
  5. The interrupt callback then re-enters Atomics.wait on the same (value-matching) location; FutexThread::wait is entered with state_ == Woken.
  6. The precondition MOZ_ASSERT(state_ == Idle || state_ == WaitingInterrupted) at line 1702 fails -> crash (debug). In release the nested wait clobbers state_, so the outer Atomics.wait loses its Woken wakeup and returns "timed-out" instead of "ok".

Steps to Reproduce

  1. Build/use the SpiderMonkey shell (debug build for the assertion: /firefox/obj-js-debug/dist/bin/js). The js_shell_evaluator runs with --fuzzing-safe.
  2. Run testcase.js: it creates a SharedArrayBuffer, shares it via setSharedObject, registers a string interrupt handler that does getSharedObject()/Atomics.notify/Atomics.wait, arms a 1-second watchdog via timeout(1.0, handler), then performs a blocking Atomics.wait(ta, 0, 0, 30000).
  3. After ~1 second the watchdog interrupt fires while the main thread is blocked in Atomics.wait; the handler notifies the outer waiter (state -> Woken) and re-enters Atomics.wait, which trips the assertion at AtomicsObject.cpp:1702.
  4. Observe the crash: 'Assertion failure: state_ == Idle || state_ == WaitingInterrupted, at js/src/builtin/AtomicsObject.cpp:1702' with FutexThread::wait at the top of the stack.

Security Impact

  • Severity: Low
  • Attacker capability: An attacker who can run JS that performs a blocking Atomics.wait and arrange for an urgent interrupt whose handler touches the same SharedArrayBuffer (notify + nested wait) drives the FutexThread state machine into an invalid (Woken) state at wait() entry. In debug builds this is a deterministic assertion crash; in shipping/release builds the assertion is compiled out and the bug corrupts the futex synchronization state, causing the outer Atomics.wait to drop a wakeup and return 'timed-out' instead of 'ok' -- i.e. a synchronization-correctness violation / potential lost-wakeup that can stall a waiting agent. It is not a memory-safety primitive in release builds.
  • Preconditions: Requires JS able to call Atomics.wait (canWait, e.g. a worker or, in the shell, the main thread) on a shared buffer and to run code from within an interrupt handler that calls Atomics.notify on the in-progress waiter followed by a nested Atomics.wait. In the --fuzzing-safe shell this is reached via timeout()/setInterruptCallback (string handler in a fresh global) plus setSharedObject/getSharedObject; in the browser, interrupt handlers do not normally re-enter content JS in this way, limiting web-content reachability.

ASAN Report

Assertion failure: state_ == Idle || state_ == WaitingInterrupted, at js/src/builtin/AtomicsObject.cpp:1702
UndefinedBehaviorSanitizer:DEADLYSIGNAL
==ERROR: UndefinedBehaviorSanitizer: SEGV on unknown address 0x000000000000
  #0 MOZ_CrashSequence(void*, long) mozilla/Assertions.h:261:3
  #1 js::FutexThread::wait(...) js/src/builtin/AtomicsObject.cpp:1702:3
  #2 AtomicsWait<int>(...) js/src/builtin/AtomicsObject.cpp:1260:43
  #3 js::atomics_wait_impl(...) js/src/builtin/AtomicsObject.cpp:1270:10
  #4 DoAtomicsWait<int>(...) js/src/builtin/AtomicsObject.cpp:1350:11
  #5 DoWait(...) js/src/builtin/AtomicsObject.cpp:1402:12
  #6 atomics_wait(...) js/src/builtin/AtomicsObject.cpp:1429:10
  ... CallJSNative / js::Interpret ...
  #17 EvaluateInner(...) js/src/shell/js.cpp:3173:32   (interrupt handler string)
  #18 ShellInterruptCallback(JSContext*) js/src/shell/js.cpp:1236:12
  #19 InvokeInterruptCallbacks(JSContext*) js/src/vm/Runtime.cpp:386:10
  #20 HandleInterrupt(JSContext*, bool, bool) js/src/vm/Runtime.cpp:425:8
  #21 js::FutexThread::wait(...) js/src/builtin/AtomicsObject.cpp:1810:20   (outer wait handleInterrupt)
  #22 AtomicsWait<int>(...) js/src/builtin/AtomicsObject.cpp:1260:43
  #23 js::atomics_wait_impl(...) js/src/builtin/AtomicsObject.cpp:1270:10
  #24 DoAtomicsWait<int>(...) js/src/builtin/AtomicsObject.cpp:1350:11
  #25 DoWait(...) js/src/builtin/AtomicsObject.cpp:1402:12
  #26 atomics_wait(...) js/src/builtin/AtomicsObject.cpp:1429:10
  ... outer Atomics.wait from top-level script ...
SUMMARY: UndefinedBehaviorSanitizer: SEGV mozilla/Assertions.h:261:3 in MOZ_CrashSequence(void*, long)
Attached file testcase.js —
Attached patch fix.patch — — Splinter Review
Attached file crash_stack.txt —
Group: core-security → javascript-core-security

Might be the same issue as bug 2042727.

See Also: → 2042727
Status: UNCONFIRMED → RESOLVED
Closed: 5 months ago
Duplicate of bug: 2042727
Resolution: --- → DUPLICATE
Group: javascript-core-security
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: