30
30
APLOG_USE_MODULE (security2 );
31
31
#endif
32
32
33
+ // Returns the rule id if existing, otherwise the file name & line number
34
+ const char * id_log (msre_rule * rule ) {
35
+ assert (rule != NULL );
36
+ assert (rule -> actionset != NULL );
37
+ const char * id = rule -> actionset -> id ;
38
+ if (!id || !* id || id == NOT_SET_P ) id = apr_psprintf (rule -> ruleset -> mp , "%s (%d)" , rule -> filename , rule -> line_num );
39
+ return id ;
40
+ }
41
+
33
42
/* -- Directory context creation and initialisation -- */
34
43
35
44
/**
@@ -239,19 +248,19 @@ static void copy_rules_phase(apr_pool_t *mp,
239
248
240
249
if (copy > 0 ) {
241
250
#ifdef DEBUG_CONF
242
- ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , mp , "Copy rule %pp [id \"%s\"]" , rule , rule -> actionset -> id );
251
+ ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , mp , "Copy rule %pp [id \"%s\"]" , rule , id_log ( rule ) );
243
252
#endif
244
253
245
254
/* Copy the rule. */
246
255
* (msre_rule * * )apr_array_push (child_phase_arr ) = rule ;
247
- if (rule -> actionset && rule -> actionset -> is_chained ) mode = 2 ;
256
+ if (rule -> actionset -> is_chained ) mode = 2 ;
248
257
} else {
249
- if (rule -> actionset && rule -> actionset -> is_chained ) mode = 1 ;
258
+ if (rule -> actionset -> is_chained ) mode = 1 ;
250
259
}
251
260
} else {
252
261
if (mode == 2 ) {
253
262
#ifdef DEBUG_CONF
254
- ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , mp , "Copy chain %pp for rule %pp [id \"%s\"]" , rule , rule -> chain_starter , rule -> chain_starter -> actionset -> id );
263
+ ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , mp , "Copy chain %pp for rule %pp [id \"%s\"]" , rule , rule -> chain_starter , id_log ( rule -> chain_starter ) );
255
264
#endif
256
265
257
266
/* Copy the rule (it belongs to the chain we want to include. */
@@ -906,16 +915,14 @@ static const char *add_rule(cmd_parms *cmd, directory_config *dcfg, int type,
906
915
*/
907
916
rule -> actionset = msre_actionset_merge (modsecurity -> msre , cmd -> pool , dcfg -> tmp_default_actionset ,
908
917
rule -> actionset , 1 );
918
+ if (rule -> actionset == NULL ) return apr_psprintf (cmd -> pool , "ModSecurity: cannot merge actionset (memory full?)." );
909
919
910
920
/* Keep track of the parent action for "block" */
911
- if (rule -> actionset ) {
912
- rule -> actionset -> parent_intercept_action_rec = dcfg -> tmp_default_actionset -> intercept_action_rec ;
913
- rule -> actionset -> parent_intercept_action = dcfg -> tmp_default_actionset -> intercept_action ;
914
- }
921
+ rule -> actionset -> parent_intercept_action_rec = dcfg -> tmp_default_actionset -> intercept_action_rec ;
922
+ rule -> actionset -> parent_intercept_action = dcfg -> tmp_default_actionset -> intercept_action ;
915
923
916
924
/* Must NOT specify a disruptive action in logging phase. */
917
- if ((rule -> actionset != NULL )
918
- && (rule -> actionset -> phase == PHASE_LOGGING )
925
+ if ( (rule -> actionset -> phase == PHASE_LOGGING )
919
926
&& (rule -> actionset -> intercept_action != ACTION_ALLOW )
920
927
&& (rule -> actionset -> intercept_action != ACTION_ALLOW_REQUEST )
921
928
&& (rule -> actionset -> intercept_action != ACTION_NONE )
@@ -926,9 +933,7 @@ static const char *add_rule(cmd_parms *cmd, directory_config *dcfg, int type,
926
933
927
934
if (dcfg -> tmp_chain_starter != NULL ) {
928
935
rule -> chain_starter = dcfg -> tmp_chain_starter ;
929
- if (rule -> actionset ) {
930
- rule -> actionset -> phase = rule -> chain_starter -> actionset -> phase ;
931
- }
936
+ rule -> actionset -> phase = rule -> chain_starter -> actionset -> phase ;
932
937
}
933
938
934
939
if (rule -> actionset -> is_chained != 1 ) {
@@ -967,8 +972,7 @@ static const char *add_rule(cmd_parms *cmd, directory_config *dcfg, int type,
967
972
968
973
#ifdef DEBUG_CONF
969
974
ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , cmd -> pool ,
970
- "Adding rule %pp phase=%d id=\"%s\"." , rule , rule -> actionset -> phase , (rule -> actionset -> id == NOT_SET_P
971
- ? "(none)" : rule -> actionset -> id ));
975
+ "Adding rule %pp phase=%d id=\"%s\"." , rule , rule -> actionset -> phase , id_log (rule ));
972
976
#endif
973
977
974
978
/* Add rule to the recipe. */
@@ -1042,8 +1046,7 @@ static const char *add_marker(cmd_parms *cmd, directory_config *dcfg,
1042
1046
for (p = PHASE_FIRST ; p <= PHASE_LAST ; p ++ ) {
1043
1047
#ifdef DEBUG_CONF
1044
1048
ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , cmd -> pool ,
1045
- "Adding marker %pp phase=%d id=\"%s\"." , rule , p , (rule -> actionset -> id == NOT_SET_P
1046
- ? "(none)" : rule -> actionset -> id ));
1049
+ "Adding marker %pp phase=%d id=\"%s\"." , rule , p , id_log (rule ));
1047
1050
#endif
1048
1051
1049
1052
if (msre_ruleset_rule_add (dcfg -> ruleset , rule , p ) < 0 ) {
@@ -1091,11 +1094,7 @@ static const char *update_rule_action(cmd_parms *cmd, directory_config *dcfg,
1091
1094
return NULL ;
1092
1095
}
1093
1096
1094
- /* Check the rule actionset */
1095
- /* ENH: Can this happen? */
1096
- if (rule -> actionset == NULL ) {
1097
- return apr_psprintf (cmd -> pool , "ModSecurity: Attempt to update action for rule \"%s\" failed: Rule does not have an actionset." , p1 );
1098
- }
1097
+ assert (rule -> actionset != NULL );
1099
1098
1100
1099
/* Create a new actionset */
1101
1100
new_actionset = msre_actionset_create (modsecurity -> msre , cmd -> pool , p2 , & my_error_msg );
@@ -1117,16 +1116,15 @@ static const char *update_rule_action(cmd_parms *cmd, directory_config *dcfg,
1117
1116
char * actions = msre_actionset_generate_action_string (ruleset -> mp , rule -> actionset );
1118
1117
ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , cmd -> pool ,
1119
1118
"Update rule %pp id=\"%s\" old action: \"%s\"" ,
1120
- rule ,
1121
- (rule -> actionset -> id == NOT_SET_P ? "(none)" : rule -> actionset -> id ),
1122
- actions );
1119
+ rule , id_log (rule ), actions );
1123
1120
}
1124
1121
#endif
1125
1122
1126
1123
/* Merge new actions with the rule */
1127
1124
/* ENH: Will this leak the old actionset? */
1128
1125
rule -> actionset = msre_actionset_merge (modsecurity -> msre , cmd -> pool , rule -> actionset ,
1129
1126
new_actionset , 1 );
1127
+ if (rule -> actionset == NULL ) return apr_psprintf (cmd -> pool , "ModSecurity: cannot merge actionset (memory full?)." );
1130
1128
msre_actionset_set_defaults (rule -> actionset );
1131
1129
1132
1130
/* Update the unparsed rule */
@@ -1137,9 +1135,7 @@ static const char *update_rule_action(cmd_parms *cmd, directory_config *dcfg,
1137
1135
char * actions = msre_actionset_generate_action_string (ruleset -> mp , rule -> actionset );
1138
1136
ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , cmd -> pool ,
1139
1137
"Update rule %pp id=\"%s\" new action: \"%s\"" ,
1140
- rule ,
1141
- (rule -> actionset -> id == NOT_SET_P ? "(none)" : rule -> actionset -> id ),
1142
- actions );
1138
+ rule , id_log (rule ), actions );
1143
1139
}
1144
1140
#endif
1145
1141
@@ -1746,6 +1742,9 @@ char *parser_conn_limits_operator(apr_pool_t *mp, const char *p2,
1746
1742
1747
1743
config_orig_path = apr_pstrndup (mp , filename ,
1748
1744
strlen (filename ) - strlen (apr_filepath_name_get (filename )));
1745
+ if (config_orig_path == NULL ) {
1746
+ return apr_psprintf (mp , "ModSecurity: failed to duplicate filename in parser_conn_limits_operator" );
1747
+ }
1749
1748
1750
1749
apr_filepath_merge (& file , config_orig_path , param , APR_FILEPATH_TRUENAME ,
1751
1750
mp );
@@ -2452,8 +2451,12 @@ static const char *cmd_rule_remove_by_id(cmd_parms *cmd, void *_dcfg,
2452
2451
const char * p1 )
2453
2452
{
2454
2453
directory_config * dcfg = (directory_config * )_dcfg ;
2455
- rule_exception * re = apr_pcalloc (cmd -> pool , sizeof (rule_exception ));
2456
2454
if (dcfg == NULL ) return NULL ;
2455
+ rule_exception * re = apr_pcalloc (cmd -> pool , sizeof (rule_exception ));
2456
+ if (re == NULL ) {
2457
+ ap_log_perror (APLOG_MARK , APLOG_STARTUP | APLOG_NOERRNO , 0 , cmd -> pool , "cmd_rule_remove_by_id: Cannot allocate memory" );
2458
+ return NULL ;
2459
+ }
2457
2460
2458
2461
re -> type = RULE_EXCEPTION_REMOVE_ID ;
2459
2462
re -> param = p1 ;
0 commit comments