Merge branch 'veritysetup'
[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 program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License
8  * version 2 as published by the Free Software Foundation.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18  */
19
20 #include <errno.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <stdint.h>
25 #include <sys/types.h>
26 #include <sys/stat.h>
27 #include <fcntl.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 #define NEW_SB 1
38
39 #ifndef NEW_SB
40 struct verity_sb {
41         uint8_t signature[8];
42         uint8_t version;
43         uint8_t data_block_bits;
44         uint8_t hash_block_bits;
45         uint8_t pad1[1];
46         uint16_t salt_size;
47         uint8_t pad2[2];
48         uint32_t data_blocks_hi;
49         uint32_t data_blocks_lo;
50         uint8_t algorithm[16];
51         uint8_t salt[384];
52         uint8_t pad3[88];
53 };
54 #else
55 struct verity_sb {
56         uint8_t  signature[8];  /* "verity\0\0" */
57         uint32_t version;       /* superblock version */
58         uint32_t hash_type;     /* 0 - Chrome OS, 1 - normal */
59         uint8_t  uuid[16];      /* UUID of hash device */
60         uint8_t  algorithm[32];/* hash algorithm name */
61         uint64_t data_block_size; /* data block in bytes */
62         uint64_t hash_block_size; /* hash block in bytes */
63         uint64_t data_blocks;   /* number of data blocks */
64         uint64_t salt_size;     /* salt size */
65         uint8_t  salt[256];     /* salt */
66         uint8_t  _pad[160];
67 } __attribute__((packed));
68 #endif
69
70 /* Read verity superblock from disk */
71 int VERITY_read_sb(struct crypt_device *cd,
72                    const char *device,
73                    uint64_t sb_offset,
74                    char **uuid_string,
75                    struct crypt_params_verity *params)
76 {
77         struct verity_sb sb = {};
78         ssize_t hdr_size = sizeof(struct verity_sb);
79         int devfd = 0, sb_version;
80 #ifndef NEW_SB
81         uint64_t sb_data_blocks;
82 #endif
83         log_dbg("Reading VERITY header of size %u on device %s, offset %" PRIu64 ".",
84                 sizeof(struct verity_sb), device, sb_offset);
85
86         if (params->flags & CRYPT_VERITY_NO_HEADER) {
87                 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
88                 return -EINVAL;
89         }
90
91         if (sb_offset % 512) {
92                 log_err(cd, _("Unsupported VERITY hash offset.\n"));
93                 return -EINVAL;
94         }
95
96         devfd = open(device ,O_RDONLY | O_DIRECT);
97         if(devfd == -1) {
98                 log_err(cd, _("Cannot open device %s.\n"), device);
99                 return -EINVAL;
100         }
101
102         if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
103            read_blockwise(devfd, &sb, hdr_size) < hdr_size) {
104                 close(devfd);
105                 return -EIO;
106         }
107         close(devfd);
108
109         if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
110                 log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
111                 return -EINVAL;
112         }
113 #ifndef NEW_SB
114         if (sb.version > 1) {
115                 log_err(cd, _("Unsupported VERITY version %d.\n"), sb.version);
116                 return -EINVAL;
117         }
118
119         if (sb.data_block_bits < 9 || sb.data_block_bits >= 31 ||
120             sb.hash_block_bits < 9 || sb.hash_block_bits >= 31 ||
121             !memchr(sb.algorithm, 0, sizeof(sb.algorithm)) ||
122             ntohs(sb.salt_size) > 256) {
123                 log_err(cd, _("VERITY header corrupted.\n"));
124                 return -EINVAL;
125         }
126
127         sb_data_blocks = ((uint64_t)ntohl(sb.data_blocks_hi) << 31 << 1) |
128                                 ntohl(sb.data_blocks_lo);
129
130         params->hash_name = strdup((const char*)sb.algorithm);
131         if (!params->hash_name)
132                 return -ENOMEM;
133         params->data_block_size = 1 << sb.data_block_bits;
134         params->hash_block_size = 1 << sb.hash_block_bits;
135         params->data_size = sb_data_blocks;
136         params->salt_size = ntohs(sb.salt_size);
137         params->salt = malloc(params->salt_size);
138         if (!params->salt)
139                 return -ENOMEM;
140         memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
141         params->hash_type = sb.version;
142 #else
143         sb_version = le32_to_cpu(sb.version);
144         if (sb_version != 1) {
145                 log_err(cd, _("Unsupported VERITY version %d.\n"), sb_version);
146                 return -EINVAL;
147         }
148         params->hash_type = le32_to_cpu(sb.hash_type);
149         if (params->hash_type > VERITY_MAX_HASH_TYPE) {
150                 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
151                 return -EINVAL;
152         }
153
154         params->data_block_size = le64_to_cpu(sb.data_block_size);
155         params->hash_block_size = le64_to_cpu(sb.hash_block_size);
156         if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
157             VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
158                 log_err(cd, _("Unsupported VERITY block size.\n"));
159                 return -EINVAL;
160         }
161         params->data_size = le64_to_cpu(sb.data_blocks);
162
163         params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
164         if (!params->hash_name)
165                 return -ENOMEM;
166         if (crypt_hash_size(params->hash_name) <= 0) {
167                 log_err(cd, _("Hash algorithm %s not supported.\n"),
168                         params->hash_name);
169                 free(CONST_CAST(char*)params->hash_name);
170                 return -EINVAL;
171         }
172
173         params->salt_size = le64_to_cpu(sb.salt_size);
174         if (params->salt_size > sizeof(sb.salt)) {
175                 log_err(cd, _("VERITY header corrupted.\n"));
176                 free(CONST_CAST(char*)params->hash_name);
177                 return -EINVAL;
178         }
179         params->salt = malloc(params->salt_size);
180         if (!params->salt) {
181                 free(CONST_CAST(char*)params->hash_name);
182                 return -ENOMEM;
183         }
184         memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
185
186         if ((*uuid_string = malloc(40)))
187                 uuid_unparse(sb.uuid, *uuid_string);
188 #endif
189         params->hash_area_offset = sb_offset;
190         return 0;
191 }
192
193 /* Write verity superblock to disk */
194 int VERITY_write_sb(struct crypt_device *cd,
195                    const char *device,
196                    uint64_t sb_offset,
197                    const char *uuid_string,
198                    struct crypt_params_verity *params)
199 {
200         struct verity_sb sb = {};
201         ssize_t hdr_size = sizeof(struct verity_sb);
202         uuid_t uuid;
203         int r, devfd = 0;
204
205         log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
206                 sizeof(struct verity_sb), device, sb_offset);
207
208         if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
209                 log_err(cd, _("Wrong VERITY UUID format provided.\n"), device);
210                 return -EINVAL;
211         }
212
213         if (params->flags & CRYPT_VERITY_NO_HEADER) {
214                 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
215                 return -EINVAL;
216         }
217
218         devfd = open(device, O_RDWR | O_DIRECT);
219         if(devfd == -1) {
220                 log_err(cd, _("Cannot open device %s.\n"), device);
221                 return -EINVAL;
222         }
223
224         memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
225 #ifndef NEW_SB
226         sb.version = params->hash_type;
227         sb.data_block_bits = ffs(params->data_block_size) - 1;
228         sb.hash_block_bits = ffs(params->hash_block_size) - 1;
229         sb.salt_size = htons(params->salt_size);
230         sb.data_blocks_hi = htonl(params->data_size >> 31 >> 1);
231         sb.data_blocks_lo = htonl(params->data_size & 0xFFFFFFFF);
232         strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
233         memcpy(sb.salt, params->salt, params->salt_size);
234 #else
235         sb.version         = cpu_to_le32(1);
236         sb.hash_type       = cpu_to_le32(params->hash_type);
237         sb.data_block_size = cpu_to_le64(params->data_block_size);
238         sb.hash_block_size = cpu_to_le64(params->hash_block_size);
239         sb.salt_size       = cpu_to_le64(params->salt_size);
240         sb.data_blocks     = cpu_to_le64(params->data_size);
241         strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
242         memcpy(sb.salt, params->salt, params->salt_size);
243         memcpy(sb.uuid, uuid, sizeof(sb.uuid));
244 #endif
245         r = write_lseek_blockwise(devfd, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
246         if (r)
247                 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
248         close(devfd);
249
250         return r;
251 }
252
253 /* Calculate hash offset in hash blocks */
254 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
255 {
256         uint64_t hash_offset = params->hash_area_offset;
257
258         if (params->flags & CRYPT_VERITY_NO_HEADER)
259                 return hash_offset / params->hash_block_size;
260
261         hash_offset += sizeof(struct verity_sb);
262         hash_offset += params->hash_block_size - 1;
263
264         return hash_offset / params->hash_block_size;
265 }
266
267 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
268 {
269         uuid_t uuid;
270
271         if (!(*uuid_string = malloc(40)))
272                 return -ENOMEM;
273         uuid_generate(uuid);
274         uuid_unparse(uuid, *uuid_string);
275         return 0;
276 }
277
278 /* Activate verity device in kernel device-mapper */
279 int VERITY_activate(struct crypt_device *cd,
280                      const char *name,
281                      const char *hash_device,
282                      const char *root_hash,
283                      size_t root_hash_size,
284                      struct crypt_params_verity *verity_hdr,
285                      uint32_t activation_flags)
286 {
287         struct crypt_dm_active_device dmd;
288         uint64_t offset = 0;
289         int r;
290
291         log_dbg("Trying to activate VERITY device %s using hash %s.",
292                 name ?: "[none]", verity_hdr->hash_name);
293
294         if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
295                 r = VERITY_verify(cd, verity_hdr,
296                                   crypt_get_device_name(cd), hash_device,
297                                   root_hash, root_hash_size);
298                 if (r < 0)
299                         return r;
300         }
301
302         if (!name)
303                 return 0;
304
305         dmd.target = DM_VERITY;
306         dmd.data_device = crypt_get_device_name(cd);
307         dmd.u.verity.hash_device = hash_device;
308         dmd.u.verity.root_hash = root_hash;
309         dmd.u.verity.root_hash_size = root_hash_size;
310         dmd.u.verity.hash_offset = VERITY_hash_offset_block(verity_hdr),
311         dmd.flags = activation_flags;
312         dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
313         dmd.uuid = crypt_get_uuid(cd);
314         dmd.u.verity.vp = verity_hdr;
315
316         r = device_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
317                                     &dmd.size, &offset, &dmd.flags);
318         if (r)
319                 return r;
320
321         r = dm_create_device(name, CRYPT_VERITY, &dmd, 0);
322         if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
323                 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
324                 return -ENOTSUP;
325         }
326         if (r < 0)
327                 return r;
328
329         r = dm_status_verity_ok(name);
330         if (r < 0)
331                 return r;
332
333         if (!r)
334                 log_err(cd, _("Verity device detected corruption after activation.\n"));
335         return 0;
336 }