Christopher N. Hesse | e74d3b7 | 2018-02-24 23:33:49 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 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 "verity_tool.h" |
| 18 | |
| 19 | #include <android-base/logging.h> |
| 20 | #include <android-base/properties.h> |
| 21 | #include <fs_mgr.h> |
| 22 | #include <fec/io.h> |
| 23 | #include <libavb_user/libavb_user.h> |
| 24 | |
| 25 | #include <linux/fs.h> |
| 26 | |
| 27 | #include <errno.h> |
| 28 | |
| 29 | static int make_block_device_writable(const std::string& block_device) { |
| 30 | int fd = open(block_device.c_str(), O_RDONLY | O_CLOEXEC); |
| 31 | if (fd < 0) { |
| 32 | return -errno; |
| 33 | } |
| 34 | |
| 35 | int OFF = 0; |
| 36 | int rc = ioctl(fd, BLKROSET, &OFF); |
| 37 | if (rc < 0) { |
| 38 | rc = -errno; |
| 39 | goto out; |
| 40 | } |
| 41 | rc = 0; |
| 42 | out: |
| 43 | close(fd); |
| 44 | return rc; |
| 45 | } |
| 46 | |
| 47 | /* Turn verity on/off */ |
| 48 | bool set_block_device_verity_enabled(const std::string& block_device, |
| 49 | bool enable) { |
| 50 | int rc = make_block_device_writable(block_device); |
| 51 | if (rc) { |
| 52 | LOG(ERROR) << "Could not make block device " |
| 53 | << block_device << " writable:" << rc; |
| 54 | return false; |
| 55 | } |
| 56 | |
| 57 | fec::io fh(block_device, O_RDWR); |
| 58 | if (!fh) { |
| 59 | PLOG(ERROR) << "Could not open block device " << block_device; |
| 60 | return false; |
| 61 | } |
| 62 | |
| 63 | fec_verity_metadata metadata; |
| 64 | if (!fh.get_verity_metadata(metadata)) { |
| 65 | LOG(ERROR) << "Couldn't find verity metadata!"; |
| 66 | return false; |
| 67 | } |
| 68 | |
| 69 | if (!enable && metadata.disabled) { |
| 70 | LOG(ERROR) << "Verity already disabled on " << block_device; |
| 71 | return false; |
| 72 | } |
| 73 | |
| 74 | if (enable && !metadata.disabled) { |
| 75 | LOG(WARNING) << "Verity already enabled on " << block_device; |
| 76 | return false; |
| 77 | } |
| 78 | |
| 79 | if (!fh.set_verity_status(enable)) { |
| 80 | PLOG(ERROR) << "Could not set verity " |
| 81 | << (enable ? "enabled" : "disabled") |
| 82 | << " flag on device " << block_device; |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | LOG(DEBUG) << "Verity " << (enable ? "enabled" : "disabled") |
| 87 | << " on " << block_device; |
| 88 | return true; |
| 89 | } |
| 90 | |
| 91 | /* Helper function to get A/B suffix, if any. If the device isn't |
| 92 | * using A/B the empty string is returned. Otherwise either "_a", |
| 93 | * "_b", ... is returned. |
| 94 | * |
| 95 | * Note that since sometime in O androidboot.slot_suffix is deprecated |
| 96 | * and androidboot.slot should be used instead. Since bootloaders may |
| 97 | * be out of sync with the OS, we check both and for extra safety |
| 98 | * prepend a leading underscore if there isn't one already. |
| 99 | */ |
| 100 | static std::string get_ab_suffix() { |
| 101 | std::string ab_suffix = android::base::GetProperty("ro.boot.slot_suffix", ""); |
| 102 | if (ab_suffix.empty()) { |
| 103 | ab_suffix = android::base::GetProperty("ro.boot.slot", ""); |
| 104 | } |
| 105 | if (ab_suffix.size() > 0 && ab_suffix[0] != '_') { |
| 106 | ab_suffix = std::string("_") + ab_suffix; |
| 107 | } |
| 108 | return ab_suffix; |
| 109 | } |
| 110 | |
Christopher N. Hesse | b20fd51 | 2018-06-26 19:56:24 +0200 | [diff] [blame] | 111 | verity_state_t get_verity_state() { |
| 112 | verity_state_t rc = VERITY_STATE_NO_DEVICE; |
| 113 | std::string ab_suffix = get_ab_suffix(); |
| 114 | |
| 115 | // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by |
| 116 | // contract, androidboot.vbmeta.digest is set by the bootloader |
| 117 | // when using AVB). |
| 118 | bool using_avb = !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty(); |
| 119 | |
| 120 | if (using_avb) { |
| 121 | // Yep, the system is using AVB. |
| 122 | AvbOps* ops = avb_ops_user_new(); |
| 123 | if (ops == nullptr) { |
| 124 | LOG(ERROR) << "Error getting AVB ops"; |
| 125 | avb_ops_user_free(ops); |
| 126 | return VERITY_STATE_UNKNOWN; |
| 127 | } |
| 128 | bool verity_enabled; |
| 129 | if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) { |
| 130 | LOG(ERROR) << "Error getting verity state"; |
| 131 | avb_ops_user_free(ops); |
| 132 | return VERITY_STATE_UNKNOWN; |
| 133 | } |
| 134 | rc = verity_enabled ? VERITY_STATE_ENABLED : VERITY_STATE_DISABLED; |
| 135 | avb_ops_user_free(ops); |
| 136 | } else { |
| 137 | // Not using AVB - assume VB1.0. |
| 138 | |
| 139 | // read all fstab entries at once from all sources |
| 140 | struct fstab* fstab = fs_mgr_read_fstab_default(); |
| 141 | if (!fstab) { |
| 142 | LOG(ERROR) << "Failed to read fstab"; |
| 143 | fs_mgr_free_fstab(fstab); |
| 144 | return VERITY_STATE_UNKNOWN; |
| 145 | } |
| 146 | |
| 147 | // Loop through entries looking for ones that vold manages. |
| 148 | for (int i = 0; i < fstab->num_entries; i++) { |
| 149 | if (fs_mgr_is_verified(&fstab->recs[i])) { |
| 150 | std::string block_device = fstab->recs[i].blk_device; |
| 151 | fec::io fh(block_device, O_RDONLY); |
| 152 | if (!fh) { |
| 153 | PLOG(ERROR) << "Could not open block device " << block_device; |
| 154 | rc = VERITY_STATE_UNKNOWN; |
| 155 | break; |
| 156 | } |
| 157 | |
| 158 | fec_verity_metadata metadata; |
| 159 | if (!fh.get_verity_metadata(metadata)) { |
| 160 | LOG(ERROR) << "Couldn't find verity metadata!"; |
| 161 | rc = VERITY_STATE_UNKNOWN; |
| 162 | break; |
| 163 | } |
| 164 | |
| 165 | rc = metadata.disabled ? VERITY_STATE_DISABLED : VERITY_STATE_ENABLED; |
| 166 | } |
| 167 | } |
| 168 | fs_mgr_free_fstab(fstab); |
| 169 | } |
| 170 | |
| 171 | return rc; |
| 172 | } |
| 173 | |
Christopher N. Hesse | e74d3b7 | 2018-02-24 23:33:49 -0800 | [diff] [blame] | 174 | /* Use AVB to turn verity on/off */ |
| 175 | static bool set_avb_verity_enabled_state(AvbOps* ops, bool enable_verity) { |
| 176 | std::string ab_suffix = get_ab_suffix(); |
| 177 | |
| 178 | bool verity_enabled; |
| 179 | if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) { |
| 180 | LOG(ERROR) << "Error getting verity state"; |
| 181 | return false; |
| 182 | } |
| 183 | |
| 184 | if ((verity_enabled && enable_verity) || |
| 185 | (!verity_enabled && !enable_verity)) { |
| 186 | LOG(WARNING) << "verity is already " |
| 187 | << verity_enabled ? "enabled" : "disabled"; |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) { |
| 192 | LOG(ERROR) << "Error setting verity"; |
| 193 | return false; |
| 194 | } |
| 195 | |
| 196 | LOG(DEBUG) << "Successfully " << (enable_verity ? "enabled" : "disabled") |
| 197 | << " verity"; |
| 198 | return true; |
| 199 | } |
| 200 | |
| 201 | bool set_verity_enabled(bool enable) { |
| 202 | bool rc = true; |
| 203 | |
| 204 | // Do not allow changing verity on user builds |
| 205 | bool is_user = (android::base::GetProperty("ro.build.type", "") == "user"); |
| 206 | if (is_user) { |
| 207 | LOG(ERROR) << "Cannot disable verity - USER BUILD"; |
| 208 | return false; |
| 209 | } |
| 210 | |
| 211 | // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by |
| 212 | // contract, androidboot.vbmeta.digest is set by the bootloader |
| 213 | // when using AVB). |
| 214 | bool using_avb = !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty(); |
| 215 | |
| 216 | // If using AVB, dm-verity is used on any build so we want it to |
| 217 | // be possible to disable/enable on any build (except USER). For |
| 218 | // VB1.0 dm-verity is only enabled on certain builds. |
| 219 | if (using_avb) { |
| 220 | // Yep, the system is using AVB. |
| 221 | AvbOps* ops = avb_ops_user_new(); |
| 222 | if (ops == nullptr) { |
| 223 | LOG(ERROR) << "Error getting AVB ops"; |
| 224 | return false; |
| 225 | } |
| 226 | rc = set_avb_verity_enabled_state(ops, enable); |
| 227 | avb_ops_user_free(ops); |
| 228 | } else { |
| 229 | // Not using AVB - assume VB1.0. |
| 230 | |
| 231 | // read all fstab entries at once from all sources |
| 232 | struct fstab* fstab = fs_mgr_read_fstab_default(); |
| 233 | if (!fstab) { |
| 234 | LOG(ERROR) << "Failed to read fstab"; |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | // Loop through entries looking for ones that vold manages. |
| 239 | for (int i = 0; i < fstab->num_entries; i++) { |
| 240 | if (fs_mgr_is_verified(&fstab->recs[i])) { |
| 241 | bool result = set_block_device_verity_enabled( |
| 242 | fstab->recs[i].blk_device, enable); |
| 243 | if (!result) { |
| 244 | // Warn, but continue if failure occurred |
| 245 | LOG(WARNING) << "Failed to set state " |
| 246 | << (enable ? "enabled" : "disabled") |
| 247 | << " on " << fstab->recs[i].mount_point; |
| 248 | } |
| 249 | rc = rc && result; |
| 250 | } |
| 251 | } |
| 252 | } |
| 253 | |
| 254 | return rc; |
| 255 | } |