<?xml version="1.0" encoding="UTF-8" standalone="yes" ?>
<!DOCTYPE bugzilla SYSTEM "https://bugs.webkit.org/page.cgi?id=bugzilla.dtd">

<bugzilla version="5.0.4.1"
          urlbase="https://bugs.webkit.org/"
          
          maintainer="admin@webkit.org"
>

    <bug>
          <bug_id>209987</bug_id>
          
          <creation_ts>2020-04-03 14:23:09 -0700</creation_ts>
          <short_desc>Use-after-move of `formState` in WebCore::PolicyChecker::checkNavigationPolicy()</short_desc>
          <delta_ts>2020-04-03 17:16:11 -0700</delta_ts>
          <reporter_accessible>1</reporter_accessible>
          <cclist_accessible>1</cclist_accessible>
          <classification_id>1</classification_id>
          <classification>Unclassified</classification>
          <product>WebKit</product>
          <component>Page Loading</component>
          <version>WebKit Nightly Build</version>
          <rep_platform>Unspecified</rep_platform>
          <op_sys>Unspecified</op_sys>
          <bug_status>RESOLVED</bug_status>
          <resolution>FIXED</resolution>
          
          
          <bug_file_loc></bug_file_loc>
          <status_whiteboard></status_whiteboard>
          <keywords>InRadar</keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="David Kilzer (:ddkilzer)">ddkilzer</reporter>
          <assigned_to name="Alex Christensen">achristensen</assigned_to>
          <cc>achristensen</cc>
    
    <cc>beidson</cc>
    
    <cc>bfulgham</cc>
    
    <cc>cdumez</cc>
    
    <cc>darin</cc>
    
    <cc>ews-watchlist</cc>
    
    <cc>japhet</cc>
    
    <cc>product-security</cc>
    
    <cc>rniwa</cc>
    
    <cc>webkit-bug-importer</cc>
    
    <cc>wenson_hsieh</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>1637526</commentid>
    <comment_count>0</comment_count>
    <who name="David Kilzer (:ddkilzer)">ddkilzer</who>
    <bug_when>2020-04-03 14:23:09 -0700</bug_when>
    <thetext>Use-after-move of `formState` in WebCore::PolicyChecker::checkNavigationPolicy().

The last if/else statement uses formState.get(), but that&apos;s been moved into the `decisionHandler` lambda.

void PolicyChecker::checkNavigationPolicy(ResourceRequest&amp;&amp; request, const ResourceResponse&amp; redirectResponse, DocumentLoader* loader, RefPtr&lt;FormState&gt;&amp;&amp; formState, NavigationPolicyDecisionFunction&amp;&amp; function, PolicyDecisionMode policyDecisionMode)
{
    [...]
    FramePolicyFunction decisionHandler = [this, function = WTFMove(function), request = ResourceRequest(request), formState = WTFMove(formState), suggestedFilename = WTFMove(suggestedFilename),
         blobURLLifetimeExtension = WTFMove(blobURLLifetimeExtension), requestIdentifier, isInitialEmptyDocumentLoad] (PolicyAction policyAction, PolicyCheckIdentifier responseIdentifier) mutable {
        [...]
    };

    if (isInitialEmptyDocumentLoad) {
        // We ignore the response from the client for initial empty document loads and proceed with the load synchronously.
        m_frame.loader().client().dispatchDecidePolicyForNavigationAction(action, request, redirectResponse, formState.get(), policyDecisionMode, requestIdentifier, [](PolicyAction, PolicyCheckIdentifier) { });
        decisionHandler(PolicyAction::Use, requestIdentifier);
    } else
        m_frame.loader().client().dispatchDecidePolicyForNavigationAction(action, request, redirectResponse, formState.get(), policyDecisionMode, requestIdentifier, WTFMove(decisionHandler));
}</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1637530</commentid>
    <comment_count>1</comment_count>
    <who name="David Kilzer (:ddkilzer)">ddkilzer</who>
    <bug_when>2020-04-03 14:32:00 -0700</bug_when>
    <thetext>Not sure the best way to fix this.  Should WebFrameLoaderClient::dispatchDecidePolicyForNavigationAction() be changed to take a RefPtr&lt;FormState&gt;&amp;&amp; instead of a FormState* and the `decisionHandler` just make a copy of `formState` instead of moving it?</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1637547</commentid>
    <comment_count>2</comment_count>
    <who name="Radar WebKit Bug Importer">webkit-bug-importer</who>
    <bug_when>2020-04-03 15:36:46 -0700</bug_when>
    <thetext>&lt;rdar://problem/61278558&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1637550</commentid>
    <comment_count>3</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2020-04-03 15:46:13 -0700</bug_when>
    <thetext>I think Alex found it a few days ago too.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1637551</commentid>
    <comment_count>4</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2020-04-03 15:48:22 -0700</bug_when>
    <thetext>(In reply to Chris Dumez from comment #3)
&gt; I think Alex found it a few days ago too.

I believe he has a pending fix for it.

Also, I don&apos;t see why this is a security bug. The behavior of the RefPtr&lt;&gt; move constructor is well defined. We simply pass null the second time if it was already moved (which was intentional behavior).

Anyway, AFAICT, the current code is safe and correct. I do agree that it is bad practice though and should be written some other way (which I believe Alex is doing).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1637552</commentid>
    <comment_count>5</comment_count>
    <who name="Ryosuke Niwa">rniwa</who>
    <bug_when>2020-04-03 15:55:29 -0700</bug_when>
    <thetext>Okay, back to normal component.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1637554</commentid>
    <comment_count>6</comment_count>
    <who name="Chris Dumez">cdumez</who>
    <bug_when>2020-04-03 15:58:51 -0700</bug_when>
    <thetext>Should be coordinated with Alex because I think he is in the process of refactoring this code (and fixing this at the same time) I believe.

That said, a trivial fix would be to replace WTFMove(formState), std::exchange(formState, nullptr). It is equivalent given our current implementation of the RefPtr&lt;&gt; move constructor but looks safer.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1637567</commentid>
    <comment_count>7</comment_count>
    <who name="Alex Christensen">achristensen</who>
    <bug_when>2020-04-03 16:46:57 -0700</bug_when>
    <thetext>I fixed a similar use-after-move in WebPageProxy::receivedNavigationPolicyDecision in r259307.  We should use std::exchange here and be well defined without changing behavior.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1637569</commentid>
    <comment_count>8</comment_count>
      <attachid>395418</attachid>
    <who name="Alex Christensen">achristensen</who>
    <bug_when>2020-04-03 16:48:49 -0700</bug_when>
    <thetext>Created attachment 395418
Patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>1637574</commentid>
    <comment_count>9</comment_count>
    <who name="EWS">ews-feeder</who>
    <bug_when>2020-04-03 17:16:09 -0700</bug_when>
    <thetext>Committed r259522: &lt;https://trac.webkit.org/changeset/259522&gt;

All reviewed patches have been landed. Closing bug and clearing flags on attachment 395418.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>395418</attachid>
            <date>2020-04-03 16:48:49 -0700</date>
            <delta_ts>2020-04-03 17:16:10 -0700</delta_ts>
            <desc>Patch</desc>
            <filename>bug-209987-20200403164847.patch</filename>
            <type>text/plain</type>
            <size>2134</size>
            <attacher name="Alex Christensen">achristensen</attacher>
            
              <data encoding="base64">SW5kZXg6IFNvdXJjZS9XZWJDb3JlL0NoYW5nZUxvZwo9PT09PT09PT09PT09PT09PT09PT09PT09
PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT09Ci0tLSBTb3VyY2UvV2Vi
Q29yZS9DaGFuZ2VMb2cJKHJldmlzaW9uIDI1OTUyMSkKKysrIFNvdXJjZS9XZWJDb3JlL0NoYW5n
ZUxvZwkod29ya2luZyBjb3B5KQpAQCAtMSwzICsxLDE2IEBACisyMDIwLTA0LTAzICBBbGV4IENo
cmlzdGVuc2VuICA8YWNocmlzdGVuc2VuQHdlYmtpdC5vcmc+CisKKyAgICAgICAgVXNlLWFmdGVy
LW1vdmUgb2YgYGZvcm1TdGF0ZWAgaW4gV2ViQ29yZTo6UG9saWN5Q2hlY2tlcjo6Y2hlY2tOYXZp
Z2F0aW9uUG9saWN5KCkKKyAgICAgICAgaHR0cHM6Ly9idWdzLndlYmtpdC5vcmcvc2hvd19idWcu
Y2dpP2lkPTIwOTk4NworCisgICAgICAgIFJldmlld2VkIGJ5IE5PQk9EWSAoT09QUyEpLgorCisg
ICAgICAgIFVzZSBzdGQ6O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