Commit 683e4db4 authored by Milan Broz's avatar Milan Broz

Fix warnings reported by static analysis.

- ensure that strings are \0 terminated (most of this is already
handled on higher level anyway)

- fix resource leak in error path in tcrypt.c

- fix time of check/time of use race in sysfs path processing

- insruct Coverity scanner to ignore constant expression in random.c
(it is intented to stop compile-time misconfiguration of RNG that would be fatal)
parent c2ddd48f
...@@ -62,7 +62,7 @@ int LUKS_keyslot_area(const struct luks_phdr *hdr, ...@@ -62,7 +62,7 @@ int LUKS_keyslot_area(const struct luks_phdr *hdr,
if(keyslot >= LUKS_NUMKEYS || keyslot < 0) if(keyslot >= LUKS_NUMKEYS || keyslot < 0)
return -EINVAL; return -EINVAL;
*offset = hdr->keyblock[keyslot].keyMaterialOffset * SECTOR_SIZE; *offset = (uint64_t)hdr->keyblock[keyslot].keyMaterialOffset * SECTOR_SIZE;
*length = AF_split_sectors(hdr->keyBytes, LUKS_STRIPES) * SECTOR_SIZE; *length = AF_split_sectors(hdr->keyBytes, LUKS_STRIPES) * SECTOR_SIZE;
return 0; return 0;
...@@ -684,9 +684,9 @@ int LUKS_generate_phdr(struct luks_phdr *header, ...@@ -684,9 +684,9 @@ int LUKS_generate_phdr(struct luks_phdr *header,
/* Set Magic */ /* Set Magic */
memcpy(header->magic,luksMagic,LUKS_MAGIC_L); memcpy(header->magic,luksMagic,LUKS_MAGIC_L);
header->version=1; header->version=1;
strncpy(header->cipherName,cipherName,LUKS_CIPHERNAME_L); strncpy(header->cipherName,cipherName,LUKS_CIPHERNAME_L-1);
strncpy(header->cipherMode,cipherMode,LUKS_CIPHERMODE_L); strncpy(header->cipherMode,cipherMode,LUKS_CIPHERMODE_L-1);
strncpy(header->hashSpec,hashSpec,LUKS_HASHSPEC_L); strncpy(header->hashSpec,hashSpec,LUKS_HASHSPEC_L-1);
header->keyBytes=vk->keylength; header->keyBytes=vk->keylength;
......
...@@ -234,9 +234,11 @@ void crypt_random_exit(void) ...@@ -234,9 +234,11 @@ void crypt_random_exit(void)
int crypt_random_default_key_rng(void) int crypt_random_default_key_rng(void)
{ {
/* coverity[pointless_string_compare] */
if (!strcmp(DEFAULT_RNG, RANDOM_DEVICE)) if (!strcmp(DEFAULT_RNG, RANDOM_DEVICE))
return CRYPT_RNG_RANDOM; return CRYPT_RNG_RANDOM;
/* coverity[pointless_string_compare] */
if (!strcmp(DEFAULT_RNG, URANDOM_DEVICE)) if (!strcmp(DEFAULT_RNG, URANDOM_DEVICE))
return CRYPT_RNG_URANDOM; return CRYPT_RNG_URANDOM;
......
...@@ -811,7 +811,7 @@ out: ...@@ -811,7 +811,7 @@ out:
if (r < 0) { if (r < 0) {
crypt_free(*cd); crypt_free(*cd);
*cd = NULL; *cd = NULL;
} else if (!(*cd)->type && name) { } else if (!(*cd)->type) {
/* For anonymous device (no header found) remember initialized name */ /* For anonymous device (no header found) remember initialized name */
(*cd)->u.none.active_name = strdup(name); (*cd)->u.none.active_name = strdup(name);
} }
......
...@@ -314,12 +314,13 @@ static int TCRYPT_decrypt_hdr_one(struct tcrypt_alg *alg, const char *mode, ...@@ -314,12 +314,13 @@ static int TCRYPT_decrypt_hdr_one(struct tcrypt_alg *alg, const char *mode,
{ {
char backend_key[TCRYPT_HDR_KEY_LEN]; char backend_key[TCRYPT_HDR_KEY_LEN];
char iv[TCRYPT_HDR_IV_LEN] = {}; char iv[TCRYPT_HDR_IV_LEN] = {};
char mode_name[MAX_CIPHER_LEN]; char mode_name[MAX_CIPHER_LEN + 1];
struct crypt_cipher *cipher; struct crypt_cipher *cipher;
char *c, *buf = (char*)&hdr->e; char *c, *buf = (char*)&hdr->e;
int r; int r;
/* Remove IV if present */ /* Remove IV if present */
mode_name[MAX_CIPHER_LEN] = '\0';
strncpy(mode_name, mode, MAX_CIPHER_LEN); strncpy(mode_name, mode, MAX_CIPHER_LEN);
c = strchr(mode_name, '-'); c = strchr(mode_name, '-');
if (c) if (c)
...@@ -611,10 +612,10 @@ int TCRYPT_read_phdr(struct crypt_device *cd, ...@@ -611,10 +612,10 @@ int TCRYPT_read_phdr(struct crypt_device *cd,
return -EINVAL; return -EINVAL;
r = device_alloc(&base_device, base_device_path); r = device_alloc(&base_device, base_device_path);
free(base_device_path);
if (r < 0) if (r < 0)
return r; return r;
devfd = device_open(base_device, O_RDONLY); devfd = device_open(base_device, O_RDONLY);
free(base_device_path);
device_free(base_device); device_free(base_device);
} else } else
devfd = device_open(device, O_RDONLY); devfd = device_open(device, O_RDONLY);
...@@ -771,7 +772,8 @@ int TCRYPT_activate(struct crypt_device *cd, ...@@ -771,7 +772,8 @@ int TCRYPT_activate(struct crypt_device *cd,
for (i = algs->chain_count; i > 0; i--) { for (i = algs->chain_count; i > 0; i--) {
if (i == 1) { if (i == 1) {
strncpy(dm_name, name, sizeof(dm_name)); dm_name[sizeof(dm_name)-1] = '\0';
strncpy(dm_name, name, sizeof(dm_name)-1);
dmd.flags = flags; dmd.flags = flags;
} else { } else {
snprintf(dm_name, sizeof(dm_name), "%s_%d", name, i-1); snprintf(dm_name, sizeof(dm_name), "%s_%d", name, i-1);
...@@ -913,7 +915,7 @@ int TCRYPT_init_by_name(struct crypt_device *cd, const char *name, ...@@ -913,7 +915,7 @@ int TCRYPT_init_by_name(struct crypt_device *cd, const char *name,
struct tcrypt_phdr *tcrypt_hdr) struct tcrypt_phdr *tcrypt_hdr)
{ {
struct tcrypt_algs *algs; struct tcrypt_algs *algs;
char cipher[MAX_CIPHER_LEN * 4], mode[MAX_CIPHER_LEN], *tmp; char cipher[MAX_CIPHER_LEN * 4], mode[MAX_CIPHER_LEN+1], *tmp;
size_t key_size; size_t key_size;
int r; int r;
...@@ -927,6 +929,7 @@ int TCRYPT_init_by_name(struct crypt_device *cd, const char *name, ...@@ -927,6 +929,7 @@ int TCRYPT_init_by_name(struct crypt_device *cd, const char *name,
if (!tmp) if (!tmp)
return -EINVAL; return -EINVAL;
*tmp = '\0'; *tmp = '\0';
mode[MAX_CIPHER_LEN] = '\0';
strncpy(mode, ++tmp, MAX_CIPHER_LEN); strncpy(mode, ++tmp, MAX_CIPHER_LEN);
key_size = dmd->u.crypt.vk->keylength; key_size = dmd->u.crypt.vk->keylength;
......
...@@ -279,24 +279,30 @@ char *crypt_get_partition_device(const char *dev_path, uint64_t offset, uint64_t ...@@ -279,24 +279,30 @@ char *crypt_get_partition_device(const char *dev_path, uint64_t offset, uint64_t
major(st.st_rdev), minor(st.st_rdev)) < 0) major(st.st_rdev), minor(st.st_rdev)) < 0)
return NULL; return NULL;
dir = opendir(path);
if (!dir)
return NULL;
len = readlink(path, link, sizeof(link) - 1); len = readlink(path, link, sizeof(link) - 1);
if (len < 0) if (len < 0) {
closedir(dir);
return NULL; return NULL;
}
/* Get top level disk name for sysfs search */ /* Get top level disk name for sysfs search */
link[len] = '\0'; link[len] = '\0';
devname = strrchr(link, '/'); devname = strrchr(link, '/');
if (!devname) if (!devname) {
closedir(dir);
return NULL; return NULL;
}
devname++; devname++;
/* DM devices do not use kernel partitions. */ /* DM devices do not use kernel partitions. */
if (dm_is_dm_kernel_name(devname)) if (dm_is_dm_kernel_name(devname)) {
return NULL; closedir(dir);
dir = opendir(path);
if (!dir)
return NULL; return NULL;
}
devname_len = strlen(devname); devname_len = strlen(devname);
while((entry = readdir(dir))) { while((entry = readdir(dir))) {
......
...@@ -103,6 +103,7 @@ int crypt_loop_attach(const char *loop, const char *file, int offset, ...@@ -103,6 +103,7 @@ int crypt_loop_attach(const char *loop, const char *file, int offset,
int autoclear, int *readonly) int autoclear, int *readonly)
{ {
struct loop_info64 lo64 = {0}; struct loop_info64 lo64 = {0};
char *lo_file_name;
int loop_fd = -1, file_fd = -1, r = 1; int loop_fd = -1, file_fd = -1, r = 1;
file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL); file_fd = open(file, (*readonly ? O_RDONLY : O_RDWR) | O_EXCL);
...@@ -117,7 +118,9 @@ int crypt_loop_attach(const char *loop, const char *file, int offset, ...@@ -117,7 +118,9 @@ int crypt_loop_attach(const char *loop, const char *file, int offset,
if (loop_fd < 0) if (loop_fd < 0)
goto out; goto out;
strncpy((char*)lo64.lo_file_name, file, LO_NAME_SIZE); 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; lo64.lo_offset = offset;
if (autoclear) if (autoclear)
lo64.lo_flags |= LO_FLAGS_AUTOCLEAR; lo64.lo_flags |= LO_FLAGS_AUTOCLEAR;
......
...@@ -156,6 +156,7 @@ int VERITY_write_sb(struct crypt_device *cd, ...@@ -156,6 +156,7 @@ int VERITY_write_sb(struct crypt_device *cd,
int bsize = device_block_size(device); int bsize = device_block_size(device);
struct verity_sb sb = {}; struct verity_sb sb = {};
ssize_t hdr_size = sizeof(struct verity_sb); ssize_t hdr_size = sizeof(struct verity_sb);
char *algorithm;
uuid_t uuid; uuid_t uuid;
int r, devfd = 0; int r, devfd = 0;
...@@ -187,7 +188,9 @@ int VERITY_write_sb(struct crypt_device *cd, ...@@ -187,7 +188,9 @@ int VERITY_write_sb(struct crypt_device *cd,
sb.hash_block_size = cpu_to_le32(params->hash_block_size); sb.hash_block_size = cpu_to_le32(params->hash_block_size);
sb.salt_size = cpu_to_le16(params->salt_size); sb.salt_size = cpu_to_le16(params->salt_size);
sb.data_blocks = cpu_to_le64(params->data_size); sb.data_blocks = cpu_to_le64(params->data_size);
strncpy((char *)sb.algorithm, params->hash_name, sizeof(sb.algorithm)); algorithm = (char *)sb.algorithm;
algorithm[sizeof(sb.algorithm)-1] = '\0';
strncpy(algorithm, params->hash_name, sizeof(sb.algorithm)-1);
memcpy(sb.salt, params->salt, params->salt_size); memcpy(sb.salt, params->salt, params->salt_size);
memcpy(sb.uuid, uuid, sizeof(sb.uuid)); memcpy(sb.uuid, uuid, sizeof(sb.uuid));
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment