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