Skip to content

Commit 649aea7

Browse files
author
Marc Stern
authored
Merge branch 'v2/master' into v2/mst/nullcheck2
2 parents 518b8ba + 788c36d commit 649aea7

23 files changed

+361
-101
lines changed

.github/security2.conf

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
LoadModule security2_module /usr/lib/apache2/modules/mod_security2.so
2+
3+
<IfModule security2_module>
4+
SecDataDir /var/cache/modsecurity
5+
Include /etc/apache2/modsecurity.conf
6+
</IfModule>

.github/workflows/ci.yml

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
name: Quality Assurance
2+
3+
on:
4+
push:
5+
pull_request:
6+
7+
jobs:
8+
build-linux:
9+
runs-on: ${{ matrix.os }}
10+
strategy:
11+
matrix:
12+
os: [ubuntu-22.04]
13+
platform: [x32, x64]
14+
compiler: [gcc, clang]
15+
configure:
16+
- {label: "with pcre, no study, no jit", opt: "--enable-pcre-study=no" }
17+
- {label: "with pcre, with study, no jit", opt: "--enable-pcre-study=yes" }
18+
- {label: "with pcre, no study, with jit", opt: "--enable-pcre-study=no --enable-pcre-jit" }
19+
- {label: "with pcre, with study, with jit", opt: "--enable-pcre-study=yes --enable-pcre-jit" }
20+
- {label: "with pcre2", opt: "--with-pcre2 --enable-pcre-study=no" }
21+
- {label: "with pcre2, with study, no jit", opt: "--with-pcre2 --enable-pcre-study=yes" }
22+
- {label: "with pcre2, no study, with jit", opt: "--with-pcre2 --enable-pcre-study=no --enable-pcre-jit" }
23+
- {label: "with pcre2, with study, with jit", opt: "--with-pcre2 --enable-pcre-study=yes --enable-pcre-jit" }
24+
- {label: "with lua", opt: "--with-lua" }
25+
- {label: "wo lua", opt: "--without-lua" }
26+
steps:
27+
- name: Setup Dependencies
28+
run: |
29+
sudo apt-get update -y -qq
30+
sudo apt-get install -y apache2-dev libxml2-dev liblua5.1-0-dev libcurl4-gnutls-dev libpcre2-dev pkg-config libyajl-dev apache2 apache2-bin apache2-data
31+
- uses: actions/checkout@v2
32+
- name: autogen.sh
33+
run: ./autogen.sh
34+
- name: configure ${{ matrix.configure.label }}
35+
run: ./configure ${{ matrix.configure.opt }}
36+
- uses: ammaraskar/gcc-problem-matcher@master
37+
- name: make
38+
run: make -j `nproc`
39+
- name: install module
40+
run: sudo make install
41+
- name: prepare config
42+
run: |
43+
sudo cp .github/security2.conf /etc/apache2/mods-enabled/
44+
sudo cp modsecurity.conf-recommended /etc/apache2/modsecurity.conf
45+
sudo cp unicode.mapping /etc/apache2/
46+
sudo mkdir -p /var/cache/modsecurity
47+
sudo chown -R www-data:www-data /var/cache/modsecurity
48+
- name: start apache with module
49+
run: |
50+
sudo systemctl restart apache2.service
51+

CHANGES

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
DD mmm YYYY - 2.9.x (to be released)
22
-------------------
33

4+
* Fix possible segfault in collection_unpack
5+
[Issue #3072 - @twouters]
46
* Set the minimum security protocol version for SecRemoteRules
57
[Issue security/code-scanning/2 - @airween]
68
* Allow lua version 5.4

apache2/msc_json.c

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -366,17 +366,15 @@ int json_process_chunk(modsec_rec *msr, const char *buf, unsigned int size, char
366366
assert(msr != NULL);
367367
assert(error_msg != NULL);
368368
*error_msg = NULL;
369-
// Take a copy in case libyajl decodes the buffer inline
370-
base_offset = apr_pstrmemdup(msr->mp, buf, size);
371-
if (!base_offset) return -1;
369+
base_offset=buf;
372370

373371
/* Feed our parser and catch any errors */
374-
msr->json->status = yajl_parse(msr->json->handle, (unsigned char*)base_offset, size);
372+
msr->json->status = yajl_parse(msr->json->handle, buf, size);
375373
if (msr->json->status != yajl_status_ok) {
376374
if (msr->json->depth_limit_exceeded) {
377375
*error_msg = "JSON depth limit exceeded";
378376
} else {
379-
char *yajl_err = yajl_get_error(msr->json->handle, 0, base_offset, size);
377+
char *yajl_err = yajl_get_error(msr->json->handle, 0, buf, size);
380378
*error_msg = apr_pstrdup(msr->mp, yajl_err);
381379
yajl_free_error(msr->json->handle, yajl_err);
382380
}

apache2/msc_logging.c

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -237,7 +237,15 @@ static char *construct_auditlog_filename(apr_pool_t *mp, const char *uniqueid) {
237237
* This is required for mpm-itk & mod_ruid2, though should be harmless for other implementations
238238
* It also changes the return statement.
239239
*/
240-
char *userinfo = get_username(mp);
240+
char *userinfo;
241+
apr_status_t rc;
242+
apr_uid_t uid;
243+
apr_gid_t gid;
244+
apr_uid_current(&uid, &gid, mp);
245+
rc = apr_uid_name_get(&userinfo, uid, mp);
246+
if (rc != APR_SUCCESS) {
247+
userinfo = apr_psprintf(mp, "%u", uid);
248+
}
241249

242250
apr_time_exp_lt(&t, apr_time_now());
243251

apache2/msc_pcre.c

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,11 @@ static apr_status_t msc_pcre_cleanup(msc_regex_t *regex) {
3131
}
3232
#else
3333
if (regex->pe != NULL) {
34+
#if defined(VERSION_NGINX)
3435
pcre_free(regex->pe);
36+
#else
37+
free(regex->pe);
38+
#endif
3539
regex->pe = NULL;
3640
}
3741
if (regex->re != NULL) {
@@ -148,15 +152,19 @@ void *msc_pregcomp_ex(apr_pool_t *pool, const char *pattern, int options,
148152

149153
#ifdef WITH_PCRE_STUDY
150154
#ifdef WITH_PCRE_JIT
151-
pe = pcre_study(regex->re, PCRE_STUDY_EXTRA_NEEDED|PCRE_STUDY_JIT_COMPILE, &errptr);
155+
pe = pcre_study(regex->re, PCRE_STUDY_JIT_COMPILE, &errptr);
152156
#else
153-
pe = pcre_study(regex->re, PCRE_STUDY_EXTRA_NEEDED, &errptr);
157+
pe = pcre_study(regex->re, 0, &errptr);
154158
#endif
155159
#endif
156160

157161
/* Setup the pcre_extra record if pcre_study did not already do it */
158162
if (pe == NULL) {
159-
pe = (pcre_extra*)pcre_malloc(sizeof(pcre_extra));
163+
#if defined(VERSION_NGINX)
164+
pe = pcre_malloc(sizeof(pcre_extra));
165+
#else
166+
pe = malloc(sizeof(pcre_extra));
167+
#endif
160168
if (pe == NULL) {
161169
return NULL;
162170
}

apache2/msc_util.c

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2850,14 +2850,3 @@ char* strtok_r(
28502850
}
28512851
#endif
28522852

2853-
// Function compatible with Linux & Windows, also with mpm-itk & mod_ruid2
2854-
char* get_username(apr_pool_t* mp) {
2855-
char* username;
2856-
apr_uid_t uid;
2857-
apr_gid_t gid;
2858-
int rc = apr_uid_current(&uid, &gid, mp);
2859-
if (rc != APR_SUCCESS) return "apache";
2860-
rc = apr_uid_name_get(&username, uid, mp);
2861-
if (rc != APR_SUCCESS) return "apache";
2862-
return username;
2863-
}

apache2/msc_util.h

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,8 +160,6 @@ int DSOLOCAL tree_contains_ip(apr_pool_t *mp, TreeRoot *rtree,
160160
int DSOLOCAL ip_tree_from_param(apr_pool_t *pool,
161161
char *param, TreeRoot **rtree, char **error_msg);
162162

163-
char DSOLOCAL *get_username(apr_pool_t* mp);
164-
165163
#ifdef WITH_CURL
166164
int ip_tree_from_uri(TreeRoot **rtree, char *uri,
167165
apr_pool_t *mp, char **error_msg);

apache2/persist_dbm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,4 +813,4 @@ int collections_remove_stale(modsec_rec *msr, const char *col_name) {
813813
}
814814

815815
return -1;
816-
}
816+
}

apache2/re.c

Lines changed: 16 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,6 @@ static int fetch_target_exception(msre_rule *rule, modsec_rec *msr, msre_var *va
8484
assert(exceptions != NULL);
8585

8686
{
87-
8887
myvar = apr_pstrdup(msr->mp, var->name);
8988

9089
c = strchr(myvar,':');
@@ -363,11 +362,11 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
363362
rc = msre_parse_targets(ruleset, p, rule->targets, &my_error_msg);
364363
if (rc < 0) {
365364
if(msr) {
366-
msr_log(msr, 9, "Error parsing rule targets to replace variable: %s", my_error_msg);
365+
msr_log(msr, 9, "Error parsing rule targets to replace variable");
367366
}
368367
#if !defined(MSC_TEST)
369368
else {
370-
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, " ModSecurity: Error parsing rule targets to replace variable: %s", my_error_msg);
369+
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, " ModSecurity: Error parsing rule targets to replace variable");
371370
}
372371
#endif
373372
goto end;
@@ -388,21 +387,16 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
388387
}
389388
#if !defined(MSC_TEST)
390389
else {
391-
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, " ModSecurity: Cannot find variable to replace");
390+
ap_log_error(APLOG_MARK, APLOG_ERR, 0, NULL, " ModSecurity: Cannot find varibale to replace");
392391
}
393392
#endif
394393
goto end;
395394
}
396395
} else {
397396

398397
target = strdup(p);
399-
if(target == NULL) {
400-
if(target_list != NULL)
401-
free(target_list);
402-
if(replace != NULL)
403-
free(replace);
404-
return NULL;
405-
}
398+
if(target == NULL)
399+
return NULL;
406400

407401
is_negated = is_counting = 0;
408402
param = name = value = NULL;
@@ -436,8 +430,6 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
436430
free(target_list);
437431
if(replace != NULL)
438432
free(replace);
439-
if(target != NULL)
440-
free(target);
441433
if(msr) {
442434
msr_log(msr, 9, "Error to update target - [%s] is not valid target", name);
443435
}
@@ -516,7 +508,7 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
516508
if(var_appended == 1) {
517509
current_targets = msre_generate_target_string(ruleset->mp, rule);
518510
rule->unparsed = msre_rule_generate_unparsed(ruleset->mp, rule, current_targets, NULL, NULL);
519-
rule->p1 = current_targets;
511+
rule->p1 = apr_pstrdup(ruleset->mp, current_targets);
520512
if(msr) {
521513
msr_log(msr, 9, "Successfully appended variable");
522514
}
@@ -529,12 +521,18 @@ char *update_rule_target_ex(modsec_rec *msr, msre_ruleset *ruleset, msre_rule *r
529521
}
530522

531523
end:
532-
if(target_list != NULL)
524+
if(target_list != NULL) {
533525
free(target_list);
534-
if(replace != NULL)
526+
target_list = NULL;
527+
}
528+
if(replace != NULL) {
535529
free(replace);
536-
if(target != NULL)
530+
replace = NULL;
531+
}
532+
if(target != NULL) {
537533
free(target);
534+
target = NULL;
535+
}
538536
return NULL;
539537
}
540538

@@ -648,10 +646,7 @@ static char *msre_generate_target_string(apr_pool_t *pool, msre_rule *rule) {
648646
/**
649647
* Generate an action string from an actionset.
650648
*/
651-
#ifndef DEBUG_CONF
652-
static
653-
#endif
654-
char *msre_actionset_generate_action_string(apr_pool_t *pool, const msre_actionset *actionset) {
649+
static char *msre_actionset_generate_action_string(apr_pool_t *pool, const msre_actionset *actionset) {
655650
const apr_array_header_t *tarr = NULL;
656651
const apr_table_entry_t *telts = NULL;
657652
char *actions = NULL;
@@ -1071,12 +1066,6 @@ int msre_parse_generic(apr_pool_t *mp, const char *text, apr_table_t *vartable,
10711066
/* ignore whitespace */
10721067
while(isspace(*p)) p++;
10731068
if (*p == '\0') return count;
1074-
1075-
/* ignore empty action */
1076-
if (*p == ',') {
1077-
p++;
1078-
continue;
1079-
}
10801069

10811070
/* we are at the beginning of the name */
10821071
name = p;

0 commit comments

Comments
 (0)