• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 != "panic_on_corruption" &&
99         mode != "restart_on_corruption" &&
100         mode != "ignore_corruption") {
101         LOG(ERROR) << "Unknown verity mode: " << mode;
102         valid_ = false;
103         return;
104     }
105     optional_args_.emplace_back(mode);
106 }
107 
IgnoreZeroBlocks()108 void DmTargetVerity::IgnoreZeroBlocks() {
109     optional_args_.emplace_back("ignore_zero_blocks");
110 }
111 
GetParameterString() const112 std::string DmTargetVerity::GetParameterString() const {
113     std::string base = android::base::Join(base_args_, " ");
114     if (optional_args_.empty()) {
115         return base;
116     }
117     std::string optional = android::base::Join(optional_args_, " ");
118     return base + " " + std::to_string(optional_args_.size()) + " " + optional;
119 }
120 
GetParameterString() const121 std::string DmTargetAndroidVerity::GetParameterString() const {
122     return keyid_ + " " + block_device_;
123 }
124 
GetParameterString() const125 std::string DmTargetBow::GetParameterString() const {
126     if (!block_size_) return target_string_;
127     return target_string_ + " 1 block_size:" + std::to_string(block_size_);
128 }
129 
name() const130 std::string DmTargetSnapshot::name() const {
131     if (mode_ == SnapshotStorageMode::Merge) {
132         return "snapshot-merge";
133     }
134     return "snapshot";
135 }
136 
GetParameterString() const137 std::string DmTargetSnapshot::GetParameterString() const {
138     std::string mode;
139     switch (mode_) {
140         case SnapshotStorageMode::Persistent:
141         case SnapshotStorageMode::Merge:
142             // Note: "O" lets us query for overflow in the status message. This
143             // is only supported on kernels 4.4+. On earlier kernels, an overflow
144             // will be reported as "Invalid" in the status string.
145             mode = "P";
146             if (ReportsOverflow(name())) {
147                 mode += "O";
148             }
149             break;
150         case SnapshotStorageMode::Transient:
151             mode = "N";
152             break;
153         default:
154             LOG(ERROR) << "DmTargetSnapshot unknown mode";
155             break;
156     }
157     return base_device_ + " " + cow_device_ + " " + mode + " " + std::to_string(chunk_size_);
158 }
159 
160 // Computes the percentage of complition for snapshot status.
161 // @sectors_initial is the number of sectors_allocated stored right before
162 // starting the merge.
MergePercent(const DmTargetSnapshot::Status & status,uint64_t sectors_initial)163 double DmTargetSnapshot::MergePercent(const DmTargetSnapshot::Status& status,
164                                       uint64_t sectors_initial) {
165     uint64_t s = status.sectors_allocated;
166     uint64_t t = status.total_sectors;
167     uint64_t m = status.metadata_sectors;
168     uint64_t i = sectors_initial == 0 ? t : sectors_initial;
169 
170     if (t <= s || i <= s) {
171         return 0.0;
172     }
173     if (s == 0 || t == 0 || s <= m) {
174         return 100.0;
175     }
176     return 100.0 / (i - m) * (i - s);
177 }
178 
ReportsOverflow(const std::string & target_type)179 bool DmTargetSnapshot::ReportsOverflow(const std::string& target_type) {
180     DeviceMapper& dm = DeviceMapper::Instance();
181     DmTargetTypeInfo info;
182     if (!dm.GetTargetByName(target_type, &info)) {
183         return false;
184     }
185     if (target_type == "snapshot") {
186         return info.IsAtLeast(1, 15, 0);
187     }
188     if (target_type == "snapshot-merge") {
189         return info.IsAtLeast(1, 4, 0);
190     }
191     return false;
192 }
193 
ParseStatusText(const std::string & text,Status * status)194 bool DmTargetSnapshot::ParseStatusText(const std::string& text, Status* status) {
195     // Try to parse the line as it should be
196     int args = sscanf(text.c_str(), "%" PRIu64 "/%" PRIu64 " %" PRIu64, &status->sectors_allocated,
197                       &status->total_sectors, &status->metadata_sectors);
198     if (args == 3) {
199         return true;
200     }
201     auto sections = android::base::Split(text, " ");
202     if (sections.size() == 0) {
203         LOG(ERROR) << "could not parse empty status";
204         return false;
205     }
206     // Error codes are: "Invalid", "Overflow" and "Merge failed"
207     if (sections.size() == 1) {
208         if (text == "Invalid" || text == "Overflow") {
209             status->error = text;
210             return true;
211         }
212     } else if (sections.size() == 2 && text == "Merge failed") {
213         status->error = text;
214         return true;
215     }
216     LOG(ERROR) << "could not parse snapshot status: wrong format";
217     return false;
218 }
219 
GetDevicesFromParams(const std::string & params,std::string * base_device,std::string * cow_device)220 bool DmTargetSnapshot::GetDevicesFromParams(const std::string& params, std::string* base_device,
221                                             std::string* cow_device) {
222     auto pieces = android::base::Split(params, " ");
223     if (pieces.size() < 2) {
224         LOG(ERROR) << "Parameter string is invalid: " << params;
225         return false;
226     }
227     *base_device = pieces[0];
228     *cow_device = pieces[1];
229     return true;
230 }
231 
GetParameterString() const232 std::string DmTargetCrypt::GetParameterString() const {
233     std::vector<std::string> argv = {
234             cipher_,
235             key_,
236             std::to_string(iv_sector_offset_),
237             device_,
238             std::to_string(device_sector_),
239     };
240 
241     std::vector<std::string> extra_argv;
242     if (allow_discards_) extra_argv.emplace_back("allow_discards");
243     if (allow_encrypt_override_) extra_argv.emplace_back("allow_encrypt_override");
244     if (iv_large_sectors_) extra_argv.emplace_back("iv_large_sectors");
245     if (sector_size_) extra_argv.emplace_back("sector_size:" + std::to_string(sector_size_));
246 
247     if (!extra_argv.empty()) argv.emplace_back(std::to_string(extra_argv.size()));
248 
249     argv.insert(argv.end(), extra_argv.begin(), extra_argv.end());
250     return android::base::Join(argv, " ");
251 }
252 
Valid() const253 bool DmTargetDefaultKey::Valid() const {
254     if (!use_legacy_options_format_ && !set_dun_) return false;
255     return true;
256 }
257 
GetParameterString() const258 std::string DmTargetDefaultKey::GetParameterString() const {
259     std::vector<std::string> argv;
260     argv.emplace_back(cipher_);
261     argv.emplace_back(key_);
262     if (!use_legacy_options_format_) {
263         argv.emplace_back("0");  // iv_offset
264     }
265     argv.emplace_back(blockdev_);
266     argv.push_back(std::to_string(start_sector_));
267     std::vector<std::string> extra_argv;
268     if (use_legacy_options_format_) {
269         if (set_dun_) {  // v2 always sets the DUN.
270             extra_argv.emplace_back("set_dun");
271         }
272     } else {
273         extra_argv.emplace_back("allow_discards");
274         extra_argv.emplace_back("sector_size:4096");
275         extra_argv.emplace_back("iv_large_sectors");
276         if (is_hw_wrapped_) extra_argv.emplace_back("wrappedkey_v0");
277     }
278     if (!extra_argv.empty()) {
279         argv.emplace_back(std::to_string(extra_argv.size()));
280         argv.insert(argv.end(), extra_argv.begin(), extra_argv.end());
281     }
282     return android::base::Join(argv, " ");
283 }
284 
GetParameterString() const285 std::string DmTargetUser::GetParameterString() const {
286     std::vector<std::string> argv;
287     argv.push_back(std::to_string(start()));
288     argv.push_back(std::to_string(size()));
289     argv.push_back(control_device());
290     return android::base::Join(argv, " ");
291 }
292 
293 }  // namespace dm
294 }  // namespace android
295