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