Remove redundant flags.
[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         if (params->flags & CRYPT_VERITY_NO_HEADER) {
64                 log_err(cd, _("Verity don't use on-disk header.\n"), device);
65                 return -EINVAL;
66         }
67
68         devfd = open(device ,O_RDONLY | O_DIRECT);
69         if(devfd == -1) {
70                 log_err(cd, _("Cannot open device %s.\n"), device);
71                 return -EINVAL;
72         }
73
74         if(lseek(devfd, sb_offset, SEEK_SET) < 0 ||
75            read_blockwise(devfd, &sb, hdr_size) < hdr_size) {
76                 close(devfd);
77                 return -EIO;
78         }
79         close(devfd);
80
81         if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
82                 log_err(cd, _("Device %s is not a valid VERITY device.\n"), device);
83                 return -EINVAL;
84         }
85
86         if (sb.version > 1) {
87                 log_err(cd, _("Unsupported VERITY version %d.\n"), sb.version);
88                 return -EINVAL;
89         }
90
91         if (sb.data_block_bits < 9 || sb.data_block_bits >= 31 ||
92             sb.hash_block_bits < 9 || sb.hash_block_bits >= 31 ||
93             !memchr(sb.algorithm, 0, sizeof(sb.algorithm)) ||
94             ntohs(sb.salt_size) > VERITY_MAX_SALT_SIZE) {
95                 log_err(cd, _("VERITY header corrupted.\n"));
96                 return -EINVAL;
97         }
98
99         sb_data_blocks = ((uint64_t)ntohl(sb.data_blocks_hi) << 31 << 1) |
100                                 ntohl(sb.data_blocks_lo);
101
102         params->hash_name = strdup((const char*)sb.algorithm);
103         if (!params->hash_name)
104                 return -ENOMEM;
105         params->data_block_size = 1 << sb.data_block_bits;
106         params->hash_block_size = 1 << sb.hash_block_bits;
107         params->data_size = sb_data_blocks;
108         params->salt_size = ntohs(sb.salt_size);
109         params->salt = malloc(params->salt_size);
110         if (!params->salt)
111                 return -ENOMEM;
112         memcpy(CONST_CAST(char*)params->salt, sb.salt, params->salt_size);
113         params->hash_area_offset = sb_offset;
114         params->version = sb.version;
115
116         return 0;
117 }
118
119 /* Write verity superblock to disk */
120 int VERITY_write_sb(struct crypt_device *cd,
121                    const char *device,
122                    uint64_t sb_offset,
123                    struct crypt_params_verity *params)
124 {
125         struct verity_sb sb = {};
126         ssize_t hdr_size = sizeof(struct verity_sb);
127         int r, devfd = 0;
128
129         log_dbg("Updating VERITY header of size %u on device %s, offset %" PRIu64 ".",
130                 sizeof(struct verity_sb), device, sb_offset);
131
132         if (params->flags & CRYPT_VERITY_NO_HEADER) {
133                 log_err(cd, _("Verity don't use on-disk header.\n"), device);
134                 return -EINVAL;
135         }
136
137         devfd = open(device, O_RDWR | O_DIRECT);
138         if(devfd == -1) {
139                 log_err(cd, _("Cannot open device %s.\n"), device);
140                 return -EINVAL;
141         }
142
143         memcpy(&sb.signature, VERITY_SIGNATURE, sizeof(sb.signature));
144         sb.version = params->version;
145         sb.data_block_bits = ffs(params->data_block_size) - 1;
146         sb.hash_block_bits = ffs(params->hash_block_size) - 1;
147         sb.salt_size = htons(params->salt_size);
148         sb.data_blocks_hi = htonl(params->data_size >> 31 >> 1);
149         sb.data_blocks_lo = htonl(params->data_size & 0xFFFFFFFF);
150         strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm));
151         memcpy(sb.salt, params->salt, params->salt_size);
152
153         r = write_lseek_blockwise(devfd, (char*)&sb, hdr_size, sb_offset) < hdr_size ? -EIO : 0;
154         if (r)
155                 log_err(cd, _("Error during update of verity header on device %s.\n"), device);
156         close(devfd);
157
158         return r;
159 }
160
161 /* Calculate hash offset in hash blocks */
162 uint64_t VERITY_hash_offset_block(struct crypt_params_verity *params)
163 {
164         uint64_t hash_offset = params->hash_area_offset;
165
166         if (params->flags & CRYPT_VERITY_NO_HEADER)
167                 return hash_offset / params->hash_block_size;
168
169         hash_offset += sizeof(struct verity_sb);
170         hash_offset += params->hash_block_size - 1;
171
172         return hash_offset / params->hash_block_size;
173 }
174
175 /* Activate verity device in kernel device-mapper */
176 int VERITY_activate(struct crypt_device *cd,
177                      const char *name,
178                      const char *hash_device,
179                      const char *root_hash,
180                      size_t root_hash_size,
181                      struct crypt_params_verity *verity_hdr,
182                      uint32_t activation_flags)
183 {
184         struct crypt_dm_active_device dmd;
185         uint64_t offset = 0;
186         int r;
187
188         log_dbg("Trying to activate VERITY device %s using hash %s.",
189                 name ?: "[none]", verity_hdr->hash_name);
190
191         if (verity_hdr->flags & CRYPT_VERITY_CHECK_HASH) {
192                 r = VERITY_verify(cd, verity_hdr,
193                                   crypt_get_device_name(cd), hash_device,
194                                   root_hash, root_hash_size);
195                 if (r < 0)
196                         return r;
197         }
198
199         if (!name)
200                 return 0;
201
202         dmd.target = DM_VERITY;
203         dmd.data_device = crypt_get_device_name(cd);
204         dmd.u.verity.hash_device = hash_device;
205         dmd.u.verity.root_hash = root_hash;
206         dmd.u.verity.root_hash_size = root_hash_size;
207         dmd.u.verity.hash_offset = VERITY_hash_offset_block(verity_hdr),
208         dmd.flags = activation_flags;
209         dmd.size = verity_hdr->data_size * verity_hdr->data_block_size / 512;
210         dmd.uuid = NULL;
211         dmd.u.verity.vp = verity_hdr;
212
213         r = device_check_and_adjust(cd, dmd.data_device, DEV_EXCL,
214                                     &dmd.size, &offset, &dmd.flags);
215         if (r)
216                 return r;
217
218         r = dm_create_device(name, CRYPT_VERITY, &dmd, 0);
219         if (!r && !(dm_flags() & DM_VERITY_SUPPORTED)) {
220                 log_err(cd, _("Kernel doesn't support dm-verity mapping.\n"));
221                 return -ENOTSUP;
222         }
223         if (r < 0)
224                 return r;
225
226         r = dm_status_verity_ok(name);
227         if (r < 0)
228                 return r;
229
230         if (!r)
231                 log_err(cd, _("Verity device detected corruption after activation.\n"));
232         return 0;
233 }