-
Notifications
You must be signed in to change notification settings - Fork 19
/
Copy pathCmdlineRedactor.cpp
236 lines (195 loc) · 8.69 KB
/
CmdlineRedactor.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
/*
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 "CmdlineRedactor.h"
#include "Config.h"
#include "FileUtils.h"
#include "Logger.h"
#include "StringUtils.h"
#define CONFIG_SUFFIX ".conf"
#define REQUIRES_SUFFIX ".requires"
std::shared_ptr<CmdlineRedactionRule> CmdlineRedactionRule::LoadFromFile(const std::string& path) {
using namespace std::string_literals;
static auto invalid_name_chars = R"inv(,/"'`$%&<>?{}[]|\)inv"s;
Config config;
try {
config.Load(path);
std::string name = Basename(path, CONFIG_SUFFIX);
std::string file_name = name + CONFIG_SUFFIX;
if (config.HasKey("name")) {
name = config.GetString("name");
}
if (name.find_first_of(invalid_name_chars) != std::string::npos) {
Logger::Error("CmdlineRedactionRule::LoadFromFile(%s): Name (%s) contains invalid characters (%s)", path.c_str(), name.c_str(), invalid_name_chars.c_str());
return nullptr;
}
std::string replacement_char = "*";
if (config.HasKey("replacement_char")) {
replacement_char = config.GetString("replacement_char");
if (replacement_char.length() > 1) {
replacement_char.resize(1);
Logger::Warn("CmdlineRedactionRule::LoadFromFile(%s): Configured replacement_char (%s) is too long, truncating to 1 char", path.c_str(), replacement_char.c_str());
}
}
if (!config.HasKey("regex")) {
Logger::Error("CmdlineRedactionRule::LoadFromFile(%s): Config is missing the 'regex' value", path.c_str());
return nullptr;
}
std::string regex = config.GetString("regex");
auto ret = std::make_shared<CmdlineRedactionRule>(file_name, name, regex, replacement_char[0]);
if (!ret->CompiledOK()) {
Logger::Error("CmdlineRedactionRule::LoadFromFile(%s): Failed to load: Invalid regex: %s", path.c_str(), ret->CompileError().c_str());
return nullptr;
}
return ret;
} catch (std::exception& ex) {
Logger::Error("CmdlineRedactionRule::LoadFromFile(%s): Failed to load: %s", path.c_str(), ex.what());
}
return nullptr;
}
bool CmdlineRedactionRule::Apply(std::string& cmdline) const {
if (cmdline.empty()) {
return false;
}
auto res = false;
size_t num_groups = _regex.NumberOfCapturingGroups() + 1;
re2::StringPiece groups[num_groups];
re2::StringPiece text(cmdline);
size_t idx = 0;
size_t end = text.size();
while (idx < end) {
if (!_regex.Match(text, idx, end, re2::RE2::UNANCHORED, &groups[0], num_groups)) {
break;
}
for (int i = 1; i < num_groups; ++i) {
if (groups[i].data() != nullptr) {
char * start = cmdline.data() + (groups[i].data() - text.data());
std::fill(start, start + groups[i].size(), _replacement_char);
}
}
res = true;
idx += groups[0].size();
}
return res;
}
void CmdlineRedactor::AddRule(std::shared_ptr<const CmdlineRedactionRule>& rule) {
std::lock_guard<std::mutex> _lock(_mutex);
_rule_names.emplace(rule->Name());
_rules.emplace_back(rule);
}
bool CmdlineRedactor::LoadFromDir(const std::string& dir, bool require_only_root) {
std::unordered_set<std::string> new_rule_names;
std::unordered_set<std::string> new_required_rule_files;
std::unordered_set<std::string> new_missing_rule_files;
std::vector<std::shared_ptr<const CmdlineRedactionRule>> new_rules;
if (!PathExists(dir)) {
return true;
}
if (require_only_root && !IsOnlyRootWritable(dir)) {
Logger::Error("CmdlineRedactor::LoadFromDir(%s): Dir is not secure, it is writable by non-root users. Redaction rules will not be loaded.", dir.c_str());
return true;
}
std::vector<std::string> files;
try {
files = GetDirList(dir);
} catch (std::exception& ex) {
Logger::Error("CmdlineRedactor::LoadFromDir(%s): Failed to read dir: %s", dir.c_str(), ex.what());
return true;
}
std::unordered_set<std::string> loaded_rule_files;
std::sort(files.begin(), files.end());
for (auto& name: files) {
std::string path = dir + "/" + name;
if (require_only_root && !IsOnlyRootWritable(path)) {
Logger::Error("CmdlineRedactor::LoadFromDir(%s): File (%s) is not secure, it is writable by non-root users. It will not be loaded.", dir.c_str(), name.c_str());
continue;
}
if (ends_with(name, CONFIG_SUFFIX)) {
auto rule = CmdlineRedactionRule::LoadFromFile(dir + "/" + name);
if (!rule) {
Logger::Warn("Excluding (%s/%s) due to errors", dir.c_str(), name.c_str());
continue;
}
// Make sure rule names are unique
auto base_name = rule->Name();
int rnum = 1;
while (new_rule_names.count(rule->Name())) {
rule->SetName(base_name+std::to_string(rnum));
rnum += 1;
}
if (rule) {
new_rules.emplace_back(rule);
}
loaded_rule_files.emplace(name);
} else if (ends_with(name, REQUIRES_SUFFIX)) {
std::vector<std::string> lines;
try {
lines = ReadFile(dir+"/"+name);
} catch (std::exception& ex) {
Logger::Error("Encountered error while trying to read %s/%s: %s", dir.c_str(), name.c_str(), ex.what());
}
for (auto& line : lines) {
auto name = trim_whitespace(line);
if (!starts_with(line, "#")) {
if (!ends_with(name, CONFIG_SUFFIX)) {
name = name + CONFIG_SUFFIX;
}
new_required_rule_files.emplace(name);
}
}
}
}
for (auto& name : new_required_rule_files) {
if (loaded_rule_files.count(name) == 0) {
new_missing_rule_files.emplace(name);
Logger::Error("Required redaction rule file %s is missing", name.c_str());
}
}
std::lock_guard<std::mutex> _lock(_mutex);
_rule_names = new_rule_names;
_required_rule_files = new_required_rule_files;
_missing_rule_files = new_missing_rule_files;
_rules = new_rules;
return _missing_rule_files.empty();
}
std::vector<std::string> CmdlineRedactor::GetMissingRules() {
std::lock_guard<std::mutex> _lock(_mutex);
std::vector<std::string> missing;
for (auto& name : _missing_rule_files) {
missing.emplace_back(name);
}
std::sort(missing.begin(), missing.end());
return missing;
}
std::vector<std::shared_ptr<const CmdlineRedactionRule>> CmdlineRedactor::GetRules() {
std::lock_guard<std::mutex> _lock(_mutex);
return _rules;
}
const std::string CmdlineRedactor::REDACT_RULE_MISSING_NAME = "*Missing Required*";
const std::string CmdlineRedactor::REDACT_RULE_MISSING_TEXT = "**** Entire cmdline redacted due to missing required redaction rules ****";
bool CmdlineRedactor::ApplyRules(std::string& cmdline, std::string& rule_names) {
std::lock_guard<std::mutex> _lock(_mutex);
rule_names.resize(0);
if (!_missing_rule_files.empty()) {
cmdline = REDACT_RULE_MISSING_TEXT;
rule_names = REDACT_RULE_MISSING_NAME;
return true;
}
auto res = false;
for (auto& rule: _rules) {
if (rule->Apply(cmdline)) {
if (!rule_names.empty()) {
rule_names.push_back(',');
}
rule_names.append(rule->Name());
res = true;
}
}
return res;
}