vsock/test: Add test for null ptr deref when transport changes
Subsystems (4)
PATCH v4 1/1 vsock/test: Add test for null ptr deref when transport changes
Add a new test to ensure that when the transport changes a null pointer dereference does not occur. The bug was reported upstream [1] and fixed with commit 2cb7c756f605 ("vsock/virtio: discard packets if the transport changes").
KASAN: null-ptr-deref in range [0x0000000000000060-0x0000000000000067] CPU: 2 UID: 0 PID: 463 Comm: kworker/2:3 Not tainted Workqueue: vsock-loopback vsock_loopback_work RIP: 0010:vsock_stream_has_data+0x44/0x70 Call Trace: virtio_transport_do_close+0x68/0x1a0 virtio_transport_recv_pkt+0x1045/0x2ae4 vsock_loopback_work+0x27d/0x3f0 process_one_work+0x846/0x1420 worker_thread+0x5b3/0xf80 kthread+0x35a/0x700 ret_from_fork+0x2d/0x70 ret_from_fork_asm+0x1a/0x30
Note that this test may not fail in a kernel without the fix, but it may hang on the client side if it triggers a kernel oops.
This works by creating a socket, trying to connect to a server, and then executing a second connect operation on the same socket but to a different CID (0). This triggers a transport change. If the connect operation is interrupted by a signal, this could cause a null-ptr-deref.
Since this bug is non-deterministic, we need to try several times. It is reasonable to assume that the bug will show up within the timeout period.
If there is a G2H transport loaded in the system, the bug is not triggered and this test will always pass.
[1]https://lore.kernel.org/netdev/Z2LvdTTQR7dBmPb5@v4bel-B760M-AORUS-ELITE-AX/
---
This series introduces a new test that checks for a null pointer dereference that may happen when there is a transport change[1]. This bug was fixed in [2].
Note that this test *cannot* fail, it hangs if it triggers a kernel oops. The intended use-case is to run it and then check if there is any oops in the dmesg.
This test is based on Hyunwoo Kim's[3] and Michal's python reproducers[4].
[1]https://lore.kernel.org/netdev/Z2LvdTTQR7dBmPb5@v4bel-B760M-AORUS-ELITE-AX/ [2]https://lore.kernel.org/netdev/[email protected]/ [3]https://lore.kernel.org/netdev/Z2LvdTTQR7dBmPb5@v4bel-B760M-AORUS-ELITE-AX/#t [4]https://lore.kernel.org/netdev/[email protected]/
---
Changes in v4:
- Addressed Stefano's comments:
- Minor style changes
- Use get_transports() to print a warning when a G2H transport is loaded
- Removed check on second connect: Because the first connect is interrupted, the socket is in an unspecified state (see man connect) . This can cause strange and unexpected behaviors (connect returning success on a non-existing CID).
- Link to v3:
https://lore.kernel.org/r/[email protected]
Sorry, this took waaay longer than expected.
Changes in v3:
Addressed Stefano's and Michal's comments:
- Added the splat text to the commit commessage.
- Introduced commit hash that fixes the bug.
- Not using perror anymore on pthread_* functions.
- Listener is just created once.
- Link to v2:
https://lore.kernel.org/r/[email protected]
Changes in v2:
- Addressed Stefano's comments:
- Timeout is now using current_nsec()
- Check for return values
- Style issues
- Added Hyunwoo Kim to Suggested-by
- Link to v1:
https://lore.kernel.org/r/[email protected]
--- tools/testing/vsock/Makefile | 1 + tools/testing/vsock/vsock_test.c | 178 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 179 insertions(+)
tools/testing/vsock/Makefile +1 -0
@@ -5,6 +5,7 @@ vsock_test: vsock_test.o vsock_test_zerocopy.o timeout.o control.o util.o msg_ze vsock_diag_test: vsock_diag_test.o timeout.o control.o util.o vsock_perf: vsock_perf.o msg_zerocopy_common.o +vsock_test: LDLIBS = -lpthread vsock_uring_test: LDLIBS = -luring vsock_uring_test: control.o util.o vsock_uring_test.o timeout.o msg_zerocopy_common.o
tools/testing/vsock/vsock_test.c +178 -0
196 lines not on this page · raw message
---
base-commit: 68d019aa14d97f8d57b0f8d203fd3b44db2ba0c7
change-id: 20250306-test_vsock-3e77a9c7a245
Best regards,
Signature · 1 line
Luigi Leonardi <[email protected]>
Stefano Garzarella
maintainerQuoted 27 lines · Luigi Leonardi
Add a new test to ensure that when the transport changes a null pointer dereference does not occur. The bug was reported upstream [1] and fixed with commit
2cb7c756f605("vsock/virtio: discard packets if the transport changes").KASAN: null-ptr-deref in range [0x0000000000000060-0x0000000000000067] CPU: 2 UID: 0 PID: 463 Comm: kworker/2:3 Not tainted Workqueue: vsock-loopback vsock_loopback_work RIP: 0010:vsock_stream_has_data+0x44/0x70 Call Trace: virtio_transport_do_close+0x68/0x1a0 virtio_transport_recv_pkt+0x1045/0x2ae4 vsock_loopback_work+0x27d/0x3f0 process_one_work+0x846/0x1420 worker_thread+0x5b3/0xf80 kthread+0x35a/0x700 ret_from_fork+0x2d/0x70 ret_from_fork_asm+0x1a/0x30Note that this test may not fail in a kernel without the fix, but it may hang on the client side if it triggers a kernel oops.
This works by creating a socket, trying to connect to a server, and then executing a second connect operation on the same socket but to a different CID (0). This triggers a transport change. If the connect operation is interrupted by a signal, this could cause a null-ptr-deref.
Quoted 6 lines
Since this bug is non-deterministic, we need to try several times. It is reasonable to assume that the bug will show up within the timeout period.
If there is a G2H transport loaded in the system, the bug is not triggered and this test will always pass.
Can you add the reason?
Quoted 117 lines
[1]https://lore.kernel.org/netdev/Z2LvdTTQR7dBmPb5@v4bel-B760M-AORUS-ELITE-AX/
Suggested-by Hyunwoo Kim <[email protected]>Suggested-by Michal Luczaj <[email protected]>Signed-off-by Luigi Leonardi <[email protected]>---
This series introduces a new test that checks for a null pointer dereference that may happen when there is a transport change[1]. This bug was fixed in [2].Note that this test *cannot* fail, it hangs if it triggers a kernel oops. The intended use-case is to run it and then check if there is any oops in the dmesg.
This test is based on Hyunwoo Kim's[3] and Michal's python reproducers[4].
[1]https://lore.kernel.org/netdev/Z2LvdTTQR7dBmPb5@v4bel-B760M-AORUS-ELITE-AX/ [2]https://lore.kernel.org/netdev/[email protected]/ [3]https://lore.kernel.org/netdev/Z2LvdTTQR7dBmPb5@v4bel-B760M-AORUS-ELITE-AX/#t [4]https://lore.kernel.org/netdev/[email protected]/
---
Changes in v4:
- Addressed Stefano's comments:
- Minor style changes
- Useget_transports()to print a warning when a G2H transport is loaded
- Removed check on second connect: Because the first connect is interrupted, the socket is in an unspecified state (see man connect) . This can cause strange and unexpected behaviors (connect returning success on a non-existing CID).- Link to v3:
https://lore.kernel.org/r/[email protected]Sorry, this took waaay longer than expected.
Changes in v3:
Addressed Stefano's and Michal's comments:
- Added the splat text to the commit commessage.
- Introduced commit hash that fixes the bug.
- Not using perror anymore on pthread_* functions.
- Listener is just created once.- Link to v2:
https://lore.kernel.org/r/[email protected]Changes in v2:
- Addressed Stefano's comments:
- Timeout is now usingcurrent_nsec()
- Check for return values
- Style issues
- Added Hyunwoo Kim to Suggested-by
- Link to v1:
https://lore.kernel.org/r/[email protected]
---tools/testing/vsock/Makefile| 1 +tools/testing/vsock/vsock_test.c| 178 +++++++++++++++++++++++++++++++++++++++2 files changed, 179 insertions(+)tools/testing/vsock/Makefile +0
-0@@ -5,6 +5,7 @@ vsock_test: vsock_test.o vsock_test_zerocopy.o timeout.o control.o util.o msg_ze
vsock_diag_test: vsock_diag_test.o timeout.o control.o util.ovsock_perf: vsock_perf.o msg_zerocopy_common.o+vsock_test: LDLIBS = -lpthread
vsock_uring_test: LDLIBS = -luringvsock_uring_test: control.o util.o vsock_uring_test.o timeout.o msg_zerocopy_common.otools/testing/vsock/vsock_test.c +29
-0@@ -22,6 +22,8 @@#include <signal.h> #include <sys/ioctl.h> #include <linux/time64.h> +#include <pthread.h> +#include <fcntl.h> #include "vsock_test_zerocopy.h" #include "timeout.h" @@ -1867,6 +1869,177 @@ static void test_stream_connect_retry_server(const struct test_opts *opts)close (fd); } +#define TRANSPORT_CHANGE_TIMEOUT 2/* seconds */ + +static void *test_stream_transport_change_thread (void *vargp) +{ +pid_t *pid = (pid_t *)vargp; +int ret; + +/* We want this thread to terminate as soon as possible */ + ret =pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS ,NULL ); +if (ret) { +fprintf (stderr,"pthread_setcanceltype: %d\n" , ret); +exit (EXIT_FAILURE ); + } + +while (true) { +if (kill (*pid,SIGUSR1 ) <0 ) { +perror ("kill" ); +exit (EXIT_FAILURE ); + } + } +return NULL ; +} + +static void test_transport_change_signal_handler (int signal) +{ +/* We need a custom handler for SIGUSR1 as the default one terminates the process. */ +}
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1908
Quoted 10 lines
tools/testing/vsock/vsock_test.c +10
-0+ +static void test_stream_transport_change_client (const struct test_opts *opts) +{ +__sighandler_t old_handler; +pid_t pid =getpid (); +pthread_t thread_id; +time_t tout; +int ret, tr; + + tr =get_transports ();
nit: add a blank line here
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1912
Quoted 4 lines
tools/testing/vsock/vsock_test.c +4
-0+/* Print a warning if there is a G2H transport loaded. +* This is on a best effort basis because VMCI can be either G2H and H2G, and there is +* no easy way to understand it. +* The bug is present in the loopback transport. However, it does not interfere
nit: s/is/was
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1913
Quoted 1 line
tools/testing/vsock/vsock_test.c +1
-0+* if it is loaded.
I don't understand this, if the bug was present in the loopback transport, our goal is to stress it, so have it loaded is great, why it should interfere?
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1914
Quoted 1 line
tools/testing/vsock/vsock_test.c +1
-0+* The bug we are testing only appears when G2H transports are not loaded.
Please add the reason also here.
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1916
Quoted 2 lines
tools/testing/vsock/vsock_test.c +2
-0+*/ +
nit: remove the blank line here
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1918
Quoted 2 lines
tools/testing/vsock/vsock_test.c +2
-0+ tr &= ~TRANSPORT_LOOPBACK ; +if (tr !=0 && tr !=TRANSPORT_VHOST )
Sorry, this is really hard to understand IMO, let's do a step back. Your goal is to check if there is a G2H transport loaded, right?
I think we have 2 options:
1. similar to your, just masking the other 2 transports
if (tr & ~(TRANSPORT_LOOPBACK | TRANSPORT_VHOST))
IMO this is much clear to understand, and should have the same effect.
2. (my preference) define in util.h some macros that we can reuse:
#define TRANSPORTS_G2H (TRANSPORT_VIRTIO | TRANSPORT_VMCI | TRANSPORT_HYPERV)
#define TRANSPORTS_H2G (TRANSPORT_VHOST | TRANSPORT_VMCI)
#define TRANSPORTS_LOCAL (TRANSPORT_LOOPBACK)
and here you can just do:
if (tr & TRANSPORTS_G2H)Quoted 23 lines
tools/testing/vsock/vsock_test.c +23
-0+fprintf (stderr,"G2H Transport detected. This test will not fail.\n" ); + + old_handler =signal (SIGUSR1 , test_transport_change_signal_handler); +if (old_handler ==SIG_ERR ) { +perror ("signal" ); +exit (EXIT_FAILURE ); + } + + ret =pthread_create (&thread_id,NULL , test_stream_transport_change_thread, &pid); +if (ret) { +fprintf (stderr,"pthread_create: %d\n" , ret); +exit (EXIT_FAILURE ); + } + +control_expectln ("LISTENING" ); + + tout =current_nsec () +TRANSPORT_CHANGE_TIMEOUT *NSEC_PER_SEC ; + do { + struct sockaddr_vm sa = { + .svm_family = AF_VSOCK, + .svm_cid = opts->peer_cid, + .svm_port = opts->peer_port, + };
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1951
Quoted 10 lines
tools/testing/vsock/vsock_test.c +10
-0+int s; + + s =socket (AF_VSOCK ,SOCK_STREAM ,0 ); +if (s <0 ) { +perror ("socket" ); +exit (EXIT_FAILURE ); + } + + ret =connect (s, (struct sockaddr *)&sa,sizeof (sa)); +/* The connect can fail due to signals coming from the thread.
. should be ,
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1963
Quoted 13 lines
tools/testing/vsock/vsock_test.c +3
-0+* or because the receiver connection queue is full. +* Ignoring also the latter case because there is no way +* of synchronizing client's connect and server's accept when
tools/testing/vsock/vsock_test.c +9
-0+* connect(s) are constantly being interrupted by signals. +*/ +if (ret ==-1 && (errno !=EINTR && errno !=ECONNRESET )) { +perror ("connect" ); +exit (EXIT_FAILURE ); + } + +/* Set CID to 0 cause a transport change. */ + sa.svm_cid =0 ;
nit: add a blank line
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1969
Quoted 6 lines
tools/testing/vsock/vsock_test.c +6
-0+/* Here we ignore the connect return value because we cannot +* safely assume that it will *always* fail. +* This is because the previous connect was interrupted +* during the connection process. The socket state, as stated +* in `man connect`, is unspecified and can result in strange +* behaviors.
Let's focus on the behaviour and try to be more concise. Something like this:
/* Ignore return value since it can fail or not.
* If the previous connect is interrupted while the
* connection request is already sent, the second
* connect() will wait for the response.
Quoted 8 lines
tools/testing/vsock/vsock_test.c +8
-0+*/ +connect (s, (struct sockaddr *)&sa,sizeof (sa)); + +close (s); + +control_writeulong (CONTROL_CONTINUE ); + + }while (current_nsec () < tout);
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1987
Quoted 10 lines
tools/testing/vsock/vsock_test.c +10
-0+ +control_writeulong (CONTROL_DONE ); + + ret =pthread_cancel (thread_id); +if (ret) { +fprintf (stderr,"pthread_cancel: %d\n" , ret); +exit (EXIT_FAILURE ); + } + +/* Wait for the thread to terminate */
useless comment
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1994
Quoted 7 lines
tools/testing/vsock/vsock_test.c +7
-0+ ret =pthread_join (thread_id,NULL ); +if (ret) { +fprintf (stderr,"pthread_join: %d\n" , ret); +exit (EXIT_FAILURE ); + } + +/* Restore the old handler */
ditto
Quoted 22 lines
tools/testing/vsock/vsock_test.c +22
-0+if (signal (SIGUSR1 , old_handler) ==SIG_ERR ) { +perror ("signal" ); +exit (EXIT_FAILURE ); + } +} + +static void test_stream_transport_change_server (const struct test_opts *opts) +{ +int s =vsock_stream_listen (VMADDR_CID_ANY , opts->peer_port); + +/* Set the socket to be nonblocking because connects that have been interrupted +* (EINTR) can fill the receiver's accept queue anyway, leading to connect failure. +* As of today (6.15) in such situation there is no way to understand, from the +* client side, if the connection has been queued in the server or not. +*/ +if (fcntl (s,F_SETFL ,fcntl (s,F_GETFL ,0 ) |O_NONBLOCK ) <0 ) { +perror ("fcntl" ); +exit (EXIT_FAILURE ); + } +control_writeln ("LISTENING" ); + +while (control_readulong () ==CONTROL_CONTINUE ) {
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 2026
Quoted 10 lines
tools/testing/vsock/vsock_test.c +10
-0+struct sockaddr_vm sa_client; +socklen_t socklen_client =sizeof (sa_client); + +/* Must accept the connection, otherwise the `listen` +* queue will fill up and new connections will fail. +* There can be more than one queued connection, +* clear them all. +*/ +while (true) { +int client =accept (s, (struct sockaddr *)&sa_client, &socklen_client);
We don't use the client address, so IMO we can just pass NULL.
Thanks,
Stefano
Quoted 40 lines
tools/testing/vsock/vsock_test.c +16
-0+ +if (client <0 ) { +if (errno ==EAGAIN ) +break ; + +perror ("accept" ); +exit (EXIT_FAILURE ); + } + +close (client); + } + } + +close (s); +} +static void
test_stream_linger_client(const struct test_opts *opts){ int fd;tools/testing/vsock/vsock_test.c +5
-1@@ -2106,6 +2279,11 @@ static struct test_case test_cases[] = { .run_client = test_stream_nolinger_client, .run_server = test_stream_nolinger_server, }, + { + .name = "SOCK_STREAM transport change null-ptr-deref", + .run_client = test_stream_transport_change_client, + .run_server = test_stream_transport_change_server, + }, {}, }; ---base-commit:
68d019aa14d97f8d57b0f8d203fd3b44db2ba0c7
change-id: 20250306-test_vsock-3e77a9c7a245Best regards,
Signature · 1 line
Luigi Leonardi <[email protected]>
Hi Stefano,
Quoted 28 lines · Stefano Garzarella
Quoted 27 lines · Luigi Leonardi
Add a new test to ensure that when the transport changes a null pointer dereference does not occur. The bug was reported upstream [1] and fixed with commit
2cb7c756f605("vsock/virtio: discard packets if the transport changes").KASAN: null-ptr-deref in range [0x0000000000000060-0x0000000000000067] CPU: 2 UID: 0 PID: 463 Comm: kworker/2:3 Not tainted Workqueue: vsock-loopback vsock_loopback_work RIP: 0010:vsock_stream_has_data+0x44/0x70 Call Trace: virtio_transport_do_close+0x68/0x1a0 virtio_transport_recv_pkt+0x1045/0x2ae4 vsock_loopback_work+0x27d/0x3f0 process_one_work+0x846/0x1420 worker_thread+0x5b3/0xf80 kthread+0x35a/0x700 ret_from_fork+0x2d/0x70 ret_from_fork_asm+0x1a/0x30Note that this test may not fail in a kernel without the fix, but it may hang on the client side if it triggers a kernel oops.
This works by creating a socket, trying to connect to a server, and then executing a second connect operation on the same socket but to a different CID (0). This triggers a transport change. If the connect operation is interrupted by a signal, this could cause a null-ptr-deref.
Quoted 8 lines
Quoted 6 lines
Since this bug is non-deterministic, we need to try several times. It is reasonable to assume that the bug will show up within the timeout period.
If there is a G2H transport loaded in the system, the bug is not triggered and this test will always pass.
Can you add the reason?
Will do.
Quoted 138 lines
Quoted 118 lines
[1]https://lore.kernel.org/netdev/Z2LvdTTQR7dBmPb5@v4bel-B760M-AORUS-ELITE-AX/
Suggested-by Hyunwoo Kim <[email protected]>Suggested-by Michal Luczaj <[email protected]>Signed-off-by Luigi Leonardi <[email protected]>---
This series introduces a new test that checks for a null pointer dereference that may happen when there is a transport change[1]. This bug was fixed in [2].Note that this test *cannot* fail, it hangs if it triggers a kernel oops. The intended use-case is to run it and then check if there is any oops in the dmesg.
This test is based on Hyunwoo Kim's[3] and Michal's python reproducers[4].
[1]https://lore.kernel.org/netdev/Z2LvdTTQR7dBmPb5@v4bel-B760M-AORUS-ELITE-AX/ [2]https://lore.kernel.org/netdev/[email protected]/ [3]https://lore.kernel.org/netdev/Z2LvdTTQR7dBmPb5@v4bel-B760M-AORUS-ELITE-AX/#t [4]https://lore.kernel.org/netdev/[email protected]/
---
Changes in v4:
- Addressed Stefano's comments:
- Minor style changes
- Useget_transports()to print a warning when a G2H transport is loaded
- Removed check on second connect: Because the first connect is interrupted, the socket is in an unspecified state (see man connect) . This can cause strange and unexpected behaviors (connect returning success on a non-existing CID).- Link to v3:
https://lore.kernel.org/r/[email protected]Sorry, this took waaay longer than expected.
Changes in v3:
Addressed Stefano's and Michal's comments:
- Added the splat text to the commit commessage.
- Introduced commit hash that fixes the bug.
- Not using perror anymore on pthread_* functions.
- Listener is just created once.- Link to v2:
https://lore.kernel.org/r/[email protected]Changes in v2:
- Addressed Stefano's comments:
- Timeout is now usingcurrent_nsec()
- Check for return values
- Style issues
- Added Hyunwoo Kim to Suggested-by
- Link to v1:
https://lore.kernel.org/r/[email protected]
---tools/testing/vsock/Makefile| 1 +tools/testing/vsock/vsock_test.c| 178 +++++++++++++++++++++++++++++++++++++++2 files changed, 179 insertions(+)tools/testing/vsock/Makefile +0
-0@@ -5,6 +5,7 @@ vsock_test: vsock_test.o vsock_test_zerocopy.o timeout.o control.o util.o msg_ze
vsock_diag_test: vsock_diag_test.o timeout.o control.o util.ovsock_perf: vsock_perf.o msg_zerocopy_common.o+vsock_test: LDLIBS = -lpthread
vsock_uring_test: LDLIBS = -luringvsock_uring_test: control.o util.o vsock_uring_test.o timeout.o msg_zerocopy_common.otools/testing/vsock/vsock_test.c +29
-0@@ -22,6 +22,8 @@#include <signal.h> #include <sys/ioctl.h> #include <linux/time64.h> +#include <pthread.h> +#include <fcntl.h> #include "vsock_test_zerocopy.h" #include "timeout.h" @@ -1867,6 +1869,177 @@ static void test_stream_connect_retry_server(const struct test_opts *opts)close (fd); } +#define TRANSPORT_CHANGE_TIMEOUT 2/* seconds */ + +static void *test_stream_transport_change_thread (void *vargp) +{ +pid_t *pid = (pid_t *)vargp; +int ret; + +/* We want this thread to terminate as soon as possible */ + ret =pthread_setcanceltype (PTHREAD_CANCEL_ASYNCHRONOUS ,NULL ); +if (ret) { +fprintf (stderr,"pthread_setcanceltype: %d\n" , ret); +exit (EXIT_FAILURE ); + } + +while (true) { +if (kill (*pid,SIGUSR1 ) <0 ) { +perror ("kill" ); +exit (EXIT_FAILURE ); + } + } +return NULL ; +} + +static void test_transport_change_signal_handler (int signal) +{ +/* We need a custom handler for SIGUSR1 as the default one terminates the process. */ +}tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1908
Quoted 10 lines
tools/testing/vsock/vsock_test.c +10
-0+ +static void test_stream_transport_change_client (const struct test_opts *opts) +{ +__sighandler_t old_handler; +pid_t pid =getpid (); +pthread_t thread_id; +time_t tout; +int ret, tr; + + tr =get_transports ();nit: add a blank line here
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1912
Quoted 4 lines
tools/testing/vsock/vsock_test.c +4
-0+/* Print a warning if there is a G2H transport loaded. +* This is on a best effort basis because VMCI can be either G2H and H2G, and there is +* no easy way to understand it. +* The bug is present in the loopback transport. However, it does not interfere nit: s/is/was
Quoted 3 lines
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1913
Quoted 1 line
tools/testing/vsock/vsock_test.c +1
-0+* if it is loaded. I don't understand this, if the bug was present in the loopback transport, our goal is to stress it, so have it loaded is great, why it should interfere?
What I meant to say is that, when testing the H2G path to trigger the
issue, and the loopback transport is there, it does not prevent the bug, like a G2H transport would do.
Quoted 10 lines
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1914
Quoted 2 lines
tools/testing/vsock/vsock_test.c +1
-0+* The bug we are testing only appears when G2H transports are not loaded. Please add the reason also here.
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1916
Quoted 2 lines
tools/testing/vsock/vsock_test.c +2
-0+*/ +nit: remove the blank line here
Quoted 5 lines
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1918
Quoted 2 lines
tools/testing/vsock/vsock_test.c +2
-0+ tr &= ~TRANSPORT_LOOPBACK ; +if (tr !=0 && tr !=TRANSPORT_VHOST )Sorry, this is really hard to understand IMO, let's do a step back. Your goal is to check if there is a G2H transport loaded, right?
correct
Quoted 9 lines
I think we have 2 options:
1. similar to your, just masking the other 2 transportsif (tr & ~(TRANSPORT_LOOPBACK |
TRANSPORT_VHOST))IMO this is much clear to understand, and should have the same effect.
Quoted 9 lines
2. (my preference) define in
util.hsome macros that we can reuse:#define TRANSPORTS_G2H (TRANSPORT_VIRTIO | TRANSPORT_VMCI | TRANSPORT_HYPERV) #define TRANSPORTS_H2G (TRANSPORT_VHOST | TRANSPORT_VMCI) #define TRANSPORTS_LOCAL (TRANSPORT_LOOPBACK)and here you can just do:
if (tr &TRANSPORTS_G2H)
I'll add these defines in a separate commit, thanks for the hint.
Quoted 167 lines
Quoted 23 lines
tools/testing/vsock/vsock_test.c +23
-0+fprintf (stderr,"G2H Transport detected. This test will not fail.\n" ); + + old_handler =signal (SIGUSR1 , test_transport_change_signal_handler); +if (old_handler ==SIG_ERR ) { +perror ("signal" ); +exit (EXIT_FAILURE ); + } + + ret =pthread_create (&thread_id,NULL , test_stream_transport_change_thread, &pid); +if (ret) { +fprintf (stderr,"pthread_create: %d\n" , ret); +exit (EXIT_FAILURE ); + } + +control_expectln ("LISTENING" ); + + tout =current_nsec () +TRANSPORT_CHANGE_TIMEOUT *NSEC_PER_SEC ; + do { + struct sockaddr_vm sa = { + .svm_family = AF_VSOCK, + .svm_cid = opts->peer_cid, + .svm_port = opts->peer_port, + };tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1951
Quoted 10 lines
tools/testing/vsock/vsock_test.c +10
-0+int s; + + s =socket (AF_VSOCK ,SOCK_STREAM ,0 ); +if (s <0 ) { +perror ("socket" ); +exit (EXIT_FAILURE ); + } + + ret =connect (s, (struct sockaddr *)&sa,sizeof (sa)); +/* The connect can fail due to signals coming from the thread. . should be ,
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1963
Quoted 12 lines
tools/testing/vsock/vsock_test.c +12
-0+* or because the receiver connection queue is full. +* Ignoring also the latter case because there is no way +* of synchronizing client's connect and server's accept when +* connect(s) are constantly being interrupted by signals. +*/ +if (ret ==-1 && (errno !=EINTR && errno !=ECONNRESET )) { +perror ("connect" ); +exit (EXIT_FAILURE ); + } + +/* Set CID to 0 cause a transport change. */ + sa.svm_cid =0 ;nit: add a blank line
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1969
Quoted 6 lines
tools/testing/vsock/vsock_test.c +6
-0+/* Here we ignore the connect return value because we cannot +* safely assume that it will *always* fail. +* This is because the previous connect was interrupted +* during the connection process. The socket state, as stated +* in `man connect`, is unspecified and can result in strange +* behaviors. Let's focus on the behaviour and try to be more concise. Something like this:
/* Ignore return value since it can fail or not.
* If the previous connect is interrupted while the
* connection request is already sent, the second
*connect()will wait for the response.Quoted 8 lines
tools/testing/vsock/vsock_test.c +8
-0+*/ +connect (s, (struct sockaddr *)&sa,sizeof (sa)); + +close (s); + +control_writeulong (CONTROL_CONTINUE ); + + }while (current_nsec () < tout);tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1987
Quoted 10 lines
tools/testing/vsock/vsock_test.c +10
-0+ +control_writeulong (CONTROL_DONE ); + + ret =pthread_cancel (thread_id); +if (ret) { +fprintf (stderr,"pthread_cancel: %d\n" , ret); +exit (EXIT_FAILURE ); + } + +/* Wait for the thread to terminate */ useless comment
tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 1994
Quoted 7 lines
tools/testing/vsock/vsock_test.c +7
-0+ ret =pthread_join (thread_id,NULL ); +if (ret) { +fprintf (stderr,"pthread_join: %d\n" , ret); +exit (EXIT_FAILURE ); + } + +/* Restore the old handler */ ditto
Quoted 22 lines
tools/testing/vsock/vsock_test.c +22
-0+if (signal (SIGUSR1 , old_handler) ==SIG_ERR ) { +perror ("signal" ); +exit (EXIT_FAILURE ); + } +} + +static void test_stream_transport_change_server (const struct test_opts *opts) +{ +int s =vsock_stream_listen (VMADDR_CID_ANY , opts->peer_port); + +/* Set the socket to be nonblocking because connects that have been interrupted +* (EINTR) can fill the receiver's accept queue anyway, leading to connect failure. +* As of today (6.15) in such situation there is no way to understand, from the +* client side, if the connection has been queued in the server or not. +*/ +if (fcntl (s,F_SETFL ,fcntl (s,F_GETFL ,0 ) |O_NONBLOCK ) <0 ) { +perror ("fcntl" ); +exit (EXIT_FAILURE ); + } +control_writeln ("LISTENING" ); + +while (control_readulong () ==CONTROL_CONTINUE ) {tools/testing/vsock/vsock_test.c static void test_stream_connect_retry_server(const struct test_opts *opts) · line 2026
Quoted 10 lines
tools/testing/vsock/vsock_test.c +10
-0+struct sockaddr_vm sa_client; +socklen_t socklen_client =sizeof (sa_client); + +/* Must accept the connection, otherwise the `listen` +* queue will fill up and new connections will fail. +* There can be more than one queued connection, +* clear them all. +*/ +while (true) { +int client =accept (s, (struct sockaddr *)&sa_client, &socklen_client);We don't use the client address, so IMO we can just pass NULL.
Thanks,
StefanoQuoted 32 lines
tools/testing/vsock/vsock_test.c +21
-0+ +if (client <0 ) { +if (errno ==EAGAIN ) +break ; + +perror ("accept" ); +exit (EXIT_FAILURE ); + } + +close (client); + } + } + +close (s); +} +static void test_stream_linger_client (const struct test_opts *opts) {int fd;@@ -2106,6 +2279,11 @@ static struct test_case test_cases[] = { .run_client = test_stream_nolinger_client, .run_server = test_stream_nolinger_server, }, + { + .name = "SOCK_STREAM transport change null-ptr-deref", + .run_client = test_stream_transport_change_client, + .run_server = test_stream_transport_change_server, + }, {}, };
Quoted 9 lines
Quoted 8 lines
---
base-commit:68d019aa14d97f8d57b0f8d203fd3b44db2ba0c7
change-id: 20250306-test_vsock-3e77a9c7a245Best regards,
Signature · 1 line
Luigi Leonardi <[email protected]>
Thanks for the review all the comments!
Luigi