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; |
| 62 | if (enable ? fs_mgr_overlayfs_teardown(nullptr, &change) |
David Anderson | e9e3f6e | 2022-08-04 11:18:01 -0700 | [diff] [blame] | 63 | : fs_mgr_overlayfs_setup(nullptr, &change)) { |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 64 | if (change) { |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 65 | LOG(INFO) << (enable ? "disabling" : "using") << " overlayfs"; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 66 | } |
| 67 | } else if (errno) { |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 68 | PLOG(ERROR) << "Failed to " << (enable ? "teardown" : "setup") << " overlayfs"; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 69 | } |
| 70 | return change; |
| 71 | } |
| 72 | |
| 73 | /* Use AVB to turn verity on/off */ |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 74 | bool set_avb_verity_enabled_state(AvbOps* ops, bool enable_verity) { |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 75 | std::string ab_suffix = get_ab_suffix(); |
| 76 | bool verity_enabled; |
| 77 | |
| 78 | if (is_avb_device_locked()) { |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 79 | LOG(ERROR) << "Device is locked. Please unlock the device first"; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 80 | return false; |
| 81 | } |
| 82 | |
| 83 | if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) { |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 84 | LOG(ERROR) << "Error getting verity state"; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 85 | return false; |
| 86 | } |
| 87 | |
| 88 | if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) { |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 89 | LOG(INFO) << "verity is already " << (verity_enabled ? "enabled" : "disabled"); |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 90 | return false; |
| 91 | } |
| 92 | |
| 93 | if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) { |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 94 | LOG(ERROR) << "Error setting verity state"; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 95 | return false; |
| 96 | } |
| 97 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 98 | LOG(INFO) << "Successfully " << (enable_verity ? "enabled" : "disabled") << " verity"; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 99 | return true; |
| 100 | } |
| 101 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 102 | void MyLogger(android::base::LogId id, android::base::LogSeverity severity, const char* tag, |
| 103 | const char* file, unsigned int line, const char* message) { |
| 104 | // Hide log starting with '[fs_mgr]' unless it's an error. |
| 105 | if (severity == android::base::ERROR || message[0] != '[') { |
| 106 | fprintf(stderr, "%s\n", message); |
| 107 | } |
| 108 | static auto logd = android::base::LogdLogger(); |
| 109 | logd(id, severity, tag, file, line, message); |
| 110 | } |
| 111 | |
| 112 | } // namespace |
| 113 | |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 114 | int main(int argc, char* argv[]) { |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 115 | android::base::InitLogging(argv, MyLogger); |
| 116 | |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 117 | if (argc == 0) { |
| 118 | LOG(FATAL) << "set-verity-state called with empty argv"; |
| 119 | } |
| 120 | |
Yi-Yo Chiang | 7fd9d4f | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 121 | bool enable = false; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 122 | std::string procname = android::base::Basename(argv[0]); |
| 123 | if (procname == "enable-verity") { |
Yi-Yo Chiang | 7fd9d4f | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 124 | enable = true; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 125 | } else if (procname == "disable-verity") { |
Yi-Yo Chiang | 7fd9d4f | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 126 | enable = false; |
| 127 | } else if (argc == 2 && (argv[1] == "1"s || argv[1] == "0"s)) { |
| 128 | enable = (argv[1] == "1"s); |
| 129 | } else { |
| 130 | printf("usage: %s [1|0]\n", argv[0]); |
| 131 | return 1; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 132 | } |
| 133 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 134 | if (!kAllowDisableVerity || !is_debuggable()) { |
| 135 | errno = EPERM; |
| 136 | PLOG(ERROR) << "Cannot disable/enable verity on user build"; |
| 137 | return 1; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 140 | if (getuid() != 0) { |
| 141 | errno = EACCES; |
| 142 | PLOG(ERROR) << "Must be running as root (adb root)"; |
| 143 | return 1; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 144 | } |
| 145 | |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 146 | if (!is_using_avb()) { |
| 147 | LOG(ERROR) << "Expected AVB device, VB1.0 is no longer supported"; |
| 148 | return 1; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 149 | } |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 150 | |
| 151 | std::unique_ptr<AvbOps, decltype(&avb_ops_user_free)> ops(avb_ops_user_new(), &avb_ops_user_free); |
| 152 | if (!ops) { |
| 153 | LOG(ERROR) << "Error getting AVB ops"; |
| 154 | return 1; |
| 155 | } |
| 156 | |
Yi-Yo Chiang | 1305c15 | 2022-08-10 12:53:39 +0800 | [diff] [blame] | 157 | // Start a threadpool to service waitForService() callbacks as |
| 158 | // fs_mgr_overlayfs_* might call waitForService() to get the image service. |
| 159 | android::ProcessState::self()->startThreadPool(); |
Yi-Yo Chiang | 6bb1acb | 2022-08-08 13:36:50 +0800 | [diff] [blame] | 160 | bool any_changed = set_avb_verity_enabled_state(ops.get(), enable); |
Yi-Yo Chiang | 4ef9fcc | 2022-08-03 19:40:45 +0800 | [diff] [blame] | 161 | any_changed |= overlayfs_setup(enable); |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 162 | |
| 163 | if (any_changed) { |
| 164 | printf("Now reboot your device for settings to take effect\n"); |
| 165 | } |
| 166 | |
| 167 | return 0; |
| 168 | } |