summaryrefslogtreecommitdiff
path: root/src/sp_utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/sp_utils.c')
-rw-r--r--src/sp_utils.c55
1 files changed, 32 insertions, 23 deletions
diff --git a/src/sp_utils.c b/src/sp_utils.c
index 087f431..56512df 100644
--- a/src/sp_utils.c
+++ b/src/sp_utils.c
@@ -201,7 +201,7 @@ bool sp_match_value(const char* value, const char* to_match, const pcre* rx) {
201 } 201 }
202 } else if (rx) { 202 } else if (rx) {
203 int substrvec[30]; 203 int substrvec[30];
204 int ret = pcre_exec(rx, NULL, value, strlen(value), 0, 0, substrvec, 30); 204 int ret = sp_pcre_exec(rx, NULL, value, strlen(value), 0, 0, substrvec, 30);
205 205
206 if (ret < 0) { 206 if (ret < 0) {
207 if (ret != PCRE_ERROR_NOMATCH) { 207 if (ret != PCRE_ERROR_NOMATCH) {
@@ -223,14 +223,14 @@ void sp_log_disable(const char* restrict path, const char* restrict arg_name,
223 const int sim = config_node->simulation; 223 const int sim = config_node->simulation;
224 if (arg_name) { 224 if (arg_name) {
225 if (alias) { 225 if (alias) {
226 sp_log_msg("disabled_function", sim?LOG_NOTICE:LOG_DROP, 226 sp_log_msg("disabled_function", sim?SP_LOG_NOTICE:SP_LOG_DROP,
227 "The call to the function '%s' in %s:%d has been disabled, " 227 "The call to the function '%s' in %s:%d has been disabled, "
228 "because its argument '%s' content (%s) matched the rule '%s'.", 228 "because its argument '%s' content (%s) matched the rule '%s'.",
229 path, zend_get_executed_filename(TSRMLS_C), 229 path, zend_get_executed_filename(TSRMLS_C),
230 zend_get_executed_lineno(TSRMLS_C), arg_name, arg_value?arg_value:"?", 230 zend_get_executed_lineno(TSRMLS_C), arg_name, arg_value?arg_value:"?",
231 alias); 231 alias);
232 } else { 232 } else {
233 sp_log_msg("disabled_function", sim?LOG_NOTICE:LOG_DROP, 233 sp_log_msg("disabled_function", sim?SP_LOG_NOTICE:SP_LOG_DROP,
234 "The call to the function '%s' in %s:%d has been disabled, " 234 "The call to the function '%s' in %s:%d has been disabled, "
235 "because its argument '%s' content (%s) matched a rule.", 235 "because its argument '%s' content (%s) matched a rule.",
236 path, zend_get_executed_filename(TSRMLS_C), 236 path, zend_get_executed_filename(TSRMLS_C),
@@ -239,13 +239,13 @@ void sp_log_disable(const char* restrict path, const char* restrict arg_name,
239 } 239 }
240 } else { 240 } else {
241 if (alias) { 241 if (alias) {
242 sp_log_msg("disabled_function", sim?LOG_NOTICE:LOG_DROP, 242 sp_log_msg("disabled_function", sim?SP_LOG_NOTICE:SP_LOG_DROP,
243 "The call to the function '%s' in %s:%d has been disabled, " 243 "The call to the function '%s' in %s:%d has been disabled, "
244 "because of the the rule '%s'.",path, 244 "because of the the rule '%s'.",path,
245 zend_get_executed_filename(TSRMLS_C), 245 zend_get_executed_filename(TSRMLS_C),
246 zend_get_executed_lineno(TSRMLS_C), alias); 246 zend_get_executed_lineno(TSRMLS_C), alias);
247 } else { 247 } else {
248 sp_log_msg("disabled_function", sim?LOG_NOTICE:LOG_DROP, 248 sp_log_msg("disabled_function", sim?SP_LOG_NOTICE:SP_LOG_DROP,
249 "The call to the function '%s' in %s:%d has been disabled.", 249 "The call to the function '%s' in %s:%d has been disabled.",
250 path, zend_get_executed_filename(TSRMLS_C), 250 path, zend_get_executed_filename(TSRMLS_C),
251 zend_get_executed_lineno(TSRMLS_C)); 251 zend_get_executed_lineno(TSRMLS_C));
@@ -263,13 +263,13 @@ void sp_log_disable_ret(const char* restrict path,
263 const char* alias = config_node->alias; 263 const char* alias = config_node->alias;
264 const int sim = config_node->simulation; 264 const int sim = config_node->simulation;
265 if (alias) { 265 if (alias) {
266 sp_log_msg("disabled_function", sim?LOG_NOTICE:LOG_DROP, 266 sp_log_msg("disabled_function", sim?SP_LOG_NOTICE:SP_LOG_DROP,
267 "The execution has been aborted in %s:%d, " 267 "The execution has been aborted in %s:%d, "
268 "because the function '%s' returned '%s', which matched the rule '%s'.", 268 "because the function '%s' returned '%s', which matched the rule '%s'.",
269 zend_get_executed_filename(TSRMLS_C), 269 zend_get_executed_filename(TSRMLS_C),
270 zend_get_executed_lineno(TSRMLS_C), path, ret_value?ret_value:"?", alias); 270 zend_get_executed_lineno(TSRMLS_C), path, ret_value?ret_value:"?", alias);
271 } else { 271 } else {
272 sp_log_msg("disabled_function", sim?LOG_NOTICE:LOG_DROP, 272 sp_log_msg("disabled_function", sim?SP_LOG_NOTICE:SP_LOG_DROP,
273 "The execution has been aborted in %s:%d, " 273 "The execution has been aborted in %s:%d, "
274 "because the return value (%s) of the function '%s' matched a rule.", 274 "because the return value (%s) of the function '%s' matched a rule.",
275 zend_get_executed_filename(TSRMLS_C), 275 zend_get_executed_filename(TSRMLS_C),
@@ -349,7 +349,7 @@ zend_always_inline char* sp_getenv(char* var) {
349 349
350zend_always_inline int is_regexp_matching(const pcre* regexp, const char* str) { 350zend_always_inline int is_regexp_matching(const pcre* regexp, const char* str) {
351 int vec[30]; 351 int vec[30];
352 int ret = pcre_exec(regexp, NULL, str, strlen(str), 0, 0, vec, sizeof(vec)); 352 int ret = sp_pcre_exec(regexp, NULL, str, strlen(str), 0, 0, vec, sizeof(vec));
353 if (ret < 0) { 353 if (ret < 0) {
354 if (ret != PCRE_ERROR_NOMATCH) { 354 if (ret != PCRE_ERROR_NOMATCH) {
355 sp_log_err("regexp", "Something went wrong with a regexp."); 355 sp_log_err("regexp", "Something went wrong with a regexp.");
@@ -367,22 +367,14 @@ int hook_function(const char* original_name, HashTable* hook_table,
367 367
368 /* The `mb` module likes to hook functions, like strlen->mb_strlen, 368 /* The `mb` module likes to hook functions, like strlen->mb_strlen,
369 * so we have to hook both of them. */ 369 * so we have to hook both of them. */
370 if (0 == strncmp(original_name, "mb_", 3)) { 370
371 CG(compiler_options) |= ZEND_COMPILE_NO_BUILTIN_STRLEN; 371 if ((func = zend_hash_str_find_ptr(ht,
372 if (zend_hash_str_find(ht, 372 VAR_AND_LEN(original_name)))) {
373 VAR_AND_LEN(original_name + 3))) { 373 if (func->handler == new_function) {
374 hook_function(original_name + 3, hook_table, new_function, hook_execution_table); 374 return SUCCESS;
375 }
376 } else { // TODO this can be moved somewhere else to gain some marginal perfs
377 CG(compiler_options) |= ZEND_COMPILE_NO_BUILTIN_STRLEN;
378 char* mb_name = pecalloc(strlen(original_name) + 3 + 1, 1, 0);
379 memcpy(mb_name, "mb_", 3);
380 memcpy(mb_name + 3, VAR_AND_LEN(original_name));
381 if (zend_hash_str_find(CG(function_table), VAR_AND_LEN(mb_name))) {
382 hook_function(mb_name, hook_table, new_function, hook_execution_table);
383 } 375 }
384 } 376 }
385 377
386 if ((func = zend_hash_str_find_ptr(CG(function_table), 378 if ((func = zend_hash_str_find_ptr(CG(function_table),
387 VAR_AND_LEN(original_name)))) { 379 VAR_AND_LEN(original_name)))) {
388 if (func->handler == new_function) { 380 if (func->handler == new_function) {
@@ -397,6 +389,23 @@ int hook_function(const char* original_name, HashTable* hook_table,
397 func->handler = new_function; 389 func->handler = new_function;
398 } 390 }
399 } 391 }
392
393 if (0 == strncmp(original_name, "mb_", 3)) {
394 CG(compiler_options) |= ZEND_COMPILE_NO_BUILTIN_STRLEN;
395 if (zend_hash_str_find(ht,
396 VAR_AND_LEN(original_name + 3))) {
397 hook_function(original_name + 3, hook_table, new_function, hook_execution_table);
398 }
399 } else { // TODO this can be moved somewhere else to gain some marginal perfs
400 CG(compiler_options) |= ZEND_COMPILE_NO_BUILTIN_STRLEN;
401 char* mb_name = pecalloc(strlen(original_name) + 3 + 1, 1, 0);
402 memcpy(mb_name, "mb_", 3);
403 memcpy(mb_name + 3, VAR_AND_LEN(original_name));
404 if (zend_hash_str_find(CG(function_table), VAR_AND_LEN(mb_name))) {
405 hook_function(mb_name, hook_table, new_function, hook_execution_table);
406 }
407 }
408
400 return SUCCESS; 409 return SUCCESS;
401} 410}
402 411
@@ -409,7 +418,7 @@ int hook_regexp(const pcre* regexp, HashTable* hook_table,
409 ZEND_HASH_FOREACH_STR_KEY(ht, key) { 418 ZEND_HASH_FOREACH_STR_KEY(ht, key) {
410 if (key) { 419 if (key) {
411 int vec[30]; 420 int vec[30];
412 int ret = pcre_exec(regexp, NULL, key->val, key->len, 0, 0, vec, 30); 421 int ret = sp_pcre_exec(regexp, NULL, key->val, key->len, 0, 0, vec, 30);
413 if (ret < 0) { /* Error or no match*/ 422 if (ret < 0) { /* Error or no match*/
414 if (PCRE_ERROR_NOMATCH != ret) { 423 if (PCRE_ERROR_NOMATCH != ret) {
415 sp_log_err("pcre", "Runtime error with pcre, error code: %d", ret); 424 sp_log_err("pcre", "Runtime error with pcre, error code: %d", ret);