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 | |
| 111 | /* Use AVB to turn verity on/off */ |
| 112 | static bool set_avb_verity_enabled_state(AvbOps* ops, bool enable_verity) { |
| 113 | std::string ab_suffix = get_ab_suffix(); |
| 114 | |
| 115 | bool verity_enabled; |
| 116 | if (!avb_user_verity_get(ops, ab_suffix.c_str(), &verity_enabled)) { |
| 117 | LOG(ERROR) << "Error getting verity state"; |
| 118 | return false; |
| 119 | } |
| 120 | |
| 121 | if ((verity_enabled && enable_verity) || |
| 122 | (!verity_enabled && !enable_verity)) { |
| 123 | LOG(WARNING) << "verity is already " |
| 124 | << verity_enabled ? "enabled" : "disabled"; |
| 125 | return false; |
| 126 | } |
| 127 | |
| 128 | if (!avb_user_verity_set(ops, ab_suffix.c_str(), enable_verity)) { |
| 129 | LOG(ERROR) << "Error setting verity"; |
| 130 | return false; |
| 131 | } |
| 132 | |
| 133 | LOG(DEBUG) << "Successfully " << (enable_verity ? "enabled" : "disabled") |
| 134 | << " verity"; |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | bool set_verity_enabled(bool enable) { |
| 139 | bool rc = true; |
| 140 | |
| 141 | // Do not allow changing verity on user builds |
| 142 | bool is_user = (android::base::GetProperty("ro.build.type", "") == "user"); |
| 143 | if (is_user) { |
| 144 | LOG(ERROR) << "Cannot disable verity - USER BUILD"; |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | // Figure out if we're using VB1.0 or VB2.0 (aka AVB) - by |
| 149 | // contract, androidboot.vbmeta.digest is set by the bootloader |
| 150 | // when using AVB). |
| 151 | bool using_avb = !android::base::GetProperty("ro.boot.vbmeta.digest", "").empty(); |
| 152 | |
| 153 | // If using AVB, dm-verity is used on any build so we want it to |
| 154 | // be possible to disable/enable on any build (except USER). For |
| 155 | // VB1.0 dm-verity is only enabled on certain builds. |
| 156 | if (using_avb) { |
| 157 | // Yep, the system is using AVB. |
| 158 | AvbOps* ops = avb_ops_user_new(); |
| 159 | if (ops == nullptr) { |
| 160 | LOG(ERROR) << "Error getting AVB ops"; |
| 161 | return false; |
| 162 | } |
| 163 | rc = set_avb_verity_enabled_state(ops, enable); |
| 164 | avb_ops_user_free(ops); |
| 165 | } else { |
| 166 | // Not using AVB - assume VB1.0. |
| 167 | |
| 168 | // read all fstab entries at once from all sources |
| 169 | struct fstab* fstab = fs_mgr_read_fstab_default(); |
| 170 | if (!fstab) { |
| 171 | LOG(ERROR) << "Failed to read fstab"; |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | // Loop through entries looking for ones that vold manages. |
| 176 | for (int i = 0; i < fstab->num_entries; i++) { |
| 177 | if (fs_mgr_is_verified(&fstab->recs[i])) { |
| 178 | bool result = set_block_device_verity_enabled( |
| 179 | fstab->recs[i].blk_device, enable); |
| 180 | if (!result) { |
| 181 | // Warn, but continue if failure occurred |
| 182 | LOG(WARNING) << "Failed to set state " |
| 183 | << (enable ? "enabled" : "disabled") |
| 184 | << " on " << fstab->recs[i].mount_point; |
| 185 | } |
| 186 | rc = rc && result; |
| 187 | } |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | return rc; |
| 192 | } |