|
2022-04-18
| ||
| 17:34 | • Closed ticket [ed29806baf]: Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes</... artifact: ae596ae018 user: jan.nijtmans | |
|
2020-11-10
| ||
| 20:43 | • Ticket [ed29806baf]: 3 changes artifact: be576040b0 user: jan.nijtmans | |
|
2020-10-26
| ||
| 13:34 | • Closed ticket [31aa44375d]: Tcl_NumUtfChars regression in default 8.6 build ... artifact: 180d000b00 user: jan.nijtmans | |
|
2020-05-13
| ||
| 18:30 | • Pending ticket [ed29806baf]: Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes<... artifact: d5cc95398c user: jan.nijtmans | |
| 16:51 | Fix [ed29806baf]: Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes check-in: 2a7beb9d18 user: jan.nijtmans tags: core-8-6-branch | |
|
2020-05-02
| ||
| 10:15 | More fixes for [ed29806baf]. Not working yet. WIP check-in: f670d1a41f user: jan.nijtmans tags: bug-ed29806baf-8.6 | |
|
2020-05-01
| ||
| 15:14 | • Ticket [ed29806baf] Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes statu... artifact: 00598a6406 user: jan.nijtmans | |
| 14:20 | Fix first part of [ed29806baf]: Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes. Tcl_UtfToUniCha... check-in: 1d9487bc7e user: jan.nijtmans tags: core-8-branch | |
| 13:38 | Fix first part of [ed29806baf]: Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes. Tcl_UtfToUniChar... check-in: 5f2bb912cf user: jan.nijtmans tags: core-8-6-branch | |
| 08:51 | Fix [ed29806baf] by introducing TclUCS4Complete(). All other calls of Tcl_UtfToUniChar() are suspici... check-in: fb85042c66 user: jan.nijtmans tags: bug-ed29806baf-8.7 | |
|
2020-04-30
| ||
| 14:54 | First, prove that bug [ed29806baf] is present in 8.7 too. Let's see what test-cases fail when we no ... check-in: 5510c6045d user: jan.nijtmans tags: bug-ed29806baf-8.7 | |
| 12:23 | • Ticket [ed29806baf] Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes statu... artifact: 0c49068132 user: jan.nijtmans | |
| 12:09 | Partial fix for [ed29806ba]: Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes. check-in: 98f5334275 user: jan.nijtmans tags: core-8-6-branch | |
|
2020-04-27
| ||
| 19:06 | Create branch to contain efforts at solving ticket [ed29806baf]. check-in: 87ba353929 user: dgp tags: bug-ed29806baf | |
| 19:05 | • New ticket [ed29806baf] Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes. artifact: 99e07e28d1 user: dgp | |
Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes
When iterating through a counted (not NUL-terminated) UTF-8 string via calls to Tcl_UtfToUniChar(), an important issue is how to keep operations from running past the memory approved for use. Failure to do this might involve transgressing memory boundaries, or it might involve reading and acting on uninitialized bits found in memory the caller was never given permission to read. The algorithm embedded in several places in Tcl is to make use of Tcl_UtfCharComplete() to check whether a call to Tcl_UtfToUniChar() is safe to make with a given number of bytes left in the string. One key invariant that these algorithms rely on is that Tcl_UtfCharComplete(p, n) will return true whenever n >= TCL_UTF_MAX. Callers make use of this invariant. They skip making the Tcl_UtfCharComplete() call that they already know will return true, and save those calls only for the tail end of the string when there are (TCL_UTF_MAX - 1) or fewer bytes left. The consequence is that Tcl_UtfToUniChar() should never read more than TCL_UTF_MAX bytes. Starting in Tcl 8.6.10 it is doing so. When p points to a byte in the range 0xF0 - 0xF7, the memory p[3] is read. This eliminates the coding guarantee that these iteration tasks are relying on. These algorithms are in Tcl itself. There was one in Tcl_NumUtfChars, which led to the failure of test utf-4.11 (new since the 8.6.10 release). The Tcl_NumUtfChars routine has been revised on the core-8-6-branch tip to work around this broken contract, but that's not a reasonable answer that can be given to extensions broken by a patch release change in a public routine. These algorithms are also in the encoding machinery of channels. It should not be difficult to cook up a demonstration case where this change will corrupt the buffer management there, or at least a scenario where mem debug or valgrind runs of Tcl will report troubles. I'm not looking for a quick fix here. I know this is a difficult issue enmeshed in work toward very important new functionality, and toward making Tcl begin to offer correctness in string processing that has been missing a long time. I'll keep my efforts at a resolution in a feature branch until they can get review.
180d000b00Closed ticket [31aa44375d]: Tcl_NumUtfChars regression in default 8.6 build plus 5 other changes ticket2020-10-26 13:34:092a7beb9d18Fix [ed29806baf]: Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes check-in2020-05-13 16:51:16f670d1a41fMore fixes for [ed29806baf]. Not working yet. WIP check-in2020-05-02 10:15:291d9487bc7eFix first part of [ed29806baf]: Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes. Tcl_UtfToUniChar() now never reads more than TCL_UTF_MAX bytes any more. Since the UtfToUtf encoder/decoder now uses TclUtfToUCS4() it doesn't join 2 surrogates as 2 x 3-byte sequences any more. Actually, it shouldn't, because such sequences are invalid UTF-8. Therefore, added the ucs2 constraint to testcase encoding-15.4. Let's see how TIP #573 goes, this TIP should make this change official. Other callers of Tcl_UtfToUniChar() needs to be revised for the same problem. Most callers will need to change Tcl_UtfToUniChar() -> TclUtfToUCS4() and Tcl_UtfCharComplete() -> TclUCS4Complete(), but that's not done yet. check-in2020-05-01 14:20:215f2bb912cfFix first part of [ed29806baf]: Tcl_UtfToUniChar reads more than TCL_UTF_MAX bytes. Tcl_UtfToUniChar() now never reads more than TCL_UTF_MAX bytes any more. The UtfToUtf encoder/decoder is adapted to do attitional checks (more tricky than in Tcl 8.7, since we want compatibility with earlier 8.6 releases). Other callers of Tcl_UtfToUniChar() needs to be revised for the same problem. Most callers will need to change Tcl_UtfToUniChar() -> TclUtfToUCS4() and Tcl_UtfCharComplete() -> TclUCS4Complete(), but that's not done yet. check-in2020-05-01 13:38:22fb85042c66Fix [ed29806baf] by introducing TclUCS4Complete(). All other calls of Tcl_UtfToUniChar() are suspicious, because those cannot handle 4-byte UTF-8 sequences reliable. So, there's more work to do, but this part can already be backported to Tcl 8.6 and see where we get. check-in2020-05-01 08:51:095510c6045dFirst, prove that bug [ed29806baf] is present in 8.7 too. Let's see what test-cases fail when we no longer check the validity of the 3th trail byte. check-in2020-04-30 14:54:5987ba353929Create branch to contain efforts at solving ticket [ed29806baf]. check-in2020-04-27 19:06:43
Not a 100% fix yet, but some improvement is possible without disturbing anything. Most code in Tcl_UtfToUniChar() is just validity checks, checking if src[3] is valid is just one of them. We can do the validity check for src[3] after all other validity checks that can be done on src[0], src[1] and src[2]. At least, the probability that this leads to a crash (however small already) becomes even smaller.
B.T.W.: Tcl 8.7 (the core-8-branch) has the same issue!
This is not all, more can be said about it. This first paritial fix is done in commit [98f53342750c9020], but I will come back to this issue later.
First part of fix committed now to core-8-6-branch, core-8-branch and trunk (done differently on 8.6 compared to 8.7/9.0 due to TIP #389). Keeping open, because there are a lot other calls to Tcl_UtfToUniChar() in the code. They all are suspicious, and should posibly be replaced by TclUtfToUCS4(). There is now a utility macro TclUCS4Complete() that can protect TclUtfToUCS4() against overflow. (naming could be better .... ideas?)
Fixed now in core-8-6-branch. Most Tcl_UtfToUniChar() calls in the core are replaced now by Tcl_UtdToUCS4(), and protection by Tcl_UtfCharComplete() has been verified. I believe I got all of it correct now.
So, changed to Pending/Fixed. Please verify.
For TCL_UTF_MAX> 3 there has not decided yet how Tcl_UtfPrev()/Tcl_UtfNext() should behave. I assumed TIP #575 behavior, which is not approved yet , neither implemented in core-8-branch. So, there's more work to do for 8.7. I suggest to keep the behavior for TCL_UTF_MAX>3 in core-8-6-branch as-is, so we have a reference to compare with. As soon as TIP #575 is accepted and everything implemented accordingly in core-8-branch, the behavior can be backported to 8.6 with TCL_UTF_MAX>3.
@dgp. Noting this commit: [87d6d589a62387d3], it looks like you are reviewing this ticket's solution in core-8-6-branch. Am I right?