Skip to content
Tcl Source Code History Of Ticket f2ff05fc84
Login

History Of Ticket f2ff05fc84

Artifacts Associated With Ticket f2ff05fc84

  1. Ticket change [a3963ccbe8] (rid 193205) by sebres on 2021-04-06 16:14:12:

    1. assignee initialized to: "nobody"
    2. closer initialized to: "nobody"
    3. cmimetype initialized to: "text/x-fossil-wiki"
    4. comment initialized to:
      <code>Tcl_UtfToExternal*</code>, <code>Tcl_ExternalToUtf*</code> (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 [https://github.com/sebres/tcl/blob/1e522ffa06a7aca2fd729e709ffdccb4f56b6882/generic/tclInt.h#L131|sebres/tcl/1e522ff/tclInt.h#L131].
      
      Simplest scenario to reproduce the SF (for understanding):
      <code><pre style="margin-left:15pt">
      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)...
      </pre></code>
      
    5. foundin initialized to: "any"
    6. is_private initialized to: "0"
    7. login: "sebres"
    8. priority initialized to: "5 Medium"
    9. resolution initialized to: "None"
    10. severity initialized to: "Critical"
    11. status initialized to: "Open"
    12. submitter initialized to: "sebres"
    13. subsystem initialized to: "11. Conversions from String"
    14. title initialized to:
      Usage of static systemEncoding is not thread safe (vulnerability)
      
    15. type initialized to: "Bug"
  2. Ticket change [788c4d160f] (rid 193206) by sebres on 2021-04-06 16:14:45:

    1. login: "sebres"
    2. mimetype: "text/plain"
    3. priority changed to: "7 High"
  3. Ticket change [a0b938651a] (rid 193211) by jan.nijtmans on 2021-04-07 09:05:34:

    1. assignee changed to: "sebres"
    2. icomment:
      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.
      
    3. login: "jan.nijtmans"
    4. mimetype: "text/x-fossil-wiki"
    5. severity changed to: "Severe"
  4. Ticket change [b23cf44e38] (rid 193716) by sebres on 2021-04-15 15:33:49:

    1. icomment:
      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:
      <code><pre style="padding-left:10pt">
      # [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
      <b style="color:red">-utf-8</b>
      <b style="color:green">+cp1252</b>
      </pre></code>
      
      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).
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  5. Ticket change [3dbdbaa10c] (rid 225370) by sebres on 2024-04-26 14:27:32:

    1. icomment:
      I backported parts of my tcl-fork responsible for encoding in [./timeline?r=sebres-encoding-perf-branch|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 <i></i> showing grave improvement of that branch for all encoding operations (up to 1000x). Here is small excerpt illustrating that:
      <pre style="font-family:monospace; font-size:85%">
        ********************************************************************************
      <b style="color:red">- Total 8 cases in 2.06 sec. (1.86 net-sec.):</b>
      <b style="color:green">+ Total 8 cases in 2.05 sec. (1.82 net-sec.):</b>
      <b style="color:red">- 1734.821698 µs/# 5967895 # 3201857.947 #/sec 1863.885 net-ms</b>
      <b style="color:green">+ 2.314888 µs/# 8030184 # 4423539.522 #/sec 1815.330 net-ms</b>
        Average:
      <b style="color:red">- 216.852712 µs/# 745986 # 3201849 #/sec 232.986 net-ms</b>
      <b style="color:green">+ 0.289361 µs/# 1003773 # 4423544 #/sec 226.916 net-ms</b>
        Min:
      <b style="color:red">- 0.124842 µs/# 1692545 # 8010151 #/sec 211.300 net-ms</b>
      <b style="color:green">+ 0.125721 µs/# 1681033 # 7954126 #/sec 211.341 net-ms</b>
        Max:
      <b style="color:red">- 478.994 µs/# 522 # 2087.7 #/sec 250.035 net-ms</b>
      <b style="color:green">+ 0.550516 µs/# 435911 # 1816477 #/sec 239.976 net-ms</b>
        ********************************************************************************
      ...
        ********************************************************************************
      <b style="color:red">- Total 2 cases in 0.52 sec. (0.50 net-sec.):</b>
      <b style="color:green">+ Total 2 cases in 0.52 sec. (0.47 net-sec.):</b>
      <b style="color:red">- 720.021000 µs/# 1391 # 2778.577 #/sec 500.616 net-ms</b>
      <b style="color:green">+ 0.774522 µs/# 1317842 # 2805745.869 #/sec 469.694 net-ms</b>
        Average:
      <b style="color:red">- 360.010500 µs/# 695 # 2777 #/sec 250.308 net-ms</b>
      <b style="color:green">+ 0.387261 µs/# 658921 # 2805746 #/sec 234.847 net-ms</b>
        Min:
      <b style="color:red">- 353.664 µs/# 708 # 2827.5 #/sec 250.394 net-ms</b>
      <b style="color:green">+ 0.274761 µs/# 839609 # 3639523 #/sec 230.692 net-ms</b>
        Max:
      <b style="color:red">- 366.357 µs/# 683 # 2729.6 #/sec 250.222 net-ms</b>
      <b style="color:green">+ 0.499761 µs/# 478233 # 2000958 #/sec 239.002 net-ms</b>
        ********************************************************************************
      </pre>
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  6. Ticket change [4dd9da46fd] (rid 225372) by sebres on 2024-04-26 14:45:45:

    1. icomment:
      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 <i>Tcl_GetEncodingName(NULL)</i> (see [./artifact?name=9ad1b5848330ad5f&ln=984-987|lines 984-987] for details).<br/>
      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).<br/>
      The fully correct fix would only work with thread-based encoding (TSD, etc), but then not backwards compatible anymore.
      
    2. login: "sebres"
    3. mimetype: "text/x-fossil-wiki"
  7. Ticket change [4a284f5311] (rid 258458) by serhiy.storchaka on 2026-10-17 08:10:31:

    1. foundin changed to: "9.0.5, 9.1.0"
    2. icomment:
      Reproduced on 9.0 and main: one thread doing <nowiki>[file exists]</nowiki> on a long path while another switches <nowiki>[encoding system]</nowiki> crashes every run. Also Tcl_GetEncoding(NULL, NULL) returned systemEncoding read again after unlocking the mutex.
      Branch [/timeline?r=system-encoding-keep-retired|system-encoding-keep-retired] is a simple alternative to [/timeline?r=sebres-encoding-perf-branch|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 <nowiki>[file exists]</nowiki> in 4 threads 1.75 times slower here. Visible change: a former system encoding is not unloaded when <nowiki>[encoding dirs]</nowiki> 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.
      
    3. login: "serhiy.storchaka"
    4. mimetype: "text/x-fossil-wiki"