From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: passt.top; dmarc=pass (p=quarantine dis=none) header.from=redhat.com Authentication-Results: passt.top; dkim=pass (1024-bit key; unprotected) header.d=redhat.com header.i=@redhat.com header.a=rsa-sha256 header.s=mimecast20190719 header.b=EWY060Qe; dkim-atps=neutral Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by passt.top (Postfix) with ESMTPS id B24C75A026D for ; Mon, 08 Jun 2026 00:19:51 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1780870790; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:mime-version:mime-version:content-type:content-type: content-transfer-encoding:content-transfer-encoding; bh=OfTITxleFmq03O2nWIPjIOg2M7Eo5IfGPIuyuZx3FMk=; b=EWY060QeNgZwYGiQBFJO0ghc5UfM7mBnTQ95sLG5FGeP2jQnAO6JmvlVZIEwnDrILtansn scjXoamfPJEKNpS7BmqFxoNqVoH0dv5d+KJrNuQh9Qcss67Rx4pxSSNiPL+OCZqPaKOr9D pKvinp1v/dHjNab6zQT66vpH+ago92o= Received: from mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (ec2-54-186-198-63.us-west-2.compute.amazonaws.com [54.186.198.63]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.3, cipher=TLS_AES_256_GCM_SHA384) id us-mta-591-chLXUDlFMAqtWQufg1qKoQ-1; Sun, 07 Jun 2026 18:19:47 -0400 X-MC-Unique: chLXUDlFMAqtWQufg1qKoQ-1 X-Mimecast-MFC-AGG-ID: chLXUDlFMAqtWQufg1qKoQ_1780870786 Received: from mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com [10.30.177.95]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mx-prod-mc-05.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTPS id AAD97195608A; Sun, 7 Jun 2026 22:19:45 +0000 (UTC) Received: from jmaloy-thinkpadp16vgen1.rmtcaqc.csb (unknown [10.22.80.65]) by mx-prod-int-10.mail-002.prod.us-west-2.aws.redhat.com (Postfix) with ESMTP id C1024765; Sun, 7 Jun 2026 22:19:43 +0000 (UTC) From: Jon Maloy To: sbrivio@redhat.com, david@gibson.dropbear.id.au, jmaloy@redhat.com, passt-dev@passt.top Subject: [PATCH v2] util, passt: Close daemon-lifetime fds on exit to avoid Coverity warning Date: Sun, 7 Jun 2026 18:19:42 -0400 Message-ID: <20260607221942.447370-1-jmaloy@redhat.com> MIME-Version: 1.0 X-Scanned-By: MIMEDefang 3.6 on 10.30.177.95 X-Mimecast-Spam-Score: 0 X-Mimecast-MFC-PROC-ID: ukJPf5G43H2182rsqtQ742kDutOHUU1rOOfMgFGDh6o_1780870786 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: 8bit content-type: text/plain; charset="US-ASCII"; x-default=true Message-ID-Hash: J7P7XT4OGIQCFEIYDZZWJIGQORLHPRSH X-Message-ID-Hash: J7P7XT4OGIQCFEIYDZZWJIGQORLHPRSH X-MailFrom: jmaloy@redhat.com X-Mailman-Rule-Misses: dmarc-mitigation; no-senders; approved; emergency; loop; banned-address; member-moderation; nonmember-moderation; administrivia; implicit-dest; max-recipients; max-size; news-moderation; no-subject; digests; suspicious-header X-Mailman-Version: 3.3.8 Precedence: list List-Id: Development discussion and patches for passt Archived-At: Archived-At: List-Archive: List-Archive: List-Help: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: conf_open_files() opens three file descriptors (fd_tap_listen, fd_repair_listen, fd_control_listen) that are held for the entire daemon lifetime. Because no close() call exists for them anywhere, Coverity flags each as INCOMPLETE_DEALLOCATOR. This is clearly a false positive, but we still want to get rid of this warning. We now register the execution context in global area so that passt_exit() can use it to close these descriptors before calling _exit(). All exit paths (signal handler, die(), die_perror()) funnel through passt_exit(), so this covers all cases. Signed-off-by: Jon Maloy --- v2: - Made struct ctx global from the beginning, on suggestion from David Gibson. - Drop pidfile_fd from passt_exit() cleanup, since it can be closed directly after use. Signed-off-by: Jon Maloy --- passt.c | 77 +++++++++++++++++++++++++++++++-------------------------- passt.h | 2 ++ util.c | 7 ++++++ 3 files changed, 51 insertions(+), 35 deletions(-) diff --git a/passt.c b/passt.c index b3f806b9..e75b80b1 100644 --- a/passt.c +++ b/passt.c @@ -62,6 +62,13 @@ char pkt_buf[PKT_BUF_BYTES] __attribute__ ((aligned(PAGE_SIZE))); +struct ctx passt_ctx = { + .pidfile_fd = -1, + .fd_tap = -1, + .pasta_netns_fd = -1, + .device_state_fd = -1, +}; + char *epoll_type_str[] = { [EPOLL_TYPE_TCP] = "connected TCP socket", [EPOLL_TYPE_TCP_SPLICE] = "connected spliced TCP socket", @@ -322,9 +329,9 @@ static void passt_worker(void *opaque, int nfds, struct epoll_event *events) */ int main(int argc, char **argv) { + struct ctx *c = &passt_ctx; struct epoll_event events[NUM_EPOLL_EVENTS]; int nfds, devnull_fd = -1; - struct ctx c = { 0 }; struct rlimit limit; struct timespec now; struct sigaction sa; @@ -336,18 +343,15 @@ int main(int argc, char **argv) isolate_initial(argc, argv); - c.pasta_netns_fd = c.fd_tap = c.pidfile_fd = -1; - c.device_state_fd = -1; - sigemptyset(&sa.sa_mask); sa.sa_flags = 0; sa.sa_handler = exit_handler; sigaction(SIGTERM, &sa, NULL); sigaction(SIGQUIT, &sa, NULL); - c.mode = conf_mode(argc, argv); + c->mode = conf_mode(argc, argv); - if (c.mode == MODE_PASTA) { + if (c->mode == MODE_PASTA) { sa.sa_handler = pasta_child_handler; if (sigaction(SIGCHLD, &sa, NULL)) die_perror("Couldn't install signal handlers"); @@ -358,67 +362,70 @@ int main(int argc, char **argv) madvise(pkt_buf, sizeof(pkt_buf), MADV_HUGEPAGE); - c.epollfd = epoll_create1(EPOLL_CLOEXEC); - if (c.epollfd == -1) + c->epollfd = epoll_create1(EPOLL_CLOEXEC); + if (c->epollfd == -1) die_perror("Failed to create epoll file descriptor"); - flow_epollid_register(EPOLLFD_ID_DEFAULT, c.epollfd); + flow_epollid_register(EPOLLFD_ID_DEFAULT, c->epollfd); if (getrlimit(RLIMIT_NOFILE, &limit)) die_perror("Failed to get maximum value of open files limit"); - c.nofile = limit.rlim_cur = limit.rlim_max; + c->nofile = limit.rlim_cur = limit.rlim_max; if (setrlimit(RLIMIT_NOFILE, &limit)) die_perror("Failed to set current limit for open files"); - sock_probe_features(&c); + sock_probe_features(c); - conf(&c, argc, argv); - trace_init(c.trace); + conf(c, argc, argv); + trace_init(c->trace); - pasta_netns_quit_init(&c); + pasta_netns_quit_init(c); - tap_backend_init(&c); + tap_backend_init(c); - random_init(&c); + random_init(c); if (clock_gettime(CLOCK_MONOTONIC, &now)) die_perror("Failed to get CLOCK_MONOTONIC time"); flow_init(); - fwd_scan_ports_init(&c); + fwd_scan_ports_init(c); - if ((!c.no_udp && udp_init(&c)) || (!c.no_tcp && tcp_init(&c))) + if ((!c->no_udp && udp_init(c)) || (!c->no_tcp && tcp_init(c))) passt_exit(EXIT_FAILURE); - if (fwd_listen_init(&c)) + if (fwd_listen_init(c)) passt_exit(EXIT_FAILURE); - proto_update_l2_buf(c.guest_mac); + proto_update_l2_buf(c->guest_mac); - if (c.ifi4 && !c.no_dhcp) + if (c->ifi4 && !c->no_dhcp) dhcp_init(); - if (c.ifi6 && !c.no_dhcpv6) - dhcpv6_init(&c); + if (c->ifi6 && !c->no_dhcpv6) + dhcpv6_init(c); - pcap_init(&c); + pcap_init(c); - fwd_neigh_table_init(&c); - nl_neigh_notify_init(&c); + fwd_neigh_table_init(c); + nl_neigh_notify_init(c); - if (!c.foreground) { + if (!c->foreground) { if ((devnull_fd = open("/dev/null", O_RDWR | O_CLOEXEC)) < 0) die_perror("Failed to open /dev/null"); } - if (isolate_prefork(&c)) + if (isolate_prefork(c)) die("Failed to sandbox process, exiting"); - if (!c.foreground) { - __daemon(c.pidfile_fd, devnull_fd); + if (!c->foreground) { + __daemon(c->pidfile_fd, devnull_fd); + close(c->pidfile_fd); + c->pidfile_fd = -1; log_stderr = false; } else { - pidfile_write(c.pidfile_fd, getpid()); + pidfile_write(c->pidfile_fd, getpid()); + c->pidfile_fd = -1; } if (pasta_child_pid) { @@ -426,19 +433,19 @@ int main(int argc, char **argv) log_stderr = false; } - isolate_postfork(&c); + isolate_postfork(c); - timer_init(&c, &now); + timer_init(c, &now); loop: /* NOLINTBEGIN(bugprone-branch-clone): intervals can be the same */ /* cppcheck-suppress [duplicateValueTernary, unmatchedSuppression] */ - nfds = epoll_wait(c.epollfd, events, NUM_EPOLL_EVENTS, TIMER_INTERVAL); + nfds = epoll_wait(c->epollfd, events, NUM_EPOLL_EVENTS, TIMER_INTERVAL); /* NOLINTEND(bugprone-branch-clone) */ if (nfds == -1 && errno != EINTR) die_perror("epoll_wait() failed in main loop"); - passt_worker(&c, nfds, events); + passt_worker(c, nfds, events); goto loop; } diff --git a/passt.h b/passt.h index 1726965d..e1905f4c 100644 --- a/passt.h +++ b/passt.h @@ -307,6 +307,8 @@ struct ctx { bool migrate_exit; }; +extern struct ctx passt_ctx; + void proto_update_l2_buf(const unsigned char *eth_d); #endif /* PASST_H */ diff --git a/util.c b/util.c index b64c29ed..ef6ba80e 100644 --- a/util.c +++ b/util.c @@ -1108,6 +1108,13 @@ void abort_with_msg(const char *fmt, ...) */ void passt_exit(int status) { + if (passt_ctx.fd_tap_listen >= 0) + close(passt_ctx.fd_tap_listen); + if (passt_ctx.fd_repair_listen >= 0) + close(passt_ctx.fd_repair_listen); + if (passt_ctx.fd_control_listen >= 0) + close(passt_ctx.fd_control_listen); + /* Make sure we don't leave the pcap file truncated */ if (pcap_fd != -1 && fsync(pcap_fd)) warn_perror("Failed to flush pcap file, it might be truncated"); -- 2.52.0