1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3 * Copyright (c) 2020 Viresh Kumar <viresh.kumar@linaro.org>
4 */
5
6 /*\
7 * [Description]
8 *
9 * Basic clone3() test to check various failures.
10 */
11
12 #define _GNU_SOURCE
13
14 #include <stdlib.h>
15
16 #include "tst_test.h"
17 #include "lapi/clone.h"
18
19 static struct clone_args *valid_args, *invalid_args;
20 unsigned long stack;
21 static int *invalid_address;
22
23 static struct tcase {
24 const char *name;
25 struct clone_args **args;
26 size_t size;
27 uint64_t flags;
28 int **pidfd;
29 int exit_signal;
30 unsigned long stack;
31 unsigned long stack_size;
32 unsigned long tls;
33 int exp_errno;
34 } tcases[] = {
35 {"invalid args", &invalid_args, sizeof(*valid_args), 0, NULL, SIGCHLD, 0, 0, 0, EFAULT},
36 {"zero size", &valid_args, 0, 0, NULL, SIGCHLD, 0, 0, 0, EINVAL},
37 {"short size", &valid_args, sizeof(*valid_args) - 1, 0, NULL, SIGCHLD, 0, 0, 0, EINVAL},
38 {"extra size", &valid_args, sizeof(*valid_args) + 1, 0, NULL, SIGCHLD, 0, 0, 0, EFAULT},
39 {"sighand-no-VM", &valid_args, sizeof(*valid_args), CLONE_SIGHAND, NULL, SIGCHLD, 0, 0, 0, EINVAL},
40 {"thread-no-sighand", &valid_args, sizeof(*valid_args), CLONE_THREAD, NULL, SIGCHLD, 0, 0, 0, EINVAL},
41 {"fs-newns", &valid_args, sizeof(*valid_args), CLONE_FS | CLONE_NEWNS, NULL, SIGCHLD, 0, 0, 0, EINVAL},
42 {"invalid pidfd", &valid_args, sizeof(*valid_args), CLONE_PIDFD, &invalid_address, SIGCHLD, 0, 0, 0, EFAULT},
43 {"invalid signal", &valid_args, sizeof(*valid_args), 0, NULL, CSIGNAL + 1, 0, 0, 0, EINVAL},
44 {"zero-stack-size", &valid_args, sizeof(*valid_args), 0, NULL, SIGCHLD, (unsigned long)&stack, 0, 0, EINVAL},
45 {"invalid-stack", &valid_args, sizeof(*valid_args), 0, NULL, SIGCHLD, 0, 4, 0, EINVAL},
46 /*
47 * Don't test CLONE_CHILD_SETTID and CLONE_PARENT_SETTID:
48 * When the parent tid is written to the memory location for
49 * CLONE_PARENT_SETTID we're past the point of no return of process
50 * creation, i.e. the return value from put_user() isn't checked and
51 * can't be checked anymore so you'd never receive EFAULT for a bogus
52 * parent_tid memory address.
53 *
54 * https://lore.kernel.org/linux-m68k/20200627122332.ki2otaiw3v7wndbl@wittgenstein/T/#u
55 */
56 };
57
setup(void)58 static void setup(void)
59 {
60 clone3_supported_by_kernel();
61
62 void *p = tst_get_bad_addr(NULL);
63
64 invalid_args = p;
65 invalid_address = p;
66 }
67
run(unsigned int n)68 static void run(unsigned int n)
69 {
70 struct tcase *tc = &tcases[n];
71 struct clone_args *args = *tc->args;
72
73 if (args == valid_args) {
74 args->flags = tc->flags;
75 if (tc->pidfd)
76 args->pidfd = (uint64_t)(*tc->pidfd);
77 else
78 args->pidfd = 0;
79 args->exit_signal = tc->exit_signal;
80 args->stack = tc->stack;
81 args->stack_size = tc->stack_size;
82 args->tls = tc->tls;
83 }
84
85 TEST(clone3(args, tc->size));
86
87 if (!TST_RET)
88 exit(EXIT_SUCCESS);
89
90 if (TST_RET >= 0) {
91 tst_res(TFAIL, "%s: clone3() passed unexpectedly", tc->name);
92 return;
93 }
94
95 if (tc->exp_errno != TST_ERR) {
96 tst_res(TFAIL | TTERRNO, "%s: clone3() should fail with %s",
97 tc->name, tst_strerrno(tc->exp_errno));
98 return;
99 }
100
101 tst_res(TPASS | TTERRNO, "%s: clone3() failed as expected", tc->name);
102 }
103
104 static struct tst_test test = {
105 .tcnt = ARRAY_SIZE(tcases),
106 .test = run,
107 .setup = setup,
108 .needs_tmpdir = 1,
109 .bufs = (struct tst_buffers []) {
110 {&valid_args, .size = sizeof(*valid_args)},
111 {},
112 }
113 };
114