Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitf98a101

Browse files
authored
Merge pull requestmicrosoft#4 from Microsoft/v2/dev/streamMemPreAlloc
Preallocate memory when SecStreamInBodyInspection is on
2 parentse94d85e +466bb73 commitf98a101

File tree

5 files changed

+57
-42
lines changed

5 files changed

+57
-42
lines changed

‎apache2/apache2_io.c‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -281,8 +281,9 @@ apr_status_t read_request_body(modsec_rec *msr, char **error_msg) {
281281
}
282282

283283
if (msr->txcfg->stream_inbody_inspection==1) {
284-
msr->stream_input_length+=buflen;
285-
modsecurity_request_body_to_stream(msr,buf,buflen,error_msg);
284+
if (modsecurity_request_body_to_stream(msr,buf,buflen,error_msg)<0) {
285+
return-1;
286+
}
286287
}
287288

288289
msr->reqbody_length+=buflen;

‎apache2/modsecurity.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -287,6 +287,7 @@ struct modsec_rec {
287287
unsignedintresbody_contains_html;
288288

289289
apr_size_tstream_input_length;
290+
apr_size_tstream_input_allocated_length;
290291
char*stream_input_data;
291292
apr_size_tstream_output_length;
292293
char*stream_output_data;

‎apache2/msc_reqbody.c‎

Lines changed: 48 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -428,55 +428,66 @@ apr_status_t modsecurity_request_body_store(modsec_rec *msr,
428428
}
429429

430430
apr_status_tmodsecurity_request_body_to_stream(modsec_rec*msr,constchar*buffer,intbuflen,char**error_msg) {
431-
char*stream_input_body=NULL;
432-
char*data=NULL;
433-
intfirst_pkt=0;
431+
apr_size_tallocate_length=0;
432+
char*allocated=NULL;
434433

435-
if(msr->stream_input_data==NULL) {
436-
msr->stream_input_data= (char*)calloc(sizeof(char),msr->stream_input_length+1);
437-
first_pkt=1;
438-
}
439-
else {
440-
441-
data= (char*)malloc(msr->stream_input_length+1-buflen);
434+
if (msr->stream_input_data==NULL) {
435+
// Is the request body length known beforehand? (requests that are not Transfer-Encoding: chunked)
436+
if (msr->request_content_length>0) {
437+
// Use min of Content-Length and SecRequestBodyLimit
438+
allocate_length=min(msr->request_content_length,msr->txcfg->reqbody_limit);
439+
}
440+
else {
441+
// We don't know how this request is going to be, so hope for just buflen to begin with (requests that are Transfer-Encoding: chunked)
442+
allocate_length=buflen;
443+
}
442444

443-
if(data==NULL)
445+
allocated= (char*)calloc(allocate_length,sizeof(char));
446+
if (allocated) {
447+
msr->stream_input_data=allocated;
448+
msr->stream_input_allocated_length=allocate_length;
449+
}
450+
else {
451+
*error_msg=apr_psprintf(
452+
msr->mp,
453+
"Unable to allocate memory to hold request body on stream. Asked for %"APR_SIZE_T_FMT" bytes.",
454+
allocate_length);
444455
return-1;
445-
446-
memset(data,0,msr->stream_input_length+1-buflen);
447-
memcpy(data,msr->stream_input_data,msr->stream_input_length-buflen);
448-
449-
stream_input_body= (char*)realloc(msr->stream_input_data,msr->stream_input_length+1);
450-
451-
msr->stream_input_data= (char*)stream_input_body;
452-
}
453-
454-
if (msr->stream_input_data==NULL) {
455-
if(data) {
456-
free(data);
457-
data=NULL;
458456
}
459-
*error_msg=apr_psprintf(msr->mp,"Unable to allocate memory to hold request body on stream. Asked for %"APR_SIZE_T_FMT" bytes.",
460-
msr->stream_input_length+1);
461-
return-1;
462457
}
458+
else {
459+
// Do we need to expand the space we have previously allocated?
460+
if ((msr->stream_input_length+buflen)>msr->stream_input_allocated_length) {
463461

464-
memset(msr->stream_input_data,0,msr->stream_input_length+1);
462+
// If this becomes a hotspot again, consider increasing by some percent extra each time, for fewer reallocs
463+
allocate_length=msr->stream_input_length+buflen;
465464

466-
if(first_pkt) {
467-
memcpy(msr->stream_input_data,buffer,msr->stream_input_length);
468-
}else {
469-
memcpy(msr->stream_input_data,data,msr->stream_input_length-buflen);
470-
memcpy(msr->stream_input_data+(msr->stream_input_length-buflen),buffer,buflen);
465+
allocated= (char*)realloc(msr->stream_input_data,allocate_length);
466+
if (allocated) {
467+
msr->stream_input_data=allocated;
468+
msr->stream_input_allocated_length=allocate_length;
469+
}
470+
else {
471+
*error_msg=apr_psprintf(
472+
msr->mp,
473+
"Unable to reallocate memory to hold request body on stream. Asked for %"APR_SIZE_T_FMT" bytes.",
474+
allocate_length);
475+
free(msr->stream_input_data);
476+
msr->stream_input_data=NULL;
477+
msr->stream_input_length=0;
478+
msr->stream_input_allocated_length=0;
479+
return-1;
480+
}
481+
}
471482
}
472483

473-
if(data) {
474-
free(data);
475-
data=NULL;
476-
}
484+
// Append buffer to msr->stream_input_data
485+
memcpy(msr->stream_input_data+msr->stream_input_length,buffer,buflen);
486+
msr->stream_input_length+=buflen;
477487

478488
return1;
479489
}
490+
480491
/**
481492
* Replace a bunch of chunks holding a request body with a single large chunk.
482493
*/

‎apache2/re_operators.c‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -634,15 +634,17 @@ static int msre_op_rsub_execute(modsec_rec *msr, msre_rule *rule, msre_var *var,
634634
free(msr->stream_input_data);
635635
msr->stream_input_data=NULL;
636636
msr->stream_input_length=0;
637+
msr->stream_input_allocated_length=0;
637638

638-
msr->stream_input_data= (char*)malloc(size+1);
639+
msr->stream_input_data= (char*)malloc(size);
639640

640641
if(msr->stream_input_data==NULL) {
641642
return-1;
642643
}
643644

644645
msr->stream_input_length=size;
645-
memset(msr->stream_input_data,0x0,size+1);
646+
msr->stream_input_allocated_length=size;
647+
memset(msr->stream_input_data,0x0,size);
646648

647649
msr->if_stream_changed=1;
648650

‎iis/dependencies/build_yajl.bat‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ copy /y "%WORK_DIR%\yajl\build\%YAJL_DIR%\lib\yajl_s.lib" "%OUTPUT_DIR%"
2828
@exit /B0
2929

3030
:file_not_found_bin
31-
@echo File not found:"%SOURCE_DIR%\%PCRE%"
31+
@echo File not found:"%SOURCE_DIR%\%YAJL%"
3232
@goto failed
3333

3434
:build_failed

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp