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