Skip to content

Commit 91da587

Browse files
author
Marc Stern
committed
Many null pointer checks
1 parent 07f4076 commit 91da587

22 files changed

+1181
-291
lines changed

apache2/apache2_config.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,13 @@
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;

apache2/apache2_io.c

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,12 +179,13 @@ apr_status_t input_filter(ap_filter_t *f, apr_bucket_brigade *bb_out,
179179
* Reads request body from a client.
180180
*/
181181
apr_status_t read_request_body(modsec_rec *msr, char **error_msg) {
182+
assert(msr != NULL);
183+
assert( error_msg!= NULL);
182184
request_rec *r = msr->r;
183185
unsigned int finished_reading;
184186
apr_bucket_brigade *bb_in;
185187
apr_bucket *bucket;
186188

187-
if (error_msg == NULL) return -1;
188189
*error_msg = NULL;
189190

190191
if (msr->reqbody_should_exist != 1) {
@@ -368,6 +369,8 @@ apr_status_t read_request_body(modsec_rec *msr, char **error_msg) {
368369
* run or not.
369370
*/
370371
static int output_filter_should_run(modsec_rec *msr, request_rec *r) {
372+
assert(msr != NULL);
373+
assert(r != NULL);
371374
char *content_type = NULL;
372375

373376
/* Check configuration. */
@@ -429,10 +432,13 @@ static int output_filter_should_run(modsec_rec *msr, request_rec *r) {
429432
static apr_status_t output_filter_init(modsec_rec *msr, ap_filter_t *f,
430433
apr_bucket_brigade *bb_in)
431434
{
435+
assert(msr != NULL);
436+
assert(f != NULL);
432437
request_rec *r = f->r;
433438
const char *s_content_length = NULL;
434439
apr_status_t rc;
435440

441+
assert(msr != NULL);
436442
msr->of_brigade = apr_brigade_create(msr->mp, f->c->bucket_alloc);
437443
if (msr->of_brigade == NULL) {
438444
msr_log(msr, 1, "Output filter: Failed to create brigade.");
@@ -496,6 +502,8 @@ static apr_status_t output_filter_init(modsec_rec *msr, ap_filter_t *f,
496502
* and to the client.
497503
*/
498504
static apr_status_t send_of_brigade(modsec_rec *msr, ap_filter_t *f) {
505+
assert(msr != NULL);
506+
assert(f != NULL);
499507
apr_status_t rc;
500508

501509
rc = ap_pass_brigade(f->next, msr->of_brigade);
@@ -537,6 +545,8 @@ static apr_status_t send_of_brigade(modsec_rec *msr, ap_filter_t *f) {
537545
*
538546
*/
539547
static void inject_content_to_of_brigade(modsec_rec *msr, ap_filter_t *f) {
548+
assert(msr != NULL);
549+
assert(f != NULL);
540550
apr_bucket *b;
541551

542552
if (msr->txcfg->content_injection_enabled && msr->stream_output_data != NULL) {
@@ -563,6 +573,8 @@ static void inject_content_to_of_brigade(modsec_rec *msr, ap_filter_t *f) {
563573
*
564574
*/
565575
static void prepend_content_to_of_brigade(modsec_rec *msr, ap_filter_t *f) {
576+
assert(msr != NULL);
577+
assert(f != NULL);
566578
if ((msr->txcfg->content_injection_enabled) && (msr->content_prepend) && (!msr->of_skipping)) {
567579
apr_bucket *bucket_ci = NULL;
568580

@@ -1008,6 +1020,12 @@ apr_status_t output_filter(ap_filter_t *f, apr_bucket_brigade *bb_in) {
10081020
/* Now send data down the filter stream
10091021
* (full-buffering only).
10101022
*/
1023+
if (!eos_bucket) {
1024+
ap_log_error(APLOG_MARK, APLOG_ERR | APLOG_NOERRNO, 0, f->r->server,
1025+
"ModSecurity: Internal Error: eos_bucket is NULL.");
1026+
return APR_EGENERAL;
1027+
}
1028+
10111029
if ((msr->of_skipping == 0)&&(!msr->of_partial)) {
10121030
if(msr->of_stream_changed == 1) {
10131031
inject_content_to_of_brigade(msr,f);

apache2/apache2_util.c

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
* Sends a brigade with an error bucket down the filter chain.
2626
*/
2727
apr_status_t send_error_bucket(modsec_rec *msr, ap_filter_t *f, int status) {
28+
assert(msr != NULL);
29+
assert(f != NULL);
2830
apr_bucket_brigade *brigade = NULL;
2931
apr_bucket *bucket = NULL;
3032

@@ -61,6 +63,9 @@ apr_status_t send_error_bucket(modsec_rec *msr, ap_filter_t *f, int status) {
6163
* the "output" parameter.
6264
*/
6365
int apache2_exec(modsec_rec *msr, const char *command, const char **argv, char **output) {
66+
assert(msr != NULL);
67+
assert(command != NULL);
68+
6469
apr_procattr_t *procattr = NULL;
6570
apr_proc_t *procnew = NULL;
6671
apr_status_t rc = APR_SUCCESS;
@@ -204,6 +209,9 @@ char *get_env_var(request_rec *r, char *name) {
204209
static void internal_log_ex(request_rec *r, directory_config *dcfg, modsec_rec *msr,
205210
int level, int fixup, const char *text, va_list ap)
206211
{
212+
assert(r != NULL);
213+
assert(msr != NULL);
214+
assert(text != NULL);
207215
apr_size_t nbytes, nbytes_written;
208216
apr_file_t *debuglog_fd = NULL;
209217
int filter_debug_level = 0;
@@ -303,6 +311,8 @@ static void internal_log_ex(request_rec *r, directory_config *dcfg, modsec_rec *
303311
* Apache error log if the message is important enough.
304312
*/
305313
void msr_log(modsec_rec *msr, int level, const char *text, ...) {
314+
assert(msr != NULL);
315+
assert(text != NULL);
306316
va_list ap;
307317

308318
va_start(ap, text);
@@ -316,6 +326,8 @@ void msr_log(modsec_rec *msr, int level, const char *text, ...) {
316326
* Apache error log. This is intended for error callbacks.
317327
*/
318328
void msr_log_error(modsec_rec *msr, const char *text, ...) {
329+
assert(msr != NULL);
330+
assert(text != NULL);
319331
va_list ap;
320332

321333
va_start(ap, text);
@@ -330,6 +342,8 @@ void msr_log_error(modsec_rec *msr, const char *text, ...) {
330342
* The 'text' will first be escaped.
331343
*/
332344
void msr_log_warn(modsec_rec *msr, const char *text, ...) {
345+
assert(msr != NULL);
346+
assert(text != NULL);
333347
va_list ap;
334348

335349
va_start(ap, text);

apache2/mod_security2.c

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -475,6 +475,8 @@ static modsec_rec *retrieve_tx_context(request_rec *r) {
475475
* phases, redirections, or subrequests.
476476
*/
477477
static void store_tx_context(modsec_rec *msr, request_rec *r) {
478+
assert(msr != NULL);
479+
assert(r != NULL);
478480
apr_table_setn(r->notes, NOTE_MSR, (void *)msr);
479481
}
480482

@@ -491,7 +493,10 @@ static modsec_rec *create_tx_context(request_rec *r) {
491493
apr_allocator_create(&allocator);
492494
apr_allocator_max_free_set(allocator, 1024);
493495
apr_pool_create_ex(&msr->mp, r->pool, NULL, allocator);
494-
if (msr->mp == NULL) return NULL;
496+
if (msr->mp == NULL) {
497+
apr_allocator_destroy(allocator);
498+
return NULL;
499+
}
495500
apr_allocator_owner_set(allocator, msr->mp);
496501

497502
msr->modsecurity = modsecurity;
@@ -862,7 +867,13 @@ static int hook_request_early(request_rec *r) {
862867
* create the initial configuration.
863868
*/
864869
msr = create_tx_context(r);
865-
if (msr == NULL) return DECLINED;
870+
if (msr == NULL) {
871+
msr_log(msr, 9, "Failed to create context after request failure.");
872+
return DECLINED;
873+
}
874+
if (msr->txcfg->debuglog_level >= 9) {
875+
msr_log(msr, 9, "Context created after request failure.");
876+
}
866877

867878
#ifdef REQUEST_EARLY
868879

@@ -1150,17 +1161,16 @@ static void hook_error_log(const char *file, int line, int level, apr_status_t s
11501161
#endif
11511162
if (msr_ap_server) {
11521163
#if AP_SERVER_MAJORVERSION_NUMBER > 1 && AP_SERVER_MINORVERSION_NUMBER > 2
1153-
msr = create_tx_context((request_rec *)info->r);
1164+
msr = create_tx_context((request_rec*)info->r);
11541165
#else
1155-
msr = create_tx_context((request_rec *)r);
1166+
msr = create_tx_context((request_rec*)r);
11561167
#endif
1157-
if (msr != NULL && msr->txcfg->debuglog_level >= 9) {
1158-
if (msr == NULL) {
1159-
msr_log(msr, 9, "Failed to create context after request failure.");
1160-
}
1161-
else {
1162-
msr_log(msr, 9, "Context created after request failure.");
1163-
}
1168+
if (msr == NULL) {
1169+
msr_log(msr, 9, "Failed to create context after request failure.");
1170+
return;
1171+
}
1172+
if (msr->txcfg->debuglog_level >= 9) {
1173+
msr_log(msr, 9, "Context created after request failure.");
11641174
}
11651175
}
11661176
if (msr == NULL) return;

apache2/modsecurity.c

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ int DSOLOCAL *unicode_map_table = NULL;
4141
const char * msc_alert_message(modsec_rec *msr, msre_actionset *actionset, const char *action_message,
4242
const char *rule_message)
4343
{
44+
assert(msr != NULL);
45+
assert(actionset != NULL);
4446
const char *message = NULL;
4547

4648
if (rule_message == NULL) rule_message = "Unknown error.";
@@ -63,6 +65,8 @@ const char * msc_alert_message(modsec_rec *msr, msre_actionset *actionset, const
6365
void msc_alert(modsec_rec *msr, int level, msre_actionset *actionset, const char *action_message,
6466
const char *rule_message)
6567
{
68+
assert(msr != NULL);
69+
assert(actionset != NULL);
6670
const char *message = msc_alert_message(msr, actionset, action_message, rule_message);
6771

6872
msr_log(msr, level, "%s", message);
@@ -126,6 +130,11 @@ msc_engine *modsecurity_create(apr_pool_t *mp, int processing_mode) {
126130
int modsecurity_init(msc_engine *msce, apr_pool_t *mp) {
127131
apr_status_t rc;
128132

133+
msce->auditlog_lock = msce->geo_lock = NULL;
134+
#ifdef GLOBAL_COLLECTION_LOCK
135+
msce->geo_lock = NULL;
136+
#endif
137+
129138
/**
130139
* Notice that curl is initialized here but never cleaned up. First version
131140
* of this implementation curl was initialized and cleaned for every
@@ -547,6 +556,7 @@ apr_status_t modsecurity_tx_init(modsec_rec *msr) {
547556
*
548557
*/
549558
static int is_response_status_relevant(modsec_rec *msr, int status) {
559+
assert(msr != NULL);
550560
char *my_error_msg = NULL;
551561
apr_status_t rc;
552562
char buf[32];
@@ -780,6 +790,7 @@ static apr_status_t modsecurity_process_phase_logging(modsec_rec *msr) {
780790
* in the modsec_rec structure.
781791
*/
782792
apr_status_t modsecurity_process_phase(modsec_rec *msr, unsigned int phase) {
793+
assert(msr != NULL);
783794
/* Check if we should run. */
784795
if ((msr->was_intercepted)&&(phase != PHASE_LOGGING)) {
785796
if (msr->txcfg->debuglog_level >= 4) {

0 commit comments

Comments
 (0)