Skip to content

Commit dc9f728

Browse files
committed
Add system ID entropy API
The `zend_system_id` is a (true global) system ID that fingerprints a process state. When extensions add engine hooks during MINIT/startup, entropy is added the system ID for each hook. This allows extensions to identify that changes have been made to the engine since the last PHP process restart.
1 parent 3375374 commit dc9f728

14 files changed

+148
-56
lines changed

Zend/zend_execute.c

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@
4141
#include "zend_type_info.h"
4242
#include "zend_smart_str.h"
4343
#include "zend_observer.h"
44+
#include "zend_system_id.h"
4445

4546
/* Virtual current working directory support */
4647
#include "zend_virtual_cwd.h"

Zend/zend_extensions.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
*/
1919

2020
#include "zend_extensions.h"
21+
#include "zend_system_id.h"
2122

2223
ZEND_API zend_llist zend_extensions;
2324
ZEND_API uint32_t zend_extension_flags = 0;
@@ -247,18 +248,19 @@ ZEND_API void zend_extension_dispatch_message(int message, void *arg)
247248
}
248249

249250

250-
ZEND_API int zend_get_resource_handle(zend_extension *extension)
251+
ZEND_API int zend_get_resource_handle(const char *module_name)
251252
{
252253
if (last_resource_number<ZEND_MAX_RESERVED_RESOURCES) {
253-
extension->resource_number = last_resource_number;
254+
zend_add_system_entropy(module_name, "zend_get_resource_handle", &last_resource_number, sizeof(int));
254255
return last_resource_number++;
255256
} else {
256257
return -1;
257258
}
258259
}
259260

260-
ZEND_API int zend_get_op_array_extension_handle(void)
261+
ZEND_API int zend_get_op_array_extension_handle(const char *module_name)
261262
{
263+
zend_add_system_entropy(module_name, "zend_get_op_array_extension_handle", &zend_op_array_extension_handles, sizeof(int));
262264
return zend_op_array_extension_handles++;
263265
}
264266

Zend/zend_extensions.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,8 +113,8 @@ struct _zend_extension {
113113
BEGIN_EXTERN_C()
114114
extern ZEND_API int zend_op_array_extension_handles;
115115

116-
ZEND_API int zend_get_resource_handle(zend_extension *extension);
117-
ZEND_API int zend_get_op_array_extension_handle(void);
116+
ZEND_API int zend_get_resource_handle(const char *module_name);
117+
ZEND_API int zend_get_op_array_extension_handle(const char *module_name);
118118
ZEND_API void zend_extension_dispatch_message(int message, void *arg);
119119
END_EXTERN_C()
120120

Zend/zend_system_id.c

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| http://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Authors: Sammy Kaye Powers <[email protected]> |
14+
| Dmitry Stogov <[email protected]> |
15+
+----------------------------------------------------------------------+
16+
*/
17+
18+
#include "php.h"
19+
#include "zend_system_id.h"
20+
#include "zend_extensions.h"
21+
#include "ext/standard/md5.h"
22+
#include "ext/hash/php_hash.h"
23+
24+
ZEND_API char zend_system_id[32];
25+
26+
static PHP_MD5_CTX context;
27+
static int finalized = 0;
28+
29+
ZEND_API ZEND_RESULT_CODE zend_add_system_entropy(const char *module_name, const char *hook_name, const void *data, size_t size)
30+
{
31+
if (finalized == 0) {
32+
PHP_MD5Update(&context, module_name, strlen(module_name));
33+
PHP_MD5Update(&context, hook_name, strlen(hook_name));
34+
if (size) {
35+
PHP_MD5Update(&context, data, size);
36+
}
37+
return SUCCESS;
38+
}
39+
return FAILURE;
40+
}
41+
42+
#define ZEND_BIN_ID "BIN_" ZEND_TOSTR(SIZEOF_INT) ZEND_TOSTR(SIZEOF_LONG) ZEND_TOSTR(SIZEOF_SIZE_T) ZEND_TOSTR(SIZEOF_ZEND_LONG) ZEND_TOSTR(ZEND_MM_ALIGNMENT)
43+
44+
void zend_startup_system_id(void)
45+
{
46+
PHP_MD5Init(&context);
47+
PHP_MD5Update(&context, PHP_VERSION, sizeof(PHP_VERSION)-1);
48+
PHP_MD5Update(&context, ZEND_EXTENSION_BUILD_ID, sizeof(ZEND_EXTENSION_BUILD_ID)-1);
49+
PHP_MD5Update(&context, ZEND_BIN_ID, sizeof(ZEND_BIN_ID)-1);
50+
if (strstr(PHP_VERSION, "-dev") != 0) {
51+
/* Development versions may be changed from build to build */
52+
PHP_MD5Update(&context, __DATE__, sizeof(__DATE__)-1);
53+
PHP_MD5Update(&context, __TIME__, sizeof(__TIME__)-1);
54+
}
55+
zend_system_id[0] = '\0';
56+
}
57+
58+
#define ZEND_HOOK_AST_PROCESS (1 << 0)
59+
#define ZEND_HOOK_COMPILE_FILE (1 << 1)
60+
#define ZEND_HOOK_EXECUTE_EX (1 << 2)
61+
#define ZEND_HOOK_EXECUTE_INTERNAL (1 << 3)
62+
63+
void zend_finalize_system_id(void)
64+
{
65+
unsigned char digest[16];
66+
zend_uchar hooks = 0;
67+
int16_t i = 0;
68+
69+
if (zend_ast_process) {
70+
hooks |= ZEND_HOOK_AST_PROCESS;
71+
}
72+
if (zend_compile_file != compile_file) {
73+
hooks |= ZEND_HOOK_COMPILE_FILE;
74+
}
75+
if (zend_execute_ex != execute_ex) {
76+
hooks |= ZEND_HOOK_EXECUTE_EX;
77+
}
78+
if (zend_execute_internal) {
79+
hooks |= ZEND_HOOK_EXECUTE_INTERNAL;
80+
}
81+
PHP_MD5Update(&context, &hooks, sizeof(zend_uchar));
82+
83+
for (; i < 256; i++) {
84+
if (zend_get_user_opcode_handler((zend_uchar) i) != NULL) {
85+
PHP_MD5Update(&context, &i, sizeof(int16_t));
86+
}
87+
}
88+
89+
PHP_MD5Final(digest, &context);
90+
php_hash_bin2hex(zend_system_id, digest, sizeof digest);
91+
finalized = 1;
92+
}

Zend/zend_system_id.h

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
/*
2+
+----------------------------------------------------------------------+
3+
| Copyright (c) The PHP Group |
4+
+----------------------------------------------------------------------+
5+
| This source file is subject to version 3.01 of the PHP license, |
6+
| that is bundled with this package in the file LICENSE, and is |
7+
| available through the world-wide-web at the following url: |
8+
| http://www.php.net/license/3_01.txt |
9+
| If you did not receive a copy of the PHP license and are unable to |
10+
| obtain it through the world-wide-web, please send a note to |
11+
| [email protected] so we can mail you a copy immediately. |
12+
+----------------------------------------------------------------------+
13+
| Author: Sammy Kaye Powers <[email protected]> |
14+
+----------------------------------------------------------------------+
15+
*/
16+
17+
#ifndef ZEND_SYSTEM_ID_H
18+
#define ZEND_SYSTEM_ID_H
19+
20+
BEGIN_EXTERN_C()
21+
/* True global; Write-only during MINIT/startup */
22+
extern ZEND_API char zend_system_id[32];
23+
24+
ZEND_API ZEND_RESULT_CODE zend_add_system_entropy(const char *module_name, const char *hook_name, const void *data, size_t size);
25+
END_EXTERN_C()
26+
27+
void zend_startup_system_id(void);
28+
void zend_finalize_system_id(void);
29+
30+
#endif /* ZEND_SYSTEM_ID_H */

configure.ac

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1464,7 +1464,7 @@ PHP_ADD_SOURCES(Zend, \
14641464
zend_closures.c zend_weakrefs.c zend_float.c zend_string.c zend_signal.c zend_generators.c \
14651465
zend_virtual_cwd.c zend_ast.c zend_objects.c zend_object_handlers.c zend_objects_API.c \
14661466
zend_default_classes.c zend_inheritance.c zend_smart_str.c zend_cpuinfo.c zend_gdb.c \
1467-
zend_observer.c, \
1467+
zend_observer.c zend_system_id.c, \
14681468
-DZEND_ENABLE_STATIC_TSRMLS_CACHE=1)
14691469

14701470
PHP_ADD_BUILD_DIR(main main/streams)

ext/opcache/Optimizer/zend_func_info.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -943,11 +943,10 @@ uint32_t zend_get_func_info(
943943

944944
int zend_func_info_startup(void)
945945
{
946-
zend_extension dummy;
947946
size_t i;
948947

949948
if (zend_func_info_rid == -1) {
950-
zend_func_info_rid = zend_get_resource_handle(&dummy);
949+
zend_func_info_rid = zend_get_resource_handle("Zend Optimizer");
951950
if (zend_func_info_rid < 0) {
952951
return FAILURE;
953952
}

ext/opcache/ZendAccelerator.c

Lines changed: 0 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,6 @@ ZEND_TSRMLS_CACHE_DEFINE()
107107
zend_accel_shared_globals *accel_shared_globals = NULL;
108108

109109
/* true globals, no need for thread safety */
110-
char accel_system_id[32];
111110
#ifdef ZEND_WIN32
112111
char accel_uname_id[32];
113112
#endif
@@ -127,7 +126,6 @@ static zif_handler orig_chdir = NULL;
127126
static ZEND_INI_MH((*orig_include_path_on_modify)) = NULL;
128127
static zend_result (*orig_post_startup_cb)(void);
129128

130-
static void accel_gen_system_id(void);
131129
static zend_result accel_post_startup(void);
132130
static int accel_finish_startup(void);
133131

@@ -2710,42 +2708,6 @@ static void accel_globals_ctor(zend_accel_globals *accel_globals)
27102708
memset(accel_globals, 0, sizeof(zend_accel_globals));
27112709
}
27122710

2713-
#define ZEND_BIN_ID "BIN_" ZEND_TOSTR(SIZEOF_INT) ZEND_TOSTR(SIZEOF_LONG) ZEND_TOSTR(SIZEOF_SIZE_T) ZEND_TOSTR(SIZEOF_ZEND_LONG) ZEND_TOSTR(ZEND_MM_ALIGNMENT)
2714-
2715-
static void accel_gen_system_id(void)
2716-
{
2717-
PHP_MD5_CTX context;
2718-
unsigned char digest[16];
2719-
zend_module_entry *module;
2720-
zend_extension *extension;
2721-
zend_llist_position pos;
2722-
2723-
PHP_MD5Init(&context);
2724-
PHP_MD5Update(&context, PHP_VERSION, sizeof(PHP_VERSION)-1);
2725-
PHP_MD5Update(&context, ZEND_EXTENSION_BUILD_ID, sizeof(ZEND_EXTENSION_BUILD_ID)-1);
2726-
PHP_MD5Update(&context, ZEND_BIN_ID, sizeof(ZEND_BIN_ID)-1);
2727-
if (strstr(PHP_VERSION, "-dev") != 0) {
2728-
/* Development versions may be changed from build to build */
2729-
PHP_MD5Update(&context, __DATE__, sizeof(__DATE__)-1);
2730-
PHP_MD5Update(&context, __TIME__, sizeof(__TIME__)-1);
2731-
}
2732-
/* Modules may have changed after restart which can cause dangling pointers from
2733-
* custom opcode handlers in the second-level cache files
2734-
*/
2735-
ZEND_HASH_FOREACH_PTR(&module_registry, module) {
2736-
PHP_MD5Update(&context, module->name, strlen(module->name));
2737-
PHP_MD5Update(&context, module->version, strlen(module->version));
2738-
} ZEND_HASH_FOREACH_END();
2739-
extension = (zend_extension *) zend_llist_get_first_ex(&zend_extensions, &pos);
2740-
while (extension) {
2741-
PHP_MD5Update(&context, extension->name, strlen(extension->name));
2742-
PHP_MD5Update(&context, extension->version, strlen(extension->version));
2743-
extension = (zend_extension *) zend_llist_get_next_ex(&zend_extensions, &pos);
2744-
}
2745-
PHP_MD5Final(digest, &context);
2746-
php_hash_bin2hex(accel_system_id, digest, sizeof digest);
2747-
}
2748-
27492711
#ifdef HAVE_HUGE_CODE_PAGES
27502712
# ifndef _WIN32
27512713
# include <sys/mman.h>
@@ -2930,8 +2892,6 @@ static int accel_startup(zend_extension *extension)
29302892
# endif
29312893
#endif
29322894

2933-
accel_gen_system_id();
2934-
29352895
if (start_accel_module() == FAILURE) {
29362896
accel_startup_ok = 0;
29372897
zend_error(E_WARNING, ACCELERATOR_PRODUCT_NAME ": module registration failed!");

ext/opcache/ZendAccelerator.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -289,7 +289,6 @@ typedef struct _zend_accel_shared_globals {
289289
zend_string_table interned_strings;
290290
} zend_accel_shared_globals;
291291

292-
extern char accel_system_id[32];
293292
#ifdef ZEND_WIN32
294293
extern char accel_uname_id[32];
295294
#endif

ext/opcache/jit/zend_jit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3929,7 +3929,7 @@ ZEND_EXT_API int zend_jit_startup(void *buf, size_t size, zend_bool reattached)
39293929
return FAILURE;
39303930
}
39313931

3932-
zend_jit_profile_counter_rid = zend_get_op_array_extension_handle();
3932+
zend_jit_profile_counter_rid = zend_get_op_array_extension_handle(ACCELERATOR_PRODUCT_NAME);
39333933

39343934
#ifdef HAVE_GDB
39353935
zend_jit_gdb_init();

ext/opcache/shared_alloc_win32.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@
2424
#include "zend_shared_alloc.h"
2525
#include "zend_accelerator_util_funcs.h"
2626
#include "zend_execute.h"
27+
#include "zend_system_id.h"
2728
#include "SAPI.h"
2829
#include "tsrm_win32.h"
2930
#include "win32/winutil.h"
@@ -71,7 +72,7 @@ static void zend_win_error_message(int type, char *msg, int err)
7172
static char *create_name_with_username(char *name)
7273
{
7374
static char newname[MAXPATHLEN + 32 + 4 + 1 + 32 + 21];
74-
snprintf(newname, sizeof(newname) - 1, "%s@%.32s@%.20s@%.32s", name, accel_uname_id, sapi_module.name, accel_system_id);
75+
snprintf(newname, sizeof(newname) - 1, "%s@%.32s@%.20s@%.32s", name, accel_uname_id, sapi_module.name, zend_system_id);
7576

7677
return newname;
7778
}

ext/opcache/zend_file_cache.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@
2222
#include "zend_vm.h"
2323
#include "zend_interfaces.h"
2424
#include "zend_attributes.h"
25+
#include "zend_system_id.h"
2526

2627
#include "php.h"
2728
#ifdef ZEND_WIN32
@@ -884,7 +885,7 @@ static void zend_file_cache_serialize(zend_persistent_script *script,
884885
zend_persistent_script *new_script;
885886

886887
memcpy(info->magic, "OPCACHE", 8);
887-
memcpy(info->system_id, accel_system_id, 32);
888+
memcpy(info->system_id, zend_system_id, 32);
888889
info->mem_size = script->size;
889890
info->str_size = 0;
890891
info->script_offset = (char*)script - (char*)script->mem;
@@ -914,7 +915,7 @@ static char *zend_file_cache_get_bin_file_path(zend_string *script_path)
914915
filename = emalloc(len + 33 + ZSTR_LEN(script_path) + sizeof(SUFFIX));
915916
memcpy(filename, ZCG(accel_directives).file_cache, len);
916917
filename[len] = '/';
917-
memcpy(filename + len + 1, accel_system_id, 32);
918+
memcpy(filename + len + 1, zend_system_id, 32);
918919
memcpy(filename + len + 33, ZSTR_VAL(script_path), ZSTR_LEN(script_path));
919920
memcpy(filename + len + 33 + ZSTR_LEN(script_path), SUFFIX, sizeof(SUFFIX));
920921
#else
@@ -928,7 +929,7 @@ static char *zend_file_cache_get_bin_file_path(zend_string *script_path)
928929
len += 1 + 32;
929930
filename[len] = '\\';
930931

931-
memcpy(filename + len + 1, accel_system_id, 32);
932+
memcpy(filename + len + 1, zend_system_id, 32);
932933

933934
if (ZSTR_LEN(script_path) >= 7 && ':' == ZSTR_VAL(script_path)[4] && '/' == ZSTR_VAL(script_path)[5] && '/' == ZSTR_VAL(script_path)[6]) {
934935
/* phar:// or file:// */
@@ -1688,7 +1689,7 @@ zend_persistent_script *zend_file_cache_script_load(zend_file_handle *file_handl
16881689
efree(filename);
16891690
return NULL;
16901691
}
1691-
if (memcmp(info.system_id, accel_system_id, 32) != 0) {
1692+
if (memcmp(info.system_id, zend_system_id, 32) != 0) {
16921693
zend_accel_error(ACCEL_LOG_WARNING, "opcache cannot read from file '%s' (wrong \"system_id\")\n", filename);
16931694
zend_file_cache_flock(fd, LOCK_UN);
16941695
close(fd);

main/main.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@
7272
#include "zend_ini.h"
7373
#include "zend_dtrace.h"
7474
#include "zend_observer.h"
75+
#include "zend_system_id.h"
7576

7677
#include "php_content_types.h"
7778
#include "php_ticks.h"
@@ -2202,6 +2203,9 @@ int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_mod
22022203
php_startup_sapi_content_types();
22032204
zend_observer_startup();
22042205

2206+
/* Begin to fingerprint the process state */
2207+
zend_startup_system_id();
2208+
22052209
/* startup extensions statically compiled in */
22062210
if (php_register_internal_extensions_func() == FAILURE) {
22072211
php_printf("Unable to start builtin modules\n");
@@ -2245,6 +2249,9 @@ int php_module_startup(sapi_module_struct *sf, zend_module_entry *additional_mod
22452249
module->info_func = PHP_MINFO(php_core);
22462250
}
22472251

2252+
/* Extensions that add engine hooks after this point do so at their own peril */
2253+
zend_finalize_system_id();
2254+
22482255
module_initialized = 1;
22492256

22502257
if (zend_post_startup() != SUCCESS) {

win32/build/config.w32

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,7 @@ ADD_SOURCES("Zend", "zend_language_parser.c zend_language_scanner.c \
237237
zend_object_handlers.c zend_objects_API.c \
238238
zend_default_classes.c zend_execute.c zend_strtod.c zend_gc.c zend_closures.c zend_weakrefs.c \
239239
zend_float.c zend_string.c zend_generators.c zend_virtual_cwd.c zend_ast.c \
240-
zend_inheritance.c zend_smart_str.c zend_cpuinfo.c zend_observer.c");
240+
zend_inheritance.c zend_smart_str.c zend_cpuinfo.c zend_observer.c zend_system_id.c");
241241

242242
ADD_FLAG("CFLAGS_BD_ZEND", "/D ZEND_ENABLE_STATIC_TSRMLS_CACHE=1");
243243
if (VS_TOOLSET && VCVERS >= 1914) {

0 commit comments

Comments
 (0)