github.com/SagerNet/gvisor@v0.0.0-20210707092255-7731c139d75c/test/syscalls/linux/processes.cc (about) 1 // Copyright 2021 The gVisor Authors. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #include <stdint.h> 16 #include <sys/syscall.h> 17 #include <unistd.h> 18 19 #include "test/util/capability_util.h" 20 #include "test/util/test_util.h" 21 22 namespace gvisor { 23 namespace testing { 24 25 int testSetPGIDOfZombie(void* arg) { 26 int p[2]; 27 28 TEST_PCHECK(pipe(p) == 0); 29 30 pid_t pid = fork(); 31 if (pid == 0) { 32 pid = fork(); 33 // Create a second child to repeat one of syzkaller reproducers. 34 if (pid == 0) { 35 pid = getpid(); 36 TEST_PCHECK(setpgid(pid, 0) == 0); 37 TEST_PCHECK(write(p[1], &pid, sizeof(pid)) == sizeof(pid)); 38 _exit(0); 39 } 40 TEST_PCHECK(pid > 0); 41 _exit(0); 42 } 43 close(p[1]); 44 TEST_PCHECK(pid > 0); 45 46 // Get PID of the second child. 47 pid_t cpid; 48 TEST_PCHECK(read(p[0], &cpid, sizeof(cpid)) == sizeof(cpid)); 49 50 // Wait when both child processes will die. 51 int c; 52 TEST_PCHECK(read(p[0], &c, sizeof(c)) == 0); 53 54 // Wait the second child process to collect its zombie. 55 int status; 56 TEST_PCHECK(RetryEINTR(waitpid)(cpid, &status, 0) == cpid); 57 58 // Set the child's group. 59 TEST_PCHECK(setpgid(pid, pid) == 0); 60 61 TEST_PCHECK(RetryEINTR(waitpid)(-pid, &status, 0) == pid); 62 63 TEST_PCHECK(status == 0); 64 _exit(0); 65 } 66 67 TEST(Processes, SetPGIDOfZombie) { 68 SKIP_IF(!ASSERT_NO_ERRNO_AND_VALUE(HaveCapability(CAP_SYS_ADMIN))); 69 70 // Fork a test process in a new PID namespace, because it needs to manipulate 71 // with reparanted processes. 72 struct clone_arg { 73 // Reserve some space for clone() to locate arguments and retcode in this 74 // place. 75 char stack[128] __attribute__((aligned(16))); 76 char stack_ptr[0]; 77 } ca; 78 pid_t pid; 79 ASSERT_THAT(pid = clone(testSetPGIDOfZombie, ca.stack_ptr, 80 CLONE_NEWPID | SIGCHLD, &ca), 81 SyscallSucceeds()); 82 83 int status; 84 ASSERT_THAT(RetryEINTR(waitpid)(pid, &status, 0), 85 SyscallSucceedsWithValue(pid)); 86 EXPECT_EQ(status, 0); 87 } 88 89 } // namespace testing 90 } // namespace gvisor