Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/ngx_http_modsecurity_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ typedef struct {
typedef struct {
void *pool;
ModSecurity *modsec;
void *empty_rules_set;
ngx_uint_t rules_inline;
ngx_uint_t rules_file;
ngx_uint_t rules_remote;
Expand Down
220 changes: 161 additions & 59 deletions src/ngx_http_modsecurity_module.c
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ static void *ngx_http_modsecurity_create_conf(ngx_conf_t *cf);
static char *ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child);
static void ngx_http_modsecurity_cleanup_instance(void *data);
static void ngx_http_modsecurity_cleanup_rules(void *data);
static void ngx_http_modsecurity_cleanup_empty_rules(void *data);


/*
Expand Down Expand Up @@ -275,6 +276,7 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
ngx_http_modsecurity_ctx_t *ctx;
ngx_http_modsecurity_conf_t *mcf;
ngx_http_modsecurity_main_conf_t *mmcf;
void *rules_set;

ctx = ngx_pcalloc(r->pool, sizeof(ngx_http_modsecurity_ctx_t));
if (ctx == NULL)
Expand All @@ -286,16 +288,22 @@ ngx_http_modsecurity_create_ctx(ngx_http_request_t *r)
mmcf = ngx_http_get_module_main_conf(r, ngx_http_modsecurity_module);
mcf = ngx_http_get_module_loc_conf(r, ngx_http_modsecurity_module);

dd("creating transaction with the following rules: '%p' -- ms: '%p'", mcf->rules_set, mmcf->modsec);
/*
* A loc conf that never went through merge_conf (created by another
* module) has no RulesSet: use the empty one, as before it was lazy.
*/
rules_set = mcf->rules_set != NULL ? mcf->rules_set : mmcf->empty_rules_set;

dd("creating transaction with the following rules: '%p' -- ms: '%p'", rules_set, mmcf->modsec);

if (mcf->transaction_id) {
if (ngx_http_complex_value(r, mcf->transaction_id, &s) != NGX_OK) {
return NGX_CONF_ERROR;
}
ctx->modsec_transaction = msc_new_transaction_with_id(mmcf->modsec, mcf->rules_set, (char *) s.data, r->connection->log);
ctx->modsec_transaction = msc_new_transaction_with_id(mmcf->modsec, rules_set, (char *) s.data, r->connection->log);

} else {
ctx->modsec_transaction = msc_new_transaction(mmcf->modsec, mcf->rules_set, r->connection->log);
ctx->modsec_transaction = msc_new_transaction(mmcf->modsec, rules_set, r->connection->log);
}

dd("transaction created");
Expand Down Expand Up @@ -343,15 +351,108 @@ ngx_http_modsecurity_get_module_ctx(ngx_http_request_t *r)
return ctx;
}

/*
* A block only gets its own RulesSet once it declares rules itself; blocks
* without any share their parent's (see merge_conf). Creating one per block
* and merging the parent into it copied the whole ruleset into every server,
* location, if and limit_except block, tens of KB each with the CRS.
*/
static ngx_int_t
ngx_http_modsecurity_own_rules_set(ngx_conf_t *cf, ngx_http_modsecurity_conf_t *mcf)
{
ngx_pool_cleanup_t *cln;

if (mcf->rules_set != NULL) {
return NGX_OK;
}

cln = ngx_pool_cleanup_add(cf->pool, 0);
if (cln == NULL) {
return NGX_ERROR;
}

mcf->rules_set = msc_create_rules_set();
if (mcf->rules_set == NULL) {
return NGX_ERROR;
}

cln->handler = ngx_http_modsecurity_cleanup_rules;
cln->data = mcf;

return NGX_OK;
}


typedef int (*ngx_http_modsecurity_add_pt)(void *rules_set, const char *a,
const char *b, const char **error);


static int
ngx_http_modsecurity_add_inline(void *rules_set, const char *rules,
const char *b, const char **error)
{
(void) b; /* only the remote variant takes a second argument */

return msc_rules_add(rules_set, rules, error);
}


static int
ngx_http_modsecurity_add_file(void *rules_set, const char *file,
const char *b, const char **error)
{
(void) b; /* only the remote variant takes a second argument */

return msc_rules_add_file(rules_set, file, error);
}


static int
ngx_http_modsecurity_add_remote(void *rules_set, const char *key,
const char *server, const char **error)
{
return msc_rules_add_remote(rules_set, key, server, error);
}


/*
* Common part of the three rules directives: make sure the block owns its
* RulesSet (see own_rules_set), add the rules, and count them. Going through
* this single path means a new rules directive can't forget to own one.
*/
static char *
ngx_http_modsecurity_add_rules(ngx_conf_t *cf, ngx_http_modsecurity_conf_t *mcf,
ngx_http_modsecurity_add_pt add, const char *a, const char *b,
ngx_uint_t *counter)
{
int res;
const char *error;
ngx_pool_t *old_pool;

if (ngx_http_modsecurity_own_rules_set(cf, mcf) != NGX_OK) {
return NGX_CONF_ERROR;
}

old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool);
res = add(mcf->rules_set, a, b, &error);
ngx_http_modsecurity_pcre_malloc_done(old_pool);

if (res < 0) {
dd("Failed to load the rules: '%s' - reason: '%s'", a, error);
return strdup(error);
}

*counter += res;

return NGX_CONF_OK;
}


char *
ngx_conf_set_rules(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
int res;
char *rules;
ngx_str_t *value;
const char *error;
ngx_pool_t *old_pool;
ngx_http_modsecurity_conf_t *mcf = conf;
ngx_http_modsecurity_main_conf_t *mmcf;

value = cf->args->elts;
Expand All @@ -361,31 +462,18 @@ ngx_conf_set_rules(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
return NGX_CONF_ERROR;
}

old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool);
res = msc_rules_add(mcf->rules_set, rules, &error);
ngx_http_modsecurity_pcre_malloc_done(old_pool);

if (res < 0) {
dd("Failed to load the rules: '%s' - reason: '%s'", rules, error);
return strdup(error);
}

mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_modsecurity_module);
mmcf->rules_inline += res;

return NGX_CONF_OK;
return ngx_http_modsecurity_add_rules(cf, conf,
ngx_http_modsecurity_add_inline, rules, NULL, &mmcf->rules_inline);
}


char *
ngx_conf_set_rules_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
int res;
char *rules_set;
ngx_str_t *value;
const char *error;
ngx_pool_t *old_pool;
ngx_http_modsecurity_conf_t *mcf = conf;
ngx_http_modsecurity_main_conf_t *mmcf;

value = cf->args->elts;
Expand All @@ -395,31 +483,18 @@ ngx_conf_set_rules_file(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
return NGX_CONF_ERROR;
}

old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool);
res = msc_rules_add_file(mcf->rules_set, rules_set, &error);
ngx_http_modsecurity_pcre_malloc_done(old_pool);

if (res < 0) {
dd("Failed to load the rules from: '%s' - reason: '%s'", rules_set, error);
return strdup(error);
}

mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_modsecurity_module);
mmcf->rules_file += res;

return NGX_CONF_OK;
return ngx_http_modsecurity_add_rules(cf, conf,
ngx_http_modsecurity_add_file, rules_set, NULL, &mmcf->rules_file);
}


char *
ngx_conf_set_rules_remote(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
{
int res;
ngx_str_t *value;
const char *error;
const char *rules_remote_key, *rules_remote_server;
ngx_pool_t *old_pool;
ngx_http_modsecurity_conf_t *mcf = conf;
ngx_http_modsecurity_main_conf_t *mmcf;

value = cf->args->elts;
Expand All @@ -434,19 +509,11 @@ ngx_conf_set_rules_remote(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
return NGX_CONF_ERROR;
}

old_pool = ngx_http_modsecurity_pcre_malloc_init(cf->pool);
res = msc_rules_add_remote(mcf->rules_set, rules_remote_key, rules_remote_server, &error);
ngx_http_modsecurity_pcre_malloc_done(old_pool);

if (res < 0) {
dd("Failed to load the rules from: '%s' - reason: '%s'", rules_remote_server, error);
return strdup(error);
}

mmcf = ngx_http_conf_get_module_main_conf(cf, ngx_http_modsecurity_module);
mmcf->rules_remote += res;

return NGX_CONF_OK;
return ngx_http_modsecurity_add_rules(cf, conf,
ngx_http_modsecurity_add_remote, rules_remote_key,
rules_remote_server, &mmcf->rules_remote);
}


Expand Down Expand Up @@ -670,6 +737,20 @@ ngx_http_modsecurity_create_main_conf(ngx_conf_t *cf)
msc_set_connector_info(conf->modsec, MODSECURITY_NGINX_WHOAMI);
msc_set_log_cb(conf->modsec, ngx_http_modsecurity_log);

/* fallback for loc confs without a RulesSet, see create_ctx */
cln = ngx_pool_cleanup_add(cf->pool, 0);
if (cln == NULL) {
return NULL;
}

conf->empty_rules_set = msc_create_rules_set();
if (conf->empty_rules_set == NULL) {
return NULL;
}

cln->handler = ngx_http_modsecurity_cleanup_empty_rules;
cln->data = conf;

dd ("main conf created at: '%p', instance is: '%p'", conf, conf->modsec);

return conf;
Expand Down Expand Up @@ -697,7 +778,6 @@ ngx_http_modsecurity_init_main_conf(ngx_conf_t *cf, void *conf)
static void *
ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
{
ngx_pool_cleanup_t *cln;
ngx_http_modsecurity_conf_t *conf;

conf = (ngx_http_modsecurity_conf_t *) ngx_pcalloc(cf->pool,
Expand All @@ -720,23 +800,14 @@ ngx_http_modsecurity_create_conf(ngx_conf_t *cf)
*/

conf->enable = NGX_CONF_UNSET;
conf->rules_set = msc_create_rules_set();
/* rules_set stays NULL until a rules directive, see own_rules_set() */

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

rules_set can now reach msc_new_transaction() as NULL. Before this change, create_conf guaranteed every loc conf had a non-NULL RulesSet. Now the invariant only holds for confs that went through merge_conf (or declared rules). ngx_http_modsecurity_create_ctx() (lines 295/298) passes mcf->rules_set to msc_new_transaction*() without checking it. A loc conf that a third-party module builds with create_loc_conf and never merges, then uses for a request with modsecurity on, now gives libmodsecurity a NULL RulesSet, and the Transaction constructor dereferences it and segfaults. Before, it got an empty set. Suggest a NULL guard in create_ctx: skip ModSecurity, or fall back to an empty set created at init time.

conf->pool = cf->pool;
conf->transaction_id = NGX_CONF_UNSET_PTR;
conf->use_error_log = NGX_CONF_UNSET;
#if defined(MODSECURITY_SANITY_CHECKS) && (MODSECURITY_SANITY_CHECKS)
conf->sanity_checks_enabled = NGX_CONF_UNSET;
#endif

cln = ngx_pool_cleanup_add(cf->pool, 0);
if (cln == NULL) {
dd("failed to create the ModSecurity configuration cleanup");
return NGX_CONF_ERROR;
}

cln->handler = ngx_http_modsecurity_cleanup_rules;
cln->data = conf;

dd ("conf created at: '%p'", conf);

return conf;
Expand Down Expand Up @@ -768,6 +839,23 @@ ngx_http_modsecurity_merge_conf(ngx_conf_t *cf, void *parent, void *child)
ngx_conf_merge_value(c->sanity_checks_enabled, p->sanity_checks_enabled, 0);
#endif

/*
* All the field merges are above on purpose: the sharing below returns
* early, so anything merged after it would be skipped for most blocks.
*/

/* transactions need a RulesSet, even an empty one */
if (ngx_http_modsecurity_own_rules_set(cf, p) != NGX_OK) {
return NGX_CONF_ERROR;
}

if (c->rules_set == NULL) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Altitude: only blocks with no ModSecurity directive at all benefit. Any block that declares even one directive, for example a per-location CRS exclusion such as modsecurity_rules 'SecRuleRemoveById 942100' or SecRuleEngine DetectionOnly, still goes through msc_rules_merge() and gets a full copy of the parent ruleset. In real CRS deployments, per-vhost/per-location exclusions are the norm, so the ~23 KB/block cost stays for exactly those blocks. The general fix belongs in libmodsecurity: a child RulesSet that references its parent (copy-on-write or layered lookup) instead of copying its phase vectors. Worth stating this limitation in the PR/CHANGES.

/* no rules of its own: share the parent's instead of copying it */
c->rules_set = p->rules_set;
return NGX_CONF_OK;
Comment on lines +852 to +855

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The early return NGX_CONF_OK makes the rest of merge_conf unreachable for the common case. Any field merge added below this point later will silently not apply to blocks without rules, which is almost every block. That kind of bug only shows up in nested-location configs. The MODSECURITY_DDEBUG parent/child dumps are also skipped now. Suggest keeping all ngx_conf_merge_* calls above this block, with a comment saying so, or using if (c->rules_set == NULL) { share } else { merge } with no early return.

}

/* the child declared rules: it gets the parent's merged into its own */
#if defined(MODSECURITY_DDEBUG) && (MODSECURITY_DDEBUG)
dd("PARENT RULES");
msc_rules_dump(p->rules_set);
Expand Down Expand Up @@ -804,6 +892,20 @@ ngx_http_modsecurity_cleanup_instance(void *data)
}


static void
ngx_http_modsecurity_cleanup_empty_rules(void *data)
{
ngx_pool_t *old_pool;
ngx_http_modsecurity_main_conf_t *mmcf;

mmcf = (ngx_http_modsecurity_main_conf_t *) data;

old_pool = ngx_http_modsecurity_pcre_malloc_init(mmcf->pool);
msc_rules_cleanup(mmcf->empty_rules_set);
ngx_http_modsecurity_pcre_malloc_done(old_pool);
}


static void
ngx_http_modsecurity_cleanup_rules(void *data)
{
Expand Down
Loading