Path: blob/master/tools/testing/selftests/clone3/clone3_set_tid.c
26285 views
// SPDX-License-Identifier: GPL-2.012/*3* Based on Christian Brauner's clone3() example.4* These tests are assuming to be running in the host's5* PID namespace.6*/78#define _GNU_SOURCE9#include <errno.h>10#include <linux/types.h>11#include <linux/sched.h>12#include <stdio.h>13#include <stdlib.h>14#include <stdbool.h>15#include <sys/syscall.h>16#include <sys/types.h>17#include <sys/un.h>18#include <sys/wait.h>19#include <unistd.h>20#include <sched.h>2122#include "../kselftest.h"23#include "clone3_selftests.h"2425#define MAX_PID_NS_LEVEL 322627static int pipe_1[2];28static int pipe_2[2];2930static void child_exit(int ret)31{32fflush(stdout);33fflush(stderr);34_exit(ret);35}3637static int call_clone3_set_tid(pid_t *set_tid,38size_t set_tid_size,39int flags,40int expected_pid,41bool wait_for_it)42{43int status;44pid_t pid = -1;4546struct __clone_args args = {47.flags = flags,48.exit_signal = SIGCHLD,49.set_tid = ptr_to_u64(set_tid),50.set_tid_size = set_tid_size,51};5253pid = sys_clone3(&args, sizeof(args));54if (pid < 0) {55ksft_print_msg("%s - Failed to create new process\n",56strerror(errno));57return -errno;58}5960if (pid == 0) {61int ret;62char tmp = 0;63int exit_code = EXIT_SUCCESS;6465ksft_print_msg("I am the child, my PID is %d (expected %d)\n",66getpid(), set_tid[0]);67if (wait_for_it) {68ksft_print_msg("[%d] Child is ready and waiting\n",69getpid());7071/* Signal the parent that the child is ready */72close(pipe_1[0]);73ret = write(pipe_1[1], &tmp, 1);74if (ret != 1) {75ksft_print_msg(76"Writing to pipe returned %d", ret);77exit_code = EXIT_FAILURE;78}79close(pipe_1[1]);80close(pipe_2[1]);81ret = read(pipe_2[0], &tmp, 1);82if (ret != 1) {83ksft_print_msg(84"Reading from pipe returned %d", ret);85exit_code = EXIT_FAILURE;86}87close(pipe_2[0]);88}8990if (set_tid[0] != getpid())91child_exit(EXIT_FAILURE);92child_exit(exit_code);93}9495if (expected_pid == 0 || expected_pid == pid) {96ksft_print_msg("I am the parent (%d). My child's pid is %d\n",97getpid(), pid);98} else {99ksft_print_msg(100"Expected child pid %d does not match actual pid %d\n",101expected_pid, pid);102return -1;103}104105if (waitpid(pid, &status, 0) < 0) {106ksft_print_msg("Child returned %s\n", strerror(errno));107return -errno;108}109110if (!WIFEXITED(status))111return -1;112113return WEXITSTATUS(status);114}115116static void test_clone3_set_tid(const char *desc,117pid_t *set_tid,118size_t set_tid_size,119int flags,120int expected,121int expected_pid,122bool wait_for_it)123{124int ret;125126ksft_print_msg(127"[%d] Trying clone3() with CLONE_SET_TID to %d and 0x%x\n",128getpid(), set_tid[0], flags);129ret = call_clone3_set_tid(set_tid, set_tid_size, flags, expected_pid,130wait_for_it);131ksft_print_msg(132"[%d] clone3() with CLONE_SET_TID %d says: %d - expected %d\n",133getpid(), set_tid[0], ret, expected);134135ksft_test_result(ret == expected, "%s with %zu TIDs and flags 0x%x\n",136desc, set_tid_size, flags);137}138139int main(int argc, char *argv[])140{141FILE *f;142char buf;143char *line;144int status;145int ret = -1;146size_t len = 0;147int pid_max = 0;148uid_t uid = getuid();149char proc_path[100] = {0};150pid_t pid, ns1, ns2, ns3, ns_pid;151pid_t set_tid[MAX_PID_NS_LEVEL * 2];152153ksft_print_header();154ksft_set_plan(29);155test_clone3_supported();156157if (pipe(pipe_1) < 0 || pipe(pipe_2) < 0)158ksft_exit_fail_msg("pipe() failed\n");159160f = fopen("/proc/sys/kernel/pid_max", "r");161if (f == NULL)162ksft_exit_fail_msg(163"%s - Could not open /proc/sys/kernel/pid_max\n",164strerror(errno));165fscanf(f, "%d", &pid_max);166fclose(f);167ksft_print_msg("/proc/sys/kernel/pid_max %d\n", pid_max);168169/* Try invalid settings */170memset(&set_tid, 0, sizeof(set_tid));171test_clone3_set_tid("invalid size, 0 TID",172set_tid, MAX_PID_NS_LEVEL + 1, 0, -EINVAL, 0, 0);173174test_clone3_set_tid("invalid size, 0 TID",175set_tid, MAX_PID_NS_LEVEL * 2, 0, -EINVAL, 0, 0);176177test_clone3_set_tid("invalid size, 0 TID",178set_tid, MAX_PID_NS_LEVEL * 2 + 1, 0,179-EINVAL, 0, 0);180181test_clone3_set_tid("invalid size, 0 TID",182set_tid, MAX_PID_NS_LEVEL * 42, 0, -EINVAL, 0, 0);183184/*185* This can actually work if this test running in a MAX_PID_NS_LEVEL - 1186* nested PID namespace.187*/188test_clone3_set_tid("invalid size, 0 TID",189set_tid, MAX_PID_NS_LEVEL - 1, 0, -EINVAL, 0, 0);190191memset(&set_tid, 0xff, sizeof(set_tid));192test_clone3_set_tid("invalid size, TID all 1s",193set_tid, MAX_PID_NS_LEVEL + 1, 0, -EINVAL, 0, 0);194195test_clone3_set_tid("invalid size, TID all 1s",196set_tid, MAX_PID_NS_LEVEL * 2, 0, -EINVAL, 0, 0);197198test_clone3_set_tid("invalid size, TID all 1s",199set_tid, MAX_PID_NS_LEVEL * 2 + 1, 0,200-EINVAL, 0, 0);201202test_clone3_set_tid("invalid size, TID all 1s",203set_tid, MAX_PID_NS_LEVEL * 42, 0, -EINVAL, 0, 0);204205/*206* This can actually work if this test running in a MAX_PID_NS_LEVEL - 1207* nested PID namespace.208*/209test_clone3_set_tid("invalid size, TID all 1s",210set_tid, MAX_PID_NS_LEVEL - 1, 0, -EINVAL, 0, 0);211212memset(&set_tid, 0, sizeof(set_tid));213/* Try with an invalid PID */214set_tid[0] = 0;215test_clone3_set_tid("valid size, 0 TID",216set_tid, 1, 0, -EINVAL, 0, 0);217218set_tid[0] = -1;219test_clone3_set_tid("valid size, -1 TID",220set_tid, 1, 0, -EINVAL, 0, 0);221222/* Claim that the set_tid array actually contains 2 elements. */223test_clone3_set_tid("2 TIDs, -1 and 0",224set_tid, 2, 0, -EINVAL, 0, 0);225226/* Try it in a new PID namespace */227if (uid == 0)228test_clone3_set_tid("valid size, -1 TID",229set_tid, 1, CLONE_NEWPID, -EINVAL, 0, 0);230else231ksft_test_result_skip("Clone3() with set_tid requires root\n");232233/* Try with a valid PID (1) this should return -EEXIST. */234set_tid[0] = 1;235if (uid == 0)236test_clone3_set_tid("duplicate PID 1",237set_tid, 1, 0, -EEXIST, 0, 0);238else239ksft_test_result_skip("Clone3() with set_tid requires root\n");240241/* Try it in a new PID namespace */242if (uid == 0)243test_clone3_set_tid("duplicate PID 1",244set_tid, 1, CLONE_NEWPID, 0, 0, 0);245else246ksft_test_result_skip("Clone3() with set_tid requires root\n");247248/* pid_max should fail everywhere */249set_tid[0] = pid_max;250test_clone3_set_tid("set TID to maximum",251set_tid, 1, 0, -EINVAL, 0, 0);252253if (uid == 0)254test_clone3_set_tid("set TID to maximum",255set_tid, 1, CLONE_NEWPID, -EINVAL, 0, 0);256else257ksft_test_result_skip("Clone3() with set_tid requires root\n");258259if (uid != 0) {260/*261* All remaining tests require root. Tell the framework262* that all those tests are skipped as non-root.263*/264ksft_cnt.ksft_xskip += ksft_plan - ksft_test_num();265goto out;266}267268/* Find the current active PID */269pid = fork();270if (pid == 0) {271ksft_print_msg("Child has PID %d\n", getpid());272child_exit(EXIT_SUCCESS);273}274if (waitpid(pid, &status, 0) < 0)275ksft_exit_fail_msg("Waiting for child %d failed", pid);276277/* After the child has finished, its PID should be free. */278set_tid[0] = pid;279test_clone3_set_tid("reallocate child TID",280set_tid, 1, 0, 0, 0, 0);281282/* This should fail as there is no PID 1 in that namespace */283test_clone3_set_tid("duplicate child TID",284set_tid, 1, CLONE_NEWPID, -EINVAL, 0, 0);285286/*287* Creating a process with PID 1 in the newly created most nested288* PID namespace and PID 'pid' in the parent PID namespace. This289* needs to work.290*/291set_tid[0] = 1;292set_tid[1] = pid;293test_clone3_set_tid("create PID 1 in new NS",294set_tid, 2, CLONE_NEWPID, 0, pid, 0);295296ksft_print_msg("unshare PID namespace\n");297if (unshare(CLONE_NEWPID) == -1)298ksft_exit_fail_msg("unshare(CLONE_NEWPID) failed: %s\n",299strerror(errno));300301set_tid[0] = pid;302303/* This should fail as there is no PID 1 in that namespace */304test_clone3_set_tid("duplicate PID 1",305set_tid, 1, 0, -EINVAL, 0, 0);306307/* Let's create a PID 1 */308ns_pid = fork();309if (ns_pid == 0) {310/*311* This and the next test cases check that all pid-s are312* released on error paths.313*/314set_tid[0] = 43;315set_tid[1] = -1;316test_clone3_set_tid("check leak on invalid TID -1",317set_tid, 2, 0, -EINVAL, 0, 0);318319set_tid[0] = 43;320set_tid[1] = pid;321test_clone3_set_tid("check leak on invalid specific TID",322set_tid, 2, 0, 0, 43, 0);323324ksft_print_msg("Child in PID namespace has PID %d\n", getpid());325set_tid[0] = 2;326test_clone3_set_tid("create PID 2 in child NS",327set_tid, 1, 0, 0, 2, 0);328329set_tid[0] = 1;330set_tid[1] = -1;331set_tid[2] = pid;332/* This should fail as there is invalid PID at level '1'. */333test_clone3_set_tid("fail due to invalid TID at level 1",334set_tid, 3, CLONE_NEWPID, -EINVAL, 0, 0);335336set_tid[0] = 1;337set_tid[1] = 42;338set_tid[2] = pid;339/*340* This should fail as there are not enough active PID341* namespaces. Again assuming this is running in the host's342* PID namespace. Not yet nested.343*/344test_clone3_set_tid("fail due to too few active PID NSs",345set_tid, 4, CLONE_NEWPID, -EINVAL, 0, 0);346347/*348* This should work and from the parent we should see349* something like 'NSpid: pid 42 1'.350*/351test_clone3_set_tid("verify that we have 3 PID NSs",352set_tid, 3, CLONE_NEWPID, 0, 42, true);353354child_exit(ksft_cnt.ksft_fail);355}356357close(pipe_1[1]);358close(pipe_2[0]);359while (read(pipe_1[0], &buf, 1) > 0) {360ksft_print_msg("[%d] Child is ready and waiting\n", getpid());361break;362}363364snprintf(proc_path, sizeof(proc_path), "/proc/%d/status", pid);365f = fopen(proc_path, "r");366if (f == NULL)367ksft_exit_fail_msg(368"%s - Could not open %s\n",369strerror(errno), proc_path);370371while (getline(&line, &len, f) != -1) {372if (strstr(line, "NSpid")) {373int i;374375/* Verify that all generated PIDs are as expected. */376i = sscanf(line, "NSpid:\t%d\t%d\t%d",377&ns3, &ns2, &ns1);378if (i != 3) {379ksft_print_msg(380"Unexpected 'NSPid:' entry: %s",381line);382ns1 = ns2 = ns3 = 0;383}384break;385}386}387fclose(f);388free(line);389close(pipe_2[0]);390391/* Tell the clone3()'d child to finish. */392write(pipe_2[1], &buf, 1);393close(pipe_2[1]);394395if (waitpid(ns_pid, &status, 0) < 0) {396ksft_print_msg("Child returned %s\n", strerror(errno));397ret = -errno;398goto out;399}400401if (!WIFEXITED(status))402ksft_test_result_fail("Child error\n");403404ksft_cnt.ksft_pass += 6 - (ksft_cnt.ksft_fail - WEXITSTATUS(status));405ksft_cnt.ksft_fail = WEXITSTATUS(status);406407ksft_print_msg("Expecting PIDs %d, 42, 1\n", pid);408ksft_print_msg("Have PIDs in namespaces: %d, %d, %d\n", ns3, ns2, ns1);409ksft_test_result(ns3 == pid && ns2 == 42 && ns1 == 1,410"PIDs in all namespaces as expected\n");411out:412ret = 0;413414if (ret)415ksft_exit_fail();416ksft_exit_pass();417}418419420