From c995855266fa8355cf7ed8133e581360b38bad83 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 19 Mar 2025 09:38:36 +0900 Subject: [PATCH 01/10] bitfield: check if specified index is non-negative Hopefully silences false-positive warnings by Coverity e.g. CID#1590746. --- src/basic/bitfield.h | 1 + 1 file changed, 1 insertion(+) diff --git a/src/basic/bitfield.h b/src/basic/bitfield.h index 048e08d753..c6eaaa484f 100644 --- a/src/basic/bitfield.h +++ b/src/basic/bitfield.h @@ -7,6 +7,7 @@ #define _INDEX_TO_MASK(type, i, uniq) \ ({ \ int UNIQ_T(_i, uniq) = (i); \ + assert(UNIQ_T(_i, uniq) >= 0); \ assert(UNIQ_T(_i, uniq) < (int)sizeof(type) * 8); \ ((type)1) << UNIQ_T(_i, uniq); \ }) From 9dc735426a9d378a61b92c5d3795d2a3b1d7b3b3 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 19 Mar 2025 10:23:26 +0900 Subject: [PATCH 02/10] boot: always pass an array of PeSectionVector to pe_locate_sections() Hopefully silences false-positive warnings by Coverity. Fixes CID#1549198, CID#1549199, CID#1564903. --- src/boot/boot.c | 12 ++++++------ src/boot/pe.c | 21 +++++++++++---------- 2 files changed, 17 insertions(+), 16 deletions(-) diff --git a/src/boot/boot.c b/src/boot/boot.c index e7ae98021f..818614a69e 100644 --- a/src/boot/boot.c +++ b/src/boot/boot.c @@ -1814,19 +1814,19 @@ static bool is_sd_boot(EFI_FILE *root_dir, const char16_t *loader_path) { if (err != EFI_SUCCESS) return false; - PeSectionVector vector = {}; + PeSectionVector vector[1] = {}; pe_locate_profile_sections( section_table, n_section_table, section_names, /* profile= */ UINT_MAX, /* validate_base= */ 0, - &vector); - if (vector.memory_size != STRLEN(SD_MAGIC)) + vector); + if (vector[0].memory_size != STRLEN(SD_MAGIC)) return false; - err = file_handle_read(handle, vector.file_offset, vector.file_size, &content, &read); - if (err != EFI_SUCCESS || vector.file_size != read) + err = file_handle_read(handle, vector[0].file_offset, vector[0].file_size, &content, &read); + if (err != EFI_SUCCESS || vector[0].file_size != read) return false; return memcmp(content, SD_MAGIC, STRLEN(SD_MAGIC)) == 0; @@ -2093,7 +2093,7 @@ static void boot_entry_add_type2( /* and now iterate through possible profiles, and create a menu item for each profile we find */ for (unsigned profile = 0; profile < UNIFIED_PROFILES_MAX; profile ++) { - PeSectionVector sections[_SECTION_MAX]; + PeSectionVector sections[_SECTION_MAX] = {}; /* Start out with the base sections */ memcpy(sections, base_sections, sizeof(sections)); diff --git a/src/boot/pe.c b/src/boot/pe.c index 04139c7ff6..1a0feb633e 100644 --- a/src/boot/pe.c +++ b/src/boot/pe.c @@ -349,21 +349,22 @@ static void pe_locate_sections( if (!firmware_devicetree_exists()) { /* Find HWIDs table and search for the current device */ - PeSectionVector hwids_section = {}; + static const char *const hwid_section_names[] = { ".hwids", NULL }; + PeSectionVector hwids_section[1] = {}; pe_locate_sections_internal( section_table, n_section_table, - (const char *const[]) { ".hwids", NULL }, + hwid_section_names, validate_base, /* device_table */ NULL, /* device */ NULL, - &hwids_section); + hwids_section); - if (PE_SECTION_VECTOR_IS_SET(&hwids_section)) { - hwids = (const uint8_t *) SIZE_TO_PTR(validate_base) + hwids_section.memory_offset; + if (PE_SECTION_VECTOR_IS_SET(hwids_section)) { + hwids = (const uint8_t *) SIZE_TO_PTR(validate_base) + hwids_section[0].memory_offset; - EFI_STATUS err = chid_match(hwids, hwids_section.memory_size, DEVICE_TYPE_DEVICETREE, &device); + EFI_STATUS err = chid_match(hwids, hwids_section[0].memory_size, DEVICE_TYPE_DEVICETREE, &device); if (err != EFI_SUCCESS) { log_error_status(err, "HWID matching failed, no DT blob will be selected: %m"); hwids = NULL; @@ -390,15 +391,15 @@ static uint32_t get_compatibility_entry_address(const DosFileHeader *dos, const assert(pe); static const char *const section_names[] = { ".compat", NULL }; - PeSectionVector vector = {}; + PeSectionVector vector[1] = {}; pe_locate_sections( (const PeSectionHeader *) ((const uint8_t *) dos + section_table_offset(dos, pe)), pe->FileHeader.NumberOfSections, section_names, PTR_TO_SIZE(dos), - &vector); + vector); - if (!PE_SECTION_VECTOR_IS_SET(&vector)) /* not found */ + if (!PE_SECTION_VECTOR_IS_SET(vector)) /* not found */ return 0; typedef struct { @@ -408,7 +409,7 @@ static uint32_t get_compatibility_entry_address(const DosFileHeader *dos, const uint32_t entry_point; } _packed_ LinuxPeCompat1; - size_t addr = vector.memory_offset, size = vector.memory_size; + size_t addr = vector[0].memory_offset, size = vector[0].memory_size; while (size >= sizeof(LinuxPeCompat1) && addr % alignof(LinuxPeCompat1) == 0) { const LinuxPeCompat1 *compat = (const LinuxPeCompat1 *) ((const uint8_t *) dos + addr); From 6a6cc56125667fe7cb8b9ceeff1a4d21fe3a3c89 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 19 Mar 2025 10:35:17 +0900 Subject: [PATCH 03/10] bus-unit-procs: add one more assertion for Coverity Hopefully fixes CID#1562447. --- src/shared/bus-unit-procs.c | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/src/shared/bus-unit-procs.c b/src/shared/bus-unit-procs.c index ff4852150b..19139403b8 100644 --- a/src/shared/bus-unit-procs.c +++ b/src/shared/bus-unit-procs.c @@ -153,23 +153,22 @@ static int dump_processes( if (!hashmap_isempty(cg->pids)) { const char *name; - size_t n = 0, i; - pid_t *pids; void *pidp; - int width; + size_t n = 0; /* Order processes by their PID */ - pids = newa(pid_t, hashmap_size(cg->pids)); + pid_t *pids = newa(pid_t, hashmap_size(cg->pids)); HASHMAP_FOREACH_KEY(name, pidp, cg->pids) pids[n++] = PTR_TO_PID(pidp); assert(n == hashmap_size(cg->pids)); + assert(n > 0); typesafe_qsort(pids, n, pid_compare_func); - width = DECIMAL_STR_WIDTH(pids[n-1]); + int width = DECIMAL_STR_WIDTH(pids[n-1]); - for (i = 0; i < n; i++) { + for (size_t i = 0; i < n; i++) { _cleanup_free_ char *e = NULL; const char *special; bool more; From b0282ecf454e7879827f9fd4b97548566d402624 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 19 Mar 2025 10:38:53 +0900 Subject: [PATCH 04/10] journal: fix indentation --- src/journal/journald-server.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/journal/journald-server.c b/src/journal/journald-server.c index 4faaae5d77..d4368093da 100644 --- a/src/journal/journald-server.c +++ b/src/journal/journald-server.c @@ -119,7 +119,7 @@ static int server_determine_path_usage( if (fstatvfs(dirfd(d), &ss) < 0) return log_ratelimit_error_errno(errno, JOURNAL_LOG_RATELIMIT, - "Failed to fstatvfs(%s): %m", path); + "Failed to fstatvfs(%s): %m", path); *ret_free = ss.f_bsize * ss.f_bavail; *ret_used = 0; From e8784005a6fd824d20c4778691569a8eedeef4e0 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 19 Mar 2025 10:47:31 +0900 Subject: [PATCH 05/10] logind-action: use BIT_SET() and SET_BIT() Hopefully fixes CID#1529417. --- src/login/logind-action.c | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/login/logind-action.c b/src/login/logind-action.c index 2ce8f628a3..a8c90d40f8 100644 --- a/src/login/logind-action.c +++ b/src/login/logind-action.c @@ -173,11 +173,10 @@ HandleAction handle_action_sleep_select(Manager *m) { assert(m); FOREACH_ELEMENT(i, sleep_actions) { - HandleActionSleepMask action_mask = 1U << *i; const HandleActionData *a; _cleanup_free_ char *load_state = NULL; - if (!FLAGS_SET(m->handle_action_sleep_mask, action_mask)) + if (!BIT_SET(m->handle_action_sleep_mask, *i)) continue; a = ASSERT_PTR(handle_action_lookup(*i)); @@ -484,7 +483,7 @@ int config_parse_handle_action_sleep( continue; } - *mask |= 1U << a; + SET_BIT(*mask, a); } if (*mask == 0) From 11065c598c980ee856a1dc5d593e5510ef924ef9 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 19 Mar 2025 10:51:35 +0900 Subject: [PATCH 06/10] logs-show: comment out dead code Follow-up for 280e85224e6273f1835a41782f2e3abd7582dd2e. Fixes CID#1548921 and CID#1548922. --- src/shared/logs-show.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/shared/logs-show.c b/src/shared/logs-show.c index cc66503819..664bffbc71 100644 --- a/src/shared/logs-show.c +++ b/src/shared/logs-show.c @@ -458,14 +458,11 @@ static void parse_display_realtime( const char *source_monotonic, usec_t *ret) { - usec_t t, s, u; + usec_t t; assert(j); assert(ret); - // FIXME: _SOURCE_MONOTONIC_TIMESTAMP is in CLOCK_BOOTTIME, hence we cannot use it for adjusting realtime. - source_monotonic = NULL; - /* First, try _SOURCE_REALTIME_TIMESTAMP. */ if (source_realtime && safe_atou64(source_realtime, &t) >= 0 && VALID_REALTIME(t)) { *ret = t; @@ -479,11 +476,15 @@ static void parse_display_realtime( } /* If _SOURCE_MONOTONIC_TIMESTAMP is provided, adjust the header timestamp. */ + // FIXME: _SOURCE_MONOTONIC_TIMESTAMP is in CLOCK_BOOTTIME, hence we cannot use it for adjusting realtime. + /* + usec_t s, u; if (source_monotonic && safe_atou64(source_monotonic, &s) >= 0 && VALID_MONOTONIC(s) && sd_journal_get_monotonic_usec(j, &u, &(sd_id128_t) {}) >= 0) { *ret = map_clock_usec_raw(t, u, s); return; } + */ /* Otherwise, use the header timestamp as is. */ *ret = t; @@ -504,14 +505,14 @@ static void parse_display_timestamp( assert(ret_display_ts); assert(ret_boot_id); - // FIXME: _SOURCE_MONOTONIC_TIMESTAMP is in CLOCK_BOOTTIME, hence we cannot use it for adjusting realtime. - source_monotonic = NULL; - if (source_realtime && safe_atou64(source_realtime, &t) >= 0 && VALID_REALTIME(t)) source_ts.realtime = t; + // FIXME: _SOURCE_MONOTONIC_TIMESTAMP is in CLOCK_BOOTTIME, hence we cannot use it for adjusting realtime. + /* if (source_monotonic && safe_atou64(source_monotonic, &t) >= 0 && VALID_MONOTONIC(t)) source_ts.monotonic = t; + */ (void) sd_journal_get_realtime_usec(j, &header_ts.realtime); (void) sd_journal_get_monotonic_usec(j, &header_ts.monotonic, &boot_id); From 816c6ec88343b6e79be5c61ed3af1c0880775190 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 19 Mar 2025 10:59:53 +0900 Subject: [PATCH 07/10] sd-dhcp-client: use event_reset_time_relative() Fixes CID#1548073. --- src/libsystemd-network/sd-dhcp-client.c | 23 +++++++++++------------ 1 file changed, 11 insertions(+), 12 deletions(-) diff --git a/src/libsystemd-network/sd-dhcp-client.c b/src/libsystemd-network/sd-dhcp-client.c index 566da47638..9df4a3dd3f 100644 --- a/src/libsystemd-network/sd-dhcp-client.c +++ b/src/libsystemd-network/sd-dhcp-client.c @@ -740,10 +740,9 @@ static void client_stop(sd_dhcp_client *client, int error) { * for retransmission delays, timeout should start at 4s then double * each attempt with max of 64s, with -1 to +1 sec of random 'fuzz' added. * This assumes the first call will be using attempt 1. */ -static usec_t client_compute_request_timeout(usec_t now, uint64_t attempt) { +static usec_t client_compute_request_timeout(uint64_t attempt) { usec_t timeout = (UINT64_C(1) << MIN(attempt + 1, UINT64_C(6))) * USEC_PER_SEC; - - return usec_sub_signed(usec_add(now, timeout), RFC2131_RANDOM_FUZZ); + return usec_sub_signed(timeout, RFC2131_RANDOM_FUZZ); } /* RFC2131 section 4.4.5: @@ -758,8 +757,8 @@ static usec_t client_compute_request_timeout(usec_t now, uint64_t attempt) { * of 60 seconds. * Note that while the default T1/T2 initial times do have random 'fuzz' applied, * the RFC sec 4.4.5 does not mention adding any fuzz to retries. */ -static usec_t client_compute_reacquisition_timeout(usec_t now, usec_t expire) { - return now + MAX(usec_sub_unsigned(expire, now) / 2, 60 * USEC_PER_SEC); +static usec_t client_compute_reacquisition_timeout(usec_t now_usec, usec_t expire) { + return MAX(usec_sub_unsigned(expire, now_usec) / 2, 60 * USEC_PER_SEC); } static int cmp_uint8(const uint8_t *a, const uint8_t *b) { @@ -1242,7 +1241,7 @@ static int client_timeout_resend( goto error; client->discover_attempt++; - next_timeout = client_compute_request_timeout(time_now, client->discover_attempt); + next_timeout = client_compute_request_timeout(client->discover_attempt); break; case DHCP_STATE_REQUESTING: case DHCP_STATE_BOUND: @@ -1250,7 +1249,7 @@ static int client_timeout_resend( goto error; client->request_attempt++; - next_timeout = client_compute_request_timeout(time_now, client->request_attempt); + next_timeout = client_compute_request_timeout(client->request_attempt); break; case DHCP_STATE_STOPPED: @@ -1261,11 +1260,11 @@ static int client_timeout_resend( assert_not_reached(); } - r = event_reset_time(client->event, &client->timeout_resend, - CLOCK_BOOTTIME, - next_timeout, 10 * USEC_PER_MSEC, - client_timeout_resend, client, - client->event_priority, "dhcp4-resend-timer", true); + r = event_reset_time_relative( + client->event, &client->timeout_resend, + CLOCK_BOOTTIME, next_timeout, 10 * USEC_PER_MSEC, + client_timeout_resend, client, + client->event_priority, "dhcp4-resend-timer", true); if (r < 0) goto error; From 91c4d1affdba02a323dc2c7caccabe240ccb8302 Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 19 Mar 2025 11:12:33 +0900 Subject: [PATCH 08/10] nspawn-oci: update overflow check Fixes CID#1548072. --- src/nspawn/nspawn-oci.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/nspawn/nspawn-oci.c b/src/nspawn/nspawn-oci.c index ecbcaefcbb..923e20b5c0 100644 --- a/src/nspawn/nspawn-oci.c +++ b/src/nspawn/nspawn-oci.c @@ -700,8 +700,8 @@ static int oci_uid_gid_mappings(const char *name, sd_json_variant *v, sd_json_di if (r < 0) return r; - if (data.host_id + data.range < data.host_id || - data.container_id + data.range < data.container_id) + if (data.range > UINT32_MAX - data.host_id || + data.range > UINT32_MAX - data.container_id) return json_log(v, flags, SYNTHETIC_ERRNO(EINVAL), "UID/GID range goes beyond UID/GID validity range, refusing."); From 4b4181f4acecb7ead9814d5430bb2010a2d1290e Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 19 Mar 2025 11:21:46 +0900 Subject: [PATCH 09/10] io-util: add one more assertion for Coverity Fixes CID#1548067. --- src/basic/io-util.c | 1 + 1 file changed, 1 insertion(+) diff --git a/src/basic/io-util.c b/src/basic/io-util.c index f12f6b192c..7d7f34f203 100644 --- a/src/basic/io-util.c +++ b/src/basic/io-util.c @@ -86,6 +86,7 @@ ssize_t loop_read(int fd, void *buf, size_t nbytes, bool do_poll) { return n; assert((size_t) k <= nbytes); + assert(k <= SSIZE_MAX - n); p += k; nbytes -= k; From 707bea6a48646496c9de74054e9405fb334fb20b Mon Sep 17 00:00:00 2001 From: Yu Watanabe Date: Wed, 19 Mar 2025 11:30:36 +0900 Subject: [PATCH 10/10] ptyfwd: use usec_add() Fixes CID#1548038. --- src/shared/ptyfwd.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/shared/ptyfwd.c b/src/shared/ptyfwd.c index d434f8e850..affa3ba37c 100644 --- a/src/shared/ptyfwd.c +++ b/src/shared/ptyfwd.c @@ -222,11 +222,11 @@ static RequestOperation look_for_escape(PTYForward *f, const char *buffer, size_ case 0x1D: { /* Check for ^] */ usec_t nw = now(CLOCK_MONOTONIC); - if (f->escape_counter == 0 || nw > f->escape_timestamp + ESCAPE_USEC) { + if (f->escape_counter == 0 || nw > usec_add(f->escape_timestamp, ESCAPE_USEC)) { f->escape_timestamp = nw; f->escape_counter = 1; } else { - (f->escape_counter)++; + f->escape_counter++; if (f->escape_counter >= 3) return REQUEST_EXIT; @@ -237,7 +237,7 @@ static RequestOperation look_for_escape(PTYForward *f, const char *buffer, size_ case 'a'...'z': if (f->escape_counter == 2 && - now(CLOCK_MONOTONIC) <= f->escape_timestamp + ESCAPE_USEC) { + now(CLOCK_MONOTONIC) <= usec_add(f->escape_timestamp, ESCAPE_USEC)) { f->escape_timestamp = 0; f->escape_counter = 0; return REQUEST_HOTKEY_BASE + *p;