Repository navigation
Conversation
…ayfs open_container_file() refuses to open a file for writing when it has other hard links, by checking st_nlink on the opened handle and the path name afterwards. While a rename over the file is still running, the old name can still be looked up even though the kernel has already dropped that link from the inode's count. A hard link that is being replaced then reports nlink 1, the path still names it, and the write goes into the other file. On overlayfs the window is wide enough for a racing container to hit it a few times per 100,000 attempts; on ext4 and tmpfs it was never hit. Before the checks that follow fstat(), look up a name that doesn't exist in the directory. A lookup that misses the dentry cache takes the directory lock that a running rename or unlink holds, so it waits for that operation to finish, and the checks after it see the old name gone. Read-only opens are unchanged. Also refuse the write when the lstat() of the path after that lookup reports other links. On hosts without /proc (macOS) only the path's inode was checked after opening, so a hard link made to the file after the lookup was not seen there.
|
No actionable comments were generated in the recent review. Walkthrough
Change: Bug fix Merge Risk:
|
open_container_file() refuses to write a file in a site's config volume when it has other hard links, by checking st_nlink. While a rename over the file is still running, its old name can still be looked up even though the link is already gone from the inode's count. So a file that is hard-linked elsewhere reports nlink 1 for a moment, and a racing container could get the write into the other file. This shows on overlayfs, for example when /opt/easyengine runs inside a container: 8 (PHP 7.4) and 15 (PHP 8.5) writes landed in the linked file per 100,000 attempts. It was never seen on ext4 or tmpfs.
This change looks up a name that doesn't exist in the directory before the checks that follow fstat(), but only for writes. That lookup takes the directory lock a running rename holds, so it waits for the rename to finish, and the checks after it see the old name gone. Nothing is created in the directory, and reads are unchanged.
The write is now also refused when the lstat() of the path after that lookup reports other links. Without /proc (macOS) only the path's inode was checked after opening, so a hard link made to the file at that point (for example the opened file renamed to another name and linked back as custom.ini) was not caught. On Linux the /proc check already catches that case; there the extra check only refuses a file that gets a second name while it is being opened.
Every write through the helper gets the extra lookup:
ee site restoreand the site-command and site-type container migrations that edit custom.ini. There is no visible change in their output.Tested with a hard-link racer on overlayfs, ext4 and tmpfs on PHP 7.4 and 8.5: no writes landed in the linked file over at least 300,000 attempts per PHP version on overlayfs, against 8 and 15 per 100,000 before the change. A lookup of an existing path at the same point, which takes no lock, still let writes through, so the lock is what closes the race. The symlink, FIFO, device and directory-swap racers and the 33-case static check of the helper behave as before.
For the link-count check, new unit tests on PHP 7.4 and 8.5 simulate a host without /proc and add a hard link to the file between opening and checking it, either a second name for it or the file renamed away and linked back; both are refused now and were written before. With /proc, the racer above never reached that check. With /proc hidden, the racer on overlayfs got 24 writes through in 200,000 without the check and 1 in 400,000 with it; only /proc can name the opened file, so that last case is left for hosts with no /proc whose sites live on overlayfs, which EasyEngine doesn't run on.
Summary by CodeRabbit