diff --git a/NEWS b/NEWS index a1b658ea9d27..2e0ace1efb82 100644 --- a/NEWS +++ b/NEWS @@ -3,6 +3,9 @@ PHP NEWS ?? ??? ????, PHP 8.7.0alpha1 - Core: + . Linux ZTS builds use a per-thread working directory when unshare(CLONE_FS) + succeeds, so chdir() also affects native code. These builds can use chroot() + in the same SAPIs as NTS builds. (henderkes) . A flat unkeyed list assignment from an array literal with one value per target, such as [$a, $b] = [$b, $a], no longer builds the array when the result is unused (GH-23048). (Artem Ukrainskiy) diff --git a/Zend/zend.c b/Zend/zend.c index 3b68f63674bb..39aa1632bea6 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -1336,6 +1336,7 @@ ZEND_API void zend_activate(void) /* {{{ */ { #ifdef ZTS virtual_cwd_activate(); + virtual_cwd_unshare(); #endif gc_reset(); init_compiler(); diff --git a/Zend/zend_virtual_cwd.c b/Zend/zend_virtual_cwd.c index 5c1253217ee4..e1e04a9d81cb 100644 --- a/Zend/zend_virtual_cwd.c +++ b/Zend/zend_virtual_cwd.c @@ -27,6 +27,17 @@ #include "zend.h" #include "zend_virtual_cwd.h" +#ifdef ZEND_HAVE_UNSHARE_FS +#include +static TSRM_TLS TSRM_TLS_MODEL_ATTR bool native_cwd; +static TSRM_TLS TSRM_TLS_MODEL_ATTR int saved_cwd_fd = -1; +#else +static const bool native_cwd = false; +#endif +#if defined(__linux__) && defined(ZTS) +static TSRM_TLS TSRM_TLS_MODEL_ATTR bool virtual_cwd_changed; +#endif + #ifdef ZEND_WIN32 #include #include "tsrm_win32.h" @@ -139,9 +150,11 @@ static int php_is_file_ok(const cwd_state *state) /* {{{ */ } /* }}} */ +static void cwd_state_init(cwd_state *state); + static void cwd_globals_ctor(virtual_cwd_globals *cwd_g) /* {{{ */ { - CWD_STATE_COPY(&cwd_g->cwd, &main_cwd_state); + cwd_state_init(&cwd_g->cwd); cwd_g->realpath_cache_size = 0; cwd_g->realpath_cache_size_limit = REALPATH_CACHE_SIZE; cwd_g->realpath_cache_ttl = REALPATH_CACHE_TTL; @@ -229,16 +242,113 @@ CWD_API void virtual_cwd_shutdown(void) /* {{{ */ } /* }}} */ +#if defined(__linux__) && defined(ZTS) +static char *native_getcwd(size_t *length) +{ + char *cwd = getcwd(NULL, 0); + *length = cwd ? strlen(cwd) : 0; + char *result = cwd ? estrndup(cwd, *length) : NULL; + free(cwd); + return result; +} +#endif + +static void cwd_state_init(cwd_state *state) +{ +#if defined(__linux__) && defined(ZTS) + state->cwd = native_getcwd(&state->cwd_length); + if (!state->cwd) { + state->cwd = estrdup(""); + } +#else + CWD_STATE_COPY(state, &main_cwd_state); +#endif +} + CWD_API void virtual_cwd_activate(void) /* {{{ */ { +#if defined(__linux__) && defined(ZTS) + if (!native_cwd && !virtual_cwd_changed) { + /* The host may have changed directory since module startup. */ + cwd_state state; + cwd_state_init(&state); + efree(CWDG(cwd).cwd); + CWDG(cwd) = state; + return; + } +#endif if (CWDG(cwd).cwd == NULL) { - CWD_STATE_COPY(&CWDG(cwd), &main_cwd_state); + cwd_state_init(&CWDG(cwd)); } } /* }}} */ +CWD_API bool virtual_cwd_is_emulated(void) +{ +#ifdef VIRTUAL_DIR + return !native_cwd; +#else + return false; +#endif +} + +/* Enter native mode only for this request. A reused worker (or a thread + * created by that worker) may share filesystem state again on entry. */ +CWD_API void virtual_cwd_unshare(void) +{ +#ifdef ZEND_HAVE_UNSHARE_FS + if (native_cwd || unshare(CLONE_FS) != 0) { + return; + } + + int fd = open(".", O_PATH | O_DIRECTORY | O_CLOEXEC); + if (fd < 0) { + return; + } + if (virtual_cwd_changed && chdir(CWDG(cwd).cwd) != 0) { + close(fd); + return; + } + saved_cwd_fd = fd; + native_cwd = true; +#endif +} + +#if defined(ZEND_HAVE_UNSHARE_FS) && defined(HAVE_CHROOT) +CWD_API int virtual_cwd_chroot(const char *path) +{ + if (!native_cwd) { + errno = EPERM; + return -1; + } + /* Native libraries may have started threads since request startup. */ + if (unshare(CLONE_FS) != 0 || chroot(path) != 0) { + return -1; + } + /* Do not restore a working directory outside the new root. */ + if (saved_cwd_fd >= 0) { + close(saved_cwd_fd); + saved_cwd_fd = -1; + } + return 0; +} +#endif + CWD_API void virtual_cwd_deactivate(void) /* {{{ */ { +#ifdef ZEND_HAVE_UNSHARE_FS + if (saved_cwd_fd >= 0) { + if (fchdir(saved_cwd_fd) != 0) { + zend_error(E_CORE_WARNING, "Could not restore working directory: %s", strerror(errno)); + } + close(saved_cwd_fd); + saved_cwd_fd = -1; + } + native_cwd = false; +#endif +#if defined(__linux__) && defined(ZTS) + virtual_cwd_changed = false; +#endif if (CWDG(cwd).cwd != NULL) { CWD_STATE_FREE(&CWDG(cwd)); CWDG(cwd).cwd = NULL; @@ -250,6 +360,11 @@ CWD_API char *virtual_getcwd_ex(size_t *length) /* {{{ */ { cwd_state *state; +#ifdef ZEND_HAVE_UNSHARE_FS + if (native_cwd) { + return native_getcwd(length); + } +#endif state = &CWDG(cwd); if (state->cwd_length == 0) { @@ -292,6 +407,11 @@ CWD_API char *virtual_getcwd(char *buf, size_t size) /* {{{ */ size_t length; char *cwd; +#ifdef ZEND_HAVE_UNSHARE_FS + if (native_cwd && buf) { + return getcwd(buf, size); + } +#endif cwd = virtual_getcwd_ex(&length); if (buf == NULL) { @@ -1215,7 +1335,16 @@ CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func CWD_API zend_result virtual_chdir(const char *path) /* {{{ */ { - return virtual_file_ex(&CWDG(cwd), path, php_is_dir_ok, CWD_REALPATH) ? FAILURE : SUCCESS; + if (native_cwd) { + return chdir(path) == 0 ? SUCCESS : FAILURE; + } + if (virtual_file_ex(&CWDG(cwd), path, php_is_dir_ok, CWD_REALPATH)) { + return FAILURE; + } +#if defined(__linux__) && defined(ZTS) + virtual_cwd_changed = true; +#endif + return SUCCESS; } /* }}} */ @@ -1261,6 +1390,10 @@ CWD_API char *virtual_realpath(const char *path, char *real_path) /* {{{ */ char *retval; char cwd[MAXPATHLEN]; + if (native_cwd) { + return tsrm_realpath(path, real_path); + } + /* realpath("") returns CWD */ if (!*path) { new_state.cwd = (char*)emalloc(1); @@ -1298,7 +1431,15 @@ CWD_API int virtual_filepath_ex(const char *path, char **filepath, verify_path_f cwd_state new_state; int retval; - CWD_STATE_COPY(&new_state, &CWDG(cwd)); + if (native_cwd && !IS_ABSOLUTE_PATH(path, strlen(path))) { + new_state.cwd = virtual_getcwd_ex(&new_state.cwd_length); + if (!new_state.cwd) { + *filepath = estrdup(""); + return 1; + } + } else { + CWD_STATE_COPY(&new_state, &CWDG(cwd)); + } retval = virtual_file_ex(&new_state, path, verify_path, CWD_FILEPATH); *filepath = new_state.cwd; @@ -1316,11 +1457,14 @@ CWD_API int virtual_filepath(const char *path, char **filepath) /* {{{ */ /* }}} */ #ifndef ZEND_WIN32 -/* An absolute path without dot segments doesn't need a per-thread CWD emulation */ +/* In emulated mode, only absolute paths without dot segments can go directly to libc. */ static zend_always_inline bool virtual_path_is_direct(const char *path) { const char *s = path; + if (native_cwd) { + return true; + } if (*s != '/') { return false; } @@ -1341,15 +1485,14 @@ CWD_API FILE *virtual_fopen(const char *path, const char *mode) /* {{{ */ cwd_state new_state; FILE *f; - if (path[0] == '\0') { /* Fail to open empty path */ - return NULL; - } - #ifndef ZEND_WIN32 if (virtual_path_is_direct(path)) { return fopen(path, mode); } #endif + if (path[0] == '\0') { /* Fail to open empty path */ + return NULL; + } CWD_STATE_COPY(&new_state, &CWDG(cwd)); if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) { @@ -1375,7 +1518,7 @@ CWD_API int virtual_access(const char *pathname, int mode) /* {{{ */ int ret; #ifndef ZEND_WIN32 - if (pathname[0] == '/') { + if (native_cwd || pathname[0] == '/') { return access(pathname, mode); } #endif @@ -1404,6 +1547,12 @@ CWD_API int virtual_utime(const char *filename, struct utimbuf *buf) /* {{{ */ cwd_state new_state; int ret; +#ifndef ZEND_WIN32 + if (native_cwd) { + return utime(filename, buf); + } +#endif + CWD_STATE_COPY(&new_state, &CWDG(cwd)); if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) { CWD_STATE_FREE_ERR(&new_state); @@ -1427,6 +1576,10 @@ CWD_API int virtual_chmod(const char *filename, mode_t mode) /* {{{ */ cwd_state new_state; int ret; + if (native_cwd) { + return chmod(filename, mode); + } + CWD_STATE_COPY(&new_state, &CWDG(cwd)); if (virtual_file_ex(&new_state, filename, NULL, CWD_REALPATH)) { CWD_STATE_FREE_ERR(&new_state); @@ -1462,6 +1615,17 @@ CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group, int li cwd_state new_state; int ret; + if (native_cwd) { + if (link) { +#ifdef HAVE_LCHOWN + return lchown(filename, owner, group); +#else + return -1; +#endif + } + return chown(filename, owner, group); + } + CWD_STATE_COPY(&new_state, &CWDG(cwd)); if (virtual_file_ex(&new_state, filename, NULL, link ? CWD_EXPAND : CWD_REALPATH)) { CWD_STATE_FREE_ERR(&new_state); @@ -1488,19 +1652,21 @@ CWD_API int virtual_open(const char *path, int flags, ...) /* {{{ */ { cwd_state new_state; int f; + mode_t mode = 0; + bool needs_mode = (flags & O_CREAT) != 0; +#ifdef O_TMPFILE + needs_mode |= (flags & O_TMPFILE) == O_TMPFILE; +#endif + if (needs_mode) { + va_list arg; + va_start(arg, flags); + mode = (mode_t) va_arg(arg, int); + va_end(arg); + } #ifndef ZEND_WIN32 if (virtual_path_is_direct(path)) { - if (flags & O_CREAT) { - mode_t mode; - va_list arg; - - va_start(arg, flags); - mode = (mode_t) va_arg(arg, int); - va_end(arg); - return open(path, flags, mode); - } - return open(path, flags); + return open(path, flags, mode); } #endif @@ -1510,26 +1676,11 @@ CWD_API int virtual_open(const char *path, int flags, ...) /* {{{ */ return -1; } - if (flags & O_CREAT) { - mode_t mode; - va_list arg; - - va_start(arg, flags); - mode = (mode_t) va_arg(arg, int); - va_end(arg); - #ifdef ZEND_WIN32 - f = php_win32_ioutil_open(new_state.cwd, flags, mode); + f = php_win32_ioutil_open(new_state.cwd, flags, mode); #else - f = open(new_state.cwd, flags, mode); + f = open(new_state.cwd, flags, mode); #endif - } else { -#ifdef ZEND_WIN32 - f = php_win32_ioutil_open(new_state.cwd, flags); -#else - f = open(new_state.cwd, flags); -#endif - } CWD_STATE_FREE_ERR(&new_state); return f; } @@ -1540,6 +1691,10 @@ CWD_API int virtual_creat(const char *path, mode_t mode) /* {{{ */ cwd_state new_state; int f; + if (native_cwd) { + return creat(path, mode); + } + CWD_STATE_COPY(&new_state, &CWDG(cwd)); if (virtual_file_ex(&new_state, path, NULL, CWD_FILEPATH)) { CWD_STATE_FREE_ERR(&new_state); @@ -1559,6 +1714,10 @@ CWD_API int virtual_rename(const char *oldname, const char *newname) /* {{{ */ cwd_state new_state; int retval; + if (native_cwd) { + return rename(oldname, newname); + } + CWD_STATE_COPY(&old_state, &CWDG(cwd)); if (virtual_file_ex(&old_state, oldname, NULL, CWD_EXPAND)) { CWD_STATE_FREE_ERR(&old_state); @@ -1596,7 +1755,7 @@ CWD_API int virtual_stat(const char *path, zend_stat_t *buf) /* {{{ */ int retval; #ifndef ZEND_WIN32 - if (path[0] == '/') { + if (native_cwd || path[0] == '/') { return php_sys_stat(path, buf); } #endif @@ -1646,6 +1805,10 @@ CWD_API int virtual_unlink(const char *path) /* {{{ */ cwd_state new_state; int retval; + if (native_cwd) { + return unlink(path); + } + CWD_STATE_COPY(&new_state, &CWDG(cwd)); if (virtual_file_ex(&new_state, path, NULL, CWD_EXPAND)) { CWD_STATE_FREE_ERR(&new_state); @@ -1668,6 +1831,12 @@ CWD_API int virtual_mkdir(const char *pathname, mode_t mode) /* {{{ */ cwd_state new_state; int retval; +#ifndef ZEND_WIN32 + if (native_cwd) { + return mkdir(pathname, mode); + } +#endif + CWD_STATE_COPY(&new_state, &CWDG(cwd)); if (virtual_file_ex(&new_state, pathname, NULL, CWD_FILEPATH)) { CWD_STATE_FREE_ERR(&new_state); @@ -1689,6 +1858,10 @@ CWD_API int virtual_rmdir(const char *pathname) /* {{{ */ cwd_state new_state; int retval; + if (native_cwd) { + return rmdir(pathname); + } + CWD_STATE_COPY(&new_state, &CWDG(cwd)); if (virtual_file_ex(&new_state, pathname, NULL, CWD_EXPAND)) { CWD_STATE_FREE_ERR(&new_state); @@ -1748,6 +1921,10 @@ CWD_API FILE *virtual_popen(const char *command, const char *type) /* {{{ */ char *ptr, *dir; FILE *retval; + if (native_cwd) { + return popen(command, type); + } + command_length = strlen(command); dir_length = CWDG(cwd).cwd_length; diff --git a/Zend/zend_virtual_cwd.h b/Zend/zend_virtual_cwd.h index 8e5a98c38b46..dc161c9a3027 100644 --- a/Zend/zend_virtual_cwd.h +++ b/Zend/zend_virtual_cwd.h @@ -173,6 +173,11 @@ CWD_API void virtual_cwd_startup(void); CWD_API void virtual_cwd_shutdown(void); CWD_API void virtual_cwd_activate(void); CWD_API void virtual_cwd_deactivate(void); +CWD_API bool virtual_cwd_is_emulated(void); +CWD_API void virtual_cwd_unshare(void); +#if defined(ZEND_HAVE_UNSHARE_FS) && defined(HAVE_CHROOT) +CWD_API int virtual_cwd_chroot(const char *path); +#endif CWD_API char *virtual_getcwd_ex(size_t *length); CWD_API char *virtual_getcwd(char *buf, size_t size); CWD_API zend_result virtual_chdir(const char *path); diff --git a/configure.ac b/configure.ac index 40675011a2c0..eb74366be703 100644 --- a/configure.ac +++ b/configure.ac @@ -863,6 +863,25 @@ AS_VAR_IF([PHP_THREAD_SAFETY], [yes], [ PHP thread safety (ZTS) build. ]))]) + dnl This will succeed when building in containers! + dnl But Docker containers with their default policies fail the call at + dnl runtime and fall back to virtual cwd behaviour. + dnl https://docs.docker.com/engine/security/seccomp/#significant-syscalls-blocked-by-the-default-profile + AS_CASE([$host_os], [linux*], [ + AC_CACHE_CHECK([for unshare(CLONE_FS)], [php_cv_unshare_fs], [ + AC_LINK_IFELSE([AC_LANG_PROGRAM([[ + #include + #include + ]], [[ + int fd = open(".", O_PATH | O_DIRECTORY | O_CLOEXEC); + return unshare(CLONE_FS) || fd < 0; + ]])], [php_cv_unshare_fs=yes], [php_cv_unshare_fs=no]) + ]) + AS_VAR_IF([php_cv_unshare_fs], [yes], + [AC_DEFINE([ZEND_HAVE_UNSHARE_FS], [1], + [Define to 1 if ZTS can use Linux per-thread filesystem state.])]) + ]) + AC_MSG_CHECKING([for POSIX threads]) AC_MSG_RESULT([yes]) diff --git a/ext/bz2/bz2.c b/ext/bz2/bz2.c index fd06cb230a7f..44b2ea1f69e7 100644 --- a/ext/bz2/bz2.c +++ b/ext/bz2/bz2.c @@ -215,29 +215,24 @@ PHP_BZ2_API php_stream *_php_stream_bz2open(php_stream_wrapper *wrapper, return NULL; } -#ifdef VIRTUAL_DIR - virtual_filepath_ex(path, &path_copy, NULL); -#else - path_copy = (char *)path; -#endif + if (virtual_cwd_is_emulated()) { + virtual_filepath_ex(path, &path_copy, NULL); + } + const char *bz_path = path_copy ? path_copy : path; - if (php_check_open_basedir(path_copy)) { -#ifdef VIRTUAL_DIR + if (php_check_open_basedir(bz_path)) { efree(path_copy); -#endif return NULL; } /* try and open it directly first */ - bz_file = BZ2_bzopen(path_copy, mode); + bz_file = BZ2_bzopen(bz_path, mode); if (opened_path && bz_file) { - *opened_path = zend_string_init(path_copy, strlen(path_copy), 0); + *opened_path = zend_string_init(bz_path, strlen(bz_path), 0); } -#ifdef VIRTUAL_DIR efree(path_copy); -#endif if (bz_file == NULL) { /* that didn't work, so try and get something from the network/wrapper */ diff --git a/ext/ffi/tests/cdef_zts_cwd.phpt b/ext/ffi/tests/cdef_zts_cwd.phpt new file mode 100644 index 000000000000..bc724973e605 --- /dev/null +++ b/ext/ffi/tests/cdef_zts_cwd.phpt @@ -0,0 +1,32 @@ +--TEST-- +FFI::cdef() loads a relative library from the current directory after chdir() on Linux ZTS +--EXTENSIONS-- +ffi +--INI-- +ffi.enable=1 +--SKIPIF-- +unshare(0x200 /* CLONE_FS */) !== 0) { + die('skip requires per-thread filesystem state'); +} +require dirname(__DIR__, 2) . '/dl_test/tests/skip.inc'; +?> +--FILE-- +get_module() == $absolute->get_module()); +?> +--EXPECT-- +bool(true) diff --git a/ext/standard/basic_functions.stub.php b/ext/standard/basic_functions.stub.php index ec0645720b8c..fc68bcbd7eac 100644 --- a/ext/standard/basic_functions.stub.php +++ b/ext/standard/basic_functions.stub.php @@ -2694,7 +2694,7 @@ function closedir($dir_handle = null): void {} function chdir(string $directory): bool {} -#if (defined(HAVE_CHROOT) && !defined(ZTS) && defined(ENABLE_CHROOT_FUNC)) +#if (defined(HAVE_CHROOT) && (!defined(ZTS) || defined(ZEND_HAVE_UNSHARE_FS)) && defined(ENABLE_CHROOT_FUNC)) function chroot(string $directory): bool {} #endif diff --git a/ext/standard/basic_functions_arginfo.h b/ext/standard/basic_functions_arginfo.h index 62fe3f768665..19d86fcda723 100644 --- a/ext/standard/basic_functions_arginfo.h +++ b/ext/standard/basic_functions_arginfo.h @@ -1,5 +1,5 @@ /* This is a generated file, edit basic_functions.stub.php instead. - * Stub hash: 53d47cfb46b55f62340f435b4637de62c4d0b531 + * Stub hash: 0df5d4a0bbe07804d15fa3bc0b4e687ab9ee9eab * Has decl header: yes */ #include "zend_attributes.h" @@ -1124,7 +1124,7 @@ ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_chdir, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, directory, IS_STRING, 0) ZEND_END_ARG_INFO() -#if (defined(HAVE_CHROOT) && !defined(ZTS) && defined(ENABLE_CHROOT_FUNC)) +#if (defined(HAVE_CHROOT) && (!defined(ZTS) || defined(ZEND_HAVE_UNSHARE_FS)) && defined(ENABLE_CHROOT_FUNC)) ZEND_BEGIN_ARG_WITH_RETURN_TYPE_INFO_EX(arginfo_chroot, 0, 1, _IS_BOOL, 0) ZEND_ARG_TYPE_INFO(0, directory, IS_STRING, 0) ZEND_END_ARG_INFO() @@ -2655,7 +2655,7 @@ ZEND_FUNCTION(opendir); ZEND_FUNCTION(dir); ZEND_FUNCTION(closedir); ZEND_FUNCTION(chdir); -#if (defined(HAVE_CHROOT) && !defined(ZTS) && defined(ENABLE_CHROOT_FUNC)) +#if (defined(HAVE_CHROOT) && (!defined(ZTS) || defined(ZEND_HAVE_UNSHARE_FS)) && defined(ENABLE_CHROOT_FUNC)) ZEND_FUNCTION(chroot); #endif ZEND_FUNCTION(getcwd); @@ -3268,7 +3268,7 @@ static const zend_function_entry ext_functions[] = { ZEND_FE(dir, arginfo_dir) ZEND_FE(closedir, arginfo_closedir) ZEND_FE(chdir, arginfo_chdir) -#if (defined(HAVE_CHROOT) && !defined(ZTS) && defined(ENABLE_CHROOT_FUNC)) +#if (defined(HAVE_CHROOT) && (!defined(ZTS) || defined(ZEND_HAVE_UNSHARE_FS)) && defined(ENABLE_CHROOT_FUNC)) ZEND_FE(chroot, arginfo_chroot) #endif ZEND_FE(getcwd, arginfo_getcwd) diff --git a/ext/standard/basic_functions_decl.h b/ext/standard/basic_functions_decl.h index a7614da39591..0b2e522c70e1 100644 --- a/ext/standard/basic_functions_decl.h +++ b/ext/standard/basic_functions_decl.h @@ -1,8 +1,8 @@ /* This is a generated file, edit basic_functions.stub.php instead. - * Stub hash: 53d47cfb46b55f62340f435b4637de62c4d0b531 */ + * Stub hash: 0df5d4a0bbe07804d15fa3bc0b4e687ab9ee9eab */ -#ifndef ZEND_BASIC_FUNCTIONS_DECL_53d47cfb46b55f62340f435b4637de62c4d0b531_H -#define ZEND_BASIC_FUNCTIONS_DECL_53d47cfb46b55f62340f435b4637de62c4d0b531_H +#ifndef ZEND_BASIC_FUNCTIONS_DECL_0df5d4a0bbe07804d15fa3bc0b4e687ab9ee9eab_H +#define ZEND_BASIC_FUNCTIONS_DECL_0df5d4a0bbe07804d15fa3bc0b4e687ab9ee9eab_H typedef enum zend_enum_SortDirection { ZEND_ENUM_SortDirection_Ascending = 1, @@ -20,4 +20,4 @@ typedef enum zend_enum_RoundingMode { ZEND_ENUM_RoundingMode_PositiveInfinity = 8, } zend_enum_RoundingMode; -#endif /* ZEND_BASIC_FUNCTIONS_DECL_53d47cfb46b55f62340f435b4637de62c4d0b531_H */ +#endif /* ZEND_BASIC_FUNCTIONS_DECL_0df5d4a0bbe07804d15fa3bc0b4e687ab9ee9eab_H */ diff --git a/ext/standard/dir.c b/ext/standard/dir.c index f313d5f539d1..53901b249ff5 100644 --- a/ext/standard/dir.c +++ b/ext/standard/dir.c @@ -309,7 +309,7 @@ PHP_METHOD(Directory, read) RETURN_FALSE; } -#if defined(HAVE_CHROOT) && !defined(ZTS) && defined(ENABLE_CHROOT_FUNC) +#if defined(HAVE_CHROOT) && (!defined(ZTS) || defined(ZEND_HAVE_UNSHARE_FS)) && defined(ENABLE_CHROOT_FUNC) /* {{{ Change root directory */ PHP_FUNCTION(chroot) { @@ -321,7 +321,11 @@ PHP_FUNCTION(chroot) Z_PARAM_PATH(str, str_len) ZEND_PARSE_PARAMETERS_END(); +#ifdef ZTS + ret = virtual_cwd_chroot(str); +#else ret = chroot(str); +#endif if (ret != 0) { php_error_docref(NULL, E_WARNING, "%s (errno %d)", strerror(errno), errno); RETURN_FALSE; @@ -457,7 +461,7 @@ PHP_FUNCTION(glob) int passed_glob_flags = flags & PHP_GLOB_FLAGMASK; #ifdef ZTS - if (!IS_ABSOLUTE_PATH(pattern, pattern_len)) { + if (virtual_cwd_is_emulated() && !IS_ABSOLUTE_PATH(pattern, pattern_len)) { /* System glob uses the current work directory which is not thread safe. * The first fix is to override the functions used to open/read/... paths * with the VCWD ones used in PHP. diff --git a/ext/standard/info.c b/ext/standard/info.c index 097f5900bc32..97c84addb83d 100644 --- a/ext/standard/info.c +++ b/ext/standard/info.c @@ -820,12 +820,7 @@ PHPAPI ZEND_COLD void php_print_info(int flag) php_info_print_table_row(2, "Server API", sapi_module.pretty_name ); } -#ifdef VIRTUAL_DIR - php_info_print_table_row(2, "Virtual Directory Support", "enabled" ); -#else - php_info_print_table_row(2, "Virtual Directory Support", "disabled" ); -#endif - + php_info_print_table_row(2, "Virtual Directory Support", virtual_cwd_is_emulated() ? "enabled" : "disabled"); php_info_print_table_row(2, "Configuration File (php.ini) Path", PHP_CONFIG_FILE_PATH); php_info_print_table_row(2, "Loaded Configuration File", php_ini_opened_path ? php_ini_opened_path : "(none)"); php_info_print_table_row(2, "Scan this dir for additional .ini files", php_ini_scanned_path ? php_ini_scanned_path : "(none)"); diff --git a/ext/standard/tests/dir/chdir_zts_native.phpt b/ext/standard/tests/dir/chdir_zts_native.phpt new file mode 100644 index 000000000000..92f6ee87d99a --- /dev/null +++ b/ext/standard/tests/dir/chdir_zts_native.phpt @@ -0,0 +1,25 @@ +--TEST-- +Linux ZTS chdir changes the native working directory +--EXTENSIONS-- +pcntl +--SKIPIF-- + +--FILE-- +current()->getFilename()); +?> +--EXPECT-- +bool(true) +string(0) "" +string(9) "empty.txt" diff --git a/ext/standard/tests/file/chroot_001.phpt b/ext/standard/tests/file/chroot_001.phpt index 79192e98fc9f..b72207687a82 100644 --- a/ext/standard/tests/file/chroot_001.phpt +++ b/ext/standard/tests/file/chroot_001.phpt @@ -5,18 +5,11 @@ chroot() if (!function_exists("chroot")) { die("skip chroot() not available"); } -// Skip if not being run by root (files are always readable, writeable and executable) -$filename = @tempnam(__DIR__, 'root_check_'); -if (!file_exists($filename)) { - die('WARN Unable to create the "root check" file'); +if (getenv('SKIP_REPEAT')) { + die('skip the changed root persists across repetitions'); } - -$isRoot = fileowner($filename) == 0; - -unlink($filename); - -if (!$isRoot) { - die('SKIP Must be run as root'); +if (!@chroot('/')) { + die('skip chroot is unavailable for this process'); } ?> --FILE-- diff --git a/ext/zip/php_zip.c b/ext/zip/php_zip.c index 08fcac6d1fe1..090e112ee3f9 100644 --- a/ext/zip/php_zip.c +++ b/ext/zip/php_zip.c @@ -731,7 +731,7 @@ static int php_zip_glob(zend_string *spattern, zend_long flags, zval *return_val } #ifdef ZTS - if (!IS_ABSOLUTE_PATH(pattern, pattern_len)) { + if (virtual_cwd_is_emulated() && !IS_ABSOLUTE_PATH(pattern, pattern_len)) { result = VCWD_GETCWD(cwd, MAXPATHLEN); if (!result) { cwd[0] = '\0'; @@ -821,7 +821,7 @@ static int php_zip_pcre(zend_string *regexp, char *path, int path_len, zval *ret pcre2_match_context *mctx = php_pcre_mctx(); #ifdef ZTS - if (!IS_ABSOLUTE_PATH(path, path_len)) { + if (virtual_cwd_is_emulated() && !IS_ABSOLUTE_PATH(path, path_len)) { result = VCWD_GETCWD(cwd, MAXPATHLEN); if (!result) { cwd[0] = '\0'; diff --git a/main/streams/glob_wrapper.c b/main/streams/glob_wrapper.c index c7f1145de200..6c6b33dca686 100644 --- a/main/streams/glob_wrapper.c +++ b/main/streams/glob_wrapper.c @@ -215,7 +215,7 @@ static php_stream *php_glob_stream_opener(php_stream_wrapper *wrapper, const cha char work_pattern[MAXPATHLEN]; char *result; size_t cwd_skip = 0; - if (!IS_ABSOLUTE_PATH(path, strlen(path))) { + if (virtual_cwd_is_emulated() && !IS_ABSOLUTE_PATH(path, strlen(path))) { result = VCWD_GETCWD(cwd, MAXPATHLEN); if (!result) { cwd[0] = '\0'; diff --git a/sapi/fpm/tests/pool-chdir.phpt b/sapi/fpm/tests/pool-chdir.phpt new file mode 100644 index 000000000000..cd932e24cf87 --- /dev/null +++ b/sapi/fpm/tests/pool-chdir.phpt @@ -0,0 +1,52 @@ +--TEST-- +FPM: Requests preserve the pool working directory +--SKIPIF-- + +--FILE-- +start(); +$tester->expectLogStartNotices(); +$expected = "$poolDir\n$poolDir"; +$tester->request()->expectBody($expected); +$tester->request()->expectBody($expected); +$tester->terminate(); +$tester->expectLogTerminatingNotices(); +$tester->close(); +?> +Done +--EXPECT-- +Done +--CLEAN-- +