Fix regression in header backup (1.5.1).
[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 /* http://code.google.com/p/cryptsetup/wiki/DMVerity#Verity_superblock_format */
38 struct verity_sb {
39         uint8_t  signature[8];  /* "verity\0\0" */
40         uint32_t version;       /* superblock version */
41         uint32_t hash_type;     /* 0 - Chrome OS, 1 - normal */
42         uint8_t  uuid[16];      /* UUID of hash device */
43         uint8_t  algorithm[32];/* hash algorithm name */
44         uint32_t data_block_size; /* data block in bytes */
45         uint32_t hash_block_size; /* hash block in bytes */
46         uint64_t data_blocks;   /* number of data blocks */
47         uint16_t salt_size;     /* salt size */
48         uint8_t  _pad1[6];
49         uint8_t  salt[256];     /* salt */
50         uint8_t  _pad2[168];
51 } __attribute__((packed));
52
53 /* Read verity superblock from disk */
54 int VERITY_read_sb(struct crypt_device *cd,
55                    uint64_t sb_offset,
56                    char **uuid_string,
57                    struct crypt_params_verity *params)
58 {
59         const char *device = device_path(crypt_metadata_device(cd));
60         int bsize = device_block_size(crypt_metadata_device(cd));
61         struct verity_sb sb = {};
62         ssize_t hdr_size = sizeof(struct verity_sb);
63         int devfd = 0, sb_version;
64
65         log_dbg("Reading VERITY header of size %u on device %s, offset %" PRIu64 ".",
66                 sizeof(struct verity_sb), device, sb_offset);
67
68         if (params->flags & CRYPT_VERITY_NO_HEADER) {
69                 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
70                 return -EINVAL;
71         }
72
73         if (sb_offset % 512) {
74                 log_err(cd, _("Unsupported VERITY hash offset.\n"));
75                 return -EINVAL;
76         }
77
78         devfd = open(device ,O_RDONLY | O_DIRECT);
79         if(devfd == -1) {
80                 log_err(cd, _("Cannot open device %s.\n"), device);
81                 return -EINVAL;
82         }
83
84         if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
85            read_blockwise(devfd, bsize, &sb, hdr_size) < hdr_size) {
86                 close(devfd);
87                 return -EIO;
88         }
89         close(devfd);
90
91         if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
92                 log_err(cd, _("Device %s is not a valid VERITY device.\n"), 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.\n"), 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.\n"), 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.\n"));
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.\n"),
121                         params->hash_name);
122                 free(CONST_CAST(char*)params->hash_name);
123                 return -EINVAL;
124         }
125
126         params->salt_size = le16_to_cpu(sb.salt_size);
127         if (params->salt_size > sizeof(sb.salt)) {
128                 log_err(cd, _("VERITY header corrupted.\n"));
129                 free(CONST_CAST(char*)params->hash_name);
130                 return -EINVAL;
131         }
132         params->salt = malloc(params->salt_size);
133         if (!params->salt) {
134                 free(CONST_CAST(char*)params->hash_name);
135                 return -ENOMEM;
136         }
137         memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
138
139         if ((*uuid_string = malloc(40)))
140                 uuid_unparse(sb.uuid, *uuid_string);
141
142         params->hash_area_offset = sb_offset;
143         return 0;
144 }
145
146 /* Write verity superblock to disk */
147 int VERITY_write_sb(struct crypt_device *cd,
148                    uint64_t sb_offset,
149                    const char *uuid_string,
150                    struct crypt_params_verity *params)
151 {
152         const char *device = device_path(crypt_metadata_device(cd));
153         int bsize = device_block_size(crypt_metadata_device(cd));
154         struct verity_sb sb = {};
155         ssize_t hdr_size = sizeof(struct verity_sb);
156         uuid_t uuid;
157         int r, devfd = 0;
158
159         log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
160                 sizeof(struct verity_sb), device, sb_offset);
161
162         if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
163                 log_err(cd, _("Wrong VERITY UUID format provided.\n"), device);
164                 return -EINVAL;
165         }
166
167         if (params->flags & CRYPT_VERITY_NO_HEADER) {
168                 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
169                 return -EINVAL;
170         }
171
172         devfd = open(device, O_RDWR | O_DIRECT);
173         if(devfd == -1) {
174                 log_err(cd, _("Cannot open device %s.\n"), device);
175                 return -EINVAL;
176         }
177
178         memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
179         sb.version         = cpu_to_le32(1);
180         sb.hash_type       = cpu_to_le32(params->hash_type);
181         sb.data_block_size = cpu_to_le32(params->data_block_size);
182         sb.hash_block_size = cpu_to_le32(params->hash_block_size);
183         sb.salt_size       = cpu_to_le16(params->salt_size);
184         sb.data_blocks     = cpu_to_le64(params->data_size);
185         strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
186         memcpy(sb.salt, params->salt, params->salt_size);
187         memcpy(sb.uuid, uuid, sizeof(sb.uuid));
188
189         r = write_lseek_blockwise(devfd, bsize, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
190         if (r)
191                 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
192         close(devfd);
193
194         return r;
195 }
196
197 /* Calculate hash offset in hash blocks */
198 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
199 {
200         uint64_t hash_offset = params->hash_area_offset;
201
202         if (params->flags & CRYPT_VERITY_NO_HEADER)
203                 return hash_offset / params->hash_block_size;
204
205         hash_offset += sizeof(struct verity_sb);
206         hash_offset += params->hash_block_size - 1;
207
208         return hash_offset / params->hash_block_size;
209 }
210
211 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
212 {
213         uuid_t uuid;
214
215         if (!(*uuid_string = malloc(40)))
216                 return -ENOMEM;
217         uuid_generate(uuid);
218         uuid_unparse(uuid, *uuid_string);
219         return 0;
220 }
221
222 /* Activate verity device in kernel device-mapper */
223 int VERITY_activate(struct crypt_device *cd,
224                      const char *name,
225                      const char *root_hash,
226                      size_t root_hash_size,
227                      struct crypt_params_verity *verity_hdr,
228                      uint32_t activation_flags)
229 {
230         struct crypt_dm_active_device dmd;
231         int r;
232
233         log_dbg("Trying to activate VERITY device %s using hash %s.",
234                 name ?: "[none]", verity_hdr->hash_name);
235
236         if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
237                 log_dbg("Verification of data in userspace required.");
238                 r = VERITY_verify(cd, verity_hdr,
239                                   root_hash, root_hash_size);
240                 if (r < 0)
241                         return r;
242         }
243
244         if (!name)
245                 return 0;
246
247         dmd.target = DM_VERITY;
248         dmd.data_device = crypt_data_device(cd);
249         dmd.u.verity.hash_device = crypt_metadata_device(cd);
250         dmd.u.verity.root_hash = root_hash;
251         dmd.u.verity.root_hash_size = root_hash_size;
252         dmd.u.verity.hash_offset = VERITY_hash_offset_block(verity_hdr),
253         dmd.flags = activation_flags;
254         dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
255         dmd.uuid = crypt_get_uuid(cd);
256         dmd.u.verity.vp = verity_hdr;
257
258         r = device_block_adjust(cd, dmd.u.verity.hash_device, DEV_OK,
259                                 0, NULL, NULL);
260         if (r)
261                 return r;
262
263         r = device_block_adjust(cd, dmd.data_device, DEV_EXCL,
264                                 0, &dmd.size, &dmd.flags);
265         if (r)
266                 return r;
267
268         r = dm_create_device(cd, name, CRYPT_VERITY, &dmd, 0);
269         if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
270                 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
271                 return -ENOTSUP;
272         }
273         if (r < 0)
274                 return r;
275
276         r = dm_status_verity_ok(cd, name);
277         if (r < 0)
278                 return r;
279
280         if (!r)
281                 log_err(cd, _("Verity device detected corruption after activation.\n"));
282         return 0;
283 }