-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathusb_bouncer.cc
490 lines (431 loc) · 16.1 KB
/
usb_bouncer.cc
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
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
// Copyright 2018 The ChromiumOS Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <sys/mount.h>
#include <cstdio>
#include <cstdlib>
#include <base/command_line.h>
#include <base/files/file_path.h>
#include <base/files/file_util.h>
#include <base/logging.h>
#include <base/strings/string_number_conversions.h>
#include <brillo/flag_helper.h>
#include <brillo/syslog_logging.h>
#include <libminijail.h>
#include <scoped_minijail.h>
#include "usb_bouncer/entry_manager.h"
#include "usb_bouncer/util.h"
using usb_bouncer::CanChown;
using usb_bouncer::Daemonize;
using usb_bouncer::DevpathToRuleCallback;
using usb_bouncer::EntryManager;
using usb_bouncer::GetRuleFromDevPath;
using usb_bouncer::kDBusPath;
namespace {
static constexpr char kUsageMessage[] = R"(Usage:
cleanup - removes stale allow-list entries.
genrules - writes the generated rules configuration and to stdout.
report_error - handles kernel errors reported with uevents.
udev (add|remove) <devpath> - handles a udev device event.
userlogin - add current entries to user allow-list.
)";
enum class SeccompEnforcement {
kEnabled,
kDisabled,
};
enum class ForkConfig {
kDoubleFork,
kDisabled,
};
enum class PrivilegeLevel {
kDefault,
kMetricsOnly,
};
class Configuration {
public:
const SeccompEnforcement seccomp;
const ForkConfig fork_config;
};
static constexpr char kLogPath[] = "/dev/log";
static constexpr char kUMAEventsPath[] = "/var/lib/metrics/uma-events";
static constexpr char kNoLoginPath[] = "/run/nologin";
static constexpr char kStructuredMetricsPath[] = "/var/lib/metrics/structured";
static constexpr char kStructuredMetricsEventsPath[] =
"/var/lib/metrics/structured/events";
void DropPrivileges(const Configuration& config, PrivilegeLevel privileges) {
if (!CanChown()) {
LOG(FATAL) << "This process doesn't have permission to chown.";
}
ScopedMinijail j(minijail_new());
minijail_change_user(j.get(), usb_bouncer::kUsbBouncerUser);
minijail_change_group(j.get(), usb_bouncer::kUsbBouncerGroup);
minijail_inherit_usergroups(j.get());
minijail_no_new_privs(j.get());
if (config.seccomp == SeccompEnforcement::kEnabled) {
minijail_use_seccomp_filter(j.get());
minijail_parse_seccomp_filters(
j.get(), "/usr/share/policy/usb_bouncer-seccomp.policy");
}
minijail_namespace_ipc(j.get());
minijail_namespace_net(j.get());
// If minijail were to run as init, then it would be tracked by udev and
// defeat the purpose of daemonizing. If minijail doesn't run as init, the
// descendant processes will die when daemonizing because there won't be an
// init to keep the pid namespace from closing.
if (config.fork_config == ForkConfig::kDisabled) {
minijail_namespace_pids(j.get());
}
minijail_namespace_uts(j.get());
minijail_namespace_vfs(j.get());
if (minijail_enter_pivot_root(j.get(), "/mnt/empty") != 0) {
PLOG(FATAL) << "minijail_enter_pivot_root() failed";
}
for (const char* path : {"/", "/proc", "/sys"}) {
if (minijail_bind(j.get(), path, path, 0 /*writable*/)) {
PLOG(FATAL) << "minijail_bind('" << path << "') failed";
}
}
if (!base::PathExists(base::FilePath(kLogPath))) {
LOG(WARNING) << "Path '" << kLogPath << "' doesn't exist; "
<< "logging via syslog won't work for this run.";
} else if (minijail_bind(j.get(), kLogPath, kLogPath, 0 /*writable*/)) {
PLOG(FATAL) << "minijail_bind('" << kLogPath << "') failed";
}
// "usb_bouncer genrules" writes to stdout.
minijail_preserve_fd(j.get(), STDOUT_FILENO, STDOUT_FILENO);
minijail_mount_dev(j.get());
minijail_mount_tmp(j.get());
for (const char* path : {"/run", "/var"}) {
if (minijail_mount_with_data(j.get(), "tmpfs", path, "tmpfs",
MS_NOSUID | MS_NOEXEC | MS_NODEV,
"mode=0755,size=10M") != 0) {
PLOG(FATAL) << "minijail_mount_with_data('" << path << "') failed";
}
}
std::string global_db_path("/");
int global_db_path_writeable = 1;
if (privileges == PrivilegeLevel::kMetricsOnly)
global_db_path_writeable = 0;
global_db_path.append(usb_bouncer::kDefaultGlobalDir);
if (minijail_bind(j.get(), global_db_path.c_str(), global_db_path.c_str(),
global_db_path_writeable /*writable*/) != 0) {
PLOG(FATAL) << "minijail_bind('" << global_db_path << "') failed";
}
if (!base::PathExists(base::FilePath(usb_bouncer::kDBusPath))) {
LOG(WARNING) << "Path '" << usb_bouncer::kDBusPath << "' doesn't exist; "
<< "assuming user is not yet logged in to the system.";
} else if (minijail_bind(j.get(), usb_bouncer::kDBusPath,
usb_bouncer::kDBusPath, 0 /*writable*/) != 0) {
PLOG(FATAL) << "minijail_bind('" << usb_bouncer::kDBusPath << "') failed";
}
if (base::PathExists(base::FilePath(kUMAEventsPath)) &&
minijail_bind(j.get(), kUMAEventsPath, kUMAEventsPath, 1 /*writable*/) !=
0) {
PLOG(FATAL) << "minijail_bind('" << kUMAEventsPath << "') failed";
}
if (base::PathExists(base::FilePath(kStructuredMetricsPath)) &&
minijail_bind(j.get(), kStructuredMetricsPath, kStructuredMetricsPath,
1 /*writable*/) != 0) {
PLOG(FATAL) << "minijail_bind('" << kStructuredMetricsPath << "') failed";
}
if (base::PathExists(base::FilePath(kStructuredMetricsEventsPath)) &&
minijail_bind(j.get(), kStructuredMetricsEventsPath,
kStructuredMetricsEventsPath, 1 /*writable*/) != 0) {
PLOG(FATAL) << "minijail_bind('" << kStructuredMetricsEventsPath
<< "') failed";
}
minijail_remount_mode(j.get(), MS_SLAVE);
// minijail_bind was not used because the MS_REC flag is needed.
if (!base::DirectoryExists(base::FilePath(usb_bouncer::kUserDbBaseDir))) {
LOG(WARNING) << "Path '" << usb_bouncer::kUserDbBaseDir
<< "' doesn't exist; userdb will be inaccessible this run.";
} else if (minijail_mount(j.get(), usb_bouncer::kUserDbBaseDir,
usb_bouncer::kUserDbBaseDir, "none",
MS_BIND | MS_REC) != 0) {
PLOG(FATAL) << "minijail_mount('/" << usb_bouncer::kUserDbBaseDir
<< "') failed";
}
minijail_forward_signals(j.get());
pid_t pid = minijail_fork(j.get());
if (pid != 0) {
exit(minijail_wait(j.get()));
}
umask(0077);
}
EntryManager* GetEntryManagerOrDie(const Configuration& config) {
if (!EntryManager::CreateDefaultGlobalDB()) {
LOG(FATAL) << "Unable to create default global DB!";
}
DropPrivileges(config, PrivilegeLevel::kDefault);
EntryManager* entry_manager = EntryManager::GetInstance(GetRuleFromDevPath);
if (!entry_manager) {
LOG(FATAL) << "EntryManager::GetInstance() failed!";
}
return entry_manager;
}
int HandleAuthorizeAll(const Configuration& config,
const std::vector<std::string>& argv) {
if (!argv.empty()) {
LOG(ERROR) << "Invalid options!";
return EXIT_FAILURE;
}
if (!usb_bouncer::AuthorizeAll()) {
LOG(FATAL) << "authorize-all failed!";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int HandleCleanup(const Configuration& config,
const std::vector<std::string>& argv) {
if (!argv.empty()) {
LOG(ERROR) << "Invalid options!";
return EXIT_FAILURE;
}
EntryManager* entry_manager = GetEntryManagerOrDie(config);
if (!entry_manager->GarbageCollect()) {
LOG(ERROR) << "cleanup failed!";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int HandleGenRules(const Configuration& config,
const std::vector<std::string>& argv) {
if (!argv.empty()) {
LOG(ERROR) << "Invalid options!";
return EXIT_FAILURE;
}
EntryManager* entry_manager = GetEntryManagerOrDie(config);
std::string rules = entry_manager->GenerateRules();
if (rules.empty()) {
LOG(ERROR) << "genrules failed!";
return EXIT_FAILURE;
}
printf("%s", rules.c_str());
return EXIT_SUCCESS;
}
int HandleReportError(const Configuration& config,
const std::vector<std::string>& argv) {
if (argv.size() != 3)
return EXIT_FAILURE;
int error_code;
std::string subsystem = argv[0];
std::string devpath = argv[1];
if (!base::StringToInt(argv[2], &error_code))
return EXIT_FAILURE;
// Drop privileges before reading from sysfs.
DropPrivileges(config, PrivilegeLevel::kMetricsOnly);
base::FilePath root_dir("/");
base::FilePath normalized_devpath = root_dir.Append("sys").Append(
usb_bouncer::StripLeadingPathSeparators(devpath));
if (!base::DirectoryExists(normalized_devpath)) {
// If the device sysfs path is no longer available, record the error code.
if (subsystem == "usb")
usb_bouncer::StructuredMetricsHubError(abs(error_code), 0, 0, 0, "", 0);
else if (subsystem == "pci")
usb_bouncer::StructuredMetricsXhciError(abs(error_code), 0);
} else {
if (subsystem == "usb") {
if (base::PathExists(normalized_devpath.Append("bInterfaceClass"))) {
normalized_devpath =
usb_bouncer::GetInterfaceDevice(normalized_devpath);
}
if (normalized_devpath.empty() ||
!base::PathExists(normalized_devpath.Append("bDeviceClass"))) {
return EXIT_FAILURE;
}
LOG(INFO) << "Reporting hub error (" << error_code << ") from "
<< normalized_devpath;
usb_bouncer::StructuredMetricsHubError(
abs(error_code), usb_bouncer::GetVendorId(normalized_devpath),
usb_bouncer::GetProductId(normalized_devpath),
usb_bouncer::GetDeviceClass(normalized_devpath),
usb_bouncer::GetUsbTreePath(normalized_devpath),
usb_bouncer::GetConnectedDuration(normalized_devpath));
} else if (subsystem == "pci") {
LOG(INFO) << "Reporting xHCI error (" << error_code << ") from "
<< normalized_devpath;
usb_bouncer::StructuredMetricsXhciError(
abs(error_code), usb_bouncer::GetPciDeviceClass(normalized_devpath));
}
}
return EXIT_SUCCESS;
}
int HandleUdev(const Configuration& config,
const std::vector<std::string>& argv) {
if (argv.size() < 2) {
LOG(ERROR) << "Invalid options!";
return EXIT_FAILURE;
}
// Ignore all events during shutdown.
if (base::PathExists(base::FilePath(kNoLoginPath))) {
LOG(INFO) << "Skipping udev event because of shutdown.";
return EXIT_SUCCESS;
}
EntryManager::UdevAction action;
if (argv[0] == "add") {
action = EntryManager::UdevAction::kAdd;
} else if (argv[0] == "remove") {
action = EntryManager::UdevAction::kRemove;
} else {
LOG(ERROR) << "Invalid options!";
return EXIT_FAILURE;
}
// We need to drop privileges prior to reading from sysfs so instead of
// calling GetEntryManagerOrDie split it up so the privileges can be dropped
// earlier.
if (!EntryManager::CreateDefaultGlobalDB()) {
LOG(FATAL) << "Unable to create default global DB!";
}
DropPrivileges(config, PrivilegeLevel::kDefault);
// Perform sysfs reads before daemonizing to avoid races.
const std::string& devpath = argv[1];
std::string rule;
if (action == EntryManager::UdevAction::kAdd) {
rule = GetRuleFromDevPath(devpath);
if (rule.empty()) {
LOG(ERROR) << "Unable convert devpath to USBGuard allow-list rule.";
exit(0);
}
}
// Gather data used for device metrics before daemonizing.
bool session_metric_available = false;
usb_bouncer::UsbSessionMetric session_metric;
if (argv.size() == 5) {
base::FilePath root_dir("/");
base::FilePath normalized_devpath = root_dir.Append("sys").Append(
usb_bouncer::StripLeadingPathSeparators(devpath));
session_metric.boot_id = usb_bouncer::GetBootId();
session_metric.system_time = usb_bouncer::GetSystemTime();
session_metric.action = static_cast<int>(action);
session_metric.depth = usb_bouncer::GetUsbTreeDepth(normalized_devpath);
base::StringToInt(argv[2], &session_metric.busnum);
base::StringToInt(argv[3], &session_metric.devnum);
usb_bouncer::GetVidPidFromEnvVar(argv[4], &session_metric.vid,
&session_metric.pid);
session_metric_available = true;
}
// All the information needed from udev and sysfs should be obtained prior to
// this point. Daemonizing here allows usb_bouncer to wait on other system
// services without blocking udev.
if (config.fork_config == ForkConfig::kDoubleFork) {
Daemonize();
}
// Record session metric if it is available.
if (session_metric_available)
usb_bouncer::StructuredMetricsUsbSessionEvent(session_metric);
// The DevpathToRuleCallback here to forwards the result of the sysfs read
// performed before daemonizing.
EntryManager* entry_manager = EntryManager::GetInstance(
[rule, devpath](const std::string& devpath_) -> const std::string {
if (devpath != devpath_) {
LOG(ERROR) << "Got devpath: '" << devpath_ << "' expected '"
<< devpath;
return "";
}
return rule;
});
if (!entry_manager) {
LOG(FATAL) << "EntryManager::GetInstance() failed!";
}
if (!entry_manager->HandleUdev(action, devpath)) {
LOG(ERROR) << "udev failed!";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
int HandleUserLogin(const Configuration& config,
const std::vector<std::string>& argv) {
if (!argv.empty()) {
LOG(ERROR) << "Invalid options!";
return EXIT_FAILURE;
}
EntryManager* entry_manager = GetEntryManagerOrDie(config);
if (!entry_manager->HandleUserLogin()) {
LOG(ERROR) << "userlogin failed!";
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
} // namespace
int main(int argc, char** argv) {
DEFINE_bool(
seccomp, true,
DCHECK_IS_ON()
? "Enable or disable seccomp filtering."
: "Flag is ignored in production, but reported as a crash if false.");
DEFINE_bool(fork, false,
"Daemonizes udev commands with a double fork if enabled.");
brillo::FlagHelper::Init(argc, argv, kUsageMessage);
base::CommandLine::Init(argc, argv);
// Logging may not be ready at early boot in which case it is ok if the logs
// are lost.
int log_flags = brillo::kLogToStderr;
if (base::PathExists(base::FilePath(kLogPath))) {
log_flags |= brillo::kLogToSyslog;
}
brillo::InitLog(log_flags);
base::CommandLine* cl = base::CommandLine::ForCurrentProcess();
std::vector<std::string> args = cl->argv();
// Remove switches.
for (int x = 1; x < args.size();) {
if (args[x].size() >= 2 && args[x].substr(0, 2) == "--") {
args.erase(args.begin() + x);
if (args[x].size() == 2) {
break;
}
} else {
++x;
}
}
if (args.size() < 2) {
LOG(ERROR) << "Invalid options!";
return EXIT_FAILURE;
}
const auto& command = args[1];
auto command_args_start = args.begin() + 2;
SeccompEnforcement seccomp;
if (!FLAGS_seccomp) {
if (DCHECK_IS_ON()) {
seccomp = SeccompEnforcement::kDisabled;
} else {
// Spin off a child to log a crash if --secomp=false is set in production.
pid_t pid = fork();
if (pid < 0) {
PLOG(FATAL) << "Failed to fork()";
}
if (pid == 0) {
LOG(FATAL) << "--seccomp=false set for production code.";
}
seccomp = SeccompEnforcement::kEnabled;
}
} else {
seccomp = SeccompEnforcement::kEnabled;
}
ForkConfig fork_config =
FLAGS_fork ? ForkConfig::kDoubleFork : ForkConfig::kDisabled;
const struct {
const std::string command;
int (*handler)(const Configuration& config,
const std::vector<std::string>& argv);
} command_handlers[] = {
// clang-format off
{"authorize-all", HandleAuthorizeAll},
{"cleanup", HandleCleanup},
{"genrules", HandleGenRules},
{"report_error", HandleReportError},
{"udev", HandleUdev},
{"userlogin", HandleUserLogin},
// clang-format on
};
for (const auto& command_handler : command_handlers) {
if (command_handler.command == command) {
return command_handler.handler(
{seccomp, fork_config},
std::vector<std::string>(command_args_start, args.end()));
}
}
if (command != "help") {
LOG(ERROR) << "Invalid options!";
}
return EXIT_FAILURE;
}