Add some verity api test.
[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                    const char *device,
56                    uint64_t sb_offset,
57                    char **uuid_string,
58                    struct crypt_params_verity *params)
59 {
60         struct verity_sb sb = {};
61         ssize_t hdr_size = sizeof(struct verity_sb);
62         int devfd = 0, sb_version;
63
64         log_dbg("Reading VERITY header of size %u on device %s, offset %" PRIu64 ".",
65                 sizeof(struct verity_sb), device, sb_offset);
66
67         if (params->flags & CRYPT_VERITY_NO_HEADER) {
68                 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
69                 return -EINVAL;
70         }
71
72         if (sb_offset % 512) {
73                 log_err(cd, _("Unsupported VERITY hash offset.\n"));
74                 return -EINVAL;
75         }
76
77         devfd = open(device ,O_RDONLY | O_DIRECT);
78         if(devfd == -1) {
79                 log_err(cd, _("Cannot open device %s.\n"), device);
80                 return -EINVAL;
81         }
82
83         if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
84            read_blockwise(devfd, &sb, hdr_size) < hdr_size) {
85                 close(devfd);
86                 return -EIO;
87         }
88         close(devfd);
89
90         if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
91                 log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
92                 return -EINVAL;
93         }
94
95         sb_version = le32_to_cpu(sb.version);
96         if (sb_version != 1) {
97                 log_err(cd, _("Unsupported VERITY version %d.\n"), sb_version);
98                 return -EINVAL;
99         }
100         params->hash_type = le32_to_cpu(sb.hash_type);
101         if (params->hash_type > VERITY_MAX_HASH_TYPE) {
102                 log_err(cd, _("Unsupported VERITY hash type %d.\n"), params->hash_type);
103                 return -EINVAL;
104         }
105
106         params->data_block_size = le32_to_cpu(sb.data_block_size);
107         params->hash_block_size = le32_to_cpu(sb.hash_block_size);
108         if (VERITY_BLOCK_SIZE_OK(params->data_block_size) ||
109             VERITY_BLOCK_SIZE_OK(params->hash_block_size)) {
110                 log_err(cd, _("Unsupported VERITY block size.\n"));
111                 return -EINVAL;
112         }
113         params->data_size = le64_to_cpu(sb.data_blocks);
114
115         params->hash_name = strndup((const char*)sb.algorithm, sizeof(sb.algorithm));
116         if (!params->hash_name)
117                 return -ENOMEM;
118         if (crypt_hash_size(params->hash_name) <= 0) {
119                 log_err(cd, _("Hash algorithm %s not supported.\n"),
120                         params->hash_name);
121                 free(CONST_CAST(char*)params->hash_name);
122                 return -EINVAL;
123         }
124
125         params->salt_size = le16_to_cpu(sb.salt_size);
126         if (params->salt_size > sizeof(sb.salt)) {
127                 log_err(cd, _("VERITY header corrupted.\n"));
128                 free(CONST_CAST(char*)params->hash_name);
129                 return -EINVAL;
130         }
131         params->salt = malloc(params->salt_size);
132         if (!params->salt) {
133                 free(CONST_CAST(char*)params->hash_name);
134                 return -ENOMEM;
135         }
136         memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
137
138         if ((*uuid_string = malloc(40)))
139                 uuid_unparse(sb.uuid, *uuid_string);
140
141         params->hash_area_offset = sb_offset;
142         return 0;
143 }
144
145 /* Write verity superblock to disk */
146 int VERITY_write_sb(struct crypt_device *cd,
147                    const char *device,
148                    uint64_t sb_offset,
149                    const char *uuid_string,
150                    struct crypt_params_verity *params)
151 {
152         struct verity_sb sb = {};
153         ssize_t hdr_size = sizeof(struct verity_sb);
154         uuid_t uuid;
155         int r, devfd = 0;
156
157         log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
158                 sizeof(struct verity_sb), device, sb_offset);
159
160         if (!uuid_string || uuid_parse(uuid_string, uuid) == -1) {
161                 log_err(cd, _("Wrong VERITY UUID format provided.\n"), device);
162                 return -EINVAL;
163         }
164
165         if (params->flags & CRYPT_VERITY_NO_HEADER) {
166                 log_err(cd, _("Verity device doesn't use on-disk header.\n"), device);
167                 return -EINVAL;
168         }
169
170         devfd = open(device, O_RDWR | O_DIRECT);
171         if(devfd == -1) {
172                 log_err(cd, _("Cannot open device %s.\n"), device);
173                 return -EINVAL;
174         }
175
176         memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
177         sb.version         = cpu_to_le32(1);
178         sb.hash_type       = cpu_to_le32(params->hash_type);
179         sb.data_block_size = cpu_to_le32(params->data_block_size);
180         sb.hash_block_size = cpu_to_le32(params->hash_block_size);
181         sb.salt_size       = cpu_to_le16(params->salt_size);
182         sb.data_blocks     = cpu_to_le64(params->data_size);
183         strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
184         memcpy(sb.salt, params->salt, params->salt_size);
185         memcpy(sb.uuid, uuid, sizeof(sb.uuid));
186
187         r = write_lseek_blockwise(devfd, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
188         if (r)
189                 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
190         close(devfd);
191
192         return r;
193 }
194
195 /* Calculate hash offset in hash blocks */
196 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
197 {
198         uint64_t hash_offset = params->hash_area_offset;
199
200         if (params->flags & CRYPT_VERITY_NO_HEADER)
201                 return hash_offset / params->hash_block_size;
202
203         hash_offset += sizeof(struct verity_sb);
204         hash_offset += params->hash_block_size - 1;
205
206         return hash_offset / params->hash_block_size;
207 }
208
209 int VERITY_UUID_generate(struct crypt_device *cd, char **uuid_string)
210 {
211         uuid_t uuid;
212
213         if (!(*uuid_string = malloc(40)))
214                 return -ENOMEM;
215         uuid_generate(uuid);
216         uuid_unparse(uuid, *uuid_string);
217         return 0;
218 }
219
220 /* Activate verity device in kernel device-mapper */
221 int VERITY_activate(struct crypt_device *cd,
222                      const char *name,
223                      const char *hash_device,
224                      const char *root_hash,
225                      size_t root_hash_size,
226                      struct crypt_params_verity *verity_hdr,
227                      uint32_t activation_flags)
228 {
229         struct crypt_dm_active_device dmd;
230         uint64_t offset = 0;
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                                   crypt_get_device_name(cd), hash_device,
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_get_device_name(cd);
250         dmd.u.verity.hash_device = hash_device;
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_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
260                                     &dmd.size, &offset, &dmd.flags);
261         if (r)
262                 return r;
263
264         r = dm_create_device(name, CRYPT_VERITY, &dmd, 0);
265         if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
266                 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
267                 return -ENOTSUP;
268         }
269         if (r < 0)
270                 return r;
271
272         r = dm_status_verity_ok(name);
273         if (r < 0)
274                 return r;
275
276         if (!r)
277                 log_err(cd, _("Verity device detected corruption after activation.\n"));
278         return 0;
279 }