com php-src: initial implementation of allowed_path_prefix option: Zend/zend.c Zend/zend.h Zend/zend_glob als.h main/fopen_wrappers.c main/main.c

From: Date: Thu, 12 Dec 2013 08:47:42 +0000
Subject: com php-src: initial implementation of allowed_path_prefix option: Zend/zend.c Zend/zend.h Zend/zend_glob als.h main/fopen_wrappers.c main/main.c
Groups: php.cvs 
Request: Send a blank email to php-cvs+get-76635@lists.php.net to get a copy of this message
Commit: cc71c7b5f51f55ee47ad14f9fe9bf15dd89ac9b6 Author: Antony Dovgal <tony2001@php.net> Wed, 13 Feb 2013 20:20:59 +0400 Parents: 0af5ce003a83b21bf85b443d194d734d585d5901 Branches: badoo-5.5_mysqlnd Link: http://git.php.net/?p=php-src.git;a=commitdiff;h=cc71c7b5f51f55ee47ad14f9fe9bf15dd89ac9b6 Log: initial implementation of allowed_path_prefix option Conflicts: Zend/zend.c main/fopen_wrappers.c Changed paths: M Zend/zend.c M Zend/zend.h M Zend/zend_globals.h M main/fopen_wrappers.c M main/main.c Diff: diff --git a/Zend/zend.c b/Zend/zend.c index 11baf34..b7ae7bf 100644 --- a/Zend/zend.c +++ b/Zend/zend.c @@ -101,6 +101,113 @@ static ZEND_INI_MH(OnUpdateScriptEncoding) /* {{{ */ } /* }}} */ +static ZEND_INI_MH(OnUpdateAllowedPathPrefix) /* {{{ */ +{ + int i, part_len; + char *start, *c, *semicolon, *end, *part_start; + + if (CG(allowed_path_prefix) && strcmp(new_value, CG(allowed_path_prefix)) == 0) { + /* do nothing */ + return SUCCESS; + } + + if (CG(allowed_path_prefix)) { + free(CG(allowed_path_prefix)); + } + CG(allowed_path_prefix) = strdup(new_value); + + /* free the old value */ + if (CG(allowed_path_prefixes)) { + for (i = 0; i < CG(allowed_path_prefixes_size); i++) { + char *prefix = CG(allowed_path_prefixes)[i]; + free(prefix); + } + free(CG(allowed_path_prefixes)); + } + CG(allowed_path_prefixes_size) = 0; + + /* parse & create the new one */ + c = start = CG(allowed_path_prefix); + end = start + new_value_length; + semicolon = NULL; + while (c <= end) { + if (c == end || *c == ':') { + if (!semicolon) { + part_start = start; + } else { + part_start = semicolon; + } + part_len = c - part_start; + + semicolon = c + 1; /* +1 to skip the semicolon */ + if (part_len > 0) { + char *value_copy; + + CG(allowed_path_prefixes) = realloc(CG(allowed_path_prefixes), sizeof(void *) * (CG(allowed_path_prefixes_size) + 1)); + if (!CG(allowed_path_prefixes)) { + free(CG(allowed_path_prefix)); + CG(allowed_path_prefix) = NULL; + CG(allowed_path_prefixes_size) = 0; + return FAILURE; + } + + CG(allowed_path_prefixes_len) = realloc(CG(allowed_path_prefixes_len), sizeof(int) * (CG(allowed_path_prefixes_size) + 1)); + if (!CG(allowed_path_prefixes_len)) { + free(CG(allowed_path_prefix)); + CG(allowed_path_prefix) = NULL; + CG(allowed_path_prefixes_size) = 0; + return FAILURE; + } + + value_copy = malloc(part_len + 1); + if (!value_copy) { + free(CG(allowed_path_prefix)); + CG(allowed_path_prefix) = NULL; + CG(allowed_path_prefixes_size) = 0; + return FAILURE; + } + memcpy(value_copy, part_start, part_len); + value_copy[part_len] = '\0'; + + CG(allowed_path_prefixes)[CG(allowed_path_prefixes_size)] = value_copy; + CG(allowed_path_prefixes_len)[CG(allowed_path_prefixes_size)] = part_len; + CG(allowed_path_prefixes_size)++; + } + } + c++; + } + return SUCCESS; +} +/* }}} */ + +ZEND_API int zend_check_allowed_path_prefix(const char *filename, int filename_len) /* {{{ */ +{ + int i; + + if (!CG(allowed_path_prefixes_size)) { + return SUCCESS; + } + + for (i = 0; i < CG(allowed_path_prefixes_size); i++) { + char *prefix = CG(allowed_path_prefixes)[i]; + int prefix_len = CG(allowed_path_prefixes_len)[i]; + + if (filename_len < prefix_len) { + continue; + } + + if (memcmp(filename, prefix, prefix_len) == 0) { + return SUCCESS; + } + } + + if (CG(allowed_path_prefix_emulate)) { + zend_error(E_WARNING, "[path] file '%s' is out of allowed path prefix '%s'", filename, CG(allowed_path_prefix)); + return SUCCESS; + } + return FAILURE; +} +/* }}} */ ZEND_INI_BEGIN() ZEND_INI_ENTRY("error_reporting", NULL, ZEND_INI_ALL, OnUpdateErrorReporting) @@ -111,6 +218,8 @@ ZEND_INI_BEGIN() #ifdef ZEND_SIGNALS STD_ZEND_INI_BOOLEAN("zend.signal_check", "0", ZEND_INI_SYSTEM, OnUpdateBool, check, zend_signal_globals_t, zend_signal_globals) #endif + STD_ZEND_INI_ENTRY("allowed_path_prefix", "", ZEND_INI_SYSTEM, OnUpdateAllowedPathPrefix, allowed_path_prefix, zend_compiler_globals, compiler_globals) + STD_ZEND_INI_ENTRY("allowed_path_prefix_emulate", "0", ZEND_INI_SYSTEM, OnUpdateBool, allowed_path_prefix_emulate, zend_compiler_globals, compiler_globals) ZEND_INI_END() @@ -539,6 +648,8 @@ static void compiler_globals_ctor(zend_compiler_globals *compiler_globals TSRMLS static void compiler_globals_dtor(zend_compiler_globals *compiler_globals TSRMLS_DC) /* {{{ */ { + int i; + if (compiler_globals->function_table != GLOBAL_FUNCTION_TABLE) { zend_hash_destroy(compiler_globals->function_table); free(compiler_globals->function_table); @@ -820,6 +931,24 @@ void zend_shutdown(TSRMLS_D) /* {{{ */ zend_destroy_rsrc_list(&EG(persistent_list) TSRMLS_CC); zend_destroy_modules(); + if (CG(allowed_path_prefixes)) { + int i; + for (i = 0; i < CG(allowed_path_prefixes_size); i++) { + char *prefix = CG(allowed_path_prefixes)[i]; + free(prefix); + } + free(CG(allowed_path_prefixes)); + } + CG(allowed_path_prefixes_size) = 0; + + if (CG(allowed_path_prefixes_len)) { + free(CG(allowed_path_prefixes_len)); + } + + if (CG(allowed_path_prefix)) { + free(CG(allowed_path_prefix)); + } + zend_hash_destroy(GLOBAL_FUNCTION_TABLE); zend_hash_destroy(GLOBAL_CLASS_TABLE); diff --git a/Zend/zend.h b/Zend/zend.h index af653b1..c1d89ca 100644 --- a/Zend/zend.h +++ b/Zend/zend.h @@ -649,6 +649,7 @@ ZEND_API void zend_print_zval_r(zval *expr, int indent TSRMLS_DC); ZEND_API void zend_print_flat_zval_r(zval *expr TSRMLS_DC); ZEND_API void zend_print_zval_r_ex(zend_write_func_t write_func, zval *expr, int indent TSRMLS_DC); ZEND_API void zend_output_debug_string(zend_bool trigger_break, const char *format, ...) ZEND_ATTRIBUTE_FORMAT(printf, 2, 3); +ZEND_API int zend_check_allowed_path_prefix(const char *filename, int filename_len); END_EXTERN_C() void zend_activate(TSRMLS_D); diff --git a/Zend/zend_globals.h b/Zend/zend_globals.h index b9a5b39..1437001 100644 --- a/Zend/zend_globals.h +++ b/Zend/zend_globals.h @@ -155,6 +155,11 @@ struct _zend_compiler_globals { zval ***static_members_table; int last_static_member; #endif + char *allowed_path_prefix; + char **allowed_path_prefixes; + int *allowed_path_prefixes_len; + int allowed_path_prefixes_size; + zend_bool allowed_path_prefix_emulate; }; diff --git a/main/fopen_wrappers.c b/main/fopen_wrappers.c index 9b8645a..02e0c4b 100644 --- a/main/fopen_wrappers.c +++ b/main/fopen_wrappers.c @@ -365,6 +365,7 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle TSRMLS_DC) zend_bool orig_display_errors; path_info = SG(request_info).request_uri; + #if HAVE_PWD_H if (PG(user_dir) && *PG(user_dir) && path_info && '/' == path_info[0] && '~' == path_info[1]) { char *s = strchr(path_info + 2, '/'); @@ -443,6 +444,11 @@ PHPAPI int php_fopen_primary_script(zend_file_handle *file_handle TSRMLS_DC) SG(request_info).path_translated = NULL; return FAILURE; } + + if (zend_check_allowed_path_prefix(resolved_path, strlen(resolved_path)) != SUCCESS) { + efree(resolved_path); + return FAILURE; + } efree(resolved_path); orig_display_errors = PG(display_errors); diff --git a/main/main.c b/main/main.c index 691f72e..68e2007 100644 --- a/main/main.c +++ b/main/main.c @@ -1364,6 +1364,11 @@ PHPAPI int php_stream_open_for_zend_ex(const char *filename, zend_file_handle *h size_t len, mapped_len; php_stream *stream = php_stream_open_wrapper((char *)filename, "rb", mode, &handle->opened_path); + if (stream && stream->orig_path && zend_check_allowed_path_prefix(stream->orig_path, strlen(stream->orig_path)) != SUCCESS) { + php_stream_close(stream); + return FAILURE; + } + if (stream) { #if HAVE_MMAP || defined(PHP_WIN32) size_t page_size = REAL_PAGE_SIZE;

« previous php.cvs (#76635) next »