git: 19a496794685 - stable/14 - tests: Fix race condition in aslr_setuid

From: Dag-Erling Smørgrav <des_at_FreeBSD.org>
Date: Tue, 07 Jul 2026 20:57:56 UTC
The branch stable/14 has been updated by des:

URL: https://cgit.FreeBSD.org/src/commit/?id=19a4967946855bb3941382a1a1ccf3c7c1d5a1f3

commit 19a4967946855bb3941382a1a1ccf3c7c1d5a1f3
Author:     Dag-Erling Smørgrav <des@FreeBSD.org>
AuthorDate: 2026-06-22 13:23:31 +0000
Commit:     Dag-Erling Smørgrav <des@FreeBSD.org>
CommitDate: 2026-07-07 07:07:55 +0000

    tests: Fix race condition in aslr_setuid
    
    Use a cloexec pipe to block the parent until the child is ready.
    
    While here, redirect the output from ping to /dev/null, and mark the
    test as requiring the inet feature since we ping the IPv4 loopback.
    
    PR:             296116
    MFC after:      1 week
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D57734
    
    (cherry picked from commit 080a4087014e1d19136cc77028019d98b5c69e1e)
    
    tests: Fix race condition in aslr_setuid, take 2
    
    Instead of a cloexec pipe, ingest ping's stdout and block until it has
    printed its initial summary, then close the pipe and return to the main
    test loop.  Run ping in quiet mode so it won't mind that stdout is gone.
    
    PR:             296116
    MFC after:      1 week
    Fixes:          080a4087014e ("tests: Fix race condition in aslr_setuid")
    Reviewed by:    markj
    Differential Revision:  https://reviews.freebsd.org/D57763
    
    (cherry picked from commit 86950cf9ffe89b7978fca019019692b3ea492044)
---
 tests/sys/kern/Makefile |  1 +
 tests/sys/kern/aslr.c   | 16 ++++++++++++----
 2 files changed, 13 insertions(+), 4 deletions(-)

diff --git a/tests/sys/kern/Makefile b/tests/sys/kern/Makefile
index d28b6e850636..f9199440903b 100644
--- a/tests/sys/kern/Makefile
+++ b/tests/sys/kern/Makefile
@@ -81,6 +81,7 @@ PROGS+=		coredump_phnum_helper
 PROGS+=		pdeathsig_helper
 PROGS+=		sendfile_helper
 
+CFLAGS.aslr+=				-I${SRCTOP}/tests
 LIBADD.aslr+=				util
 LIBADD.copy_file_range+=		md
 LIBADD.jail_lookup_root+=		jail util
diff --git a/tests/sys/kern/aslr.c b/tests/sys/kern/aslr.c
index 13038054603c..966ea5c3b83a 100644
--- a/tests/sys/kern/aslr.c
+++ b/tests/sys/kern/aslr.c
@@ -11,6 +11,7 @@
 #include <sys/user.h>
 #include <sys/wait.h>
 
+#include <fcntl.h>
 #include <libutil.h>
 #include <pwd.h>
 #include <signal.h>
@@ -19,6 +20,7 @@
 #include <unistd.h>
 
 #include <atf-c.h>
+#include "freebsd_test_suite/macros.h"
 
 /*
  * Spawn an unprivileged child with ASLR force-disabled, which then execs
@@ -27,19 +29,22 @@
 static pid_t
 spawn_ping(const atf_tc_t *tc)
 {
+	char line[64];
 	const char *user;
 	struct passwd *passwd;
 	pid_t child;
-	int arg, error;
+	int arg, error, io[2];
 
 	user = atf_tc_get_config_var(tc, "unprivileged_user");
 	passwd = getpwnam(user);
 	ATF_REQUIRE(passwd != NULL);
 
+	ATF_REQUIRE(pipe2(io, O_CLOEXEC) == 0);
 	child = fork();
 	ATF_REQUIRE(child >= 0);
 	if (child == 0) {
-		if (seteuid(passwd->pw_uid) != 0)
+		if (dup2(io[1], STDOUT_FILENO) != STDOUT_FILENO ||
+		    seteuid(passwd->pw_uid) != 0)
 			_exit(1);
 
 		arg = PROC_ASLR_FORCE_DISABLE;
@@ -47,10 +52,12 @@ spawn_ping(const atf_tc_t *tc)
 		if (error != 0)
 			_exit(2);
 
-		execl("/sbin/ping", "ping", "127.0.0.1", NULL);
+		execl("/sbin/ping", "ping", "-q", "127.0.0.1", NULL);
 		_exit(127);
 	}
-	usleep(500000); /* XXX-MJ */
+	ATF_REQUIRE(close(io[1]) == 0);
+	ATF_REQUIRE(read(io[0], line, sizeof(line)) > 0);
+	ATF_REQUIRE(close(io[0]) == 0);
 
 	return (child);
 }
@@ -98,6 +105,7 @@ ATF_TC_BODY(aslr_setuid, tc)
 	pid_t child, pid;
 	int arg, error, st;
 
+	ATF_REQUIRE_FEATURE("inet");
 	if (!atf_tc_has_config_var(tc, "unprivileged_user"))
 		atf_tc_skip("unprivileged_user not set");