Skip to content

Commit 609195e

Browse files
committed
Merge branch 'PHP-7.2' into PHP-7.3
* PHP-7.2: Fix bug #77630 - safer rename() procedure
2 parents a976283 + 2cc1ab8 commit 609195e

File tree

1 file changed

+35
-18
lines changed

1 file changed

+35
-18
lines changed

main/streams/plain_wrapper.c

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1198,34 +1198,51 @@ static int php_plain_files_rename(php_stream_wrapper *wrapper, const char *url_f
11981198
# ifdef EXDEV
11991199
if (errno == EXDEV) {
12001200
zend_stat_t sb;
1201+
# if !defined(ZTS) && !defined(TSRM_WIN32)
1202+
/* not sure what to do in ZTS case, umask is not thread-safe */
1203+
int oldmask = umask(077);
1204+
# endif
1205+
int success = 0;
12011206
if (php_copy_file(url_from, url_to) == SUCCESS) {
12021207
if (VCWD_STAT(url_from, &sb) == 0) {
1203-
# ifndef TSRM_WIN32
1204-
if (VCWD_CHMOD(url_to, sb.st_mode)) {
1205-
if (errno == EPERM) {
1206-
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
1207-
VCWD_UNLINK(url_from);
1208-
return 1;
1209-
}
1208+
success = 1;
1209+
# if !defined(TSRM_WIN32)
1210+
/*
1211+
* Try to set user and permission info on the target.
1212+
* If we're not root, then some of these may fail.
1213+
* We try chown first, to set proper group info, relying
1214+
* on the system environment to have proper umask to not allow
1215+
* access to the file in the meantime.
1216+
*/
1217+
if (VCWD_CHOWN(url_to, sb.st_uid, sb.st_gid)) {
12101218
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
1211-
return 0;
1219+
if (errno != EPERM) {
1220+
success = 0;
1221+
}
12121222
}
1213-
if (VCWD_CHOWN(url_to, sb.st_uid, sb.st_gid)) {
1214-
if (errno == EPERM) {
1223+
1224+
if (success) {
1225+
if (VCWD_CHMOD(url_to, sb.st_mode)) {
12151226
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
1216-
VCWD_UNLINK(url_from);
1217-
return 1;
1227+
if (errno != EPERM) {
1228+
success = 0;
1229+
}
12181230
}
1219-
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
1220-
return 0;
12211231
}
12221232
# endif
1223-
VCWD_UNLINK(url_from);
1224-
return 1;
1233+
if (success) {
1234+
VCWD_UNLINK(url_from);
1235+
}
1236+
} else {
1237+
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
12251238
}
1239+
} else {
1240+
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
12261241
}
1227-
php_error_docref2(NULL, url_from, url_to, E_WARNING, "%s", strerror(errno));
1228-
return 0;
1242+
# if !defined(ZTS) && !defined(TSRM_WIN32)
1243+
umask(oldmask);
1244+
# endif
1245+
return success;
12291246
}
12301247
# endif
12311248
#endif

0 commit comments

Comments
 (0)