Debian Codegraph @

dracut/modules.d/50kiwi-verity/kiwi-parse-verity.c

raw
1 // vi: set sw=4 ts=4 :
2 // SPDX-License-Identifier: GPL-2.0-or-later
3 // Copyright (C) 2023 Canonical Ltd
4 // Author: Isaac True <[email protected]>
5 //
6 // This file is part of kiwi.
7 //
8 // kiwi is free software: you can redistribute it and/or modify
9 // it under the terms of the GNU General Public License as published by
10 // the Free Software Foundation, either version 3 of the License, or
11 // (at your option) any later version.
12 //
13 // kiwi is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 // GNU General Public License for more details.
17 //
18 // You should have received a copy of the GNU General Public License
19 // along with kiwi. If not, see <http://www.gnu.org/licenses/>
20 //
21
22 #define _POSIX_C_SOURCE (200809L)
23 #define _LARGEFILE64_SOURCE
24
25 #include <inttypes.h>
26 #include <sys/types.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <unistd.h>
31 #include <errno.h>
32 #include <stdint.h>
33 #include <stdbool.h>
34 #include <assert.h>
35 #include <fcntl.h>
36 #include <ctype.h>
37
38 enum output_type {
39 OUTPUT_VERSION,
40 OUTPUT_FSTYPE,
41 OUTPUT_ACCESS,
42 OUTPUT_IDENTIFIER,
43 OUTPUT_HASH_TYPE,
44 OUTPUT_DATA_BLOCK_SIZE,
45 OUTPUT_HASH_BLOCK_SIZE,
46 OUTPUT_DATA_BLOCKS,
47 OUTPUT_HASH_START_BLOCK,
48 OUTPUT_ALGORITHM,
49 OUTPUT_ROOT_HASH,
50 OUTPUT_SALT,
51 OUTPUT_UNKNOWN,
52 };
53
54 struct args {
55 char *path;
56 uint64_t block_size;
57 uint64_t end_offset;
58 bool verbose;
59 enum output_type output;
60 };
61
62 struct kiwi_verity_header {
63 /* currently set to 1 */
64 char version;
65 /* name of filesystem attribute */
66 char *fstype;
67 /* either ro or rw depending on the filesystem capabilities */
68 char *access;
69 /* fixed identifier value */
70 char *identifier;
71 };
72
73 struct kiwi_verity_metadata {
74 /* hash type name as returned by veritysetup */
75 char hash_type;
76 /* data blocksize as returned by veritysetup */
77 char *data_blksize;
78 /* hash blocksize as returned by veritysetup */
79 char *hash_blksize;
80 /* number of data blocks as returned by veritysetup */
81 char *data_blocks;
82 /* hash start block as required by the kernel to construct the device map */
83 char *hash_start_block;
84 /* hash algorithm as returned by veritysetup */
85 char *algorithm;
86 /* root hash as returned by veritysetup */
87 char *root_hash;
88 /* salt hash as returned by veritysetup */
89 char *salt;
90 };
91
92 static const char *const output_table[] = {
93 [OUTPUT_VERSION] = "version",
94 [OUTPUT_FSTYPE] = "fs-type",
95 [OUTPUT_ACCESS] = "access",
96 [OUTPUT_IDENTIFIER] = "identifier",
97 [OUTPUT_HASH_TYPE] = "hash-type",
98 [OUTPUT_DATA_BLOCK_SIZE] = "data-blocksize",
99 [OUTPUT_HASH_BLOCK_SIZE] = "hash-blocksize",
100 [OUTPUT_DATA_BLOCKS] = "data-blocks",
101 [OUTPUT_HASH_START_BLOCK] = "hash-start-block",
102 [OUTPUT_ALGORITHM] = "algorithm",
103 [OUTPUT_ROOT_HASH] = "root-hash",
104 [OUTPUT_SALT] = "salt",
105 };
106
107 static void print_usage(char *const argv[]);
108 static enum output_type parse_output_type(const char *const str);
109 static int parse_args(struct args *const args, const int argc,
110 char *const argv[]);
111 static int kiwi_read_verity_info(const struct args *const args,
112 struct kiwi_verity_header *const header,
113 struct kiwi_verity_metadata *const metadata);
114 static bool is_data_separator(char c);
115 static int read_string(int fd, char **const dest);
116 static int read_char(int fd, char *const dest);
117 static void cleanup(struct kiwi_verity_header *const header,
118 struct kiwi_verity_metadata *const metadata);
119
120 static enum output_type parse_output_type(const char *const str)
121 {
122 char buffer[32] = {'\0'};
123
124 assert(str);
125
126 /*
127 * Copy the string to a temporary buffer so we can ensure it's
128 * NULL-terminated.
129 */
130 memcpy(buffer, str, strnlen(str, sizeof(buffer) - 1));
131 buffer[sizeof(buffer) - 1] = '\0';
132
133 for (size_t i = 0; i < (sizeof(output_table) / sizeof(output_table[0]));
134 i++) {
135 if (!memcmp(buffer, output_table[i],
136 strnlen(output_table[i], sizeof(buffer) - 1) + 1)) {
137 return (enum output_type)i;
138 }
139 }
140
141 return OUTPUT_UNKNOWN;
142 }
143
144 static int parse_args(struct args *const args, const int argc,
145 char *const argv[])
146 {
147 int c;
148
149 assert(args);
150 assert(argc > 0);
151 assert(argv);
152
153 while ((c = getopt(argc, argv, "p:b:e:vo:")) != -1)
154 switch (c) {
155 case 'p':
156 args->path = optarg;
157 break;
158 case 'b':
159 args->block_size = strtoul(optarg, NULL, 10);
160 break;
161 case 'e':
162 args->end_offset = strtoul(optarg, NULL, 10);
163 break;
164 case 'v':
165 args->verbose = true;
166 break;
167 case 'o':
168 args->output = parse_output_type(optarg);
169 break;
170 case '?':
171 return 1;
172 default:
173 print_usage(argv);
174 return 1;
175 }
176
177 return 0;
178 }
179
180 static void print_usage(char *const argv[])
181 {
182 assert(argv);
183
184 puts("kiwi-parse-verity");
185 puts("Tool for extracting verity information from a Kiwi image");
186 puts("Written by Isaac True <[email protected]>");
187 puts("");
188 printf("Usage: %s -p <path> [options]\n", argv[0]);
189 puts(" -p <path>: path to the device where the verity data is stored");
190 puts(" -o <output type>: data to extract (default root-hash). Can be one of the following:");
191
192 for (size_t i = 0; i < (sizeof(output_table) / sizeof(output_table[0]));
193 i++) {
194 printf(" %s\n", output_table[i]);
195 }
196
197 puts(" -e <offset>: offset in blocks from the end of the target device where the data is stored (default 1)");
198 puts(" -b <block size>: hash block size (default 4096)");
199 puts(" -v: enable verbose mode");
200 }
201
202 static int kiwi_read_verity_info(const struct args *const args,
203 struct kiwi_verity_header *const header,
204 struct kiwi_verity_metadata *const metadata)
205 {
206 int fd;
207 int ret;
208 off64_t offset;
209
210 assert(args);
211 assert(header);
212 assert(metadata);
213
214 fd = open(args->path, O_RDONLY);
215 if (fd < 0) {
216 printf("Failed to open %s: %s (%d)\n", args->path,
217 strerror(errno), errno);
218 return -errno;
219 }
220
221 offset = (off64_t)args->block_size * (off64_t)args->end_offset;
222 if (lseek64(fd, -offset, SEEK_END) < 0) {
223 printf("Failed to go to offset %"PRIu64" in %s: %s (%d)\n", offset,
224 args->path, strerror(errno), errno);
225 return -errno;
226 }
227
228 /*
229 * Make sure that we are parsing a known version. This also serves as
230 * a basic preliminary sanity check.
231 */
232 ret = read_char(fd, &header->version);
233 if (ret < 0) {
234 return ret;
235 }
236
237 if (header->version != '1') {
238 printf("Invalid Kiwi verity metadata version: %c\n",
239 header->version);
240 return -EINVAL;
241 }
242
243 ret = read_string(fd, &header->fstype);
244 if (ret < 0) {
245 return ret;
246 }
247 ret = read_string(fd, &header->access);
248 if (ret < 0) {
249 return ret;
250 }
251 ret = read_string(fd, &header->identifier);
252 if (ret < 0) {
253 return ret;
254 }
255
256 ret = read_char(fd, &metadata->hash_type);
257 if (ret < 0) {
258 return ret;
259 }
260 ret = read_string(fd, &metadata->data_blksize);
261 if (ret < 0) {
262 return ret;
263 }
264 ret = read_string(fd, &metadata->hash_blksize);
265 if (ret < 0) {
266 return ret;
267 }
268 ret = read_string(fd, &metadata->data_blocks);
269 if (ret < 0) {
270 return ret;
271 }
272 ret = read_string(fd, &metadata->hash_start_block);
273 if (ret < 0) {
274 return ret;
275 }
276 ret = read_string(fd, &metadata->algorithm);
277 if (ret < 0) {
278 return ret;
279 }
280 ret = read_string(fd, &metadata->root_hash);
281 if (ret < 0) {
282 return ret;
283 }
284 ret = read_string(fd, &metadata->salt);
285 if (ret < 0) {
286 return ret;
287 }
288
289 close(fd);
290
291 return 0;
292 }
293
294 static bool is_data_separator(char c)
295 {
296 return (c == ' ' || c == '\xff');
297 }
298
299 static int read_string(int fd, char **const dest)
300 {
301 char buffer[256] = {'\0'};
302 size_t i = 0;
303
304 assert(fd >= 0);
305 assert(dest);
306
307 do {
308 ssize_t ret = read(fd, &buffer[i], 1);
309 if (ret < 0) {
310 return -errno;
311 } else if (ret < 1) {
312 return -ENOSPC;
313 }
314
315 if (is_data_separator(buffer[i])) {
316 /* End the string here */
317 buffer[i] = '\0';
318 break;
319 }
320
321 } while (i++ < (sizeof(buffer) - 1));
322
323 *dest = strdup(buffer);
324 if (!*dest) {
325 return -errno;
326 }
327
328 return 0;
329 }
330
331 static int read_char(int fd, char *const dest)
332 {
333 char *buffer = NULL;
334 int ret;
335
336 assert(fd >= 0);
337 assert(dest);
338
339 ret = read_string(fd, &buffer);
340 if (ret < 0) {
341 return 0;
342 }
343
344 assert(buffer);
345
346 *dest = buffer[0];
347 free(buffer);
348
349 return 0;
350 }
351
352 static void cleanup(struct kiwi_verity_header *const header,
353 struct kiwi_verity_metadata *const metadata)
354 {
355 assert(header);
356 assert(metadata);
357
358 if (header->access != NULL) {
359 free(header->access);
360 }
361 if (header->fstype != NULL) {
362 free(header->fstype);
363 }
364 if (header->identifier != NULL) {
365 free(header->identifier);
366 }
367 if (metadata->algorithm != NULL) {
368 free(metadata->algorithm);
369 }
370 if (metadata->data_blksize != NULL) {
371 free(metadata->data_blksize);
372 }
373 if (metadata->data_blocks != NULL) {
374 free(metadata->data_blocks);
375 }
376 if (metadata->hash_blksize != NULL) {
377 free(metadata->hash_blksize);
378 }
379 if (metadata->hash_start_block != NULL) {
380 free(metadata->hash_start_block);
381 }
382 if (metadata->root_hash != NULL) {
383 free(metadata->root_hash);
384 }
385 if (metadata->salt != NULL) {
386 free(metadata->salt);
387 }
388 }
389
390 int main(int argc, char *argv[])
391 {
392 int ret = 0;
393 struct args args = {
394 .path = NULL,
395 .block_size = 4096,
396 .end_offset = 1,
397 .verbose = false,
398 .output = OUTPUT_ROOT_HASH,
399 };
400 struct kiwi_verity_header header = {0};
401 struct kiwi_verity_metadata metadata = {0};
402
403 ret = parse_args(&args, argc, argv);
404 if (ret) {
405 return ret;
406 }
407
408 if (args.path == NULL || args.output == OUTPUT_UNKNOWN ||
409 args.block_size == 0 || args.end_offset == 0) {
410 print_usage(argv);
411 return -1;
412 }
413
414 if (args.verbose) {
415 printf("Reading from %s, block size %"PRIu64", block offset from end %"PRIu64"\n",
416 args.path, args.block_size, args.end_offset);
417 }
418
419 ret = kiwi_read_verity_info(&args, &header, &metadata);
420 if (ret) {
421 printf("Failed to read metadata: %s (%d)\n", strerror(-ret),
422 ret);
423 goto exit;
424 }
425
426 if (args.verbose) {
427 printf("\nHeader:\n");
428 printf(" Version: %c\n", header.version);
429 printf(" FS Type: %s\n", header.fstype);
430 printf(" Access: %s\n", header.access);
431 printf(" Identifier: %s\n", header.identifier);
432 printf("\nMetadata:\n");
433 printf(" Hash type: %c\n", metadata.hash_type);
434 printf(" Data blksize: %s\n", metadata.data_blksize);
435 printf(" Hash blksize: %s\n", metadata.hash_blksize);
436 printf(" Data blocks: %s\n", metadata.data_blocks);
437 printf(" Hash start block: %s\n", metadata.hash_start_block);
438 printf(" Algorithm: %s\n", metadata.algorithm);
439 printf(" Root hash: %s\n", metadata.root_hash);
440 printf(" Salt: %s\n", metadata.salt);
441 }
442
443 switch (args.output) {
444 case OUTPUT_VERSION:
445 printf("%c\n", header.version);
446 break;
447 case OUTPUT_FSTYPE:
448 printf("%s\n", header.fstype);
449 break;
450 case OUTPUT_ACCESS:
451 printf("%s\n", header.access);
452 break;
453 case OUTPUT_IDENTIFIER:
454 printf("%s\n", header.identifier);
455 break;
456 case OUTPUT_HASH_TYPE:
457 printf("%c\n", metadata.hash_type);
458 break;
459 case OUTPUT_DATA_BLOCK_SIZE:
460 printf("%s\n", metadata.data_blksize);
461 break;
462 case OUTPUT_HASH_BLOCK_SIZE:
463 printf("%s\n", metadata.hash_blksize);
464 break;
465 case OUTPUT_DATA_BLOCKS:
466 printf("%s\n", metadata.data_blocks);
467 break;
468 case OUTPUT_HASH_START_BLOCK:
469 printf("%s\n", metadata.hash_start_block);
470 break;
471 case OUTPUT_ALGORITHM:
472 printf("%s\n", metadata.algorithm);
473 break;
474 case OUTPUT_ROOT_HASH:
475 printf("%s\n", metadata.root_hash);
476 break;
477 case OUTPUT_SALT:
478 printf("%s\n", metadata.salt);
479 break;
480 default:
481 return -1;
482 }
483
484 exit:
485 cleanup(&header, &metadata);
486 return ret;
487 }