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