Repository navigation
Support log reopen on SIGHUP and SIGUSR1 - #372
AntonDemushkin wants to merge 1 commit into
Conversation
`msc_rules_reopen_logs()` only exists in rules_set.h (the newer API) — it's not available in the older rules.h at all. So the ``#ifdef MSC_USE_RULES_SET` guard is necessary here; without it, the build would fail against older libmodsecurity versions that don't have this function.
|
There was a problem hiding this comment.
Pull request overview
Adds support for reopening ModSecurity audit/debug logs when nginx handles log reopen/reload signals (SIGUSR1/SIGHUP), using the newer msc_rules_reopen_logs() ModSecurity API behind an MSC_USE_RULES_SET feature guard.
Changes:
- Registers a custom
ngx_open_file_tflush callback to trigger ModSecurity log reopen on nginx log reopen events. - Tracks each merged
RulesSet *so log reopen can be applied across all configured rulesets. - Extends main module configuration to store the reopen trigger file handle.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
src/ngx_http_modsecurity_module.c |
Adds log-reopen trigger setup, ruleset registration, and a flush callback calling msc_rules_reopen_logs() on reopen signals. |
src/ngx_http_modsecurity_common.h |
Extends main конф struct to hold the reopen trigger ngx_open_file_t *. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| #if defined(MSC_USE_RULES_SET) | ||
| /* Set up log reopening on SIGUSR1/SIGHUP */ | ||
| { | ||
| ngx_str_t log_reopen_file = ngx_string("/dev/null"); | ||
|
|
||
| conf->log_reopen = ngx_conf_open_file(cf->cycle, &log_reopen_file); | ||
| if (conf->log_reopen == NULL) { | ||
| dd("failed to open file for triggering log reopen"); | ||
| return NGX_CONF_ERROR; | ||
| } | ||
|
|
||
| conf->log_reopen->data = ngx_list_create(cf->pool, 100, | ||
| sizeof(RulesSet *)); | ||
| if (conf->log_reopen->data == NULL) { | ||
| dd("failed to create list of rules sets for log reopen"); | ||
| return NGX_CONF_ERROR; | ||
| } | ||
|
|
||
| conf->log_reopen->flush = ngx_http_modsecurity_log_reopen; | ||
| } |
|
could you take a look at Copilot's review? Current status: we wait to merge ModSecurity/3582 then release a new version of library. |
| return NGX_CONF_ERROR; | ||
| } | ||
|
|
||
| conf->log_reopen->data = ngx_list_create(cf->pool, 100, |
There was a problem hiding this comment.
ngx_conf_open_file("/dev/null") returns the shared ngx_open_file_t that any other module using /dev/null also gets, and this code overwrites its data/flush with a ngx_list_t and the modsecurity callback.
access_log /dev/null; is a common config. ngx_http_log_handler does buffer = log[l].file->data on every request (ngx_http_log_module.c:327), so it treats this ngx_list_t as an ngx_http_log_buf_t and memcpy's each log line into the list's elts array, corrupting the heap. With access_log /dev/null buffer=32k; the config is rejected with "already defined with conflicting parameters". In the other direction, a stream-block access_log /dev/null buffer=... parsed before http{} has its flush/data clobbered, and its buffer is then walked as a list. Use a sentinel name unique to this module, or register the real audit/debug log paths, rather than /dev/null.
| #if defined(MSC_USE_RULES_SET) | ||
| /* Set up log reopening on SIGUSR1/SIGHUP */ | ||
| { | ||
| ngx_str_t log_reopen_file = ngx_string("/dev/null"); |
There was a problem hiding this comment.
Adding /dev/null to cycle->open_files makes the master process, running as root, chown("/dev/null", <worker user>) on every SIGUSR1.
In ngx_reopen_files(cycle, ccf->user) (ngx_cycle.c:1242) the master stats each open file and calls chown if st_uid != user. After the first nginx -s reopen (for example from logrotate's postrotate), /dev/null on the host is owned by www-data/nobody. The worker user then owns a system-wide device node and can chmod it, so a compromised worker can break /dev/null for every process on the box. Every modsecurity-enabled nginx gets this even if it never mentions /dev/null.
| msc_set_connector_info(conf->modsec, MODSECURITY_NGINX_WHOAMI); | ||
| msc_set_log_cb(conf->modsec, ngx_http_modsecurity_log); | ||
|
|
||
| #if defined(MSC_USE_RULES_SET) |
There was a problem hiding this comment.
The new code is guarded by MSC_USE_RULES_SET, which is defined for every libmodsecurity >= 3.0.4 (common.h:32), but msc_rules_reopen_logs exists only in the unmerged owasp-modsecurity/ModSecurity#3521.
Building against any released libmodsecurity (3.0.4 through current 3.0.x) fails: the compiler reports msc_rules_reopen_logs as implicitly declared, and linking fails with an undefined reference. The PR body already notes that CI fails. The guard needs a real feature test, such as a config ngx_feature probe for msc_rules_reopen_logs or a MODSECURITY_VERSION_NUM bound for the release that ships it, not the RulesSet header switch.
| for (part = &list->part; part != NULL; part = part->next) { | ||
| rules_sets = part->elts; | ||
| for (i = 0; i < part->nelts; i++) { | ||
| if (msc_rules_reopen_logs(rules_sets[i], &error) < 0) { |
There was a problem hiding this comment.
The flush callback also runs in the master, as root, before the workers are signalled, so rotated audit/debug logs are recreated as root-owned 0644 files that the workers cannot then reopen.
Sequence: logrotate moves audit.log without create, then sends SIGUSR1. The master's ngx_reopen_files calls this callback, and SharedFiles::reopen fopen("a") creates audit.log owned by root:root with mode 0644. nginx chowns only its own files, not this one. Each worker then stats a different inode, its fopen("a") fails with EACCES, and it keeps writing to the rotated file, which may then be compressed or deleted. Audit events are lost and every worker logs "failed to reopen modsecurity logs". Skip the reopen when ngx_process == NGX_PROCESS_MASTER, or chown the new file to ccf->user.
| return NGX_CONF_ERROR; | ||
| } | ||
|
|
||
| conf->log_reopen->flush = ngx_http_modsecurity_log_reopen; |
There was a problem hiding this comment.
ngx_open_file_t.flush is also called from ngx_conf_flush_files, the core module's exit_process hook, so every worker reopens all ModSecurity logs as it shuts down, not only on SIGUSR1.
On every nginx -s reload or quit, each old worker runs msc_rules_reopen_logs for every registered rules set during exit. If the log was rotated but SIGUSR1 hasn't been sent yet, or the worker lacks permission, as in the previous finding, each exiting worker writes spurious "failed to reopen modsecurity logs" errors and does useless file I/O in the shutdown path. The flush slot means "flush buffers", not "reopen".
| for (i = 0; i < part->nelts; i++) { | ||
| if (msc_rules_reopen_logs(rules_sets[i], &error) < 0) { | ||
| ngx_log_error(NGX_LOG_ERR, log, 0, | ||
| "failed to reopen modsecurity logs: %s", error); |
There was a problem hiding this comment.
msc_rules_reopen_logs returns a strdup'd *error on failure, and the reopen callback logs it but never frees it.
Each SIGUSR1 where reopen fails (for example the EACCES case above) leaks one heap string per registered rules set per worker. With thousands of locations and periodic logrotate, worker memory grows unbounded. The merge_conf path (return strdup(error) at line 813) also leaks the original allocation, because it copies the already-strdup'd string again. Call free((void *) error) after logging, and return error directly from merge_conf.
| return strdup(error); | ||
| } | ||
|
|
||
| if (ngx_http_modsecurity_set_up_log_reopen(cf, c) < 0) { |
There was a problem hiding this comment.
Every merged location/server RulesSet is pushed into the reopen list and reopened again at merge time, so the work is O(number of locations) per signal in every process, even though all of them share a few SharedFiles paths.
A config with 5,000 locations and one SecAuditLog/SecDebugLog makes each SIGUSR1 run 5,000 msc_rules_reopen_logs calls in the master and in every worker, each doing stat+fstat (plus fopen/fclose on the first one). Config load also does 5,000 extra reopen calls in merge_conf. Because SharedFiles is process-global and keyed by filename, reopening once per unique log path, or once per process, would do the same job. Registering only rule sets that actually set an audit/debug log, or deduping by path, avoids this.



This PR uses new ModSecurity API implemented in owasp-modsecurity/ModSecurity#3521
The CI build fails because the ModSecurity API isn't released yet ☝️
There is another PR with a similar code -- #198 -- but its code is outdated and uses a different API. In any case this PR is based on work in #198, so thank you @brandonpayton for the initial work on that.
I've tested the code using testing notes from #198. I hope we'll see these changes released soon!
Side notes for reviewers
msc_rules_reopen_logs()only exists in rules_set.h (the newer API) — it's not available in the older rules.h at all. So the ``#ifdef MSC_USE_RULES_SET` guard is necessary. Without it the build would fail against olderlibmodsecurity versions that don't have this function.
Links
This PR depends on code from owasp-modsecurity/ModSecurity#3521
Resolves #121
Similar PR #198