| Ticket UUID: | 6015221f59fd742aac833c7a4521de9745c8c868 | ||
| Title: | binary scan segfaults when too much a is requested | ||
| Type: | Bug | Created on: | 2017-05-03 02:55:38 |
| Submitter: | anonymous | Assigned to: | dgp |
| Subsystem: | 12. ByteArray Object | Severity: | Critical |
| Priority: | 7 High | Last modified: | 2017-05-05 19:18:30 |
| Status: | Closed | Closed by: | dgp |
| Resolution: | Fixed | Closed on: | 2017-05-05 19:18:30 |
| Version: | >= 8.5 (*nix only) | ||
| Description: | ||||
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) | ||||
| User Comments: | ||||
aspect added on 2017-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.nijtmans added on 2017-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? sebres added on 2017-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.
sebres added on 2017-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 dgp added on 2017-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. dgp added on 2017-05-05 19:18:30:
fixed on all dev branches | ||||
