| Ticket UUID: | 69218ab7bf0014c813964631b955509e4d16ae44 | ||
| Title: | TEBCResume(): buffer over-read in INST_STR_MAP | ||
| Type: | Patch | Created on: | 2021-12-22 18:45:50 |
| Submitter: | chrstphrchvz | Assigned to: | jan.nijtmans |
| Subsystem: | 18. Commands M-Z | Severity: | Minor |
| Priority: | 5 Medium | Last modified: | 2022-01-14 22:53:41 |
| Status: | Closed | Closed by: | jan.nijtmans |
| Resolution: | Fixed | Closed on: | 2022-01-14 22:53:41 |
| Version: | 8.6.12 | ||
| Description: | ||||
|
Example:
AddressSanitizer (-fsanitize=address, used with -DPURIFY, Linux x86) error: ================================================================= ==16557==ERROR: AddressSanitizer: heap-buffer-overflow on address 0xb143d0e0 at pc 0xb7a8d726 bp 0xbfbf2438 sp 0xbfbf2008 READ of size 6 at 0xb143d0e0 thread T0 #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) 0xb143d0e0 is located 0 bytes to the right of 32-byte region [0xb143d0c0,0xb143d0e0) allocated by thread T0 here: #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) This is caused by memcmp() 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, memcmp() 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 StringMapCmd() (generic/tclCmdMZ.c) since [fab27ad2b82f] because Tcl_UniCharNcmp() and Tcl_UniCharNcasecmp() likewise assume both strings are at least the specified number of characters long.
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++) {
- if ((*ustring1 == *ustring2) && (length2==1 ||
+ if ((*ustring1 == *ustring2) &&
+ /* Fix bug hhhhhhhhhh: restrict max compare length. */
+ (end-ustring1 >= length2) && (length2==1 ||
memcmp(ustring1, ustring2, sizeof(Tcl_UniChar) * length2)
== 0)) {
if (p != ustring1) {
| ||||
| User Comments: | ||||
jan.nijtmans added on 2022-01-12 15:51:59:
Fix committed [0f45a92d03741a43|here] Thanks for the report and patch! | ||||
