3030 APLOG_USE_MODULE (security2 );
3131#endif
3232
33+ // Returns the rule id if existing, otherwise the file name & line number
34+ static const char * id_log (msre_rule * rule ) {
35+ const char * id = rule -> actionset -> id ;
36+ if (!id || !* id || id == NOT_SET_P ) id = apr_psprintf (rule -> ruleset -> mp , "%s (%d)" , rule -> filename , rule -> line_num );
37+ return id ;
38+ }
39+
3340/* -- Directory context creation and initialisation -- */
3441
3542/**
@@ -239,19 +246,19 @@ static void copy_rules_phase(apr_pool_t *mp,
239246
240247 if (copy > 0 ) {
241248#ifdef DEBUG_CONF
242- ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , mp , "Copy rule %pp [id \"%s\"]" , rule , rule -> actionset -> id );
249+ ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , mp , "Copy rule %pp [id \"%s\"]" , rule , id_log ( rule ) );
243250#endif
244251
245252 /* Copy the rule. */
246253 * (msre_rule * * )apr_array_push (child_phase_arr ) = rule ;
247- if (rule -> actionset && rule -> actionset -> is_chained ) mode = 2 ;
254+ if (rule -> actionset -> is_chained ) mode = 2 ;
248255 } else {
249- if (rule -> actionset && rule -> actionset -> is_chained ) mode = 1 ;
256+ if (rule -> actionset -> is_chained ) mode = 1 ;
250257 }
251258 } else {
252259 if (mode == 2 ) {
253260#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 );
261+ 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 ) );
255262#endif
256263
257264 /* Copy the rule (it belongs to the chain we want to include. */
@@ -906,9 +913,7 @@ static const char *add_rule(cmd_parms *cmd, directory_config *dcfg, int type,
906913 */
907914 rule -> actionset = msre_actionset_merge (modsecurity -> msre , cmd -> pool , dcfg -> tmp_default_actionset ,
908915 rule -> actionset , 1 );
909- if (rule -> actionset == NULL ) {
910- return apr_psprintf (cmd -> pool , "ModSecurity: cannot merge actionset (memory full?)." );
911- }
916+ if (rule -> actionset == NULL ) return apr_psprintf (cmd -> pool , "ModSecurity: cannot merge actionset (memory full?)." );
912917
913918 /* Keep track of the parent action for "block" */
914919 rule -> actionset -> parent_intercept_action_rec = dcfg -> tmp_default_actionset -> intercept_action_rec ;
@@ -965,8 +970,7 @@ static const char *add_rule(cmd_parms *cmd, directory_config *dcfg, int type,
965970
966971 #ifdef DEBUG_CONF
967972 ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , cmd -> pool ,
968- "Adding rule %pp phase=%d id=\"%s\"." , rule , rule -> actionset -> phase , (rule -> actionset -> id == NOT_SET_P
969- ? "(none)" : rule -> actionset -> id ));
973+ "Adding rule %pp phase=%d id=\"%s\"." , rule , rule -> actionset -> phase , id_log (rule ));
970974 #endif
971975
972976 /* Add rule to the recipe. */
@@ -1040,8 +1044,7 @@ static const char *add_marker(cmd_parms *cmd, directory_config *dcfg,
10401044 for (p = PHASE_FIRST ; p <= PHASE_LAST ; p ++ ) {
10411045 #ifdef DEBUG_CONF
10421046 ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , cmd -> pool ,
1043- "Adding marker %pp phase=%d id=\"%s\"." , rule , p , (rule -> actionset -> id == NOT_SET_P
1044- ? "(none)" : rule -> actionset -> id ));
1047+ "Adding marker %pp phase=%d id=\"%s\"." , rule , p , id_log (rule ));
10451048 #endif
10461049
10471050 if (msre_ruleset_rule_add (dcfg -> ruleset , rule , p ) < 0 ) {
@@ -1089,11 +1092,7 @@ static const char *update_rule_action(cmd_parms *cmd, directory_config *dcfg,
10891092 return NULL ;
10901093 }
10911094
1092- /* Check the rule actionset */
1093- /* ENH: Can this happen? */
1094- if (rule -> actionset == NULL ) {
1095- return apr_psprintf (cmd -> pool , "ModSecurity: Attempt to update action for rule \"%s\" failed: Rule does not have an actionset." , p1 );
1096- }
1095+ assert (rule -> actionset != NULL );
10971096
10981097 /* Create a new actionset */
10991098 new_actionset = msre_actionset_create (modsecurity -> msre , cmd -> pool , p2 , & my_error_msg );
@@ -1115,16 +1114,15 @@ static const char *update_rule_action(cmd_parms *cmd, directory_config *dcfg,
11151114 char * actions = msre_actionset_generate_action_string (ruleset -> mp , rule -> actionset );
11161115 ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , cmd -> pool ,
11171116 "Update rule %pp id=\"%s\" old action: \"%s\"" ,
1118- rule ,
1119- (rule -> actionset -> id == NOT_SET_P ? "(none)" : rule -> actionset -> id ),
1120- actions );
1117+ rule , id_log (rule ), actions );
11211118 }
11221119 #endif
11231120
11241121 /* Merge new actions with the rule */
11251122 /* ENH: Will this leak the old actionset? */
11261123 rule -> actionset = msre_actionset_merge (modsecurity -> msre , cmd -> pool , rule -> actionset ,
11271124 new_actionset , 1 );
1125+ if (rule -> actionset == NULL ) return apr_psprintf (cmd -> pool , "ModSecurity: cannot merge actionset (memory full?)." );
11281126 msre_actionset_set_defaults (rule -> actionset );
11291127
11301128 /* Update the unparsed rule */
@@ -1135,9 +1133,7 @@ static const char *update_rule_action(cmd_parms *cmd, directory_config *dcfg,
11351133 char * actions = msre_actionset_generate_action_string (ruleset -> mp , rule -> actionset );
11361134 ap_log_perror (APLOG_MARK , APLOG_STARTUP |APLOG_NOERRNO , 0 , cmd -> pool ,
11371135 "Update rule %pp id=\"%s\" new action: \"%s\"" ,
1138- rule ,
1139- (rule -> actionset -> id == NOT_SET_P ? "(none)" : rule -> actionset -> id ),
1140- actions );
1136+ rule , id_log (rule ), actions );
11411137 }
11421138 #endif
11431139
@@ -1744,6 +1740,9 @@ char *parser_conn_limits_operator(apr_pool_t *mp, const char *p2,
17441740
17451741 config_orig_path = apr_pstrndup (mp , filename ,
17461742 strlen (filename ) - strlen (apr_filepath_name_get (filename )));
1743+ if (config_orig_path == NULL ) {
1744+ return apr_psprintf (mp , "ModSecurity: failed to duplicate filename in parser_conn_limits_operator" );
1745+ }
17471746
17481747 apr_filepath_merge (& file , config_orig_path , param , APR_FILEPATH_TRUENAME ,
17491748 mp );
@@ -2450,8 +2449,12 @@ static const char *cmd_rule_remove_by_id(cmd_parms *cmd, void *_dcfg,
24502449 const char * p1 )
24512450{
24522451 directory_config * dcfg = (directory_config * )_dcfg ;
2453- rule_exception * re = apr_pcalloc (cmd -> pool , sizeof (rule_exception ));
24542452 if (dcfg == NULL ) return NULL ;
2453+ rule_exception * re = apr_pcalloc (cmd -> pool , sizeof (rule_exception ));
2454+ if (re == NULL ) {
2455+ ap_log_perror (APLOG_MARK , APLOG_STARTUP | APLOG_NOERRNO , 0 , cmd -> pool , "cmd_rule_remove_by_id: Cannot allocate memory" );
2456+ return NULL ;
2457+ }
24552458
24562459 re -> type = RULE_EXCEPTION_REMOVE_ID ;
24572460 re -> param = p1 ;
0 commit comments