Imported Upstream version 2.6.1
[platform/upstream/cryptsetup.git] / lib / verity / verity.c
1 /*
2  * dm-verity volume handling
3  *
4  * Copyright (C) 2012-2023 Red Hat, Inc. All rights reserved.
5  *
6  * This file is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Lesser General Public
8  * License as published by the Free Software Foundation; either
9  * version 2.1 of the License, or (at your option) any later version.
10  *
11  * This file is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14  * Lesser General Public License for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public
17  * License along with this file; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19  */
20
21 #include <errno.h>
22 #include <stdio.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <stdint.h>
26 #include <ctype.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #include <uuid/uuid.h>
30
31 #include "libcryptsetup.h"
32 #include "verity.h"
33 #include "internal.h"
34
35 #define VERITY_SIGNATURE "verity\0\0"
36
37 /* https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity#verity-superblock-format */
38 struct verity_sb {
39         uint8_t  signature[8];  /* "verity\0\0" */
40         uint32_t version;       /* superblock version */
41         uint32_t hash_type;     /* 0 - Chrome OS, 1 - normal */
42         uint8_t  uuid[16];      /* UUID of hash device */
43         uint8_t  algorithm[32];/* hash algorithm name */
44         uint32_t data_block_size; /* data block in bytes */
45         uint32_t hash_block_size; /* hash block in bytes */
46         uint64_t data_blocks;   /* number of data blocks */
47         uint16_t salt_size;     /* salt size */
48         uint8_t  _pad1[6];
49         uint8_t  salt[256];     /* salt */
50         uint8_t  _pad2[168];
51 } __attribute__((packed));
52
53 /* Read verity superblock from disk */
54 int VERITY_read_sb(struct crypt_device *cd,
55                    uint64_t sb_offset,
56                    char **uuid_string,
57                    struct crypt_params_verity *params)
58 {
59         struct device *device = crypt_metadata_device(cd);
60         struct verity_sb sb = {};
61         ssize_t hdr_size = sizeof(struct verity_sb);
62         int devfd, sb_version;
63
64         log_dbg(cd, "Reading VERITY header of size %zu on device %s, offset %" PRIu64 ".",
65                 sizeof(struct verity_sb), device_path(device), sb_offset);
66
67         if (params->flags & CRYPT_VERITY_NO_HEADER) {
68                 log_err(cd, _("Verity device %s does not use on-disk header."),
69                         device_path(device));
70                 return -EINVAL;
71         }
72
73         if (MISALIGNED_512(sb_offset)) {
74                 log_err(cd, _("Unsupported VERITY hash offset."));
75                 return -EINVAL;
76         }
77
78         devfd = device_open(cd, device, O_RDONLY);
79         if (devfd < 0) {
80                 log_err(cd, _("Cannot open device %s."), device_path(device));
81                 return -EINVAL;
82         }
83
84         if (read_lseek_blockwise(devfd, device_block_size(cd, device),
85                                  device_alignment(device), &sb, hdr_size,
86                                  sb_offset) < hdr_size)
87                 return -EIO;
88
89         if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
90                 log_dbg(cd, "No VERITY signature detected.");
91                 return -EINVAL;
92         }
93
94         sb_version = le32_to_cpu(sb.version);
95         if (sb_version != 1) {
96                 log_err(cd, _("Unsupported VERITY version %d."), sb_version);
97                 return -EINVAL;
98         }
99         params->hash_type = le32_to_cpu(sb.hash_type);
100         if (params->hash_type > VERITY_MAX_HASH_TYPE) {
101                 log_err(cd, _("Unsupported VERITY hash type %d."), params->hash_type);
102                 return -EINVAL;
103         }
104
105         params->data_block_size = le32_to_cpu(sb.data_block_size);
106         params->hash_block_size = le32_to_cpu(sb.hash_block_size);
107         if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
108             VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
109                 log_err(cd, _("Unsupported VERITY block size."));
110                 return -EINVAL;
111         }
112         params->data_size = le64_to_cpu(sb.data_blocks);
113
114         /* Update block size to be used for loop devices */
115         device_set_block_size(crypt_metadata_device(cd), params->hash_block_size);
116         device_set_block_size(crypt_data_device(cd), params->data_block_size);
117
118         params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
119         if (!params->hash_name)
120                 return -ENOMEM;
121         if (crypt_hash_size(params->hash_name) <= 0) {
122                 log_err(cd, _("Hash algorithm %s not supported."),
123                         params->hash_name);
124                 free(CONST_CAST(char*)params->hash_name);
125                 params->hash_name = NULL;
126                 return -EINVAL;
127         }
128
129         params->salt_size = le16_to_cpu(sb.salt_size);
130         if (params->salt_size > sizeof(sb.salt)) {
131                 log_err(cd, _("VERITY header corrupted."));
132                 free(CONST_CAST(char*)params->hash_name);
133                 params->hash_name = NULL;
134                 return -EINVAL;
135         }
136         params->salt = malloc(params->salt_size);
137         if (!params->salt) {
138                 free(CONST_CAST(char*)params->hash_name);
139                 params->hash_name = NULL;
140                 return -ENOMEM;
141         }
142         memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
143
144         if ((*uuid_string = malloc(40)))
145                 uuid_unparse(sb.uuid, *uuid_string);
146
147         params->hash_area_offset = sb_offset;
148         return 0;
149 }
150
151 static void _to_lower(char *str)
152 {
153         for(; *str; str++)
154                 if (isupper(*str))
155                         *str = tolower(*str);
156 }
157
158 /* Write verity superblock to disk */
159 int VERITY_write_sb(struct crypt_device *cd,
160                    uint64_t sb_offset,
161                    const char *uuid_string,
162                    struct crypt_params_verity *params)
163 {
164         struct device *device = crypt_metadata_device(cd);
165         struct verity_sb sb = {};
166         ssize_t hdr_size = sizeof(struct verity_sb);
167         size_t block_size;
168         char *algorithm;
169         uuid_t uuid;
170         int r, devfd;
171
172         log_dbg(cd, "Updating VERITY header of size %zu on device %s, offset %" PRIu64 ".",
173                 sizeof(struct verity_sb), device_path(device), sb_offset);
174
175         if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
176                 log_err(cd, _("Wrong VERITY UUID format provided on device %s."),
177                         device_path(device));
178                 return -EINVAL;
179         }
180
181         if (params->flags & CRYPT_VERITY_NO_HEADER) {
182                 log_err(cd, _("Verity device %s does not use on-disk header."),
183                         device_path(device));
184                 return -EINVAL;
185         }
186
187         /* Avoid possible increasing of image size - FEC could fail later because of it */
188         block_size = device_block_size(cd, device);
189         if (block_size > params->hash_block_size) {
190                 device_disable_direct_io(device);
191                 block_size = params->hash_block_size;
192         }
193
194         devfd = device_open(cd, device, O_RDWR);
195         if (devfd < 0) {
196                 log_err(cd, _("Cannot open device %s."), device_path(device));
197                 return -EINVAL;
198         }
199
200         memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
201         sb.version         = cpu_to_le32(1);
202         sb.hash_type       = cpu_to_le32(params->hash_type);
203         sb.data_block_size = cpu_to_le32(params->data_block_size);
204         sb.hash_block_size = cpu_to_le32(params->hash_block_size);
205         sb.salt_size       = cpu_to_le16(params->salt_size);
206         sb.data_blocks     = cpu_to_le64(params->data_size);
207
208         /* Kernel always use lower-case */
209         algorithm = (char *)sb.algorithm;
210         strncpy(algorithm, params->hash_name, sizeof(sb.algorithm)-1);
211         algorithm[sizeof(sb.algorithm)-1] = '\0';
212         _to_lower(algorithm);
213
214         memcpy(sb.salt, params->salt, params->salt_size);
215         memcpy(sb.uuid, uuid, sizeof(sb.uuid));
216
217         r = write_lseek_blockwise(devfd, block_size, device_alignment(device),
218                                   (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
219         if (r)
220                 log_err(cd, _("Error during update of verity header on device %s."),
221                         device_path(device));
222
223         device_sync(cd, device);
224
225         return r;
226 }
227
228 /* Calculate hash offset in hash blocks */
229 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
230 {
231         uint64_t hash_offset = params->hash_area_offset;
232
233         if (params->flags & CRYPT_VERITY_NO_HEADER)
234                 return hash_offset / params->hash_block_size;
235
236         hash_offset += sizeof(struct verity_sb);
237         hash_offset += params->hash_block_size - 1;
238
239         return hash_offset / params->hash_block_size;
240 }
241
242 int VERITY_UUID_generate(char **uuid_string)
243 {
244         uuid_t uuid;
245
246         *uuid_string = malloc(40);
247         if (!*uuid_string)
248                 return -ENOMEM;
249         uuid_generate(uuid);
250         uuid_unparse(uuid, *uuid_string);
251         return 0;
252 }
253
254 /* Activate verity device in kernel device-mapper */
255 int VERITY_activate(struct crypt_device *cd,
256                      const char *name,
257                      const char *root_hash,
258                      size_t root_hash_size,
259                      const char *signature_description,
260                      struct device *fec_device,
261                      struct crypt_params_verity *verity_hdr,
262                      uint32_t activation_flags)
263 {
264         uint32_t dmv_flags;
265         unsigned int fec_errors = 0;
266         int r, v;
267         struct crypt_dm_active_device dmd = {
268                 .size = verity_hdr->data_size * verity_hdr->data_block_size / 512,
269                 .flags = activation_flags,
270                 .uuid = crypt_get_uuid(cd),
271         };
272
273         log_dbg(cd, "Trying to activate VERITY device %s using hash %s.",
274                 name ?: "[none]", verity_hdr->hash_name);
275
276         if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
277                 if (signature_description) {
278                         log_err(cd, _("Root hash signature verification is not supported."));
279                         return -EINVAL;
280                 }
281
282                 log_dbg(cd, "Verification of data in userspace required.");
283                 r = VERITY_verify(cd, verity_hdr, root_hash, root_hash_size);
284
285                 if ((r == -EPERM || r == -EFAULT) && fec_device) {
286                         v = r;
287                         log_dbg(cd, "Verification failed, trying to repair with FEC device.");
288                         r = VERITY_FEC_process(cd, verity_hdr, fec_device, 1, &fec_errors);
289                         if (r < 0)
290                                 log_err(cd, _("Errors cannot be repaired with FEC device."));
291                         else if (fec_errors) {
292                                 log_err(cd, _("Found %u repairable errors with FEC device."),
293                                         fec_errors);
294                                 /* If root hash failed, we cannot be sure it was properly repaired */
295                         }
296                         if (v == -EFAULT)
297                                 r = -EPERM;
298                 }
299
300                 if (r < 0)
301                         return r;
302         }
303
304         if (!name)
305                 return 0;
306
307         r = device_block_adjust(cd, crypt_metadata_device(cd), DEV_OK,
308                                 0, NULL, NULL);
309         if (r)
310                 return r;
311
312         r = device_block_adjust(cd, crypt_data_device(cd), DEV_EXCL,
313                                 0, &dmd.size, &dmd.flags);
314         if (r)
315                 return r;
316
317         if (fec_device) {
318                 r = device_block_adjust(cd, fec_device, DEV_OK,
319                                         0, NULL, NULL);
320                 if (r)
321                         return r;
322         }
323
324         r = dm_verity_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd),
325                         crypt_metadata_device(cd), fec_device, root_hash,
326                         root_hash_size, signature_description,
327                         VERITY_hash_offset_block(verity_hdr),
328                         VERITY_FEC_blocks(cd, fec_device, verity_hdr), verity_hdr);
329
330         if (r)
331                 return r;
332
333         r = dm_create_device(cd, name, CRYPT_VERITY, &dmd);
334         if (r < 0 && (dm_flags(cd, DM_VERITY, &dmv_flags) || !(dmv_flags & DM_VERITY_SUPPORTED))) {
335                 log_err(cd, _("Kernel does not support dm-verity mapping."));
336                 r = -ENOTSUP;
337         }
338         if (r < 0 && signature_description && !(dmv_flags & DM_VERITY_SIGNATURE_SUPPORTED)) {
339                 log_err(cd, _("Kernel does not support dm-verity signature option."));
340                 r = -ENOTSUP;
341         }
342         if (r < 0)
343                 goto out;
344
345         r = dm_status_verity_ok(cd, name);
346         if (r < 0)
347                 goto out;
348
349         if (!r)
350                 log_err(cd, _("Verity device detected corruption after activation."));
351
352         r = 0;
353 out:
354         dm_targets_free(cd, &dmd);
355         return r;
356 }
357
358 int VERITY_dump(struct crypt_device *cd,
359                 struct crypt_params_verity *verity_hdr,
360                 const char *root_hash,
361                 unsigned int root_hash_size,
362                 struct device *fec_device)
363 {
364         uint64_t hash_blocks, verity_blocks, fec_blocks = 0, rs_blocks = 0;
365         bool fec_on_hash_device = false;
366
367         hash_blocks  = VERITY_hash_blocks(cd, verity_hdr);
368         verity_blocks = VERITY_hash_offset_block(verity_hdr) + hash_blocks;
369
370         if (fec_device && verity_hdr->fec_roots) {
371                 fec_blocks = VERITY_FEC_blocks(cd, fec_device, verity_hdr);
372                 rs_blocks  = VERITY_FEC_RS_blocks(fec_blocks, verity_hdr->fec_roots);
373                 fec_on_hash_device = device_is_identical(crypt_metadata_device(cd), fec_device) > 0;
374                 /*
375                 * No way to access fec_area_offset directly.
376                 * Assume FEC area starts directly after hash blocks.
377                 */
378                 if (fec_on_hash_device)
379                         verity_blocks += rs_blocks;
380         }
381
382         log_std(cd, "VERITY header information for %s\n", device_path(crypt_metadata_device(cd)));
383         log_std(cd, "UUID:            \t%s\n", crypt_get_uuid(cd) ?: "");
384         log_std(cd, "Hash type:       \t%u\n", verity_hdr->hash_type);
385         log_std(cd, "Data blocks:     \t%" PRIu64 "\n", verity_hdr->data_size);
386         log_std(cd, "Data block size: \t%u\n", verity_hdr->data_block_size);
387         log_std(cd, "Hash blocks:     \t%" PRIu64 "\n", hash_blocks);
388         log_std(cd, "Hash block size: \t%u\n", verity_hdr->hash_block_size);
389         log_std(cd, "Hash algorithm:  \t%s\n", verity_hdr->hash_name);
390         if (fec_device && fec_blocks) {
391                 log_std(cd, "FEC RS roots:   \t%" PRIu32 "\n", verity_hdr->fec_roots);
392                 log_std(cd, "FEC blocks:     \t%" PRIu64 "\n", rs_blocks);
393         }
394
395         log_std(cd, "Salt:            \t");
396         if (verity_hdr->salt_size)
397                 crypt_log_hex(cd, verity_hdr->salt, verity_hdr->salt_size, "", 0, NULL);
398         else
399                 log_std(cd, "-");
400         log_std(cd, "\n");
401
402         if (root_hash) {
403                 log_std(cd, "Root hash:      \t");
404                 crypt_log_hex(cd, root_hash, root_hash_size, "", 0, NULL);
405                 log_std(cd, "\n");
406         }
407
408         /* As dump can take only hash device, we have no idea about offsets here. */
409         if (verity_hdr->hash_area_offset == 0)
410                 log_std(cd, "Hash device size: \t%" PRIu64 " [bytes]\n", verity_blocks * verity_hdr->hash_block_size);
411
412         if (fec_device && verity_hdr->fec_area_offset == 0 && fec_blocks && !fec_on_hash_device)
413                 log_std(cd, "FEC device size: \t%" PRIu64 " [bytes]\n", rs_blocks * verity_hdr->data_block_size);
414
415         return 0;
416 }