Fixes and workarounds for some Coverity scan reports.

This commit is contained in:
Milan Broz
2018-04-30 12:26:12 +02:00
parent 7fede3ee45
commit 2a1a773777
7 changed files with 23 additions and 11 deletions

View File

@@ -1517,11 +1517,16 @@ static int _dm_query_crypt(uint32_t get_flags,
if (get_flags & DM_ACTIVE_CRYPT_KEY) {
if (key_[0] == ':') {
key_desc = strpbrk(strpbrk(key_ + 1, ":") + 1, ":") + 1;
/* :<key_size>:<key_type>:<key_description> */
key_desc = NULL;
endp = strpbrk(key_ + 1, ":");
if (endp)
key_desc = strpbrk(endp + 1, ":");
if (!key_desc) {
r = -ENOMEM;
goto err;
}
key_desc++;
crypt_volume_key_set_description(vk, key_desc);
} else {
buffer[2] = '\0';
@@ -1723,9 +1728,10 @@ static int _dm_query_verity(uint32_t get_flags,
goto err;
}
}
if (vp)
if (vp) {
free(fec_dev_str);
fec_dev_str = str2;
else
} else
free(str2);
i++;
} else if (!strcasecmp(arg, "fec_start")) {

View File

@@ -74,9 +74,10 @@ static int hdr_checksum_calculate(const char *alg, struct luks2_hdr_disk *hdr_di
const char *json_area, size_t json_len)
{
struct crypt_hash *hd = NULL;
int r;
int hash_size, r;
if (crypt_hash_size(alg) <= 0 || crypt_hash_init(&hd, alg))
hash_size = crypt_hash_size(alg);
if (hash_size <= 0 || crypt_hash_init(&hd, alg))
return -EINVAL;
/* Binary header, csum zeroed. */
@@ -87,7 +88,7 @@ static int hdr_checksum_calculate(const char *alg, struct luks2_hdr_disk *hdr_di
r = crypt_hash_write(hd, json_area, json_len);
if (!r)
r = crypt_hash_final(hd, (char*)hdr_disk->csum, crypt_hash_size(alg));
r = crypt_hash_final(hd, (char*)hdr_disk->csum, (size_t)hash_size);
crypt_hash_destroy(hd);
return r;
@@ -100,9 +101,10 @@ static int hdr_checksum_check(const char *alg, struct luks2_hdr_disk *hdr_disk,
const char *json_area, size_t json_len)
{
struct luks2_hdr_disk hdr_tmp;
int r;
int hash_size, r;
if (crypt_hash_size(alg) <= 0)
hash_size = crypt_hash_size(alg);
if (hash_size <= 0)
return -EINVAL;
/* Copy header and zero checksum. */
@@ -116,7 +118,7 @@ static int hdr_checksum_check(const char *alg, struct luks2_hdr_disk *hdr_disk,
log_dbg_checksum(hdr_disk->csum, alg, "on-disk");
log_dbg_checksum(hdr_tmp.csum, alg, "in-memory");
if (memcmp(hdr_tmp.csum, hdr_disk->csum, crypt_hash_size(alg)))
if (memcmp(hdr_tmp.csum, hdr_disk->csum, (size_t)hash_size))
return -EINVAL;
return 0;

View File

@@ -172,6 +172,7 @@ static int FEC_process_inputs(struct crypt_device *cd,
goto out;
}
/* coverity[tainted_data] */
r = decode_rs_char(rs, rs_block);
if (r < 0) {
log_err(cd, _("Failed to repair parity for block %" PRIu64 "."), n);

View File

@@ -966,6 +966,7 @@ static int action_luksFormat(void)
return -EPERM;
log_dbg("Creating header file.");
/* coverity[toctou] */
fd = open(opt_header_device, O_CREAT|O_EXCL|O_WRONLY, S_IRUSR|S_IWUSR);
if (fd == -1 || posix_fallocate(fd, 0, 4096))
log_err(_("Cannot create header file %s."), opt_header_device);

View File

@@ -717,6 +717,7 @@ static int backup_luks_headers(struct reenc_ctx *rc)
goto out;
if ((r = stat(rc->header_file_tmp, &st)))
goto out;
/* coverity[toctou] */
if ((r = chmod(rc->header_file_tmp, st.st_mode | S_IWUSR)))
goto out;
}
@@ -898,6 +899,7 @@ static int restore_luks_header(struct reenc_ctx *rc)
goto out;
} else if ((st.st_mode & S_IFMT) == S_IFREG &&
stat(rc->header_file_new, &st) != -1) {
/* coverity[toctou] */
fd = open(rc->device_header, O_WRONLY);
if (fd != -1) {
if (posix_fallocate(fd, 0, st.st_size)) {};

View File

@@ -556,7 +556,7 @@ int main(int argc, const char **argv)
action_argc++;
/* Handle aliases */
if (!strcmp(aname, "create")) {
if (!strcmp(aname, "create") && action_argc > 1) {
/* create command had historically switched arguments */
if (action_argv[0] && action_argv[1]) {
const char *tmp = action_argv[0];

View File

@@ -515,7 +515,7 @@ int main(int argc, const char **argv)
action_argc++;
/* Handle aliases */
if (!strcmp(aname, "create")) {
if (!strcmp(aname, "create") && action_argc > 1) {
/* create command had historically switched arguments */
if (action_argv[0] && action_argv[1]) {
const char *tmp = action_argv[0];