Skip to contentSkip to conversation

selftests: vsock: avoid mktemp -u for Unix socket paths

PATCH In review CaoRuichuang 4 participants 9 messages Apr 5 – Apr 11

Subsystems (5)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests: vsock: avoid mktemp -u for Unix socket paths

The namespace tests create temporary Unix socket paths with mktemp -u and then hand them to socat. That only prints an unused pathname and leaves a race before the socket is created.

Create a private temporary directory with mktemp -d and place the Unix socket inside it instead. This keeps the path unique without relying on mktemp -u for filesystem socket names.

---
 tools/testing/selftests/vsock/vmtest.sh | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
tools/testing/selftests/vsock/vmtest.sh +9 -4
index 86e338886..91e0037d2 100755
@@ -718,6 +718,7 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
 	local pids pid pidfile
 	local ns0 ns1 port
 	declare -a pids
+	local unixdir
 	local unixfile
 	ns0="global0"
 	ns1="global1"
@@ -736,7 +737,8 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
 	oops_before=$(vm_dmesg_oops_count "${ns0}")
 	warn_before=$(vm_dmesg_warn_count "${ns0}")
 
-	unixfile=$(mktemp -u /tmp/XXXX.sock)
+	unixdir=$(mktemp -d /tmp/vsock_vmtest_XXXXXX)
+	unixfile="${unixdir}/sock"
 	ip netns exec "${ns1}" \
 		socat TCP-LISTEN:"${TEST_HOST_PORT}",fork \
 			UNIX-CONNECT:"${unixfile}" &
@@ -758,6 +760,7 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
 
 	terminate_pids "${pids[@]}"
 	terminate_pidfiles "${pidfile}"
+	rm -rf "${unixdir}"
 
 	if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then
 		return "${KSFT_FAIL}"
@@ -814,6 +817,7 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 	local ns0="global0"
 	local ns1="global1"
 	local port=12345
+	local unixdir
 	local unixfile
 	local dmesg_rc
 	local pidfile
@@ -826,7 +830,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 
 	log_host "Setup socat bridge from ns ${ns0} to ns ${ns1} over port ${port}"
 
-	unixfile=$(mktemp -u /tmp/XXXX.sock)
+	unixdir=$(mktemp -d /tmp/vsock_vmtest_XXXXXX)
+	unixfile="${unixdir}/sock"
 
 	ip netns exec "${ns0}" \
 		socat TCP-LISTEN:"${port}" UNIX-CONNECT:"${unixfile}" &
@@ -845,7 +850,7 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 	if ! vm_start "${pidfile}" "${ns0}"; then
 		log_host "failed to start vm (cid=${cid}, ns=${ns0})"
 		terminate_pids "${pids[@]}"
-		rm -f "${unixfile}"
+		rm -rf "${unixdir}"
 		return "${KSFT_FAIL}"
 	fi
 
@@ -862,7 +867,7 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 
 	terminate_pidfiles "${pidfile}"
 	terminate_pids "${pids[@]}"
-	rm -f "${unixfile}"
+	rm -rf "${unixdir}"
 
 	if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then
 		return "${KSFT_FAIL}"
Signature · 1 line
2.39.5 (Apple Git-154)
reviewer
#1 lore raw
Quoted 9 lines · CaoRuichuang

The namespace tests create temporary Unix socket paths with mktemp -u and then hand them to socat. That only prints an unused pathname and leaves a race before the socket is created.

Create a private temporary directory with mktemp -d and place the Unix socket inside it instead. This keeps the path unique without relying on mktemp -u for filesystem socket names.

I think the subject could be improved a bit. More along the lines of the problem being solved, less on how it is achieved.

...

tools/testing/selftests/vsock/vmtest.sh test_ns_diff_global_vm_connect_to_global_host_ok() { · line 853

Quoted 6 lines
tools/testing/selftests/vsock/vmtest.sh +1 -1
@@ -845,7 +850,7 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 	if ! vm_start "${pidfile}" "${ns0}"; then
 		log_host "failed to start vm (cid=${cid}, ns=${ns0})"
 		terminate_pids "${pids[@]}"
-		rm -f "${unixfile}"
+		rm -rf "${unixdir}"

rm -rf makes me feel queasy.
Can rmdir, and rm, ideally without the -f, be used instead?

rm "$unixfile"
rmdir "$unixdir"

Quoted 3 lines
tools/testing/selftests/vsock/vmtest.sh +0 -0
 		return "${KSFT_FAIL}"
 	fi
 

...

#2 lore raw

PATCH v2 1/1 selftests: vsock: avoid races creating Unix socket paths

In reply to CaoRuichuang

---
v2:
- retitle the patch to describe the race being fixed
- replace rm -rf with explicit rm and rmdir cleanup

 tools/testing/selftests/vsock/vmtest.sh | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
tools/testing/selftests/vsock/vmtest.sh +12 -4
index 86e338886b3..c345fa539d3 100755
@@ -718,6 +718,7 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
 	local pids pid pidfile
 	local ns0 ns1 port
 	declare -a pids
+	local unixdir
 	local unixfile
 	ns0="global0"
 	ns1="global1"
@@ -736,7 +737,8 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
 	oops_before=$(vm_dmesg_oops_count "${ns0}")
 	warn_before=$(vm_dmesg_warn_count "${ns0}")
 
-	unixfile=$(mktemp -u /tmp/XXXX.sock)
+	unixdir=$(mktemp -d /tmp/vsock_vmtest_XXXXXX)
+	unixfile="${unixdir}/sock"
 	ip netns exec "${ns1}" \
 		socat TCP-LISTEN:"${TEST_HOST_PORT}",fork \
 			UNIX-CONNECT:"${unixfile}" &
@@ -758,6 +760,8 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
 
 	terminate_pids "${pids[@]}"
 	terminate_pidfiles "${pidfile}"
+	rm "${unixfile}"
+	rmdir "${unixdir}"
 
 	if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then
 		return "${KSFT_FAIL}"
@@ -814,6 +818,7 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 	local ns0="global0"
 	local ns1="global1"
 	local port=12345
+	local unixdir
 	local unixfile
 	local dmesg_rc
 	local pidfile
@@ -826,7 +831,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 
 	log_host "Setup socat bridge from ns ${ns0} to ns ${ns1} over port ${port}"
 
-	unixfile=$(mktemp -u /tmp/XXXX.sock)
+	unixdir=$(mktemp -d /tmp/vsock_vmtest_XXXXXX)
+	unixfile="${unixdir}/sock"
 
 	ip netns exec "${ns0}" \
 		socat TCP-LISTEN:"${port}" UNIX-CONNECT:"${unixfile}" &
@@ -845,7 +851,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 	if ! vm_start "${pidfile}" "${ns0}"; then
 		log_host "failed to start vm (cid=${cid}, ns=${ns0})"
 		terminate_pids "${pids[@]}"
-		rm -f "${unixfile}"
+		rm "${unixfile}"
+		rmdir "${unixdir}"
 		return "${KSFT_FAIL}"
 	fi
 
@@ -862,7 +869,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 
 	terminate_pidfiles "${pidfile}"
 	terminate_pids "${pids[@]}"
-	rm -f "${unixfile}"
+	rm "${unixfile}"
+	rmdir "${unixdir}"
 
 	if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then
 		return "${KSFT_FAIL}"
Signature · 1 line
2.39.5 (Apple Git-154)
#3 lore raw

Re: [PATCH v2] selftests: vsock: avoid races creating Unix socket paths

On Fri, Apr 10, 2026 at 11:52:37AM +0800, Cao Ruichuang wrote:

No patch description at all?

Quoted 82 lines

---
v2:
- retitle the patch to describe the race being fixed
- replace rm -rf with explicit rm and rmdir cleanup

tools/testing/selftests/vsock/vmtest.sh | 16 ++++++++++++----

1 file changed, 12 insertions(+), 4 deletions(-)
tools/testing/selftests/vsock/vmtest.sh +12 -4
index 86e338886b3..c345fa539d3 100755
@@ -718,6 +718,7 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
 	local pids pid pidfile
 	local ns0 ns1 port
 	declare -a pids
+	local unixdir
 	local unixfile
 	ns0="global0"
 	ns1="global1"
@@ -736,7 +737,8 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
 	oops_before=$(vm_dmesg_oops_count "${ns0}")
 	warn_before=$(vm_dmesg_warn_count "${ns0}")
 
-	unixfile=$(mktemp -u /tmp/XXXX.sock)
+	unixdir=$(mktemp -d /tmp/vsock_vmtest_XXXXXX)
+	unixfile="${unixdir}/sock"
 	ip netns exec "${ns1}" \
 		socat TCP-LISTEN:"${TEST_HOST_PORT}",fork \
 			UNIX-CONNECT:"${unixfile}" &
@@ -758,6 +760,8 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
 
 	terminate_pids "${pids[@]}"
 	terminate_pidfiles "${pidfile}"
+	rm "${unixfile}"
+	rmdir "${unixdir}"
 
 	if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then
 		return "${KSFT_FAIL}"
@@ -814,6 +818,7 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 	local ns0="global0"
 	local ns1="global1"
 	local port=12345
+	local unixdir
 	local unixfile
 	local dmesg_rc
 	local pidfile
@@ -826,7 +831,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 
 	log_host "Setup socat bridge from ns ${ns0} to ns ${ns1} over port ${port}"
 
-	unixfile=$(mktemp -u /tmp/XXXX.sock)
+	unixdir=$(mktemp -d /tmp/vsock_vmtest_XXXXXX)
+	unixfile="${unixdir}/sock"
 
 	ip netns exec "${ns0}" \
 		socat TCP-LISTEN:"${port}" UNIX-CONNECT:"${unixfile}" &
@@ -845,7 +851,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 	if ! vm_start "${pidfile}" "${ns0}"; then
 		log_host "failed to start vm (cid=${cid}, ns=${ns0})"
 		terminate_pids "${pids[@]}"
-		rm -f "${unixfile}"
+		rm "${unixfile}"
+		rmdir "${unixdir}"
 		return "${KSFT_FAIL}"
 	fi
 
@@ -862,7 +869,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 
 	terminate_pidfiles "${pidfile}"
 	terminate_pids "${pids[@]}"
-	rm -f "${unixfile}"
+	rm "${unixfile}"
+	rmdir "${unixdir}"
 
 	if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then
 		return "${KSFT_FAIL}"
Signature · 1 line
2.39.5 (Apple Git-154)
#4 lore raw

Re: [PATCH v2] selftests: vsock: avoid races creating Unix socket paths

Quoted 4 lines · Stefano Garzarella

On Fri, Apr 10, 2026 at 11:52:37AM +0800, Cao Ruichuang wrote:

No patch description at all?

Also, maybe better to CC Bobby.

Stefano

Quoted 83 lines
Quoted 83 lines

---
v2:
- retitle the patch to describe the race being fixed
- replace rm -rf with explicit rm and rmdir cleanup

tools/testing/selftests/vsock/vmtest.sh | 16 ++++++++++++----

1 file changed, 12 insertions(+), 4 deletions(-)
tools/testing/selftests/vsock/vmtest.sh +12 -5
index 86e338886b3..c345fa539d3 100755
@@ -718,6 +718,7 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
      local pids pid pidfile
      local ns0 ns1 port
      declare -a pids
+      local unixdir
      local unixfile
      ns0="global0"
      ns1="global1"
@@ -736,7 +737,8 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
      oops_before=$(vm_dmesg_oops_count "${ns0}")
      warn_before=$(vm_dmesg_warn_count "${ns0}")
 
-      unixfile=$(mktemp -u /tmp/XXXX.sock)
+      unixdir=$(mktemp -d /tmp/vsock_vmtest_XXXXXX)
+      unixfile="${unixdir}/sock"
      ip netns exec "${ns1}" \
              socat TCP-LISTEN:"${TEST_HOST_PORT}",fork \
                      UNIX-CONNECT:"${unixfile}" &
@@ -758,6 +760,8 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
 
      terminate_pids "${pids[@]}"
      terminate_pidfiles "${pidfile}"
+      rm "${unixfile}"
+      rmdir "${unixdir}"
 
      if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then
              return "${KSFT_FAIL}"
@@ -814,6 +818,7 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
      local ns0="global0"
      local ns1="global1"
      local port=12345
+      local unixdir
      local unixfile
      local dmesg_rc
      local pidfile
@@ -826,7 +831,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 
      log_host "Setup socat bridge from ns ${ns0} to ns ${ns1} over port ${port}"
 
-      unixfile=$(mktemp -u /tmp/XXXX.sock)
+      unixdir=$(mktemp -d /tmp/vsock_vmtest_XXXXXX)
+      unixfile="${unixdir}/sock"
 
      ip netns exec "${ns0}" \
              socat TCP-LISTEN:"${port}" UNIX-CONNECT:"${unixfile}" &
@@ -845,7 +851,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
      if ! vm_start "${pidfile}" "${ns0}"; then
              log_host "failed to start vm (cid=${cid}, ns=${ns0})"
              terminate_pids "${pids[@]}"
-              rm -f "${unixfile}"
+              rm "${unixfile}"
+              rmdir "${unixdir}"
              return "${KSFT_FAIL}"
      fi
 
@@ -862,7 +869,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 
      terminate_pidfiles "${pidfile}"
      terminate_pids "${pids[@]}"
-      rm -f "${unixfile}"
+      rm "${unixfile}"
+      rmdir "${unixdir}"
 
      if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then
              return "${KSFT_FAIL}"
--

2.39.5 (Apple Git-154)

#5 lore raw

Re: [PATCH v2] selftests: vsock: avoid races creating Unix socket paths

In reply to Stefano Garzarella

Sorry, that was my mistake.

I accidentally sent v2 without the patch description. I will resend it as v3 with the proper changelog and Cc Bobby as suggested.

Thanks for catching this.

#6 lore raw

PATCH v3 1/1 selftests: vsock: avoid races creating Unix socket paths

In reply to Cao Ruichuang

vmtest.sh currently uses mktemp -u to precompute Unix socket paths for the namespace bridge helpers. That only returns an unused pathname and leaves a time-of-check/time-of-use window before socat binds or connects to it.

Create a private temporary directory with mktemp -d and place the socket path inside it instead. This removes the pathname race while keeping cleanup straightforward.

---
v3:
- restore the missing patch description
- add Bobby Eshleman to Cc

 tools/testing/selftests/vsock/vmtest.sh | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)
tools/testing/selftests/vsock/vmtest.sh +12 -4
index 86e338886b3..c345fa539d3 100755
@@ -718,6 +718,7 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
 	local pids pid pidfile
 	local ns0 ns1 port
 	declare -a pids
+	local unixdir
 	local unixfile
 	ns0="global0"
 	ns1="global1"
@@ -736,7 +737,8 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
 	oops_before=$(vm_dmesg_oops_count "${ns0}")
 	warn_before=$(vm_dmesg_warn_count "${ns0}")
 
-	unixfile=$(mktemp -u /tmp/XXXX.sock)
+	unixdir=$(mktemp -d /tmp/vsock_vmtest_XXXXXX)
+	unixfile="${unixdir}/sock"
 	ip netns exec "${ns1}" \
 		socat TCP-LISTEN:"${TEST_HOST_PORT}",fork \
 			UNIX-CONNECT:"${unixfile}" &
@@ -758,6 +760,8 @@ test_ns_diff_global_host_connect_to_global_vm_ok() {
 
 	terminate_pids "${pids[@]}"
 	terminate_pidfiles "${pidfile}"
+	rm "${unixfile}"
+	rmdir "${unixdir}"
 
 	if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then
 		return "${KSFT_FAIL}"
@@ -814,6 +818,7 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 	local ns0="global0"
 	local ns1="global1"
 	local port=12345
+	local unixdir
 	local unixfile
 	local dmesg_rc
 	local pidfile
@@ -826,7 +831,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 
 	log_host "Setup socat bridge from ns ${ns0} to ns ${ns1} over port ${port}"
 
-	unixfile=$(mktemp -u /tmp/XXXX.sock)
+	unixdir=$(mktemp -d /tmp/vsock_vmtest_XXXXXX)
+	unixfile="${unixdir}/sock"
 
 	ip netns exec "${ns0}" \
 		socat TCP-LISTEN:"${port}" UNIX-CONNECT:"${unixfile}" &
@@ -845,7 +851,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 	if ! vm_start "${pidfile}" "${ns0}"; then
 		log_host "failed to start vm (cid=${cid}, ns=${ns0})"
 		terminate_pids "${pids[@]}"
-		rm -f "${unixfile}"
+		rm "${unixfile}"
+		rmdir "${unixdir}"
 		return "${KSFT_FAIL}"
 	fi
 
@@ -862,7 +869,8 @@ test_ns_diff_global_vm_connect_to_global_host_ok() {
 
 	terminate_pidfiles "${pidfile}"
 	terminate_pids "${pids[@]}"
-	rm -f "${unixfile}"
+	rm "${unixfile}"
+	rmdir "${unixdir}"
 
 	if [[ "${rc}" -ne 0 ]] || [[ "${dmesg_rc}" -ne 0 ]]; then
 		return "${KSFT_FAIL}"
Signature · 1 line
2.39.5 (Apple Git-154)
maintainer
#7 lore raw

Re: [PATCH v3] selftests: vsock: avoid races creating Unix socket paths

Quoted 7 lines · On Fri, 10 Apr 2026 18:07:00 +0800 Cao Ruichuang wrote:

vmtest.sh currently uses mktemp -u to precompute Unix socket paths for the namespace bridge helpers. That only returns an unused pathname and leaves a time-of-check/time-of-use window before socat binds or connects to it.

Create a private temporary directory with mktemp -d and place the socket path inside it instead. This removes the pathname race while keeping cleanup straightforward.

And you actually run into this as a real problem?
How do you repro the failure?

Basic netdev rules:
- don't post new version of patches in reply to the old ones
- no more than 1 posting in a 24h period

#8 lore raw

Re: [PATCH v3] selftests: vsock: avoid races creating Unix socket paths

Hi Jakub,

Thanks for the correction.

No, I do not have a concrete failure reproducer for this one. My change was based on the mktemp -u TOCTOU window in the selftest setup rather than on a demonstrated flaky failure.

Given that, I agree this is not strong enough to keep reposting on the netdev side. I will stop here unless I can come back with a real repro.

I will also follow the netdev posting rules you mentioned for future updates.

Thanks,
Cao Ruichuang