mirror of
https://github.com/php/php-src.git
synced 2025-08-16 05:58:45 +02:00
Virtualize realpath, chmod, chown and utime
This should fix #5935 and #5904 @- Virtualize realpath, chmod, chown and utime (Stas)
This commit is contained in:
parent
868e3b1db8
commit
c756ae2d9f
6 changed files with 110 additions and 10 deletions
|
@ -1979,15 +1979,16 @@ PHP_FUNCTION(fgetcsv) {
|
|||
PHP_FUNCTION(realpath)
|
||||
{
|
||||
zval **path;
|
||||
char resolved_path[MAXPATHLEN];
|
||||
char resolved_path_buff[MAXPATHLEN];
|
||||
|
||||
if (ZEND_NUM_ARGS() != 1 || zend_get_parameters_ex(ZEND_NUM_ARGS(), &path) == FAILURE) {
|
||||
WRONG_PARAM_COUNT;
|
||||
}
|
||||
|
||||
convert_to_string_ex(path);
|
||||
if (php_realpath((*path)->value.str.val, resolved_path)) {
|
||||
RETURN_STRING(resolved_path, 1);
|
||||
|
||||
if (V_REALPATH((*path)->value.str.val, resolved_path_buff)) {
|
||||
RETURN_STRING(resolved_path_buff, 1);
|
||||
} else {
|
||||
RETURN_FALSE;
|
||||
}
|
||||
|
|
|
@ -260,7 +260,7 @@ PHP_FUNCTION(chgrp)
|
|||
if (php_check_open_basedir((*filename)->value.str.val))
|
||||
RETURN_FALSE;
|
||||
|
||||
ret = chown((*filename)->value.str.val, -1, gid);
|
||||
ret = V_CHOWN((*filename)->value.str.val, -1, gid);
|
||||
if (ret == -1) {
|
||||
php_error(E_WARNING, "chgrp failed: %s", strerror(errno));
|
||||
RETURN_FALSE;
|
||||
|
@ -308,7 +308,7 @@ PHP_FUNCTION(chown)
|
|||
if (php_check_open_basedir((*filename)->value.str.val))
|
||||
RETURN_FALSE;
|
||||
|
||||
ret = chown((*filename)->value.str.val, uid, -1);
|
||||
ret = V_CHOWN((*filename)->value.str.val, uid, -1);
|
||||
if (ret == -1) {
|
||||
php_error(E_WARNING, "chown failed: %s", strerror(errno));
|
||||
RETURN_FALSE;
|
||||
|
@ -349,7 +349,7 @@ PHP_FUNCTION(chmod)
|
|||
if(PG(safe_mode))
|
||||
imode &= 0777;
|
||||
|
||||
ret = chmod((*filename)->value.str.val, imode);
|
||||
ret = V_CHMOD((*filename)->value.str.val, imode);
|
||||
if (ret == -1) {
|
||||
php_error(E_WARNING, "chmod failed: %s", strerror(errno));
|
||||
RETURN_FALSE;
|
||||
|
@ -419,7 +419,7 @@ PHP_FUNCTION(touch)
|
|||
fclose(file);
|
||||
}
|
||||
|
||||
ret = utime((*filename)->value.str.val, newtime);
|
||||
ret = V_UTIME((*filename)->value.str.val, newtime);
|
||||
if (newtime) efree(newtime);
|
||||
if (ret == -1) {
|
||||
php_error(E_WARNING, "utime failed: %s", strerror(errno));
|
||||
|
|
|
@ -187,7 +187,7 @@ PHPAPI int php_check_specific_open_basedir(char *basedir, char *path PLS_DC)
|
|||
}
|
||||
|
||||
/* Resolve the real path into resolved_name */
|
||||
if ((php_realpath(path, resolved_name) != NULL) && (php_realpath(local_open_basedir, resolved_basedir) != NULL)) {
|
||||
if ((V_REALPATH(path, resolved_name) != NULL) && (V_REALPATH(local_open_basedir, resolved_basedir) != NULL)) {
|
||||
/* Check the path */
|
||||
#ifdef PHP_WIN32
|
||||
if (strncasecmp(resolved_basedir, resolved_name, strlen(resolved_basedir)) == 0) {
|
||||
|
|
18
main/php.h
18
main/php.h
|
@ -303,6 +303,7 @@ PHPAPI int cfg_get_string(char *varname, char **result);
|
|||
#define V_CHDIR(path) virtual_chdir(path)
|
||||
#define V_CHDIR_FILE(path) virtual_chdir_file(path)
|
||||
#define V_GETWD(buf)
|
||||
#define V_REALPATH(path,realpath) virtual_realpath(path,realpath)
|
||||
#define V_STAT(path, buff) virtual_stat(path, buff)
|
||||
#ifdef PHP_WIN32
|
||||
#define V_LSTAT(path, buff) virtual_stat(path, buff)
|
||||
|
@ -314,7 +315,13 @@ PHPAPI int cfg_get_string(char *varname, char **result);
|
|||
#define V_RMDIR(pathname) virtual_rmdir(pathname)
|
||||
#define V_OPENDIR(pathname) virtual_opendir(pathname)
|
||||
#define V_POPEN(command, type) virtual_popen(command, type)
|
||||
|
||||
#if HAVE_UTIME
|
||||
#define V_UTIME(path,time) virtual_utime(path,time)
|
||||
#endif
|
||||
#define V_CHMOD(path,mode) virtual_chmod(path,mode)
|
||||
#ifndef PHP_WIN32
|
||||
#define V_CHOWN(path,owner,group) virtual_chown(path,owner,group)
|
||||
#endif
|
||||
#else
|
||||
|
||||
#define V_GETCWD(buff, size) getcwd(buff,size)
|
||||
|
@ -331,7 +338,14 @@ PHPAPI int cfg_get_string(char *varname, char **result);
|
|||
#define V_RMDIR(pathname) rmdir(pathname)
|
||||
#define V_OPENDIR(pathname) opendir(pathname)
|
||||
#define V_POPEN(command, type) popen(command, type)
|
||||
|
||||
#define V_REALPATH(path,realpath) realpath(path,realpath)
|
||||
#if HAVE_UTIME
|
||||
#define V_UTIME(path,time) utime(path,time)
|
||||
#endif
|
||||
#define V_CHMOD(path,mode) chmod(path,mode)
|
||||
#ifndef PHP_WIN32
|
||||
#define V_CHOWN(path,owner,group) chown(path,owner,group)
|
||||
#endif
|
||||
#endif
|
||||
|
||||
#include "zend_constants.h"
|
||||
|
|
|
@ -33,6 +33,15 @@
|
|||
#include "win95nt.h"
|
||||
#endif
|
||||
|
||||
#if HAVE_UTIME
|
||||
# ifdef PHP_WIN32
|
||||
# include <sys/utime.h>
|
||||
# else
|
||||
# include <utime.h>
|
||||
# endif
|
||||
#endif
|
||||
|
||||
|
||||
#include "php_virtual_cwd.h"
|
||||
#include "php_reentrancy.h" /* for php_strtok_r */
|
||||
|
||||
|
@ -390,6 +399,7 @@ CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func
|
|||
ret = 1;
|
||||
} else {
|
||||
CWD_STATE_FREE(old_state);
|
||||
ret = (verify_path)? 0:1;
|
||||
}
|
||||
|
||||
free(old_state);
|
||||
|
@ -438,6 +448,24 @@ CWD_API int virtual_chdir_file(char *path)
|
|||
return retval;
|
||||
}
|
||||
|
||||
CWD_API char *virtual_realpath(char *path, char *real_path)
|
||||
{
|
||||
cwd_state new_state;
|
||||
int retval;
|
||||
CWDLS_FETCH();
|
||||
|
||||
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
||||
retval = virtual_file_ex(&new_state, path, NULL);
|
||||
|
||||
if(retval) {
|
||||
int len = min(MAXPATHLEN-1,new_state.cwd_length);
|
||||
memcpy(real_path, new_state.cwd, len);
|
||||
real_path[len] = '\0';
|
||||
return real_path;
|
||||
}
|
||||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
CWD_API int virtual_filepath(char *path, char **filepath)
|
||||
{
|
||||
|
@ -472,6 +500,55 @@ CWD_API FILE *virtual_fopen(const char *path, const char *mode)
|
|||
return f;
|
||||
}
|
||||
|
||||
#if HAVE_UTIME
|
||||
CWD_API int virtual_utime(const char *filename, struct utimbuf *buf)
|
||||
{
|
||||
cwd_state new_state;
|
||||
int ret;
|
||||
CWDLS_FETCH();
|
||||
|
||||
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
||||
virtual_file_ex(&new_state, filename, NULL);
|
||||
|
||||
ret = utime(new_state.cwd, buf);
|
||||
|
||||
CWD_STATE_FREE(&new_state);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
CWD_API int virtual_chmod(const char *filename, mode_t mode)
|
||||
{
|
||||
cwd_state new_state;
|
||||
int ret;
|
||||
CWDLS_FETCH();
|
||||
|
||||
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
||||
virtual_file_ex(&new_state, filename, NULL);
|
||||
|
||||
ret = chmod(new_state.cwd, mode);
|
||||
|
||||
CWD_STATE_FREE(&new_state);
|
||||
return ret;
|
||||
}
|
||||
|
||||
#ifndef PHP_WIN32
|
||||
CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group)
|
||||
{
|
||||
cwd_state new_state;
|
||||
int ret;
|
||||
CWDLS_FETCH();
|
||||
|
||||
CWD_STATE_COPY(&new_state, &CWDG(cwd));
|
||||
virtual_file_ex(&new_state, filename, NULL);
|
||||
|
||||
ret = chown(new_state.cwd, owner, group);
|
||||
|
||||
CWD_STATE_FREE(&new_state);
|
||||
return ret;
|
||||
}
|
||||
#endif
|
||||
|
||||
CWD_API int virtual_open(const char *path, int flags, ...)
|
||||
{
|
||||
cwd_state new_state;
|
||||
|
|
|
@ -69,6 +69,7 @@ CWD_API char *virtual_getcwd(char *buf, size_t size);
|
|||
CWD_API int virtual_chdir(char *path);
|
||||
CWD_API int virtual_chdir_file(char *path);
|
||||
CWD_API int virtual_filepath(char *path, char **filepath);
|
||||
CWD_API char *virtual_realpath(char *path, char *real_path);
|
||||
CWD_API FILE *virtual_fopen(const char *path, const char *mode);
|
||||
CWD_API int virtual_open(const char *path, int flags, ...);
|
||||
CWD_API int virtual_creat(const char *path, mode_t mode);
|
||||
|
@ -81,6 +82,13 @@ CWD_API int virtual_mkdir(const char *pathname, mode_t mode);
|
|||
CWD_API int virtual_rmdir(const char *pathname);
|
||||
CWD_API DIR *virtual_opendir(const char *pathname);
|
||||
CWD_API FILE *virtual_popen(const char *command, const char *type);
|
||||
#if HAVE_UTIME
|
||||
CWD_API int virtual_utime(const char *filename, struct utimbuf *buf);
|
||||
#endif
|
||||
CWD_API int virtual_chmod(const char *filename, mode_t mode);
|
||||
#ifndef PHP_WIN32
|
||||
CWD_API int virtual_chown(const char *filename, uid_t owner, gid_t group);
|
||||
#endif
|
||||
|
||||
CWD_API int virtual_file_ex(cwd_state *state, const char *path, verify_path_func verify_path);
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue