From mboxrd@z Thu Jan 1 00:00:00 1970 Received: by passt.top (Postfix, from userid 1000) id 0F2EA5A0274; Tue, 7 Nov 2023 13:28:33 +0100 (CET) From: Stefano Brivio To: passt-dev@passt.top Subject: [PATCH 2/3] port_fwd: Don't try to read bound ports from invalid file handles Date: Tue, 7 Nov 2023 13:28:32 +0100 Message-Id: <20231107122833.1673498-3-sbrivio@redhat.com> X-Mailer: git-send-email 2.39.2 In-Reply-To: <20231107122833.1673498-1-sbrivio@redhat.com> References: <20231107122833.1673498-1-sbrivio@redhat.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Message-ID-Hash: JKLJATJVGCC4LFIUDR2I32HMAFTW6PYH X-Message-ID-Hash: JKLJATJVGCC4LFIUDR2I32HMAFTW6PYH X-MailFrom: sbrivio@passt.top 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 CC: David Gibson 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: This is a minimal fix for what would be reported by Coverity as "Improper use of negative value" (CWE-394): port_fwd_init() doesn't guarantee that all the pre-opened file handles are actually valid. We should probably warn on failing open() and open_in_ns() in port_fwd_init(), too, but that's outside the scope of this minimal fix. Before commit 5a0485425bc9 ("port_fwd: Pre-open /proc/net/* files rather than on-demand"), we used to have a single open() call and a check after it. Fixes: 5a0485425bc9 ("port_fwd: Pre-open /proc/net/* files rather than on-demand") Signed-off-by: Stefano Brivio --- port_fwd.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/port_fwd.c b/port_fwd.c index fc4d5cb..eac8d2f 100644 --- a/port_fwd.c +++ b/port_fwd.c @@ -45,6 +45,9 @@ static void procfs_scan_listen(int fd, unsigned int lstate, unsigned int state; char *line; + if (fd < 0) + return; + if (lseek(fd, 0, SEEK_SET)) { warn("lseek() failed on /proc/net file: %s", strerror(errno)); return; -- 2.39.2