Skip to content
Tcl Source Code View Ticket
Login

View Ticket

Usage of static systemEncoding is not thread safe (vulnerability)

Openf2ff05fc8411be80a93913b2b065f2c85fa9bcf0Created 6 years agoUpdated 47 days ago
Descriptionwiki2021-04-06 16:14:12
Tcl_UtfToExternal*, Tcl_ExternalToUtf* (et al.) have a certain weakness if used with NULL (system-encoding). If for some reason systemEncoding is a single reference (used only once), it will be used in that functions without to touch its refCount, so if another thread sets it to different value - it could segfault inside conversion if this previous system encoding gets freed.

I know that normally the change of system encoding is either rare case, but a vulnerability remains vulnerability...

Atomic incr/decr of its refCount may help here... But currently (without to have such atomic facilities) it would imply to use mutex, and it would be ugly by each conversion (would provide a multi-threaded bottleneck, even twice by start and end of convert-routines).

As for atomic facilities, I made some for another issue - for example see sebres/tcl/1e522ff/tclInt.h#L131.

Simplest scenario to reproduce the SF (for understanding):

Tcl_SetSystemEncoding(NULL, "enc1");
... thread::create { encoding convertfrom [string repeat "..." $Some_Large_N] }; 
Tcl_SetSystemEncoding(NULL, "enc2"); 
// BOOM in helper thread (if 2nd set takes place during convertfrom process and "enc1" is unused elsewhere)...
Referenced by5
  • d518a8ab5dFix [f2ff05fc84]: a conversion with the system encoding could crash if another thread changed the system encoding. Keep former system encodings until finalization, so that a conversion which read systemEncoding can still use it, without locking. Also Tcl_GetEncoding(NULL, NULL) now returns the encoding it took the reference to. check-in2026-10-17 08:06:11
  • 3f2c8da63aTicket [87b69745be] interp creation resets encoding directory search path status still Open with 3 other changes ticket2025-08-14 11:47:28
  • fc3258f5aaTicket [2570123b63] system encoding is not protected from freeing during encode/decode operations status still Open with 3 other changes ticket2025-08-13 17:59:20
  • 1aa7e19a58Ticket [87b69745be] interp creation resets encoding directory search path status still Open with 3 other changes ticket2025-08-13 16:11:19
  • e0925741a7Ticket [61c01e0edb] likely race condition with package require in several threads causing error status still Open with 3 other changes ticket2025-07-17 12:40:11
sebres Ticket [f2ff05fc84]: 3 changes [History diff]2021-04-06 16:14:45
jan.nijtmanswiki2021-04-07 09:05:34

Any suggestion for a solution? Maybe store the system-encoding in thread local storage? That would mean that each thread has its own system-encoding. And that any time a new thread is created, it takes over the system encoding from its parent thread.

sebreswiki2021-04-15 15:33:49

Well, TSD solution would be possible, but in simplest case it would imply indirectly that the setting of system encoding affects only current thread (and all of its interpreters), what would be backwards incompatible.

Here is a diff explaining that:

# [T0] main thread set system encoding to "cp1252"
...
# [T1] then at some point thread 1 doing:
% encoding system utf-8
utf-8
# [T2] hereafter in other thread:
% encoding system
-utf-8
+cp1252

Sure, one could additionally store an epoch in TSD and compare it with a common static epoch which gets incremented by Tcl_SetSystemEncoding, so an access by Tcl_GetEncoding(interp, NULL) could check it and invalidate cached in TSD encoding if epoch changed globally.

Just whole performance of that (TSD+epoch) will be almost comparable to a simple lock variant (in case of NULL by encoding), so similat to: get and incr ref in lock, convert, and decr ref in lock at end.

FWIW: in my fork I used similar way, without TSD and interim lock (with atomic refCount increment/decrement and an epoch used only for Tcl_ObjType "encoding" cached in tcl-object).

sebreswiki2024-04-26 14:27:32

I backported parts of my tcl-fork responsible for encoding in sebres-encoding-perf-branch, which fixes this issue (it is 8.5 based, because it was initially implemented to that branch on my side).

This also provides a performance tests showing grave improvement of that branch for all encoding operations (up to 1000x). Here is small excerpt illustrating that:

  ********************************************************************************
- Total 8 cases in 2.06 sec. (1.86 net-sec.):
+ Total 8 cases in 2.05 sec. (1.82 net-sec.):
- 1734.821698 µs/# 5967895 # 3201857.947 #/sec 1863.885 net-ms
+ 2.314888 µs/# 8030184 # 4423539.522 #/sec 1815.330 net-ms
  Average:
- 216.852712 µs/# 745986 # 3201849 #/sec 232.986 net-ms
+ 0.289361 µs/# 1003773 # 4423544 #/sec 226.916 net-ms
  Min:
- 0.124842 µs/# 1692545 # 8010151 #/sec 211.300 net-ms
+ 0.125721 µs/# 1681033 # 7954126 #/sec 211.341 net-ms
  Max:
- 478.994 µs/# 522 # 2087.7 #/sec 250.035 net-ms
+ 0.550516 µs/# 435911 # 1816477 #/sec 239.976 net-ms
  ********************************************************************************
...
  ********************************************************************************
- Total 2 cases in 0.52 sec. (0.50 net-sec.):
+ Total 2 cases in 0.52 sec. (0.47 net-sec.):
- 720.021000 µs/# 1391 # 2778.577 #/sec 500.616 net-ms
+ 0.774522 µs/# 1317842 # 2805745.869 #/sec 469.694 net-ms
  Average:
- 360.010500 µs/# 695 # 2777 #/sec 250.308 net-ms
+ 0.387261 µs/# 658921 # 2805746 #/sec 234.847 net-ms
  Min:
- 353.664 µs/# 708 # 2827.5 #/sec 250.394 net-ms
+ 0.274761 µs/# 839609 # 3639523 #/sec 230.692 net-ms
  Max:
- 366.357 µs/# 683 # 2729.6 #/sec 250.222 net-ms
+ 0.499761 µs/# 478233 # 2000958 #/sec 239.002 net-ms
  ********************************************************************************

sebreswiki2024-04-26 14:45:45

The encoding cache introduced there doesn't have a goal to improve performance only - the second major job, it does, is a protection of "vulnerable" call Tcl_GetEncodingName(NULL) (see lines 984-987 for details).
The issue is - so as it is constructed, it is impossible to repair completely, because this function doesn't imply a decrement of reference or free of returned name, so the caller may use a pointer later, which can be released by switch of system encoding (e. g. by other threads).

Now the cache avoids that situation, as long as the system encoding would not be switched too often (hardly imaginable in normal regular case, that 4 threads switch it to more than 4 different encodings at once).
The fully correct fix would only work with thread-based encoding (TSD, etc), but then not backwards compatible anymore.

serhiy.storchakawiki2026-10-17 08:10:31

Reproduced on 9.0 and main: one thread doing [file exists] on a long path while another switches [encoding system] crashes every run. Also Tcl_GetEncoding(NULL, NULL) returned systemEncoding read again after unlocking the mutex. Branch system-encoding-keep-retired is a simple alternative to sebres-encoding-perf-branch: former system encodings are kept until finalization, so a conversion can keep using the one it read, without a mutex or atomic reference counts. (The atomic reference count in sebres-encoding-perf-branch narrows the race but does not close it: the encoding can be freed between reading systemEncoding and incrementing its count.) A reference in each conversion with a mutex works too, but made [file exists] in 4 threads 1.75 times slower here. Visible change: a former system encoding is not unloaded when [encoding dirs] changes (encoding-2.2 adjusted). Test thread-9.1. We can find a better solution later, but it is better to fix the crash now.

Log in (anonymous is fine) to add a comment.