Skip to content
Tcl Source Code Ticket Change Details
Login

Ticket Change Details

Overview

Artifact ID: 2cfe68c3e28b4c7581b019325e5b38f59128c1f22ce08af9eeb42ee6ad5738be
Ticket: 69218ab7bf0014c813964631b955509e4d16ae44
TEBCResume(): buffer over-read in INST_STR_MAP
User & Date: chrstphrchvz on 2021-12-22 18:45:50
Changes

  1. assignee changed to: "nobody"
  2. closer changed to: "nobody"
  3. cmimetype changed to: "text/x-fossil-wiki"
  4. comment changed to:
    Example:
    
    <blockquote><pre>
    proc myproc {s} {
        puts [string map {abc def} $s]
    }
    
    myproc {zzzzzza}
    </pre></blockquote>
    
    AddressSanitizer (<tt>-fsanitize=address</tt>, used with <tt>-DPURIFY</tt>, Linux x86) error:
    
    <blockquote><pre>
    =================================================================
    <span style="font-weight:bold;color:red">==16557==ERROR: AddressSanitizer: heap-buffer-overflow on address 0xb143d0e0 at pc 0xb7a8d726 bp 0xbfbf2438 sp 0xbfbf2008</span>
    <span style="font-weight:bold;color:blue">READ of size 6 at 0xb143d0e0 thread T0</span>
        #0 0xb7a8d725 in MemcmpInterceptorCommon(void*, int (*)(void const*, void const*, unsigned long), void const*, void const*, unsigned long) /build/gcc/src/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:860
        #1 0xb7a8dd48 in __interceptor_memcmp /build/gcc/src/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:892
        #2 0xb7a8dd48 in __interceptor_memcmp /build/gcc/src/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:887
        #3 0xb63ddad3 in TEBCresume tcl/generic/tclExecute.c:5859
        #4 0xb5d74c43 in TclNRRunCallbacks tcl/generic/tclBasic.c:4503
        #5 0xb5d719e4 in Tcl_EvalObjv tcl/generic/tclBasic.c:4226
        #6 0xb5d80013 in TclEvalEx tcl/generic/tclBasic.c:5372
        #7 0xb653cafa in Tcl_FSEvalFileEx tcl/generic/tclIOUtil.c:1824
        #8 0xb6577599 in Tcl_MainEx tcl/generic/tclMain.c:403
        #9 0x447253 in main tcl/unix/tclAppInit.c:84
        #10 0xb4f7998c in __libc_start_main (/usr/lib/libc.so.6+0x1e98c)
        #11 0x447114 in _start (tclsh8.6+0x1114)
    
    <span style="font-weight:bold;color:green">0xb143d0e0 is located 0 bytes to the right of 32-byte region &#91;0xb143d0c0,0xb143d0e0)</span>
    <span style="font-weight:bold;color:magenta">allocated by thread T0 here:</span>
        #0 0xb7aaf99d in __interceptor_realloc /build/gcc/src/gcc/libsanitizer/asan/asan_malloc_linux.cpp:164
        #1 0xb5d2b7de in TclpRealloc tcl/generic/tclAlloc.c:747
        #2 0xb5dc036a in Tcl_Realloc tcl/generic/tclCkalloc.c:1147
        #3 0xb665b526 in GrowUnicodeBuffer tcl/generic/tclStringObj.c:227
        #4 0xb6671b61 in ExtendUnicodeRepWithString tcl/generic/tclStringObj.c:3083
        #5 0xb6671602 in FillUnicodeRep tcl/generic/tclStringObj.c:3058
        #6 0xb665dcb5 in Tcl_GetUnicodeFromObj tcl/generic/tclStringObj.c:725
        #7 0xb63dd3aa in TEBCresume tcl/generic/tclExecute.c:5835
        #8 0xb5d74c43 in TclNRRunCallbacks tcl/generic/tclBasic.c:4503
        #9 0xb5d719e4 in Tcl_EvalObjv tcl/generic/tclBasic.c:4226
        #10 0xb5d80013 in TclEvalEx tcl/generic/tclBasic.c:5372
        #11 0xb653cafa in Tcl_FSEvalFileEx tcl/generic/tclIOUtil.c:1824
        #12 0xb6577599 in Tcl_MainEx tcl/generic/tclMain.c:403
        #13 0x447253 in main tcl/unix/tclAppInit.c:84
        #14 0xb4f7998c in __libc_start_main (/usr/lib/libc.so.6+0x1e98c)
    
    SUMMARY: AddressSanitizer: heap-buffer-overflow /build/gcc/src/gcc/libsanitizer/sanitizer_common/sanitizer_common_interceptors.inc:860 in MemcmpInterceptorCommon(void*, int (*)(void const*, void const*, unsigned long), void const*, void const*, unsigned long)
    Shadow bytes around the buggy address:
      0x362879c0: <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span>
      0x362879d0: <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> 00 00 00 00
      0x362879e0: <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> 00 00 04 <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> 00 00 00 <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span>
      0x362879f0: <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span>
      0x36287a00: <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span>
    =>0x36287a10: <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> 00 00 00 00&#91;<span style="font-weight:bold;color:red">fa</span>&#93;<span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span>
      0x36287a20: <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> 00 00 00 <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> 00 00 00 <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span>
      0x36287a30: 00 00 00 <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> 00 00 04 <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> 00 00 04 <span style="font-weight:bold;color:red">fa</span>
      0x36287a40: <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span>
      0x36287a50: <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span>
      0x36287a60: <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:red">fa</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:magenta">fd</span> <span style="font-weight:bold;color:red">fa</span>
    Shadow byte legend (one shadow byte represents 8 application bytes):
      Addressable:           00
      Partially addressable: 01 02 03 04 05 06 07 
      Heap left redzone:       <span style="font-weight:bold;color:red">fa</span>
      Freed heap region:       <span style="font-weight:bold;color:magenta">fd</span>
    […]
    ==16557==ABORTING
    </pre></blockquote>
    
    This is caused by <tt>memcmp()</tt> being called even when the remaining portion of the operand string is shorter than the match string, present since [b6871e5b803d]. Unlike some other string comparison functions, <tt>memcmp()</tt> requires buffers to be valid for the length specified even if they differ within the valid range of the shorter buffer (I am aware some find this restriction controversial). I believe the fix is to check whether the remainder of the string is at least as long as the match string, comparable to what is done in <tt>StringMapCmd()</tt> (generic/tclCmdMZ.c) since [fab27ad2b82f] because <tt>Tcl_UniCharNcmp()</tt> and <tt>Tcl_UniCharNcasecmp()</tt> likewise assume both strings are at least the specified number of characters long.
    
    <pre>
    diff --git generic/tclExecute.c generic/tclExecute.c
    index 7e014d4..accdbe7 100644
    --- generic/tclExecute.c
    +++ generic/tclExecute.c
    @@ -5855,7 +5855,9 @@ TEBCresume(
     	p = ustring1;
     	end = ustring1 + length;
     	for (; ustring1 < end; ustring1++) {
    <span style="color:red">-	    if ((*ustring1 == *ustring2) && (length2==1 ||</span>
    <span style="color:green">+	    if ((*ustring1 == *ustring2) &&
    +		/* Fix bug hhhhhhhhhh: restrict max compare length. */
    +		(end-ustring1 >= length2) && (length2==1 ||</span>
     		    memcmp(ustring1, ustring2, sizeof(Tcl_UniChar) * length2)
     			    == 0)) {
     		if (p != ustring1) {
    </pre>
    
  5. foundin changed to: "8.6.12"
  6. is_private changed to: "0"
  7. login: "chrstphrchvz"
  8. priority changed to: "5 Medium"
  9. resolution changed to: "None"
  10. severity changed to: "Minor"
  11. status changed to: "Open"
  12. submitter changed to: "chrstphrchvz"
  13. subsystem changed to: "18. Commands M-Z"
  14. title changed to: "TEBCResume(): buffer over-read in INST_STR_MAP"
  15. type changed to: "Patch"