Verity salt size is really uint16 in sb and uint32 internally.
[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         uint16_t salt_size;     /* salt size */
65         uint8_t  _pad1[6];
66         uint8_t  salt[256];     /* salt */
67         uint8_t  _pad2[160];
68 } __attribute__((packed));
69 #endif
70
71 /* Read verity superblock from disk */
72 int VERITY_read_sb(struct crypt_device *cd,
73                    const char *device,
74                    uint64_t sb_offset,
75                    char **uuid_string,
76                    struct crypt_params_verity *params)
77 {
78         struct verity_sb sb = {};
79         ssize_t hdr_size = sizeof(struct verity_sb);
80         int devfd = 0, sb_version;
81 #ifndef NEW_SB
82         uint64_t sb_data_blocks;
83 #endif
84         log_dbg("Reading VERITY header of size %u on device %s, offset %" PRIu64 ".",
85                 sizeof(struct verity_sb), device, sb_offset);
86
87         if (params->flags & CRYPT_VERITY_NO_HEADER) {
88                 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
89                 return -EINVAL;
90         }
91
92         if (sb_offset % 512) {
93                 log_err(cd, _("Unsupported VERITY hash offset.\n"));
94                 return -EINVAL;
95         }
96
97         devfd = open(device ,O_RDONLY | O_DIRECT);
98         if(devfd == -1) {
99                 log_err(cd, _("Cannot open device %s.\n"), device);
100                 return -EINVAL;
101         }
102
103         if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
104            read_blockwise(devfd, &sb, hdr_size) < hdr_size) {
105                 close(devfd);
106                 return -EIO;
107         }
108         close(devfd);
109
110         if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
111                 log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
112                 return -EINVAL;
113         }
114 #ifndef NEW_SB
115         if (sb.version > 1) {
116                 log_err(cd, _("Unsupported VERITY version %d.\n"), sb.version);
117                 return -EINVAL;
118         }
119
120         if (sb.data_block_bits < 9 || sb.data_block_bits >= 31 ||
121             sb.hash_block_bits < 9 || sb.hash_block_bits >= 31 ||
122             !memchr(sb.algorithm, 0, sizeof(sb.algorithm)) ||
123             ntohs(sb.salt_size) > 256) {
124                 log_err(cd, _("VERITY header corrupted.\n"));
125                 return -EINVAL;
126         }
127
128         sb_data_blocks = ((uint64_t)ntohl(sb.data_blocks_hi) << 31 << 1) |
129                                 ntohl(sb.data_blocks_lo);
130
131         params->hash_name = strdup((const char*)sb.algorithm);
132         if (!params->hash_name)
133                 return -ENOMEM;
134         params->data_block_size = 1 << sb.data_block_bits;
135         params->hash_block_size = 1 << sb.hash_block_bits;
136         params->data_size = sb_data_blocks;
137         params->salt_size = ntohs(sb.salt_size);
138         params->salt = malloc(params->salt_size);
139         if (!params->salt)
140                 return -ENOMEM;
141         memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
142         params->hash_type = sb.version;
143 #else
144         sb_version = le32_to_cpu(sb.version);
145         if (sb_version != 1) {
146                 log_err(cd, _("Unsupported VERITY version %d.\n"), sb_version);
147                 return -EINVAL;
148         }
149         params->hash_type = le32_to_cpu(sb.hash_type);
150         if (params->hash_type > VERITY_MAX_HASH_TYPE) {
151                 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
152                 return -EINVAL;
153         }
154
155         params->data_block_size = le64_to_cpu(sb.data_block_size);
156         params->hash_block_size = le64_to_cpu(sb.hash_block_size);
157         if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
158             VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
159                 log_err(cd, _("Unsupported VERITY block size.\n"));
160                 return -EINVAL;
161         }
162         params->data_size = le64_to_cpu(sb.data_blocks);
163
164         params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
165         if (!params->hash_name)
166                 return -ENOMEM;
167         if (crypt_hash_size(params->hash_name) <= 0) {
168                 log_err(cd, _("Hash algorithm %s not supported.\n"),
169                         params->hash_name);
170                 free(CONST_CAST(char*)params->hash_name);
171                 return -EINVAL;
172         }
173
174         params->salt_size = le16_to_cpu(sb.salt_size);
175         if (params->salt_size > sizeof(sb.salt)) {
176                 log_err(cd, _("VERITY header corrupted.\n"));
177                 free(CONST_CAST(char*)params->hash_name);
178                 return -EINVAL;
179         }
180         params->salt = malloc(params->salt_size);
181         if (!params->salt) {
182                 free(CONST_CAST(char*)params->hash_name);
183                 return -ENOMEM;
184         }
185         memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
186
187         if ((*uuid_string = malloc(40)))
188                 uuid_unparse(sb.uuid, *uuid_string);
189 #endif
190         params->hash_area_offset = sb_offset;
191         return 0;
192 }
193
194 /* Write verity superblock to disk */
195 int VERITY_write_sb(struct crypt_device *cd,
196                    const char *device,
197                    uint64_t sb_offset,
198                    const char *uuid_string,
199                    struct crypt_params_verity *params)
200 {
201         struct verity_sb sb = {};
202         ssize_t hdr_size = sizeof(struct verity_sb);
203         uuid_t uuid;
204         int r, devfd = 0;
205
206         log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
207                 sizeof(struct verity_sb), device, sb_offset);
208
209         if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
210                 log_err(cd, _("Wrong VERITY UUID format provided.\n"), device);
211                 return -EINVAL;
212         }
213
214         if (params->flags & CRYPT_VERITY_NO_HEADER) {
215                 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
216                 return -EINVAL;
217         }
218
219         devfd = open(device, O_RDWR | O_DIRECT);
220         if(devfd == -1) {
221                 log_err(cd, _("Cannot open device %s.\n"), device);
222                 return -EINVAL;
223         }
224
225         memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
226 #ifndef NEW_SB
227         sb.version = params->hash_type;
228         sb.data_block_bits = ffs(params->data_block_size) - 1;
229         sb.hash_block_bits = ffs(params->hash_block_size) - 1;
230         sb.salt_size = htons(params->salt_size);
231         sb.data_blocks_hi = htonl(params->data_size >> 31 >> 1);
232         sb.data_blocks_lo = htonl(params->data_size & 0xFFFFFFFF);
233         strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
234         memcpy(sb.salt, params->salt, params->salt_size);
235 #else
236         sb.version         = cpu_to_le32(1);
237         sb.hash_type       = cpu_to_le32(params->hash_type);
238         sb.data_block_size = cpu_to_le64(params->data_block_size);
239         sb.hash_block_size = cpu_to_le64(params->hash_block_size);
240         sb.salt_size       = cpu_to_le16(params->salt_size);
241         sb.data_blocks     = cpu_to_le64(params->data_size);
242         strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
243         memcpy(sb.salt, params->salt, params->salt_size);
244         memcpy(sb.uuid, uuid, sizeof(sb.uuid));
245 #endif
246         r = write_lseek_blockwise(devfd, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
247         if (r)
248                 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
249         close(devfd);
250
251         return r;
252 }
253
254 /* Calculate hash offset in hash blocks */
255 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
256 {
257         uint64_t hash_offset = params->hash_area_offset;
258
259         if (params->flags & CRYPT_VERITY_NO_HEADER)
260                 return hash_offset / params->hash_block_size;
261
262         hash_offset += sizeof(struct verity_sb);
263         hash_offset += params->hash_block_size - 1;
264
265         return hash_offset / params->hash_block_size;
266 }
267
268 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
269 {
270         uuid_t uuid;
271
272         if (!(*uuid_string = malloc(40)))
273                 return -ENOMEM;
274         uuid_generate(uuid);
275         uuid_unparse(uuid, *uuid_string);
276         return 0;
277 }
278
279 /* Activate verity device in kernel device-mapper */
280 int VERITY_activate(struct crypt_device *cd,
281                      const char *name,
282                      const char *hash_device,
283                      const char *root_hash,
284                      size_t root_hash_size,
285                      struct crypt_params_verity *verity_hdr,
286                      uint32_t activation_flags)
287 {
288         struct crypt_dm_active_device dmd;
289         uint64_t offset = 0;
290         int r;
291
292         log_dbg("Trying to activate VERITY device %s using hash %s.",
293                 name ?: "[none]", verity_hdr->hash_name);
294
295         if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
296                 r = VERITY_verify(cd, verity_hdr,
297                                   crypt_get_device_name(cd), hash_device,
298                                   root_hash, root_hash_size);
299                 if (r < 0)
300                         return r;
301         }
302
303         if (!name)
304                 return 0;
305
306         dmd.target = DM_VERITY;
307         dmd.data_device = crypt_get_device_name(cd);
308         dmd.u.verity.hash_device = hash_device;
309         dmd.u.verity.root_hash = root_hash;
310         dmd.u.verity.root_hash_size = root_hash_size;
311         dmd.u.verity.hash_offset = VERITY_hash_offset_block(verity_hdr),
312         dmd.flags = activation_flags;
313         dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
314         dmd.uuid = crypt_get_uuid(cd);
315         dmd.u.verity.vp = verity_hdr;
316
317         r = device_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
318                                     &dmd.size, &offset, &dmd.flags);
319         if (r)
320                 return r;
321
322         r = dm_create_device(name, CRYPT_VERITY, &dmd, 0);
323         if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
324                 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
325                 return -ENOTSUP;
326         }
327         if (r < 0)
328                 return r;
329
330         r = dm_status_verity_ok(name);
331         if (r < 0)
332                 return r;
333
334         if (!r)
335                 log_err(cd, _("Verity device detected corruption after activation.\n"));
336         return 0;
337 }