| Ticket UUID: | e2698b94b151aecf275b3dd2c5d605b89022684e | ||
| Title: | Tcl_FSChdir crashes after seteuid | ||
| Type: | Bug | Created on: | 2026-04-14 23:48:01 |
| Submitter: | jmroot | Assigned to: | nobody |
| Subsystem: | 36. Pathname Management | Severity: | Severe |
| Priority: | 5 Medium | Last modified: | 2026-04-18 02:22:40 |
| Status: | Closed | Closed by: | apnadkarni |
| Resolution: | Fixed | Closed on: | 2026-04-18 02:22:40 |
| Version: | 9.0.3 | ||
| Description: | ||||
Tcl_FSChdir can trigger thousands of levels of recursion and finally crash in some circumstances. The attached reproducer will crash when run as root from a directory that is inaccessible to 'nobody'. It's as minimal as I could get it; removing either of the Tcl_FSGetCwd calls seems to avoid the crash. This was discovered in MacPorts, where the issue can easily be triggered by the user running 'sudo port ...' from their home directory (since it is typically not accessible to the account used for dropping privileges). The crash happens when it calls pwd and cd at the script level: [https://github.com/macports/macports-base/blob/master/src/port1.0/portutil.tcl#L1419] I'm also attaching the crash log generated by running the reproduction program when linked with Tcl built with debug information and clang sanitizers (hence the weird stuff at the top of the stack trace). | ||||
| User Comments: | ||||
apnadkarni added on 2026-04-16 05:47:20:
I could not reproduce this on Ubuntu 22 but that could very well be because I'm running under WSL and behavior of getcwd() etc. on NTFS could very well be different as far as access permissions go. In fact, the test suite already disables chmod related tests for WSL. Looking at the source code, it appears the recursion case happens here where the path value is "". How it gets to be the empty string is a question but I suspect somewhere getcwd returns NULL on error and that gets translated to the empty string. That's just conjecture. The comment there
is rather telling but I'm in the same boat as DGP in that regard. If @jmr or someone running macos can trace and check if the path is in fact "" at that point, it would help track this down. apnadkarni added on 2026-04-16 06:30:55:
Ok, I was able to reproduce this behavior by hacking getcwd to return NULL after the seteuid() call and can confirm it results in the current directory being set to the empty string which then results in the recursive crash you saw. Examining for potential fixes... Thanks much for the detailed crash and short test case. Would not have been possible for me to tackle this otherwise. jmroot added on 2026-04-16 08:02:43:
Thanks. I modified the test program to call getcwd directly and print the result, and can confirm that it returns a NULL pointer after the seteuid on macOS. apnadkarni added on 2026-04-17 05:40:17:
@jmroot, could you try the bug-e2698b94b1 branch and see if that works better for you? The sample test case no longer crashes but I can't test your real scenario. jmroot added on 2026-04-17 07:12:12:
Applying that change prevents the crash in MacPorts. Thank you! apnadkarni added on 2026-04-17 11:10:30:
Great. Committed to core-9-0-branch and main. Did this come up because of some change in macports or something new with Tcl 9? Closing. jmroot added on 2026-04-17 23:38:11:
It seems to have been new with Tcl 9 since MacPorts did not see the issue when using 8.6.17. apnadkarni added on 2026-04-18 02:22:40:
Right, should have realized that without asking. The recursion is through the zipfs file system as your trace showed, which of course is not present in 8.6 by default. I suspect it may have shown up in 8.6 as well if a VFS was mounted (depending on the VFS). | ||||
