Skip to content
Tcl Source Code View Ticket
Login

View Ticket

2017-05-05
19:18 • Closed ticket [6015221f59]: binary scan segfaults when too much a is requested</... artifact: dfb562cf4d user: dgp
19:11
[6015221f59] Segfault after overflow of [binary] field specifier numeric count. check-in: 5c07ae32cf user: dgp tags: trunk
19:10
[6015221f59] Segfault after overflow of [binary] field specifier numeric count. check-in: 56c454c814 user: dgp tags: core-8-6-branch
17:12
[6015221f59] Segfault after overflow of [binary] field specifier numeric count. check-in: 0055a16a8b user: dgp tags: core-8-5-branch
2017-05-04
16:00 • Ticket [6015221f59] binary scan segfaults when too much a is requested statu... artifact: 8a305859d0 user: dgp
2017-05-03
09:49 • Ticket [6015221f59]: 3 changes artifact: 8132ecf041 user: sebres
09:29 • Ticket [6015221f59]: 7 changes artifact: 0a294c5250 user: sebres
07:50 • Ticket [6015221f59]: 4 changes artifact: 136ef3e43e user: jan.nijtmans
06:21 • Ticket [6015221f59]: 3 changes artifact: ac56560238 user: aspect
02:55 • New ticket [6015221f59]. artifact: 90eaad9614 user: anonymous

binary scan segfaults when too much a is requested

Closed / Fixed6015221f59fd742aac833c7a4521de9745c8c868Created 10 years agoUpdated 10 years ago
Descriptionplain2017-05-03 02:55:38
if "binary scan" is requesting too much character data, it just segfaults instead of complaining that too much data is requested.

% binary scan x a172983798173981a r n
Segmentation fault (core dumped)
Referenced by3
  • 5c07ae32cf[6015221f59] Segfault after overflow of [binary] field specifier numeric count. check-in2017-05-05 19:11:33
  • 56c454c814[6015221f59] Segfault after overflow of [binary] field specifier numeric count. check-in2017-05-05 19:10:52
  • 0055a16a8b[6015221f59] Segfault after overflow of [binary] field specifier numeric count. check-in2017-05-05 17:12:52
aspectplain2017-05-03 06:21:31
playing around in gdb on core-8-5-branch, the problem in this case seems to be that after GetFormatSpec(&format, &cmd, &count, &flags), count is added to offset making it point outside the buffer.

Specifically, this is wrong:

GetFormatSpec( .. int *countPtr .. ) {
   (*countPtr) = strtoul( .. )
}

Similar issue likely exists in [binary format].

Simplest fix is probably to return 0 if the strtoul() result is <0.  Else Tcl_BinaryObjCmd() needs to check after each call whether count is BINARY_UNSIGNED, BINARY_ALL, >=0 and otherwise TCL_ERROR.

Note:  this looks like a relatively easily exploitable memory corruption bug, for anywhere untrusted users can provide format arguments to [binary].
jan.nijtmansplain2017-05-03 07:50:58
Actually, Segmentation faults from pure Tcl code is always bad ....

It would be nice if this could be fixed in at least Tcl 8.6.7. Suggestion for a fix always welcome?
sebresplain2017-05-03 09:29:25
I guess something like this in GetFormatSpec would be more appreciated:
```
if (*countPtr) < 0 {
  countPtr = BINARY_ALL;
}
```
According to documentation "count is a non-negative decimal integer", so I think in this case BINARY_ALL is a little bit better as boom.

If no objections I would fix it this way in all 3 major branches (including test cases).

BTW. Strange is, that at all not reproduceable under windows (neither x86 nor x64), but pretty segfaulting on linux in all versions.
sebresplain2017-05-03 09:49:27
Another way would be to redefine GetFormatSpec (and count as unsigned), like:

  GetFormatSpec( .. unsigned int *countPtr .. )
  unsigned int count;

and 

  #define BINARY_ALL ((unsigned int)-1)
  ...

So "overflow" will reach both special cases only by following ranges (accoring INT_MAX):

  binary scan x a[format %ld 0xffffffff]a r n; # equal BINARY_ALL
  binary scan x a[format %ld 0xfffffffe]a r n; # equal BINARY_UNSIGNED
dgpplain2017-05-04 16:00:31
Seems to me the root of the issue is making a
call to strtoul() without checking for the
various errors that can result from it.

This has been wrong since the dawn of Tcl's
CVS history.

There are a handful of other strtoul() calls
here and there in Tcl. An audit of all of them
would be a good idea.
dgpplain2017-05-05 19:18:30
fixed on all dev branches
Log in (anonymous is fine) to add a comment.