Merge "Merge branch 'upstream' into tizen" into tizen
[platform/upstream/cryptsetup.git] / lib / verity / verity.c
1 /*
2  * dm-verity volume handling
3  *
4  * Copyright (C) 2012-2021 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 <netinet/in.h>
30 #include <uuid/uuid.h>
31
32 #include "libcryptsetup.h"
33 #include "verity.h"
34 #include "internal.h"
35
36 #define VERITY_SIGNATURE "verity\0\0"
37
38 /* https://gitlab.com/cryptsetup/cryptsetup/wikis/DMVerity#verity-superblock-format */
39 struct verity_sb {
40         uint8_t  signature[8];  /* "verity\0\0" */
41         uint32_t version;       /* superblock version */
42         uint32_t hash_type;     /* 0 - Chrome OS, 1 - normal */
43         uint8_t  uuid[16];      /* UUID of hash device */
44         uint8_t  algorithm[32];/* hash algorithm name */
45         uint32_t data_block_size; /* data block in bytes */
46         uint32_t hash_block_size; /* hash block in bytes */
47         uint64_t data_blocks;   /* number of data blocks */
48         uint16_t salt_size;     /* salt size */
49         uint8_t  _pad1[6];
50         uint8_t  salt[256];     /* salt */
51         uint8_t  _pad2[168];
52 } __attribute__((packed));
53
54 /* Read verity superblock from disk */
55 int VERITY_read_sb(struct crypt_device *cd,
56                    uint64_t sb_offset,
57                    char **uuid_string,
58                    struct crypt_params_verity *params)
59 {
60         struct device *device = crypt_metadata_device(cd);
61         struct verity_sb sb = {};
62         ssize_t hdr_size = sizeof(struct verity_sb);
63         int devfd, sb_version;
64
65         log_dbg(cd, "Reading VERITY header of size %zu on device %s, offset %" PRIu64 ".",
66                 sizeof(struct verity_sb), device_path(device), sb_offset);
67
68         if (params->flags & CRYPT_VERITY_NO_HEADER) {
69                 log_err(cd, _("Verity device %s does not use on-disk header."),
70                         device_path(device));
71                 return -EINVAL;
72         }
73
74         if (MISALIGNED_512(sb_offset)) {
75                 log_err(cd, _("Unsupported VERITY hash offset."));
76                 return -EINVAL;
77         }
78
79         devfd = device_open(cd, device, O_RDONLY);
80         if (devfd < 0) {
81                 log_err(cd, _("Cannot open device %s."), device_path(device));
82                 return -EINVAL;
83         }
84
85         if (read_lseek_blockwise(devfd, device_block_size(cd, device),
86                                  device_alignment(device), &sb, hdr_size,
87                                  sb_offset) < hdr_size)
88                 return -EIO;
89
90         if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
91                 log_err(cd, _("Device %s is not a valid VERITY device."),
92                         device_path(device));
93                 return -EINVAL;
94         }
95
96         sb_version = le32_to_cpu(sb.version);
97         if (sb_version != 1) {
98                 log_err(cd, _("Unsupported VERITY version %d."), sb_version);
99                 return -EINVAL;
100         }
101         params->hash_type = le32_to_cpu(sb.hash_type);
102         if (params->hash_type > VERITY_MAX_HASH_TYPE) {
103                 log_err(cd, _("Unsupported VERITY hash type %d."), params->hash_type);
104                 return -EINVAL;
105         }
106
107         params->data_block_size = le32_to_cpu(sb.data_block_size);
108         params->hash_block_size = le32_to_cpu(sb.hash_block_size);
109         if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
110             VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
111                 log_err(cd, _("Unsupported VERITY block size."));
112                 return -EINVAL;
113         }
114         params->data_size = le64_to_cpu(sb.data_blocks);
115
116         params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
117         if (!params->hash_name)
118                 return -ENOMEM;
119         if (crypt_hash_size(params->hash_name) <= 0) {
120                 log_err(cd, _("Hash algorithm %s not supported."),
121                         params->hash_name);
122                 free(CONST_CAST(char*)params->hash_name);
123                 params->hash_name = NULL;
124                 return -EINVAL;
125         }
126
127         params->salt_size = le16_to_cpu(sb.salt_size);
128         if (params->salt_size > sizeof(sb.salt)) {
129                 log_err(cd, _("VERITY header corrupted."));
130                 free(CONST_CAST(char*)params->hash_name);
131                 params->hash_name = NULL;
132                 return -EINVAL;
133         }
134         params->salt = malloc(params->salt_size);
135         if (!params->salt) {
136                 free(CONST_CAST(char*)params->hash_name);
137                 params->hash_name = NULL;
138                 return -ENOMEM;
139         }
140         memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
141
142         if ((*uuid_string = malloc(40)))
143                 uuid_unparse(sb.uuid, *uuid_string);
144
145         params->hash_area_offset = sb_offset;
146         return 0;
147 }
148
149 static void _to_lower(char *str)
150 {
151         for(; *str; str++)
152                 if (isupper(*str))
153                         *str = tolower(*str);
154 }
155
156 /* Write verity superblock to disk */
157 int VERITY_write_sb(struct crypt_device *cd,
158                    uint64_t sb_offset,
159                    const char *uuid_string,
160                    struct crypt_params_verity *params)
161 {
162         struct device *device = crypt_metadata_device(cd);
163         struct verity_sb sb = {};
164         ssize_t hdr_size = sizeof(struct verity_sb);
165         size_t block_size;
166         char *algorithm;
167         uuid_t uuid;
168         int r, devfd;
169
170         log_dbg(cd, "Updating VERITY header of size %zu on device %s, offset %" PRIu64 ".",
171                 sizeof(struct verity_sb), device_path(device), sb_offset);
172
173         if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
174                 log_err(cd, _("Wrong VERITY UUID format provided on device %s."),
175                         device_path(device));
176                 return -EINVAL;
177         }
178
179         if (params->flags & CRYPT_VERITY_NO_HEADER) {
180                 log_err(cd, _("Verity device %s does not use on-disk header."),
181                         device_path(device));
182                 return -EINVAL;
183         }
184
185         /* Avoid possible increasing of image size - FEC could fail later because of it */
186         block_size = device_block_size(cd, device);
187         if (block_size > params->hash_block_size) {
188                 device_disable_direct_io(device);
189                 block_size = params->hash_block_size;
190         }
191
192         devfd = device_open(cd, device, O_RDWR);
193         if (devfd < 0) {
194                 log_err(cd, _("Cannot open device %s."), device_path(device));
195                 return -EINVAL;
196         }
197
198         memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
199         sb.version         = cpu_to_le32(1);
200         sb.hash_type       = cpu_to_le32(params->hash_type);
201         sb.data_block_size = cpu_to_le32(params->data_block_size);
202         sb.hash_block_size = cpu_to_le32(params->hash_block_size);
203         sb.salt_size       = cpu_to_le16(params->salt_size);
204         sb.data_blocks     = cpu_to_le64(params->data_size);
205
206         /* Kernel always use lower-case */
207         algorithm = (char *)sb.algorithm;
208         strncpy(algorithm, params->hash_name, sizeof(sb.algorithm)-1);
209         algorithm[sizeof(sb.algorithm)-1] = '\0';
210         _to_lower(algorithm);
211
212         memcpy(sb.salt, params->salt, params->salt_size);
213         memcpy(sb.uuid, uuid, sizeof(sb.uuid));
214
215         r = write_lseek_blockwise(devfd, block_size, device_alignment(device),
216                                   (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
217         if (r)
218                 log_err(cd, _("Error during update of verity header on device %s."),
219                         device_path(device));
220
221         device_sync(cd, device);
222
223         return r;
224 }
225
226 /* Calculate hash offset in hash blocks */
227 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
228 {
229         uint64_t hash_offset = params->hash_area_offset;
230
231         if (params->flags & CRYPT_VERITY_NO_HEADER)
232                 return hash_offset / params->hash_block_size;
233
234         hash_offset += sizeof(struct verity_sb);
235         hash_offset += params->hash_block_size - 1;
236
237         return hash_offset / params->hash_block_size;
238 }
239
240 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
241 {
242         uuid_t uuid;
243
244         *uuid_string = malloc(40);
245         if (!*uuid_string)
246                 return -ENOMEM;
247         uuid_generate(uuid);
248         uuid_unparse(uuid, *uuid_string);
249         return 0;
250 }
251
252 /* Activate verity device in kernel device-mapper */
253 int VERITY_activate(struct crypt_device *cd,
254                      const char *name,
255                      const char *root_hash,
256                      size_t root_hash_size,
257                      const char *signature_description,
258                      struct device *fec_device,
259                      struct crypt_params_verity *verity_hdr,
260                      uint32_t activation_flags)
261 {
262         uint32_t dmv_flags;
263         unsigned int fec_errors = 0;
264         int r, v;
265         struct crypt_dm_active_device dmd = {
266                 .size = verity_hdr->data_size * verity_hdr->data_block_size / 512,
267                 .flags = activation_flags,
268                 .uuid = crypt_get_uuid(cd),
269         };
270
271         log_dbg(cd, "Trying to activate VERITY device %s using hash %s.",
272                 name ?: "[none]", verity_hdr->hash_name);
273
274         if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
275                 if (signature_description) {
276                         log_err(cd, _("Root hash signature verification is not supported."));
277                         return -EINVAL;
278                 }
279
280                 log_dbg(cd, "Verification of data in userspace required.");
281                 r = VERITY_verify(cd, verity_hdr, root_hash, root_hash_size);
282
283                 if ((r == -EPERM || r == -EFAULT) && fec_device) {
284                         v = r;
285                         log_dbg(cd, "Verification failed, trying to repair with FEC device.");
286                         r = VERITY_FEC_process(cd, verity_hdr, fec_device, 1, &fec_errors);
287                         if (r < 0)
288                                 log_err(cd, _("Errors cannot be repaired with FEC device."));
289                         else if (fec_errors) {
290                                 log_err(cd, _("Found %u repairable errors with FEC device."),
291                                         fec_errors);
292                                 /* If root hash failed, we cannot be sure it was properly repaired */
293                         }
294                         if (v == -EFAULT)
295                                 r = -EPERM;
296                 }
297
298                 if (r < 0)
299                         return r;
300         }
301
302         if (!name)
303                 return 0;
304
305         r = device_block_adjust(cd, crypt_metadata_device(cd), DEV_OK,
306                                 0, NULL, NULL);
307         if (r)
308                 return r;
309
310         r = device_block_adjust(cd, crypt_data_device(cd), DEV_EXCL,
311                                 0, &dmd.size, &dmd.flags);
312         if (r)
313                 return r;
314
315         if (fec_device) {
316                 r = device_block_adjust(cd, fec_device, DEV_OK,
317                                         0, NULL, NULL);
318                 if (r)
319                         return r;
320         }
321
322         r = dm_verity_target_set(&dmd.segment, 0, dmd.size, crypt_data_device(cd),
323                         crypt_metadata_device(cd), fec_device, root_hash,
324                         root_hash_size, signature_description,
325                         VERITY_hash_offset_block(verity_hdr),
326                         VERITY_FEC_blocks(cd, fec_device, verity_hdr), verity_hdr);
327
328         if (r)
329                 return r;
330
331         r = dm_create_device(cd, name, CRYPT_VERITY, &dmd);
332         if (r < 0 && (dm_flags(cd, DM_VERITY, &dmv_flags) || !(dmv_flags & DM_VERITY_SUPPORTED))) {
333                 log_err(cd, _("Kernel does not support dm-verity mapping."));
334                 r = -ENOTSUP;
335         }
336         if (r < 0 && signature_description && !(dmv_flags & DM_VERITY_SIGNATURE_SUPPORTED)) {
337                 log_err(cd, _("Kernel does not support dm-verity signature option."));
338                 r = -ENOTSUP;
339         }
340         if (r < 0)
341                 goto out;
342
343         r = dm_status_verity_ok(cd, name);
344         if (r < 0)
345                 goto out;
346
347         if (!r)
348                 log_err(cd, _("Verity device detected corruption after activation."));
349
350         r = 0;
351 out:
352         dm_targets_free(cd, &dmd);
353         return r;
354 }