Netdev List
 help / color / mirror / Atom feed
From: Qingshuang Fu <fffsqian@163.com>
To: Antonio Quartulli <antonio@openvpn.net>,
	Sabrina Dubroca <sd@queasysnail.net>,
	"David S . Miller" <davem@davemloft.net>,
	Eric Dumazet <edumazet@google.com>,
	Jakub Kicinski <kuba@kernel.org>, Paolo Abeni <pabeni@redhat.com>,
	Simon Horman <horms@kernel.org>, Shuah Khan <shuah@kernel.org>
Cc: netdev@vger.kernel.org, linux-kselftest@vger.kernel.org,
	linux-kernel@vger.kernel.org,
	Qingshuang Fu <fuqingshuang@kylinos.cn>,
	Qingshuang Fu <fffsqian@163.com>
Subject: [PATCH] selftests/net: ovpn: fix socket leak in ovpn_socket() error paths
Date: Fri,  7 Aug 2026 14:48:16 +0800	[thread overview]
Message-ID: <20260807064816.53811-1-fffsqian@163.com> (raw)

From: Qingshuang Fu <fuqingshuang@kylinos.cn>

The ovpn_socket() function creates a socket but fails to close it on
several error paths, leading to a file descriptor leak:

1. When the address family is neither AF_INET nor AF_INET6, the socket
   is leaked in the switch default case.
2. When setsockopt() for SO_REUSEADDR, SO_REUSEPORT, or SO_MARK fails,
   the socket is leaked.
3. When setsockopt() for IPV6_V6ONLY fails, the socket is leaked.

The existing err_socket label already handles closing the socket for
the bind() failure path. Fix all other error paths to use goto
err_socket instead of returning directly, ensuring the socket is
properly closed on every error path.

Fixes: 959bc330a439 ("testing/selftests: add test tool and scripts for ovpn module")
Signed-off-by: Qingshuang Fu <fuqingshuang@kylinos.cn>
---
 tools/testing/selftests/net/ovpn/ovpn-cli.c | 15 ++++++++-------
 1 file changed, 8 insertions(+), 7 deletions(-)

diff --git a/tools/testing/selftests/net/ovpn/ovpn-cli.c b/tools/testing/selftests/net/ovpn/ovpn-cli.c
index f4effa7580c0..81b81d5fc162 100644
--- a/tools/testing/selftests/net/ovpn/ovpn-cli.c
+++ b/tools/testing/selftests/net/ovpn/ovpn-cli.c
@@ -507,7 +507,8 @@ static int ovpn_socket(struct ovpn_ctx *ctx, sa_family_t family, int proto)
 		sock_len = sizeof(*in6);
 		break;
 	default:
-		return -1;
+		ret = -EINVAL;
+		goto err_socket;
 	}
 
 	int opt = 1;
@@ -516,13 +517,13 @@ static int ovpn_socket(struct ovpn_ctx *ctx, sa_family_t family, int proto)
 
 	if (ret < 0) {
 		perror("setsockopt for SO_REUSEADDR");
-		return ret;
+		goto err_socket;
 	}
 
 	ret = setsockopt(s, SOL_SOCKET, SO_REUSEPORT, &opt, sizeof(opt));
 	if (ret < 0) {
 		perror("setsockopt for SO_REUSEPORT");
-		return ret;
+		goto err_socket;
 	}
 
 	if (ctx->mark != 0) {
@@ -530,16 +531,16 @@ static int ovpn_socket(struct ovpn_ctx *ctx, sa_family_t family, int proto)
 				 sizeof(ctx->mark));
 		if (ret < 0) {
 			perror("setsockopt for SO_MARK");
-			return ret;
+			goto err_socket;
 		}
 	}
 
 	if (family == AF_INET6) {
 		opt = 0;
-		if (setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &opt,
-			       sizeof(opt))) {
+		ret = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &opt, sizeof(opt));
+		if (ret < 0) {
 			perror("failed to set IPV6_V6ONLY");
-			return -1;
+			goto err_socket;
 		}
 	}
 

base-commit: f9a2394a23482bfd330911e9c8295b71724feacd
-- 
2.25.1


             reply	other threads:[~2026-08-07  6:49 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-08-07  6:48 Qingshuang Fu [this message]
2026-08-07  7:07 ` [PATCH] selftests/net: ovpn: fix socket leak in ovpn_socket() error paths Antonio Quartulli
2026-08-07  7:23   ` Qingshuang Fu

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=20260807064816.53811-1-fffsqian@163.com \
    --to=fffsqian@163.com \
    --cc=antonio@openvpn.net \
    --cc=davem@davemloft.net \
    --cc=edumazet@google.com \
    --cc=fuqingshuang@kylinos.cn \
    --cc=horms@kernel.org \
    --cc=kuba@kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux-kselftest@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=pabeni@redhat.com \
    --cc=sd@queasysnail.net \
    --cc=shuah@kernel.org \
    /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.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox