mirror of
https://gitlab.com/cryptsetup/cryptsetup.git
synced 2025-12-13 20:00:08 +01:00
Do not depend in test on loop code from library sources.
This patch duplicates part of the code because following switch to non-recursive automake is not easily fixable without this change. (Automake cannot use top_srcdir anymore in this context.)
This commit is contained in:
@@ -64,13 +64,13 @@ clean-local:
|
||||
differ_SOURCES = differ.c
|
||||
differ_CFLAGS = $(AM_CFLAGS) -Wall -O2
|
||||
|
||||
api_test_SOURCES = api-test.c api_test.h test_utils.c $(top_srcdir)/lib/utils_loop.c
|
||||
api_test_SOURCES = api-test.c api_test.h test_utils.c
|
||||
api_test_LDADD = ../lib/libcryptsetup.la
|
||||
api_test_LDFLAGS = $(AM_LDFLAGS) -static
|
||||
api_test_CFLAGS = -g -Wall -O0 $(AM_CFLAGS) -I$(top_srcdir)/lib/ -I$(top_srcdir)/lib/luks1
|
||||
api_test_CPPFLAGS = $(AM_CPPFLAGS) -include config.h
|
||||
|
||||
api_test_2_SOURCES = api-test-2.c api_test.h test_utils.c $(top_srcdir)/lib/utils_loop.c
|
||||
api_test_2_SOURCES = api-test-2.c api_test.h test_utils.c
|
||||
api_test_2_LDADD = ../lib/libcryptsetup.la
|
||||
api_test_2_LDFLAGS = $(AM_LDFLAGS) -static
|
||||
api_test_2_CFLAGS = -g -Wall -O0 $(AM_CFLAGS) -I$(top_srcdir)/lib/ -I$(top_srcdir)/lib/luks1
|
||||
|
||||
@@ -42,7 +42,6 @@ typedef int32_t key_serial_t;
|
||||
#include "api_test.h"
|
||||
#include "luks.h"
|
||||
#include "libcryptsetup.h"
|
||||
#include "utils_loop.h"
|
||||
|
||||
#define DMDIR "/dev/mapper/"
|
||||
|
||||
@@ -235,26 +234,26 @@ static void _cleanup(void)
|
||||
|
||||
_cleanup_dmdevices();
|
||||
|
||||
if (crypt_loop_device(THE_LOOP_DEV))
|
||||
crypt_loop_detach(THE_LOOP_DEV);
|
||||
if (loop_device(THE_LOOP_DEV))
|
||||
loop_detach(THE_LOOP_DEV);
|
||||
|
||||
if (crypt_loop_device(DEVICE_1))
|
||||
crypt_loop_detach(DEVICE_1);
|
||||
if (loop_device(DEVICE_1))
|
||||
loop_detach(DEVICE_1);
|
||||
|
||||
if (crypt_loop_device(DEVICE_2))
|
||||
crypt_loop_detach(DEVICE_2);
|
||||
if (loop_device(DEVICE_2))
|
||||
loop_detach(DEVICE_2);
|
||||
|
||||
if (crypt_loop_device(DEVICE_3))
|
||||
crypt_loop_detach(DEVICE_3);
|
||||
if (loop_device(DEVICE_3))
|
||||
loop_detach(DEVICE_3);
|
||||
|
||||
if (crypt_loop_device(DEVICE_4))
|
||||
crypt_loop_detach(DEVICE_4);
|
||||
if (loop_device(DEVICE_4))
|
||||
loop_detach(DEVICE_4);
|
||||
|
||||
if (crypt_loop_device(DEVICE_5))
|
||||
crypt_loop_detach(DEVICE_5);
|
||||
if (loop_device(DEVICE_5))
|
||||
loop_detach(DEVICE_5);
|
||||
|
||||
if (crypt_loop_device(DEVICE_6))
|
||||
crypt_loop_detach(DEVICE_6);
|
||||
if (loop_device(DEVICE_6))
|
||||
loop_detach(DEVICE_6);
|
||||
|
||||
_system("rm -f " IMAGE_EMPTY, 0);
|
||||
_system("rm -f " IMAGE1, 0);
|
||||
@@ -297,7 +296,7 @@ static int _setup(void)
|
||||
if (_system(cmd, 1))
|
||||
return 1;
|
||||
|
||||
fd = crypt_loop_attach(&THE_LOOP_DEV, test_loop_file, 0, 0, &ro);
|
||||
fd = loop_attach(&THE_LOOP_DEV, test_loop_file, 0, 0, &ro);
|
||||
close(fd);
|
||||
|
||||
tmp_file_1 = strdup(THE_LFILE_TEMPLATE);
|
||||
@@ -315,23 +314,23 @@ static int _setup(void)
|
||||
_system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
|
||||
|
||||
_system(" [ ! -e " IMAGE1 " ] && xz -dk " IMAGE1 ".xz", 1);
|
||||
fd = crypt_loop_attach(&DEVICE_1, IMAGE1, 0, 0, &ro);
|
||||
fd = loop_attach(&DEVICE_1, IMAGE1, 0, 0, &ro);
|
||||
close(fd);
|
||||
|
||||
_system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=32 2>/dev/null", 1);
|
||||
fd = crypt_loop_attach(&DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
|
||||
fd = loop_attach(&DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
|
||||
close(fd);
|
||||
|
||||
_system(" [ ! -e " VALID_LUKS2_HEADER " ] && xz -dk " VALID_LUKS2_HEADER ".xz", 1);
|
||||
fd = crypt_loop_attach(&DEVICE_4, VALID_LUKS2_HEADER, 0, 0, &ro);
|
||||
fd = loop_attach(&DEVICE_4, VALID_LUKS2_HEADER, 0, 0, &ro);
|
||||
close(fd);
|
||||
|
||||
_system(" [ ! -e " REQS_LUKS2_HEADER " ] && xz -dk " REQS_LUKS2_HEADER ".xz", 1);
|
||||
fd = crypt_loop_attach(&DEVICE_5, REQS_LUKS2_HEADER, 0, 0, &ro);
|
||||
fd = loop_attach(&DEVICE_5, REQS_LUKS2_HEADER, 0, 0, &ro);
|
||||
close(fd);
|
||||
|
||||
_system(" [ ! -e " NO_REQS_LUKS2_HEADER " ] && xz -dk " NO_REQS_LUKS2_HEADER ".xz", 1);
|
||||
fd = crypt_loop_attach(&DEVICE_6, NO_REQS_LUKS2_HEADER, 0, 0, &ro);
|
||||
fd = loop_attach(&DEVICE_6, NO_REQS_LUKS2_HEADER, 0, 0, &ro);
|
||||
close(fd);
|
||||
|
||||
_system(" [ ! -d " CONV_DIR " ] && tar xJf " CONV_DIR ".tar.xz", 1);
|
||||
@@ -1108,7 +1107,7 @@ static void Luks2HeaderBackup(void)
|
||||
crypt_free(cd);
|
||||
|
||||
// exercise luksOpen using backup header on block device
|
||||
fd = crypt_loop_attach(&DEVICE_3, BACKUP_FILE, 0, 0, &ro);
|
||||
fd = loop_attach(&DEVICE_3, BACKUP_FILE, 0, 0, &ro);
|
||||
close(fd);
|
||||
OK_(fd < 0);
|
||||
OK_(crypt_init(&cd, DEVICE_3));
|
||||
|
||||
@@ -33,7 +33,6 @@
|
||||
#include "api_test.h"
|
||||
#include "luks.h"
|
||||
#include "libcryptsetup.h"
|
||||
#include "utils_loop.h"
|
||||
|
||||
#define DMDIR "/dev/mapper/"
|
||||
|
||||
@@ -185,17 +184,17 @@ static void _cleanup(void)
|
||||
|
||||
_cleanup_dmdevices();
|
||||
|
||||
if (crypt_loop_device(THE_LOOP_DEV))
|
||||
crypt_loop_detach(THE_LOOP_DEV);
|
||||
if (loop_device(THE_LOOP_DEV))
|
||||
loop_detach(THE_LOOP_DEV);
|
||||
|
||||
if (crypt_loop_device(DEVICE_1))
|
||||
crypt_loop_detach(DEVICE_1);
|
||||
if (loop_device(DEVICE_1))
|
||||
loop_detach(DEVICE_1);
|
||||
|
||||
if (crypt_loop_device(DEVICE_2))
|
||||
crypt_loop_detach(DEVICE_2);
|
||||
if (loop_device(DEVICE_2))
|
||||
loop_detach(DEVICE_2);
|
||||
|
||||
if (crypt_loop_device(DEVICE_3))
|
||||
crypt_loop_detach(DEVICE_3);
|
||||
if (loop_device(DEVICE_3))
|
||||
loop_detach(DEVICE_3);
|
||||
|
||||
_system("rm -f " IMAGE_EMPTY, 0);
|
||||
_system("rm -f " IMAGE1, 0);
|
||||
@@ -237,7 +236,7 @@ static int _setup(void)
|
||||
if (_system(cmd, 1))
|
||||
return 1;
|
||||
|
||||
fd = crypt_loop_attach(&THE_LOOP_DEV, test_loop_file, 0, 0, &ro);
|
||||
fd = loop_attach(&THE_LOOP_DEV, test_loop_file, 0, 0, &ro);
|
||||
close(fd);
|
||||
|
||||
tmp_file_1 = strdup(THE_LFILE_TEMPLATE);
|
||||
@@ -255,11 +254,11 @@ static int _setup(void)
|
||||
_system("dmsetup create " DEVICE_ERROR_name " --table \"0 10000 error\"", 1);
|
||||
|
||||
_system(" [ ! -e " IMAGE1 " ] && bzip2 -dk " IMAGE1 ".bz2", 1);
|
||||
fd = crypt_loop_attach(&DEVICE_1, IMAGE1, 0, 0, &ro);
|
||||
fd = loop_attach(&DEVICE_1, IMAGE1, 0, 0, &ro);
|
||||
close(fd);
|
||||
|
||||
_system("dd if=/dev/zero of=" IMAGE_EMPTY " bs=1M count=4 2>/dev/null", 1);
|
||||
fd = crypt_loop_attach(&DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
|
||||
fd = loop_attach(&DEVICE_2, IMAGE_EMPTY, 0, 0, &ro);
|
||||
close(fd);
|
||||
|
||||
/* Keymaterial offset is less than 8 sectors */
|
||||
@@ -1227,7 +1226,7 @@ static void LuksHeaderBackup(void)
|
||||
crypt_free(cd);
|
||||
|
||||
// exercise luksOpen using backup header on block device
|
||||
fd = crypt_loop_attach(&DEVICE_3, BACKUP_FILE, 0, 0, &ro);
|
||||
fd = loop_attach(&DEVICE_3, BACKUP_FILE, 0, 0, &ro);
|
||||
close(fd);
|
||||
OK_(fd < 0);
|
||||
OK_(crypt_init(&cd, DEVICE_3));
|
||||
|
||||
@@ -105,4 +105,10 @@ void xlog(const char *msg, const char *tst, const char *func, int line, const ch
|
||||
#define T_DM_INTEGRITY_SUPPORTED (1 << 12) /* dm-integrity target supported */
|
||||
//FIXME add T_DM_SECTOR_SIZE once we have version
|
||||
|
||||
/* loop helpers */
|
||||
int loop_device(const char *loop);
|
||||
int loop_attach(char **loop, const char *file, int offset,
|
||||
int autoclear, int *readonly);
|
||||
int loop_detach(const char *loop);
|
||||
|
||||
#endif
|
||||
|
||||
@@ -29,6 +29,7 @@
|
||||
#include <sys/stat.h>
|
||||
#include <sys/types.h>
|
||||
#include <unistd.h>
|
||||
#include <linux/loop.h>
|
||||
|
||||
#include "api_test.h"
|
||||
#include "libcryptsetup.h"
|
||||
@@ -402,3 +403,170 @@ int t_dm_crypt_keyring_support(void)
|
||||
{
|
||||
return t_dm_crypt_flags & T_DM_KERNEL_KEYRING_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[20];
|
||||
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_info64 lo64 = {0};
|
||||
char *lo_file_name;
|
||||
int loop_fd = -1, file_fd = -1, r = 1;
|
||||
|
||||
*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;
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
lo_file_name = (char*)lo64.lo_file_name;
|
||||
lo_file_name[LO_NAME_SIZE-1] = '\0';
|
||||
strncpy(lo_file_name, file, LO_NAME_SIZE-1);
|
||||
lo64.lo_offset = offset;
|
||||
if (autoclear)
|
||||
lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
|
||||
|
||||
if (ioctl(loop_fd, LOOP_SET_STATUS64, &lo64) < 0) {
|
||||
(void)ioctl(loop_fd, LOOP_CLR_FD, 0);
|
||||
goto out;
|
||||
}
|
||||
|
||||
/* Verify that autoclear is really set */
|
||||
if (autoclear) {
|
||||
memset(&lo64, 0, sizeof(lo64));
|
||||
if (ioctl(loop_fd, LOOP_GET_STATUS64, &lo64) < 0 ||
|
||||
!(lo64.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;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user