From 3f41b4af551e9506d4e82ea05a49fc62238d23f7 Mon Sep 17 00:00:00 2001 From: Zdenek Kabelac Date: Tue, 7 May 2024 13:24:01 +0200 Subject: [PATCH] cov: check for overlow math Add some extre protection to avoid integer overflow type of problems. --- lib/metadata/vdo_manip.c | 5 ++++- lib/mm/memlock.c | 2 +- libdm/libdm-stats.c | 3 ++- 3 files changed, 7 insertions(+), 3 deletions(-) diff --git a/lib/metadata/vdo_manip.c b/lib/metadata/vdo_manip.c index d7b63ecf0..1716e29cc 100644 --- a/lib/metadata/vdo_manip.c +++ b/lib/metadata/vdo_manip.c @@ -196,6 +196,7 @@ int parse_vdo_pool_status(struct dm_pool *mem, const struct logical_volume *vdo_ { struct dm_vdo_status_parse_result result; char *dm_name; + uint64_t blocks; status->usage = DM_PERCENT_INVALID; status->saving = DM_PERCENT_INVALID; @@ -227,7 +228,9 @@ int parse_vdo_pool_status(struct dm_pool *mem, const struct logical_volume *vdo_ result.status->total_blocks); status->saving = dm_make_percent(status->logical_blocks_used - status->data_blocks_used, status->logical_blocks_used); - status->data_usage = dm_make_percent(status->data_blocks_used * DM_VDO_BLOCK_SIZE, + /* coverity needs to use a local variable to handle check here */ + status->data_usage = dm_make_percent(((blocks = status->data_blocks_used) < (ULLONG_MAX / DM_VDO_BLOCK_SIZE)) ? + (blocks * DM_VDO_BLOCK_SIZE) : ULLONG_MAX, first_seg(vdo_pool_lv)->vdo_pool_virtual_extents * (uint64_t) vdo_pool_lv->vg->extent_size); } diff --git a/lib/mm/memlock.c b/lib/mm/memlock.c index a2f491426..735cda8eb 100644 --- a/lib/mm/memlock.c +++ b/lib/mm/memlock.c @@ -364,7 +364,7 @@ static int _memlock_maps(struct cmd_context *cmd, lvmlock_t lock, size_t *mstats for (len = 0 ; len < _maps_len; len += n) { if (!(n = read(_maps_fd, _maps_buffer + len, _maps_len - len))) break; /* EOF */ - if (n == -1) { + if ((n < 0) || (len >= (SSIZE_MAX - n))) { log_sys_debug("read", _procselfmaps); return 0; } diff --git a/libdm/libdm-stats.c b/libdm/libdm-stats.c index 796d1b2be..ad74fb881 100644 --- a/libdm/libdm-stats.c +++ b/libdm/libdm-stats.c @@ -1444,7 +1444,8 @@ static void _stats_walk_next_present(const struct dm_stats *dms, } /* advance to next present, non-skipped region or end */ - while (++(*cur_r) <= dms->max_region) { + while ((*cur_r < UINT64_MAX) && + ++(*cur_r) <= dms->max_region) { cur = &dms->regions[*cur_r]; if (!_stats_region_present(cur)) continue;