Reuse device file desriptors.

This commit is contained in:
Ondrej Kozina
2019-05-10 15:19:33 +02:00
committed by Milan Broz
parent ecbb9cfa90
commit ee57b865b0
14 changed files with 105 additions and 118 deletions

View File

@@ -35,9 +35,8 @@ static int INTEGRITY_read_superblock(struct crypt_device *cd,
int devfd, r;
devfd = device_open(cd, device, O_RDONLY);
if(devfd < 0) {
if(devfd < 0)
return -EINVAL;
}
if (read_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), sb, sizeof(*sb), offset) != sizeof(*sb) ||
@@ -55,7 +54,6 @@ static int INTEGRITY_read_superblock(struct crypt_device *cd,
r = 0;
}
close(devfd);
return r;
}

View File

@@ -145,10 +145,9 @@ int LUKS_encrypt_to_storage(char *src, size_t srcLength,
unsigned int sector,
struct crypt_device *ctx)
{
struct device *device = crypt_metadata_device(ctx);
struct crypt_storage *s;
int devfd = -1, r = 0;
int devfd, r = 0;
/* Only whole sector writes supported */
if (MISALIGNED_512(srcLength))
@@ -197,10 +196,8 @@ int LUKS_encrypt_to_storage(char *src, size_t srcLength,
r = 0;
out:
if (devfd >= 0) {
if (devfd >= 0)
device_sync(ctx, device, devfd);
close(devfd);
}
if (r)
log_err(ctx, _("IO error while encrypting keyslot."));
@@ -217,7 +214,7 @@ int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
struct device *device = crypt_metadata_device(ctx);
struct crypt_storage *s;
struct stat st;
int devfd = -1, r = 0;
int devfd, r = 0;
/* Only whole sector reads supported */
if (MISALIGNED_512(dstLength))
@@ -261,13 +258,10 @@ int LUKS_decrypt_from_storage(char *dst, size_t dstLength,
else
log_err(ctx, _("IO error while decrypting keyslot."));
close(devfd);
crypt_storage_destroy(s);
return -EIO;
}
close(devfd);
/* Decrypt buffer */
r = crypt_storage_decrypt(s, 0, dstLength, dst);
crypt_storage_destroy(s);

View File

@@ -200,9 +200,10 @@ int LUKS_hdr_backup(const char *backup_file, struct crypt_device *ctx)
{
struct device *device = crypt_metadata_device(ctx);
struct luks_phdr hdr;
int r = 0, devfd = -1;
int fd, devfd, r = 0;
size_t hdr_size;
size_t buffer_size;
ssize_t ret;
char *buffer = NULL;
r = LUKS_read_phdr(&hdr, 1, 0, ctx);
@@ -230,19 +231,18 @@ int LUKS_hdr_backup(const char *backup_file, struct crypt_device *ctx)
goto out;
}
if (read_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
buffer, hdr_size) < (ssize_t)hdr_size) {
if (read_lseek_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
buffer, hdr_size, 0) < (ssize_t)hdr_size) {
r = -EIO;
goto out;
}
close(devfd);
/* Wipe unused area, so backup cannot contain old signatures */
if (hdr.keyblock[0].keyMaterialOffset * SECTOR_SIZE == LUKS_ALIGN_KEYSLOTS)
memset(buffer + sizeof(hdr), 0, LUKS_ALIGN_KEYSLOTS - sizeof(hdr));
devfd = open(backup_file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR);
if (devfd == -1) {
fd = open(backup_file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR);
if (fd == -1) {
if (errno == EEXIST)
log_err(ctx, _("Requested header backup file %s already exists."), backup_file);
else
@@ -250,7 +250,9 @@ int LUKS_hdr_backup(const char *backup_file, struct crypt_device *ctx)
r = -EINVAL;
goto out;
}
if (write_buffer(devfd, buffer, buffer_size) < (ssize_t)buffer_size) {
ret = write_buffer(fd, buffer, buffer_size);
close(fd);
if (ret < (ssize_t)buffer_size) {
log_err(ctx, _("Cannot write header backup file %s."), backup_file);
r = -EIO;
goto out;
@@ -258,8 +260,6 @@ int LUKS_hdr_backup(const char *backup_file, struct crypt_device *ctx)
r = 0;
out:
if (devfd >= 0)
close(devfd);
crypt_memzero(&hdr, sizeof(hdr));
crypt_safe_free(buffer);
return r;
@@ -271,8 +271,8 @@ int LUKS_hdr_restore(
struct crypt_device *ctx)
{
struct device *device = crypt_metadata_device(ctx);
int r = 0, devfd = -1, diff_uuid = 0;
ssize_t buffer_size = 0;
int fd, r = 0, devfd = -1, diff_uuid = 0;
ssize_t ret, buffer_size = 0;
char *buffer = NULL, msg[200];
struct luks_phdr hdr_file;
@@ -295,20 +295,20 @@ int LUKS_hdr_restore(
goto out;
}
devfd = open(backup_file, O_RDONLY);
if (devfd == -1) {
fd = open(backup_file, O_RDONLY);
if (fd == -1) {
log_err(ctx, _("Cannot open header backup file %s."), backup_file);
r = -EINVAL;
goto out;
}
if (read_buffer(devfd, buffer, buffer_size) < buffer_size) {
ret = read_buffer(fd, buffer, buffer_size);
close(fd);
if (ret < buffer_size) {
log_err(ctx, _("Cannot read header backup file %s."), backup_file);
r = -EIO;
goto out;
}
close(devfd);
devfd = -1;
r = LUKS_read_phdr(hdr, 0, 0, ctx);
if (r == 0) {
@@ -350,21 +350,17 @@ int LUKS_hdr_restore(
goto out;
}
if (write_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
buffer, buffer_size) < buffer_size) {
if (write_lseek_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
buffer, buffer_size, 0) < buffer_size) {
r = -EIO;
goto out;
}
close(devfd);
devfd = -1;
/* Be sure to reload new data */
r = LUKS_read_phdr(hdr, 1, 0, ctx);
out:
if (devfd >= 0) {
if (devfd >= 0)
device_sync(ctx, device, devfd);
close(devfd);
}
crypt_safe_free(buffer);
return r;
}
@@ -573,9 +569,9 @@ int LUKS_read_phdr(struct luks_phdr *hdr,
int repair,
struct crypt_device *ctx)
{
int devfd, r = 0;
struct device *device = crypt_metadata_device(ctx);
ssize_t hdr_size = sizeof(struct luks_phdr);
int devfd = 0, r = 0;
/* LUKS header starts at offset 0, first keyslot on LUKS_ALIGN_KEYSLOTS */
assert(sizeof(struct luks_phdr) <= LUKS_ALIGN_KEYSLOTS);
@@ -595,8 +591,8 @@ int LUKS_read_phdr(struct luks_phdr *hdr,
return -EINVAL;
}
if (read_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
hdr, hdr_size) < hdr_size)
if (read_lseek_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
hdr, hdr_size, 0) < hdr_size)
r = -EIO;
else
r = _check_and_convert_hdr(device_path(device), hdr, require_luks_device,
@@ -615,7 +611,6 @@ int LUKS_read_phdr(struct luks_phdr *hdr,
device_disable_direct_io(device);
}
close(devfd);
return r;
}
@@ -661,13 +656,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(ctx, device), device_alignment(device),
&convHdr, hdr_size) < hdr_size ? -EIO : 0;
r = write_lseek_blockwise(devfd, device_block_size(ctx, device), device_alignment(device),
&convHdr, hdr_size, 0) < hdr_size ? -EIO : 0;
if (r)
log_err(ctx, _("Error during update of LUKS header on device %s."), device_path(device));
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. */
if (!r) {

View File

@@ -233,7 +233,7 @@ static int hdr_read_disk(struct crypt_device *cd,
char **json_area, uint64_t offset, int secondary)
{
size_t hdr_json_size = 0;
int devfd = -1, r;
int devfd, r;
log_dbg(cd, "Trying to read %s LUKS2 header at offset 0x%" PRIx64 ".",
secondary ? "secondary" : "primary", offset);
@@ -249,13 +249,11 @@ static int hdr_read_disk(struct crypt_device *cd,
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);
return -EIO;
}
r = hdr_disk_sanity_check_pre(cd, hdr_disk, &hdr_json_size, secondary, offset);
if (r < 0) {
close(devfd);
return r;
}
@@ -264,21 +262,17 @@ static int hdr_read_disk(struct crypt_device *cd,
*/
*json_area = malloc(hdr_json_size);
if (!*json_area) {
close(devfd);
return -ENOMEM;
}
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);
free(*json_area);
*json_area = NULL;
return -EIO;
}
close(devfd);
/*
* Calculate and validate checksum and zero it afterwards.
*/
@@ -302,7 +296,7 @@ static int hdr_write_disk(struct crypt_device *cd,
struct luks2_hdr_disk hdr_disk;
uint64_t offset = secondary ? hdr->hdr_size : 0;
size_t hdr_json_len;
int devfd = -1, r;
int devfd, r;
log_dbg(cd, "Trying to write LUKS2 header (%zu bytes) at offset %" PRIu64 ".",
hdr->hdr_size, offset);
@@ -323,7 +317,6 @@ static int hdr_write_disk(struct crypt_device *cd,
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);
return -EIO;
}
@@ -334,7 +327,6 @@ static int hdr_write_disk(struct crypt_device *cd,
device_alignment(device),
CONST_CAST(char*)json_area, hdr_json_len,
LUKS2_HDR_BIN_LEN + offset) < (ssize_t)hdr_json_len) {
close(devfd);
return -EIO;
}
@@ -344,7 +336,6 @@ static int hdr_write_disk(struct crypt_device *cd,
r = hdr_checksum_calculate(hdr_disk.checksum_alg, &hdr_disk,
json_area, hdr_json_len);
if (r < 0) {
close(devfd);
return r;
}
log_dbg_checksum(cd, hdr_disk.csum, hdr_disk.checksum_alg, "in-memory");
@@ -355,7 +346,6 @@ static int hdr_write_disk(struct crypt_device *cd,
r = -EIO;
device_sync(cd, device, devfd);
close(devfd);
return r;
}

View File

@@ -1034,9 +1034,9 @@ int LUKS2_hdr_backup(struct crypt_device *cd, struct luks2_hdr *hdr,
const char *backup_file)
{
struct device *device = crypt_metadata_device(cd);
int r = 0, devfd = -1;
int fd, devfd, r = 0;
ssize_t hdr_size;
ssize_t buffer_size;
ssize_t ret, buffer_size;
char *buffer = NULL;
hdr_size = LUKS2_hdr_and_areas_size(hdr->jobj);
@@ -1065,19 +1065,17 @@ int LUKS2_hdr_backup(struct crypt_device *cd, struct luks2_hdr *hdr,
return devfd == -1 ? -EINVAL : devfd;
}
if (read_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), buffer, hdr_size) < hdr_size) {
close(devfd);
if (read_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), buffer, hdr_size, 0) < hdr_size) {
device_read_unlock(cd, device);
crypt_safe_free(buffer);
return -EIO;
}
close(devfd);
device_read_unlock(cd, device);
devfd = open(backup_file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR);
if (devfd == -1) {
fd = open(backup_file, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR);
if (fd == -1) {
if (errno == EEXIST)
log_err(cd, _("Requested header backup file %s already exists."), backup_file);
else
@@ -1085,13 +1083,14 @@ int LUKS2_hdr_backup(struct crypt_device *cd, struct luks2_hdr *hdr,
crypt_safe_free(buffer);
return -EINVAL;
}
if (write_buffer(devfd, buffer, buffer_size) < buffer_size) {
ret = write_buffer(fd, buffer, buffer_size);
close(fd);
if (ret < buffer_size) {
log_err(cd, _("Cannot write header backup file %s."), backup_file);
r = -EIO;
} else
r = 0;
close(devfd);
crypt_safe_free(buffer);
return r;
}
@@ -1115,8 +1114,8 @@ int LUKS2_hdr_restore(struct crypt_device *cd, struct luks2_hdr *hdr,
const char *backup_file)
{
struct device *backup_device, *device = crypt_metadata_device(cd);
int r, devfd = -1, diff_uuid = 0;
ssize_t buffer_size = 0;
int r, fd, devfd = -1, diff_uuid = 0;
ssize_t ret, buffer_size = 0;
char *buffer = NULL, msg[1024];
struct luks2_hdr hdr_file;
struct luks2_hdr tmp_hdr = {};
@@ -1159,20 +1158,20 @@ int LUKS2_hdr_restore(struct crypt_device *cd, struct luks2_hdr *hdr,
goto out;
}
devfd = open(backup_file, O_RDONLY);
if (devfd == -1) {
fd = open(backup_file, O_RDONLY);
if (fd == -1) {
log_err(cd, _("Cannot open header backup file %s."), backup_file);
r = -EINVAL;
goto out;
}
if (read_buffer(devfd, buffer, buffer_size) < buffer_size) {
ret = read_buffer(fd, buffer, buffer_size);
close(fd);
if (ret < buffer_size) {
log_err(cd, _("Cannot read header backup file %s."), backup_file);
r = -EIO;
goto out;
}
close(devfd);
devfd = -1;
r = LUKS2_hdr_read(cd, &tmp_hdr, 0);
if (r == 0) {
@@ -1240,8 +1239,8 @@ int LUKS2_hdr_restore(struct crypt_device *cd, struct luks2_hdr *hdr,
goto out;
}
if (write_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), buffer, buffer_size) < buffer_size)
if (write_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), buffer, buffer_size, 0) < buffer_size)
r = -EIO;
else
r = 0;
@@ -1257,11 +1256,8 @@ out:
crypt_memzero(&tmp_hdr, sizeof(tmp_hdr));
crypt_safe_free(buffer);
if (devfd >= 0) {
if (devfd >= 0)
device_sync(cd, device, devfd);
close(devfd);
}
return r;
}

View File

@@ -48,7 +48,7 @@ static int luks2_encrypt_to_storage(char *src, size_t srcLength,
return r;
#else
struct crypt_storage *s;
int devfd = -1, r;
int devfd, r;
/* Only whole sector writes supported */
if (MISALIGNED_512(srcLength))
@@ -84,7 +84,6 @@ static int luks2_encrypt_to_storage(char *src, size_t srcLength,
r = 0;
device_sync(cd, device, devfd);
close(devfd);
} else
r = -EIO;
@@ -113,7 +112,7 @@ static int luks2_decrypt_from_storage(char *dst, size_t dstLength,
return r;
#else
struct crypt_storage *s;
int devfd = -1, r;
int devfd, r;
/* Only whole sector writes supported */
if (MISALIGNED_512(dstLength))
@@ -142,7 +141,6 @@ static int luks2_decrypt_from_storage(char *dst, size_t dstLength,
r = -EIO;
else
r = 0;
close(devfd);
} else
r = -EIO;

View File

@@ -134,7 +134,6 @@ static int reenc_keyslot_store_data(struct crypt_device *cd,
r = -EIO;
else
r = 0;
close(devfd);
} else
r = -EINVAL;

View File

@@ -427,9 +427,9 @@ static void move_keyslot_offset(json_object *jobj, int offset_add)
static int move_keyslot_areas(struct crypt_device *cd, off_t offset_from,
off_t offset_to, size_t buf_size)
{
int devfd, r = -EIO;
struct device *device = crypt_metadata_device(cd);
void *buf = NULL;
int r = -EIO, devfd = -1;
log_dbg(cd, "Moving keyslot areas of size %zu from %jd to %jd.",
buf_size, (intmax_t)offset_from, (intmax_t)offset_to);
@@ -438,7 +438,7 @@ static int move_keyslot_areas(struct crypt_device *cd, off_t offset_from,
return -ENOMEM;
devfd = device_open(cd, device, O_RDWR);
if (devfd == -1) {
if (devfd < 0) {
free(buf);
return -EIO;
}
@@ -466,7 +466,6 @@ static int move_keyslot_areas(struct crypt_device *cd, off_t offset_from,
r = 0;
out:
device_sync(cd, device, devfd);
close(devfd);
crypt_memzero(buf, buf_size);
free(buf);
@@ -487,16 +486,16 @@ static int luks_header_in_use(struct crypt_device *cd)
/* Check if there is a luksmeta area (foreign metadata created by the luksmeta package) */
static int luksmeta_header_present(struct crypt_device *cd, off_t luks1_size)
{
int devfd, r = 0;
static const uint8_t LM_MAGIC[] = { 'L', 'U', 'K', 'S', 'M', 'E', 'T', 'A' };
struct device *device = crypt_metadata_device(cd);
void *buf = NULL;
int devfd, r = 0;
if (posix_memalign(&buf, crypt_getpagesize(), sizeof(LM_MAGIC)))
return -ENOMEM;
devfd = device_open(cd, device, O_RDONLY);
if (devfd == -1) {
if (devfd < 0) {
free(buf);
return -EIO;
}
@@ -509,7 +508,6 @@ static int luksmeta_header_present(struct crypt_device *cd, off_t luks1_size)
r = -EBUSY;
}
close(devfd);
free(buf);
return r;
}

View File

@@ -1190,7 +1190,7 @@ int LUKS2_reenc_recover(struct crypt_device *cd,
unsigned resilience;
uint64_t area_offset, area_length, area_length_read, crash_iv_offset,
data_offset = crypt_get_data_offset(cd) << SECTOR_SHIFT;
int r, new_sector_size, old_sector_size, rseg = json_segments_segment_in_reencrypt(rh->jobj_segs_pre), fd = -1;
int devfd, r, new_sector_size, old_sector_size, rseg = json_segments_segment_in_reencrypt(rh->jobj_segs_pre);
char *checksum_tmp = NULL, *data_buffer = NULL;
struct crypt_storage_wrapper *cw1 = NULL, *cw2 = NULL;
@@ -1259,16 +1259,15 @@ int LUKS2_reenc_recover(struct crypt_device *cd,
}
/* TODO: lock for read */
fd = device_open(cd, crypt_metadata_device(cd), O_RDONLY);
if (fd < 0) {
devfd = device_open(cd, crypt_metadata_device(cd), O_RDONLY);
if (devfd < 0) {
log_err(cd, _("Failed to open mdata device."));
goto out;
}
/* read old data checksums */
read = read_lseek_blockwise(fd, device_block_size(cd, crypt_metadata_device(cd)),
read = read_lseek_blockwise(devfd, device_block_size(cd, crypt_metadata_device(cd)),
device_alignment(crypt_metadata_device(cd)), rh->rp.p.csum.checksums, area_length_read, area_offset);
close(fd);
if (read < 0 || (size_t)read != area_length_read) {
log_err(cd, _("Failed to read checksums."));
r = -EINVAL;
@@ -2305,6 +2304,7 @@ static int _reencrypt_init(struct crypt_device *cd,
log_err(cd, _("Data shift (%" PRIu64 " sectors) is less than future data offset (%" PRIu64 " sectors)."), params->data_shift, LUKS2_get_data_offset(hdr));
return -EINVAL;
}
/* FIXME: This is broken with current commit. Will get fixed with next one */
devfd = device_open_excl(cd, crypt_data_device(cd), O_RDWR);
if (devfd < 0) {
log_err(cd, _("Failed to open %s in exclusive mode (perhaps already mapped or mounted)."),
@@ -2348,9 +2348,6 @@ static int _reencrypt_init(struct crypt_device *cd,
} else
r = reencrypt_keyslot;
err:
if (devfd >= 0)
close(devfd);
if (r < 0)
crypt_load(cd, CRYPT_LUKS2, NULL);
@@ -3011,6 +3008,7 @@ int crypt_reencrypt(struct crypt_device *cd,
} else {
if (crypt_storage_wrapper_get_type(rh->cw1) != DMCRYPT &&
crypt_storage_wrapper_get_type(rh->cw2) != DMCRYPT) {
/* FIXME: This is broken with current commit. Will get fixed with next one */
excl_devfd = device_open_excl(cd, crypt_data_device(cd), O_RDONLY);
if (excl_devfd < 0) {
log_err(cd, _("Failed to open device %s in exclusive mode. Already mounted?."), device_path(crypt_data_device(cd)));
@@ -3046,8 +3044,6 @@ int crypt_reencrypt(struct crypt_device *cd,
}
r = _reencrypt_free(cd, hdr, rh, rs, progress);
if (excl_devfd >= 0)
close(excl_devfd);
return r;
}

View File

@@ -635,7 +635,7 @@ int TCRYPT_read_phdr(struct crypt_device *cd,
struct device *base_device, *device = crypt_metadata_device(cd);
ssize_t hdr_size = sizeof(struct tcrypt_phdr);
char *base_device_path;
int devfd = 0, r;
int devfd, r;
assert(sizeof(struct tcrypt_phdr) == 512);
@@ -692,11 +692,10 @@ int TCRYPT_read_phdr(struct crypt_device *cd,
device_alignment(device), hdr, hdr_size,
TCRYPT_HDR_OFFSET_BCK) == hdr_size)
r = TCRYPT_init_hdr(cd, hdr, params);
} else if (read_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), hdr, hdr_size) == hdr_size)
} else if (read_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), hdr, hdr_size, 0) == hdr_size)
r = TCRYPT_init_hdr(cd, hdr, params);
close(devfd);
if (r < 0)
memset(hdr, 0, sizeof (*hdr));
return r;

View File

@@ -46,6 +46,9 @@ struct device {
char *file_path;
int loop_fd;
int ro_dev_fd;
int dev_fd;
struct crypt_lock_handle *lh;
unsigned int o_direct:1;
@@ -256,20 +259,39 @@ void device_sync(struct crypt_device *cd, struct device *device, int devfd)
*/
static int device_open_internal(struct crypt_device *cd, struct device *device, int flags)
{
int devfd;
int access, devfd;
if (device->o_direct)
flags |= O_DIRECT;
access = flags & O_ACCMODE;
if (access == O_WRONLY)
access = O_RDWR;
if (access == O_RDONLY && device->ro_dev_fd >= 0) {
log_dbg(cd, "Reusing open r%c fd on device %s", 'o', device_path(device));
return device->ro_dev_fd;
} else if (access == O_RDWR && device->dev_fd >= 0) {
log_dbg(cd, "Reusing open r%c fd on device %s", 'w', device_path(device));
return device->dev_fd;
}
if (device_locked(device->lh))
devfd = _open_locked(cd, device, flags);
else
devfd = open(device_path(device), flags);
if (devfd < 0)
if (devfd < 0) {
log_dbg(cd, "Cannot open device %s%s.",
device_path(device),
(flags & O_ACCMODE) != O_RDONLY ? " for write" : "");
access != O_RDONLY ? " for write" : "");
return devfd;
}
if (access == O_RDONLY)
device->ro_dev_fd = devfd;
else
device->dev_fd = devfd;
return devfd;
}
@@ -320,6 +342,8 @@ int device_alloc_no_check(struct device **device, const char *path)
return -ENOMEM;
}
dev->loop_fd = -1;
dev->ro_dev_fd = -1;
dev->dev_fd = -1;
dev->o_direct = 1;
*device = dev;
@@ -357,6 +381,16 @@ void device_free(struct crypt_device *cd, struct device *device)
if (!device)
return;
if (device->ro_dev_fd != -1) {
log_dbg(cd, "Closed read only fd for %s.", device_path(device));
close(device->ro_dev_fd);
}
if (device->dev_fd != -1) {
log_dbg(cd, "Closed read write fd for %s.", device_path(device));
close(device->dev_fd);
}
if (device->loop_fd != -1) {
log_dbg(cd, "Closed loop %s (%s).", device->path, device->file_path);
close(device->loop_fd);

View File

@@ -185,7 +185,6 @@ int crypt_storage_wrapper_init(struct crypt_device *cd,
return -ENOMEM;
memset(w, 0, sizeof(*w));
w->dev_fd = -1;
w->data_offset = data_offset;
w->mem_alignment = device_alignment(device);
w->block_size = device_block_size(cd, device);
@@ -377,9 +376,6 @@ void crypt_storage_wrapper_destroy(struct crypt_storage_wrapper *cw)
dm_remove_device(NULL, cw->u.dm.name, CRYPT_DEACTIVATE_FORCE);
}
if (cw->dev_fd >= 0)
close(cw->dev_fd);
free(cw);
}

View File

@@ -139,7 +139,7 @@ int crypt_wipe_device(struct crypt_device *cd,
int (*progress)(uint64_t size, uint64_t offset, void *usrptr),
void *usrptr)
{
int r, devfd = -1;
int r, devfd;
size_t bsize, alignment;
char *sf = NULL;
uint64_t dev_size;
@@ -217,7 +217,6 @@ int crypt_wipe_device(struct crypt_device *cd,
device_sync(cd, device, devfd);
out:
close(devfd);
free(sf);
return r;
}

View File

@@ -60,7 +60,7 @@ int VERITY_read_sb(struct crypt_device *cd,
struct device *device = crypt_metadata_device(cd);
struct verity_sb sb = {};
ssize_t hdr_size = sizeof(struct verity_sb);
int devfd = 0, sb_version;
int devfd, sb_version;
log_dbg(cd, "Reading VERITY header of size %zu on device %s, offset %" PRIu64 ".",
sizeof(struct verity_sb), device_path(device), sb_offset);
@@ -84,11 +84,8 @@ int VERITY_read_sb(struct crypt_device *cd,
if (read_lseek_blockwise(devfd, device_block_size(cd, device),
device_alignment(device), &sb, hdr_size,
sb_offset) < hdr_size) {
close(devfd);
sb_offset) < hdr_size)
return -EIO;
}
close(devfd);
if (memcmp(sb.signature, VERITY_SIGNATURE, sizeof(sb.signature))) {
log_err(cd, _("Device %s is not a valid VERITY device."),
@@ -160,7 +157,7 @@ int VERITY_write_sb(struct crypt_device *cd,
ssize_t hdr_size = sizeof(struct verity_sb);
char *algorithm;
uuid_t uuid;
int r, devfd = 0;
int r, devfd;
log_dbg(cd, "Updating VERITY header of size %zu on device %s, offset %" PRIu64 ".",
sizeof(struct verity_sb), device_path(device), sb_offset);
@@ -203,7 +200,6 @@ int VERITY_write_sb(struct crypt_device *cd,
device_path(device));
device_sync(cd, device, devfd);
close(devfd);
return r;
}