Closed Bug 1626047 Opened 6 years ago Closed 4 years ago

Intermittent SUMMARY: AddressSanitizer: heap-use-after-free /builds/worker/checkouts/gecko/security/nss/lib/dev/devtoken.c:75:32 in nssToken_GetSlot

Categories

(NSS :: Libraries, defect, P3)

3.77

Tracking

(Root Cause:Coding: Logical Error)

RESOLVED FIXED

People

(Reporter: dluca, Unassigned)

References

Details

(Keywords: csectype-race, csectype-uaf, sec-moderate)

Attachments

(1 file)

Log:
https://treeherder.mozilla.org/logviewer.html#/jobs?job_id=295391823&repo=autoland&lineNumber=2948

Treehereder link:
https://treeherder.mozilla.org/#/jobs?repo=autoland&selectedJob=295391823&revision=4ba3f4a39170e1625509b553ff60b2bd1bdaf2de

[task 2020-03-30T16:05:19.989Z] 16:05:19     INFO -  PID 28144 |     #45 0x7f9ca4f5a573 in js::RunScript(JSContext*, js::RunState&) /builds/worker/checkouts/gecko/js/src/vm/Interpreter.cpp:449:10
[task 2020-03-30T16:05:19.990Z] 16:05:19     INFO -  PID 28144 |     #46 0x7f9ca4f8b4ba in js::InternalCallOrConstruct(JSContext*, JS::CallArgs const&, js::MaybeConstruct, js::CallReason) /builds/worker/checkouts/gecko/js/src/vm/Interpreter.cpp:604:13
[task 2020-03-30T16:05:19.990Z] 16:05:19     INFO -  PID 28144 |     #47 0x7f9ca4f8d08a in InternalCall(JSContext*, js::AnyInvokeArgs const&, js::CallReason) /builds/worker/checkouts/gecko/js/src/vm/Interpreter.cpp:632:10
[task 2020-03-30T16:05:19.990Z] 16:05:19     INFO -  PID 28144 |     #48 0x7f9ca5e1f42f in js::jit::DoCallFallback(JSContext*, js::jit::BaselineFrame*, js::jit::ICCall_Fallback*, unsigned int, JS::Value*, JS::MutableHandle<JS::Value>) /builds/worker/checkouts/gecko/js/src/jit/BaselineIC.cpp:2926:10
[task 2020-03-30T16:05:19.990Z] 16:05:19     INFO -  PID 28144 |     #49 0x321e86ccfe27  (<unknown module>)
[task 2020-03-30T16:05:19.990Z] 16:05:19     INFO -  PID 28144 |     #50 0x6210000d23d7  (<unknown module>)
[task 2020-03-30T16:05:19.991Z] 16:05:19     INFO -  PID 28144 |     #51 0x321e86ccd48e  (<unknown module>)
[task 2020-03-30T16:05:19.991Z] 16:05:19     INFO -  PID 28144 |     #52 0x7f9ca5f8a01b in EnterBaseline /builds/worker/checkouts/gecko/js/src/jit/BaselineJIT.cpp:114:5
[task 2020-03-30T16:05:19.991Z] 16:05:19     INFO -  PID 28144 |     #53 0x7f9ca5f8a01b in js::jit::EnterBaselineInterpreterAtBranch(JSContext*, js::InterpreterFrame*, unsigned char*) /builds/worker/checkouts/gecko/js/src/jit/BaselineJIT.cpp:187:26
[task 2020-03-30T16:05:19.991Z] 16:05:19     INFO -  PID 28144 |     #54 0x7f9ca4f7f364 in Interpret(JSContext*, js::RunState&) /builds/worker/checkouts/gecko/js/src/vm/Interpreter.cpp:1928:17
[task 2020-03-30T16:05:19.991Z] 16:05:19     INFO -  PID 28144 |     #55 0x7f9ca4f5a573 in js::RunScript(JSContext*, js::RunState&) /builds/worker/checkouts/gecko/js/src/vm/Interpreter.cpp:449:10
[task 2020-03-30T16:05:19.992Z] 16:05:19     INFO -  PID 28144 |     #56 0x7f9ca4f8f388 in js::ExecuteKernel(JSContext*, JS::Handle<JSScript*>, JSObject&, JS::Value const&, js::AbstractFramePtr, JS::Value*) /builds/worker/checkouts/gecko/js/src/vm/Interpreter.cpp:825:13
[task 2020-03-30T16:05:19.992Z] 16:05:19     INFO -  PID 28144 |     #57 0x7f9ca4f8fd08 in js::Execute(JSContext*, JS::Handle<JSScript*>, JSObject&, JS::Value*) /builds/worker/checkouts/gecko/js/src/vm/Interpreter.cpp:858:10
[task 2020-03-30T16:05:19.992Z] 16:05:19     INFO -  PID 28144 |     #58 0x7f9ca5265006 in EvaluateSourceBuffer<mozilla::Utf8Unit> /builds/worker/checkouts/gecko/js/src/vm/CompilationAndEvaluation.cpp:564:10
[task 2020-03-30T16:05:19.992Z] 16:05:19     INFO -  PID 28144 |     #59 0x7f9ca5265006 in JS::EvaluateDontInflate(JSContext*, JS::ReadOnlyCompileOptions const&, JS::SourceText<mozilla::Utf8Unit>&, JS::MutableHandle<JS::Value>) /builds/worker/checkouts/gecko/js/src/vm/CompilationAndEvaluation.cpp:596:10
[task 2020-03-30T16:05:19.992Z] 16:05:19     INFO -  PID 28144 |     #60 0x7f9c9bfcfd90 in ProcessArgs /builds/worker/checkouts/gecko/js/xpconnect/src/XPCShellImpl.cpp:958:11
[task 2020-03-30T16:05:19.993Z] 16:05:19     INFO -  PID 28144 |     #61 0x7f9c9bfcfd90 in XRE_XPCShellMain(int, char**, char**, XREShellData const*) /builds/worker/checkouts/gecko/js/xpconnect/src/XPCShellImpl.cpp:1353:14
[task 2020-03-30T16:05:19.993Z] 16:05:19     INFO -  PID 28144 |     #62 0x556f2d9e7b5b in main /builds/worker/checkouts/gecko/js/xpconnect/shell/xpcshell.cpp:66:27
[task 2020-03-30T16:05:19.993Z] 16:05:19     INFO -  PID 28144 |     #63 0x7f9c946f8b96 in __libc_start_main /build/glibc-OTsEL5/glibc-2.27/csu/../csu/libc-start.c:310
[task 2020-03-30T16:05:19.996Z] 16:05:19     INFO -  PID 28144 | SUMMARY: AddressSanitizer: heap-use-after-free /builds/worker/checkouts/gecko/security/nss/lib/dev/devtoken.c:75:32 in nssToken_GetSlot
[task 2020-03-30T16:05:19.996Z] 16:05:19     INFO -  PID 28144 | Shadow bytes around the buggy address:
[task 2020-03-30T16:05:19.996Z] 16:05:19     INFO -  PID 28144 |   0x0c3a80012780: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
[task 2020-03-30T16:05:19.997Z] 16:05:19     INFO -  PID 28144 |   0x0c3a80012790: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
[task 2020-03-30T16:05:19.997Z] 16:05:19     INFO -  PID 28144 |   0x0c3a800127a0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
[task 2020-03-30T16:05:19.997Z] 16:05:19     INFO -  PID 28144 |   0x0c3a800127b0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
[task 2020-03-30T16:05:19.998Z] 16:05:19     INFO -  PID 28144 |   0x0c3a800127c0: fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa fa
[task 2020-03-30T16:05:19.998Z] 16:05:19     INFO -  PID 28144 | =>0x0c3a800127d0: fd fd fd fd fd fd fd fd fd fd fd[fd]fd fd fd fd
[task 2020-03-30T16:05:19.999Z] 16:05:19     INFO -  PID 28144 |   0x0c3a800127e0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
[task 2020-03-30T16:05:19.999Z] 16:05:19     INFO -  PID 28144 |   0x0c3a800127f0: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
[task 2020-03-30T16:05:19.999Z] 16:05:19     INFO -  PID 28144 |   0x0c3a80012800: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
[task 2020-03-30T16:05:20.000Z] 16:05:20     INFO -  PID 28144 |   0x0c3a80012810: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
[task 2020-03-30T16:05:20.000Z] 16:05:20     INFO -  PID 28144 |   0x0c3a80012820: fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd fd
[task 2020-03-30T16:05:20.001Z] 16:05:20     INFO -  PID 28144 | Shadow byte legend (one shadow byte represents 8 application bytes):
[task 2020-03-30T16:05:20.001Z] 16:05:20     INFO -  PID 28144 |   Addressable:           00
[task 2020-03-30T16:05:20.002Z] 16:05:20     INFO -  PID 28144 |   Partially addressable: 01 02 03 04 05 06 07
[task 2020-03-30T16:05:20.002Z] 16:05:20     INFO -  PID 28144 |   Heap left redzone:       fa
[task 2020-03-30T16:05:20.002Z] 16:05:20     INFO -  PID 28144 |   Freed heap region:       fd
[task 2020-03-30T16:05:20.003Z] 16:05:20     INFO -  PID 28144 |   Stack left redzone:      f1
[task 2020-03-30T16:05:20.003Z] 16:05:20     INFO -  PID 28144 |   Stack mid redzone:       f2
[task 2020-03-30T16:05:20.004Z] 16:05:20     INFO -  PID 28144 |   Stack right redzone:     f3
[task 2020-03-30T16:05:20.004Z] 16:05:20     INFO -  PID 28144 |   Stack after return:      f5
[task 2020-03-30T16:05:20.004Z] 16:05:20     INFO -  PID 28144 |   Stack use after scope:   f8
[task 2020-03-30T16:05:20.005Z] 16:05:20     INFO -  PID 28144 |   Global redzone:          f9
[task 2020-03-30T16:05:20.005Z] 16:05:20     INFO -  PID 28144 |   Global init order:       f6
[task 2020-03-30T16:05:20.006Z] 16:05:20     INFO -  PID 28144 |   Poisoned by user:        f7
[task 2020-03-30T16:05:20.006Z] 16:05:20     INFO -  PID 28144 |   Container overflow:      fc
[task 2020-03-30T16:05:20.006Z] 16:05:20     INFO -  PID 28144 |   Array cookie:            ac
[task 2020-03-30T16:05:20.007Z] 16:05:20     INFO -  PID 28144 |   Intra object redzone:    bb
[task 2020-03-30T16:05:20.007Z] 16:05:20     INFO -  PID 28144 |   ASan internal:           fe
[task 2020-03-30T16:05:20.008Z] 16:05:20     INFO -  PID 28144 |   Left alloca redzone:     ca
[task 2020-03-30T16:05:20.008Z] 16:05:20     INFO -  PID 28144 |   Right alloca redzone:    cb
[task 2020-03-30T16:05:20.008Z] 16:05:20     INFO -  PID 28144 |   Shadow gap:              cc
[task 2020-03-30T16:05:20.009Z] 16:05:20     INFO -  PID 28144 | ==28144==ABORTING
[task 2020-03-30T16:05:20.009Z] 16:05:20     INFO -  <<<<<<<
[task 2020-03-30T16:05:20.010Z] 16:05:20     INFO -  INFO | Result summary:
[task 2020-03-30T16:05:20.010Z] 16:05:20     INFO -  INFO | Passed: 723
[task 2020-03-30T16:05:20.010Z] 16:05:20  WARNING -  INFO | Failed: 1
[task 2020-03-30T16:05:20.011Z] 16:05:20  WARNING -  One or more unittests failed.
[task 2020-03-30T16:05:20.011Z] 16:05:20     INFO -  INFO | Todo: 0
[task 2020-03-30T16:05:20.012Z] 16:05:20     INFO -  INFO | Retried: 1
[task 2020-03-30T16:05:20.012Z] 16:05:20     INFO -  SUITE-END | took 1152s
[task 2020-03-30T16:05:20.013Z] 16:05:20     INFO -  Node moz-http2 server shutting down ...
[task 2020-03-30T16:05:20.100Z] 16:05:20    ERROR - Return code: 1
[task 2020-03-30T16:05:20.100Z] 16:05:20     INFO - TinderboxPrint: xpcshell-xpcshell<br/>723/<em class="testfail">1</em>/0
[task 2020-03-30T16:05:20.101Z] 16:05:20    ERROR - # TBPL FAILURE #
[task 2020-03-30T16:05:20.101Z] 16:05:20  WARNING - setting return code to 2
[task 2020-03-30T16:05:20.102Z] 16:05:20    ERROR - The xpcshell suite: xpcshell ran with return status: FAILURE

This seems very similar to bug 1607588, but it might not be exactly the same thing. In any case, it's an NSS issue.

Assignee: nobody → nobody
Group: crypto-core-security
Component: Security: PSM → Libraries
Product: Core → NSS
QA Contact: jjones
Version: unspecified → other
Group: core-security
Summary: TEST-UNEXPECTED-FAIL | security/manager/ssl/tests/unit/test_nss_shutdown.js | xpcshell return code: 1 → Intermittent SUMMARY: AddressSanitizer: heap-use-after-free /builds/worker/checkouts/gecko/security/nss/lib/dev/devtoken.c:75:32 in nssToken_GetSlot

The priority flag is not set for this bug.
:jcj, could you have a look please?

For more information, please visit auto_nag documentation.

Flags: needinfo?(jjones)

A given NSSToken's refcount goes to 0 and destruction begins (nssToken_Destroy) while we're in the middle of nssTrustDomain_GetActiveSlots's iteration across tokens. In particular, we UAF while trying to increment the refcount on the token's slot, which has been freed during the destruction.

Iteration across these nssTokens should be consistent if nssList_GetArray was protected by the trustDomain's tokensLock, however I don't see evidence of nssTrustDomain_FindTrustForCertificate locking that, which is our race-offender. nssTrustDomain_FindTrustForCertificate is iterating, itself, but doesn't lock tokensLock, but destroys the tokens as it goes anwyay because NSS is in the middle of shutting down, and it happens to be the last one holding the individual nssToken handles.

It seems to me like the for loop on https://searchfox.org/nss/rev/ac9ae54487203114f62c6e095deb7518f6f3fa99/lib/pki/trustdomain.c#1058 should somehow keep the read-lock on td->tokensLock from the nssTrustDomain_GetActiveSlots, because it's not done yet. Sadly that pattern is never used in trustdomain.c, it's always just proceeding blindly assuming that no one would dare delete those slots it just got handles to.
https://searchfox.org/nss/rev/ac9ae54487203114f62c6e095deb7518f6f3fa99/lib/pki/trustdomain.c#129

The other pattern that we see sometimes in NSS is that get methods like that call the relevant _AddRef on each element as it passes them back, so that the caller has to release the handles when it's done. Given the relatively small number of places nssTrustDomain_GetActiveSlots gets called, that might be more suitable, since we have no reentrant locks in NSS.

We'll triage this shortly.

Flags: needinfo?(jjones)
Priority: -- → P3

This is a difficult race to accomplish, and also primarily a shutdown crash. The only way I could see this being triggered during normal operation would be hotplugging a PKCS11 token (smart card) repeatedly while also spinning certificate validation via TLS.

While a heap-UAF, I think this is a sec-moderate: Both because it's a shutdown crash, and because of the difficulty to trigger.

It still looks like the correct solution is to adjust the get methods to _AddRef and release in the callers.

Severity: normal → S2
Root Cause: --- → Coding: Logical Error
Keywords: sec-high → sec-moderate
Depends on: 1755555

A consequence of the changes made in Bug 1370866 is that the NSSToken in question here cannot be destroyed while td->tokensLock is held. The changes made in Bug 1755555 ensure that we hold td->tokensLock while making the nssToken_GetSlot call that caused the crash here. So this is resolved.

Status: NEW → RESOLVED
Closed: 4 years ago
Depends on: 1370866
Resolution: --- → FIXED
Version: other → 3.77
Group: crypto-core-security → core-security-release
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: