Skip to content

Commit c837f84

Browse files
committed
Make emit error generic to use string arguments before releasing them
1 parent c5b1852 commit c837f84

File tree

1 file changed

+37
-35
lines changed

1 file changed

+37
-35
lines changed

appsec/src/extension/request_abort.c

Lines changed: 37 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -375,19 +375,12 @@ void dd_request_abort_redirect(void)
375375
? ZSTR_VAL(_block_parameters->security_response_id)
376376
: "");
377377
} else {
378-
zend_string *security_response_id = NULL;
379-
if (_block_parameters->security_response_id) {
380-
security_response_id =
381-
zend_string_dup(_block_parameters->security_response_id, 0);
382-
}
383378
_emit_error("Datadog blocked the request and attempted a redirection "
384379
"to %s. No action required. Security Response ID: %s",
385380
ZSTR_VAL(_block_parameters->redirection_location),
386-
security_response_id ? ZSTR_VAL(security_response_id) : "");
387-
if (security_response_id) {
388-
zend_string_release(security_response_id);
389-
security_response_id = NULL;
390-
}
381+
_block_parameters->security_response_id
382+
? ZSTR_VAL(_block_parameters->security_response_id)
383+
: "");
391384
}
392385
}
393386

@@ -469,18 +462,11 @@ void _request_abort_static_page(int response_code, int type)
469462
? ZSTR_VAL(_block_parameters->security_response_id)
470463
: "");
471464
} else {
472-
zend_string *security_response_id = NULL;
473-
if (_block_parameters->security_response_id) {
474-
security_response_id =
475-
zend_string_dup(_block_parameters->security_response_id, 0);
476-
}
477465
_emit_error("Datadog blocked the request and presented a static error "
478466
"page. No action required. Security Response ID: %s",
479-
security_response_id ? ZSTR_VAL(security_response_id) : "");
480-
if (security_response_id) {
481-
zend_string_release(security_response_id);
482-
security_response_id = NULL;
483-
}
467+
_block_parameters->security_response_id
468+
? ZSTR_VAL(_block_parameters->security_response_id)
469+
: "");
484470
}
485471
}
486472

@@ -566,20 +552,13 @@ static bool _abort_prelude(void)
566552
? ZSTR_VAL(_block_parameters->security_response_id)
567553
: "");
568554
} else {
569-
zend_string *security_response_id = NULL;
570-
if (_block_parameters->security_response_id) {
571-
security_response_id =
572-
zend_string_dup(_block_parameters->security_response_id, 0);
573-
}
574555
_emit_error(
575556
"Datadog blocked the request, but the response has already "
576557
"been partially committed. No action required. Security "
577558
"Response ID: %s",
578-
security_response_id ? ZSTR_VAL(security_response_id) : "");
579-
if (security_response_id) {
580-
zend_string_release(security_response_id);
581-
security_response_id = NULL;
582-
}
559+
_block_parameters->security_response_id
560+
? ZSTR_VAL(_block_parameters->security_response_id)
561+
: "");
583562
}
584563
return false;
585564
}
@@ -621,6 +600,21 @@ static void _emit_error(const char *format, ...)
621600

622601
va_list args;
623602
va_start(args, format);
603+
char buf[0x100];
604+
va_list args2;
605+
va_copy(args2, args);
606+
int len = vsnprintf(buf, sizeof(buf), format, args);
607+
char *msg = NULL;
608+
bool free_msg = false;
609+
if (len > (int)sizeof(buf)) {
610+
msg = emalloc(len + 1);
611+
len = vsnprintf(msg, len + 1, format, args2);
612+
free_msg = true;
613+
} else {
614+
msg = buf;
615+
}
616+
va_end(args2);
617+
624618
if (PG(during_request_startup)) {
625619
/* if emitting error during startup, RSHUTDOWN will not run (except fpm)
626620
* so we need to run the same logic from here */
@@ -637,15 +631,18 @@ static void _emit_error(const char *format, ...)
637631
/* fpm children exit if we throw an error at this point. So emit
638632
* only warning and use other means to prevent the script from
639633
* executing */
640-
php_verror(NULL, "", E_WARNING, format, args);
634+
php_verror(NULL, "", E_WARNING, msg, args);
635+
if (free_msg) {
636+
efree(msg);
637+
}
641638
va_end(args);
642639
// fpm doesn't try to run the script if it sees this null
643640
SG(request_info).request_method = NULL;
644641
return;
645642
}
646643
#ifdef FRANKENPHP_SUPPORT
647644
if (strcmp(sapi_module.name, "frankenphp") == 0) {
648-
php_verror(NULL, "", E_WARNING, format, args);
645+
php_verror(NULL, "", E_WARNING, msg, args);
649646
va_end(args);
650647
_prepare_req_init_block();
651648
return;
@@ -676,14 +673,19 @@ static void _emit_error(const char *format, ...)
676673
{
677674
va_list args2;
678675
va_copy(args2, args);
679-
php_verror(NULL, "", E_COMPILE_WARNING, format, args2);
676+
php_verror(NULL, "", E_COMPILE_WARNING, msg, args2);
677+
if (free_msg) {
678+
efree(msg);
679+
}
680680
va_end(args2);
681681
}
682682

683683
// not enough: EG(error_handling) = EH_SUPPRESS;
684684
_suppress_error_reporting();
685-
php_verror(NULL, "", E_ERROR, format, args);
686-
685+
php_verror(NULL, "", E_ERROR, msg, args);
686+
if (free_msg) {
687+
efree(msg);
688+
}
687689
va_end(args);
688690
__builtin_unreachable();
689691
}

0 commit comments

Comments
 (0)