Resguardo inicial

This commit is contained in:
lastdragon
2026-09-06 12:53:21 -06:00
parent 662efe3c2a
commit ad2088d178
94853 changed files with 43200404 additions and 0 deletions
@@ -0,0 +1,5 @@
# SPDX-License-Identifier: GPL-2.0-only
clone3
clone3_clear_sighand
clone3_set_tid
clone3_cap_checkpoint_restore
@@ -0,0 +1,8 @@
# SPDX-License-Identifier: GPL-2.0
CFLAGS += -g -std=gnu99 $(KHDR_INCLUDES)
LDLIBS += -lcap
TEST_GEN_PROGS := clone3 clone3_clear_sighand clone3_set_tid \
clone3_cap_checkpoint_restore
include ../lib.mk
@@ -0,0 +1,342 @@
// SPDX-License-Identifier: GPL-2.0
/* Based on Christian Brauner's clone3() example */
#define _GNU_SOURCE
#include <errno.h>
#include <inttypes.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <stdbool.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sched.h>
#include "kselftest.h"
#include "clone3_selftests.h"
enum test_mode {
CLONE3_ARGS_NO_TEST,
CLONE3_ARGS_ALL_0,
CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG,
CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG,
CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG,
CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG,
};
static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode)
{
struct __clone_args args = {
.flags = flags,
.exit_signal = SIGCHLD,
};
struct clone_args_extended {
struct __clone_args args;
__aligned_u64 excess_space[2];
} args_ext;
pid_t pid = -1;
int status;
memset(&args_ext, 0, sizeof(args_ext));
if (size > sizeof(struct __clone_args))
args_ext.excess_space[1] = 1;
if (size == 0)
size = sizeof(struct __clone_args);
switch (test_mode) {
case CLONE3_ARGS_NO_TEST:
/*
* Uses default 'flags' and 'SIGCHLD'
* assignment.
*/
break;
case CLONE3_ARGS_ALL_0:
args.flags = 0;
args.exit_signal = 0;
break;
case CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG:
args.exit_signal = 0xbadc0ded00000000ULL;
break;
case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG:
args.exit_signal = 0x0000000080000000ULL;
break;
case CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG:
args.exit_signal = 0x0000000000000100ULL;
break;
case CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG:
args.exit_signal = 0x00000000000000f0ULL;
break;
}
memcpy(&args_ext.args, &args, sizeof(struct __clone_args));
pid = sys_clone3((struct __clone_args *)&args_ext, size);
if (pid < 0) {
ksft_print_msg("%s - Failed to create new process\n",
strerror(errno));
return -errno;
}
if (pid == 0) {
ksft_print_msg("I am the child, my PID is %d\n", getpid());
_exit(EXIT_SUCCESS);
}
ksft_print_msg("I am the parent (%d). My child's pid is %d\n",
getpid(), pid);
if (waitpid(-1, &status, __WALL) < 0) {
ksft_print_msg("waitpid() returned %s\n", strerror(errno));
return -errno;
}
if (!WIFEXITED(status)) {
ksft_print_msg("Child did not exit normally, status 0x%x\n",
status);
return EXIT_FAILURE;
}
if (WEXITSTATUS(status))
return WEXITSTATUS(status);
return 0;
}
static bool test_clone3(uint64_t flags, size_t size, int expected,
enum test_mode test_mode)
{
int ret;
ksft_print_msg(
"[%d] Trying clone3() with flags %#" PRIx64 " (size %zu)\n",
getpid(), flags, size);
ret = call_clone3(flags, size, test_mode);
ksft_print_msg("[%d] clone3() with flags says: %d expected %d\n",
getpid(), ret, expected);
if (ret != expected) {
ksft_print_msg(
"[%d] Result (%d) is different than expected (%d)\n",
getpid(), ret, expected);
return false;
}
return true;
}
typedef bool (*filter_function)(void);
typedef size_t (*size_function)(void);
static bool not_root(void)
{
if (getuid() != 0) {
ksft_print_msg("Not running as root\n");
return true;
}
return false;
}
static bool no_timenamespace(void)
{
if (not_root())
return true;
if (!access("/proc/self/ns/time", F_OK))
return false;
ksft_print_msg("Time namespaces are not supported\n");
return true;
}
static size_t page_size_plus_8(void)
{
return getpagesize() + 8;
}
struct test {
const char *name;
uint64_t flags;
size_t size;
size_function size_function;
int expected;
enum test_mode test_mode;
filter_function filter;
};
static const struct test tests[] = {
{
.name = "simple clone3()",
.flags = 0,
.size = 0,
.expected = 0,
.test_mode = CLONE3_ARGS_NO_TEST,
},
{
.name = "clone3() in a new PID_NS",
.flags = CLONE_NEWPID,
.size = 0,
.expected = 0,
.test_mode = CLONE3_ARGS_NO_TEST,
.filter = not_root,
},
{
.name = "CLONE_ARGS_SIZE_VER0",
.flags = 0,
.size = CLONE_ARGS_SIZE_VER0,
.expected = 0,
.test_mode = CLONE3_ARGS_NO_TEST,
},
{
.name = "CLONE_ARGS_SIZE_VER0 - 8",
.flags = 0,
.size = CLONE_ARGS_SIZE_VER0 - 8,
.expected = -EINVAL,
.test_mode = CLONE3_ARGS_NO_TEST,
},
{
.name = "sizeof(struct clone_args) + 8",
.flags = 0,
.size = sizeof(struct __clone_args) + 8,
.expected = 0,
.test_mode = CLONE3_ARGS_NO_TEST,
},
{
.name = "exit_signal with highest 32 bits non-zero",
.flags = 0,
.size = 0,
.expected = -EINVAL,
.test_mode = CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG,
},
{
.name = "negative 32-bit exit_signal",
.flags = 0,
.size = 0,
.expected = -EINVAL,
.test_mode = CLONE3_ARGS_INVAL_EXIT_SIGNAL_NEG,
},
{
.name = "exit_signal not fitting into CSIGNAL mask",
.flags = 0,
.size = 0,
.expected = -EINVAL,
.test_mode = CLONE3_ARGS_INVAL_EXIT_SIGNAL_CSIG,
},
{
.name = "NSIG < exit_signal < CSIG",
.flags = 0,
.size = 0,
.expected = -EINVAL,
.test_mode = CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG,
},
{
.name = "Arguments sizeof(struct clone_args) + 8",
.flags = 0,
.size = sizeof(struct __clone_args) + 8,
.expected = 0,
.test_mode = CLONE3_ARGS_ALL_0,
},
{
.name = "Arguments sizeof(struct clone_args) + 16",
.flags = 0,
.size = sizeof(struct __clone_args) + 16,
.expected = -E2BIG,
.test_mode = CLONE3_ARGS_ALL_0,
},
{
.name = "Arguments sizeof(struct clone_arg) * 2",
.flags = 0,
.size = sizeof(struct __clone_args) + 16,
.expected = -E2BIG,
.test_mode = CLONE3_ARGS_ALL_0,
},
{
.name = "Arguments > page size",
.flags = 0,
.size_function = page_size_plus_8,
.expected = -E2BIG,
.test_mode = CLONE3_ARGS_NO_TEST,
},
{
.name = "CLONE_ARGS_SIZE_VER0 in a new PID NS",
.flags = CLONE_NEWPID,
.size = CLONE_ARGS_SIZE_VER0,
.expected = 0,
.test_mode = CLONE3_ARGS_NO_TEST,
.filter = not_root,
},
{
.name = "CLONE_ARGS_SIZE_VER0 - 8 in a new PID NS",
.flags = CLONE_NEWPID,
.size = CLONE_ARGS_SIZE_VER0 - 8,
.expected = -EINVAL,
.test_mode = CLONE3_ARGS_NO_TEST,
},
{
.name = "sizeof(struct clone_args) + 8 in a new PID NS",
.flags = CLONE_NEWPID,
.size = sizeof(struct __clone_args) + 8,
.expected = 0,
.test_mode = CLONE3_ARGS_NO_TEST,
.filter = not_root,
},
{
.name = "Arguments > page size in a new PID NS",
.flags = CLONE_NEWPID,
.size_function = page_size_plus_8,
.expected = -E2BIG,
.test_mode = CLONE3_ARGS_NO_TEST,
},
{
.name = "New time NS",
.flags = CLONE_NEWTIME,
.size = 0,
.expected = 0,
.test_mode = CLONE3_ARGS_NO_TEST,
.filter = no_timenamespace,
},
{
.name = "exit signal (SIGCHLD) in flags",
.flags = SIGCHLD,
.size = 0,
.expected = -EINVAL,
.test_mode = CLONE3_ARGS_NO_TEST,
},
};
int main(int argc, char *argv[])
{
size_t size;
int i;
ksft_print_header();
ksft_set_plan(ARRAY_SIZE(tests));
test_clone3_supported();
for (i = 0; i < ARRAY_SIZE(tests); i++) {
if (tests[i].filter && tests[i].filter()) {
ksft_test_result_skip("%s\n", tests[i].name);
continue;
}
if (tests[i].size_function)
size = tests[i].size_function();
else
size = tests[i].size;
ksft_print_msg("Running test '%s'\n", tests[i].name);
ksft_test_result(test_clone3(tests[i].flags, size,
tests[i].expected,
tests[i].test_mode),
"%s\n", tests[i].name);
}
ksft_finished();
}
@@ -0,0 +1,164 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Based on Christian Brauner's clone3() example.
* These tests are assuming to be running in the host's
* PID namespace.
*/
/* capabilities related code based on selftests/bpf/test_verifier.c */
#define _GNU_SOURCE
#include <errno.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/capability.h>
#include <sys/prctl.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sched.h>
#include "kselftest_harness.h"
#include "clone3_selftests.h"
static void child_exit(int ret)
{
fflush(stdout);
fflush(stderr);
_exit(ret);
}
static int call_clone3_set_tid(struct __test_metadata *_metadata,
pid_t *set_tid, size_t set_tid_size)
{
int status;
pid_t pid = -1;
struct __clone_args args = {
.exit_signal = SIGCHLD,
.set_tid = ptr_to_u64(set_tid),
.set_tid_size = set_tid_size,
};
pid = sys_clone3(&args, sizeof(args));
if (pid < 0) {
TH_LOG("%s - Failed to create new process", strerror(errno));
return -errno;
}
if (pid == 0) {
TH_LOG("I am the child, my PID is %d (expected %d)", getpid(), set_tid[0]);
if (set_tid[0] != getpid())
child_exit(EXIT_FAILURE);
child_exit(EXIT_SUCCESS);
}
TH_LOG("I am the parent (%d). My child's pid is %d", getpid(), pid);
if (waitpid(pid, &status, 0) < 0) {
TH_LOG("Child returned %s", strerror(errno));
return -errno;
}
if (!WIFEXITED(status))
return -1;
return WEXITSTATUS(status);
}
static int test_clone3_set_tid(struct __test_metadata *_metadata,
pid_t *set_tid, size_t set_tid_size)
{
int ret;
TH_LOG("[%d] Trying clone3() with CLONE_SET_TID to %d", getpid(), set_tid[0]);
ret = call_clone3_set_tid(_metadata, set_tid, set_tid_size);
TH_LOG("[%d] clone3() with CLONE_SET_TID %d says:%d", getpid(), set_tid[0], ret);
return ret;
}
static int set_capability(void)
{
cap_value_t cap_values[] = {
CAP_SETUID, CAP_SETGID, CAP_CHECKPOINT_RESTORE
};
int ret = -1;
cap_t caps;
caps = cap_get_proc();
if (!caps) {
perror("cap_get_proc");
return -1;
}
/* Drop all capabilities */
if (cap_clear(caps)) {
perror("cap_clear");
goto out;
}
cap_set_flag(caps, CAP_EFFECTIVE, 3, cap_values, CAP_SET);
cap_set_flag(caps, CAP_PERMITTED, 3, cap_values, CAP_SET);
if (cap_set_proc(caps)) {
perror("cap_set_proc");
goto out;
}
ret = 0;
out:
if (cap_free(caps))
perror("cap_free");
return ret;
}
TEST(clone3_cap_checkpoint_restore)
{
pid_t pid;
int status;
pid_t set_tid[1];
test_clone3_supported();
EXPECT_EQ(getuid(), 0)
SKIP(return, "Skipping all tests as non-root");
memset(&set_tid, 0, sizeof(set_tid));
/* Find the current active PID */
pid = fork();
if (pid == 0) {
TH_LOG("Child has PID %d", getpid());
child_exit(EXIT_SUCCESS);
}
ASSERT_GT(waitpid(pid, &status, 0), 0)
TH_LOG("Waiting for child %d failed", pid);
/* After the child has finished, its PID should be free. */
set_tid[0] = pid;
ASSERT_EQ(set_capability(), 0)
TH_LOG("Could not set CAP_CHECKPOINT_RESTORE");
ASSERT_EQ(prctl(PR_SET_KEEPCAPS, 1, 0, 0, 0), 0);
EXPECT_EQ(setgid(65534), 0)
TH_LOG("Failed to setgid(65534)");
ASSERT_EQ(setuid(65534), 0);
set_tid[0] = pid;
/* This would fail without CAP_CHECKPOINT_RESTORE */
ASSERT_EQ(test_clone3_set_tid(_metadata, set_tid, 1), -EPERM);
ASSERT_EQ(set_capability(), 0)
TH_LOG("Could not set CAP_CHECKPOINT_RESTORE");
/* This should work as we have CAP_CHECKPOINT_RESTORE as non-root */
ASSERT_EQ(test_clone3_set_tid(_metadata, set_tid, 1), 0);
}
TEST_HARNESS_MAIN
@@ -0,0 +1,124 @@
/* SPDX-License-Identifier: GPL-2.0 */
#define _GNU_SOURCE
#include <errno.h>
#include <sched.h>
#include <signal.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <sys/syscall.h>
#include <sys/wait.h>
#include "kselftest.h"
#include "clone3_selftests.h"
static void nop_handler(int signo)
{
}
static int wait_for_pid(pid_t pid)
{
int status, ret;
again:
ret = waitpid(pid, &status, 0);
if (ret == -1) {
if (errno == EINTR)
goto again;
return -1;
}
if (!WIFEXITED(status))
return -1;
return WEXITSTATUS(status);
}
static void test_clone3_clear_sighand(void)
{
int ret;
pid_t pid;
struct __clone_args args = {};
struct sigaction act;
/*
* Check that CLONE_CLEAR_SIGHAND and CLONE_SIGHAND are mutually
* exclusive.
*/
args.flags |= CLONE_CLEAR_SIGHAND | CLONE_SIGHAND;
args.exit_signal = SIGCHLD;
pid = sys_clone3(&args, sizeof(args));
if (pid > 0)
ksft_exit_fail_msg(
"clone3(CLONE_CLEAR_SIGHAND | CLONE_SIGHAND) succeeded\n");
act.sa_handler = nop_handler;
ret = sigemptyset(&act.sa_mask);
if (ret < 0)
ksft_exit_fail_msg("%s - sigemptyset() failed\n",
strerror(errno));
act.sa_flags = 0;
/* Register signal handler for SIGUSR1 */
ret = sigaction(SIGUSR1, &act, NULL);
if (ret < 0)
ksft_exit_fail_msg(
"%s - sigaction(SIGUSR1, &act, NULL) failed\n",
strerror(errno));
/* Register signal handler for SIGUSR2 */
ret = sigaction(SIGUSR2, &act, NULL);
if (ret < 0)
ksft_exit_fail_msg(
"%s - sigaction(SIGUSR2, &act, NULL) failed\n",
strerror(errno));
/* Check that CLONE_CLEAR_SIGHAND works. */
args.flags = CLONE_CLEAR_SIGHAND;
pid = sys_clone3(&args, sizeof(args));
if (pid < 0)
ksft_exit_fail_msg("%s - clone3(CLONE_CLEAR_SIGHAND) failed\n",
strerror(errno));
if (pid == 0) {
ret = sigaction(SIGUSR1, NULL, &act);
if (ret < 0)
exit(EXIT_FAILURE);
if (act.sa_handler != SIG_DFL)
exit(EXIT_FAILURE);
ret = sigaction(SIGUSR2, NULL, &act);
if (ret < 0)
exit(EXIT_FAILURE);
if (act.sa_handler != SIG_DFL)
exit(EXIT_FAILURE);
exit(EXIT_SUCCESS);
}
ret = wait_for_pid(pid);
if (ret)
ksft_exit_fail_msg(
"Failed to clear signal handler for child process\n");
ksft_test_result_pass("Cleared signal handlers for child process\n");
}
int main(int argc, char **argv)
{
ksft_print_header();
ksft_set_plan(1);
test_clone3_supported();
test_clone3_clear_sighand();
ksft_exit_pass();
}
@@ -0,0 +1,69 @@
/* SPDX-License-Identifier: GPL-2.0 */
#ifndef _CLONE3_SELFTESTS_H
#define _CLONE3_SELFTESTS_H
#define _GNU_SOURCE
#include <sched.h>
#include <linux/sched.h>
#include <linux/types.h>
#include <stdint.h>
#include <syscall.h>
#include <sys/wait.h>
#include "kselftest.h"
#define ptr_to_u64(ptr) ((__u64)((uintptr_t)(ptr)))
#ifndef __NR_clone3
#define __NR_clone3 435
#endif
struct __clone_args {
__aligned_u64 flags;
__aligned_u64 pidfd;
__aligned_u64 child_tid;
__aligned_u64 parent_tid;
__aligned_u64 exit_signal;
__aligned_u64 stack;
__aligned_u64 stack_size;
__aligned_u64 tls;
__aligned_u64 set_tid;
__aligned_u64 set_tid_size;
__aligned_u64 cgroup;
};
static pid_t sys_clone3(struct __clone_args *args, size_t size)
{
fflush(stdout);
fflush(stderr);
return syscall(__NR_clone3, args, size);
}
static inline void test_clone3_supported(void)
{
pid_t pid;
struct __clone_args args = {};
if (__NR_clone3 < 0)
ksft_exit_skip("clone3() syscall is not supported\n");
/* Set to something that will always cause EINVAL. */
args.exit_signal = -1;
pid = sys_clone3(&args, sizeof(args));
if (!pid)
exit(EXIT_SUCCESS);
if (pid > 0) {
wait(NULL);
ksft_exit_fail_msg(
"Managed to create child process with invalid exit_signal\n");
}
if (errno == ENOSYS)
ksft_exit_skip("clone3() syscall is not supported\n");
ksft_print_msg("clone3() syscall supported\n");
}
#endif /* _CLONE3_SELFTESTS_H */
@@ -0,0 +1,418 @@
// SPDX-License-Identifier: GPL-2.0
/*
* Based on Christian Brauner's clone3() example.
* These tests are assuming to be running in the host's
* PID namespace.
*/
#define _GNU_SOURCE
#include <errno.h>
#include <linux/types.h>
#include <linux/sched.h>
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <sys/syscall.h>
#include <sys/types.h>
#include <sys/un.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sched.h>
#include "kselftest.h"
#include "clone3_selftests.h"
#define MAX_PID_NS_LEVEL 32
static int pipe_1[2];
static int pipe_2[2];
static void child_exit(int ret)
{
fflush(stdout);
fflush(stderr);
_exit(ret);
}
static int call_clone3_set_tid(pid_t *set_tid,
size_t set_tid_size,
int flags,
int expected_pid,
bool wait_for_it)
{
int status;
pid_t pid = -1;
struct __clone_args args = {
.flags = flags,
.exit_signal = SIGCHLD,
.set_tid = ptr_to_u64(set_tid),
.set_tid_size = set_tid_size,
};
pid = sys_clone3(&args, sizeof(args));
if (pid < 0) {
ksft_print_msg("%s - Failed to create new process\n",
strerror(errno));
return -errno;
}
if (pid == 0) {
int ret;
char tmp = 0;
int exit_code = EXIT_SUCCESS;
ksft_print_msg("I am the child, my PID is %d (expected %d)\n",
getpid(), set_tid[0]);
if (wait_for_it) {
ksft_print_msg("[%d] Child is ready and waiting\n",
getpid());
/* Signal the parent that the child is ready */
close(pipe_1[0]);
ret = write(pipe_1[1], &tmp, 1);
if (ret != 1) {
ksft_print_msg(
"Writing to pipe returned %d", ret);
exit_code = EXIT_FAILURE;
}
close(pipe_1[1]);
close(pipe_2[1]);
ret = read(pipe_2[0], &tmp, 1);
if (ret != 1) {
ksft_print_msg(
"Reading from pipe returned %d", ret);
exit_code = EXIT_FAILURE;
}
close(pipe_2[0]);
}
if (set_tid[0] != getpid())
child_exit(EXIT_FAILURE);
child_exit(exit_code);
}
if (expected_pid == 0 || expected_pid == pid) {
ksft_print_msg("I am the parent (%d). My child's pid is %d\n",
getpid(), pid);
} else {
ksft_print_msg(
"Expected child pid %d does not match actual pid %d\n",
expected_pid, pid);
return -1;
}
if (waitpid(pid, &status, 0) < 0) {
ksft_print_msg("Child returned %s\n", strerror(errno));
return -errno;
}
if (!WIFEXITED(status))
return -1;
return WEXITSTATUS(status);
}
static void test_clone3_set_tid(const char *desc,
pid_t *set_tid,
size_t set_tid_size,
int flags,
int expected,
int expected_pid,
bool wait_for_it)
{
int ret;
ksft_print_msg(
"[%d] Trying clone3() with CLONE_SET_TID to %d and 0x%x\n",
getpid(), set_tid[0], flags);
ret = call_clone3_set_tid(set_tid, set_tid_size, flags, expected_pid,
wait_for_it);
ksft_print_msg(
"[%d] clone3() with CLONE_SET_TID %d says: %d - expected %d\n",
getpid(), set_tid[0], ret, expected);
ksft_test_result(ret == expected, "%s with %zu TIDs and flags 0x%x\n",
desc, set_tid_size, flags);
}
int main(int argc, char *argv[])
{
FILE *f;
char buf;
char *line = NULL;
int status;
int ret = -1;
size_t len = 0;
int pid_max = 0;
uid_t uid = getuid();
char proc_path[100] = {0};
pid_t pid, ns1, ns2, ns3, ns_pid;
pid_t set_tid[MAX_PID_NS_LEVEL * 2];
ksft_print_header();
ksft_set_plan(29);
test_clone3_supported();
if (pipe(pipe_1) < 0 || pipe(pipe_2) < 0)
ksft_exit_fail_msg("pipe() failed\n");
f = fopen("/proc/sys/kernel/pid_max", "r");
if (f == NULL)
ksft_exit_fail_msg(
"%s - Could not open /proc/sys/kernel/pid_max\n",
strerror(errno));
fscanf(f, "%d", &pid_max);
fclose(f);
ksft_print_msg("/proc/sys/kernel/pid_max %d\n", pid_max);
/* Try invalid settings */
memset(&set_tid, 0, sizeof(set_tid));
test_clone3_set_tid("invalid size, 0 TID",
set_tid, MAX_PID_NS_LEVEL + 1, 0, -EINVAL, 0, 0);
test_clone3_set_tid("invalid size, 0 TID",
set_tid, MAX_PID_NS_LEVEL * 2, 0, -EINVAL, 0, 0);
test_clone3_set_tid("invalid size, 0 TID",
set_tid, MAX_PID_NS_LEVEL * 2 + 1, 0,
-EINVAL, 0, 0);
test_clone3_set_tid("invalid size, 0 TID",
set_tid, MAX_PID_NS_LEVEL * 42, 0, -EINVAL, 0, 0);
/*
* This can actually work if this test running in a MAX_PID_NS_LEVEL - 1
* nested PID namespace.
*/
test_clone3_set_tid("invalid size, 0 TID",
set_tid, MAX_PID_NS_LEVEL - 1, 0, -EINVAL, 0, 0);
memset(&set_tid, 0xff, sizeof(set_tid));
test_clone3_set_tid("invalid size, TID all 1s",
set_tid, MAX_PID_NS_LEVEL + 1, 0, -EINVAL, 0, 0);
test_clone3_set_tid("invalid size, TID all 1s",
set_tid, MAX_PID_NS_LEVEL * 2, 0, -EINVAL, 0, 0);
test_clone3_set_tid("invalid size, TID all 1s",
set_tid, MAX_PID_NS_LEVEL * 2 + 1, 0,
-EINVAL, 0, 0);
test_clone3_set_tid("invalid size, TID all 1s",
set_tid, MAX_PID_NS_LEVEL * 42, 0, -EINVAL, 0, 0);
/*
* This can actually work if this test running in a MAX_PID_NS_LEVEL - 1
* nested PID namespace.
*/
test_clone3_set_tid("invalid size, TID all 1s",
set_tid, MAX_PID_NS_LEVEL - 1, 0, -EINVAL, 0, 0);
memset(&set_tid, 0, sizeof(set_tid));
/* Try with an invalid PID */
set_tid[0] = 0;
test_clone3_set_tid("valid size, 0 TID",
set_tid, 1, 0, -EINVAL, 0, 0);
set_tid[0] = -1;
test_clone3_set_tid("valid size, -1 TID",
set_tid, 1, 0, -EINVAL, 0, 0);
/* Claim that the set_tid array actually contains 2 elements. */
test_clone3_set_tid("2 TIDs, -1 and 0",
set_tid, 2, 0, -EINVAL, 0, 0);
/* Try it in a new PID namespace */
if (uid == 0)
test_clone3_set_tid("valid size, -1 TID",
set_tid, 1, CLONE_NEWPID, -EINVAL, 0, 0);
else
ksft_test_result_skip("Clone3() with set_tid requires root\n");
/* Try with a valid PID (1) this should return -EEXIST. */
set_tid[0] = 1;
if (uid == 0)
test_clone3_set_tid("duplicate PID 1",
set_tid, 1, 0, -EEXIST, 0, 0);
else
ksft_test_result_skip("Clone3() with set_tid requires root\n");
/* Try it in a new PID namespace */
if (uid == 0)
test_clone3_set_tid("duplicate PID 1",
set_tid, 1, CLONE_NEWPID, 0, 0, 0);
else
ksft_test_result_skip("Clone3() with set_tid requires root\n");
/* pid_max should fail everywhere */
set_tid[0] = pid_max;
test_clone3_set_tid("set TID to maximum",
set_tid, 1, 0, -EINVAL, 0, 0);
if (uid == 0)
test_clone3_set_tid("set TID to maximum",
set_tid, 1, CLONE_NEWPID, -EINVAL, 0, 0);
else
ksft_test_result_skip("Clone3() with set_tid requires root\n");
if (uid != 0) {
/*
* All remaining tests require root. Tell the framework
* that all those tests are skipped as non-root.
*/
ksft_cnt.ksft_xskip += ksft_plan - ksft_test_num();
goto out;
}
/* Find the current active PID */
pid = fork();
if (pid == 0) {
ksft_print_msg("Child has PID %d\n", getpid());
child_exit(EXIT_SUCCESS);
}
if (waitpid(pid, &status, 0) < 0)
ksft_exit_fail_msg("Waiting for child %d failed", pid);
/* After the child has finished, its PID should be free. */
set_tid[0] = pid;
test_clone3_set_tid("reallocate child TID",
set_tid, 1, 0, 0, 0, 0);
/* This should fail as there is no PID 1 in that namespace */
test_clone3_set_tid("duplicate child TID",
set_tid, 1, CLONE_NEWPID, -EINVAL, 0, 0);
/*
* Creating a process with PID 1 in the newly created most nested
* PID namespace and PID 'pid' in the parent PID namespace. This
* needs to work.
*/
set_tid[0] = 1;
set_tid[1] = pid;
test_clone3_set_tid("create PID 1 in new NS",
set_tid, 2, CLONE_NEWPID, 0, pid, 0);
ksft_print_msg("unshare PID namespace\n");
if (unshare(CLONE_NEWPID) == -1)
ksft_exit_fail_msg("unshare(CLONE_NEWPID) failed: %s\n",
strerror(errno));
set_tid[0] = pid;
/* This should fail as there is no PID 1 in that namespace */
test_clone3_set_tid("duplicate PID 1",
set_tid, 1, 0, -EINVAL, 0, 0);
/* Let's create a PID 1 */
ns_pid = fork();
if (ns_pid == 0) {
/*
* This and the next test cases check that all pid-s are
* released on error paths.
*/
set_tid[0] = 43;
set_tid[1] = -1;
test_clone3_set_tid("check leak on invalid TID -1",
set_tid, 2, 0, -EINVAL, 0, 0);
set_tid[0] = 43;
set_tid[1] = pid;
test_clone3_set_tid("check leak on invalid specific TID",
set_tid, 2, 0, 0, 43, 0);
ksft_print_msg("Child in PID namespace has PID %d\n", getpid());
set_tid[0] = 2;
test_clone3_set_tid("create PID 2 in child NS",
set_tid, 1, 0, 0, 2, 0);
set_tid[0] = 1;
set_tid[1] = -1;
set_tid[2] = pid;
/* This should fail as there is invalid PID at level '1'. */
test_clone3_set_tid("fail due to invalid TID at level 1",
set_tid, 3, CLONE_NEWPID, -EINVAL, 0, 0);
set_tid[0] = 1;
set_tid[1] = 42;
set_tid[2] = pid;
/*
* This should fail as there are not enough active PID
* namespaces. Again assuming this is running in the host's
* PID namespace. Not yet nested.
*/
test_clone3_set_tid("fail due to too few active PID NSs",
set_tid, 4, CLONE_NEWPID, -EINVAL, 0, 0);
/*
* This should work and from the parent we should see
* something like 'NSpid: pid 42 1'.
*/
test_clone3_set_tid("verify that we have 3 PID NSs",
set_tid, 3, CLONE_NEWPID, 0, 42, true);
child_exit(ksft_cnt.ksft_fail);
}
close(pipe_1[1]);
close(pipe_2[0]);
while (read(pipe_1[0], &buf, 1) > 0) {
ksft_print_msg("[%d] Child is ready and waiting\n", getpid());
break;
}
snprintf(proc_path, sizeof(proc_path), "/proc/%d/status", pid);
f = fopen(proc_path, "r");
if (f == NULL)
ksft_exit_fail_msg(
"%s - Could not open %s\n",
strerror(errno), proc_path);
while (getline(&line, &len, f) != -1) {
if (strstr(line, "NSpid")) {
int i;
/* Verify that all generated PIDs are as expected. */
i = sscanf(line, "NSpid:\t%d\t%d\t%d",
&ns3, &ns2, &ns1);
if (i != 3) {
ksft_print_msg(
"Unexpected 'NSPid:' entry: %s",
line);
ns1 = ns2 = ns3 = 0;
}
break;
}
}
fclose(f);
free(line);
close(pipe_2[0]);
/* Tell the clone3()'d child to finish. */
write(pipe_2[1], &buf, 1);
close(pipe_2[1]);
if (waitpid(ns_pid, &status, 0) < 0) {
ksft_print_msg("Child returned %s\n", strerror(errno));
ret = -errno;
goto out;
}
if (!WIFEXITED(status))
ksft_test_result_fail("Child error\n");
ksft_cnt.ksft_pass += 6 - (ksft_cnt.ksft_fail - WEXITSTATUS(status));
ksft_cnt.ksft_fail = WEXITSTATUS(status);
ksft_print_msg("Expecting PIDs %d, 42, 1\n", pid);
ksft_print_msg("Have PIDs in namespaces: %d, %d, %d\n", ns3, ns2, ns1);
ksft_test_result(ns3 == pid && ns2 == 42 && ns1 == 1,
"PIDs in all namespaces as expected\n");
out:
ret = 0;
if (ret)
ksft_exit_fail();
ksft_exit_pass();
}