Skip to content

Commit 538ffa6

Browse files
author
Marc Stern
committed
Added some null pointer checks.
Added a design doc.
1 parent 91da587 commit 538ffa6

File tree

5 files changed

+43
-15
lines changed

5 files changed

+43
-15
lines changed

apache2/mod_security2.c

+2-9
Original file line numberDiff line numberDiff line change
@@ -867,10 +867,7 @@ static int hook_request_early(request_rec *r) {
867867
* create the initial configuration.
868868
*/
869869
msr = create_tx_context(r);
870-
if (msr == NULL) {
871-
msr_log(msr, 9, "Failed to create context after request failure.");
872-
return DECLINED;
873-
}
870+
if (msr == NULL) return DECLINED;
874871
if (msr->txcfg->debuglog_level >= 9) {
875872
msr_log(msr, 9, "Context created after request failure.");
876873
}
@@ -1165,11 +1162,7 @@ static void hook_error_log(const char *file, int line, int level, apr_status_t s
11651162
#else
11661163
msr = create_tx_context((request_rec*)r);
11671164
#endif
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) {
1165+
if (msr && msr->txcfg->debuglog_level >= 9) {
11731166
msr_log(msr, 9, "Context created after request failure.");
11741167
}
11751168
}

apache2/msc_json.c

+9
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ int json_add_argument(modsec_rec *msr, const char *value, unsigned length)
8787
static int yajl_map_key(void *ctx, const unsigned char *key, size_t length)
8888
{
8989
modsec_rec *msr = (modsec_rec *) ctx;
90+
assert(msr != NULL);
9091
unsigned char *safe_key = (unsigned char *) NULL;
9192

9293
/**
@@ -118,6 +119,7 @@ static int yajl_map_key(void *ctx, const unsigned char *key, size_t length)
118119
static int yajl_null(void *ctx)
119120
{
120121
modsec_rec *msr = (modsec_rec *) ctx;
122+
assert(msr != NULL);
121123

122124
return json_add_argument(msr, "", 0);
123125
}
@@ -128,6 +130,7 @@ static int yajl_null(void *ctx)
128130
static int yajl_boolean(void *ctx, int value)
129131
{
130132
modsec_rec *msr = (modsec_rec *) ctx;
133+
assert(msr != NULL);
131134

132135
if (value) {
133136
return json_add_argument(msr, "true", strlen("true"));
@@ -143,6 +146,7 @@ static int yajl_boolean(void *ctx, int value)
143146
static int yajl_string(void *ctx, const unsigned char *value, size_t length)
144147
{
145148
modsec_rec *msr = (modsec_rec *) ctx;
149+
assert(msr != NULL);
146150

147151
return json_add_argument(msr, value, length);
148152
}
@@ -155,12 +159,14 @@ static int yajl_string(void *ctx, const unsigned char *value, size_t length)
155159
static int yajl_number(void *ctx, const char *value, size_t length)
156160
{
157161
modsec_rec *msr = (modsec_rec *) ctx;
162+
assert(msr != NULL);
158163

159164
return json_add_argument(msr, value, length);
160165
}
161166

162167
static int yajl_start_array(void *ctx) {
163168
modsec_rec *msr = (modsec_rec *) ctx;
169+
assert(msr != NULL);
164170

165171
if (!msr->json->current_key && !msr->json->prefix) {
166172
msr->json->prefix = apr_pstrdup(msr->mp, "array");
@@ -190,6 +196,7 @@ static int yajl_start_array(void *ctx) {
190196

191197
static int yajl_end_array(void *ctx) {
192198
modsec_rec *msr = (modsec_rec *) ctx;
199+
assert(msr != NULL);
193200
unsigned char *separator = (unsigned char *) NULL;
194201

195202
/**
@@ -226,6 +233,7 @@ static int yajl_end_array(void *ctx) {
226233
static int yajl_start_map(void *ctx)
227234
{
228235
modsec_rec *msr = (modsec_rec *) ctx;
236+
assert(msr != NULL);
229237

230238
/**
231239
* If we do not have a current_key, this is a top-level hash, so we do not
@@ -264,6 +272,7 @@ static int yajl_start_map(void *ctx)
264272
static int yajl_end_map(void *ctx)
265273
{
266274
modsec_rec *msr = (modsec_rec *) ctx;
275+
assert(msr != NULL);
267276
unsigned char *separator = (unsigned char *) NULL;
268277

269278
/**

apache2/msc_multipart.c

-3
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,6 @@ void validate_quotes(modsec_rec *msr, char *data, char quote) {
2424
assert(msr != NULL);
2525
int i, len;
2626

27-
if(msr == NULL)
28-
return;
29-
3027
if(msr->mpd == NULL)
3128
return;
3229

apache2/re.c

+3-3
Original file line numberDiff line numberDiff line change
@@ -215,11 +215,9 @@ char *msre_ruleset_phase_rule_update_target_matching_exception(modsec_rec *msr,
215215
rules = (msre_rule **)phase_arr->elts;
216216
for (i = 0; i < phase_arr->nelts; i++) {
217217
msre_rule *rule = (msre_rule *)rules[i];
218-
219218
if (mode == 0) { /* Looking for next rule. */
220219
if (msre_ruleset_rule_matches_exception(rule, re)) {
221-
222-
err = update_rule_target_ex(NULL, ruleset, rule, p2, p3);
220+
err = update_rule_target_ex(msr, ruleset, rule, p2, p3);
223221
if (err) return err;
224222
if (rule->actionset->is_chained) mode = 2; /* Match all rules in this chain. */
225223
} else {
@@ -3141,6 +3139,8 @@ static apr_status_t msre_rule_process_normal(msre_rule *rule, modsec_rec *msr) {
31413139
/* Perform transformations. */
31423140

31433141
tarr = apr_table_elts(normtab);
3142+
/* if no transformation, multi_match makes no sense and breaks the logic */
3143+
if (tarr->nelts == 0) multi_match = 0;
31443144

31453145
/* Execute transformations in a loop. */
31463146

design.md

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Design notes for source code
2+
==
3+
This file give some explanations and guidelines regarding ModSecurity v2 source code.
4+
The goal is to discuss topics that are not related to a specific location in the code, so that cannot be best explained by comments.
5+
The goal is not to replace comments where it is probably better.
6+
It's quite short for the moment, but the goal is to extend it from time to time.
7+
8+
## Null pointer check
9+
The default behaviour is to check for null pointer dereference everywhere it may be needed.
10+
In case a pointer cannot be null, it has to be explained with a comment at the beginning of the function of when dereferencing the pointer.
11+
On top of that, an explicit check should be done when compiling in debug mode with the following code:
12+
```
13+
assert(mypointer);
14+
```
15+
In case a pointer that cannot be null is used at several locations (say more than 3 times),
16+
the explanation could be given globally in this file.
17+
18+
### Pointers never null
19+
The following pointers can never be null:
20+
21+
#### msr
22+
23+
msr is assigned at the following places:
24+
- mod_security2.c (14 x): initialization
25+
In all the above calls, and all calling functions, it immediately returns (with an error code) in case msr is null, up to a place where no mod_security2 processing at all occurs.
26+
In subsequent calls, there's thus no possibility to have msr null.
27+
- apache2_io.c (2 x): assign a previously initialized msr
28+
- msc_json (9 x): assign a previously initialized msr
29+
- msc_lua.c (4 x): assign a previously initialized msr

0 commit comments

Comments
 (0)