af31784d6984f1ef790270fd9dd1e7936314a459
[platform/upstream/cryptsetup.git] / lib / verity / verity.c
1 /*
2  * dm-verity volume handling
3  *
4  * Copyright (C) 2012-2020 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 <sys/types.h>
27 #include <sys/stat.h>
28 #include <netinet/in.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_err(cd, _("Device %s is not a valid VERITY device."),
91                         device_path(device));
92                 return -EINVAL;
93         }
94
95         sb_version = le32_to_cpu(sb.version);
96         if (sb_version != 1) {
97                 log_err(cd, _("Unsupported VERITY version %d."), sb_version);
98                 return -EINVAL;
99         }
100         params->hash_type = le32_to_cpu(sb.hash_type);
101         if (params->hash_type > VERITY_MAX_HASH_TYPE) {
102                 log_err(cd, _("Unsupported VERITY hash type %d."), params->hash_type);
103                 return -EINVAL;
104         }
105
106         params->data_block_size = le32_to_cpu(sb.data_block_size);
107         params->hash_block_size = le32_to_cpu(sb.hash_block_size);
108         if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
109             VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
110                 log_err(cd, _("Unsupported VERITY block size."));
111                 return -EINVAL;
112         }
113         params->data_size = le64_to_cpu(sb.data_blocks);
114
115         params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
116         if (!params->hash_name)
117                 return -ENOMEM;
118         if (crypt_hash_size(params->hash_name) <= 0) {
119                 log_err(cd, _("Hash algorithm %s not supported."),
120                         params->hash_name);
121                 free(CONST_CAST(char*)params->hash_name);
122                 params->hash_name = NULL;
123                 return -EINVAL;
124         }
125
126         params->salt_size = le16_to_cpu(sb.salt_size);
127         if (params->salt_size > sizeof(sb.salt)) {
128                 log_err(cd, _("VERITY header corrupted."));
129                 free(CONST_CAST(char*)params->hash_name);
130                 params->hash_name = NULL;
131                 return -EINVAL;
132         }
133         params->salt = malloc(params->salt_size);
134         if (!params->salt) {
135                 free(CONST_CAST(char*)params->hash_name);
136                 params->hash_name = NULL;
137                 return -ENOMEM;
138         }
139         memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
140
141         if ((*uuid_string = malloc(40)))
142                 uuid_unparse(sb.uuid, *uuid_string);
143
144         params->hash_area_offset = sb_offset;
145         return 0;
146 }
147
148 /* Write verity superblock to disk */
149 int VERITY_write_sb(struct crypt_device *cd,
150                    uint64_t sb_offset,
151                    const char *uuid_string,
152                    struct crypt_params_verity *params)
153 {
154         struct device *device = crypt_metadata_device(cd);
155         struct verity_sb sb = {};
156         ssize_t hdr_size = sizeof(struct verity_sb);
157         char *algorithm;
158         uuid_t uuid;
159         int r, devfd;
160
161         log_dbg(cd, "Updating VERITY header of size %zu on device %s, offset %" PRIu64 ".",
162                 sizeof(struct verity_sb), device_path(device), sb_offset);
163
164         if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
165                 log_err(cd, _("Wrong VERITY UUID format provided on device %s."),
166                         device_path(device));
167                 return -EINVAL;
168         }
169
170         if (params->flags & CRYPT_VERITY_NO_HEADER) {
171                 log_err(cd, _("Verity device %s does not use on-disk header."),
172                         device_path(device));
173                 return -EINVAL;
174         }
175
176         devfd = device_open(cd, device, O_RDWR);
177         if (devfd < 0) {
178                 log_err(cd, _("Cannot open device %s."), device_path(device));
179                 return -EINVAL;
180         }
181
182         memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
183         sb.version         = cpu_to_le32(1);
184         sb.hash_type       = cpu_to_le32(params->hash_type);
185         sb.data_block_size = cpu_to_le32(params->data_block_size);
186         sb.hash_block_size = cpu_to_le32(params->hash_block_size);
187         sb.salt_size       = cpu_to_le16(params->salt_size);
188         sb.data_blocks     = cpu_to_le64(params->data_size);
189         algorithm = (char *)sb.algorithm;
190         algorithm[sizeof(sb.algorithm)-1] = '\0';
191         strncpy(algorithm, params->hash_name, sizeof(sb.algorithm)-1);
192         memcpy(sb.salt, params->salt, params->salt_size);
193         memcpy(sb.uuid, uuid, sizeof(sb.uuid));
194
195         r = write_lseek_blockwise(devfd, device_block_size(cd, device), device_alignment(device),
196                                   (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
197         if (r)
198                 log_err(cd, _("Error during update of verity header on device %s."),
199                         device_path(device));
200
201         device_sync(cd, device);
202
203         return r;
204 }
205
206 /* Calculate hash offset in hash blocks */
207 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
208 {
209         uint64_t hash_offset = params->hash_area_offset;
210
211         if (params->flags & CRYPT_VERITY_NO_HEADER)
212                 return hash_offset / params->hash_block_size;
213
214         hash_offset += sizeof(struct verity_sb);
215         hash_offset += params->hash_block_size - 1;
216
217         return hash_offset / params->hash_block_size;
218 }
219
220 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
221 {
222         uuid_t uuid;
223
224         *uuid_string = malloc(40);
225         if (!*uuid_string)
226                 return -ENOMEM;
227         uuid_generate(uuid);
228         uuid_unparse(uuid, *uuid_string);
229         return 0;
230 }
231
232 /* Activate verity device in kernel device-mapper */
233 int VERITY_activate(struct crypt_device *cd,
234                      const char *name,
235                      const char *root_hash,
236                      size_t root_hash_size,
237                      const char *signature_description,
238                      struct device *fec_device,
239                      struct crypt_params_verity *verity_hdr,
240                      uint32_t activation_flags)
241 {
242         uint32_t dmv_flags;
243         unsigned int fec_errors = 0;
244         int r;
245         struct crypt_dm_active_device dmd = {
246                 .size = verity_hdr->data_size * verity_hdr->data_block_size / 512,
247                 .flags = activation_flags,
248                 .uuid = crypt_get_uuid(cd),
249         };
250
251         log_dbg(cd, "Trying to activate VERITY device %s using hash %s.",
252                 name ?: "[none]", verity_hdr->hash_name);
253
254         if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
255                 if (signature_description) {
256                         log_err(cd, _("Root hash signature verification is not supported."));
257                         return -EINVAL;
258                 }
259
260                 log_dbg(cd, "Verification of data in userspace required.");
261                 r = VERITY_verify(cd, verity_hdr, root_hash, root_hash_size);
262
263                 if (r == -EPERM && fec_device) {
264                         log_dbg(cd, "Verification failed, trying to repair with FEC device.");
265                         r = VERITY_FEC_process(cd, verity_hdr, fec_device, 1, &fec_errors);
266                         if (r < 0)
267                                 log_err(cd, _("Errors cannot be repaired with FEC device."));
268                         else if (fec_errors)
269                                 log_err(cd, _("Found %u repairable errors with FEC device."),
270                                         fec_errors);
271                 }
272
273                 if (r < 0)
274                         return r;
275         }
276
277         if (!name)
278                 return 0;
279
280         r = device_block_adjust(cd, crypt_metadata_device(cd), DEV_OK,
281                                 0, NULL, NULL);
282         if (r)
283                 return r;
284
285         r = device_block_adjust(cd, crypt_data_device(cd), DEV_EXCL,
286                                 0, &dmd.size, &dmd.flags);
287         if (r)
288                 return r;
289
290         if (fec_device) {
291                 r = device_block_adjust(cd, fec_device, DEV_OK,
292                                         0, NULL, NULL);
293                 if (r)
294                         return r;
295         }
296
297         r = dm_verity_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd),
298                         crypt_metadata_device(cd), fec_device, root_hash,
299                         root_hash_size, signature_description,
300                         VERITY_hash_offset_block(verity_hdr),
301                         VERITY_hash_blocks(cd, verity_hdr), verity_hdr);
302
303         if (r)
304                 return r;
305
306         r = dm_create_device(cd, name, CRYPT_VERITY, &dmd);
307         if (r < 0 && (dm_flags(cd, DM_VERITY, &dmv_flags) || !(dmv_flags & DM_VERITY_SUPPORTED))) {
308                 log_err(cd, _("Kernel does not support dm-verity mapping."));
309                 r = -ENOTSUP;
310         }
311         if (r < 0 && signature_description && !(dmv_flags & DM_VERITY_SIGNATURE_SUPPORTED)) {
312                 log_err(cd, _("Kernel does not support dm-verity signature option."));
313                 r = -ENOTSUP;
314         }
315         if (r < 0)
316                 goto out;
317
318         r = dm_status_verity_ok(cd, name);
319         if (r < 0)
320                 goto out;
321
322         if (!r)
323                 log_err(cd, _("Verity device detected corruption after activation."));
324
325         r = 0;
326 out:
327         dm_targets_free(cd, &dmd);
328         return r;
329 }