summaryrefslogtreecommitdiffstats
path: root/tests/test_utils.c
blob: d06e7381342c88a5ea0338dd56913fa9d209685b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
/*
 * cryptsetup library API test utilities
 *
 * Copyright (C) 2009-2024 Red Hat, Inc. All rights reserved.
 * Copyright (C) 2009-2024 Milan Broz
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <assert.h>
#include <errno.h>
#include <fcntl.h>
#include <inttypes.h>
#include <stdlib.h>
#include <libdevmapper.h>
#include <linux/fs.h>
#include <sys/ioctl.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#ifdef KERNEL_KEYRING
# include <linux/keyctl.h>
# include <sys/syscall.h>
#endif
#ifdef HAVE_SYS_SYSMACROS_H
# include <sys/sysmacros.h>
#endif
#include <linux/loop.h>

#include "api_test.h"
#include "libcryptsetup.h"

#ifndef LOOP_CONFIGURE
#define LOOP_CONFIGURE 0x4C0A
struct loop_config {
  __u32 fd;
  __u32 block_size;
  struct loop_info64 info;
  __u64 __reserved[8];
};
#endif

static char last_error[256];
static char global_log[4096];
static uint32_t t_dm_crypt_flags = 0;

char *THE_LOOP_DEV = NULL;
int _debug   = 0;
int global_lines = 0;
int _quit = 0;
int _verbose = 0;
uint64_t t_dev_offset = 0;

static void (*_cleanup)(void);

void register_cleanup(void (*cleanup)(void))
{
	_cleanup = cleanup;
}

void check_ok(int status, int line, const char *func)
{
	if (status) {
		printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, last_error);
		_cleanup();
		exit(-1);
	}
}

void check_ok_return(int status, int line, const char *func)
{
	if (status < 0) {
		printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, last_error);
		_cleanup();
		exit(-1);
	}
}

void check_ko(int status, int line, const char *func)
{
	if (status >= 0) {
		printf("FAIL line %d [%s]: code %d, %s\n", line, func, status, last_error);
		_cleanup();
		exit(-1);
	} else if (_verbose)
		printf("   => errno %d, errmsg: %s\n", status, last_error);
}

void check_equal(int line, const char *func, int64_t x, int64_t y)
{
	printf("FAIL line %d [%s]: expected equal values differs: %"
		PRIi64 " != %" PRIi64 "\n", line, func, x, y);
	_cleanup();
	exit(-1);
}

void check_ge_equal(int line, const char *func, int64_t x, int64_t y)
{
	printf("FAIL line %d [%s]: expected greater or equal values differs: %"
		PRIi64 " < %" PRIi64 "\n", line, func, x, y);
	_cleanup();
	exit(-1);
}

void check_null(int line, const char *func, const void *x)
{
	if (x) {
		printf("FAIL line %d [%s]: expected NULL value: %p\n", line, func, x);
		_cleanup();
		exit(-1);
	}
}

void check_notnull(int line, const char *func, const void *x)
{
	if (!x) {
		printf("FAIL line %d [%s]: expected not NULL value: %p\n", line, func, x);
		_cleanup();
		exit(-1);
	}
}

void xlog(const char *msg, const char *tst, const char *func, int line, const char *txt)
{
	if (_verbose) {
		if (txt)
			printf(" [%s,%s:%d] %s [%s]\n", msg, func, line, tst, txt);
		else
			printf(" [%s,%s:%d] %s\n", msg, func, line, tst);
	}
	if (_quit) {
		if (_verbose)
			printf("Interrupted by a signal.\n");
		_cleanup();
		exit(-1);
	}
}

int t_device_size(const char *device, uint64_t *size)
{
	int devfd, r = 0;

	devfd = open(device, O_RDONLY);
	if(devfd == -1)
		return -EINVAL;

	if (ioctl(devfd, BLKGETSIZE64, size) < 0)
		r = -EINVAL;
	close(devfd);
	return r;
}

int t_set_readahead(const char *device, unsigned value)
{
	int devfd, r = 0;

	devfd = open(device, O_RDONLY);
	if(devfd == -1)
		return -EINVAL;

	if (ioctl(devfd, BLKRASET, value) < 0)
		r = -EINVAL;
	close(devfd);
	return r;
}

int fips_mode(void)
{
	int fd;
	char buf = 0;

	fd = open("/proc/sys/crypto/fips_enabled", O_RDONLY);

	if (fd < 0)
		return 0;

	if (read(fd, &buf, 1) != 1)
		buf = '0';

	close(fd);

	return (buf == '1');
}

/*
 * Creates dm-linear target over the test loop device. Offset is held in
 * global variables so that size can be tested whether it fits into remaining
 * size of the loop device or not
 */
int create_dmdevice_over_loop(const char *dm_name, const uint64_t size)
{
	int r;

	r = create_dmdevice_over_device(dm_name, THE_LOOP_DEV, size, t_dev_offset);
	if (r != 0)
		return r;

	t_dev_offset += size;

	return r;
}

/*
 * Creates dm-linear target over the desired block device.
 */
int create_dmdevice_over_device(const char *dm_name, const char *device, uint64_t size, uint64_t offset)
{
	char cmd[128];
	int r;
	uint64_t r_size;

	if (!device || t_device_size(device, &r_size) < 0 || r_size <= offset || !size)
		return -1;
	if ((r_size - offset) < size) {
		printf("No enough space on device %s\n.", device);
		return -2;
	}
	r = snprintf(cmd, sizeof(cmd),
		     "dmsetup create %s --table \"0 %" PRIu64 " linear %s %" PRIu64 "\"",
		     dm_name, size, device, offset);
	if (r < 0 || (size_t)r >= sizeof(cmd))
		return -3;

	return _system(cmd, 1);
}

__attribute__((format(printf, 3, 4)))
static int _snprintf(char **r_ptr, size_t *r_remains, const char *format, ...)
{
	int len, r = 0;
	va_list argp;

	assert(r_remains);
	assert(r_ptr);

	va_start(argp, format);

	len = vsnprintf(*r_ptr, *r_remains, format, argp);
	if (len < 0 || (size_t)len >= *r_remains) {
		r = -EINVAL;
	} else {
		*r_ptr += len;
		*r_remains -= len;
	}

	va_end(argp);

	return r;
}

int dmdevice_error_io(const char *dm_name,
	const char *dm_device,
	const char *error_device,
	uint64_t data_offset,
	uint64_t offset,
	uint64_t length,
	error_io_info ei)
{
	char str[256], cmd[384];
	int r;
	uint64_t dev_size;
	size_t remains;
	char *ptr;

	if (t_device_size(dm_device, &dev_size) < 0 || !length)
		return -1;

	dev_size >>= TST_SECTOR_SHIFT;

	if (dev_size <= offset)
		return -1;

	if (ei == ERR_REMOVE) {
		r = snprintf(cmd, sizeof(cmd),
			     "dmsetup load %s --table \"0 %" PRIu64 " linear %s %" PRIu64 "\"",
			     dm_name, dev_size, THE_LOOP_DEV, data_offset);
		if (r < 0 || (size_t)r >= sizeof(str))
			return -3;

		if ((r = _system(cmd, 1)))
			return r;

		r = snprintf(cmd, sizeof(cmd), "dmsetup resume %s", dm_name);
		if (r < 0 || (size_t)r >= sizeof(cmd))
			return -3;

		return _system(cmd, 1);
	}

	if ((dev_size - offset) < length) {
		printf("Not enough space on target device\n.");
		return -2;
	}

	remains = sizeof(str);
	ptr = str;

	if (offset) {
		r = _snprintf(&ptr, &remains,
			     "0 %" PRIu64 " linear %s %" PRIu64 "\n",
			     offset, THE_LOOP_DEV, data_offset);
		if (r < 0)
			return r;
	}
	r = _snprintf(&ptr, &remains, "%" PRIu64 " %" PRIu64 " delay ",
		      offset, length);
	if (r < 0)
		return r;

	if (ei == ERR_RW || ei == ERR_RD) {
		r = _snprintf(&ptr, &remains, "%s 0 0",
			     error_device);
		if (r < 0)
			return r;
		if (ei == ERR_RD) {
			r = _snprintf(&ptr, &remains, " %s %" PRIu64 " 0",
				     THE_LOOP_DEV, data_offset + offset);
			if (r < 0)
				return r;
		}
	} else if (ei == ERR_WR) {
		r = _snprintf(&ptr, &remains, "%s %" PRIu64 " 0 %s 0 0",
			     THE_LOOP_DEV, data_offset + offset, error_device);
		if (r < 0)
			return r;
	}

	if (dev_size > (offset + length)) {
		r = _snprintf(&ptr, &remains,
			     "\n%" PRIu64 " %" PRIu64 " linear %s %" PRIu64,
			     offset + length, dev_size - offset - length, THE_LOOP_DEV,
			     data_offset + offset + length);
		if (r < 0)
			return r;
	}

	/*
	 * Hello darkness, my old friend...
	 *
	 * On few old distributions there's issue with
	 * processing multiline tables via dmsetup load --table.
	 * This workaround passes on all systems we run tests on.
	 */
	r = snprintf(cmd, sizeof(cmd), "dmsetup load %s <<EOF\n%s\nEOF", dm_name, str);
	if (r < 0 || (size_t)r >= sizeof(cmd))
		return -3;

	if ((r = _system(cmd, 1)))
		return r;

	r = snprintf(cmd, sizeof(cmd), "dmsetup resume %s", dm_name);
	if (r < 0 || (size_t)r >= sizeof(cmd))
		return -3;

	if ((r = _system(cmd, 1)))
		return r;

	return t_set_readahead(dm_device, 0);
}

// Get key from kernel dm mapping table using dm-ioctl
int get_key_dm(const char *name, char *buffer, unsigned int buffer_size)
{
	struct dm_task *dmt;
	struct dm_info dmi;
	uint64_t start, length;
	char *target_type, *key, *params;
	int r = -EINVAL;

	if (!(dmt = dm_task_create(DM_DEVICE_TABLE)))
		goto out;
	if (!dm_task_set_name(dmt, name))
		goto out;
	if (!dm_task_run(dmt))
		goto out;
	if (!dm_task_get_info(dmt, &dmi))
		goto out;
	if (!dmi.exists)
		goto out;

	dm_get_next_target(dmt, NULL, &start, &length, &target_type, &params);
	if (!target_type || strcmp(target_type, "crypt") != 0)
		goto out;

	(void)strsep(&params, " "); /* rcipher */
	key = strsep(&params, " ");

	if (buffer_size <= strlen(key))
		goto out;

	strncpy(buffer, key, buffer_size);
	r = 0;
out:
	if (dmt)
		dm_task_destroy(dmt);

	return r;
}

int prepare_keyfile(const char *name, const char *passphrase, int size)
{
	int fd, r;

	fd = open(name, O_RDWR | O_CREAT | O_TRUNC, S_IRUSR|S_IWUSR);
	if (fd != -1) {
		r = write(fd, passphrase, size);
		close(fd);
	} else
		r = 0;

	return r == size ? 0 : 1;
}

// Decode key from its hex representation
int crypt_decode_key(char *key, const char *hex, unsigned int size)
{
	char buffer[3];
	char *endp;
	unsigned int i;

	buffer[2] = '\0';

	for (i = 0; i < size; i++) {
		buffer[0] = *hex++;
		buffer[1] = *hex++;

		key[i] = (unsigned char)strtoul(buffer, &endp, 16);

		if (endp != &buffer[2])
			return -1;
	}

	if (*hex != '\0')
		return -1;

	return 0;
}

void global_log_callback(int level, const char *msg, void *usrptr __attribute__((unused)))
{
	size_t len;

	if (_debug) {
		if (level == CRYPT_LOG_DEBUG)
			fprintf(stdout, "# %s", msg);
		else
			fprintf(stdout, "%s", msg);
	}

	if (level <= CRYPT_LOG_DEBUG)
		return;

	len = strlen(global_log);

	if (len + strlen(msg) >= sizeof(global_log)) {
			printf("Log buffer is too small, fix the test.\n");
			return;
	}

	strncat(global_log, msg, sizeof(global_log) - len - 1);
	global_lines++;
	if (level == CRYPT_LOG_ERROR) {
		len = strlen(msg);
		if (len > sizeof(last_error))
			len = sizeof(last_error);
		strncpy(last_error, msg, sizeof(last_error));
		last_error[len-1] = '\0';
	}
}

void reset_log(void)
{
	memset(global_log, 0, sizeof(global_log));
	memset(last_error, 0, sizeof(last_error));
	global_lines = 0;
}

int _system(const char *command, int warn)
{
	int r;
	if (_debug)
		printf("Running system: %s\n", command);
	if ((r=system(command)) < 0 && warn)
		printf("System command failed: %s", command);
	return r;
}

static int _keyring_check(void)
{
#ifdef KERNEL_KEYRING
	return syscall(__NR_request_key, "logon", "dummy", NULL, 0) == -1l && errno != ENOSYS;
#else
	return 0;
#endif
}

static int t_dm_satisfies_version(unsigned target_maj, unsigned target_min, unsigned target_patch,
				 unsigned actual_maj, unsigned actual_min, unsigned actual_patch)
{
	if (actual_maj > target_maj)
		return 1;
	if (actual_maj == target_maj && actual_min > target_min)
		return 1;
	if (actual_maj == target_maj && actual_min == target_min && actual_patch >= target_patch)
		return 1;
	return 0;
}

static void t_dm_set_crypt_compat(const char *dm_version, unsigned crypt_maj,
				 unsigned crypt_min, unsigned crypt_patch)
{
	unsigned dm_maj = 0, dm_min = 0, dm_patch = 0;

	if (sscanf(dm_version, "%u.%u.%u", &dm_maj, &dm_min, &dm_patch) != 3) {
		dm_maj = 0;
		dm_min = 0;
		dm_patch = 0;
	}

	if (t_dm_satisfies_version(1, 2, 0, crypt_maj, crypt_min, 0))
		t_dm_crypt_flags |= T_DM_KEY_WIPE_SUPPORTED;

	if (t_dm_satisfies_version(1, 10, 0, crypt_maj, crypt_min, 0))
		t_dm_crypt_flags |= T_DM_LMK_SUPPORTED;

	if (t_dm_satisfies_version(4, 20, 0, dm_maj, dm_min, 0))
		t_dm_crypt_flags |= T_DM_SECURE_SUPPORTED;

	if (t_dm_satisfies_version(1, 8, 0, crypt_maj, crypt_min, 0))
		t_dm_crypt_flags |= T_DM_PLAIN64_SUPPORTED;

	if (t_dm_satisfies_version(1, 11, 0, crypt_maj, crypt_min, 0))
		t_dm_crypt_flags |= T_DM_DISCARDS_SUPPORTED;

	if (t_dm_satisfies_version(1, 13, 0, crypt_maj, crypt_min, 0))
		t_dm_crypt_flags |= T_DM_TCW_SUPPORTED;

	if (t_dm_satisfies_version(1, 14, 0, crypt_maj, crypt_min, 0)) {
		t_dm_crypt_flags |= T_DM_SAME_CPU_CRYPT_SUPPORTED;
		t_dm_crypt_flags |= T_DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED;
	}

	if (t_dm_satisfies_version(1, 18, 1, crypt_maj, crypt_min, crypt_patch) && _keyring_check())
		t_dm_crypt_flags |= T_DM_KERNEL_KEYRING_SUPPORTED;

	if (t_dm_satisfies_version(1, 17, 0, crypt_maj, crypt_min, crypt_patch)) {
		t_dm_crypt_flags |= T_DM_SECTOR_SIZE_SUPPORTED;
		t_dm_crypt_flags |= T_DM_CAPI_STRING_SUPPORTED;
	}

	if (t_dm_satisfies_version(1, 19, 0, crypt_maj, crypt_min, crypt_patch))
		t_dm_crypt_flags |= T_DM_BITLK_EBOIV_SUPPORTED;

	if (t_dm_satisfies_version(1, 20, 0, crypt_maj, crypt_min, crypt_patch))
		t_dm_crypt_flags |= T_DM_BITLK_ELEPHANT_SUPPORTED;

	if (t_dm_satisfies_version(1, 22, 0, crypt_maj, crypt_min, crypt_patch))
		t_dm_crypt_flags |= T_DM_CRYPT_NO_WORKQUEUE_SUPPORTED;
}

static void t_dm_set_verity_compat(const char *dm_version __attribute__((unused)),
	unsigned verity_maj,
	unsigned verity_min,
	unsigned verity_patch __attribute__((unused)))
{
	if (verity_maj > 0)
		t_dm_crypt_flags |= T_DM_VERITY_SUPPORTED;
	else
		return;
	/*
	 * ignore_corruption, restart_on corruption is available since 1.2 (kernel 4.1)
	 * ignore_zero_blocks since 1.3 (kernel 4.5)
	 * (but some dm-verity targets 1.2 don't support it)
	 * FEC is added in 1.3 as well.
	 */
	if (t_dm_satisfies_version(1, 3, 0, verity_maj, verity_min, 0)) {
		t_dm_crypt_flags |= T_DM_VERITY_ON_CORRUPTION_SUPPORTED;
		t_dm_crypt_flags |= T_DM_VERITY_FEC_SUPPORTED;
	}

	if (t_dm_satisfies_version(1, 5, 0, verity_maj, verity_min, verity_patch))
		t_dm_crypt_flags |= T_DM_VERITY_SIGNATURE_SUPPORTED;

	if (t_dm_satisfies_version(1, 7, 0, verity_maj, verity_min, verity_patch))
		t_dm_crypt_flags |= T_DM_VERITY_PANIC_CORRUPTION_SUPPORTED;

	if (t_dm_satisfies_version(1, 9, 0, verity_maj, verity_min, verity_patch))
		t_dm_crypt_flags |= T_DM_VERITY_TASKLETS_SUPPORTED;
}

static void t_dm_set_integrity_compat(const char *dm_version __attribute__((unused)),
	unsigned integrity_maj __attribute__((unused)),
	unsigned integrity_min __attribute__((unused)),
	unsigned integrity_patch __attribute__((unused)))
{
	if (integrity_maj > 0)
		t_dm_crypt_flags |= T_DM_INTEGRITY_SUPPORTED;

	if (t_dm_satisfies_version(1, 2, 0, integrity_maj, integrity_min, integrity_patch))
		t_dm_crypt_flags |= T_DM_INTEGRITY_RECALC_SUPPORTED;

	if (t_dm_satisfies_version(1, 3, 0, integrity_maj, integrity_min, integrity_patch))
		t_dm_crypt_flags |= T_DM_INTEGRITY_BITMAP_SUPPORTED;

	if (t_dm_satisfies_version(1, 4, 0, integrity_maj, integrity_min, integrity_patch))
		t_dm_crypt_flags |= T_DM_INTEGRITY_FIX_PADDING_SUPPORTED;

	if (t_dm_satisfies_version(1, 6, 0, integrity_maj, integrity_min, integrity_patch))
		t_dm_crypt_flags |= T_DM_INTEGRITY_DISCARDS_SUPPORTED;

	if (t_dm_satisfies_version(1, 7, 0, integrity_maj, integrity_min, integrity_patch))
		t_dm_crypt_flags |= T_DM_INTEGRITY_FIX_HMAC_SUPPORTED;

	if (t_dm_satisfies_version(1, 8, 0, integrity_maj, integrity_min, integrity_patch))
		t_dm_crypt_flags |= T_DM_INTEGRITY_RESET_RECALC_SUPPORTED;
}

int t_dm_check_versions(void)
{
	struct dm_task *dmt;
	struct dm_versions *target, *last_target;
	char dm_version[16];
	int r = 1;

	if (!(dmt = dm_task_create(DM_DEVICE_LIST_VERSIONS)))
		goto out;

	if (!dm_task_run(dmt))
		goto out;

	if (!dm_task_get_driver_version(dmt, dm_version, sizeof(dm_version)))
		goto out;

	target = dm_task_get_versions(dmt);
	do {
		last_target = target;
		if (!strcmp("crypt", target->name)) {
			t_dm_set_crypt_compat(dm_version,
					     (unsigned)target->version[0],
					     (unsigned)target->version[1],
					     (unsigned)target->version[2]);
		} else if (!strcmp("verity", target->name)) {
			t_dm_set_verity_compat(dm_version,
					     (unsigned)target->version[0],
					     (unsigned)target->version[1],
					     (unsigned)target->version[2]);
		} else if (!strcmp("integrity", target->name)) {
			t_dm_set_integrity_compat(dm_version,
					     (unsigned)target->version[0],
					     (unsigned)target->version[1],
					     (unsigned)target->version[2]);
		}
		target = VOIDP_CAST(struct dm_versions *)((char *) target + target->next);
	} while (last_target != target);

	r = 0;
out:
	if (dmt)
		dm_task_destroy(dmt);

	return r;
}

int t_dm_crypt_keyring_support(void)
{
	return t_dm_crypt_flags & T_DM_KERNEL_KEYRING_SUPPORTED;
}

int t_dm_crypt_cpu_switch_support(void)
{
	return t_dm_crypt_flags & (T_DM_SAME_CPU_CRYPT_SUPPORTED |
				   T_DM_SUBMIT_FROM_CRYPT_CPUS_SUPPORTED);
}

int t_dm_crypt_discard_support(void)
{
	return t_dm_crypt_flags & T_DM_DISCARDS_SUPPORTED;
}

int t_dm_integrity_resize_support(void)
{
	return t_dm_crypt_flags & T_DM_INTEGRITY_RESIZE_SUPPORTED;
}

int t_dm_integrity_recalculate_support(void)
{
	return t_dm_crypt_flags & T_DM_INTEGRITY_RECALC_SUPPORTED;
}

int t_dm_capi_string_supported(void)
{
	return t_dm_crypt_flags & T_DM_CAPI_STRING_SUPPORTED;
}

/* loop helpers */

#define LOOP_DEV_MAJOR 7

#ifndef LO_FLAGS_AUTOCLEAR
#define LO_FLAGS_AUTOCLEAR 4
#endif

#ifndef LOOP_CTL_GET_FREE
#define LOOP_CTL_GET_FREE 0x4C82
#endif

#ifndef LOOP_SET_CAPACITY
#define LOOP_SET_CAPACITY 0x4C07
#endif

int loop_device(const char *loop)
{
	struct stat st;

	if (!loop)
		return 0;

	if (stat(loop, &st) || !S_ISBLK(st.st_mode) ||
	    major(st.st_rdev) != LOOP_DEV_MAJOR)
		return 0;

	return 1;
}

static char *crypt_loop_get_device_old(void)
{
	char dev[64];
	int i, loop_fd;
	struct loop_info64 lo64 = {0};

	for (i = 0; i < 256; i++) {
		sprintf(dev, "/dev/loop%d", i);

		loop_fd = open(dev, O_RDONLY);
		if (loop_fd < 0)
			return NULL;

		if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) &&
		    errno == ENXIO) {
			close(loop_fd);
			return strdup(dev);
		}
		close(loop_fd);
	}

	return NULL;
}

static char *crypt_loop_get_device(void)
{
	char dev[64];
	int i, loop_fd;
	struct stat st;

	loop_fd = open("/dev/loop-control", O_RDONLY);
	if (loop_fd < 0)
		return crypt_loop_get_device_old();

	i = ioctl(loop_fd, LOOP_CTL_GET_FREE);
	if (i < 0) {
		close(loop_fd);
		return NULL;
	}
	close(loop_fd);

	if (sprintf(dev, "/dev/loop%d", i) < 0)
		return NULL;

	if (stat(dev, &st) || !S_ISBLK(st.st_mode))
		return NULL;

	return strdup(dev);
}

int loop_attach(char **loop, const char *file, int offset,
		      int autoclear, int *readonly)
{
	struct loop_config config = {0};
	char *lo_file_name;
	int loop_fd = -1, file_fd = -1, r = 1;
	int fallback = 0;

	*loop = NULL;

	file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
	if (file_fd < 0 && (errno == EROFS || errno == EACCES) && !*readonly) {
		*readonly = 1;
		file_fd = open(file, O_RDONLY | O_EXCL);
	}
	if (file_fd < 0)
		goto out;

	config.fd = file_fd;

	lo_file_name = (char*)config.info.lo_file_name;
	lo_file_name[LO_NAME_SIZE-1] = '\0';
	strncpy(lo_file_name, file, LO_NAME_SIZE-1);
	config.info.lo_offset = offset;
	if (autoclear)
		config.info.lo_flags |= LO_FLAGS_AUTOCLEAR;

	while (loop_fd < 0)  {
		*loop = crypt_loop_get_device();
		if (!*loop)
			goto out;

		loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR);
		if (loop_fd < 0)
			goto out;
		if (ioctl(loop_fd, LOOP_CONFIGURE, &config) < 0) {
			if (errno == EINVAL || errno == ENOTTY) {
				free(*loop);
				*loop = NULL;

				close(loop_fd);
				loop_fd = -1;

				/* kernel doesn't support LOOP_CONFIGURE */
				fallback = 1;
				break;
			}
			if (errno != EBUSY)
				goto out;
			free(*loop);
			*loop = NULL;

			close(loop_fd);
			loop_fd = -1;
		}
	}

	if (fallback)
	{
		while (loop_fd < 0)  {
			*loop = crypt_loop_get_device();
			if (!*loop)
				goto out;

			loop_fd = open(*loop, *readonly ? O_RDONLY : O_RDWR);
			if (loop_fd < 0)
				goto out;
			if (ioctl(loop_fd, LOOP_SET_FD, file_fd) < 0) {
				if (errno != EBUSY)
					goto out;
				free(*loop);
				*loop = NULL;

				close(loop_fd);
				loop_fd = -1;
			}
		}

		if (ioctl(loop_fd, LOOP_SET_STATUS64, &config.info) < 0) {
			(void)ioctl(loop_fd, LOOP_CLR_FD, 0);
			goto out;
		}
	}

	/* Verify that autoclear is really set */
	if (autoclear) {
		memset(&config.info, 0, sizeof(config.info));
		if (ioctl(loop_fd, LOOP_GET_STATUS64, &config.info) < 0 ||
		   !(config.info.lo_flags & LO_FLAGS_AUTOCLEAR)) {
		(void)ioctl(loop_fd, LOOP_CLR_FD, 0);
			goto out;
		}
	}

	r = 0;
out:
	if (r && loop_fd >= 0)
		close(loop_fd);
	if (file_fd >= 0)
		close(file_fd);
	if (r && *loop) {
		free(*loop);
		*loop = NULL;
	}
	return r ? -1 : loop_fd;
}

int loop_detach(const char *loop)
{
	int loop_fd = -1, r = 1;

	loop_fd = open(loop, O_RDONLY);
	if (loop_fd < 0)
                return 1;

	if (!ioctl(loop_fd, LOOP_CLR_FD, 0))
		r = 0;

	close(loop_fd);
	return r;
}

int t_get_devno(const char *name, dev_t *devno)
{
	char path[PATH_MAX];
	int r;
	struct stat st;

	r = snprintf(path, sizeof(path), DMDIR "%s", name);
	if (r < 0 || (size_t)r >= sizeof(path))
		return 1;

	if (stat(path, &st) || !S_ISBLK(st.st_mode))
		return 1;

	*devno = st.st_rdev;

	return 0;
}

static int _read_uint64(const char *sysfs_path, uint64_t *value)
{
        char tmp[64] = {0};
        int fd, r;

        if ((fd = open(sysfs_path, O_RDONLY)) < 0)
                return 0;
        r = read(fd, tmp, sizeof(tmp));
        close(fd);

        if (r <= 0)
                return 0;

        if (sscanf(tmp, "%" PRIu64, value) != 1)
                return 0;

        return 1;
}

static int _sysfs_get_uint64(int major, int minor, uint64_t *value, const char *attr)
{
        char path[PATH_MAX];

        if (snprintf(path, sizeof(path), "/sys/dev/block/%d:%d/%s",
                     major, minor, attr) < 0)
                return 0;

        return _read_uint64(path, value);
}

int t_device_size_by_devno(dev_t devno, uint64_t *retval)
{
	if (!_sysfs_get_uint64(major(devno), minor(devno), retval, "size"))
		return 1;

	*retval *= 512;
	return 0;
}