Support init_by_name for verity.
[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 <sys/types.h>
25 #include <sys/stat.h>
26 #include <fcntl.h>
27 #include <netinet/in.h>
28
29 #include "libcryptsetup.h"
30 #include "verity.h"
31 #include "internal.h"
32
33 /* FIXME: not yet final on-disk format! Add UUID etc */
34 struct verity_sb {
35         uint8_t signature[8];
36         uint8_t version;
37         uint8_t data_block_bits;
38         uint8_t hash_block_bits;
39         uint8_t pad1[1];
40         uint16_t salt_size;
41         uint8_t pad2[2];
42         uint32_t data_blocks_hi;
43         uint32_t data_blocks_lo;
44         uint8_t algorithm[16];
45         uint8_t salt[VERITY_MAX_SALT_SIZE];
46         uint8_t pad3[88];
47 };
48
49 /* Read verity superblock from disk */
50 int VERITY_read_sb(struct crypt_device *cd,
51                    const char *device,
52                    uint64_t sb_offset,
53                    struct crypt_params_verity *params)
54 {
55         struct verity_sb sb = {};
56         ssize_t hdr_size = sizeof(struct verity_sb);
57         int devfd = 0;
58         uint64_t sb_data_blocks;
59
60         log_dbg("Reading VERITY header of size %u on device %s, offset %" PRIu64 ".",
61                 sizeof(struct verity_sb), device, sb_offset);
62
63         devfd = open(device ,O_RDONLY | O_DIRECT);
64         if(devfd == -1) {
65                 log_err(cd, _("Cannot open device %s.\n"), device);
66                 return -EINVAL;
67         }
68
69         if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
70            read_blockwise(devfd, &sb, hdr_size) < hdr_size) {
71                 close(devfd);
72                 return -EIO;
73         }
74         close(devfd);
75
76         if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
77                 log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
78                 return -EINVAL;
79         }
80
81         if (sb.version > 1) {
82                 log_err(cd, _("Unsupported VERITY version %d.\n"), sb.version);
83                 return -EINVAL;
84         }
85
86         if (sb.data_block_bits < 9 || sb.data_block_bits >= 31 ||
87             sb.hash_block_bits < 9 || sb.hash_block_bits >= 31 ||
88             !memchr(sb.algorithm, 0, sizeof(sb.algorithm)) ||
89             ntohs(sb.salt_size) > VERITY_MAX_SALT_SIZE) {
90                 log_err(cd, _("VERITY header corrupted.\n"));
91                 return -EINVAL;
92         }
93
94         sb_data_blocks = ((uint64_t)ntohl(sb.data_blocks_hi) << 31 << 1) |
95                                 ntohl(sb.data_blocks_lo);
96
97         params->hash_name = strdup((const char*)sb.algorithm);
98         if (!params->hash_name)
99                 return -ENOMEM;
100         params->data_block_size = 1 << sb.data_block_bits;
101         params->hash_block_size = 1 << sb.hash_block_bits;
102         params->data_size = sb_data_blocks;
103         params->salt_size = ntohs(sb.salt_size);
104         params->salt = malloc(params->salt_size);
105         if (!params->salt)
106                 return -ENOMEM;
107         memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
108         params->hash_area_offset = sb_offset;
109         params->version = sb.version;
110
111         return 0;
112 }
113
114 /* Write verity superblock to disk */
115 int VERITY_write_sb(struct crypt_device *cd,
116                    const char *device,
117                    uint64_t sb_offset,
118                    struct crypt_params_verity *params)
119 {
120         struct verity_sb sb = {};
121         ssize_t hdr_size = sizeof(struct verity_sb);
122         int r, devfd = 0;
123
124         log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
125                 sizeof(struct verity_sb), device, sb_offset);
126
127         devfd = open(device, O_RDWR | O_DIRECT);
128         if(devfd == -1) {
129                 log_err(cd, _("Cannot open device %s.\n"), device);
130                 return -EINVAL;
131         }
132
133         memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
134         sb.version = params->version;
135         sb.data_block_bits = ffs(params->data_block_size) - 1;
136         sb.hash_block_bits = ffs(params->hash_block_size) - 1;
137         sb.salt_size = htons(params->salt_size);
138         sb.data_blocks_hi = htonl(params->data_size >> 31 >> 1);
139         sb.data_blocks_lo = htonl(params->data_size & 0xFFFFFFFF);
140         strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
141         memcpy(sb.salt, params->salt, params->salt_size);
142
143         r = write_lseek_blockwise(devfd, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
144         if (r)
145                 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
146         close(devfd);
147
148         return r;
149 }
150
151 /* Calculate hash offset in hash blocks */
152 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
153 {
154         uint64_t hash_offset = params->hash_area_offset;
155
156         if (params->flags & CRYPT_VERITY_NO_HEADER)
157                 return hash_offset / params->hash_block_size;
158
159         hash_offset += sizeof(struct verity_sb);
160         hash_offset += params->hash_block_size - 1;
161
162         return hash_offset / params->hash_block_size;
163 }
164
165 /* Activate verity device in kernel device-mapper */
166 int VERITY_activate(struct crypt_device *cd,
167                      const char *name,
168                      const char *hash_device,
169                      const char *root_hash,
170                      size_t root_hash_size,
171                      struct crypt_params_verity *verity_hdr,
172                      uint32_t flags)
173 {
174         struct crypt_dm_active_device dmd;
175         uint64_t offset = 0;
176         int r;
177
178         log_dbg("Trying to activate VERITY device %s using hash %s.",
179                 name ?: "[none]", verity_hdr->hash_name);
180
181         if (flags & CRYPT_VERITY_CHECK_HASH) {
182                 r = VERITY_verify(cd, verity_hdr,
183                                   crypt_get_device_name(cd), hash_device,
184                                   root_hash, root_hash_size);
185                 if (r < 0)
186                         return r;
187         }
188
189         if (!name)
190                 return 0;
191
192         dmd.target = DM_VERITY;
193         dmd.data_device = crypt_get_device_name(cd);
194         dmd.u.verity.hash_device = hash_device;
195         dmd.u.verity.root_hash = root_hash;
196         dmd.u.verity.root_hash_size = root_hash_size;
197         dmd.u.verity.hash_offset = VERITY_hash_offset_block(verity_hdr),
198         dmd.flags = CRYPT_ACTIVATE_READONLY;
199         dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
200         dmd.uuid = NULL;
201         dmd.u.verity.vp = verity_hdr;
202
203         r = device_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
204                                     &dmd.size, &offset, &dmd.flags);
205         if (r)
206                 return r;
207
208         r = dm_create_device(name, CRYPT_VERITY, &dmd, 0);
209         if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
210                 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
211                 return -ENOTSUP;
212         }
213         if (r < 0)
214                 return r;
215
216         r = dm_status_verity_ok(name);
217         if (r < 0)
218                 return r;
219
220         if (!r)
221                 log_err(cd, _("Verity device detected corruption after activation.\n"));
222         return 0;
223 }