Skip to content

Commit 1bdd8f7

Browse files
committed
Merge branch 'PHP-7.4' into PHP-8.0
* PHP-7.4: Fixed bug #42560
2 parents f33105d + 5d31ee3 commit 1bdd8f7

File tree

6 files changed

+37
-10
lines changed

6 files changed

+37
-10
lines changed

ext/standard/file.c

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -828,18 +828,14 @@ PHP_FUNCTION(tempnam)
828828
Z_PARAM_PATH(prefix, prefix_len)
829829
ZEND_PARSE_PARAMETERS_END();
830830

831-
if (php_check_open_basedir(dir)) {
832-
RETURN_FALSE;
833-
}
834-
835831
p = php_basename(prefix, prefix_len, NULL, 0);
836832
if (ZSTR_LEN(p) > 64) {
837833
ZSTR_VAL(p)[63] = '\0';
838834
}
839835

840836
RETVAL_FALSE;
841837

842-
if ((fd = php_open_temporary_fd_ex(dir, ZSTR_VAL(p), &opened_path, 1)) >= 0) {
838+
if ((fd = php_open_temporary_fd_ex(dir, ZSTR_VAL(p), &opened_path, PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ALWAYS)) >= 0) {
843839
close(fd);
844840
RETVAL_STR(opened_path);
845841
}

ext/standard/tests/file/bug42560.phpt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
--TEST--
2+
Bug #42560 Empty directory argument to tempnam yields open_basedir problems
3+
--FILE--
4+
<?php
5+
$tmpdir = sys_get_temp_dir();
6+
ini_set('open_basedir', $tmpdir);
7+
$tempnam = tempnam('', 'test');
8+
var_dump($tempnam !== false);
9+
var_dump(file_exists($tempnam));
10+
11+
if (file_exists($tempnam)) {
12+
unlink($tempnam);
13+
}
14+
?>
15+
--EXPECT--
16+
bool(true)
17+
bool(true)

main/php_open_temporary_file.c

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -299,13 +299,19 @@ PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, zend_strin
299299
def_tmp:
300300
temp_dir = php_get_temporary_directory();
301301

302-
if (temp_dir && *temp_dir != '\0' && (!(flags & PHP_TMP_FILE_OPEN_BASEDIR_CHECK) || !php_check_open_basedir(temp_dir))) {
302+
if (temp_dir &&
303+
*temp_dir != '\0' &&
304+
(!(flags & PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_FALLBACK) || !php_check_open_basedir(temp_dir))) {
303305
return php_do_open_temporary_file(temp_dir, pfx, opened_path_p);
304306
} else {
305307
return -1;
306308
}
307309
}
308310

311+
if ((flags & PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_EXPLICIT_DIR) && php_check_open_basedir(dir)) {
312+
return -1;
313+
}
314+
309315
/* Try the directory given as parameter. */
310316
fd = php_do_open_temporary_file(dir, pfx, opened_path_p);
311317
if (fd == -1) {
@@ -320,7 +326,7 @@ PHPAPI int php_open_temporary_fd_ex(const char *dir, const char *pfx, zend_strin
320326

321327
PHPAPI int php_open_temporary_fd(const char *dir, const char *pfx, zend_string **opened_path_p)
322328
{
323-
return php_open_temporary_fd_ex(dir, pfx, opened_path_p, 0);
329+
return php_open_temporary_fd_ex(dir, pfx, opened_path_p, PHP_TMP_FILE_DEFAULT);
324330
}
325331

326332
PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, zend_string **opened_path_p)

main/php_open_temporary_file.h

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,16 @@
1717
#ifndef PHP_OPEN_TEMPORARY_FILE_H
1818
#define PHP_OPEN_TEMPORARY_FILE_H
1919

20-
#define PHP_TMP_FILE_OPEN_BASEDIR_CHECK (1<<0)
20+
#define PHP_TMP_FILE_DEFAULT 0
21+
#define PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_FALLBACK (1<<0)
2122
#define PHP_TMP_FILE_SILENT (1<<1)
23+
#define PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_EXPLICIT_DIR (1<<2)
24+
#define PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ALWAYS \
25+
(PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_FALLBACK | PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_EXPLICIT_DIR)
26+
27+
/* for compatibility purpose */
28+
#define PHP_TMP_FILE_OPEN_BASEDIR_CHECK PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_FALLBACK
29+
2230

2331
BEGIN_EXTERN_C()
2432
PHPAPI FILE *php_open_temporary_file(const char *dir, const char *pfx, zend_string **opened_path_p);

main/rfc1867.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -998,7 +998,7 @@ SAPI_API SAPI_POST_HANDLER_FUNC(rfc1867_post_handler) /* {{{ */
998998
/* in non-debug mode we have no problem with 0-length files */
999999
{
10001000
#endif
1001-
fd = php_open_temporary_fd_ex(PG(upload_tmp_dir), "php", &temp_filename, 1);
1001+
fd = php_open_temporary_fd_ex(PG(upload_tmp_dir), "php", &temp_filename, PHP_TMP_FILE_OPEN_BASEDIR_CHECK_ON_FALLBACK);
10021002
upload_cnt--;
10031003
if (fd == -1) {
10041004
sapi_module.sapi_error(E_WARNING, "File upload error - unable to create a temporary file");

tests/security/open_basedir_tempnam.phpt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ bool(false)
6363
Warning: tempnam(): open_basedir restriction in effect. File(./../.) is not within the allowed path(s): (.) in %s on line %d
6464
bool(false)
6565

66-
Warning: tempnam(): open_basedir restriction in effect. File() is not within the allowed path(s): (.) in %s on line %d
66+
Warning: tempnam(): open_basedir restriction in effect. File(%s) is not within the allowed path(s): (.) in %s on line %d
6767
bool(false)
6868
string(%d) "%s"
6969
bool(true)

0 commit comments

Comments
 (0)