public inbox for passt-dev@passt.top
 help / color / mirror / code / Atom feed
From: Stefano Brivio <sbrivio@redhat.com>
To: passt-dev@passt.top
Cc: David Gibson <david@gibson.dropbear.id.au>
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	[thread overview]
Message-ID: <20231107122833.1673498-3-sbrivio@redhat.com> (raw)
In-Reply-To: <20231107122833.1673498-1-sbrivio@redhat.com>

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 <sbrivio@redhat.com>
---
 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;
-- 
@@ -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


  parent reply	other threads:[~2023-11-07 12:28 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-11-07 12:28 [PATCH 0/3] Minimal fixes for issues reported by static checkers Stefano Brivio
2023-11-07 12:28 ` [PATCH 1/3] netlink: Sequence numbers are actually 32 bits wide Stefano Brivio
2023-11-07 23:32   ` David Gibson
2023-11-07 12:28 ` Stefano Brivio [this message]
2023-11-07 12:28 ` [PATCH 3/3] log: Match implicit va_start() with va_end() in vlogmsg() Stefano Brivio
2023-11-07 23:31   ` David Gibson

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20231107122833.1673498-3-sbrivio@redhat.com \
    --to=sbrivio@redhat.com \
    --cc=david@gibson.dropbear.id.au \
    --cc=passt-dev@passt.top \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://passt.top/passt

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for IMAP folder(s).