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