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