Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2018-07-27
17:29 • Ticket [d051b77fc1] segmentation fault in tclStringObj.c, line 2405 status s... artifact: c898f735f1 user: sebres
15:31 • Ticket [d051b77fc1]: 5 changes artifact: f53cf21cc2 user: dgp
2018-07-26
18:58
merge 8.6: bug [d051b77fc1] fixed check-in: 4e57a1242b user: sebres tags: core-8-branch
18:51
merge 8.5 - bug [d051b77fc1] fixed check-in: de522f0a45 user: sebres tags: core-8-6-branch
16:03 • Ticket [d051b77fc1] segmentation fault in tclStringObj.c, line 2405 status s... artifact: 7b31d54f6a user: sebres
16:01 • Closed ticket [d051b77fc1]. artifact: 760654989f user: sebres
15:57
closes [d051b77fc18d7340]: fixed segfault by integer overflow (if width by format like "%4000000000g... check-in: 16846911c7 user: sebres tags: core-8-5-branch
15:24 • Ticket [d051b77fc1] segmentation fault in tclStringObj.c, line 2405 status s... artifact: 78d4f86974 user: aspect
2018-07-25
13:57 • Ticket [d051b77fc1]: 4 changes artifact: dc2dc3a1c3 user: sebres
13:25 • New ticket [d051b77fc1]. artifact: 5d11be0b90 user: jfe

segmentation fault in tclStringObj.c, line 2405

Closed / Fixedd051b77fc18d7340dbc7db61897461a8784ac32eCreated 8 years agoUpdated 8 years ago
Descriptionplain2018-07-25 13:25:55
Hi,
I am working on a project in which I use the afl fuzzer to find bugs in open-source software and I decided to fuzz tcl.
In doing so, I discovered a segmentation fault in generic/tclStringObj.c, line 2405. Following is a detailed backtrace
as supplied by the address sanitizer:

ASAN:DEADLYSIGNAL
=================================================================
==65478==ERROR: AddressSanitizer: SEGV on unknown address 0x629000090000 (pc 0x7f3ee8d2b9dc bp 0x000000000d68 sp 0x7fffcfede090 T0)
==65478==The signal is caused by a WRITE memory access.
    #0 0x7f3ee8d2b9db in _IO_default_xsputn (/lib/x86_64-linux-gnu/libc.so.6+0x7b9db)
    #1 0x7f3ee8d1ee65 in _IO_padn (/lib/x86_64-linux-gnu/libc.so.6+0x6ee65)
    #2 0x7f3ee8d02cf7  (/lib/x86_64-linux-gnu/libc.so.6+0x52cf7)
    #3 0x7f3ee8cfe360 in vfprintf (/lib/x86_64-linux-gnu/libc.so.6+0x4e360)
    #4 0x7f3ee8d200d0 in _IO_vsprintf (/lib/x86_64-linux-gnu/libc.so.6+0x700d0)
    #5 0x7f3eea43ba28 in vsprintf (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x9aa28)
    #6 0x7f3eea43be56 in sprintf (/usr/lib/x86_64-linux-gnu/libasan.so.4+0x9ae56)
    #7 0x7f3ee9f90b3b in Tcl_AppendFormatToObj /home/jfe/tcl/generic/tclStringObj.c:2405
    #8 0x7f3ee9f97dc7 in Tcl_Format /home/jfe/tcl/generic/tclStringObj.c:2513
    #9 0x7f3ee9bc62b8 in TclCompileFormatCmd /home/jfe/tcl/generic/tclCompCmds.c:3184
    #10 0x7f3ee9dc16cd in TclAttemptCompileProc /home/jfe/tcl/generic/tclEnsemble.c:3256
    #11 0x7f3ee9d62c85 in CompileCmdCompileProc /home/jfe/tcl/generic/tclCompile.c:1947
    #12 0x7f3ee9d62c85 in CompileCommandTokens /home/jfe/tcl/generic/tclCompile.c:2062
    #13 0x7f3ee9d62c85 in TclCompileScript /home/jfe/tcl/generic/tclCompile.c:2193
    #14 0x7f3ee9d6404a in TclSetByteCodeFromAny /home/jfe/tcl/generic/tclCompile.c:800
    #15 0x7f3ee9dd8ebe in TclCompileObj /home/jfe/tcl/generic/tclExecute.c:1689
    #16 0x7f3ee9b28aab in TclNREvalObjEx /home/jfe/tcl/generic/tclBasic.c:5668
    #17 0x7f3ee9b29340 in TclEvalObjEx /home/jfe/tcl/generic/tclBasic.c:5546
    #18 0x7f3ee9e2bf3b in Tcl_RecordAndEvalObj /home/jfe/tcl/generic/tclHistory.c:182
    #19 0x7f3ee9ee4b61 in Tcl_MainEx /home/jfe/tcl/generic/tclMain.c:537
    #20 0x55db82b8fc62 in main /home/jfe/tcl/unix/tclAppInit.c:84
    #21 0x7f3ee8cd1a86 in __libc_start_main (/lib/x86_64-linux-gnu/libc.so.6+0x21a86)
    #22 0x55db82b8fc99 in _start (/home/jfe/tcl/unix/tclsh+0xc99)

AddressSanitizer can not provide additional info.
SUMMARY: AddressSanitizer: SEGV (/lib/x86_64-linux-gnu/libc.so.6+0x7b9db) in _IO_default_xsputn
==65478==ABORTING

This bug can be reproduced by running:
echo "format %6600000000g 0"|tclsh

cheers,
project-repo
Referenced by2
sebresplain2018-07-25 13:57:58
Hmmm... seems to be *nix bug (or sprintf/stdlib) only, I can pretty good reproduce this there, trying since v.8.5.
Not reproducible on windows, no matter which compiler (VC or gcc) and target (x86 or x64) selected.
(Of course by x86 I tried shorter prefix).

Although sprintf is known as vulnerable, we should try to find the way to fix it (meant to avoid segfault).
aspectplain2018-07-26 15:24:04
This crash can be stopped by changing tclStringObj.c at line 1870 (trunk):

-       if (width > limit) {
+       if (width > limit || width < 0) {

but the underlying issue is earlier:

   int width;
   width = strtoul(..);

looks like there are several signed int's that are assumed to not overflow,
all should be examined.
sebresplain2018-07-26 16:01:48
fixed now within [16846911c7] for 8.5, can be simply merged into upper branches
sebres Ticket [d051b77fc1]: 4 changes [History diff]2018-07-26 16:03:13
dgpplain2018-07-27 15:31:03
There's plenty more mis-use of strtoul() lurking in
the [format] implementation for an interested programmer
to go in and correct.  One example:

% format {%4294967297$s} foo
foo

Possibly conversion to make use of TclParseNumber
is a better idea.
sebresplain2018-07-27 17:29:27
On which version you have tested this, Don? and x64/x86?

(I thing the $ was a typo, but) I get following:
<code><pre>
% format {%4294967297$s} foo
"%n$" argument index out of range
% format {%4294967297s} foo
max size for a Tcl value exceeded
</pre></code>

Possibly the limit is large...
Log in (anonymous is fine) to add a comment.