Bug 1388656

Summary: ModSecurity: collections_remove_stale: Failed deleting collection
Product: Red Hat Enterprise Linux 7 Reporter: Robert Bost <rbost>
Component: mod_securityAssignee: Daniel Kopeček <dkopecek>
Status: CLOSED ERRATA QA Contact: Jakub Heger <jheger>
Severity: high Docs Contact:
Priority: high    
Version: 7.4CC: akhaitov, aogburn, cww, dkopecek, dmasirka, insights-rule-dev, isenfeld, jheger, mbabacek, mgrepl, mkolaja, pvrabec, rbost
Target Milestone: rcKeywords: Patch, Triaged
Target Release: ---   
Hardware: Unspecified   
OS: Unspecified   
Whiteboard:
Fixed In Version: mod_security-2.9.2-1.el7 Doc Type: If docs needed, set a value
Doc Text:
Story Points: ---
Clone Of:
: 1650268 (view as bug list) Environment:
Last Closed: 2018-04-10 16:34:21 UTC Type: Bug
Regression: --- Mount Type: ---
Documentation: --- CRM:
Verified Versions: Category: ---
oVirt Team: --- RHEL 7.3 requirements from Atomic Host:
Cloudforms Team: --- Target Upstream Version:
Embargoed:
Bug Depends On:    
Bug Blocks: 1420851    
Attachments:
Description Flags
upstream patch
none
proposed patch
none
proposed patch
none
SRPM for testing purposes none

Description Robert Bost 2016-10-25 20:59:45 UTC
Description of problem:
Repeating error message about failure to delete from collection:

[Fri Sep 23 10:52:17.022473 2016] [:error] [pid 14825] [client ::1] ModSecurity: collections_remove_stale: Failed deleting collection (name "ip", key "44.33.55.777_17c09ec5cebff2507db855e6cac332d395dd43c7"): Internal error [hostname "192.168.1.2"] [uri "*"] [unique_id "V@TtQHqZvlSpAUCIOTgi@gAAACc"]

Files in SecTmpDir and SecDataDir become very large and may be related to the error above.

-rw-r-----. 1 apache apache    0 Sep 19 16:03 global.dir
-rw-r-----. 1 apache apache    0 Sep 19 16:03 global.pag
-rw-r-----. 1 apache apache  36K Sep 23 11:11 ip.dir
-rw-r-----. 1 apache apache 287M Sep 23 11:11 ip.pag
-rw-r-----. 1 apache apache  96K Sep 23 11:10 resource.dir
-rw-r-----. 1 apache apache 932M Sep 23 11:11 resource.pag



Version-Release number of selected component (if applicable):
mod_security-2.7.3-5.el7


How reproducible: Seems to be reproduced over a period of time or after many requests.

Actual results: .pag files become very large and require manual cleanup.

Expected results: .pag files would be cleaned up automatically and not see "collections_remove_stale: Failed".


Additional info:
Customer seeing this issue has only added this single directive:

SecRule REMOTE_ADDR "@ipMatch 44.33.0.0/16,192.168.1.0/24,10.0.0.0/8" "phase:1,id:'981033',nolog,allow,ctl:ruleEngine=Off,ctl:auditEngine=Off

They also have the stock configuration shipped with mod_security.

Comment 3 Robert Bost 2016-11-19 21:57:46 UTC
I think this issue is related to https://issues.jboss.org/browse/JWS-489. Basically, looks like two threads are trying to delete a stale entry from database but one is winning and the other one throws the "Failed deleting collection" error.

Comment 4 Robert Bost 2016-12-01 15:59:26 UTC
This issue has changed slightly since initially reported. The reason for noticing the issue was that the database was growing very very large. The customer had to stop httpd, remove the mod_security collection files in SecDataDir, and then start httpd back up.

There is already a solution for this in mod_security 2.9.1:

  https://github.com/SpiderLabs/ModSecurity/pull/836

The root cause was a bad call to srand before calling rand to determine if garbage collection should run (i.e. the logic that removes expired elements from collection).

Can RHEL 7 mod_security be rebased to 2.9.1 or can that one line change in the pull request above be backported?

Comment 5 Daniel Kopeček 2016-12-06 09:06:14 UTC
Created attachment 1228341 [details]
upstream patch

Comment 6 Daniel Kopeček 2016-12-06 09:13:59 UTC
Created attachment 1228355 [details]
proposed patch

Comment 7 Daniel Kopeček 2016-12-06 09:18:06 UTC
(In reply to Robert Bost from comment #4)
> This issue has changed slightly since initially reported. The reason for
> noticing the issue was that the database was growing very very large. The
> customer had to stop httpd, remove the mod_security collection files in
> SecDataDir, and then start httpd back up.
> 
> There is already a solution for this in mod_security 2.9.1:
> 
>   https://github.com/SpiderLabs/ModSecurity/pull/836
> 
> The root cause was a bad call to srand before calling rand to determine if
> garbage collection should run (i.e. the logic that removes expired elements
> from collection).
> 
> Can RHEL 7 mod_security be rebased to 2.9.1 or can that one line change in
> the pull request above be backported?

Looks like an easy fix since we already have a patch for this. Getting the acks for 7.4 might be harder.

Comment 13 Daniel Kopeček 2017-02-16 13:40:41 UTC
Created attachment 1250867 [details]
proposed patch

based on https://github.com/SpiderLabs/ModSecurity/pull/1224

Comment 14 Daniel Kopeček 2017-02-16 13:46:10 UTC
Created attachment 1250868 [details]
SRPM for testing purposes

Upstream did not accept the solution in PR#1224 yet. That is because of fears of a performance hit that a solution based on a global mutex locking might cause.

Would it be possible to test the provided SRPM w.r.t. to performance in a real world environment?

Comment 21 Daniel Kopeček 2017-03-10 13:27:23 UTC
*** Bug 1400625 has been marked as a duplicate of this bug. ***

Comment 34 Daniel Kopeček 2017-05-22 12:00:19 UTC
The upstream PR was merged as a compile-time option:

https://github.com/SpiderLabs/ModSecurity/pull/1224#issuecomment-302938891

Comment 45 errata-xmlrpc 2018-04-10 16:34:21 UTC
Since the problem described in this bug report should be
resolved in a recent advisory, it has been closed with a
resolution of ERRATA.

For information on the advisory, and where to find the updated
files, follow the link below.

If the solution does not work for you, open a new bug report.

https://access.redhat.com/errata/RHBA-2018:0908