summaryrefslogtreecommitdiff
path: root/src/sp_unserialize.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sp_unserialize.c')
-rw-r--r--src/sp_unserialize.c37
1 files changed, 26 insertions, 11 deletions
diff --git a/src/sp_unserialize.c b/src/sp_unserialize.c
index 64cf1b5..641d989 100644
--- a/src/sp_unserialize.c
+++ b/src/sp_unserialize.c
@@ -61,6 +61,10 @@ PHP_FUNCTION(sp_serialize) {
61 orig_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU); 61 orig_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
62 } 62 }
63 63
64 if (!SPCFG(unserialize).enable) {
65 return;
66 }
67
64 /* Compute the HMAC of the textual representation of the serialized data*/ 68 /* Compute the HMAC of the textual representation of the serialized data*/
65 zend_string *hmac = sp_do_hash_hmac_sha256(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value), ZSTR_VAL(SPCFG(encryption_key)), ZSTR_LEN(SPCFG(encryption_key))); 69 zend_string *hmac = sp_do_hash_hmac_sha256(Z_STRVAL_P(return_value), Z_STRLEN_P(return_value), ZSTR_VAL(SPCFG(encryption_key)), ZSTR_LEN(SPCFG(encryption_key)));
66 70
@@ -84,19 +88,28 @@ PHP_FUNCTION(sp_serialize) {
84} 88}
85 89
86PHP_FUNCTION(sp_unserialize) { 90PHP_FUNCTION(sp_unserialize) {
87 zif_handler orig_handler;
88
89 char *buf = NULL; 91 char *buf = NULL;
90 char *serialized_str = NULL;
91 char *hmac = NULL;
92 size_t buf_len = 0; 92 size_t buf_len = 0;
93 zval *opts = NULL; 93 HashTable *opts = NULL;
94 94
95 const sp_config_unserialize *config_unserialize = &(SPCFG(unserialize)); 95 ZEND_PARSE_PARAMETERS_START(1, 2)
96 Z_PARAM_STRING(buf, buf_len)
97 Z_PARAM_OPTIONAL
98 Z_PARAM_ARRAY_HT(opts)
99 ZEND_PARSE_PARAMETERS_END();
96 100
97 if (zend_parse_parameters(ZEND_NUM_ARGS(), "s|a", &buf, &buf_len, &opts) == 101 if (SPCFG(unserialize_noclass).enable) {
98 FAILURE) { 102#if PHP_VERSION_ID > 80000
99 RETURN_FALSE; 103 HashTable ht;
104 zend_hash_init(&ht, 1, NULL, NULL, 0);
105 zval zv;
106 ZVAL_FALSE(&zv);
107 zend_hash_str_add(&ht, "allowed_classes", sizeof("allowed_classes")-1, &zv);
108 php_unserialize_with_options(return_value, buf, buf_len, &ht, "unserialize");
109 return;
110#else
111 sp_log_drop("unserialize_noclass", "unserialize_noclass is only supported on PHP8+");
112#endif
100 } 113 }
101 114
102 /* 64 is the length of HMAC-256 */ 115 /* 64 is the length of HMAC-256 */
@@ -104,8 +117,8 @@ PHP_FUNCTION(sp_unserialize) {
104 sp_log_drop("unserialize", "The serialized object is too small."); 117 sp_log_drop("unserialize", "The serialized object is too small.");
105 } 118 }
106 119
107 hmac = buf + buf_len - 64; 120 char* hmac = buf + buf_len - 64;
108 serialized_str = ecalloc(buf_len - 64 + 1, 1); 121 char* serialized_str = ecalloc(buf_len - 64 + 1, 1);
109 memcpy(serialized_str, buf, buf_len - 64); 122 memcpy(serialized_str, buf, buf_len - 64);
110 123
111 zend_string *expected_hmac = sp_do_hash_hmac_sha256(serialized_str, strlen(serialized_str), ZSTR_VAL(SPCFG(encryption_key)), ZSTR_LEN(SPCFG(encryption_key))); 124 zend_string *expected_hmac = sp_do_hash_hmac_sha256(serialized_str, strlen(serialized_str), ZSTR_VAL(SPCFG(encryption_key)), ZSTR_LEN(SPCFG(encryption_key)));
@@ -117,11 +130,13 @@ PHP_FUNCTION(sp_unserialize) {
117 } 130 }
118 } else { status = 1; } 131 } else { status = 1; }
119 132
133 zif_handler orig_handler;
120 if (0 == status) { 134 if (0 == status) {
121 if ((orig_handler = zend_hash_str_find_ptr(SPG(sp_internal_functions_hook), ZEND_STRL("unserialize")))) { 135 if ((orig_handler = zend_hash_str_find_ptr(SPG(sp_internal_functions_hook), ZEND_STRL("unserialize")))) {
122 orig_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU); 136 orig_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
123 } 137 }
124 } else { 138 } else {
139 const sp_config_unserialize *config_unserialize = &(SPCFG(unserialize));
125 if (config_unserialize->dump) { 140 if (config_unserialize->dump) {
126 sp_log_request(config_unserialize->dump, 141 sp_log_request(config_unserialize->dump,
127 config_unserialize->textual_representation); 142 config_unserialize->textual_representation);