Title: [270718] trunk/Source
Revision
270718
Author
[email protected]
Date
2020-12-11 16:32:06 -0800 (Fri, 11 Dec 2020)

Log Message

Potential null dereference of m_frame under DocumentLoader::stopLoading()
https://bugs.webkit.org/show_bug.cgi?id=219786
<rdar://71945402>

Reviewed by Geoffrey Garen.

Source/WebCore:

The crash indicates that FrameLoader::stopAllLoaders() is calling m_documentLoader->stopLoading()
and that DocumentLoader::m_frame is null, causing DocumentLoader::stopLoading() to do a null
dereference of m_frame.

I believe 3 things could have happened:
1. DocumentLoader::detachFromFrame() was called on a DocumentLoader but that DocumentLoader
   somehow stayed attached to the FrameLoader. DocumentLoader::detachFromFrame() is what
   clears DocumentLoader::m_frame.
2. FrameLoader::setDocumentLoader() was called with a DocumentLoader which already did not have
   a frame.
3. The Frame was destroyed. DocumentLoader is a FrameDestructionObserver so m_frame would
   actually get nulled out in case of Frame destruction.

Now, I do not believe that 3 is actually possible. We know the FrameLoader is alive here since
FrameLoader::stopAllLoaders() is what's calling DocumentLoader::stopLoading(). The Frame owns
the FrameLoader so the Frame is alive. Also note that FrameLoader::stopAllLoaders() protects
the frame AND dereferences m_frame without crashing *BEFORE* calling DocumentLoader::stopLoading().

Looking at case 1, I believe this is actually possible currently because
FrameLoader::setProvisionalDocumentLoader() does not early return when the new DocumentLoader
being set is the same one as m_provisionalDocumentLoader. This means that
FrameLoader::setProvisionalDocumentLoader() would in such case call DocumentLoader::detachFromFrame()
on the DocumentLoader even though it will remain attached to this FrameLoader. Note that
FrameLoader::setPolicyDocumentLoader() & FrameLoader::setDocumentLoader() already return early
properly in such cases. This patch fixes FrameLoader::setProvisionalDocumentLoader() to early
return, thus making issue 1 impossible I believe.

Case 2 is a bit harder to fix. We had this debug assertion in FrameLoader::setDocumentLoader():
`ASSERT(!loader || loader->frameLoader() == this);`
So in debug builds at least, we are protected against issue 2 because loader->frameLoader()
would return null when the loader's frame is null. I turned this debug assertion into a release
assertion to help us figure out if this is what's causing this crash.

I also made the following changes:
- Added release logging to the related code to help debug this in the future if the issue
  remains.
- Do an early return in DocumentLoader::stopLoading() if m_frame is already null to avoid the
  crash. I added a debug assertion to ensure that m_frame is not null though to see if this
  is still happening.

No new tests, we do not know how this happens at this time.

* loader/DocumentLoader.cpp:
(WebCore::DocumentLoader::stopLoading):
(WebCore::DocumentLoader::attachToFrame):
(WebCore::DocumentLoader::detachFromFrame):
* loader/FrameLoader.cpp:
(WebCore::FrameLoader::stopAllLoaders):
(WebCore::FrameLoader::setDocumentLoader):
(WebCore::FrameLoader::setPolicyDocumentLoader):
(WebCore::FrameLoader::setProvisionalDocumentLoader):
Return early when the new loader is identical to the existing one, to avoid incorrectly calling
detachFromFrame() on it.

Source/WebKit:

* WebProcess/WebPage/WebPage.cpp:
(WebKit::WebPage::close):
Add some release logging.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (270717 => 270718)


--- trunk/Source/WebCore/ChangeLog	2020-12-12 00:07:48 UTC (rev 270717)
+++ trunk/Source/WebCore/ChangeLog	2020-12-12 00:32:06 UTC (rev 270718)
@@ -1,3 +1,65 @@
+2020-12-11  Chris Dumez  <[email protected]>
+
+        Potential null dereference of m_frame under DocumentLoader::stopLoading()
+        https://bugs.webkit.org/show_bug.cgi?id=219786
+        <rdar://71945402>
+
+        Reviewed by Geoffrey Garen.
+
+        The crash indicates that FrameLoader::stopAllLoaders() is calling m_documentLoader->stopLoading()
+        and that DocumentLoader::m_frame is null, causing DocumentLoader::stopLoading() to do a null
+        dereference of m_frame.
+
+        I believe 3 things could have happened:
+        1. DocumentLoader::detachFromFrame() was called on a DocumentLoader but that DocumentLoader
+           somehow stayed attached to the FrameLoader. DocumentLoader::detachFromFrame() is what
+           clears DocumentLoader::m_frame.
+        2. FrameLoader::setDocumentLoader() was called with a DocumentLoader which already did not have
+           a frame.
+        3. The Frame was destroyed. DocumentLoader is a FrameDestructionObserver so m_frame would
+           actually get nulled out in case of Frame destruction.
+
+        Now, I do not believe that 3 is actually possible. We know the FrameLoader is alive here since
+        FrameLoader::stopAllLoaders() is what's calling DocumentLoader::stopLoading(). The Frame owns
+        the FrameLoader so the Frame is alive. Also note that FrameLoader::stopAllLoaders() protects
+        the frame AND dereferences m_frame without crashing *BEFORE* calling DocumentLoader::stopLoading().
+
+        Looking at case 1, I believe this is actually possible currently because
+        FrameLoader::setProvisionalDocumentLoader() does not early return when the new DocumentLoader
+        being set is the same one as m_provisionalDocumentLoader. This means that
+        FrameLoader::setProvisionalDocumentLoader() would in such case call DocumentLoader::detachFromFrame()
+        on the DocumentLoader even though it will remain attached to this FrameLoader. Note that
+        FrameLoader::setPolicyDocumentLoader() & FrameLoader::setDocumentLoader() already return early
+        properly in such cases. This patch fixes FrameLoader::setProvisionalDocumentLoader() to early
+        return, thus making issue 1 impossible I believe.
+
+        Case 2 is a bit harder to fix. We had this debug assertion in FrameLoader::setDocumentLoader():
+        `ASSERT(!loader || loader->frameLoader() == this);`
+        So in debug builds at least, we are protected against issue 2 because loader->frameLoader()
+        would return null when the loader's frame is null. I turned this debug assertion into a release
+        assertion to help us figure out if this is what's causing this crash.
+
+        I also made the following changes:
+        - Added release logging to the related code to help debug this in the future if the issue
+          remains.
+        - Do an early return in DocumentLoader::stopLoading() if m_frame is already null to avoid the
+          crash. I added a debug assertion to ensure that m_frame is not null though to see if this
+          is still happening.
+
+        No new tests, we do not know how this happens at this time.
+
+        * loader/DocumentLoader.cpp:
+        (WebCore::DocumentLoader::stopLoading):
+        (WebCore::DocumentLoader::attachToFrame):
+        (WebCore::DocumentLoader::detachFromFrame):
+        * loader/FrameLoader.cpp:
+        (WebCore::FrameLoader::stopAllLoaders):
+        (WebCore::FrameLoader::setDocumentLoader):
+        (WebCore::FrameLoader::setPolicyDocumentLoader):
+        (WebCore::FrameLoader::setProvisionalDocumentLoader):
+        Return early when the new loader is identical to the existing one, to avoid incorrectly calling
+        detachFromFrame() on it.
+
 2020-12-11  Andy Estes  <[email protected]>
 
         [Mac] Abstract SourceBufferParser segments

Modified: trunk/Source/WebCore/loader/DocumentLoader.cpp (270717 => 270718)


--- trunk/Source/WebCore/loader/DocumentLoader.cpp	2020-12-12 00:07:48 UTC (rev 270717)
+++ trunk/Source/WebCore/loader/DocumentLoader.cpp	2020-12-12 00:32:06 UTC (rev 270718)
@@ -287,6 +287,12 @@
     frameLoader()->receivedMainResourceError(error);
 }
 
+void DocumentLoader::frameDestroyed()
+{
+    RELEASE_LOG_IF_ALLOWED("DocumentLoader::frameDestroyed: m_frame=%p", m_frame.get());
+    FrameDestructionObserver::frameDestroyed();
+}
+
 // Cancels the data source's pending loads.  Conceptually, a data source only loads
 // one document at a time, but one document may have many related resources. 
 // stopLoading will stop all loads initiated by the data source, 
@@ -293,6 +299,12 @@
 // but not loads initiated by child frames' data sources -- that's the WebFrame's job.
 void DocumentLoader::stopLoading()
 {
+    RELEASE_LOG_IF_ALLOWED("DocumentLoader::stopLoading: m_frame=%p", m_frame.get());
+
+    ASSERT(m_frame);
+    if (!m_frame)
+        return;
+
     RefPtr<Frame> protectedFrame(m_frame.get());
     Ref<DocumentLoader> protectedThis(*this);
 
@@ -1329,10 +1341,13 @@
 void DocumentLoader::attachToFrame()
 {
     ASSERT(m_frame);
+    RELEASE_LOG_IF_ALLOWED("DocumentLoader::attachToFrame: m_frame=%p", m_frame.get());
 }
 
 void DocumentLoader::detachFromFrame()
 {
+    RELEASE_LOG_IF_ALLOWED("DocumentLoader::detachFromFrame: m_frame=%p", m_frame.get());
+
 #if ASSERT_ENABLED
     if (m_hasEverBeenAttached)
         ASSERT_WITH_MESSAGE(m_frame, "detachFromFrame() is being called on a DocumentLoader twice without an attachToFrame() inbetween");

Modified: trunk/Source/WebCore/loader/DocumentLoader.h (270717 => 270718)


--- trunk/Source/WebCore/loader/DocumentLoader.h	2020-12-12 00:07:48 UTC (rev 270717)
+++ trunk/Source/WebCore/loader/DocumentLoader.h	2020-12-12 00:32:06 UTC (rev 270718)
@@ -233,6 +233,9 @@
     void scheduleSubstituteResourceLoad(ResourceLoader&, SubstituteResource&);
     void scheduleCannotShowURLError(ResourceLoader&);
 
+    // FrameDestructionObserver.
+    WEBCORE_EXPORT void frameDestroyed() final;
+
     // Return the ArchiveResource for the URL only when loading an Archive
     WEBCORE_EXPORT ArchiveResource* archiveResourceForURL(const URL&) const;
 

Modified: trunk/Source/WebCore/loader/FrameLoader.cpp (270717 => 270718)


--- trunk/Source/WebCore/loader/FrameLoader.cpp	2020-12-12 00:07:48 UTC (rev 270717)
+++ trunk/Source/WebCore/loader/FrameLoader.cpp	2020-12-12 00:32:06 UTC (rev 270718)
@@ -1818,6 +1818,9 @@
 
     for (RefPtr<Frame> child = m_frame.tree().firstChild(); child; child = child->tree().nextSibling())
         child->loader().stopAllLoaders(clearProvisionalItemPolicy);
+
+    FRAMELOADER_RELEASE_LOG_IF_ALLOWED(ResourceLoading, "stopAllLoaders: m_provisionalDocumentLoader=%p, m_documentLoader=%p", m_provisionalDocumentLoader.get(), m_documentLoader.get());
+
     if (m_provisionalDocumentLoader)
         m_provisionalDocumentLoader->stopLoading();
     if (m_documentLoader)
@@ -1910,14 +1913,12 @@
 
 void FrameLoader::setDocumentLoader(DocumentLoader* loader)
 {
-    if (!loader && !m_documentLoader)
+    if (loader == m_documentLoader)
         return;
 
-    if (loader == m_documentLoader)
-        return;
+    FRAMELOADER_RELEASE_LOG_IF_ALLOWED(ResourceLoading, "setDocumentLoader: Setting document loader to %p (was %p)", loader, m_documentLoader.get());
     
-    ASSERT(loader != m_documentLoader);
-    ASSERT(!loader || loader->frameLoader() == this);
+    RELEASE_ASSERT(!loader || loader->frameLoader() == this);
 
     m_client->prepareForDataSourceReplacement();
     detachChildren();
@@ -1943,6 +1944,8 @@
     if (m_policyDocumentLoader == loader)
         return;
 
+    FRAMELOADER_RELEASE_LOG_IF_ALLOWED(ResourceLoading, "setPolicyDocumentLoader: Setting policy document loader to %p (was %p)", loader, m_policyDocumentLoader.get());
+
     if (loader)
         loader->attachToFrame(m_frame);
     if (m_policyDocumentLoader
@@ -1955,10 +1958,13 @@
 
 void FrameLoader::setProvisionalDocumentLoader(DocumentLoader* loader)
 {
-    FRAMELOADER_RELEASE_LOG_IF_ALLOWED(ResourceLoading, "setProvisionalDocumentLoader: Setting provisional document loader (m_provisionalDocumentLoader=%p)", loader);
+    if (m_provisionalDocumentLoader == loader)
+        return;
 
+    FRAMELOADER_RELEASE_LOG_IF_ALLOWED(ResourceLoading, "setProvisionalDocumentLoader: Setting provisional document loader to %p (was %p)", loader, m_provisionalDocumentLoader.get());
+
     ASSERT(!loader || !m_provisionalDocumentLoader);
-    ASSERT(!loader || loader->frameLoader() == this);
+    RELEASE_ASSERT(!loader || loader->frameLoader() == this);
 
     if (m_provisionalDocumentLoader && m_provisionalDocumentLoader != m_documentLoader)
         m_provisionalDocumentLoader->detachFromFrame();

Modified: trunk/Source/WebKit/ChangeLog (270717 => 270718)


--- trunk/Source/WebKit/ChangeLog	2020-12-12 00:07:48 UTC (rev 270717)
+++ trunk/Source/WebKit/ChangeLog	2020-12-12 00:32:06 UTC (rev 270718)
@@ -1,3 +1,15 @@
+2020-12-11  Chris Dumez  <[email protected]>
+
+        Potential null dereference of m_frame under DocumentLoader::stopLoading()
+        https://bugs.webkit.org/show_bug.cgi?id=219786
+        <rdar://71945402>
+
+        Reviewed by Geoffrey Garen.
+
+        * WebProcess/WebPage/WebPage.cpp:
+        (WebKit::WebPage::close):
+        Add some release logging.
+
 2020-12-11  Tim Horton  <[email protected]>
 
         Fix the iOS !UISCROLLVIEW_ASYNCHRONOUS_SCROLL_EVENT_HANDLING build.

Modified: trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp (270717 => 270718)


--- trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2020-12-12 00:07:48 UTC (rev 270717)
+++ trunk/Source/WebKit/WebProcess/WebPage/WebPage.cpp	2020-12-12 00:32:06 UTC (rev 270718)
@@ -1403,6 +1403,8 @@
     if (m_isClosed)
         return;
 
+    RELEASE_LOG_IF_ALLOWED(Loading, "close:");
+
 #if ENABLE(RESOURCE_LOAD_STATISTICS)
     WebProcess::singleton().ensureNetworkProcessConnection().connection().send(Messages::NetworkConnectionToWebProcess::ClearPageSpecificDataForResourceLoadStatistics(m_identifier), 0);
 #endif
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to