-
Notifications
You must be signed in to change notification settings - Fork 8k
Fix GH-21992: ext/standard: Fix symlink trying to resolve dangling links #22024
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zaaarf
wants to merge
5
commits into
php:PHP-8.4
Choose a base branch
from
zaaarf:gh-21992
base: PHP-8.4
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+86
−0
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,76 @@ | ||
| --TEST-- | ||
| GH-21992: symlink() must not resolve the final dangling link component | ||
| --SKIPIF-- | ||
| <?php | ||
| if (PHP_OS_FAMILY === 'Windows') { | ||
| die('skip (not for Windows)'); | ||
| } | ||
| ?> | ||
|
zaaarf marked this conversation as resolved.
|
||
| --FILE-- | ||
| <?php | ||
| $dir = __DIR__ . '/gh21992'; | ||
| $targetDir = $dir . '/target_dir'; | ||
|
|
||
| // dangling link to nonexisting $ghost | ||
| $deadLink = $dir . '/dead_link.txt'; | ||
| $ghost = $targetDir . '/ghost.txt'; | ||
| $newTarget = $dir . '/nothing'; | ||
|
|
||
| // real link to $realFile | ||
| $realLink = $dir . '/real_link.txt'; | ||
| $realFile = $targetDir . '/real.txt'; | ||
| $realTarget = $dir . '/something'; | ||
|
|
||
| mkdir($dir); | ||
| mkdir($targetDir); | ||
| touch($realFile); // ensure $realFile exists | ||
|
|
||
| // verify the real file works fine | ||
| var_dump(symlink($realFile, $realLink)); | ||
| var_dump(readlink($realLink)); | ||
| var_dump(symlink($realTarget, $realLink)); | ||
| var_dump(is_link($realLink)); | ||
| var_dump(readlink($realLink)); | ||
| var_dump(is_link($realFile)); | ||
|
|
||
| // verify the ghost file works as intended | ||
| var_dump(symlink($ghost, $deadLink)); | ||
| var_dump(readlink($deadLink)); | ||
| var_dump(symlink($newTarget, $deadLink)); | ||
| var_dump(is_link($deadLink)); | ||
| var_dump(readlink($deadLink)); | ||
| var_dump(is_link($ghost)); | ||
| ?> | ||
| --CLEAN-- | ||
| <?php | ||
| $dir = __DIR__ . '/gh21992'; | ||
| $targetDir = $dir . '/target_dir'; | ||
| $deadLink = $dir . '/dead_link.txt'; | ||
| $ghost = $targetDir . '/ghost.txt'; | ||
| $realLink = $dir . '/real_link.txt'; | ||
| $realFile = $targetDir . '/real.txt'; | ||
|
|
||
| @unlink($ghost); | ||
| @unlink($deadLink); | ||
| @unlink($realLink); | ||
| @unlink($realFile); | ||
| @rmdir($targetDir); | ||
| @rmdir($dir); | ||
| ?> | ||
| --EXPECTF-- | ||
| bool(true) | ||
| string(%d) "%s%egh21992%etarget_dir%ereal.txt" | ||
|
|
||
| Warning: symlink(): File exists in %s on line %d | ||
| bool(false) | ||
| bool(true) | ||
| string(%d) "%s%egh21992%etarget_dir%ereal.txt" | ||
| bool(false) | ||
| bool(true) | ||
| string(%d) "%s%egh21992%etarget_dir%eghost.txt" | ||
|
|
||
| Warning: symlink(): Dangling symlink at %s on line %d | ||
| bool(false) | ||
| bool(true) | ||
| string(%d) "%s%egh21992%etarget_dir%eghost.txt" | ||
| bool(false) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Now, I think there is a edge case,
You see, your check happens before we resolve the path from
expand_filepathand since this is a file operation, we must consider race condition. A file can pass your check and suddenly be changed into dangling links to cause a bug.I hope this make sense to you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suppose, but I don't think there's a way to make the check + symlink operation atomic, is there?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
AFAIK there may be, but only by letting the kernel do it, ideally via symlink() on the unmodified final path or symlinkat() on an opened parent directory.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Would that check for the symlink being dangling though? If so, that's pretty convenient. We have
zend_dirnameto gettofdand its length, at least.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Okay well, are you sure? I tried with
symlinkatjust now and I'm getting the same buggy behaviour described by OP (at least, I think so? The test is failing because symlink now returns true on PHP). I removed my dangling check, and I'm replacing thephp_sys_symlinkcall with this:I'm going to sleep for tonight now, so don't worry if I don't reply for a while.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems fine. Let's wait for David's opinion :)