| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 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 <sys/stat.h> | 
 | 18 | #include <sys/syscall.h> | 
 | 19 |  | 
| Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 20 | #include <android-base/file.h> | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 21 | #include <android-base/logging.h> | 
 | 22 | #include <android-base/unique_fd.h> | 
 | 23 |  | 
 | 24 | #include <modprobe/modprobe.h> | 
 | 25 |  | 
| Steve Muckle | 373a3ca | 2019-12-06 17:08:09 -0800 | [diff] [blame] | 26 | std::string Modprobe::GetKernelCmdline(void) { | 
 | 27 |     std::string cmdline; | 
 | 28 |     if (!android::base::ReadFileToString("/proc/cmdline", &cmdline)) { | 
 | 29 |         return ""; | 
 | 30 |     } | 
 | 31 |     return cmdline; | 
 | 32 | } | 
 | 33 |  | 
| Steve Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 34 | bool Modprobe::Insmod(const std::string& path_name, const std::string& parameters) { | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 35 |     android::base::unique_fd fd( | 
 | 36 |             TEMP_FAILURE_RETRY(open(path_name.c_str(), O_RDONLY | O_NOFOLLOW | O_CLOEXEC))); | 
 | 37 |     if (fd == -1) { | 
| Elliott Hughes | 1ab2c02 | 2020-06-16 11:48:28 -0700 | [diff] [blame] | 38 |         PLOG(ERROR) << "Could not open module '" << path_name << "'"; | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 39 |         return false; | 
 | 40 |     } | 
 | 41 |  | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 42 |     auto canonical_name = MakeCanonical(path_name); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 43 |     std::string options = ""; | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 44 |     auto options_iter = module_options_.find(canonical_name); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 45 |     if (options_iter != module_options_.end()) { | 
 | 46 |         options = options_iter->second; | 
 | 47 |     } | 
| Steve Muckle | 13700a6 | 2019-07-31 09:59:48 -0700 | [diff] [blame] | 48 |     if (!parameters.empty()) { | 
 | 49 |         options = options + " " + parameters; | 
 | 50 |     } | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 51 |  | 
| Elliott Hughes | 1ab2c02 | 2020-06-16 11:48:28 -0700 | [diff] [blame] | 52 |     LOG(INFO) << "Loading module " << path_name << " with args '" << options << "'"; | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 53 |     int ret = syscall(__NR_finit_module, fd.get(), options.c_str(), 0); | 
 | 54 |     if (ret != 0) { | 
 | 55 |         if (errno == EEXIST) { | 
 | 56 |             // Module already loaded | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 57 |             std::lock_guard guard(module_loaded_lock_); | 
 | 58 |             module_loaded_paths_.emplace(path_name); | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 59 |             module_loaded_.emplace(canonical_name); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 60 |             return true; | 
 | 61 |         } | 
| Elliott Hughes | 1ab2c02 | 2020-06-16 11:48:28 -0700 | [diff] [blame] | 62 |         PLOG(ERROR) << "Failed to insmod '" << path_name << "' with args '" << options << "'"; | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 63 |         return false; | 
 | 64 |     } | 
 | 65 |  | 
 | 66 |     LOG(INFO) << "Loaded kernel module " << path_name; | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 67 |     std::lock_guard guard(module_loaded_lock_); | 
 | 68 |     module_loaded_paths_.emplace(path_name); | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 69 |     module_loaded_.emplace(canonical_name); | 
| Steve Muckle | b0c4881 | 2020-05-29 16:30:33 -0700 | [diff] [blame] | 70 |     module_count_++; | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 71 |     return true; | 
 | 72 | } | 
 | 73 |  | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 74 | bool Modprobe::Rmmod(const std::string& module_name) { | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 75 |     auto canonical_name = MakeCanonical(module_name); | 
 | 76 |     int ret = syscall(__NR_delete_module, canonical_name.c_str(), O_NONBLOCK); | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 77 |     if (ret != 0) { | 
 | 78 |         PLOG(ERROR) << "Failed to remove module '" << module_name << "'"; | 
 | 79 |         return false; | 
 | 80 |     } | 
| Chungkai | c60300a | 2021-02-03 20:30:07 -0800 | [diff] [blame] | 81 |     std::lock_guard guard(module_loaded_lock_); | 
| Mark Salyzyn | 8c10519 | 2019-10-29 08:38:55 -0700 | [diff] [blame] | 82 |     module_loaded_.erase(canonical_name); | 
| Steve Muckle | bb58b01 | 2019-07-30 11:58:11 -0700 | [diff] [blame] | 83 |     return true; | 
 | 84 | } | 
 | 85 |  | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 86 | bool Modprobe::ModuleExists(const std::string& module_name) { | 
| Kelvin Zhang | db15b6f | 2023-05-03 15:28:39 -0700 | [diff] [blame] | 87 |     struct stat fileStat {}; | 
| Mark Salyzyn | 703fb74 | 2020-06-15 11:51:59 -0700 | [diff] [blame] | 88 |     if (blocklist_enabled && module_blocklist_.count(module_name)) { | 
 | 89 |         LOG(INFO) << "module " << module_name << " is blocklisted"; | 
| Steve Muckle | e31f840 | 2019-07-31 14:34:52 -0700 | [diff] [blame] | 90 |         return false; | 
 | 91 |     } | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 92 |     auto deps = GetDependencies(module_name); | 
 | 93 |     if (deps.empty()) { | 
 | 94 |         // missing deps can happen in the case of an alias | 
 | 95 |         return false; | 
 | 96 |     } | 
 | 97 |     if (stat(deps.front().c_str(), &fileStat)) { | 
| Kelvin Zhang | db15b6f | 2023-05-03 15:28:39 -0700 | [diff] [blame] | 98 |         PLOG(INFO) << "module " << module_name << " can't be loaded; can't access " << deps.front(); | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 99 |         return false; | 
 | 100 |     } | 
 | 101 |     if (!S_ISREG(fileStat.st_mode)) { | 
| Steve Muckle | ded44c0 | 2019-08-01 16:03:02 -0700 | [diff] [blame] | 102 |         LOG(INFO) << "module " << module_name << " is not a regular file"; | 
| Steve Muckle | 18b981e | 2019-04-15 17:43:02 -0700 | [diff] [blame] | 103 |         return false; | 
 | 104 |     } | 
 | 105 |     return true; | 
 | 106 | } |