Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support reopening audit logs #2304

Open
wants to merge 2 commits into
base: v3/master
Choose a base branch
from

Conversation

brandonpayton
Copy link
Contributor

@brandonpayton brandonpayton commented May 1, 2020

This PR adds support for reopening log files by:

  • Exposing an msc_rules_reopen_audit_log(rules, error) function to trigger audit log reopen for a given rules set
  • Adding a reopen(error) method to AuditLog
  • Adding a reopen(error) method to the audit log Writer interface
  • Adding a reopen(filename, error) method to SharedFiles

⚠️ Currently, I do not believe SharedFiles::reopen() to be thread-safe, and thread-safety must be ensured by the caller. Even if the SharedFiles general lock could be used, we may not want to lock for all SharedFile writes. Please let me know if you'd like to resolve the issue as part of this PR or a later PR.

This is based on work to support ModSecurity audit log rotation within Automattic. It is used in a ModSecurity-nginx patch, and the PR for that is here.

resolves #1968

@brandonpayton
Copy link
Contributor Author

This is failing cppcheck. Will take a look and fix.

@brandonpayton
Copy link
Contributor Author

brandonpayton commented May 1, 2020

Actually, the warning appears to be unrelated to this patch:

warning: src/utils/shared_files.h,88,performance,useInitializationList,When an object of a class is created, the constructors of all member variables are called consecutively in the order the variables are declared, even if you don't explicitly write them to the initialization list. You could avoid assigning 'm_memKeyStructure' a value by passing the value to the constructor in the initialization list.

@brandonpayton
Copy link
Contributor Author

Sorry, I was incorrect. The previous warning is the only warning I'm seeing on my dev machine.

There is a relevant warning from TravisCI running make check-static:

warning: src/utils/shared_files.h,60,style,functionConst,The member function 'modsecurity::utils::SharedFiles::reopen' can be made a const function. Making this function 'const' should not cause compiler errors. Even though the function can be made const function technically it may not make sense conceptually. Think about your design and the task of the function first - is it a function that must not change object internal state?

@brandonpayton
Copy link
Contributor Author

I added the above warning to the cppcheck suppressions list because SharedFiles::reopen() does change SharedFiles internal state.

@zimmerle zimmerle self-requested a review May 4, 2020 13:14
@zimmerle zimmerle self-assigned this May 4, 2020
@zimmerle zimmerle added the 3.x Related to ModSecurity version 3.x label May 4, 2020
@brandonpayton
Copy link
Contributor Author

Regarding thread-safety:

I was reading the nginx source this evening and noticed it redirects stderr using a POSIX function, dup2. dup2 takes an old and a new descriptor and atomically retargets the new to refer to the old. As evidence, both glibc docs and POSIX docs (requires login) state that dup2 should be atomic.

This looks like a good way to address the thread-safety issue with the current SharedFiles::reopen() in this PR.

MS also provides an implementation of dup2 for POSIX compliance, and given that the POSIX docs require atomicity, it seems like this should be thread-safe on Windows as well.

@JokerQyou
Copy link

What's the current status of this PR?

@mike-mckenna
Copy link

Is this pull request getting attention? The code in this pull request will support the rotation of the audit log in Nginx. More specifically, the code in this pull request is required before ModSecurity-nginx pull request owasp-modsecurity/ModSecurity-nginx#198 can proceed.

@liudongmiao
Copy link
Contributor

There should be a reopen for debug log.

I'd like to use a flush on modsecurity::utils::SharedFiles directly.

@tomsommer
Copy link
Contributor

Any chance of getting this fixed? Sort of annoying not being able to reliably rotate logs

@brandonpayton
Copy link
Contributor Author

As @liudongmiao mentioned here, this does not reopen the debug log. It would be good to generalize this PR to reopen logs in general.

I don't have time to work on this immediately but hope to get back to it in the next month or so. If anyone else wants to work on this before then, please feel free.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
3.x Related to ModSecurity version 3.x
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Add interface in libModSecurity for reopening log files
6 participants