Skip to content

Commit 14b52fa

Browse files
committed
Fix GH-21992: symlink() creates link at dangling symlink target
expand_filepath() resolves the last component of the link path, so when it is an existing dangling symlink, the new link is created at its target instead of failing. The same applies to link(). Resolve only the directory part and keep the last component as given so the kernel reports EEXIST.
1 parent bea8d20 commit 14b52fa

3 files changed

Lines changed: 110 additions & 5 deletions

File tree

‎NEWS‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,8 @@ PHP NEWS
4444
- Standard:
4545
. Fixed chown() and lchown() failing to resolve user names in ZTS builds
4646
when getpwnam_r() needs a larger buffer. (Ilia Alshanetsky)
47+
. Fixed bug GH-21992 (symlink() and link() on a dangling symlink create the
48+
link at the symlink target instead of failing). (Jakub Zelenka)
4749
. Fixed password_get_info() and password_needs_rehash() accepting malformed
4850
bcrypt costs. (Ilia Alshanetsky)
4951

‎ext/standard/link.c‎

Lines changed: 40 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,42 @@ PHP_FUNCTION(linkinfo)
120120
}
121121
/* }}} */
122122

123+
/* Expand the link path but do not resolve its last component (it is the link itself) */
124+
static char *php_expand_link_path(const char *path, size_t path_len, char *expanded, char *dir_p, size_t *dir_len)
125+
{
126+
char dir[MAXPATHLEN];
127+
zend_string *base;
128+
size_t len, base_len;
129+
130+
if (path_len == 0 || path_len >= MAXPATHLEN) {
131+
return NULL;
132+
}
133+
134+
memcpy(dir, path, path_len + 1);
135+
zend_dirname(dir, path_len);
136+
if (!expand_filepath(dir, dir_p)) {
137+
return NULL;
138+
}
139+
len = strlen(dir_p);
140+
141+
base = php_basename(path, path_len, NULL, 0);
142+
base_len = ZSTR_LEN(base);
143+
if (len + 1 + base_len >= MAXPATHLEN) {
144+
zend_string_release_ex(base, 0);
145+
return NULL;
146+
}
147+
148+
memcpy(expanded, dir_p, len);
149+
*dir_len = len;
150+
if (!IS_SLASH(dir_p[len - 1])) {
151+
expanded[len++] = DEFAULT_SLASH;
152+
}
153+
memcpy(expanded + len, ZSTR_VAL(base), base_len + 1);
154+
zend_string_release_ex(base, 0);
155+
156+
return expanded;
157+
}
158+
123159
/* {{{ Create a symbolic link */
124160
PHP_FUNCTION(symlink)
125161
{
@@ -136,14 +172,11 @@ PHP_FUNCTION(symlink)
136172
Z_PARAM_PATH(frompath, frompath_len)
137173
ZEND_PARSE_PARAMETERS_END();
138174

139-
if (!expand_filepath(frompath, source_p)) {
175+
if (!php_expand_link_path(frompath, frompath_len, source_p, dirname, &len)) {
140176
php_error_docref(NULL, E_WARNING, "No such file or directory");
141177
RETURN_FALSE;
142178
}
143179

144-
memcpy(dirname, source_p, sizeof(source_p));
145-
len = zend_dirname(dirname, strlen(dirname));
146-
147180
if (!expand_filepath_ex(topath, dest_p, dirname, len)) {
148181
php_error_docref(NULL, E_WARNING, "No such file or directory");
149182
RETURN_FALSE;
@@ -186,13 +219,15 @@ PHP_FUNCTION(link)
186219
int ret;
187220
char source_p[MAXPATHLEN];
188221
char dest_p[MAXPATHLEN];
222+
char dirname[MAXPATHLEN];
223+
size_t len;
189224

190225
ZEND_PARSE_PARAMETERS_START(2, 2)
191226
Z_PARAM_PATH(topath, topath_len)
192227
Z_PARAM_PATH(frompath, frompath_len)
193228
ZEND_PARSE_PARAMETERS_END();
194229

195-
if (!expand_filepath(frompath, source_p) || !expand_filepath(topath, dest_p)) {
230+
if (!php_expand_link_path(frompath, frompath_len, source_p, dirname, &len) || !expand_filepath(topath, dest_p)) {
196231
php_error_docref(NULL, E_WARNING, "No such file or directory");
197232
RETURN_FALSE;
198233
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
--TEST--
2+
GH-21992 (symlink() on dangling symlink returns true and creates link at wrong location)
3+
--SKIPIF--
4+
<?php
5+
if (PHP_OS_FAMILY === 'Windows') {
6+
die('skip not for Windows');
7+
}
8+
?>
9+
--FILE--
10+
<?php
11+
$dir = __DIR__ . '/gh21992';
12+
$targetDir = $dir . '/target_dir';
13+
$ghost = $targetDir . '/ghost.txt';
14+
$deadLink = $dir . '/dead_link.txt';
15+
$realFile = $dir . '/real.txt';
16+
17+
mkdir($dir);
18+
mkdir($targetDir);
19+
touch($realFile);
20+
21+
// Create a dangling link
22+
var_dump(symlink($ghost, $deadLink));
23+
var_dump(is_link($deadLink));
24+
25+
echo "--- symlink() over dangling link\n";
26+
var_dump(symlink($realFile, $deadLink));
27+
var_dump(readlink($deadLink) === $ghost);
28+
var_dump(file_exists($ghost) || is_link($ghost));
29+
30+
echo "--- link() over dangling link\n";
31+
var_dump(link($realFile, $deadLink));
32+
var_dump(readlink($deadLink) === $ghost);
33+
var_dump(file_exists($ghost) || is_link($ghost));
34+
35+
echo "--- relative link path\n";
36+
chdir($dir);
37+
var_dump(symlink('real.txt', 'dead_link.txt'));
38+
var_dump(file_exists($ghost) || is_link($ghost));
39+
?>
40+
--CLEAN--
41+
<?php
42+
$dir = __DIR__ . '/gh21992';
43+
@unlink($dir . '/target_dir/ghost.txt');
44+
@unlink($dir . '/dead_link.txt');
45+
@unlink($dir . '/real.txt');
46+
@rmdir($dir . '/target_dir');
47+
@rmdir($dir);
48+
?>
49+
--EXPECTF--
50+
bool(true)
51+
bool(true)
52+
--- symlink() over dangling link
53+
54+
Warning: symlink(): File exists in %s on line %d
55+
bool(false)
56+
bool(true)
57+
bool(false)
58+
--- link() over dangling link
59+
60+
Warning: link(): File exists in %s on line %d
61+
bool(false)
62+
bool(true)
63+
bool(false)
64+
--- relative link path
65+
66+
Warning: symlink(): File exists in %s on line %d
67+
bool(false)
68+
bool(false)

0 commit comments

Comments
 (0)