/src/php-src/Zend/zend_system_id.c
Line | Count | Source |
1 | | /* |
2 | | +----------------------------------------------------------------------+ |
3 | | | Copyright © The PHP Group and Contributors. | |
4 | | +----------------------------------------------------------------------+ |
5 | | | This source file is subject to the Modified BSD License that is | |
6 | | | bundled with this package in the file LICENSE, and is available | |
7 | | | through the World Wide Web at <https://www.php.net/license/>. | |
8 | | | | |
9 | | | SPDX-License-Identifier: BSD-3-Clause | |
10 | | +----------------------------------------------------------------------+ |
11 | | | Authors: Sammy Kaye Powers <sammyk@php.net> | |
12 | | | Dmitry Stogov <dmitry@php.net> | |
13 | | +----------------------------------------------------------------------+ |
14 | | */ |
15 | | |
16 | | #include "php.h" |
17 | | #include "zend_system_id.h" |
18 | | #include "zend_extensions.h" |
19 | | #include "ext/standard/md5.h" |
20 | | |
21 | | ZEND_API char zend_system_id[32]; |
22 | | |
23 | | static PHP_MD5_CTX context; |
24 | | static int finalized = 0; |
25 | | |
26 | | ZEND_API zend_result zend_add_system_entropy(const char *module_name, const char *hook_name, const void *data, size_t size) |
27 | 20 | { |
28 | 20 | if (finalized == 0) { |
29 | 16 | PHP_MD5Update(&context, module_name, strlen(module_name)); |
30 | 16 | PHP_MD5Update(&context, hook_name, strlen(hook_name)); |
31 | 16 | if (size) { |
32 | 16 | PHP_MD5Update(&context, data, size); |
33 | 16 | } |
34 | 16 | return SUCCESS; |
35 | 16 | } |
36 | 4 | return FAILURE; |
37 | 20 | } |
38 | | |
39 | 32 | #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) |
40 | | |
41 | | void zend_startup_system_id(void) |
42 | 16 | { |
43 | 16 | finalized = 0; |
44 | 16 | PHP_MD5Init(&context); |
45 | 16 | PHP_MD5Update(&context, PHP_VERSION, sizeof(PHP_VERSION)-1); |
46 | 16 | PHP_MD5Update(&context, ZEND_EXTENSION_BUILD_ID, sizeof(ZEND_EXTENSION_BUILD_ID)-1); |
47 | 16 | PHP_MD5Update(&context, ZEND_BIN_ID, sizeof(ZEND_BIN_ID)-1); |
48 | 16 | if (strstr(PHP_VERSION, "-dev") != 0) { |
49 | | /* Development versions may be changed from build to build */ |
50 | 16 | PHP_MD5Update(&context, __DATE__, sizeof(__DATE__)-1); |
51 | 16 | PHP_MD5Update(&context, __TIME__, sizeof(__TIME__)-1); |
52 | 16 | } |
53 | 16 | zend_system_id[0] = '\0'; |
54 | 16 | } |
55 | | |
56 | 0 | #define ZEND_HOOK_AST_PROCESS (1 << 0) |
57 | 0 | #define ZEND_HOOK_COMPILE_FILE (1 << 1) |
58 | 0 | #define ZEND_HOOK_EXECUTE_EX (1 << 2) |
59 | 0 | #define ZEND_HOOK_EXECUTE_INTERNAL (1 << 3) |
60 | 0 | #define ZEND_HOOK_INTERRUPT_FUNCTION (1 << 4) |
61 | | |
62 | | void zend_finalize_system_id(void) |
63 | 16 | { |
64 | 16 | unsigned char digest[16]; |
65 | 16 | uint8_t hooks = 0; |
66 | | |
67 | 16 | if (zend_ast_process) { |
68 | 0 | hooks |= ZEND_HOOK_AST_PROCESS; |
69 | 0 | } |
70 | 16 | if (zend_compile_file != compile_file) { |
71 | 0 | hooks |= ZEND_HOOK_COMPILE_FILE; |
72 | 0 | } |
73 | 16 | if (zend_execute_ex != execute_ex) { |
74 | 0 | hooks |= ZEND_HOOK_EXECUTE_EX; |
75 | 0 | } |
76 | 16 | if (zend_execute_internal) { |
77 | 0 | hooks |= ZEND_HOOK_EXECUTE_INTERNAL; |
78 | 0 | } |
79 | 16 | if (zend_interrupt_function) { |
80 | 0 | hooks |= ZEND_HOOK_INTERRUPT_FUNCTION; |
81 | 0 | } |
82 | 16 | PHP_MD5Update(&context, &hooks, sizeof hooks); |
83 | | |
84 | 4.11k | for (int16_t i = 0; i < 256; i++) { |
85 | 4.09k | if (zend_get_user_opcode_handler((uint8_t) i) != NULL) { |
86 | 0 | PHP_MD5Update(&context, &i, sizeof i); |
87 | 0 | } |
88 | 4.09k | } |
89 | | |
90 | | /* Cached scripts can refer to offsets of internal functions. |
91 | | * Effects of disable_functions and frameless functions must be taken into account. |
92 | | * Therefore, embed the function names in-order. |
93 | | * Updating third party extensions may change function signatures, arginfo, offsets, ... |
94 | | * Therefore, also embed the loaded modules and their versions. */ |
95 | 22.1k | ZEND_HASH_MAP_FOREACH_STR_KEY(CG(function_table), zend_string *name) { |
96 | 22.1k | PHP_MD5Update(&context, ZSTR_VAL(name), ZSTR_LEN(name) + 1); |
97 | 22.1k | } ZEND_HASH_FOREACH_END(); |
98 | 448 | ZEND_HASH_MAP_FOREACH_PTR(&module_registry, zend_module_entry *mod) { |
99 | 448 | PHP_MD5Update(&context, mod->name, strlen(mod->name) + 1); |
100 | 448 | if (EXPECTED(mod->version != NO_VERSION_YET)) { |
101 | 208 | PHP_MD5Update(&context, mod->version, strlen(mod->version) + 1); |
102 | 208 | } |
103 | 448 | } ZEND_HASH_FOREACH_END(); |
104 | | |
105 | 16 | PHP_MD5Final(digest, &context); |
106 | 16 | zend_bin2hex(zend_system_id, digest, sizeof digest); |
107 | 16 | finalized = 1; |
108 | 16 | } |