<?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>91642</bug_id>
          
          <creation_ts>2012-07-18 10:54:49 -0700</creation_ts>
          <short_desc>OOB read of stack buffer below DoubleToStringConverter::CreateExponentialRepresentation() in debug builds</short_desc>
          <delta_ts>2012-07-18 15:16:26 -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>WebCore Misc.</component>
          <version>528+ (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></keywords>
          <priority>P2</priority>
          <bug_severity>Normal</bug_severity>
          <target_milestone>---</target_milestone>
          
          
          <everconfirmed>1</everconfirmed>
          <reporter name="Thomas Sepez">tsepez</reporter>
          <assigned_to name="Thomas Sepez">tsepez</assigned_to>
          <cc>darin</cc>
    
    <cc>eae</cc>
    
    <cc>inferno</cc>
    
    <cc>leviw</cc>
    
    <cc>mhahnenberg</cc>
    
    <cc>webkit.review.bot</cc>
          

      

      

      

          <comment_sort_order>oldest_to_newest</comment_sort_order>  
          <long_desc isprivate="0" >
    <commentid>672526</commentid>
    <comment_count>0</comment_count>
    <who name="Thomas Sepez">tsepez</who>
    <bug_when>2012-07-18 10:54:49 -0700</bug_when>
    <thetext>Extracted from https://bugs.webkit.org/show_bug.cgi?id=91151

The ASAN-ified debug build trips over an OOB read in an assert() which (obviously) is not present in the release build.  Although this has no user impact, it limits the use of ASAN debug builds to diagnose issues.

The call stack looks like:

=================================================================
==6469== ERROR: AddressSanitizer stack-buffer-overflow on address 0x7fffaf275165 at pc 0x1200649 bp 0x7fffaf274c30 sp 0x7fffaf274c10
READ of size 1 at 0x7fffaf275165 thread T0
    #0 0x1200649 in __interceptor_strlen 
    #1 0x7f5f5ded5a6a in WTF::double_conversion::StringBuilder::AddSubstring(char const*, int) third_party/WebKit/Source/WTF/wtf/dtoa/utils.h:231
    #2 0x7f5f5dec6155 in WTF::double_conversion::DoubleToStringConverter::CreateExponentialRepresentation(char const*, int, int, WTF::double_conversion::StringBuilder*) const third_party/WebKit/Source/WTF/wtf/dtoa/double-conversion.cc:113
    #3 0x7f5f5dec8a1b in WTF::double_conversion::DoubleToStringConverter::ToShortest(double, WTF::double_conversion::StringBuilder*) const third_party/WebKit/Source/WTF/wtf/dtoa/double-conversion.cc:195
    #4 0x7f5f5de5fb8b in WTF::numberToString(double, char*) third_party/WebKit/Source/WTF/wtf/dtoa.cpp:1235
    #5 0x7f5f5f6825ce in WebCore::Decimal::fromDouble(double) third_party/WebKit/Source/WebCore/platform/Decimal.cpp:686
    #6 0x7f5f5f3f23bb in WebCore::parseToDecimalForNumberType(WTF::String const&amp;, WebCore::Decimal const&amp;) third_party/WebKit/Source/WebCore/html/parser/HTMLParserIdioms.cpp:96

This appears to be introduced in 94452.

The reason for this is that in wtf/dtoa/utils.h, StringBuilder::addSubstring() takes both a pointer and a length, but asserts:
   ASSERT(static_cast&lt;size_t&gt;(n) &lt;= strlen(s));
which implies that the input |s| must be NUL-terminated despite otherwise being fully described by the length.

DoubleToStringConverter::CreateExponentialRepresentation() declares an array 
  char buffer[kMaxExponentLength];
but doesn&apos;t NUL-terminate it before passing it to addSubstring().

The other callers of addSubstring() in this file appear be passing NUL-terminated buffers, so the easiest fix is to NUL-terminate this one as well.  I&apos;ll cobble up a patch to correct this as it&apos;s blocking some of my other ASAN work.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>672700</commentid>
    <comment_count>1</comment_count>
      <attachid>153076</attachid>
    <who name="Thomas Sepez">tsepez</who>
    <bug_when>2012-07-18 13:28:06 -0700</bug_when>
    <thetext>Created attachment 153076
Patch</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>672722</commentid>
    <comment_count>2</comment_count>
      <attachid>153076</attachid>
    <who name="Abhishek Arya">inferno</who>
    <bug_when>2012-07-18 13:52:50 -0700</bug_when>
    <thetext>Comment on attachment 153076
Patch

View in context: https://bugs.webkit.org/attachment.cgi?id=153076&amp;action=review

Was the testcase minimizable ? i know it looked ugly on ClusterFuzz.

&gt; Source/WTF/ChangeLog:9
&gt; +        (DoubleToStringConverter::CreateExponentialRepresentation): NUL-terminate string buffer before passing it to StringBuilder::AddSubstring()

typo s/NUL/NULL

&gt; Source/WTF/wtf/dtoa/double-conversion.cc:107
&gt; +        buffer[first_char_pos] = &apos;\0&apos;;

nit: it will be more readable to use kMaxExponentLength instead of first_char_pos.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>672732</commentid>
    <comment_count>3</comment_count>
    <who name="Thomas Sepez">tsepez</who>
    <bug_when>2012-07-18 14:11:52 -0700</bug_when>
    <thetext>(In reply to comment #2)
&gt; (From update of attachment 153076 [details])
&gt; View in context: https://bugs.webkit.org/attachment.cgi?id=153076&amp;action=review
&gt; 
&gt; Was the testcase minimizable ? i know it looked ugly on ClusterFuzz.
&gt; 
&gt; &gt; Source/WTF/ChangeLog:9
&gt; &gt; +        (DoubleToStringConverter::CreateExponentialRepresentation): NUL-terminate string buffer before passing it to StringBuilder::AddSubstring()
&gt; 
&gt; typo s/NUL/NULL

I&apos;ve always believed that NULL is a pointer but NUL is the character (3-letter abbr from the TTY days, see man ascii).  There&apos;s some use of this form in WebCore/icu for example.  Let me know which way you want it.
&gt; 
&gt; &gt; Source/WTF/wtf/dtoa/double-conversion.cc:107
&gt; &gt; +        buffer[first_char_pos] = &apos;\0&apos;;
&gt; 
&gt; nit: it will be more readable to use kMaxExponentLength instead of first_char_pos.
Perhaps.  But the rest of the assignments are relative to first_char_pos.  If later on, someone decides to initialize first_char_pos to some other value, then this code still works unchanged and we don&apos;t get a gap between the characters from the loop below and the terminator.  Or so was my thinking when I did it this way.  Let me know which way you want it.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>672739</commentid>
    <comment_count>4</comment_count>
      <attachid>153076</attachid>
    <who name="Abhishek Arya">inferno</who>
    <bug_when>2012-07-18 14:18:05 -0700</bug_when>
    <thetext>Comment on attachment 153076
Patch

Thanks for the explanation.</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>672750</commentid>
    <comment_count>5</comment_count>
    <who name="Thomas Sepez">tsepez</who>
    <bug_when>2012-07-18 14:23:05 -0700</bug_when>
    <thetext>(In reply to comment #4)
&gt; (From update of attachment 153076 [details])
&gt; Thanks for the explanation.

RE: testcase.  It&apos;s not practical without automated ASAN/Valgrind debug binary setup to test this.   Had there been one, this would go off all over the place as lots of existing code/tests hits this path (opening the devtools window does it pretty reliably on my chromium binary).</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>672825</commentid>
    <comment_count>6</comment_count>
      <attachid>153076</attachid>
    <who name="WebKit Review Bot">webkit.review.bot</who>
    <bug_when>2012-07-18 15:16:21 -0700</bug_when>
    <thetext>Comment on attachment 153076
Patch

Clearing flags on attachment: 153076

Committed r123027: &lt;http://trac.webkit.org/changeset/123027&gt;</thetext>
  </long_desc><long_desc isprivate="0" >
    <commentid>672826</commentid>
    <comment_count>7</comment_count>
    <who name="WebKit Review Bot">webkit.review.bot</who>
    <bug_when>2012-07-18 15:16:26 -0700</bug_when>
    <thetext>All reviewed patches have been landed.  Closing bug.</thetext>
  </long_desc>
      
          <attachment
              isobsolete="0"
              ispatch="1"
              isprivate="0"
          >
            <attachid>153076</attachid>
            <date>2012-07-18 13:28:06 -0700</date>
            <delta_ts>2012-07-18 15:16:21 -0700</delta_ts>
            <desc>Patch</desc>
            <filename>patch-91642.txt</filename>
            <type>text/plain</type>
            <size>1414</size>
            <attacher name="Thomas Sepez">tsepez</attacher>
            
              <data encoding="base64">SW5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T09PT09PT09PT09PT09PT09PT09PT09PT09PT09PT0KLS0t
IFNvdXJjZS9XVEYvd3RmL2R0b2EvZG91YmxlLWNvbnZlcnNpb24uY2MJKHJldmlzaW9uIDEyMzAw
MCkKKysrIFNvdXJjZS9XVEYvd3RmL2R0b2EvZG91YmxlLWNvbnZlcnNpb24uY2MJKHdvcmtpbmcg
Y29weSkKQEAgLTEwMiw4ICsxMDIsOSBAQCBuYW1lc3BhY2UgZG91YmxlX2NvbnZlcnNpb24gewog
ICAgICAgICB9CiAgICAgICAgIEFTU0VSVChleHBvbmVudCA8IDFlNCk7CiAgICAgICAgIGNvbnN0
IGludCBrTWF4RXhwb25lbnRMZW5ndGggPSA1OwotICAgICAgICBjaGFyIGJ1ZmZlcltrTWF4RXhw
b25lbnRMZW5ndGhdOworICAgICAgICBjaGFyIGJ1ZmZlcltrTWF4RXhwb25lbnRMZW5ndGggKyAx
XTsKICAgICAgICAgaW50IGZpcnN0X2NoYXJfcG9zID0ga01heEV4cG9uZW50TGVuZ3RoOworICAg
ICAgICBidWZmZXJbZmlyc3RfY2hhcl9wb3NdID0gJ1wwJzsKICAgICAgICAgd2hpbGUgKGV4cG9u
ZW50ID4gMCkgewogICAgICAgICAgICAgYnVmZmVyWy0tZmlyc3RfY2hhcl9wb3NdID0gJzAnICsg
KGV4cG9uZW50ICUgMTApOwogICAgICAgICAgICAgZXhwb25lbnQgLz0gMTA7Cg==
</data>

          </attachment>
      

    </bug>

</bugzilla>