From: David Gibson <david@gibson.dropbear.id.au>
To: passt-dev@passt.top, Stefano Brivio <sbrivio@redhat.com>
Cc: David Gibson <david@gibson.dropbear.id.au>
Subject: [PATCH 2/2] tcp: Don't leak sockets on error paths
Date: Wed, 13 May 2026 17:18:21 +1000 [thread overview]
Message-ID: <20260513071821.3137329-3-david@gibson.dropbear.id.au> (raw)
In-Reply-To: <20260513071821.3137329-1-david@gibson.dropbear.id.au>
tcp_listen_handler() has several error paths that will cancel the creation
of a new flow, after having accept()ed an incoming socket connection.
Coverity pointed out that in those cases we leak the new socket. Correct
this by properly closing the socket. Make sure to also set SO_LINGER so
that the peer will get an RST.
Signed-off-by: David Gibson <david@gibson.dropbear.id.au>
---
tcp.c | 9 ++++++---
1 file changed, 6 insertions(+), 3 deletions(-)
diff --git a/tcp.c b/tcp.c
index 1078bdc3..652c68a5 100644
--- a/tcp.c
+++ b/tcp.c
@@ -2575,11 +2575,11 @@ void tcp_listen_handler(const struct ctx *c, union epoll_ref ref,
err("Invalid endpoint from TCP accept(): %s",
sockaddr_ntop(&sa, sastr, sizeof(sastr)));
- goto cancel;
+ goto rst;
}
if (!flow_target(c, flow, ref.listen.rule, IPPROTO_TCP))
- goto cancel;
+ goto rst;
switch (flow->f.pif[TGTSIDE]) {
case PIF_SPLICE:
@@ -2595,11 +2595,14 @@ void tcp_listen_handler(const struct ctx *c, union epoll_ref ref,
flow_err(flow, "No support for forwarding TCP from %s to %s",
pif_name(flow->f.pif[INISIDE]),
pif_name(flow->f.pif[TGTSIDE]));
- goto cancel;
+ goto rst;
}
return;
+rst:
+ tcp_linger0(flow, s);
+ close(s);
cancel:
flow_alloc_cancel(flow);
}
--
2.54.0
next prev parent reply other threads:[~2026-05-13 7:18 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-13 7:18 [PATCH 0/2] Fix a Coverity reported socket leak David Gibson
2026-05-13 7:18 ` [PATCH 1/2] tcp, tcp_splice: Make helper for setting SO_LINGER socket option David Gibson
2026-05-13 7:18 ` David Gibson [this message]
2026-05-13 7:23 ` [PATCH 0/2] Fix a Coverity reported socket leak Stefano Brivio
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=20260513071821.3137329-3-david@gibson.dropbear.id.au \
--to=david@gibson.dropbear.id.au \
--cc=passt-dev@passt.top \
--cc=sbrivio@redhat.com \
/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).