3030 APLOG_USE_MODULE (security2 );
3131#endif
3232
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+
3342/* -- Directory context creation and initialisation -- */
3443
3544/**
@@ -239,19 +248,19 @@ static void copy_rules_phase(apr_pool_t *mp,
239248
240249 if (copy > 0 ) {
241250#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 ) );
243252#endif
244253
245254 /* Copy the rule. */
246255 * (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 ;
248257 } else {
249- if (rule -> actionset && rule -> actionset -> is_chained ) mode = 1 ;
258+ if (rule -> actionset -> is_chained ) mode = 1 ;
250259 }
251260 } else {
252261 if (mode == 2 ) {
253262#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 ) );
255264#endif
256265
257266 /* 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,
906915 */
907916 rule -> actionset = msre_actionset_merge (modsecurity -> msre , cmd -> pool , dcfg -> tmp_default_actionset ,
908917 rule -> actionset , 1 );
918+ if (rule -> actionset == NULL ) return apr_psprintf (cmd -> pool , "ModSecurity: cannot merge actionset (memory full?)." );
909919
910920 /* 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 ;
915923
916924 /* 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 )
919926 && (rule -> actionset -> intercept_action != ACTION_ALLOW )
920927 && (rule -> actionset -> intercept_action != ACTION_ALLOW_REQUEST )
921928 && (rule -> actionset -> intercept_action != ACTION_NONE )
@@ -926,9 +933,7 @@ static const char *add_rule(cmd_parms *cmd, directory_config *dcfg, int type,
926933
927934 if (dcfg -> tmp_chain_starter != NULL ) {
928935 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 ;
932937 }
933938
934939 if (rule -> actionset -> is_chained != 1 ) {
@@ -967,8 +972,7 @@ static const char *add_rule(cmd_parms *cmd, directory_config *dcfg, int type,
967972
968973 #ifdef DEBUG_CONF
969974 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 ));
972976 #endif
973977
974978 /* Add rule to the recipe. */
@@ -1042,8 +1046,7 @@ static const char *add_marker(cmd_parms *cmd, directory_config *dcfg,
10421046 for (p = PHASE_FIRST ; p <= PHASE_LAST ; p ++ ) {
10431047 #ifdef DEBUG_CONF
10441048 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 ));
10471050 #endif
10481051
10491052 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,
10911094 return NULL ;
10921095 }
10931096
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 );
10991098
11001099 /* Create a new actionset */
11011100 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,
11171116 char * actions = msre_actionset_generate_action_string (ruleset -> mp , rule -> actionset );
11181117 ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , cmd -> pool ,
11191118 "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 );
11231120 }
11241121 #endif
11251122
11261123 /* Merge new actions with the rule */
11271124 /* ENH: Will this leak the old actionset? */
11281125 rule -> actionset = msre_actionset_merge (modsecurity -> msre , cmd -> pool , rule -> actionset ,
11291126 new_actionset , 1 );
1127+ if (rule -> actionset == NULL ) return apr_psprintf (cmd -> pool , "ModSecurity: cannot merge actionset (memory full?)." );
11301128 msre_actionset_set_defaults (rule -> actionset );
11311129
11321130 /* Update the unparsed rule */
@@ -1137,9 +1135,7 @@ static const char *update_rule_action(cmd_parms *cmd, directory_config *dcfg,
11371135 char * actions = msre_actionset_generate_action_string (ruleset -> mp , rule -> actionset );
11381136 ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , cmd -> pool ,
11391137 "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 );
11431139 }
11441140 #endif
11451141
@@ -1746,6 +1742,9 @@ char *parser_conn_limits_operator(apr_pool_t *mp, const char *p2,
17461742
17471743 config_orig_path = apr_pstrndup (mp , filename ,
17481744 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+ }
17491748
17501749 apr_filepath_merge (& file , config_orig_path , param , APR_FILEPATH_TRUENAME ,
17511750 mp );
@@ -2452,8 +2451,12 @@ static const char *cmd_rule_remove_by_id(cmd_parms *cmd, void *_dcfg,
24522451 const char * p1 )
24532452{
24542453 directory_config * dcfg = (directory_config * )_dcfg ;
2455- rule_exception * re = apr_pcalloc (cmd -> pool , sizeof (rule_exception ));
24562454 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+ }
24572460
24582461 re -> type = RULE_EXCEPTION_REMOVE_ID ;
24592462 re -> param = p1 ;
0 commit comments