Skip to content

Commit 658e86b

Browse files
committed
- prevent unexpectable behaviors (for the user) with invalid path
1 parent c58f63a commit 658e86b

File tree

5 files changed

+48
-23
lines changed

5 files changed

+48
-23
lines changed

ext/standard/tests/file/tempnam_variation3-win32.phpt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ for( $i=0; $i<count($names_arr); $i++ ) {
7070
if (realpath($file_dir) == $file_path || realpath($file_dir . "\\") == $file_path) {
7171
echo "OK\n";
7272
} else {
73-
echo "Failed, not created in the correct directory" . realpath($file_dir) . ' vs ' . $file_path ."\n";
73+
echo "Failed, not created in the correct directory " . realpath($file_dir) . ' vs ' . $file_path ."\n";
7474
}
7575

7676
if (!is_writable($file_name)) {
@@ -99,7 +99,8 @@ OK
9999
-- Iteration 4 --
100100
OK
101101
-- Iteration 5 --
102-
OK
102+
Failed, not created in the correct directory %s vs %s
103+
0
103104
-- Iteration 6 --
104105
OK
105106
-- Iteration 7 --

main/php_open_temporary_file.c

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,13 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, char **
113113
return -1;
114114
}
115115

116+
#ifdef PHP_WIN32
117+
if (!php_win32_check_trailing_space(pfx, (const int)strlen(pfx))) {
118+
SetLastError(ERROR_INVALID_NAME);
119+
return -1;
120+
}
121+
#endif
122+
116123
if (!VCWD_GETCWD(cwd, MAXPATHLEN)) {
117124
cwd[0] = '\0';
118125
}
@@ -138,19 +145,22 @@ static int php_do_open_temporary_file(const char *path, const char *pfx, char **
138145
}
139146

140147
#ifdef PHP_WIN32
148+
141149
if (GetTempFileName(new_state.cwd, pfx, 0, opened_path)) {
142150
/* Some versions of windows set the temp file to be read-only,
143151
* which means that opening it will fail... */
144152
VCWD_CHMOD(opened_path, 0600);
145153
fd = VCWD_OPEN_MODE(opened_path, open_flags, 0600);
146154
}
155+
147156
#elif defined(HAVE_MKSTEMP)
148157
fd = mkstemp(opened_path);
149158
#else
150159
if (mktemp(opened_path)) {
151160
fd = VCWD_OPEN(opened_path, open_flags);
152161
}
153162
#endif
163+
154164
if (fd == -1 || !opened_path_p) {
155165
efree(opened_path);
156166
} else {

main/streams/plain_wrapper.c

Lines changed: 19 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -38,12 +38,11 @@
3838
#endif
3939
#include "SAPI.h"
4040

41+
#include "php_streams_int.h"
4142
#ifdef PHP_WIN32
42-
# include "ext/standard/php_string.h"
43+
# include "win32/winutil.h"
4344
#endif
4445

45-
#include "php_streams_int.h"
46-
4746
#define php_stream_fopen_from_fd_int(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_CC TSRMLS_CC)
4847
#define php_stream_fopen_from_fd_int_rel(fd, mode, persistent_id) _php_stream_fopen_from_fd_int((fd), (mode), (persistent_id) STREAMS_REL_CC TSRMLS_CC)
4948
#define php_stream_fopen_from_file_int(file, mode) _php_stream_fopen_from_file_int((file), (mode) STREAMS_CC TSRMLS_CC)
@@ -1065,24 +1064,13 @@ static int php_plain_files_rename(php_stream_wrapper *wrapper, char *url_from, c
10651064
}
10661065

10671066
#ifdef PHP_WIN32
1068-
/* Prevent bad things to happen when invalid path are used with MoveFileEx */
1069-
{
1070-
int url_from_len = strlen(url_from);
1071-
int url_to_len = strlen(url_to);
1072-
char *trimed = php_trim(url_from, url_from_len, NULL, 0, NULL, 1 TSRMLS_CC);
1073-
int trimed_len = strlen(trimed);
1074-
1075-
if (trimed_len == 0 || trimed_len != url_from_len) {
1076-
php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to TSRMLS_CC);
1077-
return 0;
1078-
}
1079-
1080-
trimed = php_trim(url_to, url_to_len, NULL, 0, NULL, 1 TSRMLS_CC);
1081-
trimed_len = strlen(trimed);
1082-
if (trimed_len == 0 || trimed_len != url_to_len) {
1083-
php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to TSRMLS_CC);
1084-
return 0;
1085-
}
1067+
if (!php_win32_check_trailing_space(url_from, strlen(url_from))) {
1068+
php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to TSRMLS_CC);
1069+
return 0;
1070+
}
1071+
if (!php_win32_check_trailing_space(url_to, strlen(url_to))) {
1072+
php_win32_docref2_from_error(ERROR_INVALID_NAME, url_from, url_to TSRMLS_CC);
1073+
return 0;
10861074
}
10871075
#endif
10881076

@@ -1251,6 +1239,9 @@ static int php_plain_files_mkdir(php_stream_wrapper *wrapper, char *dir, int mod
12511239

12521240
static int php_plain_files_rmdir(php_stream_wrapper *wrapper, char *url, int options, php_stream_context *context TSRMLS_DC)
12531241
{
1242+
#if PHP_WIN32
1243+
int url_len = strlen(url);
1244+
#endif
12541245
if (PG(safe_mode) &&(!php_checkuid(url, NULL, CHECKUID_CHECK_FILE_AND_DIR))) {
12551246
return 0;
12561247
}
@@ -1259,6 +1250,13 @@ static int php_plain_files_rmdir(php_stream_wrapper *wrapper, char *url, int opt
12591250
return 0;
12601251
}
12611252

1253+
#if PHP_WIN32
1254+
if (!php_win32_check_trailing_space(url, url_len)) {
1255+
php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "%s", strerror(ENOENT));
1256+
return 0;
1257+
}
1258+
#endif
1259+
12621260
if (VCWD_RMDIR(url) < 0) {
12631261
php_error_docref1(NULL TSRMLS_CC, url, E_WARNING, "%s", strerror(errno));
12641262
return 0;

win32/winutil.c

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,3 +31,18 @@ PHPAPI char *php_win_err(int error)
3131

3232
return (buf ? (char *) buf : "");
3333
}
34+
35+
int php_win32_check_trailing_space(const char * path, const int path_len) {
36+
if (path_len < 1) {
37+
return 1;
38+
}
39+
if (path) {
40+
if (path[0] == ' ' || path[path_len - 1] == ' ') {
41+
return 0;
42+
} else {
43+
return 1;
44+
}
45+
} else {
46+
return 0;
47+
}
48+
}

win32/winutil.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,3 +19,4 @@
1919
PHPAPI char *php_win_err(int error);
2020

2121
#define php_win_err() php_win_err(GetLastError())
22+
int php_win32_check_trailing_space(const char * path, const int path_len);

0 commit comments

Comments
 (0)