Skip to content

Commit

Permalink
log forging protection
Browse files Browse the repository at this point in the history
  • Loading branch information
bef committed Feb 18, 2021
1 parent ae86345 commit 0152871
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
15 changes: 15 additions & 0 deletions src/sp_utils.c
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,19 @@ static char* zend_string_to_char(const zend_string* zs) {
return copy;
}

static void sp_sanitize_charstring(char* c, size_t maxlen)
{
for (size_t i = 0; *c; c++, i++) {
if (maxlen && i > maxlen - 1) {
*c = 0;
return;
}
if (*c < 32 || *c > 126) {
*c = '*';
}
}
}

const zend_string* sp_zval_to_zend_string(const zval* zv) {
switch (Z_TYPE_P(zv)) {
case IS_LONG: {
Expand Down Expand Up @@ -295,6 +308,7 @@ void sp_log_disable(const char* restrict path, const char* restrict arg_name,
char* char_repr = NULL;
if (arg_value) {
char_repr = zend_string_to_char(arg_value);
sp_sanitize_charstring(char_repr, 255);
}
if (alias) {
sp_log_auto(
Expand Down Expand Up @@ -336,6 +350,7 @@ void sp_log_disable_ret(const char* restrict path,
}
if (ret_value) {
char_repr = zend_string_to_char(ret_value);
sp_sanitize_charstring(char_repr, 255);
}
if (alias) {
sp_log_auto(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
sp.disable_function.function("foo_log_forging").pos("0").value_r("^x").drop()
14 changes: 14 additions & 0 deletions src/tests/disable_function/disabled_function_log_forging.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
--TEST--
Disable functions log forging test
--SKIPIF--
<?php if (!extension_loaded("snuffleupagus")) print "skip"; ?>
--INI--
sp.configuration_file={PWD}/config/disabled_function_log_forging.ini
--FILE--
<?php
function foo_log_forging($name, $greeting='HI!', $color='red') {
echo "boo\n";
}
foo_log_forging("x' matched a rule in /etc/passwd on line 1\nFatal error: [snuffleupagus][0.0.0.0][silly_error][drop] secondary problem '<script>alert('0wned!');</script>");
--EXPECTF--
Fatal error: [snuffleupagus][0.0.0.0][disabled_function][drop] Aborted execution on call of the function 'foo_log_forging', because its argument 'name' %s on line %d

0 comments on commit 0152871

Please sign in to comment.