* [LTP] [PATCH] setsockopt10: Bind test sockets to any free port
@ 2026-07-20 14:09 Martin Doucha
2026-07-20 15:53 ` [LTP] " linuxtestproject.agent
2026-07-20 21:36 ` [LTP] [PATCH] " Petr Vorel
0 siblings, 2 replies; 4+ messages in thread
From: Martin Doucha @ 2026-07-20 14:09 UTC (permalink / raw)
To: ltp
The setsockopt10 test currently binds two test sockets to hardcoded port
numbers. This can result in errors due to the ports being already in use.
Bind the sockets to any free ports instead.
Signed-off-by: Martin Doucha <mdoucha@suse.cz>
---
Bug reproducibility verified on affected kernel v5.14. I've also tested
that running multiple iterations of the test works correctly.
.../kernel/syscalls/setsockopt/setsockopt10.c | 34 ++++++++++++++-----
1 file changed, 25 insertions(+), 9 deletions(-)
diff --git a/testcases/kernel/syscalls/setsockopt/setsockopt10.c b/testcases/kernel/syscalls/setsockopt/setsockopt10.c
index f955f4e83..76c977d2e 100644
--- a/testcases/kernel/syscalls/setsockopt/setsockopt10.c
+++ b/testcases/kernel/syscalls/setsockopt/setsockopt10.c
@@ -77,8 +77,8 @@ static int tcp0_sk, tcp1_sk, tcp2_sk, tcp3_sk;
static void setup(void)
{
- tst_init_sockaddr_inet(&tcp0_addr, "127.0.0.1", 0x7c90);
- tst_init_sockaddr_inet(&tcp1_addr, "127.0.0.1", 0x7c91);
+ tst_init_sockaddr_inet(&tcp0_addr, "127.0.0.1", 0);
+ tst_init_sockaddr_inet(&tcp1_addr, "127.0.0.1", 0);
}
static void cleanup(void)
@@ -95,9 +95,13 @@ static void cleanup(void)
static void child(void)
{
+ struct sockaddr_storage sockaddr;
+ socklen_t addrlen;
+
+ addrlen = tst_get_connect_address(tcp1_sk, &sockaddr);
+ SAFE_CLOSE(tcp1_sk);
+
tst_res(TINFO, "child: Listen for tcp1 connection");
- tcp0_sk = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
- SAFE_BIND(tcp0_sk, (struct sockaddr *)&tcp0_addr, sizeof(tcp0_addr));
SAFE_LISTEN(tcp0_sk, 1);
TST_CHECKPOINT_WAKE(0);
@@ -110,7 +114,7 @@ static void child(void)
TST_CHECKPOINT_WAIT(2);
tst_res(TINFO, "child: connect for tcp2 connection");
- TEST(connect(tcp3_sk, (struct sockaddr *)&tcp1_addr, sizeof(tcp1_addr)));
+ TEST(connect(tcp3_sk, (struct sockaddr *)&sockaddr, addrlen));
if (TST_RET == -1) {
tst_res(TINFO | TTERRNO, "child: could not connect to tcp1");
@@ -122,18 +126,27 @@ static void child(void)
static void run(void)
{
- const pid_t child_pid = SAFE_FORK();
+ struct sockaddr_storage sockaddr;
+ socklen_t addrlen;
+ pid_t child_pid;
+
+ tcp0_sk = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
+ tcp1_sk = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
+ SAFE_BIND(tcp0_sk, (struct sockaddr *)&tcp0_addr, sizeof(tcp0_addr));
+ SAFE_BIND(tcp1_sk, (struct sockaddr *)&tcp1_addr, sizeof(tcp1_addr));
+ child_pid = SAFE_FORK();
if (child_pid == 0) {
child();
return;
}
- tcp1_sk = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
+ addrlen = tst_get_connect_address(tcp0_sk, &sockaddr);
+ SAFE_CLOSE(tcp0_sk);
TST_CHECKPOINT_WAIT(0);
tst_res(TINFO, "parent: Connect for tcp0 connection");
- SAFE_CONNECT(tcp1_sk, (struct sockaddr *)&tcp0_addr, sizeof(tcp0_addr));
+ SAFE_CONNECT(tcp1_sk, (struct sockaddr *)&sockaddr, addrlen);
TEST(setsockopt(tcp1_sk, SOL_TCP, TCP_ULP, "tls", 3));
if (TST_RET == -1 && TST_ERR == ENOENT)
@@ -145,6 +158,7 @@ static void run(void)
TST_CHECKPOINT_WAKE(1);
tst_res(TINFO, "parent: Disconnect by setting unspec address");
+ addrlen = tst_get_connect_address(tcp1_sk, &sockaddr);
TEST(connect(tcp1_sk, &unspec_addr, sizeof(unspec_addr)));
if (TST_RET == -1) {
if (TST_ERR == EOPNOTSUPP)
@@ -155,7 +169,9 @@ static void run(void)
tst_reap_children();
return;
}
- SAFE_BIND(tcp1_sk, (struct sockaddr *)&tcp1_addr, sizeof(tcp1_addr));
+
+ /* Rebind the same port as before */
+ SAFE_BIND(tcp1_sk, (struct sockaddr *)&sockaddr, addrlen);
TEST(listen(tcp1_sk, 1));
--
2.54.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply related [flat|nested] 4+ messages in thread
* Re: [LTP] setsockopt10: Bind test sockets to any free port
2026-07-20 14:09 [LTP] [PATCH] setsockopt10: Bind test sockets to any free port Martin Doucha
@ 2026-07-20 15:53 ` linuxtestproject.agent
2026-07-20 17:42 ` Andrea Cervesato via ltp
2026-07-20 21:36 ` [LTP] [PATCH] " Petr Vorel
1 sibling, 1 reply; 4+ messages in thread
From: linuxtestproject.agent @ 2026-07-20 15:53 UTC (permalink / raw)
To: Martin Doucha; +Cc: ltp
Hi Martin,
On Mon, Jul 20, 2026 at 16:09:22 +0200, Martin Doucha wrote:
> setsockopt10: Bind test sockets to any free port
> + tcp0_sk = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
> + tcp1_sk = SAFE_SOCKET(AF_INET, SOCK_STREAM, 0);
> SAFE_BIND(tcp0_sk, (struct sockaddr *)&tcp0_addr, sizeof(tcp0_addr));
> SAFE_BIND(tcp1_sk, (struct sockaddr *)&tcp1_addr, sizeof(tcp1_addr));
> - const pid_t child_pid = SAFE_FORK();
> + child_pid = SAFE_FORK();
>
> if (child_pid == 0) {
> child();
> return;
> }
>
> + addrlen = tst_get_connect_address(tcp0_sk, &sockaddr);
> + SAFE_CLOSE(tcp0_sk);
This moves tcp0_sk creation into run() before SAFE_FORK(), which means
the parent now holds a live fd in tcp0_sk when SAFE_CLOSE() is called
here.
SAFE_CLOSE() closes the fd but leaves the tcp0_sk variable unchanged.
In the original code, tcp0_sk was only assigned inside child(), in the
child process, so the parent's tcp0_sk was always 0 and cleanup() skipped
it. Now cleanup() sees tcp0_sk > 0 and calls SAFE_CLOSE() on an already
closed fd, causing EBADF and a TBROK on every test run.
Setting tcp0_sk = -1 (or 0) after the SAFE_CLOSE() here should fix it.
Verdict - Needs revision
---
Note:
The agent can sometimes produce false positives although often its
findings are genuine. If you find issues with the review, please
comment this email or ignore the suggestions.
Regards,
LTP AI Reviewer
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [LTP] setsockopt10: Bind test sockets to any free port
2026-07-20 15:53 ` [LTP] " linuxtestproject.agent
@ 2026-07-20 17:42 ` Andrea Cervesato via ltp
0 siblings, 0 replies; 4+ messages in thread
From: Andrea Cervesato via ltp @ 2026-07-20 17:42 UTC (permalink / raw)
To: linuxtestproject.agent; +Cc: ltp
> Setting tcp0_sk = -1 (or 0) after the SAFE_CLOSE() here should fix it.
SAFE_CLOSE() already set fd to -1, so we can ignore this review.
--
Andrea Cervesato
SUSE QE Automation Engineer Linux
andrea.cervesato@suse.com
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 4+ messages in thread
* Re: [LTP] [PATCH] setsockopt10: Bind test sockets to any free port
2026-07-20 14:09 [LTP] [PATCH] setsockopt10: Bind test sockets to any free port Martin Doucha
2026-07-20 15:53 ` [LTP] " linuxtestproject.agent
@ 2026-07-20 21:36 ` Petr Vorel
1 sibling, 0 replies; 4+ messages in thread
From: Petr Vorel @ 2026-07-20 21:36 UTC (permalink / raw)
To: Martin Doucha; +Cc: ltp
Hi Martin,
> The setsockopt10 test currently binds two test sockets to hardcoded port
> numbers. This can result in errors due to the ports being already in use.
> Bind the sockets to any free ports instead.
Thanks for fixing it, merged!
Kind regards,
Petr
--
Mailing list info: https://lists.linux.it/listinfo/ltp
^ permalink raw reply [flat|nested] 4+ messages in thread
end of thread, other threads:[~2026-07-20 21:36 UTC | newest]
Thread overview: 4+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-20 14:09 [LTP] [PATCH] setsockopt10: Bind test sockets to any free port Martin Doucha
2026-07-20 15:53 ` [LTP] " linuxtestproject.agent
2026-07-20 17:42 ` Andrea Cervesato via ltp
2026-07-20 21:36 ` [LTP] [PATCH] " Petr Vorel
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.