5108e9e3dcbcf62d75f65bf427fa2b79e0aae2e3
[platform/upstream/cryptsetup.git] / lib / verity / verity.c
1 /*
2  * dm-verity volume handling
3  *
4  * Copyright (C) 2012, 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 <fcntl.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         int bsize = device_block_size(device);
62         struct verity_sb sb = {};
63         ssize_t hdr_size = sizeof(struct verity_sb);
64         int devfd = 0, sb_version;
65
66         log_dbg("Reading VERITY header of size %zu on device %s, offset %" PRIu64 ".",
67                 sizeof(struct verity_sb), device_path(device), sb_offset);
68
69         if (params->flags & CRYPT_VERITY_NO_HEADER) {
70                 log_err(cd, _("Verity device %s doesn't use on-disk header.\n"),
71                         device_path(device));
72                 return -EINVAL;
73         }
74
75         if (sb_offset % 512) {
76                 log_err(cd, _("Unsupported VERITY hash offset.\n"));
77                 return -EINVAL;
78         }
79
80         devfd = device_open(device, O_RDONLY);
81         if(devfd == -1) {
82                 log_err(cd, _("Cannot open device %s.\n"), device_path(device));
83                 return -EINVAL;
84         }
85
86         if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
87            read_blockwise(devfd, bsize, &sb, hdr_size) < hdr_size) {
88                 close(devfd);
89                 return -EIO;
90         }
91         close(devfd);
92
93         if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
94                 log_err(cd, _("Device %s is not a valid VERITY device.\n"),
95                         device_path(device));
96                 return -EINVAL;
97         }
98
99         sb_version = le32_to_cpu(sb.version);
100         if (sb_version != 1) {
101                 log_err(cd, _("Unsupported VERITY version %d.\n"), sb_version);
102                 return -EINVAL;
103         }
104         params->hash_type = le32_to_cpu(sb.hash_type);
105         if (params->hash_type > VERITY_MAX_HASH_TYPE) {
106                 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
107                 return -EINVAL;
108         }
109
110         params->data_block_size = le32_to_cpu(sb.data_block_size);
111         params->hash_block_size = le32_to_cpu(sb.hash_block_size);
112         if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
113             VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
114                 log_err(cd, _("Unsupported VERITY block size.\n"));
115                 return -EINVAL;
116         }
117         params->data_size = le64_to_cpu(sb.data_blocks);
118
119         params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
120         if (!params->hash_name)
121                 return -ENOMEM;
122         if (crypt_hash_size(params->hash_name) <= 0) {
123                 log_err(cd, _("Hash algorithm %s not supported.\n"),
124                         params->hash_name);
125                 free(CONST_CAST(char*)params->hash_name);
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.\n"));
132                 free(CONST_CAST(char*)params->hash_name);
133                 return -EINVAL;
134         }
135         params->salt = malloc(params->salt_size);
136         if (!params->salt) {
137                 free(CONST_CAST(char*)params->hash_name);
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 /* Write verity superblock to disk */
150 int VERITY_write_sb(struct crypt_device *cd,
151                    uint64_t sb_offset,
152                    const char *uuid_string,
153                    struct crypt_params_verity *params)
154 {
155         struct device *device = crypt_metadata_device(cd);
156         int bsize = device_block_size(device);
157         struct verity_sb sb = {};
158         ssize_t hdr_size = sizeof(struct verity_sb);
159         uuid_t uuid;
160         int r, devfd = 0;
161
162         log_dbg("Updating VERITY header of size %zu on device %s, offset %" PRIu64 ".",
163                 sizeof(struct verity_sb), device_path(device), sb_offset);
164
165         if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
166                 log_err(cd, _("Wrong VERITY UUID format provided on device %s.\n"),
167                         device_path(device));
168                 return -EINVAL;
169         }
170
171         if (params->flags & CRYPT_VERITY_NO_HEADER) {
172                 log_err(cd, _("Verity device %s doesn't use on-disk header.\n"),
173                         device_path(device));
174                 return -EINVAL;
175         }
176
177         devfd = device_open(device, O_RDWR);
178         if(devfd == -1) {
179                 log_err(cd, _("Cannot open device %s.\n"), device_path(device));
180                 return -EINVAL;
181         }
182
183         memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
184         sb.version         = cpu_to_le32(1);
185         sb.hash_type       = cpu_to_le32(params->hash_type);
186         sb.data_block_size = cpu_to_le32(params->data_block_size);
187         sb.hash_block_size = cpu_to_le32(params->hash_block_size);
188         sb.salt_size       = cpu_to_le16(params->salt_size);
189         sb.data_blocks     = cpu_to_le64(params->data_size);
190         strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
191         memcpy(sb.salt, params->salt, params->salt_size);
192         memcpy(sb.uuid, uuid, sizeof(sb.uuid));
193
194         r = write_lseek_blockwise(devfd, bsize, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
195         if (r)
196                 log_err(cd, _("Error during update of verity header on device %s.\n"),
197                         device_path(device));
198         close(devfd);
199
200         return r;
201 }
202
203 /* Calculate hash offset in hash blocks */
204 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
205 {
206         uint64_t hash_offset = params->hash_area_offset;
207
208         if (params->flags & CRYPT_VERITY_NO_HEADER)
209                 return hash_offset / params->hash_block_size;
210
211         hash_offset += sizeof(struct verity_sb);
212         hash_offset += params->hash_block_size - 1;
213
214         return hash_offset / params->hash_block_size;
215 }
216
217 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
218 {
219         uuid_t uuid;
220
221         if (!(*uuid_string = malloc(40)))
222                 return -ENOMEM;
223         uuid_generate(uuid);
224         uuid_unparse(uuid, *uuid_string);
225         return 0;
226 }
227
228 /* Activate verity device in kernel device-mapper */
229 int VERITY_activate(struct crypt_device *cd,
230                      const char *name,
231                      const char *root_hash,
232                      size_t root_hash_size,
233                      struct crypt_params_verity *verity_hdr,
234                      uint32_t activation_flags)
235 {
236         struct crypt_dm_active_device dmd;
237         int r;
238
239         log_dbg("Trying to activate VERITY device %s using hash %s.",
240                 name ?: "[none]", verity_hdr->hash_name);
241
242         if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
243                 log_dbg("Verification of data in userspace required.");
244                 r = VERITY_verify(cd, verity_hdr,
245                                   root_hash, root_hash_size);
246                 if (r < 0)
247                         return r;
248         }
249
250         if (!name)
251                 return 0;
252
253         dmd.target = DM_VERITY;
254         dmd.data_device = crypt_data_device(cd);
255         dmd.u.verity.hash_device = crypt_metadata_device(cd);
256         dmd.u.verity.root_hash = root_hash;
257         dmd.u.verity.root_hash_size = root_hash_size;
258         dmd.u.verity.hash_offset = VERITY_hash_offset_block(verity_hdr),
259         dmd.flags = activation_flags;
260         dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
261         dmd.uuid = crypt_get_uuid(cd);
262         dmd.u.verity.vp = verity_hdr;
263
264         r = device_block_adjust(cd, dmd.u.verity.hash_device, DEV_OK,
265                                 0, NULL, NULL);
266         if (r)
267                 return r;
268
269         r = device_block_adjust(cd, dmd.data_device, DEV_EXCL,
270                                 0, &dmd.size, &dmd.flags);
271         if (r)
272                 return r;
273
274         r = dm_create_device(cd, name, CRYPT_VERITY, &dmd, 0);
275         if (r < 0 && !(dm_flags() & DM_VERITY_SUPPORTED)) {
276                 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
277                 return -ENOTSUP;
278         }
279         if (r < 0)
280                 return r;
281
282         r = dm_status_verity_ok(cd, name);
283         if (r < 0)
284                 return r;
285
286         if (!r)
287                 log_err(cd, _("Verity device detected corruption after activation.\n"));
288         return 0;
289 }