1 /*
2 * Copyright (C) 2018 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 "libdm/dm_target.h"
18
19 #include <inttypes.h>
20 #include <stdio.h>
21 #include <sys/types.h>
22
23 #include <android-base/logging.h>
24 #include <android-base/macros.h>
25 #include <android-base/parseint.h>
26 #include <android-base/strings.h>
27
28 #include <libdm/dm.h>
29
30 namespace android {
31 namespace dm {
32
Serialize() const33 std::string DmTarget::Serialize() const {
34 // Create a string containing a dm_target_spec, parameter data, and an
35 // explicit null terminator.
36 std::string data(sizeof(dm_target_spec), '\0');
37 data += GetParameterString();
38 data.push_back('\0');
39
40 // The kernel expects each target to be 8-byte aligned.
41 size_t padding = DM_ALIGN(data.size()) - data.size();
42 for (size_t i = 0; i < padding; i++) {
43 data.push_back('\0');
44 }
45
46 // Finally fill in the dm_target_spec.
47 struct dm_target_spec* spec = reinterpret_cast<struct dm_target_spec*>(&data[0]);
48 spec->sector_start = start();
49 spec->length = size();
50 snprintf(spec->target_type, sizeof(spec->target_type), "%s", name().c_str());
51 spec->next = (uint32_t)data.size();
52 return data;
53 }
54
GetParameterString() const55 std::string DmTargetZero::GetParameterString() const {
56 // The zero target type has no additional parameters.
57 return "";
58 }
59
GetParameterString() const60 std::string DmTargetLinear::GetParameterString() const {
61 return block_device_ + " " + std::to_string(physical_sector_);
62 }
63
DmTargetVerity(uint64_t start,uint64_t length,uint32_t version,const std::string & block_device,const std::string & hash_device,uint32_t data_block_size,uint32_t hash_block_size,uint32_t num_data_blocks,uint32_t hash_start_block,const std::string & hash_algorithm,const std::string & root_digest,const std::string & salt)64 DmTargetVerity::DmTargetVerity(uint64_t start, uint64_t length, uint32_t version,
65 const std::string& block_device, const std::string& hash_device,
66 uint32_t data_block_size, uint32_t hash_block_size,
67 uint32_t num_data_blocks, uint32_t hash_start_block,
68 const std::string& hash_algorithm, const std::string& root_digest,
69 const std::string& salt)
70 : DmTarget(start, length), valid_(true) {
71 base_args_ = {
72 std::to_string(version),
73 block_device,
74 hash_device,
75 std::to_string(data_block_size),
76 std::to_string(hash_block_size),
77 std::to_string(num_data_blocks),
78 std::to_string(hash_start_block),
79 hash_algorithm,
80 root_digest,
81 salt,
82 };
83 }
84
UseFec(const std::string & device,uint32_t num_roots,uint32_t num_blocks,uint32_t start)85 void DmTargetVerity::UseFec(const std::string& device, uint32_t num_roots, uint32_t num_blocks,
86 uint32_t start) {
87 optional_args_.emplace_back("use_fec_from_device");
88 optional_args_.emplace_back(device);
89 optional_args_.emplace_back("fec_roots");
90 optional_args_.emplace_back(std::to_string(num_roots));
91 optional_args_.emplace_back("fec_blocks");
92 optional_args_.emplace_back(std::to_string(num_blocks));
93 optional_args_.emplace_back("fec_start");
94 optional_args_.emplace_back(std::to_string(start));
95 }
96
SetVerityMode(const std::string & mode)97 void DmTargetVerity::SetVerityMode(const std::string& mode) {
98 if (mode != "restart_on_corruption" && mode != "ignore_corruption") {
99 LOG(ERROR) << "Unknown verity mode: " << mode;
100 valid_ = false;
101 return;
102 }
103 optional_args_.emplace_back(mode);
104 }
105
IgnoreZeroBlocks()106 void DmTargetVerity::IgnoreZeroBlocks() {
107 optional_args_.emplace_back("ignore_zero_blocks");
108 }
109
GetParameterString() const110 std::string DmTargetVerity::GetParameterString() const {
111 std::string base = android::base::Join(base_args_, " ");
112 if (optional_args_.empty()) {
113 return base;
114 }
115 std::string optional = android::base::Join(optional_args_, " ");
116 return base + " " + std::to_string(optional_args_.size()) + " " + optional;
117 }
118
GetParameterString() const119 std::string DmTargetAndroidVerity::GetParameterString() const {
120 return keyid_ + " " + block_device_;
121 }
122
GetParameterString() const123 std::string DmTargetBow::GetParameterString() const {
124 if (!block_size_) return target_string_;
125 return target_string_ + " 1 block_size:" + std::to_string(block_size_);
126 }
127
name() const128 std::string DmTargetSnapshot::name() const {
129 if (mode_ == SnapshotStorageMode::Merge) {
130 return "snapshot-merge";
131 }
132 return "snapshot";
133 }
134
GetParameterString() const135 std::string DmTargetSnapshot::GetParameterString() const {
136 std::string mode;
137 switch (mode_) {
138 case SnapshotStorageMode::Persistent:
139 case SnapshotStorageMode::Merge:
140 // Note: "O" lets us query for overflow in the status message. This
141 // is only supported on kernels 4.4+. On earlier kernels, an overflow
142 // will be reported as "Invalid" in the status string.
143 mode = "P";
144 if (ReportsOverflow(name())) {
145 mode += "O";
146 }
147 break;
148 case SnapshotStorageMode::Transient:
149 mode = "N";
150 break;
151 default:
152 LOG(ERROR) << "DmTargetSnapshot unknown mode";
153 break;
154 }
155 return base_device_ + " " + cow_device_ + " " + mode + " " + std::to_string(chunk_size_);
156 }
157
158 // Computes the percentage of complition for snapshot status.
159 // @sectors_initial is the number of sectors_allocated stored right before
160 // starting the merge.
MergePercent(const DmTargetSnapshot::Status & status,uint64_t sectors_initial)161 double DmTargetSnapshot::MergePercent(const DmTargetSnapshot::Status& status,
162 uint64_t sectors_initial) {
163 uint64_t s = status.sectors_allocated;
164 uint64_t t = status.total_sectors;
165 uint64_t m = status.metadata_sectors;
166 uint64_t i = sectors_initial == 0 ? t : sectors_initial;
167
168 if (t <= s || i <= s) {
169 return 0.0;
170 }
171 if (s == 0 || t == 0 || s <= m) {
172 return 100.0;
173 }
174 return 100.0 / (i - m) * (i - s);
175 }
176
ReportsOverflow(const std::string & target_type)177 bool DmTargetSnapshot::ReportsOverflow(const std::string& target_type) {
178 DeviceMapper& dm = DeviceMapper::Instance();
179 DmTargetTypeInfo info;
180 if (!dm.GetTargetByName(target_type, &info)) {
181 return false;
182 }
183 if (target_type == "snapshot") {
184 return info.IsAtLeast(1, 15, 0);
185 }
186 if (target_type == "snapshot-merge") {
187 return info.IsAtLeast(1, 4, 0);
188 }
189 return false;
190 }
191
ParseStatusText(const std::string & text,Status * status)192 bool DmTargetSnapshot::ParseStatusText(const std::string& text, Status* status) {
193 // Try to parse the line as it should be
194 int args = sscanf(text.c_str(), "%" PRIu64 "/%" PRIu64 " %" PRIu64, &status->sectors_allocated,
195 &status->total_sectors, &status->metadata_sectors);
196 if (args == 3) {
197 return true;
198 }
199 auto sections = android::base::Split(text, " ");
200 if (sections.size() == 0) {
201 LOG(ERROR) << "could not parse empty status";
202 return false;
203 }
204 // Error codes are: "Invalid", "Overflow" and "Merge failed"
205 if (sections.size() == 1) {
206 if (text == "Invalid" || text == "Overflow") {
207 status->error = text;
208 return true;
209 }
210 } else if (sections.size() == 2 && text == "Merge failed") {
211 status->error = text;
212 return true;
213 }
214 LOG(ERROR) << "could not parse snapshot status: wrong format";
215 return false;
216 }
217
GetDevicesFromParams(const std::string & params,std::string * base_device,std::string * cow_device)218 bool DmTargetSnapshot::GetDevicesFromParams(const std::string& params, std::string* base_device,
219 std::string* cow_device) {
220 auto pieces = android::base::Split(params, " ");
221 if (pieces.size() < 2) {
222 LOG(ERROR) << "Parameter string is invalid: " << params;
223 return false;
224 }
225 *base_device = pieces[0];
226 *cow_device = pieces[1];
227 return true;
228 }
229
GetParameterString() const230 std::string DmTargetCrypt::GetParameterString() const {
231 std::vector<std::string> argv = {
232 cipher_,
233 key_,
234 std::to_string(iv_sector_offset_),
235 device_,
236 std::to_string(device_sector_),
237 };
238
239 std::vector<std::string> extra_argv;
240 if (allow_discards_) extra_argv.emplace_back("allow_discards");
241 if (allow_encrypt_override_) extra_argv.emplace_back("allow_encrypt_override");
242 if (iv_large_sectors_) extra_argv.emplace_back("iv_large_sectors");
243 if (sector_size_) extra_argv.emplace_back("sector_size:" + std::to_string(sector_size_));
244
245 if (!extra_argv.empty()) argv.emplace_back(std::to_string(extra_argv.size()));
246
247 argv.insert(argv.end(), extra_argv.begin(), extra_argv.end());
248 return android::base::Join(argv, " ");
249 }
250
Valid() const251 bool DmTargetDefaultKey::Valid() const {
252 if (!use_legacy_options_format_ && !set_dun_) return false;
253 return true;
254 }
255
GetParameterString() const256 std::string DmTargetDefaultKey::GetParameterString() const {
257 std::vector<std::string> argv;
258 argv.emplace_back(cipher_);
259 argv.emplace_back(key_);
260 if (!use_legacy_options_format_) {
261 argv.emplace_back("0"); // iv_offset
262 }
263 argv.emplace_back(blockdev_);
264 argv.push_back(std::to_string(start_sector_));
265 std::vector<std::string> extra_argv;
266 if (use_legacy_options_format_) {
267 if (set_dun_) { // v2 always sets the DUN.
268 extra_argv.emplace_back("set_dun");
269 }
270 } else {
271 extra_argv.emplace_back("allow_discards");
272 extra_argv.emplace_back("sector_size:4096");
273 extra_argv.emplace_back("iv_large_sectors");
274 if (is_hw_wrapped_) extra_argv.emplace_back("wrappedkey_v0");
275 }
276 if (!extra_argv.empty()) {
277 argv.emplace_back(std::to_string(extra_argv.size()));
278 argv.insert(argv.end(), extra_argv.begin(), extra_argv.end());
279 }
280 return android::base::Join(argv, " ");
281 }
282
GetParameterString() const283 std::string DmTargetUser::GetParameterString() const {
284 std::vector<std::string> argv;
285 argv.push_back(std::to_string(start()));
286 argv.push_back(std::to_string(size()));
287 return android::base::Join(argv, " ");
288 }
289
290 } // namespace dm
291 } // namespace android
292