Commit 7812214d authored by Milan Broz's avatar Milan Broz

Add context to device handling functions.

parent a5a84679
......@@ -34,12 +34,12 @@ static int INTEGRITY_read_superblock(struct crypt_device *cd,
{
int devfd, r;
devfd = device_open(device, O_RDONLY);
devfd = device_open(cd, device, O_RDONLY);
if(devfd < 0) {
return -EINVAL;
}
if (read_lseek_blockwise(devfd, device_block_size(device),
if (read_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), sb, sizeof(*sb), offset) != sizeof(*sb) ||
memcmp(sb->magic, SB_MAGIC, sizeof(sb->magic)) ||
(sb->version != SB_VERSION_1 && sb->version != SB_VERSION_2)) {
......
......@@ -90,33 +90,34 @@ int crypt_benchmark_pbkdf_internal(struct crypt_device *cd,
/* Device backend */
struct device;
int device_alloc(struct device **device, const char *path);
int device_alloc(struct crypt_device *cd, struct device **device, const char *path);
int device_alloc_no_check(struct device **device, const char *path);
void device_free(struct device *device);
void device_free(struct crypt_device *cd, struct device *device);
const char *device_path(const struct device *device);
const char *device_dm_name(const struct device *device);
const char *device_block_path(const struct device *device);
void device_topology_alignment(struct device *device,
unsigned long *required_alignment, /* bytes */
unsigned long *alignment_offset, /* bytes */
unsigned long default_alignment);
size_t device_block_size(struct device *device);
void device_topology_alignment(struct crypt_device *cd,
struct device *device,
unsigned long *required_alignment, /* bytes */
unsigned long *alignment_offset, /* bytes */
unsigned long default_alignment);
size_t device_block_size(struct crypt_device *cd, struct device *device);
int device_read_ahead(struct device *device, uint32_t *read_ahead);
int device_size(struct device *device, uint64_t *size);
int device_open(struct device *device, int flags);
int device_open(struct crypt_device *cd, struct device *device, int flags);
void device_disable_direct_io(struct device *device);
int device_is_identical(struct device *device1, struct device *device2);
int device_is_rotational(struct device *device);
size_t device_alignment(struct device *device);
int device_direct_io(const struct device *device);
int device_fallocate(struct device *device, uint64_t size);
void device_sync(struct device *device, int devfd);
void device_sync(struct crypt_device *cd, struct device *device, int devfd);
int device_open_locked(struct device *device, int flags);
int device_open_locked(struct crypt_device *cd, struct device *device, int flags);
int device_read_lock(struct crypt_device *cd, struct device *device);
int device_write_lock(struct crypt_device *cd, struct device *device);
void device_read_unlock(struct device *device);
void device_write_unlock(struct device *device);
void device_read_unlock(struct crypt_device *cd, struct device *device);
void device_write_unlock(struct crypt_device *cd, struct device *device);
enum devcheck { DEV_OK = 0, DEV_EXCL = 1, DEV_SHARED = 2 };
int device_check_access(struct crypt_device *cd,
......
......@@ -1423,7 +1423,7 @@ static int _dm_query_crypt(uint32_t get_flags,
rdevice = strsep(&params, " ");
if (get_flags & DM_ACTIVE_DEVICE) {
arg = crypt_lookup_dev(rdevice);
r = device_alloc(&data_device, arg);
r = device_alloc(NULL, &data_device, arg);
free(arg);
if (r < 0 && r != -ENOTBLK)
goto err;
......@@ -1552,7 +1552,7 @@ static int _dm_query_crypt(uint32_t get_flags,
err:
free(cipher);
free(integrity);
device_free(data_device);
device_free(NULL, data_device);
crypt_free_volume_key(vk);
return r;
}
......@@ -1594,7 +1594,7 @@ static int _dm_query_verity(uint32_t get_flags,
return -EINVAL;
if (get_flags & DM_ACTIVE_DEVICE) {
str2 = crypt_lookup_dev(str);
r = device_alloc(&data_device, str2);
r = device_alloc(NULL, &data_device, str2);
free(str2);
if (r < 0 && r != -ENOTBLK)
return r;
......@@ -1608,7 +1608,7 @@ static int _dm_query_verity(uint32_t get_flags,
goto err;
if (get_flags & DM_ACTIVE_VERITY_HASH_DEVICE) {
str2 = crypt_lookup_dev(str);
r = device_alloc(&hash_device, str2);
r = device_alloc(NULL, &hash_device, str2);
free(str2);
if (r < 0 && r != -ENOTBLK)
goto err;
......@@ -1719,7 +1719,7 @@ static int _dm_query_verity(uint32_t get_flags,
str = strsep(&params, " ");
str2 = crypt_lookup_dev(str);
if (get_flags & DM_ACTIVE_VERITY_HASH_DEVICE) {
r = device_alloc(&fec_device, str2);
r = device_alloc(NULL, &fec_device, str2);
if (r < 0 && r != -ENOTBLK) {
free(str2);
goto err;
......@@ -1779,9 +1779,9 @@ static int _dm_query_verity(uint32_t get_flags,
vp->fec_device = fec_dev_str;
return 0;
err:
device_free(data_device);
device_free(hash_device);
device_free(fec_device);
device_free(NULL, data_device);
device_free(NULL, hash_device);
device_free(NULL, fec_device);
free(root_hash);
free(hash_name);
free(salt);
......@@ -1812,7 +1812,7 @@ static int _dm_query_integrity(uint32_t get_flags,
str = strsep(&params, " ");
if (get_flags & DM_ACTIVE_DEVICE) {
str2 = crypt_lookup_dev(str);
r = device_alloc(&data_device, str2);
r = device_alloc(NULL, &data_device, str2);
free(str2);
if (r < 0 && r != -ENOTBLK)
return r;
......@@ -1946,7 +1946,7 @@ static int _dm_query_integrity(uint32_t get_flags,
dmd->u.integrity.vk = vk;
return 0;
err:
device_free(data_device);
device_free(NULL, data_device);
free(integrity);
free(journal_crypt);
free(journal_integrity);
......
......@@ -75,7 +75,7 @@ static int LUKS_endec_template(char *src, size_t srcLength,
log_dbg(ctx, "Using dmcrypt to access keyslot area.");
bsize = device_block_size(dmd.data_device);
bsize = device_block_size(ctx, dmd.data_device);
alignment = device_alignment(dmd.data_device);
if (!bsize || !alignment)
return -EINVAL;
......@@ -183,11 +183,11 @@ int LUKS_encrypt_to_storage(char *src, size_t srcLength,
r = -EIO;
/* Write buffer to device */
devfd = device_open(device, O_RDWR);
devfd = device_open(ctx, device, O_RDWR);
if (devfd < 0)
goto out;
if (write_lseek_blockwise(devfd, device_block_size(device),
if (write_lseek_blockwise(devfd, device_block_size(ctx, device),
device_alignment(device), src, srcLength,
sector * SECTOR_SIZE) < 0)
goto out;
......@@ -195,7 +195,7 @@ int LUKS_encrypt_to_storage(char *src, size_t srcLength,
r = 0;
out:
if (devfd >= 0) {
device_sync(device, devfd);
device_sync(ctx, device, devfd);
close(devfd);
}
if (r)
......@@ -240,14 +240,14 @@ int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
log_dbg(ctx, "Using userspace crypto wrapper to access keyslot area.");
/* Read buffer from device */
devfd = device_open(device, O_RDONLY);
devfd = device_open(ctx, device, O_RDONLY);
if (devfd < 0) {
log_err(ctx, _("Cannot open device %s."), device_path(device));
crypt_storage_destroy(s);
return -EIO;
}
if (read_lseek_blockwise(devfd, device_block_size(device),
if (read_lseek_blockwise(devfd, device_block_size(ctx, device),
device_alignment(device), dst, dstLength,
sector * SECTOR_SIZE) < 0) {
if (!fstat(devfd, &st) && (st.st_size < (off_t)dstLength))
......
......@@ -240,14 +240,14 @@ int LUKS_hdr_backup(const char *backup_file, struct crypt_device *ctx)
log_dbg(ctx, "Output backup file size: %zu bytes.", buffer_size);
devfd = device_open(device, O_RDONLY);
devfd = device_open(ctx, device, O_RDONLY);
if (devfd < 0) {
log_err(ctx, _("Device %s is not a valid LUKS device."), device_path(device));
r = -EINVAL;
goto out;
}
if (read_blockwise(devfd, device_block_size(device), device_alignment(device),
if (read_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
buffer, hdr_size) < (ssize_t)hdr_size) {
r = -EIO;
goto out;
......@@ -356,7 +356,7 @@ int LUKS_hdr_restore(
log_dbg(ctx, "Storing backup of header (%zu bytes) and keyslot area (%zu bytes) to device %s.",
sizeof(*hdr), buffer_size - LUKS_ALIGN_KEYSLOTS, device_path(device));
devfd = device_open(device, O_RDWR);
devfd = device_open(ctx, device, O_RDWR);
if (devfd < 0) {
if (errno == EACCES)
log_err(ctx, _("Cannot write to device %s, permission denied."),
......@@ -367,7 +367,7 @@ int LUKS_hdr_restore(
goto out;
}
if (write_blockwise(devfd, device_block_size(device), device_alignment(device),
if (write_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
buffer, buffer_size) < buffer_size) {
r = -EIO;
goto out;
......@@ -379,7 +379,7 @@ int LUKS_hdr_restore(
r = LUKS_read_phdr(hdr, 1, 0, ctx);
out:
if (devfd >= 0) {
device_sync(device, devfd);
device_sync(ctx, device, devfd);
close(devfd);
}
crypt_safe_free(buffer);
......@@ -607,13 +607,13 @@ int LUKS_read_phdr(struct luks_phdr *hdr,
log_dbg(ctx, "Reading LUKS header of size %zu from device %s",
hdr_size, device_path(device));
devfd = device_open(device, O_RDONLY);
devfd = device_open(ctx, device, O_RDONLY);
if (devfd < 0) {
log_err(ctx, _("Cannot open device %s."), device_path(device));
return -EINVAL;
}
if (read_blockwise(devfd, device_block_size(device), device_alignment(device),
if (read_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
hdr, hdr_size) < hdr_size)
r = -EIO;
else
......@@ -654,7 +654,7 @@ int LUKS_write_phdr(struct luks_phdr *hdr,
if (r)
return r;
devfd = device_open(device, O_RDWR);
devfd = device_open(ctx, device, O_RDWR);
if (devfd < 0) {
if (errno == EACCES)
log_err(ctx, _("Cannot write to device %s, permission denied."),
......@@ -679,12 +679,12 @@ int LUKS_write_phdr(struct luks_phdr *hdr,
convHdr.keyblock[i].stripes = htonl(hdr->keyblock[i].stripes);
}
r = write_blockwise(devfd, device_block_size(device), device_alignment(device),
r = write_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
&convHdr, hdr_size) < hdr_size ? -EIO : 0;
if (r)
log_err(ctx, _("Error during update of LUKS header on device %s."), device_path(device));
device_sync(device, devfd);
device_sync(ctx, device, devfd);
close(devfd);
/* Re-read header from disk to be sure that in-memory and on-disk data are the same. */
......
......@@ -238,7 +238,7 @@ static int hdr_read_disk(struct crypt_device *cd,
log_dbg(cd, "Trying to read %s LUKS2 header at offset 0x%" PRIx64 ".",
secondary ? "secondary" : "primary", offset);
devfd = device_open_locked(device, O_RDONLY);
devfd = device_open_locked(cd, device, O_RDONLY);
if (devfd < 0)
return devfd == -1 ? -EIO : devfd;
......@@ -246,7 +246,7 @@ static int hdr_read_disk(struct crypt_device *cd,
* Read binary header and run sanity check before reading
* JSON area and validating checksum.
*/
if (read_lseek_blockwise(devfd, device_block_size(device),
if (read_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), hdr_disk,
LUKS2_HDR_BIN_LEN, offset) != LUKS2_HDR_BIN_LEN) {
close(devfd);
......@@ -268,7 +268,7 @@ static int hdr_read_disk(struct crypt_device *cd,
return -ENOMEM;
}
if (read_lseek_blockwise(devfd, device_block_size(device),
if (read_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), *json_area, hdr_json_size,
offset + LUKS2_HDR_BIN_LEN) != (ssize_t)hdr_json_size) {
close(devfd);
......@@ -309,7 +309,7 @@ static int hdr_write_disk(struct crypt_device *cd,
/* FIXME: read-only device silent fail? */
devfd = device_open_locked(device, O_RDWR);
devfd = device_open_locked(cd, device, O_RDWR);
if (devfd < 0)
return devfd == -1 ? -EINVAL : devfd;
......@@ -320,7 +320,7 @@ static int hdr_write_disk(struct crypt_device *cd,
/*
* Write header without checksum but with proper seqid.
*/
if (write_lseek_blockwise(devfd, device_block_size(device),
if (write_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), (char *)&hdr_disk,
LUKS2_HDR_BIN_LEN, offset) < (ssize_t)LUKS2_HDR_BIN_LEN) {
close(devfd);
......@@ -330,7 +330,7 @@ static int hdr_write_disk(struct crypt_device *cd,
/*
* Write json area.
*/
if (write_lseek_blockwise(devfd, device_block_size(device),
if (write_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device),
CONST_CAST(char*)json_area, hdr_json_len,
LUKS2_HDR_BIN_LEN + offset) < (ssize_t)hdr_json_len) {
......@@ -349,12 +349,12 @@ static int hdr_write_disk(struct crypt_device *cd,
}
log_dbg_checksum(cd, hdr_disk.csum, hdr_disk.checksum_alg, "in-memory");
if (write_lseek_blockwise(devfd, device_block_size(device),
if (write_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), (char *)&hdr_disk,
LUKS2_HDR_BIN_LEN, offset) < (ssize_t)LUKS2_HDR_BIN_LEN)
r = -EIO;
device_sync(device, devfd);
device_sync(cd, device, devfd);
close(devfd);
return r;
}
......@@ -448,7 +448,7 @@ int LUKS2_disk_hdr_write(struct crypt_device *cd, struct luks2_hdr *hdr, struct
if (r)
log_dbg(cd, "LUKS2 header write failed (%d).", r);
device_write_unlock(device);
device_write_unlock(cd, device);
/* FIXME: try recovery here? */
......@@ -765,7 +765,7 @@ int LUKS2_hdr_version_unlocked(struct crypt_device *cd, const char *backup_file)
if (!backup_file)
device = crypt_metadata_device(cd);
else if (device_alloc(&device, backup_file) < 0)
else if (device_alloc(cd, &device, backup_file) < 0)
return 0;
if (!device)
......@@ -779,7 +779,7 @@ int LUKS2_hdr_version_unlocked(struct crypt_device *cd, const char *backup_file)
if (devfd < 0)
goto err;
if ((read_lseek_blockwise(devfd, device_block_size(device),
if ((read_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), &hdr, sizeof(hdr), 0) == sizeof(hdr)) &&
!memcmp(hdr.magic, LUKS2_MAGIC_1ST, LUKS2_MAGIC_L))
r = (int)be16_to_cpu(hdr.version);
......@@ -788,7 +788,7 @@ err:
close(devfd);
if (backup_file)
device_free(device);
device_free(cd, device);
return r;
}
......@@ -883,7 +883,7 @@ int LUKS2_hdr_read(struct crypt_device *cd, struct luks2_hdr *hdr, int repair)
r = LUKS2_disk_hdr_read(cd, hdr, crypt_metadata_device(cd), 1, !repair);
if (r == -EAGAIN) {
/* unlikely: auto-recovery is required and failed due to read lock being held */
device_read_unlock(crypt_metadata_device(cd));
device_read_unlock(cd, crypt_metadata_device(cd));
r = device_write_lock(cd, crypt_metadata_device(cd));
if (r) {
......@@ -894,9 +894,9 @@ int LUKS2_hdr_read(struct crypt_device *cd, struct luks2_hdr *hdr, int repair)
r = LUKS2_disk_hdr_read(cd, hdr, crypt_metadata_device(cd), 1, !repair);
device_write_unlock(crypt_metadata_device(cd));
device_write_unlock(cd, crypt_metadata_device(cd));
} else
device_read_unlock(crypt_metadata_device(cd));
device_read_unlock(cd, crypt_metadata_device(cd));
return r;
}
......@@ -1004,24 +1004,24 @@ int LUKS2_hdr_backup(struct crypt_device *cd, struct luks2_hdr *hdr,
return r;
}
devfd = device_open_locked(device, O_RDONLY);
devfd = device_open_locked(cd, device, O_RDONLY);
if (devfd < 0) {
device_read_unlock(device);
device_read_unlock(cd, device);
log_err(cd, _("Device %s is not a valid LUKS device."), device_path(device));
crypt_safe_free(buffer);
return devfd == -1 ? -EINVAL : devfd;
}
if (read_blockwise(devfd, device_block_size(device),
if (read_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), buffer, hdr_size) < hdr_size) {
close(devfd);
device_read_unlock(device);
device_read_unlock(cd, device);
crypt_safe_free(buffer);
return -EIO;
}
close(devfd);
device_read_unlock(device);
device_read_unlock(cd, device);
devfd = open(backup_file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR);
if (devfd == -1) {
......@@ -1064,7 +1064,7 @@ int LUKS2_hdr_restore(struct crypt_device *cd, struct luks2_hdr *hdr,
struct luks2_hdr tmp_hdr = {};
uint32_t reqs = 0;
r = device_alloc(&backup_device, backup_file);
r = device_alloc(cd, &backup_device, backup_file);
if (r < 0)
return r;
......@@ -1073,13 +1073,13 @@ int LUKS2_hdr_restore(struct crypt_device *cd, struct luks2_hdr *hdr,
if (r) {
log_err(cd, _("Failed to acquire read lock on device %s."),
device_path(backup_device));
device_free(backup_device);
device_free(cd, backup_device);
return r;
}
r = LUKS2_disk_hdr_read(cd, &hdr_file, backup_device, 0, 0);
device_read_unlock(backup_device);
device_free(backup_device);
device_read_unlock(cd, backup_device);
device_free(cd, backup_device);
if (r < 0) {
log_err(cd, _("Backup file doesn't contain valid LUKS header."));
......@@ -1170,25 +1170,25 @@ int LUKS2_hdr_restore(struct crypt_device *cd, struct luks2_hdr *hdr,
goto out;
}
devfd = device_open_locked(device, O_RDWR);
devfd = device_open_locked(cd, device, O_RDWR);
if (devfd < 0) {
if (errno == EACCES)
log_err(cd, _("Cannot write to device %s, permission denied."),
device_path(device));
else
log_err(cd, _("Cannot open device %s."), device_path(device));
device_write_unlock(device);
device_write_unlock(cd, device);
r = -EINVAL;
goto out;
}
if (write_blockwise(devfd, device_block_size(device),
if (write_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), buffer, buffer_size) < buffer_size)
r = -EIO;
else
r = 0;
device_write_unlock(device);
device_write_unlock(cd, device);
/* end of TODO */
out:
......@@ -1200,7 +1200,7 @@ out:
crypt_safe_free(buffer);
if (devfd >= 0) {
device_sync(device, devfd);
device_sync(cd, device, devfd);
close(devfd);
}
......@@ -1923,7 +1923,7 @@ int LUKS2_activate(struct crypt_device *cd,
return r;
snprintf(dm_int_dev_name, sizeof(dm_int_dev_name), "%s/%s", dm_get_dir(), dm_int_name);
r = device_alloc(&device, dm_int_dev_name);
r = device_alloc(cd, &device, dm_int_dev_name);
if (r) {
dm_remove_device(cd, dm_int_name, 0);
return r;
......@@ -1941,7 +1941,7 @@ int LUKS2_activate(struct crypt_device *cd,
&dmd.size);
if (r < 0) {
log_err(cd, "Cannot detect integrity device size.");
device_free(device);
device_free(cd, device);
dm_remove_device(cd, dm_int_name, 0);
return r;
}
......@@ -1955,7 +1955,7 @@ int LUKS2_activate(struct crypt_device *cd,
if (r < 0 && dmd.u.crypt.integrity)
dm_remove_device(cd, dm_int_name, 0);
device_free(device);
device_free(cd, device);
return r;
}
......
......@@ -435,7 +435,7 @@ int LUKS2_keyslot_wipe(struct crypt_device *cd,
device_path(device));
return r;
}
device_write_unlock(device);
device_write_unlock(cd, device);
/* secure deletion of possible key material in keyslot area */
r = crypt_keyslot_area(cd, keyslot, &area_offset, &area_length);
......
......@@ -41,7 +41,7 @@ static int luks2_encrypt_to_storage(char *src, size_t srcLength,
return r;
}
r = LUKS_encrypt_to_storage(src, srcLength, cipher, cipher_mode, vk, sector, cd);
device_write_unlock(crypt_metadata_device(cd));
device_write_unlock(cd, crypt_metadata_device(cd));
return r;
#else
struct crypt_storage *s;
......@@ -71,21 +71,21 @@ static int luks2_encrypt_to_storage(char *src, size_t srcLength,
return r;
}
devfd = device_open_locked(device, O_RDWR);
devfd = device_open_locked(cd, device, O_RDWR);
if (devfd >= 0) {
if (write_lseek_blockwise(devfd, device_block_size(device),
if (write_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), src,
srcLength, sector * SECTOR_SIZE) < 0)
r = -EIO;
else
r = 0;
device_sync(device, devfd);
device_sync(cd, device, devfd);
close(devfd);
} else
r = -EIO;
device_write_unlock(device);
device_write_unlock(cd, device);
if (r)
log_err(cd, _("IO error while encrypting keyslot."));
......@@ -106,7 +106,7 @@ static int luks2_decrypt_from_storage(char *dst, size_t dstLength,
return r;
}
r = LUKS_decrypt_from_storage(dst, dstLength, cipher, cipher_mode, vk, sector, cd);
device_read_unlock(crypt_metadata_device(cd));
device_read_unlock(cd, crypt_metadata_device(cd));
return r;
#else
struct crypt_storage *s;
......@@ -131,9 +131,9 @@ static int luks2_decrypt_from_storage(char *dst, size_t dstLength,
return r;
}
devfd = device_open_locked(device, O_RDONLY);
devfd = device_open_locked(cd, device, O_RDONLY);
if (devfd >= 0) {
if (read_lseek_blockwise(devfd, device_block_size(device),
if (read_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), dst,
dstLength, sector * SECTOR_SIZE) < 0)
r = -EIO;
......@@ -143,7 +143,7 @@ static int luks2_decrypt_from_storage(char *dst, size_t dstLength,
} else
r = -EIO;
device_read_unlock(device);
device_read_unlock(cd, device);
/* Decrypt buffer */
if (!r)
......
......@@ -433,7 +433,7 @@ static int move_keyslot_areas(struct crypt_device *cd, off_t offset_from,
if (posix_memalign(&buf, crypt_getpagesize(), buf_size))
return -ENOMEM;
devfd = device_open(device, O_RDWR);
devfd = device_open(cd, device, O_RDWR);
if (devfd == -1) {
free(buf);
return -EIO;
......@@ -444,24 +444,24 @@ static int move_keyslot_areas(struct crypt_device *cd, off_t offset_from,
log_dbg(cd, "Preallocation (fallocate) of new keyslot area not available.");
/* Try to read *new* area to check that area is there (trimmed backup). */
if (read_lseek_blockwise(devfd, device_block_size(device),
if (read_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), buf, buf_size,
offset_to)!= (ssize_t)buf_size)
goto out;
if (read_lseek_blockwise(devfd, device_block_size(device),
if (read_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), buf, buf_size,
offset_from)!= (ssize_t)buf_size)
goto out;
if (write_lseek_blockwise(devfd, device_block_size(device),
if (write_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), buf, buf_size,
offset_to) != (ssize_t)buf_size)
goto out;
r = 0;
out:
device_sync(device, devfd);
device_sync(cd, device, devfd);
close(devfd);
crypt_memzero(buf, buf_size);
free(buf);
......@@ -491,14 +491,14 @@ static int luksmeta_header_present(struct crypt_device *cd, off_t luks1_size)
if (posix_memalign(&buf, crypt_getpagesize(), sizeof(LM_MAGIC)))
return -ENOMEM;
devfd = device_open(device, O_RDONLY);
devfd = device_open(cd, device, O_RDONLY);
if (devfd == -1) {
free(buf);
return -EIO;
}
/* Note: we must not detect failure as problem here, header can be trimmed. */
if (read_lseek_blockwise(devfd, device_block_size(device), device_alignment(device),
if (read_lseek_blockwise(devfd, device_block_size(cd, device), device_alignment(device),
buf, sizeof(LM_MAGIC), luks1_size) == (ssize_t)sizeof(LM_MAGIC) &&
!memcmp(LM_MAGIC, buf, sizeof(LM_MAGIC))) {
log_err(cd, _("Unable to convert header with LUKSMETA additional metadata."));
......
......@@ -56,10 +56,8 @@ int crypt_token_register(const crypt_token_handler *handler)
}
}
if (i == LUKS2_TOKENS_MAX) {
log_dbg(NULL, "No more space for another token handler.");
if (i == LUKS2_TOKENS_MAX)
return -EINVAL;
}
token_handlers[i].h = handler;
return 0;
......
......@@ -567,7 +567,7 @@ int crypt_init(struct crypt_device **cd, const char *device)
memset(h, 0, sizeof(*h));
r = device_alloc(&h->device, device);
r = device_alloc(NULL, &h->device, device);
if (r < 0)
goto bad;
......@@ -578,7 +578,7 @@ int crypt_init(struct crypt_device **cd, const char *device)
*cd = h;
return 0;
bad:
device_free(h->device);
device_free(NULL, h->device);
free(h);
return r;
}
......@@ -623,14 +623,14 @@ int crypt_set_data_device(struct crypt_device *cd, const char *device)
if (!cd->device || !device)
return -EINVAL;
r = device_alloc(&dev, device);
r = device_alloc(cd, &dev, device);
if (r < 0)
return r;
if (!cd->metadata_device) {
cd->metadata_device = cd->device;
} else
device_free(cd->device);
device_free(cd, cd->device);
cd->device = dev;
......@@ -839,7 +839,7 @@ static int _crypt_load_verity(struct crypt_device *cd, struct crypt_params_verit
return r;
if (params && params->fec_device) {
r = device_alloc(&cd->u.verity.fec_device, params->fec_device);
r = device_alloc(cd, &cd->u.verity.fec_device, params->fec_device);
if (r < 0)
return r;
cd->u.verity.hdr.fec_area_offset = params->fec_area_offset;
......@@ -1008,7 +1008,7 @@ static void crypt_free_type(struct crypt_device *cd)
free(CONST_CAST(void*)cd->u.verity.hdr.salt);
free(cd->u.verity.root_hash);
free(cd->u.verity.uuid);
device_free(cd->u.verity.fec_device);
device_free(cd, cd->u.verity.fec_device);
} else if (isINTEGRITY(cd->type)) {
free(CONST_CAST(void*)cd->u.integrity.params.integrity);
free(CONST_CAST(void*)cd->u.integrity.params.journal_integrity);
......@@ -1050,10 +1050,10 @@ static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
if (r < 0)
goto out;
if (dmdi.target == DM_INTEGRITY && !cd->metadata_device) {
device_free(cd->device);
device_free(cd, cd->device);
cd->device = dmdi.data_device;
} else
device_free(dmdi.data_device);
device_free(cd, dmdi.data_device);
}
if (isPLAIN(cd->type)) {
......@@ -1101,7 +1101,7 @@ static int _init_by_name_crypt(struct crypt_device *cd, const char *name)
}
out:
crypt_free_volume_key(dmd.u.crypt.vk);
device_free(dmd.data_device);
device_free(cd, dmd.data_device);
free(CONST_CAST(void*)dmd.u.crypt.cipher);
free(CONST_CAST(void*)dmd.u.crypt.integrity);