Loader.GetAll() returned the loader internal map by reference under an
RLock that was released as the call returned. getClientConfig() then
called GetAll() twice -- once for len() to size a slice, once to range
over the entries -- without holding any lock. A concurrent goroutine
(e.g. the loader still synchronously processing rules during
reloadConfiguration) writing to the map between the two calls makes the
iteration yield more entries than the pre-sized slice, panicking with
"index out of range [N] with length N". On bad luck the Go runtime
trips "concurrent map iteration and map write" instead.
The race is reachable on real systems whenever the UI poller, started
by reloadConfiguration via c.Connect(), reconnects before
c.rules.Reload(path) finishes loading rules from disk. Consistently
reproducible with ~2100 rules on disk.
Make GetAll() return a shallow snapshot of the rule map under the read
lock so length and iteration are guaranteed consistent for the caller.
Update the one caller (getClientConfig) to take the snapshot once.
Add a regression test running concurrent writers and readers against
the loader; -race catches the original bug, and the new GetAll
contract makes it pass.
Disabled rules were part of the active rules. The fields were not
evaluated, but we still went through the entire list.
Not adding them to the list of active rules improves matching time,
especially when there're a lot of disabled rules.
It mainly affected when matching rules that were non-priority or
ordered alphabetically, with action Allow.
Previously when creating a new rule we followed these steps:
- Create a new protobuf Rule object from the ruleseditor or the
pop-ups.
- If the rule contained more than one operator, we converted the
list of operators to a JSON string.
- This JSON string was sent back to the daemon, and saved to the
DB.
- The list of operators were never expanded on the GUI, i.e., they
were not saved as a list of protobuf Operator objects.
- Once received in the daemon, the JSON string was parsed and
converted to a protobuf Operator list of objects.
Both, the JSON string and the list of protobuf Operator objects were
saved to disk, but the JSON string was ignored when loading the
rules.
Saving the list of operators as a JSON string was a problem if you
wanted to create or modify rules without the GUI.
Now when creating or modifying rules from the GUI, the list of operators
is no longer converted to JSON string. Instead the list is sent to the
daemon as a list of protobuf Operators, and saved as JSON objects.
Notes:
- The JSON string is no longer saved to disk as part of the rules.
- The list of operators is still saved as JSON string to the DB.
- About not enabled rules:
Previously, not enabled rules only had the list of operators as JSON
string, with the field list:[] empty.
Now the list of operators is saved as JSON objects, but if the rule
is not enabled, it won't be parsed/loaded.
Closes#1047
* unittest: test live reloading of rules
* data files for the previous commit
* tests: move files under /testdata; give better names to rules
Co-authored-by: themighty1 <themighty1>