Closed Bug 1735152 Opened 3 years ago Closed 3 years ago

nsSocketTransport::Open{Input/Output}Stream can set output pointer without AddRef-ing it.

Categories

(Core :: Networking, defect, P2)

defect

Tracking

()

RESOLVED FIXED
95 Branch
Tracking Status
firefox-esr78 --- wontfix
firefox-esr91 94+ fixed
firefox93 --- wontfix
firefox94 + fixed
firefox95 + fixed

People

(Reporter: valentin, Assigned: valentin)

References

(Regression)

Details

(Keywords: csectype-uaf, regression, sec-high, Whiteboard: [necko-triaged][sec-survey][adv-main94+r][adv-esr91.3+r])

Attachments

(1 file)

In SocketTransport2 we have a somewhat bad pattern:

NS_IMETHODIMP
nsSocketTransport::OpenInputStream(uint32_t flags, uint32_t segsize,
                                   uint32_t segcount, nsIInputStream** result) {
  if (!(flags & OPEN_UNBUFFERED) || (flags & OPEN_BLOCKING)) {
   // [...]
    nsCOMPtr<nsIAsyncOutputStream> pipeOut;
    rv = NS_NewPipe2(getter_AddRefs(pipeIn), getter_AddRefs(pipeOut),
                     !openBlocking, true, segsize, segcount);
    if (NS_FAILED(rv)) return rv;

    // async copy from socket to pipe
    rv = NS_AsyncCopy(&mInput, pipeOut, mSocketTransportService,
                      NS_ASYNCCOPY_VIA_WRITESEGMENTS, segsize);
    if (NS_FAILED(rv)) return rv;

    *result = pipeIn;
  } else {
    *result = &mInput;
  }

  // flag input stream as open
  mInputClosed = false;

  rv = PostEvent(MSG_ENSURE_CONNECT);
  if (NS_FAILED(rv)) return rv; 
  // BUG IS HERE!!!
  NS_ADDREF(*result);
  return NS_OK;
}

The problem is that if PostEvent fails to dispatch the event for some reason, we exit the method and result is assigned but not addreffed.
If the callsite passes in a member variable like here, that member nsCOMPtr gets assigned a value, but not addreffed.
When the object gets released, we also release the COMPtr, but since the refcount was not incremented this will lead to a double-free, either immediately or sometimes later.

It's possible this is what's causing bug 1687269.

Blocks: 1687269

Note: postEvent is unlikely to fail in regular usage. It would only be a problem at shutdown and in low-memory situations.

Comment on attachment 9245258 [details]
Bug 1735152 - Avoid using NS_ADDREF in nsSocketTransport r=#necko

Security Approval Request

  • How easily could an exploit be constructed based on the patch?: Unclear. This requires posting an event to the thread to fail, which is possible during shutdown but unlikely otherwise (might be triggered by low memory though).
  • Do comments in the patch, the check-in comment, or tests included in the patch paint a bulls-eye on the security problem?: Unknown
  • Which older supported branches are affected by this flaw?: all
  • If not all supported branches, which bug introduced the flaw?: Bug 176919
  • Do you have backports for the affected branches?: Yes
  • If not, how different, hard to create, and risky will they be?: patch should apply to all branches
  • How likely is this patch to cause regressions; how much testing does it need?: Unlikely to cause regressions.
Attachment #9245258 - Flags: sec-approval?
Keywords: sec-high
Group: core-security
Has Regression Range: --- → yes
Keywords: regression

If not all supported branches, which bug introduced the flaw?: Bug 176919

That's a long time ago. Any idea what changed in Fx90 that might have made this crash more likely? (see bug 1687269, bug 1731943) If nothing immediately comes to mind don't worry about it, could be a lot of things.

Flags: needinfo?(valentin.gosu)
Blocks: 1731943

(In reply to Daniel Veditz [:dveditz] from comment #5)

If not all supported branches, which bug introduced the flaw?: Bug 176919

That's a long time ago. Any idea what changed in Fx90 that might have made this crash more likely? (see bug 1687269, bug 1731943) If nothing immediately comes to mind don't worry about it, could be a lot of things.

No idea. It could be related to the conditions in which Dispatch to the thread fails, but I'm not able to find a change that I can point to.

Flags: needinfo?(valentin.gosu)

Comment on attachment 9245258 [details]
Bug 1735152 - Avoid using NS_ADDREF in nsSocketTransport r=#necko

Approved to land/request uplift

Attachment #9245258 - Flags: sec-approval? → sec-approval+
Group: network-core-security → core-security
Status: ASSIGNED → RESOLVED
Closed: 3 years ago
Resolution: --- → FIXED
Target Milestone: --- → 95 Branch

Comment on attachment 9245258 [details]
Bug 1735152 - Avoid using NS_ADDREF in nsSocketTransport r=#necko

Beta/Release Uplift Approval Request

  • User impact if declined: Potential UAF
  • Is this code covered by automated tests?: Yes
  • Has the fix been verified in Nightly?: No
  • 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): No functionality change other than making sure return arg is always AddRef'd
  • String changes made/needed:

ESR Uplift Approval Request

  • If this is not a sec:{high,crit} bug, please state case for ESR consideration: sec-high
  • User impact if declined: Potential UAF
  • Fix Landed on Version: 95
  • Risk to taking this patch: Low
  • Why is the change risky/not risky? (and alternatives if risky): No functionality change other than making sure return arg is always AddRef'd
  • String or UUID changes made by this patch:
Attachment #9245258 - Flags: approval-mozilla-esr78?
Attachment #9245258 - Flags: approval-mozilla-beta?

Comment on attachment 9245258 [details]
Bug 1735152 - Avoid using NS_ADDREF in nsSocketTransport r=#necko

Approved for 94.0b7 and 91.3esr.

Attachment #9245258 - Flags: approval-mozilla-esr78?
Attachment #9245258 - Flags: approval-mozilla-esr78+
Attachment #9245258 - Flags: approval-mozilla-beta?
Attachment #9245258 - Flags: approval-mozilla-beta+
Attachment #9245258 - Flags: approval-mozilla-esr78+ → approval-mozilla-esr91+
Group: core-security → core-security-release

As part of a security bug pattern analysis, we are requesting your help with a high level analysis of this bug. It is our hope to develop static analysis (or potentially runtime/dynamic analysis) in the future to identify classes of bugs.

Please visit this google form to reply.

Flags: needinfo?(valentin.gosu)
Whiteboard: [necko-triaged] → [necko-triaged][sec-survey]
Flags: needinfo?(valentin.gosu)
QA Whiteboard: [post-critsmash-triage]
Flags: qe-verify-
Whiteboard: [necko-triaged][sec-survey] → [necko-triaged][sec-survey][adv-main94+r]
Whiteboard: [necko-triaged][sec-survey][adv-main94+r] → [necko-triaged][sec-survey][adv-main94+r][adv-esr91.3+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: