summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGiovanni2020-07-22 09:28:42 +0200
committerGitHub2020-07-22 09:28:42 +0200
commite8d3cd9b26f0b4d660e424f2657f11bbc01eb171 (patch)
treefa2b312815be8b50afae7fd0447fe891ab19a487 /src
parent5a655dda1c3b666adf552fd50f5ebf5f4cbd3ce7 (diff)
refactoring sp_log_* (#340)
Co-authored-by: Giovanni Dante Grazioli <giovanni.dantegrazioli@nbs-system.com>
Diffstat (limited to 'src')
-rw-r--r--src/sp_crypt.c22
-rw-r--r--src/sp_disabled_functions.c4
-rw-r--r--src/sp_execute.c12
-rw-r--r--src/sp_unserialize.c9
-rw-r--r--src/sp_upload_validation.c8
-rw-r--r--src/sp_utils.c30
-rw-r--r--src/sp_utils.h21
7 files changed, 55 insertions, 51 deletions
diff --git a/src/sp_crypt.c b/src/sp_crypt.c
index 42c1510..b353ebe 100644
--- a/src/sp_crypt.c
+++ b/src/sp_crypt.c
@@ -49,16 +49,16 @@ int decrypt_zval(zval *pDest, bool simulation, zend_hash_key *hash_key) {
49 49
50 if (ZSTR_LEN(debase64) < crypto_secretbox_NONCEBYTES) { 50 if (ZSTR_LEN(debase64) < crypto_secretbox_NONCEBYTES) {
51 if (true == simulation) { 51 if (true == simulation) {
52 sp_log_msg( 52 sp_log_simulation(
53 "cookie_encryption", SP_LOG_SIMULATION, 53 "cookie_encryption",
54 "Buffer underflow tentative detected in cookie encryption handling " 54 "Buffer underflow tentative detected in cookie encryption handling "
55 "for %s. Using the cookie 'as it' instead of decrypting it", 55 "for %s. Using the cookie 'as it' instead of decrypting it",
56 hash_key ? ZSTR_VAL(hash_key->key) : "the session"); 56 hash_key ? ZSTR_VAL(hash_key->key) : "the session");
57 return ZEND_HASH_APPLY_KEEP; 57 return ZEND_HASH_APPLY_KEEP;
58 } else { 58 } else {
59 // LCOV_EXCL_START 59 // LCOV_EXCL_START
60 sp_log_msg( 60 sp_log_drop(
61 "cookie_encryption", SP_LOG_DROP, 61 "cookie_encryption",
62 "Buffer underflow tentative detected in cookie encryption handling"); 62 "Buffer underflow tentative detected in cookie encryption handling");
63 return ZEND_HASH_APPLY_REMOVE; 63 return ZEND_HASH_APPLY_REMOVE;
64 // LCOV_EXCL_STOP 64 // LCOV_EXCL_STOP
@@ -69,15 +69,15 @@ int decrypt_zval(zval *pDest, bool simulation, zend_hash_key *hash_key) {
69 if (ZSTR_LEN(debase64) + (size_t)crypto_secretbox_ZEROBYTES < 69 if (ZSTR_LEN(debase64) + (size_t)crypto_secretbox_ZEROBYTES <
70 ZSTR_LEN(debase64)) { 70 ZSTR_LEN(debase64)) {
71 if (true == simulation) { 71 if (true == simulation) {
72 sp_log_msg( 72 sp_log_simulation(
73 "cookie_encryption", SP_LOG_SIMULATION, 73 "cookie_encryption",
74 "Integer overflow tentative detected in cookie encryption handling " 74 "Integer overflow tentative detected in cookie encryption handling "
75 "for %s. Using the cookie 'as it' instead of decrypting it.", 75 "for %s. Using the cookie 'as it' instead of decrypting it.",
76 hash_key ? ZSTR_VAL(hash_key->key) : "the session"); 76 hash_key ? ZSTR_VAL(hash_key->key) : "the session");
77 return ZEND_HASH_APPLY_KEEP; 77 return ZEND_HASH_APPLY_KEEP;
78 } else { 78 } else {
79 sp_log_msg( 79 sp_log_drop(
80 "cookie_encryption", SP_LOG_DROP, 80 "cookie_encryption",
81 "Integer overflow tentative detected in cookie encryption handling."); 81 "Integer overflow tentative detected in cookie encryption handling.");
82 return ZEND_HASH_APPLY_REMOVE; 82 return ZEND_HASH_APPLY_REMOVE;
83 } 83 }
@@ -98,8 +98,8 @@ int decrypt_zval(zval *pDest, bool simulation, zend_hash_key *hash_key) {
98 98
99 if (-1 == ret) { 99 if (-1 == ret) {
100 if (true == simulation) { 100 if (true == simulation) {
101 sp_log_msg( 101 sp_log_simulation(
102 "cookie_encryption", SP_LOG_SIMULATION, 102 "cookie_encryption",
103 "Something went wrong with the decryption of %s. Using the cookie " 103 "Something went wrong with the decryption of %s. Using the cookie "
104 "'as it' instead of decrypting it", 104 "'as it' instead of decrypting it",
105 hash_key ? ZSTR_VAL(hash_key->key) : "the session"); 105 hash_key ? ZSTR_VAL(hash_key->key) : "the session");
@@ -107,7 +107,7 @@ int decrypt_zval(zval *pDest, bool simulation, zend_hash_key *hash_key) {
107 efree(backup); 107 efree(backup);
108 return ZEND_HASH_APPLY_KEEP; 108 return ZEND_HASH_APPLY_KEEP;
109 } else { 109 } else {
110 sp_log_msg("cookie_encryption", SP_LOG_WARN, 110 sp_log_warn("cookie_encryption",
111 "Something went wrong with the decryption of %s", 111 "Something went wrong with the decryption of %s",
112 hash_key ? ZSTR_VAL(hash_key->key) : "the session"); 112 hash_key ? ZSTR_VAL(hash_key->key) : "the session");
113 efree(backup); 113 efree(backup);
diff --git a/src/sp_disabled_functions.c b/src/sp_disabled_functions.c
index f35f5ca..a7136df 100644
--- a/src/sp_disabled_functions.c
+++ b/src/sp_disabled_functions.c
@@ -574,11 +574,11 @@ ZEND_FUNCTION(eval_blacklist_callback) {
574 SP_TOKEN_EVAL_BLACKLIST); 574 SP_TOKEN_EVAL_BLACKLIST);
575 } 575 }
576 if (config_eval->simulation) { 576 if (config_eval->simulation) {
577 sp_log_msg("eval", SP_LOG_SIMULATION, 577 sp_log_simulation("eval",
578 "A call to %s was tried in eval, in %s:%d, logging it.", 578 "A call to %s was tried in eval, in %s:%d, logging it.",
579 current_function_name, ZSTR_VAL(filename), line_number); 579 current_function_name, ZSTR_VAL(filename), line_number);
580 } else { 580 } else {
581 sp_log_msg("eval", SP_LOG_DROP, 581 sp_log_drop("eval",
582 "A call to %s was tried in eval, in %s:%d, dropping it.", 582 "A call to %s was tried in eval, in %s:%d, dropping it.",
583 current_function_name, ZSTR_VAL(filename), line_number); 583 current_function_name, ZSTR_VAL(filename), line_number);
584 } 584 }
diff --git a/src/sp_execute.c b/src/sp_execute.c
index 4eae874..73cc560 100644
--- a/src/sp_execute.c
+++ b/src/sp_execute.c
@@ -18,10 +18,10 @@ ZEND_COLD static inline void terminate_if_writable(const char *filename) {
18 SP_TOKEN_READONLY_EXEC); 18 SP_TOKEN_READONLY_EXEC);
19 } 19 }
20 if (true == config_ro_exec->simulation) { 20 if (true == config_ro_exec->simulation) {
21 sp_log_msg("readonly_exec", SP_LOG_SIMULATION, 21 sp_log_simulation("readonly_exec",
22 "Attempted execution of a writable file (%s).", filename); 22 "Attempted execution of a writable file (%s).", filename);
23 } else { 23 } else {
24 sp_log_msg("readonly_exec", SP_LOG_DROP, 24 sp_log_drop("readonly_exec",
25 "Attempted execution of a writable file (%s).", filename); 25 "Attempted execution of a writable file (%s).", filename);
26 zend_bailout(); 26 zend_bailout();
27 } 27 }
@@ -79,14 +79,14 @@ is_in_eval_and_whitelisted(const zend_execute_data *execute_data) {
79 SP_TOKEN_EVAL_WHITELIST); 79 SP_TOKEN_EVAL_WHITELIST);
80 } 80 }
81 if (config_eval->simulation) { 81 if (config_eval->simulation) {
82 sp_log_msg( 82 sp_log_simulation(
83 "Eval_whitelist", SP_LOG_SIMULATION, 83 "Eval_whitelist",
84 "The function '%s' isn't in the eval whitelist, logging its call.", 84 "The function '%s' isn't in the eval whitelist, logging its call.",
85 ZSTR_VAL(current_function)); 85 ZSTR_VAL(current_function));
86 return; 86 return;
87 } else { 87 } else {
88 sp_log_msg( 88 sp_log_drop(
89 "Eval_whitelist", SP_LOG_DROP, 89 "Eval_whitelist",
90 "The function '%s' isn't in the eval whitelist, dropping its call.", 90 "The function '%s' isn't in the eval whitelist, dropping its call.",
91 ZSTR_VAL(current_function)); 91 ZSTR_VAL(current_function));
92 } 92 }
diff --git a/src/sp_unserialize.c b/src/sp_unserialize.c
index 29706c9..8977dd9 100644
--- a/src/sp_unserialize.c
+++ b/src/sp_unserialize.c
@@ -61,8 +61,7 @@ PHP_FUNCTION(sp_unserialize) {
61 61
62 /* 64 is the length of HMAC-256 */ 62 /* 64 is the length of HMAC-256 */
63 if (buf_len < 64) { 63 if (buf_len < 64) {
64 sp_log_msg("unserialize", SP_LOG_DROP, 64 sp_log_drop("unserialize", "The serialized object is too small.");
65 "The serialized object is too small.");
66 } 65 }
67 66
68 hmac = buf + buf_len - 64; 67 hmac = buf + buf_len - 64;
@@ -99,16 +98,14 @@ PHP_FUNCTION(sp_unserialize) {
99 SP_TOKEN_UNSERIALIZE_HMAC); 98 SP_TOKEN_UNSERIALIZE_HMAC);
100 } 99 }
101 if (true == config_unserialize->simulation) { 100 if (true == config_unserialize->simulation) {
102 sp_log_msg("unserialize", SP_LOG_SIMULATION, "Invalid HMAC for %s", 101 sp_log_simulation("unserialize", "Invalid HMAC for %s", serialized_str);
103 serialized_str);
104 if ((orig_handler = zend_hash_str_find_ptr( 102 if ((orig_handler = zend_hash_str_find_ptr(
105 SNUFFLEUPAGUS_G(sp_internal_functions_hook), "unserialize", 103 SNUFFLEUPAGUS_G(sp_internal_functions_hook), "unserialize",
106 sizeof("unserialize") - 1))) { 104 sizeof("unserialize") - 1))) {
107 orig_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU); 105 orig_handler(INTERNAL_FUNCTION_PARAM_PASSTHRU);
108 } 106 }
109 } else { 107 } else {
110 sp_log_msg("unserialize", SP_LOG_DROP, "Invalid HMAC for %s", 108 sp_log_drop("unserialize", "Invalid HMAC for %s", serialized_str);
111 serialized_str);
112 } 109 }
113 } 110 }
114 efree(serialized_str); 111 efree(serialized_str);
diff --git a/src/sp_upload_validation.c b/src/sp_upload_validation.c
index 54b0481..4ee7bd7 100644
--- a/src/sp_upload_validation.c
+++ b/src/sp_upload_validation.c
@@ -13,7 +13,7 @@ int sp_rfc1867_callback(unsigned int event, void *event_data, void **extra);
13 13
14int sp_rfc1867_callback_win(unsigned int event, void *event_data, 14int sp_rfc1867_callback_win(unsigned int event, void *event_data,
15 void **extra) { 15 void **extra) {
16 sp_log_msg("upload_validation", SP_LOG_SIMULATION, 16 sp_log_simulation("upload_validation",
17 "The upload validation doesn't work for now on Windows yet, " 17 "The upload validation doesn't work for now on Windows yet, "
18 "see https://github.com/jvoisin/snuffleupagus/issues/248 for " 18 "see https://github.com/jvoisin/snuffleupagus/issues/248 for "
19 "details."); 19 "details.");
@@ -90,9 +90,9 @@ int sp_rfc1867_callback(unsigned int event, void *event_data, void **extra) {
90 if (WEXITSTATUS(waitstatus) != 0) { // Nope 90 if (WEXITSTATUS(waitstatus) != 0) { // Nope
91 char *uri = getenv("REQUEST_URI"); 91 char *uri = getenv("REQUEST_URI");
92 int sim = config_upload->simulation; 92 int sim = config_upload->simulation;
93 sp_log_msg("upload_validation", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, 93 sp_log_auto("upload_validation", sim,
94 "The upload of %s on %s was rejected.", filename, 94 "The upload of %s on %s was rejected.",
95 uri ? uri : "?"); 95 filename, uri ? uri : "?");
96 } 96 }
97 } 97 }
98 ZEND_HASH_FOREACH_END(); 98 ZEND_HASH_FOREACH_END();
diff --git a/src/sp_utils.c b/src/sp_utils.c
index 146fe77..8032e0a 100644
--- a/src/sp_utils.c
+++ b/src/sp_utils.c
@@ -40,7 +40,7 @@ const char* get_ipaddr() {
40 return default_ipaddr; 40 return default_ipaddr;
41} 41}
42 42
43void sp_log_msg(char const* restrict feature, int type, 43void sp_log_msgf(char const* restrict feature, int level, int type,
44 const char* restrict fmt, ...) { 44 const char* restrict fmt, ...) {
45 char* msg; 45 char* msg;
46 va_list args; 46 va_list args;
@@ -51,16 +51,14 @@ void sp_log_msg(char const* restrict feature, int type,
51 51
52 const char* client_ip = get_ipaddr(); 52 const char* client_ip = get_ipaddr();
53 const char* logtype = NULL; 53 const char* logtype = NULL;
54 int bailout = type == SP_LOG_DROP;
55 switch(type) { 54 switch(type) {
56 case SP_LOG_SIMULATION: 55 case SP_TYPE_SIMULATION:
57 logtype = "simulation"; 56 logtype = "simulation";
58 type = E_WARNING;
59 break; 57 break;
60 case SP_LOG_DROP: 58 case SP_TYPE_DROP:
61 logtype = "drop"; 59 logtype = "drop";
62 type = E_ERROR;
63 break; 60 break;
61 case SP_TYPE_LOG:
64 default: 62 default:
65 logtype = "log"; 63 logtype = "log";
66 break; 64 break;
@@ -69,20 +67,20 @@ void sp_log_msg(char const* restrict feature, int type,
69 switch (SNUFFLEUPAGUS_G(config).log_media) { 67 switch (SNUFFLEUPAGUS_G(config).log_media) {
70 case SP_SYSLOG: { 68 case SP_SYSLOG: {
71 const char* error_filename = zend_get_executed_filename(); 69 const char* error_filename = zend_get_executed_filename();
72 int syslog_level = (type == E_ERROR) ? LOG_ERR : LOG_INFO; 70 int syslog_level = (level == E_ERROR) ? LOG_ERR : LOG_INFO;
73 int error_lineno = zend_get_executed_lineno(TSRMLS_C); 71 int error_lineno = zend_get_executed_lineno(TSRMLS_C);
74 openlog(PHP_SNUFFLEUPAGUS_EXTNAME, LOG_PID, LOG_AUTH); 72 openlog(PHP_SNUFFLEUPAGUS_EXTNAME, LOG_PID, LOG_AUTH);
75 syslog(syslog_level, "[snuffleupagus][%s][%s][%s] %s in %s on line %d", 73 syslog(syslog_level, "[snuffleupagus][%s][%s][%s] %s in %s on line %d",
76 client_ip, feature, logtype, msg, error_filename, error_lineno); 74 client_ip, feature, logtype, msg, error_filename, error_lineno);
77 closelog(); 75 closelog();
78 if (bailout) { 76 if (type == SP_TYPE_DROP) {
79 zend_bailout(); 77 zend_bailout();
80 } 78 }
81 break; 79 break;
82 } 80 }
83 case SP_ZEND: 81 case SP_ZEND:
84 default: 82 default:
85 zend_error(type, "[snuffleupagus][%s][%s][%s] %s", client_ip, feature, logtype, msg); 83 zend_error(level, "[snuffleupagus][%s][%s][%s] %s", client_ip, feature, logtype, msg);
86 break; 84 break;
87 } 85 }
88} 86}
@@ -282,12 +280,12 @@ void sp_log_disable(const char* restrict path, const char* restrict arg_name,
282 char_repr = zend_string_to_char(arg_value); 280 char_repr = zend_string_to_char(arg_value);
283 } 281 }
284 if (alias) { 282 if (alias) {
285 sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, 283 sp_log_auto("disabled_function", sim,
286 "Aborted execution on call of the function '%s', " 284 "Aborted execution on call of the function '%s', "
287 "because its argument '%s' content (%s) matched the rule '%s'", 285 "because its argument '%s' content (%s) matched the rule '%s'",
288 path, arg_name, char_repr ? char_repr : "?", ZSTR_VAL(alias)); 286 path, arg_name, char_repr ? char_repr : "?", ZSTR_VAL(alias));
289 } else { 287 } else {
290 sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, 288 sp_log_auto("disabled_function", sim,
291 "Aborted execution on call of the function '%s', " 289 "Aborted execution on call of the function '%s', "
292 "because its argument '%s' content (%s) matched a rule", 290 "because its argument '%s' content (%s) matched a rule",
293 path, arg_name, char_repr ? char_repr : "?"); 291 path, arg_name, char_repr ? char_repr : "?");
@@ -295,12 +293,12 @@ void sp_log_disable(const char* restrict path, const char* restrict arg_name,
295 efree(char_repr); 293 efree(char_repr);
296 } else { 294 } else {
297 if (alias) { 295 if (alias) {
298 sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, 296 sp_log_auto("disabled_function", sim,
299 "Aborted execution on call of the function '%s', " 297 "Aborted execution on call of the function '%s', "
300 "because of the the rule '%s'", 298 "because of the the rule '%s'",
301 path, ZSTR_VAL(alias)); 299 path, ZSTR_VAL(alias));
302 } else { 300 } else {
303 sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, 301 sp_log_auto("disabled_function", sim,
304 "Aborted execution on call of the function '%s'", path); 302 "Aborted execution on call of the function '%s'", path);
305 } 303 }
306 } 304 }
@@ -322,13 +320,13 @@ void sp_log_disable_ret(const char* restrict path,
322 char_repr = zend_string_to_char(ret_value); 320 char_repr = zend_string_to_char(ret_value);
323 } 321 }
324 if (alias) { 322 if (alias) {
325 sp_log_msg( 323 sp_log_auto(
326 "disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, 324 "disabled_function", sim,
327 "Aborted execution on return of the function '%s', " 325 "Aborted execution on return of the function '%s', "
328 "because the function returned '%s', which matched the rule '%s'", 326 "because the function returned '%s', which matched the rule '%s'",
329 path, char_repr ? char_repr : "?", ZSTR_VAL(alias)); 327 path, char_repr ? char_repr : "?", ZSTR_VAL(alias));
330 } else { 328 } else {
331 sp_log_msg("disabled_function", sim ? SP_LOG_SIMULATION : SP_LOG_DROP, 329 sp_log_auto("disabled_function", sim,
332 "Aborted execution on return of the function '%s', " 330 "Aborted execution on return of the function '%s', "
333 "because the function returned '%s', which matched a rule", 331 "because the function returned '%s', which matched a rule",
334 path, char_repr ? char_repr : "?"); 332 path, char_repr ? char_repr : "?");
diff --git a/src/sp_utils.h b/src/sp_utils.h
index 91a5a20..744bbff 100644
--- a/src/sp_utils.h
+++ b/src/sp_utils.h
@@ -28,16 +28,25 @@
28#define HOOK_FUNCTION_BY_REGEXP(regexp, hook_table, new_function) \ 28#define HOOK_FUNCTION_BY_REGEXP(regexp, hook_table, new_function) \
29 hook_regexp(regexp, SNUFFLEUPAGUS_G(hook_table), new_function) 29 hook_regexp(regexp, SNUFFLEUPAGUS_G(hook_table), new_function)
30 30
31#define SP_LOG_SIMULATION 0x100000 31#define SP_TYPE_LOG (0)
32#define SP_LOG_DROP 0x200000 32#define SP_TYPE_DROP (1)
33#define SP_TYPE_SIMULATION (2)
34
33#define SP_LOG_DEBUG E_NOTICE 35#define SP_LOG_DEBUG E_NOTICE
34#define SP_LOG_ERROR E_ERROR 36#define SP_LOG_ERROR E_ERROR
35#define SP_LOG_WARN E_WARNING 37#define SP_LOG_WARN E_WARNING
36 38
37#define sp_log_err(feature, ...) sp_log_msg(feature, SP_LOG_ERROR, __VA_ARGS__) 39#define sp_log_msg(feature, level, ...) sp_log_msgf(feature, level, SP_TYPE_LOG, __VA_ARGS__)
38#define sp_log_warn(feature, ...) sp_log_msg(feature, SP_LOG_WARN, __VA_ARGS__) 40#define sp_log_drop(feature, ...) sp_log_msgf(feature, SP_LOG_ERROR, SP_TYPE_DROP, __VA_ARGS__)
41#define sp_log_simulation(feature, ...) sp_log_msgf(feature, SP_LOG_WARN, SP_TYPE_SIMULATION, __VA_ARGS__)
42#define sp_log_auto(feature, is_simulation, ...) sp_log_msgf(feature, \
43 (is_simulation ? SP_LOG_WARN : SP_LOG_ERROR), \
44 (is_simulation ? SP_TYPE_SIMULATION : SP_TYPE_DROP), __VA_ARGS__)
45
46#define sp_log_err(feature, ...) sp_log_msgf(feature, SP_LOG_ERROR, SP_TYPE_LOG, __VA_ARGS__)
47#define sp_log_warn(feature, ...) sp_log_msgf(feature, SP_LOG_WARN, SP_TYPE_LOG, __VA_ARGS__)
39#ifdef SP_DEBUG 48#ifdef SP_DEBUG
40#define sp_log_debug(...) sp_log_msg("DEBUG", SP_LOG_DEBUG, __VA_ARGS__) 49#define sp_log_debug(...) sp_log_msgf("DEBUG", SP_LOG_DEBUG, SP_TYPE_LOG, __VA_ARGS__)
41#else 50#else
42#define sp_log_debug(...) 51#define sp_log_debug(...)
43#endif 52#endif
@@ -45,7 +54,7 @@
45#define GET_SUFFIX(x) (x == 1) ? "st" : ((x == 2) ? "nd" : "th") 54#define GET_SUFFIX(x) (x == 1) ? "st" : ((x == 2) ? "nd" : "th")
46 55
47const char *get_ipaddr(); 56const char *get_ipaddr();
48void sp_log_msg(char const *restrict feature, int type, 57void sp_log_msgf(char const *restrict feature, int level, int type,
49 const char *restrict fmt, ...); 58 const char *restrict fmt, ...);
50int compute_hash(const char *const restrict filename, char *restrict file_hash); 59int compute_hash(const char *const restrict filename, char *restrict file_hash);
51const zend_string *sp_zval_to_zend_string(const zval *); 60const zend_string *sp_zval_to_zend_string(const zval *);