^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) /* Based on Christian Brauner's clone3() example */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) #define _GNU_SOURCE
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) #include <errno.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) #include <inttypes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <stdint.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <sys/syscall.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <sys/un.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <sys/wait.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <sched.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include "../kselftest.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include "clone3_selftests.h"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) enum test_mode {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) CLONE3_ARGS_NO_TEST,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) CLONE3_ARGS_ALL_0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) struct __clone_args args = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) .flags = flags,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) .exit_signal = SIGCHLD,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) struct clone_args_extended {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) struct __clone_args args;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) __aligned_u64 excess_space[2];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) } args_ext;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) pid_t pid = -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) int status;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) memset(&args_ext, 0, sizeof(args_ext));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) if (size > sizeof(struct __clone_args))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) args_ext.excess_space[1] = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) if (size == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) size = sizeof(struct __clone_args);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) switch (test_mode) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) case CLONE3_ARGS_NO_TEST:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * Uses default 'flags' and 'SIGCHLD'
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * assignment.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) case CLONE3_ARGS_ALL_0:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) args.flags = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) args.exit_signal = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) case CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) args.exit_signal = 0xbadc0ded00000000ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) args.exit_signal = 0x0000000080000000ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) case CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) args.exit_signal = 0x0000000000000100ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) args.exit_signal = 0x00000000000000f0ULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) memcpy(&args_ext.args, &args, sizeof(struct __clone_args));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) pid = sys_clone3((struct __clone_args *)&args_ext, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) if (pid < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) ksft_print_msg("%s - Failed to create new process\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) if (pid == 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) ksft_print_msg("I am the child, my PID is %d\n", getpid());
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) _exit(EXIT_SUCCESS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) ksft_print_msg("I am the parent (%d). My child's pid is %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) getpid(), pid);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) if (waitpid(-1, &status, __WALL) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ksft_print_msg("Child returned %s\n", strerror(errno));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) return -errno;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (WEXITSTATUS(status))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) return WEXITSTATUS(status);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) static void test_clone3(uint64_t flags, size_t size, int expected,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) enum test_mode test_mode)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) ksft_print_msg(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) "[%d] Trying clone3() with flags %#" PRIx64 " (size %zu)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) getpid(), flags, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) ret = call_clone3(flags, size, test_mode);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) ksft_print_msg("[%d] clone3() with flags says: %d expected %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) getpid(), ret, expected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) if (ret != expected)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) ksft_test_result_fail(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) "[%d] Result (%d) is different than expected (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) getpid(), ret, expected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) ksft_test_result_pass(
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) "[%d] Result (%d) matches expectation (%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) getpid(), ret, expected);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) int main(int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) uid_t uid = getuid();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) ksft_print_header();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) ksft_set_plan(17);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) test_clone3_supported();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) /* Just a simple clone3() should return 0.*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) test_clone3(0, 0, 0, CLONE3_ARGS_NO_TEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) /* Do a clone3() in a new PID NS.*/
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) if (uid == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) test_clone3(CLONE_NEWPID, 0, 0, CLONE3_ARGS_NO_TEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) /* Do a clone3() with CLONE_ARGS_SIZE_VER0. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) test_clone3(0, CLONE_ARGS_SIZE_VER0, 0, CLONE3_ARGS_NO_TEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) /* Do a clone3() with CLONE_ARGS_SIZE_VER0 - 8 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) test_clone3(0, CLONE_ARGS_SIZE_VER0 - 8, -EINVAL, CLONE3_ARGS_NO_TEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) /* Do a clone3() with sizeof(struct clone_args) + 8 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) test_clone3(0, sizeof(struct __clone_args) + 8, 0, CLONE3_ARGS_NO_TEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) /* Do a clone3() with exit_signal having highest 32 bits non-zero */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) /* Do a clone3() with negative 32-bit exit_signal */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) /* Do a clone3() with exit_signal not fitting into CSIGNAL mask */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) /* Do a clone3() with NSIG < exit_signal < CSIG */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) test_clone3(0, sizeof(struct __clone_args) + 8, 0, CLONE3_ARGS_ALL_0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) test_clone3(0, sizeof(struct __clone_args) + 16, -E2BIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) CLONE3_ARGS_ALL_0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) test_clone3(0, sizeof(struct __clone_args) * 2, -E2BIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) CLONE3_ARGS_ALL_0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) /* Do a clone3() with > page size */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) test_clone3(0, getpagesize() + 8, -E2BIG, CLONE3_ARGS_NO_TEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /* Do a clone3() with CLONE_ARGS_SIZE_VER0 in a new PID NS. */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) if (uid == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) test_clone3(CLONE_NEWPID, CLONE_ARGS_SIZE_VER0, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) CLONE3_ARGS_NO_TEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) /* Do a clone3() with CLONE_ARGS_SIZE_VER0 - 8 in a new PID NS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) test_clone3(CLONE_NEWPID, CLONE_ARGS_SIZE_VER0 - 8, -EINVAL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) CLONE3_ARGS_NO_TEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /* Do a clone3() with sizeof(struct clone_args) + 8 in a new PID NS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if (uid == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) test_clone3(CLONE_NEWPID, sizeof(struct __clone_args) + 8, 0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) CLONE3_ARGS_NO_TEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) /* Do a clone3() with > page size in a new PID NS */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) test_clone3(CLONE_NEWPID, getpagesize() + 8, -E2BIG,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) CLONE3_ARGS_NO_TEST);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return !ksft_get_fail_cnt() ? ksft_exit_pass() : ksft_exit_fail();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) }