clone3.c 5.3 KB

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