xref: /xnu-12377.81.4/tests/exec-race-128791723.c (revision 043036a2b3718f7f0be807e2870f8f47d3fa0796)
1*043036a2SApple OSS Distributions #include <signal.h>
2*043036a2SApple OSS Distributions #include <libproc.h>
3*043036a2SApple OSS Distributions #include <sys/sysctl.h>
4*043036a2SApple OSS Distributions 
5*043036a2SApple OSS Distributions #include <darwintest.h>
6*043036a2SApple OSS Distributions 
7*043036a2SApple OSS Distributions // rdar://128791723
8*043036a2SApple OSS Distributions // Ensure pidversion always changes across exec
9*043036a2SApple OSS Distributions 
10*043036a2SApple OSS Distributions static int32_t
get_pidversion_for_pid(pid_t pid)11*043036a2SApple OSS Distributions get_pidversion_for_pid(pid_t pid)
12*043036a2SApple OSS Distributions {
13*043036a2SApple OSS Distributions 	struct proc_bsdinfowithuniqid bsd_info;
14*043036a2SApple OSS Distributions 	int ret = proc_pidinfo(pid, PROC_PIDT_BSDINFOWITHUNIQID, 0, &bsd_info, sizeof(bsd_info));
15*043036a2SApple OSS Distributions 	T_ASSERT_EQ((unsigned long)ret, sizeof(bsd_info), "PROC_PIDT_BSDINFOWITHUNIQID");
16*043036a2SApple OSS Distributions 	return bsd_info.p_uniqidentifier.p_idversion;
17*043036a2SApple OSS Distributions }
18*043036a2SApple OSS Distributions 
19*043036a2SApple OSS Distributions T_DECL(ensure_pidversion_changes_on_exec,
20*043036a2SApple OSS Distributions     "Ensure pidversion always changes across exec, even when groomed not to",
21*043036a2SApple OSS Distributions     T_META_NAMESPACE("xnu.exec"),
22*043036a2SApple OSS Distributions     T_META_TAG_VM_PREFERRED
23*043036a2SApple OSS Distributions     ) {
24*043036a2SApple OSS Distributions 	T_SETUPBEGIN;
25*043036a2SApple OSS Distributions 
26*043036a2SApple OSS Distributions 	// Given we exec a helper program (in a forked child, so this runner can stick around)
27*043036a2SApple OSS Distributions 	// (And we set up some resources to communicate with the forked process)
28*043036a2SApple OSS Distributions 	int pipefd[2];
29*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(pipe(pipefd), "pipe");
30*043036a2SApple OSS Distributions 
31*043036a2SApple OSS Distributions 	pid_t forked_pid = fork();
32*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(forked_pid, "fork");
33*043036a2SApple OSS Distributions 
34*043036a2SApple OSS Distributions 	if (forked_pid == 0) {
35*043036a2SApple OSS Distributions 		close(pipefd[0]);
36*043036a2SApple OSS Distributions 
37*043036a2SApple OSS Distributions 		// And we keep track of our current pidversion
38*043036a2SApple OSS Distributions 		int32_t forked_proc_pidv = get_pidversion_for_pid(getpid());
39*043036a2SApple OSS Distributions 
40*043036a2SApple OSS Distributions 		// And we ask the kernel to groom things such that `nextpidversion == current_proc->p_idversion + 1`
41*043036a2SApple OSS Distributions 		int64_t val = 0;
42*043036a2SApple OSS Distributions 		size_t val_len = sizeof(val);
43*043036a2SApple OSS Distributions 		sysctlbyname("debug.test.setup_ensure_pidversion_changes_on_exec", &val, &val_len, &val, sizeof(val));
44*043036a2SApple OSS Distributions 
45*043036a2SApple OSS Distributions 		// (And we send the parent's pidversion back to the test runner, for comparison with the exec'd process)
46*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(write(pipefd[1], (void*)&forked_proc_pidv, sizeof(forked_proc_pidv)), "write");
47*043036a2SApple OSS Distributions 		T_ASSERT_POSIX_SUCCESS(close(pipefd[1]), "close");
48*043036a2SApple OSS Distributions 
49*043036a2SApple OSS Distributions 		// When I exec a child
50*043036a2SApple OSS Distributions 		// (Which spins forever, so we can poke it)
51*043036a2SApple OSS Distributions 		char *args[4];
52*043036a2SApple OSS Distributions 		char *tail_path = "/usr/bin/tail";
53*043036a2SApple OSS Distributions 		args[0] = tail_path;
54*043036a2SApple OSS Distributions 		args[1] = "-f";
55*043036a2SApple OSS Distributions 		args[2] = "/dev/null";
56*043036a2SApple OSS Distributions 		args[3] = NULL;
57*043036a2SApple OSS Distributions 		execv(tail_path, args);
58*043036a2SApple OSS Distributions 		T_FAIL("execve() failed");
59*043036a2SApple OSS Distributions 	}
60*043036a2SApple OSS Distributions 
61*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(pipefd[1]), "close");
62*043036a2SApple OSS Distributions 
63*043036a2SApple OSS Distributions 	// (And we read the parent's pidversion from our forked counterpart, for comparison with the exec'd process)
64*043036a2SApple OSS Distributions 	int32_t forked_proc_pidversion;
65*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(read(pipefd[0], &forked_proc_pidversion, sizeof(forked_proc_pidversion)), "read");
66*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(close(pipefd[0]), "close");
67*043036a2SApple OSS Distributions 
68*043036a2SApple OSS Distributions 	// (Give the forked process a moment to exec().)
69*043036a2SApple OSS Distributions 	// (To get rid of this, we could exec something controlled that signals a semaphore.)
70*043036a2SApple OSS Distributions 	sleep(1);
71*043036a2SApple OSS Distributions 
72*043036a2SApple OSS Distributions 	T_SETUPEND;
73*043036a2SApple OSS Distributions 
74*043036a2SApple OSS Distributions 	// And I interrogate the pidversion of the exec'd process
75*043036a2SApple OSS Distributions 	int32_t exec_proc_pidversion = get_pidversion_for_pid(forked_pid);
76*043036a2SApple OSS Distributions 
77*043036a2SApple OSS Distributions 	// Then the pidversion should NOT be reused, despite our grooming
78*043036a2SApple OSS Distributions 	T_ASSERT_NE(exec_proc_pidversion, forked_proc_pidversion, "Prevent pidversion reuse");
79*043036a2SApple OSS Distributions 
80*043036a2SApple OSS Distributions 	// Cleanup: kill our errant child
81*043036a2SApple OSS Distributions 	T_SETUPBEGIN;
82*043036a2SApple OSS Distributions 	T_ASSERT_POSIX_SUCCESS(kill(forked_pid, SIGKILL), "kill");
83*043036a2SApple OSS Distributions 	T_SETUPEND;
84*043036a2SApple OSS Distributions }
85