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 | |
| 17 | #include <errno.h> |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 18 | #include <libavb_user/libavb_user.h> |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 19 | #include <stdio.h> |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 20 | |
| 21 | #include <android-base/file.h> |
| 22 | #include <android-base/logging.h> |
| 23 | #include <android-base/properties.h> |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 24 | #include <fs_mgr_overlayfs.h> |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 25 | #include <log/log_properties.h> |
| 26 | |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 27 | #ifdef ALLOW_DISABLE_VERITY |
| 28 | static const bool kAllowDisableVerity = true; |
| 29 | #else |
| 30 | static const bool kAllowDisableVerity = false; |
| 31 | #endif |
| 32 | |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 33 | static void suggest_run_adb_root() { |
| 34 | if (getuid() != 0) printf("Maybe run adb root?\n"); |
| 35 | } |
| 36 | |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 37 | /* Helper function to get A/B suffix, if any. If the device isn't |
| 38 | * using A/B the empty string is returned. Otherwise either "_a", |
| 39 | * "_b", ... is returned. |
| 40 | */ |
| 41 | static std::string get_ab_suffix() { |
| 42 | return android::base::GetProperty("ro.boot.slot_suffix", ""); |
| 43 | } |
| 44 | |
| 45 | static bool is_avb_device_locked() { |
| 46 | return android::base::GetProperty("ro.boot.vbmeta.device_state", "") == "locked"; |
| 47 | } |
| 48 | |
| 49 | static bool overlayfs_setup(bool enable) { |
| 50 | auto change = false; |
| 51 | errno = 0; |
| 52 | if (enable ? fs_mgr_overlayfs_teardown(nullptr, &change) |
Ed Chen | 1981401 | 2019-12-17 08:53:13 +0000 | [diff] [blame] | 53 | : fs_mgr_overlayfs_setup(nullptr, nullptr, &change)) { |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 54 | if (change) { |
| 55 | printf("%s overlayfs\n", enable ? "disabling" : "using"); |
| 56 | } |
| 57 | } else if (errno) { |
| 58 | printf("Overlayfs %s failed with error %s\n", enable ? "teardown" : "setup", strerror(errno)); |
| 59 | suggest_run_adb_root(); |
| 60 | } |
| 61 | return change; |
| 62 | } |
| 63 | |
| 64 | /* Use AVB to turn verity on/off */ |
| 65 | static bool set_avb_verity_enabled_state(AvbOps* ops, bool enable_verity) { |
| 66 | std::string ab_suffix = get_ab_suffix(); |
| 67 | bool verity_enabled; |
| 68 | |
| 69 | if (is_avb_device_locked()) { |
| 70 | printf("Device is locked. Please unlock the device first\n"); |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) { |
| 75 | printf("Error getting verity state. Try adb root first?\n"); |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | if ((verity_enabled && enable_verity) || (!verity_enabled && !enable_verity)) { |
| 80 | printf("verity is already %s\n", verity_enabled ? "enabled" : "disabled"); |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) { |
| 85 | printf("Error setting verity\n"); |
| 86 | return false; |
| 87 | } |
| 88 | |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 89 | printf("Successfully %s verity\n", enable_verity ? "enabled" : "disabled"); |
| 90 | return true; |
| 91 | } |
| 92 | |
| 93 | int main(int argc, char* argv[]) { |
| 94 | if (argc == 0) { |
| 95 | LOG(FATAL) << "set-verity-state called with empty argv"; |
| 96 | } |
| 97 | |
| 98 | std::optional<bool> enable_opt; |
| 99 | std::string procname = android::base::Basename(argv[0]); |
| 100 | if (procname == "enable-verity") { |
| 101 | enable_opt = true; |
| 102 | } else if (procname == "disable-verity") { |
| 103 | enable_opt = false; |
| 104 | } |
| 105 | |
| 106 | if (!enable_opt.has_value()) { |
| 107 | if (argc != 2) { |
| 108 | printf("usage: %s [1|0]\n", argv[0]); |
| 109 | return 1; |
| 110 | } |
| 111 | |
| 112 | if (strcmp(argv[1], "1") == 0) { |
| 113 | enable_opt = true; |
| 114 | } else if (strcmp(argv[1], "0") == 0) { |
| 115 | enable_opt = false; |
| 116 | } else { |
| 117 | printf("usage: %s [1|0]\n", argv[0]); |
| 118 | return 1; |
| 119 | } |
| 120 | } |
| 121 | |
| 122 | bool enable = enable_opt.value(); |
| 123 | |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 124 | // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by |
| 125 | // contract, androidboot.vbmeta.digest is set by the bootloader |
| 126 | // when using AVB). |
| 127 | bool using_avb = !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty(); |
| 128 | |
| 129 | // If using AVB, dm-verity is used on any build so we want it to |
| 130 | // be possible to disable/enable on any build (except USER). For |
| 131 | // VB1.0 dm-verity is only enabled on certain builds. |
| 132 | if (!using_avb) { |
| 133 | if (!kAllowDisableVerity) { |
| 134 | printf("%s only works for userdebug builds\n", argv[0]); |
| 135 | } |
| 136 | |
| 137 | if (!android::base::GetBoolProperty("ro.secure", false)) { |
| 138 | overlayfs_setup(enable); |
| 139 | printf("verity not enabled - ENG build\n"); |
| 140 | return 0; |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | // Should never be possible to disable dm-verity on a USER build |
| 145 | // regardless of using AVB or VB1.0. |
| 146 | if (!__android_log_is_debuggable()) { |
| 147 | printf("verity cannot be disabled/enabled - USER build\n"); |
| 148 | return 0; |
| 149 | } |
| 150 | |
Yi-Yo Chiang | 4ef9fcc | 2022-08-03 19:40:45 +0800 | [diff] [blame^] | 151 | bool any_changed = false; |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 152 | if (using_avb) { |
| 153 | // Yep, the system is using AVB. |
| 154 | AvbOps* ops = avb_ops_user_new(); |
| 155 | if (ops == nullptr) { |
| 156 | printf("Error getting AVB ops\n"); |
| 157 | return 1; |
| 158 | } |
Yi-Yo Chiang | 4ef9fcc | 2022-08-03 19:40:45 +0800 | [diff] [blame^] | 159 | any_changed |= set_avb_verity_enabled_state(ops, enable); |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 160 | avb_ops_user_free(ops); |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 161 | } |
Yi-Yo Chiang | 4ef9fcc | 2022-08-03 19:40:45 +0800 | [diff] [blame^] | 162 | any_changed |= overlayfs_setup(enable); |
Josh Gao | f61f414 | 2019-10-22 12:30:30 -0700 | [diff] [blame] | 163 | |
| 164 | if (any_changed) { |
| 165 | printf("Now reboot your device for settings to take effect\n"); |
| 166 | } |
| 167 | |
| 168 | return 0; |
| 169 | } |