Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2019 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 | |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 17 | #include <stdio.h> |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 18 | |
| 19 | #include <android-base/file.h> |
| 20 | #include <android-base/logging.h> |
| 21 | #include <android-base/properties.h> |
Yi-Yo Chiang | 1305c15 | 2022-08-10 12:53:39 +0800 | [diff] [blame] | 22 | #include <binder/ProcessState.h> |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 23 | #include <fs_mgr_overlayfs.h> |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 24 | #include <libavb_user/libavb_user.h> |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 25 | |
Yi-Yo Chiang | 7fd9d4f | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 26 | using namespace std::string_literals; |
| 27 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 28 | namespace { |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 29 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 30 | #ifdef ALLOW_DISABLE_VERITY |
| 31 | const bool kAllowDisableVerity = true; |
| 32 | #else |
| 33 | const bool kAllowDisableVerity = false; |
| 34 | #endif |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 35 | |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 36 | /* Helper function to get A/B suffix, if any. If the device isn't |
| 37 | * using A/B the empty string is returned. Otherwise either "_a", |
| 38 | * "_b", ... is returned. |
| 39 | */ |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 40 | std::string get_ab_suffix() { |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 41 | return android::base::GetProperty("ro.boot.slot_suffix", ""); |
| 42 | } |
| 43 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 44 | bool is_avb_device_locked() { |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 45 | return android::base::GetProperty("ro.boot.vbmeta.device_state", "") == "locked"; |
| 46 | } |
| 47 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 48 | bool is_debuggable() { |
| 49 | return android::base::GetBoolProperty("ro.debuggable", false); |
| 50 | } |
| 51 | |
| 52 | bool is_using_avb() { |
| 53 | // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by |
| 54 | // contract, androidboot.vbmeta.digest is set by the bootloader |
| 55 | // when using AVB). |
| 56 | return !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty(); |
| 57 | } |
| 58 | |
| 59 | bool overlayfs_setup(bool enable) { |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 60 | auto change = false; |
| 61 | errno = 0; |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 62 | if (enable ? fs_mgr_overlayfs_setup(nullptr, &change) |
| 63 | : fs_mgr_overlayfs_teardown(nullptr, &change)) { |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 64 | if (change) { |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 65 | LOG(INFO) << (enable ? "Enabled" : "Disabled") << " overlayfs"; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 66 | } |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 67 | } else { |
| 68 | LOG(ERROR) << "Failed to " << (enable ? "enable" : "disable") << " overlayfs"; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 69 | } |
| 70 | return change; |
| 71 | } |
| 72 | |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 73 | struct SetVerityStateResult { |
| 74 | bool success = false; |
| 75 | bool want_reboot = false; |
| 76 | }; |
| 77 | |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 78 | /* Use AVB to turn verity on/off */ |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 79 | SetVerityStateResult SetVerityState(bool enable_verity) { |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 80 | std::string ab_suffix = get_ab_suffix(); |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 81 | bool verity_enabled = false; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 82 | |
| 83 | if (is_avb_device_locked()) { |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 84 | LOG(ERROR) << "Device must be bootloader unlocked to change verity state"; |
| 85 | return {}; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 86 | } |
| 87 | |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 88 | std::unique_ptr<AvbOps, decltype(&avb_ops_user_free)> ops(avb_ops_user_new(), &avb_ops_user_free); |
| 89 | if (!ops) { |
| 90 | LOG(ERROR) << "Error getting AVB ops"; |
| 91 | return {}; |
| 92 | } |
| 93 | |
| 94 | if (!avb_user_verity_get(ops.get(), ab_suffix.c_str(), &verity_enabled)) { |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 95 | LOG(ERROR) << "Error getting verity state"; |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 96 | return {}; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 97 | } |
| 98 | |
| 99 | if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) { |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 100 | LOG(INFO) << "Verity is already " << (verity_enabled ? "enabled" : "disabled"); |
| 101 | return {.success = true, .want_reboot = false}; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 104 | if (!avb_user_verity_set(ops.get(), ab_suffix.c_str(), enable_verity)) { |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 105 | LOG(ERROR) << "Error setting verity state"; |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 106 | return {}; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 109 | LOG(INFO) << "Successfully " << (enable_verity ? "enabled" : "disabled") << " verity"; |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 110 | return {.success = true, .want_reboot = true}; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 111 | } |
| 112 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 113 | void MyLogger(android::base::LogId id, android::base::LogSeverity severity, const char* tag, |
| 114 | const char* file, unsigned int line, const char* message) { |
| 115 | // Hide log starting with '[fs_mgr]' unless it's an error. |
| 116 | if (severity == android::base::ERROR || message[0] != '[') { |
| 117 | fprintf(stderr, "%s\n", message); |
| 118 | } |
| 119 | static auto logd = android::base::LogdLogger(); |
| 120 | logd(id, severity, tag, file, line, message); |
| 121 | } |
| 122 | |
| 123 | } // namespace |
| 124 | |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 125 | int main(int argc, char* argv[]) { |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 126 | android::base::InitLogging(argv, MyLogger); |
| 127 | |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 128 | if (argc == 0) { |
| 129 | LOG(FATAL) << "set-verity-state called with empty argv"; |
| 130 | } |
| 131 | |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 132 | bool enable_verity = false; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 133 | std::string procname = android::base::Basename(argv[0]); |
| 134 | if (procname == "enable-verity") { |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 135 | enable_verity = true; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 136 | } else if (procname == "disable-verity") { |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 137 | enable_verity = false; |
Yi-Yo Chiang | 7fd9d4f | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 138 | } else if (argc == 2 && (argv[1] == "1"s || argv[1] == "0"s)) { |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 139 | enable_verity = (argv[1] == "1"s); |
Yi-Yo Chiang | 7fd9d4f | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 140 | } else { |
| 141 | printf("usage: %s [1|0]\n", argv[0]); |
| 142 | return 1; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 143 | } |
| 144 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 145 | if (!kAllowDisableVerity || !is_debuggable()) { |
| 146 | errno = EPERM; |
| 147 | PLOG(ERROR) << "Cannot disable/enable verity on user build"; |
| 148 | return 1; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 149 | } |
| 150 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 151 | if (getuid() != 0) { |
| 152 | errno = EACCES; |
| 153 | PLOG(ERROR) << "Must be running as root (adb root)"; |
| 154 | return 1; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 157 | if (!is_using_avb()) { |
| 158 | LOG(ERROR) << "Expected AVB device, VB1.0 is no longer supported"; |
| 159 | return 1; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 160 | } |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 161 | |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 162 | int exit_code = 0; |
| 163 | bool want_reboot = false; |
| 164 | |
| 165 | auto ret = SetVerityState(enable_verity); |
| 166 | if (ret.success) { |
| 167 | want_reboot |= ret.want_reboot; |
| 168 | } else { |
| 169 | exit_code = 1; |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 170 | } |
| 171 | |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 172 | // Disable any overlayfs unconditionally if we want verity enabled. |
| 173 | // Enable overlayfs only if verity is successfully disabled or is already disabled. |
| 174 | if (enable_verity || ret.success) { |
| 175 | // Start a threadpool to service waitForService() callbacks as |
| 176 | // fs_mgr_overlayfs_* might call waitForService() to get the image service. |
| 177 | android::ProcessState::self()->startThreadPool(); |
| 178 | want_reboot |= overlayfs_setup(!enable_verity); |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 179 | } |
| 180 | |
Yi-Yo Chiang | 258f023 | 2022-08-08 19:23:48 +0800 | [diff] [blame^] | 181 | if (want_reboot) { |
| 182 | printf("Reboot the device for new settings to take effect\n"); |
| 183 | } |
| 184 | |
| 185 | return exit_code; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 186 | } |