-
Notifications
You must be signed in to change notification settings - Fork 19
/
AuditRulesMonitor.cpp
331 lines (296 loc) · 13.4 KB
/
AuditRulesMonitor.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
/*
microsoft-oms-auditd-plugin
Copyright (c) Microsoft Corporation
All rights reserved.
MIT License
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the ""Software""), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED *AS IS*, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <sstream>
#include "AuditRulesMonitor.h"
#include "Logger.h"
#include "ExecUtil.h"
#include "AuditStatus.h"
void AuditRulesMonitor::run() {
Logger::Info("AuditRulesMonitor: Starting");
if (_netlink.Open(nullptr) != 0) {
Logger::Error("AuditRulesMonitor: Could not open NETLINK connect, exiting");
return;
}
check_audit_status();
while(!_sleep(15000)) {
auto now = std::chrono::steady_clock::now();
if (now - _last_auoms_file_check > std::chrono::seconds(60)) {
_last_auoms_file_check = now;
get_desired_rules();
}
if (now - _last_audit_file_check > std::chrono::seconds(5*60)) {
_last_audit_file_check = now;
check_file_rules();
}
if (!check_kernel_rules()) {
Logger::Error("AuditRulesMonitor: Encountered unrecoverable error, stopping");
return;
}
}
}
void AuditRulesMonitor::on_stop() {
_netlink.Close();
Logger::Info("AuditRulesMonitor stopped");
}
void AuditRulesMonitor::get_desired_rules() {
try {
std::vector<std::string> errors;
auto rules = ReadAuditRulesFromDir(_audit_rules_dir, &errors);
_desired_rules.resize(0);
for (auto& rule: rules) {
// Only include the rule in the desired rules if it is supported on the host system
if (rule.IsLoadable()) {
rule.AddKey(AUOMS_RULE_KEY);
_desired_rules.emplace_back(rule);
}
}
if (errors.empty()) {
_op_status->ClearErrorCondition(ErrorCategory::DESIRED_RULES);
} else {
std::stringstream ss;
ss << " Encountered parse errors: " << std::endl;
for (auto& err : errors) {
ss << " " << err << std::endl;
}
_op_status->SetErrorCondition(ErrorCategory::DESIRED_RULES, ss.str());
}
} catch(std::exception& ex) {
Logger::Error("AuditRulesMonitor: Failed to read desired rules from %s: %s", _audit_rules_dir.c_str(), ex.what());
_op_status->SetErrorCondition(ErrorCategory::DESIRED_RULES, "Failed to read desired rules from " + _audit_rules_dir + ": " + ex.what());
}
}
void AuditRulesMonitor::check_file_rules() {
if (_desired_rules.empty() || !HasAuditdRulesFiles()) {
_op_status->ClearErrorCondition(ErrorCategory::AUDIT_RULES_FILE);
return;
}
try {
std::vector<std::string> errors;
auto rules = ReadActualAuditdRules(false, &errors);
auto merged_rules = MergeRules(rules);
auto diff = DiffRules(merged_rules, _desired_rules, "");
if (diff.empty()) {
// At this stage we only care about errors if the diff is empty.
// If diff is non-empty, the rules file will be reread/reparsed.
if (errors.empty()) {
_op_status->ClearErrorCondition(ErrorCategory::AUDIT_RULES_FILE);
} else {
std::stringstream ss;
ss << " Encountered parse errors: " << std::endl;
for (auto& err : errors) {
ss << " " << err << std::endl;
}
_op_status->SetErrorCondition(ErrorCategory::AUDIT_RULES_FILE, ss.str());
}
return;
}
Logger::Info("AuditRulesMonitor: Found desired audit rules not currently present in auditd rules files(s), adding new rules");
// Re-read rules but exclude auoms rules
errors.clear();
rules = ReadActualAuditdRules(true, &errors);
merged_rules = MergeRules(rules);
// Re-calculate diff
diff = DiffRules(merged_rules, _desired_rules, "");
if (WriteAuditdRules(diff)) {
Logger::Info("AuditRulesMonitor: augenrules appears to be in-use, running augenrules after updating auoms rules in /etc/audit/rules.d");
Cmd cmd(AUGENRULES_BIN, {}, Cmd::NULL_STDIN|Cmd::COMBINE_OUTPUT);
std::string output;
auto ret = cmd.Run(output);
if (ret != 0) {
Logger::Warn("AuditRulesMonitor: augenrules failed: %s", cmd.FailMsg().c_str());
Logger::Warn("AuditRulesMonitor: augenrules output: %s", output.c_str());
if (errors.empty()) {
_op_status->SetErrorCondition(ErrorCategory::AUDIT_RULES_FILE, std::string("augenrules failed: ") + cmd.FailMsg());
} else {
std::stringstream ss;
ss << " Encountered parse errors and augenrules failed: " << std::endl;
ss << " augenrules error:" << cmd.FailMsg() << std::endl;
for (auto& err : errors) {
ss << " " << err << std::endl;
}
_op_status->SetErrorCondition(ErrorCategory::AUDIT_RULES_FILE, ss.str());
}
return;
} else {
Logger::Warn("AuditRulesMonitor: augenrules succeeded");
}
}
if (errors.empty()) {
_op_status->ClearErrorCondition(ErrorCategory::AUDIT_RULES_FILE);
} else {
std::stringstream ss;
ss << " Encountered parse errors: " << std::endl;
for (auto& err : errors) {
ss << " " << err << std::endl;
}
_op_status->SetErrorCondition(ErrorCategory::AUDIT_RULES_FILE, ss.str());
}
} catch (std::exception& ex) {
Logger::Error("AuditRulesMonitor: Failed to check/update auditd rules: %s", ex.what());
_op_status->SetErrorCondition(ErrorCategory::AUDIT_RULES_FILE, std::string("Failed to check/update auditd rules: ") + ex.what());
}
}
template<typename T>
bool is_set_intersect(T a, T b) {
for (auto& e: b) {
if (a.find(e) == a.end()) {
return false;
}
}
return true;
}
bool AuditRulesMonitor::check_kernel_rules() {
_op_status->SetDesiredAuditRules(_desired_rules);
if (_desired_rules.empty()) {
return true;
}
std::vector<AuditRule> rules;
auto ret = NetlinkRetry([this,&rules]() {
rules.clear();
return _netlink.AuditListRules(rules);
});
if (ret != 0) {
Logger::Error("AuditRulesMonitor: Unable to fetch audit rules from kernel: %s", std::strerror(-ret));
_op_status->SetErrorCondition(ErrorCategory::AUDIT_RULES_KERNEL, std::string("Unable to fetch audit rules from kernel: ") + std::strerror(-ret));
_op_status->SetLoadedAuditRules({{}});
return false;
} else {
_op_status->SetLoadedAuditRules(rules);
}
auto merged_rules = MergeRules(rules);
auto diff = DiffRules(merged_rules, _desired_rules, "");
if (diff.empty()) {
_op_status->ClearErrorCondition(ErrorCategory::AUDIT_RULES_KERNEL);
return true;
}
uint32_t enabled = 0;
ret = NetlinkRetry([this,&enabled]() { return _netlink.AuditGetEnabled(enabled); });
if (ret != 0) {
Logger::Error("AuditRulesMonitor: Unable to get audit status from kernel: %s", std::strerror(-ret));
_op_status->SetErrorCondition(ErrorCategory::AUDIT_RULES_KERNEL, std::string("Unable to get audit status from kernel: ") + std::strerror(-ret));
return false;
}
if (enabled == 2) {
if (!_rules_immutable) {
Logger::Error("AuditRulesMonitor: Unable to add desired rules because audit rules are set to immutable");
_op_status->SetErrorCondition(ErrorCategory::AUDIT_RULES_KERNEL, "Unable to add desired rules because audit rules are set to immutable");
_rules_immutable = true;
}
return true;
} else {
_rules_immutable = false;
}
Logger::Info("AuditRulesMonitor: Found desired audit rules not currently loaded, loading new rules");
std::unordered_map<std::string, AuditRule> _dmap;
for (auto& rule: _desired_rules) {
_dmap.emplace(rule.CanonicalMergeKey(), rule);
}
bool failed_old = false;
bool failed_new = false;
// Delete all old auoms rules
for (auto& rule: rules) {
// Delete rule if it has AUOMS_RULE_KEY or matches any of the desired rules.
bool delete_it = rule.GetKeys().count(AUOMS_RULE_KEY) > 0;
if (!delete_it) {
auto itr = _dmap.find(rule.CanonicalMergeKey());
if (itr != _dmap.end()) {
if (rule.IsWatch()) {
// Check to see if the rule's perms is a subset of the desired rule's perms
auto dset = itr->second.GetPerms();
auto aset = rule.GetPerms();
if (is_set_intersect(dset, aset)) {
delete_it = true;
}
} else {
// Check to see if the rule's syscalls is a subset of the desired rule's syscalls
auto dset = itr->second.GetSyscalls();
auto aset = rule.GetSyscalls();
if (is_set_intersect(dset, aset)) {
delete_it = true;
}
}
}
}
if (delete_it) {
ret = _netlink.AuditDelRule(rule);
if (ret != 0) {
Logger::Warn("AuditRulesMonitor: Failed to delete audit rule (%s): %s\n", rule.CanonicalText().c_str(), strerror(-ret));
failed_old = true;
}
}
}
// refresh rules list
ret = NetlinkRetry([this,&rules]() {
rules.clear();
return _netlink.AuditListRules(rules);
});
if (ret != 0) {
Logger::Error("AuditRulesMonitor: Unable to fetch audit rules from kernel: %s", std::strerror(-ret));
_op_status->SetErrorCondition(ErrorCategory::AUDIT_RULES_KERNEL, std::string("Unable to fetch audit rules from kernel: ") + std::strerror(-ret));
return false;
}
merged_rules = MergeRules(rules);
// re-diff rules
diff = DiffRules(merged_rules, _desired_rules, "");
if (diff.empty()) {
_op_status->ClearErrorCondition(ErrorCategory::AUDIT_RULES_KERNEL);
return true;
}
// Add diff rules
for (auto& rule: diff) {
ret = _netlink.AuditAddRule(rule);
if (ret != 0) {
Logger::Warn("AuditRulesMonitor: Failed to load audit rule (%s): %s\n", rule.CanonicalText().c_str(), strerror(-ret));
failed_new = true;
}
}
if (failed_new && !failed_old) {
_op_status->SetErrorCondition(ErrorCategory::AUDIT_RULES_KERNEL, "Failed to add new rule(s)");
} else if (!failed_new && failed_old) {
_op_status->SetErrorCondition(ErrorCategory::AUDIT_RULES_KERNEL, "Failed to delete old rule(s)");
} else if (failed_new && failed_old) {
_op_status->SetErrorCondition(ErrorCategory::AUDIT_RULES_KERNEL, "Failed to delete old rule(s) and failed to add new rule(s)");
} else {
_op_status->ClearErrorCondition(ErrorCategory::AUDIT_RULES_KERNEL);
}
return true;
}
void AuditRulesMonitor::check_audit_status() {
AuditStatus status;
auto ret = NetlinkRetry([this,&status]() { return status.GetStatus(_netlink); } );
if (ret != 0) {
Logger::Error("Failed to get audit status: %s", std::strerror(-ret));
return;
}
if (status.GetBacklogLimit() < _backlog_limit) {
AuditStatus new_status;
Logger::Error("Increasing audit backlog limit from %u to %u", status.GetBacklogLimit(), _backlog_limit);
new_status.SetBacklogLimit(_backlog_limit);
ret = NetlinkRetry([this,&new_status]() {
return new_status.UpdateStatus(_netlink);
});
if (ret != 0) {
Logger::Error("Failed to set audit backlog limit to %d: %s", _backlog_limit, std::strerror(-ret));
return;
}
}
if (status.HasFeature(AuditStatus::Feature::BacklogWaitTime) && status.GetBacklogWaitTime() != _backlog_wait_time) {
AuditStatus new_status;
Logger::Error("Changing audit backlog wait time from %u to %u", status.GetBacklogWaitTime(), _backlog_wait_time);
new_status.SetBacklogWaitTime(_backlog_wait_time);
ret = NetlinkRetry([this,&new_status]() {
return new_status.UpdateStatus(_netlink);
});
if (ret != 0) {
Logger::Error("Failed to set audit backlog wait time to %d: %s", _backlog_wait_time, std::strerror(-ret));
return;
}
}
}