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