xref: /PHP-8.4/Zend/zend_system_id.c (revision 6435bb5a)
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    | https://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    | license@php.net so we can mail you a copy immediately.               |
12    +----------------------------------------------------------------------+
13    | Authors: Sammy Kaye Powers <sammyk@php.net>                          |
14    |          Dmitry Stogov <dmitry@php.net>                              |
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 
zend_add_system_entropy(const char * module_name,const char * hook_name,const void * data,size_t size)29 ZEND_API zend_result 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 
zend_startup_system_id(void)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 #define ZEND_HOOK_INTERRUPT_FUNCTION (1 << 4)
63 
zend_finalize_system_id(void)64 void zend_finalize_system_id(void)
65 {
66 	unsigned char digest[16];
67 	uint8_t hooks = 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 	if (zend_interrupt_function) {
82 		hooks |= ZEND_HOOK_INTERRUPT_FUNCTION;
83 	}
84 	PHP_MD5Update(&context, &hooks, sizeof hooks);
85 
86 	for (int16_t i = 0; i < 256; i++) {
87 		if (zend_get_user_opcode_handler((uint8_t) i) != NULL) {
88 			PHP_MD5Update(&context, &i, sizeof i);
89 		}
90 	}
91 
92 	PHP_MD5Final(digest, &context);
93 	php_hash_bin2hex(zend_system_id, digest, sizeof digest);
94 	finalized = 1;
95 }
96