1 /*
2 * Copyright (C) 2018 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <modprobe/modprobe.h>
18
19 #include <fnmatch.h>
20 #include <sys/stat.h>
21 #include <sys/syscall.h>
22
23 #include <algorithm>
24 #include <set>
25 #include <string>
26 #include <vector>
27
28 #include <android-base/chrono_utils.h>
29 #include <android-base/file.h>
30 #include <android-base/logging.h>
31 #include <android-base/strings.h>
32 #include <android-base/unique_fd.h>
33
MakeCanonical(const std::string & module_path)34 std::string Modprobe::MakeCanonical(const std::string& module_path) {
35 auto start = module_path.find_last_of('/');
36 if (start == std::string::npos) {
37 start = 0;
38 } else {
39 start += 1;
40 }
41 auto end = module_path.size();
42 if (android::base::EndsWith(module_path, ".ko")) {
43 end -= 3;
44 }
45 if ((end - start) <= 1) {
46 LOG(ERROR) << "malformed module name: " << module_path;
47 return "";
48 }
49 std::string module_name = module_path.substr(start, end - start);
50 // module names can have '-', but their file names will have '_'
51 std::replace(module_name.begin(), module_name.end(), '-', '_');
52 return module_name;
53 }
54
ParseDepCallback(const std::string & base_path,const std::vector<std::string> & args)55 bool Modprobe::ParseDepCallback(const std::string& base_path,
56 const std::vector<std::string>& args) {
57 std::vector<std::string> deps;
58 std::string prefix = "";
59
60 // Set first item as our modules path
61 std::string::size_type pos = args[0].find(':');
62 if (args[0][0] != '/') {
63 prefix = base_path + "/";
64 }
65 if (pos != std::string::npos) {
66 deps.emplace_back(prefix + args[0].substr(0, pos));
67 } else {
68 LOG(ERROR) << "dependency lines must start with name followed by ':'";
69 return false;
70 }
71
72 // Remaining items are dependencies of our module
73 for (auto arg = args.begin() + 1; arg != args.end(); ++arg) {
74 if ((*arg)[0] != '/') {
75 prefix = base_path + "/";
76 } else {
77 prefix = "";
78 }
79 deps.push_back(prefix + *arg);
80 }
81
82 std::string canonical_name = MakeCanonical(args[0].substr(0, pos));
83 if (canonical_name.empty()) {
84 return false;
85 }
86 this->module_deps_[canonical_name] = deps;
87
88 return true;
89 }
90
ParseAliasCallback(const std::vector<std::string> & args)91 bool Modprobe::ParseAliasCallback(const std::vector<std::string>& args) {
92 auto it = args.begin();
93 const std::string& type = *it++;
94
95 if (type != "alias") {
96 LOG(ERROR) << "non-alias line encountered in modules.alias, found " << type;
97 return false;
98 }
99
100 if (args.size() != 3) {
101 LOG(ERROR) << "alias lines in modules.alias must have 3 entries, not " << args.size();
102 return false;
103 }
104
105 const std::string& alias = *it++;
106 const std::string& module_name = *it++;
107 this->module_aliases_.emplace_back(alias, module_name);
108
109 return true;
110 }
111
ParseSoftdepCallback(const std::vector<std::string> & args)112 bool Modprobe::ParseSoftdepCallback(const std::vector<std::string>& args) {
113 auto it = args.begin();
114 const std::string& type = *it++;
115 std::string state = "";
116
117 if (type != "softdep") {
118 LOG(ERROR) << "non-softdep line encountered in modules.softdep, found " << type;
119 return false;
120 }
121
122 if (args.size() < 4) {
123 LOG(ERROR) << "softdep lines in modules.softdep must have at least 4 entries";
124 return false;
125 }
126
127 const std::string& module = *it++;
128 while (it != args.end()) {
129 const std::string& token = *it++;
130 if (token == "pre:" || token == "post:") {
131 state = token;
132 continue;
133 }
134 if (state == "") {
135 LOG(ERROR) << "malformed modules.softdep at token " << token;
136 return false;
137 }
138 if (state == "pre:") {
139 this->module_pre_softdep_.emplace_back(module, token);
140 } else {
141 this->module_post_softdep_.emplace_back(module, token);
142 }
143 }
144
145 return true;
146 }
147
ParseLoadCallback(const std::vector<std::string> & args)148 bool Modprobe::ParseLoadCallback(const std::vector<std::string>& args) {
149 auto it = args.begin();
150 const std::string& module = *it++;
151
152 const std::string& canonical_name = MakeCanonical(module);
153 if (canonical_name.empty()) {
154 return false;
155 }
156 this->module_load_.emplace_back(canonical_name);
157
158 return true;
159 }
160
ParseOptionsCallback(const std::vector<std::string> & args)161 bool Modprobe::ParseOptionsCallback(const std::vector<std::string>& args) {
162 auto it = args.begin();
163 const std::string& type = *it++;
164
165 if (type != "options") {
166 LOG(ERROR) << "non-options line encountered in modules.options";
167 return false;
168 }
169
170 if (args.size() < 2) {
171 LOG(ERROR) << "lines in modules.options must have at least 2 entries, not " << args.size();
172 return false;
173 }
174
175 const std::string& module = *it++;
176 std::string options = "";
177
178 const std::string& canonical_name = MakeCanonical(module);
179 if (canonical_name.empty()) {
180 return false;
181 }
182
183 while (it != args.end()) {
184 options += *it++;
185 if (it != args.end()) {
186 options += " ";
187 }
188 }
189
190 auto [unused, inserted] = this->module_options_.emplace(canonical_name, options);
191 if (!inserted) {
192 LOG(ERROR) << "multiple options lines present for module " << module;
193 return false;
194 }
195 return true;
196 }
197
ParseBlocklistCallback(const std::vector<std::string> & args)198 bool Modprobe::ParseBlocklistCallback(const std::vector<std::string>& args) {
199 auto it = args.begin();
200 const std::string& type = *it++;
201
202 if (type != "blocklist") {
203 LOG(ERROR) << "non-blocklist line encountered in modules.blocklist";
204 return false;
205 }
206
207 if (args.size() != 2) {
208 LOG(ERROR) << "lines in modules.blocklist must have exactly 2 entries, not " << args.size();
209 return false;
210 }
211
212 const std::string& module = *it++;
213
214 const std::string& canonical_name = MakeCanonical(module);
215 if (canonical_name.empty()) {
216 return false;
217 }
218 this->module_blocklist_.emplace(canonical_name);
219
220 return true;
221 }
222
ParseCfg(const std::string & cfg,std::function<bool (const std::vector<std::string> &)> f)223 void Modprobe::ParseCfg(const std::string& cfg,
224 std::function<bool(const std::vector<std::string>&)> f) {
225 std::string cfg_contents;
226 if (!android::base::ReadFileToString(cfg, &cfg_contents, false)) {
227 return;
228 }
229
230 std::vector<std::string> lines = android::base::Split(cfg_contents, "\n");
231 for (const std::string line : lines) {
232 if (line.empty() || line[0] == '#') {
233 continue;
234 }
235 const std::vector<std::string> args = android::base::Split(line, " ");
236 if (args.empty()) continue;
237 f(args);
238 }
239 return;
240 }
241
AddOption(const std::string & module_name,const std::string & option_name,const std::string & value)242 void Modprobe::AddOption(const std::string& module_name, const std::string& option_name,
243 const std::string& value) {
244 auto canonical_name = MakeCanonical(module_name);
245 auto options_iter = module_options_.find(canonical_name);
246 auto option_str = option_name + "=" + value;
247 if (options_iter != module_options_.end()) {
248 options_iter->second = options_iter->second + " " + option_str;
249 } else {
250 module_options_.emplace(canonical_name, option_str);
251 }
252 }
253
ParseKernelCmdlineOptions(void)254 void Modprobe::ParseKernelCmdlineOptions(void) {
255 std::string cmdline = GetKernelCmdline();
256 std::string module_name = "";
257 std::string option_name = "";
258 std::string value = "";
259 bool in_module = true;
260 bool in_option = false;
261 bool in_value = false;
262 bool in_quotes = false;
263 int start = 0;
264
265 for (int i = 0; i < cmdline.size(); i++) {
266 if (cmdline[i] == '"') {
267 in_quotes = !in_quotes;
268 }
269
270 if (in_quotes) continue;
271
272 if (cmdline[i] == ' ') {
273 if (in_value) {
274 value = cmdline.substr(start, i - start);
275 if (!module_name.empty() && !option_name.empty()) {
276 AddOption(module_name, option_name, value);
277 }
278 }
279 module_name = "";
280 option_name = "";
281 value = "";
282 in_value = false;
283 start = i + 1;
284 in_module = true;
285 continue;
286 }
287
288 if (cmdline[i] == '.') {
289 if (in_module) {
290 module_name = cmdline.substr(start, i - start);
291 start = i + 1;
292 in_module = false;
293 }
294 in_option = true;
295 continue;
296 }
297
298 if (cmdline[i] == '=') {
299 if (in_option) {
300 option_name = cmdline.substr(start, i - start);
301 start = i + 1;
302 in_option = false;
303 }
304 in_value = true;
305 continue;
306 }
307 }
308 if (in_value && !in_quotes) {
309 value = cmdline.substr(start, cmdline.size() - start);
310 if (!module_name.empty() && !option_name.empty()) {
311 AddOption(module_name, option_name, value);
312 }
313 }
314 }
315
Modprobe(const std::vector<std::string> & base_paths,const std::string load_file,bool use_blocklist)316 Modprobe::Modprobe(const std::vector<std::string>& base_paths, const std::string load_file,
317 bool use_blocklist)
318 : blocklist_enabled(use_blocklist) {
319 using namespace std::placeholders;
320
321 for (const auto& base_path : base_paths) {
322 auto alias_callback = std::bind(&Modprobe::ParseAliasCallback, this, _1);
323 ParseCfg(base_path + "/modules.alias", alias_callback);
324
325 auto dep_callback = std::bind(&Modprobe::ParseDepCallback, this, base_path, _1);
326 ParseCfg(base_path + "/modules.dep", dep_callback);
327
328 auto softdep_callback = std::bind(&Modprobe::ParseSoftdepCallback, this, _1);
329 ParseCfg(base_path + "/modules.softdep", softdep_callback);
330
331 auto load_callback = std::bind(&Modprobe::ParseLoadCallback, this, _1);
332 ParseCfg(base_path + "/" + load_file, load_callback);
333
334 auto options_callback = std::bind(&Modprobe::ParseOptionsCallback, this, _1);
335 ParseCfg(base_path + "/modules.options", options_callback);
336
337 auto blocklist_callback = std::bind(&Modprobe::ParseBlocklistCallback, this, _1);
338 ParseCfg(base_path + "/modules.blocklist", blocklist_callback);
339 }
340
341 ParseKernelCmdlineOptions();
342 }
343
GetDependencies(const std::string & module)344 std::vector<std::string> Modprobe::GetDependencies(const std::string& module) {
345 auto it = module_deps_.find(module);
346 if (it == module_deps_.end()) {
347 return {};
348 }
349 return it->second;
350 }
351
InsmodWithDeps(const std::string & module_name,const std::string & parameters)352 bool Modprobe::InsmodWithDeps(const std::string& module_name, const std::string& parameters) {
353 if (module_name.empty()) {
354 LOG(ERROR) << "Need valid module name, given: " << module_name;
355 return false;
356 }
357
358 auto dependencies = GetDependencies(module_name);
359 if (dependencies.empty()) {
360 LOG(ERROR) << "Module " << module_name << " not in dependency file";
361 return false;
362 }
363
364 // load module dependencies in reverse order
365 for (auto dep = dependencies.rbegin(); dep != dependencies.rend() - 1; ++dep) {
366 LOG(VERBOSE) << "Loading hard dep for '" << module_name << "': " << *dep;
367 if (!LoadWithAliases(*dep, true)) {
368 return false;
369 }
370 }
371
372 // try to load soft pre-dependencies
373 for (const auto& [module, softdep] : module_pre_softdep_) {
374 if (module_name == module) {
375 LOG(VERBOSE) << "Loading soft pre-dep for '" << module << "': " << softdep;
376 LoadWithAliases(softdep, false);
377 }
378 }
379
380 // load target module itself with args
381 if (!Insmod(dependencies[0], parameters)) {
382 return false;
383 }
384
385 // try to load soft post-dependencies
386 for (const auto& [module, softdep] : module_post_softdep_) {
387 if (module_name == module) {
388 LOG(VERBOSE) << "Loading soft post-dep for '" << module << "': " << softdep;
389 LoadWithAliases(softdep, false);
390 }
391 }
392
393 return true;
394 }
395
LoadWithAliases(const std::string & module_name,bool strict,const std::string & parameters)396 bool Modprobe::LoadWithAliases(const std::string& module_name, bool strict,
397 const std::string& parameters) {
398 auto canonical_name = MakeCanonical(module_name);
399 if (module_loaded_.count(canonical_name)) {
400 return true;
401 }
402
403 std::set<std::string> modules_to_load = {canonical_name};
404 bool module_loaded = false;
405
406 // use aliases to expand list of modules to load (multiple modules
407 // may alias themselves to the requested name)
408 for (const auto& [alias, aliased_module] : module_aliases_) {
409 if (fnmatch(alias.c_str(), module_name.c_str(), 0) != 0) continue;
410 LOG(VERBOSE) << "Found alias for '" << module_name << "': '" << aliased_module;
411 if (module_loaded_.count(MakeCanonical(aliased_module))) continue;
412 modules_to_load.emplace(aliased_module);
413 }
414
415 // attempt to load all modules aliased to this name
416 for (const auto& module : modules_to_load) {
417 if (!ModuleExists(module)) continue;
418 if (InsmodWithDeps(module, parameters)) module_loaded = true;
419 }
420
421 if (strict && !module_loaded) {
422 LOG(ERROR) << "LoadWithAliases was unable to load " << module_name;
423 return false;
424 }
425 return true;
426 }
427
IsBlocklisted(const std::string & module_name)428 bool Modprobe::IsBlocklisted(const std::string& module_name) {
429 if (!blocklist_enabled) return false;
430
431 auto canonical_name = MakeCanonical(module_name);
432 auto dependencies = GetDependencies(canonical_name);
433 for (auto dep = dependencies.begin(); dep != dependencies.end(); ++dep) {
434 if (module_blocklist_.count(MakeCanonical(*dep))) return true;
435 }
436
437 return module_blocklist_.count(canonical_name) > 0;
438 }
439
LoadListedModules(bool strict)440 bool Modprobe::LoadListedModules(bool strict) {
441 auto ret = true;
442 for (const auto& module : module_load_) {
443 if (!LoadWithAliases(module, true)) {
444 if (IsBlocklisted(module)) continue;
445 ret = false;
446 if (strict) break;
447 }
448 }
449 return ret;
450 }
451
Remove(const std::string & module_name)452 bool Modprobe::Remove(const std::string& module_name) {
453 auto dependencies = GetDependencies(MakeCanonical(module_name));
454 for (auto dep = dependencies.begin(); dep != dependencies.end(); ++dep) {
455 Rmmod(*dep);
456 }
457 Rmmod(module_name);
458 return true;
459 }
460
ListModules(const std::string & pattern)461 std::vector<std::string> Modprobe::ListModules(const std::string& pattern) {
462 std::vector<std::string> rv;
463 for (const auto& [module, deps] : module_deps_) {
464 // Attempt to match both the canonical module name and the module filename.
465 if (!fnmatch(pattern.c_str(), module.c_str(), 0)) {
466 rv.emplace_back(module);
467 } else if (!fnmatch(pattern.c_str(), basename(deps[0].c_str()), 0)) {
468 rv.emplace_back(deps[0]);
469 }
470 }
471 return rv;
472 }
473
GetAllDependencies(const std::string & module,std::vector<std::string> * pre_dependencies,std::vector<std::string> * dependencies,std::vector<std::string> * post_dependencies)474 bool Modprobe::GetAllDependencies(const std::string& module,
475 std::vector<std::string>* pre_dependencies,
476 std::vector<std::string>* dependencies,
477 std::vector<std::string>* post_dependencies) {
478 std::string canonical_name = MakeCanonical(module);
479 if (pre_dependencies) {
480 pre_dependencies->clear();
481 for (const auto& [it_module, it_softdep] : module_pre_softdep_) {
482 if (canonical_name == it_module) {
483 pre_dependencies->emplace_back(it_softdep);
484 }
485 }
486 }
487 if (dependencies) {
488 dependencies->clear();
489 auto hard_deps = GetDependencies(canonical_name);
490 if (hard_deps.empty()) {
491 return false;
492 }
493 for (auto dep = hard_deps.rbegin(); dep != hard_deps.rend(); dep++) {
494 dependencies->emplace_back(*dep);
495 }
496 }
497 if (post_dependencies) {
498 for (const auto& [it_module, it_softdep] : module_post_softdep_) {
499 if (canonical_name == it_module) {
500 post_dependencies->emplace_back(it_softdep);
501 }
502 }
503 }
504 return true;
505 }
506