-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathusermode-helper_lib.cc
167 lines (137 loc) · 4.89 KB
/
usermode-helper_lib.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
// Copyright 2018 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <base/logging.h>
#include <base/strings/string_util.h>
#include "init/usermode-helper.h"
namespace usermode_helper {
namespace {
bool IsTestCaseFlag(const char* arg) {
return std::string_view(arg) == "--core2md_failure" ||
std::string_view(arg) == "--directory_failure";
}
// Processes core dumps from the kernel when a crash is detected.
// Controlled via /proc/sys/kernel/core_pattern.
bool ValidateCrashReporter(int argc, const char* argv[]) {
// Allow normal crash reporting. Most invocations will be this.
if (argc == 2 &&
base::StartsWith(argv[1], "--user=", base::CompareCase::SENSITIVE)) {
return true;
}
// Allow early crash reporting invocation.
if (argc == 4 && std::string_view(argv[1]) == "--early" &&
std::string_view(argv[2]) == "--log_to_stderr" &&
base::StartsWith(argv[3], "--user=", base::CompareCase::SENSITIVE)) {
return true;
}
// Allow for crash-reporter crash testing invocations.
if (argc == 3 &&
base::StartsWith(argv[1], "--user=", base::CompareCase::SENSITIVE) &&
std::string_view(argv[2]) == "--crash_test") {
return true;
}
// Allow for log-collection-failure tests. Currently the test is being
// transitioned from autotest to Tast, having 2 different versions.
// TODO(crbug.com/1040335): Remove the former one after removing the
// logging_UserCrash autotest.
if (argc == 3 &&
base::StartsWith(argv[1], "--user=", base::CompareCase::SENSITIVE) &&
IsTestCaseFlag(argv[2])) {
return true;
}
// Disallow all other invocations.
LOG(ERROR) << "crash_reporter: unknown invocation";
return false;
}
// Automatic module loading when kernel code calls request_module().
// Controlled via /proc/sys/kernel/modprobe.
bool ValidateModprobe(int argc, const char* argv[]) {
// The kernel has loaded modules with the form `modprobe -q -- modname` since
// at least linux-2.6.12. We'll enforce that until the kernel changes, but it
// rarely does, so maybe it's fine to be lazy.
if (argc != 4) {
LOG(ERROR) << "modprobe: argc must be 4";
return false;
}
if (strcmp(argv[1], "-q") != 0) {
LOG(ERROR) << "modprobe: argv[1] must be -q";
return false;
}
if (strcmp(argv[2], "--") != 0) {
LOG(ERROR) << "modprobe: argv[2] must be --";
return false;
}
// We allow the last arg to be anything since the -- marker told modprobe to
// parse it exactly as a module name and not an option.
return true;
}
// When kernel code poweroffs the system by calling orderly_poweroff().
// This is not related to userspace calling `poweroff` or uses the reboot
// syscall.
// Controlled via /proc/sys/kernel/poweroff_cmd.
bool ValidatePoweroff(int argc, const char* argv[]) {
if (argc != 1) {
LOG(ERROR) << "poweroff: argc must be 1";
return false;
}
return true;
}
// When kernel code reboots the system by calling orderly_reboot().
// This is not related to userspace calling `reboot` or uses the reboot
// syscall.
bool ValidateReboot(int argc, const char* argv[]) {
if (argc != 1) {
LOG(ERROR) << "reboot: argc must be 1";
return false;
}
return true;
}
// When the kernel needs access to a key as part of the kernel keyring.
bool ValidateRequestKey(int argc, const char* argv[]) {
// The kernel always executes this as:
// /sbin/request-key <op> <key> <uid> <gid> <keyring> <keyring> <keyring>
if (argc != 8) {
LOG(ERROR) << "request-key: argc must be 8";
return false;
}
// Don't allow any command line options.
for (size_t i = 1; i < argc; ++i) {
const std::string_view arg(argv[i]);
if (arg[0] == '-') {
LOG(ERROR) << "request-key: options not allowed: " << arg;
return false;
}
}
return true;
}
bool ValidateBridgeStp(int argc, const char* argv[]) {
if (argc != 3) {
LOG(ERROR) << "bridge-stp: argc must be 3";
return false;
}
if (strcmp(argv[2], "start") != 0 && strcmp(argv[2], "stop") != 0) {
LOG(ERROR) << "bridge-stp: argv[2] must be start or stop";
return false;
}
return true;
}
} // namespace
// Whether the arguments to the program are permitted.
bool ValidateProgramArgs(int argc, const char* argv[]) {
const std::string_view prog(argv[0]);
if (prog == "/sbin/crash_reporter")
return ValidateCrashReporter(argc, argv);
else if (prog == "/sbin/modprobe")
return ValidateModprobe(argc, argv);
else if (prog == "/sbin/poweroff")
return ValidatePoweroff(argc, argv);
else if (prog == "/sbin/reboot")
return ValidateReboot(argc, argv);
else if (prog == "/sbin/request-key")
return ValidateRequestKey(argc, argv);
else if (prog == "/sbin/bridge-stp")
return ValidateBridgeStp(argc, argv);
LOG(ERROR) << "program not permitted: " << argv[0];
return false;
}
} // namespace usermode_helper