2 * cryptsetup library API test utilities
4 * Copyright (C) 2009-2020 Red Hat, Inc. All rights reserved.
5 * Copyright (C) 2009-2020 Milan Broz
7 * This program is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU General Public License
9 * as published by the Free Software Foundation; either version 2
10 * of the License, or (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <libdevmapper.h>
28 #include <sys/ioctl.h>
30 #include <sys/types.h>
33 # include <linux/keyctl.h>
34 # include <sys/syscall.h>
36 #ifdef HAVE_SYS_SYSMACROS_H
37 # include <sys/sysmacros.h>
39 #include <linux/loop.h>
42 #include "libcryptsetup.h"
44 static char last_error[256];
45 static char global_log[4096];
46 static uint32_t t_dm_crypt_flags = 0;
48 char *THE_LOOP_DEV = NULL;
53 uint64_t t_dev_offset = 0;
55 static void (*_cleanup)(void);
57 void register_cleanup(void (*cleanup)(void))
62 void check_ok(int status, int line, const char *func)
65 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, last_error);
71 void check_ok_return(int status, int line, const char *func)
74 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, last_error);
80 void check_ko(int status, int line, const char *func)
83 printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, last_error);
87 printf(" => errno %d, errmsg: %s\n", status, last_error);
90 void check_equal(int line, const char *func, int64_t x, int64_t y)
92 printf("FAIL line %d [%s]: expected equal values differs: %"
93 PRIi64 " != %" PRIi64 "\n", line, func, x, y);
98 void check_null(int line, const char *func, const void *x)
101 printf("FAIL line %d [%s]: expected NULL value: %p\n", line, func, x);
107 void check_notnull(int line, const char *func, const void *x)
110 printf("FAIL line %d [%s]: expected not NULL value: %p\n", line, func, x);
116 void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
120 printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
122 printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
126 printf("Interrupted by a signal.\n");
132 int t_device_size(const char *device, uint64_t *size)
136 devfd = open(device, O_RDONLY);
140 if (ioctl(devfd, BLKGETSIZE64, size) < 0)
151 fd = open("/proc/sys/crypto/fips_enabled", O_RDONLY);
156 if (read(fd, &buf, 1) != 1)
165 * Creates dm-linear target over the test loop device. Offset is held in
166 * global variables so that size can be tested whether it fits into remaining
167 * size of the loop device or not
169 int create_dmdevice_over_loop(const char *dm_name, const uint64_t size)
175 if (t_device_size(THE_LOOP_DEV, &r_size) < 0 || r_size <= t_dev_offset || !size)
177 if ((r_size - t_dev_offset) < size) {
178 printf("No enough space on backing loop device\n.");
181 snprintf(cmd, sizeof(cmd),
182 "dmsetup create %s --table \"0 %" PRIu64 " linear %s %" PRIu64 "\"",
183 dm_name, size, THE_LOOP_DEV, t_dev_offset);
184 if (!(r = _system(cmd, 1)))
185 t_dev_offset += size;
189 // Get key from kernel dm mapping table using dm-ioctl
190 int get_key_dm(const char *name, char *buffer, unsigned int buffer_size)
194 uint64_t start, length;
195 char *target_type, *key, *params;
199 if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
201 if (!dm_task_set_name(dmt, name))
203 if (!dm_task_run(dmt))
205 if (!dm_task_get_info(dmt, &dmi))
210 next = dm_get_next_target(dmt, next, &start, &length, &target_type, ¶ms);
211 if (!target_type || strcmp(target_type, "crypt") != 0)
214 (void)strsep(¶ms, " "); /* rcipher */
215 key = strsep(¶ms, " ");
217 if (buffer_size <= strlen(key))
220 strncpy(buffer, key, buffer_size);
224 dm_task_destroy(dmt);
229 int prepare_keyfile(const char *name, const char *passphrase, int size)
233 fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR);
235 r = write(fd, passphrase, size);
240 return r == size ? 0 : 1;
243 // Decode key from its hex representation
244 int crypt_decode_key(char *key, const char *hex, unsigned int size)
252 for (i = 0; i < size; i++) {
256 key[i] = (unsigned char)strtoul(buffer, &endp, 16);
258 if (endp != &buffer[2])
268 void global_log_callback(int level, const char *msg, void *usrptr)
273 if (level == CRYPT_LOG_DEBUG)
274 fprintf(stdout, "# %s", msg);
276 fprintf(stdout, "%s", msg);
279 if (level <= CRYPT_LOG_DEBUG)
282 strncat(global_log, msg, sizeof(global_log) - strlen(global_log));
284 if (level == CRYPT_LOG_ERROR) {
286 if (len > sizeof(last_error))
287 len = sizeof(last_error);
288 strncpy(last_error, msg, sizeof(last_error));
289 last_error[len-1] = '\0';
295 memset(global_log, 0, sizeof(global_log));
296 memset(last_error, 0, sizeof(last_error));
300 int _system(const char *command, int warn)
304 printf("Running system: %s\n", command);
305 if ((r=system(command)) < 0 && warn)
306 printf("System command failed: %s", command);
310 static int keyring_check(void)
312 #ifdef KERNEL_KEYRING
313 return syscall(__NR_request_key, "logon", "dummy", NULL, 0) == -1l && errno != ENOSYS;
319 static int t_dm_satisfies_version(unsigned target_maj, unsigned target_min, unsigned target_patch,
320 unsigned actual_maj, unsigned actual_min, unsigned actual_patch)
322 if (actual_maj > target_maj)
324 if (actual_maj == target_maj && actual_min > target_min)
326 if (actual_maj == target_maj && actual_min == target_min && actual_patch >= target_patch)
331 static void t_dm_set_crypt_compat(const char *dm_version, unsigned crypt_maj,
332 unsigned crypt_min, unsigned crypt_patch)
334 unsigned dm_maj = 0, dm_min = 0, dm_patch = 0;
336 if (sscanf(dm_version, "%u.%u.%u", &dm_maj, &dm_min, &dm_patch) != 3) {
342 if (t_dm_satisfies_version(1, 2, 0, crypt_maj, crypt_min, 0))
343 t_dm_crypt_flags |= T_DM_KEY_WIPE_SUPPORTED;
345 if (t_dm_satisfies_version(1, 10, 0, crypt_maj, crypt_min, 0))
346 t_dm_crypt_flags |= T_DM_LMK_SUPPORTED;
348 if (t_dm_satisfies_version(4, 20, 0, dm_maj, dm_min, 0))
349 t_dm_crypt_flags |= T_DM_SECURE_SUPPORTED;
351 if (t_dm_satisfies_version(1, 8, 0, crypt_maj, crypt_min, 0))
352 t_dm_crypt_flags |= T_DM_PLAIN64_SUPPORTED;
354 if (t_dm_satisfies_version(1, 11, 0, crypt_maj, crypt_min, 0))
355 t_dm_crypt_flags |= T_DM_DISCARDS_SUPPORTED;
357 if (t_dm_satisfies_version(1, 13, 0, crypt_maj, crypt_min, 0))
358 t_dm_crypt_flags |= T_DM_TCW_SUPPORTED;
360 if (t_dm_satisfies_version(1, 14, 0, crypt_maj, crypt_min, 0)) {
361 t_dm_crypt_flags |= T_DM_SAME_CPU_CRYPT_SUPPORTED;
362 t_dm_crypt_flags |= T_DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED;
365 if (t_dm_satisfies_version(1, 18, 1, crypt_maj, crypt_min, crypt_patch) && keyring_check())
366 t_dm_crypt_flags |= T_DM_KERNEL_KEYRING_SUPPORTED;
369 static void t_dm_set_verity_compat(const char *dm_version, unsigned verity_maj,
370 unsigned verity_min, unsigned verity_patch)
373 t_dm_crypt_flags |= T_DM_VERITY_SUPPORTED;
377 * ignore_corruption, restart_on corruption is available since 1.2 (kernel 4.1)
378 * ignore_zero_blocks since 1.3 (kernel 4.5)
379 * (but some dm-verity targets 1.2 don't support it)
380 * FEC is added in 1.3 as well.
382 if (t_dm_satisfies_version(1, 3, 0, verity_maj, verity_min, 0)) {
383 t_dm_crypt_flags |= T_DM_VERITY_ON_CORRUPTION_SUPPORTED;
384 t_dm_crypt_flags |= T_DM_VERITY_FEC_SUPPORTED;
388 static void t_dm_set_integrity_compat(const char *dm_version, unsigned integrity_maj,
389 unsigned integrity_min, unsigned integrity_patch)
391 if (integrity_maj > 0)
392 t_dm_crypt_flags |= T_DM_INTEGRITY_SUPPORTED;
395 int t_dm_check_versions(void)
398 struct dm_versions *target, *last_target;
402 if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
405 if (!dm_task_run(dmt))
408 if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version)))
411 target = dm_task_get_versions(dmt);
413 last_target = target;
414 if (!strcmp("crypt", target->name)) {
415 t_dm_set_crypt_compat(dm_version,
416 (unsigned)target->version[0],
417 (unsigned)target->version[1],
418 (unsigned)target->version[2]);
419 } else if (!strcmp("verity", target->name)) {
420 t_dm_set_verity_compat(dm_version,
421 (unsigned)target->version[0],
422 (unsigned)target->version[1],
423 (unsigned)target->version[2]);
424 } else if (!strcmp("integrity", target->name)) {
425 t_dm_set_integrity_compat(dm_version,
426 (unsigned)target->version[0],
427 (unsigned)target->version[1],
428 (unsigned)target->version[2]);
430 target = (struct dm_versions *)((char *) target + target->next);
431 } while (last_target != target);
436 dm_task_destroy(dmt);
441 int t_dm_crypt_keyring_support(void)
443 return t_dm_crypt_flags & T_DM_KERNEL_KEYRING_SUPPORTED;
446 int t_dm_crypt_cpu_switch_support(void)
448 return t_dm_crypt_flags & (T_DM_SAME_CPU_CRYPT_SUPPORTED |
449 T_DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED);
452 int t_dm_crypt_discard_support(void)
454 return t_dm_crypt_flags & T_DM_DISCARDS_SUPPORTED;
459 #define LOOP_DEV_MAJOR 7
461 #ifndef LO_FLAGS_AUTOCLEAR
462 #define LO_FLAGS_AUTOCLEAR 4
465 #ifndef LOOP_CTL_GET_FREE
466 #define LOOP_CTL_GET_FREE 0x4C82
469 #ifndef LOOP_SET_CAPACITY
470 #define LOOP_SET_CAPACITY 0x4C07
473 int loop_device(const char *loop)
480 if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
481 major(st.st_rdev) != LOOP_DEV_MAJOR)
487 static char *crypt_loop_get_device_old(void)
491 struct loop_info64 lo64 = {0};
493 for (i = 0; i < 256; i++) {
494 sprintf(dev, "/dev/loop%d", i);
496 loop_fd = open(dev, O_RDONLY);
500 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
511 static char *crypt_loop_get_device(void)
517 loop_fd = open("/dev/loop-control", O_RDONLY);
519 return crypt_loop_get_device_old();
521 i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
528 if (sprintf(dev, "/dev/loop%d", i) < 0)
531 if (stat(dev, &st) || !S_ISBLK(st.st_mode))
537 int loop_attach(char **loop, const char *file, int offset,
538 int autoclear, int *readonly)
540 struct loop_info64 lo64 = {0};
542 int loop_fd = -1, file_fd = -1, r = 1;
546 file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
547 if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
549 file_fd = open(file, O_RDONLY | O_EXCL);
554 while (loop_fd < 0) {
555 *loop = crypt_loop_get_device();
559 loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR);
563 if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0) {
574 lo_file_name = (char*)lo64.lo_file_name;
575 lo_file_name[LO_NAME_SIZE-1] = '\0';
576 strncpy(lo_file_name, file, LO_NAME_SIZE-1);
577 lo64.lo_offset = offset;
579 lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
581 if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
582 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
586 /* Verify that autoclear is really set */
588 memset(&lo64, 0, sizeof(lo64));
589 if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
590 !(lo64.lo_flags & LO_FLAGS_AUTOCLEAR)) {
591 (void)ioctl(loop_fd, LOOP_CLR_FD, 0);
598 if (r && loop_fd >= 0)
606 return r ? -1 : loop_fd;
609 int loop_detach(const char *loop)
611 int loop_fd = -1, r = 1;
613 loop_fd = open(loop, O_RDONLY);
617 if (!ioctl(loop_fd, LOOP_CLR_FD, 0))