selftests/clone3: Avoid OS-defined clone_args
As the UAPI headers start to appear in distros, we need to avoid outdated versions of struct clone_args to be able to test modern features, named "struct __clone_args". Additionally update the struct size macro names to match UAPI names. Acked-by: Christian Brauner <christian.brauner@ubuntu.com> Link: https://lore.kernel.org/lkml/20200921075432.u4gis3s2o5qrsb5g@wittgenstein/ Signed-off-by: Kees Cook <keescook@chromium.org>
This commit is contained in:
parent
a39caac02f
commit
e953aeaa91
|
@ -20,13 +20,6 @@
|
||||||
#include "../kselftest.h"
|
#include "../kselftest.h"
|
||||||
#include "clone3_selftests.h"
|
#include "clone3_selftests.h"
|
||||||
|
|
||||||
/*
|
|
||||||
* Different sizes of struct clone_args
|
|
||||||
*/
|
|
||||||
#ifndef CLONE3_ARGS_SIZE_V0
|
|
||||||
#define CLONE3_ARGS_SIZE_V0 64
|
|
||||||
#endif
|
|
||||||
|
|
||||||
enum test_mode {
|
enum test_mode {
|
||||||
CLONE3_ARGS_NO_TEST,
|
CLONE3_ARGS_NO_TEST,
|
||||||
CLONE3_ARGS_ALL_0,
|
CLONE3_ARGS_ALL_0,
|
||||||
|
@ -38,13 +31,13 @@ enum test_mode {
|
||||||
|
|
||||||
static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode)
|
static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode)
|
||||||
{
|
{
|
||||||
struct clone_args args = {
|
struct __clone_args args = {
|
||||||
.flags = flags,
|
.flags = flags,
|
||||||
.exit_signal = SIGCHLD,
|
.exit_signal = SIGCHLD,
|
||||||
};
|
};
|
||||||
|
|
||||||
struct clone_args_extended {
|
struct clone_args_extended {
|
||||||
struct clone_args args;
|
struct __clone_args args;
|
||||||
__aligned_u64 excess_space[2];
|
__aligned_u64 excess_space[2];
|
||||||
} args_ext;
|
} args_ext;
|
||||||
|
|
||||||
|
@ -52,11 +45,11 @@ static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode)
|
||||||
int status;
|
int status;
|
||||||
|
|
||||||
memset(&args_ext, 0, sizeof(args_ext));
|
memset(&args_ext, 0, sizeof(args_ext));
|
||||||
if (size > sizeof(struct clone_args))
|
if (size > sizeof(struct __clone_args))
|
||||||
args_ext.excess_space[1] = 1;
|
args_ext.excess_space[1] = 1;
|
||||||
|
|
||||||
if (size == 0)
|
if (size == 0)
|
||||||
size = sizeof(struct clone_args);
|
size = sizeof(struct __clone_args);
|
||||||
|
|
||||||
switch (test_mode) {
|
switch (test_mode) {
|
||||||
case CLONE3_ARGS_ALL_0:
|
case CLONE3_ARGS_ALL_0:
|
||||||
|
@ -77,9 +70,9 @@ static int call_clone3(uint64_t flags, size_t size, enum test_mode test_mode)
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
|
||||||
memcpy(&args_ext.args, &args, sizeof(struct clone_args));
|
memcpy(&args_ext.args, &args, sizeof(struct __clone_args));
|
||||||
|
|
||||||
pid = sys_clone3((struct clone_args *)&args_ext, size);
|
pid = sys_clone3((struct __clone_args *)&args_ext, size);
|
||||||
if (pid < 0) {
|
if (pid < 0) {
|
||||||
ksft_print_msg("%s - Failed to create new process\n",
|
ksft_print_msg("%s - Failed to create new process\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
|
@ -144,14 +137,14 @@ int main(int argc, char *argv[])
|
||||||
else
|
else
|
||||||
ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
|
ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
|
||||||
|
|
||||||
/* Do a clone3() with CLONE3_ARGS_SIZE_V0. */
|
/* Do a clone3() with CLONE_ARGS_SIZE_VER0. */
|
||||||
test_clone3(0, CLONE3_ARGS_SIZE_V0, 0, CLONE3_ARGS_NO_TEST);
|
test_clone3(0, CLONE_ARGS_SIZE_VER0, 0, CLONE3_ARGS_NO_TEST);
|
||||||
|
|
||||||
/* Do a clone3() with CLONE3_ARGS_SIZE_V0 - 8 */
|
/* Do a clone3() with CLONE_ARGS_SIZE_VER0 - 8 */
|
||||||
test_clone3(0, CLONE3_ARGS_SIZE_V0 - 8, -EINVAL, CLONE3_ARGS_NO_TEST);
|
test_clone3(0, CLONE_ARGS_SIZE_VER0 - 8, -EINVAL, CLONE3_ARGS_NO_TEST);
|
||||||
|
|
||||||
/* Do a clone3() with sizeof(struct clone_args) + 8 */
|
/* Do a clone3() with sizeof(struct clone_args) + 8 */
|
||||||
test_clone3(0, sizeof(struct clone_args) + 8, 0, CLONE3_ARGS_NO_TEST);
|
test_clone3(0, sizeof(struct __clone_args) + 8, 0, CLONE3_ARGS_NO_TEST);
|
||||||
|
|
||||||
/* Do a clone3() with exit_signal having highest 32 bits non-zero */
|
/* Do a clone3() with exit_signal having highest 32 bits non-zero */
|
||||||
test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG);
|
test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_BIG);
|
||||||
|
@ -165,31 +158,31 @@ int main(int argc, char *argv[])
|
||||||
/* Do a clone3() with NSIG < exit_signal < CSIG */
|
/* Do a clone3() with NSIG < exit_signal < CSIG */
|
||||||
test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG);
|
test_clone3(0, 0, -EINVAL, CLONE3_ARGS_INVAL_EXIT_SIGNAL_NSIG);
|
||||||
|
|
||||||
test_clone3(0, sizeof(struct clone_args) + 8, 0, CLONE3_ARGS_ALL_0);
|
test_clone3(0, sizeof(struct __clone_args) + 8, 0, CLONE3_ARGS_ALL_0);
|
||||||
|
|
||||||
test_clone3(0, sizeof(struct clone_args) + 16, -E2BIG,
|
test_clone3(0, sizeof(struct __clone_args) + 16, -E2BIG,
|
||||||
CLONE3_ARGS_ALL_0);
|
CLONE3_ARGS_ALL_0);
|
||||||
|
|
||||||
test_clone3(0, sizeof(struct clone_args) * 2, -E2BIG,
|
test_clone3(0, sizeof(struct __clone_args) * 2, -E2BIG,
|
||||||
CLONE3_ARGS_ALL_0);
|
CLONE3_ARGS_ALL_0);
|
||||||
|
|
||||||
/* Do a clone3() with > page size */
|
/* Do a clone3() with > page size */
|
||||||
test_clone3(0, getpagesize() + 8, -E2BIG, CLONE3_ARGS_NO_TEST);
|
test_clone3(0, getpagesize() + 8, -E2BIG, CLONE3_ARGS_NO_TEST);
|
||||||
|
|
||||||
/* Do a clone3() with CLONE3_ARGS_SIZE_V0 in a new PID NS. */
|
/* Do a clone3() with CLONE_ARGS_SIZE_VER0 in a new PID NS. */
|
||||||
if (uid == 0)
|
if (uid == 0)
|
||||||
test_clone3(CLONE_NEWPID, CLONE3_ARGS_SIZE_V0, 0,
|
test_clone3(CLONE_NEWPID, CLONE_ARGS_SIZE_VER0, 0,
|
||||||
CLONE3_ARGS_NO_TEST);
|
CLONE3_ARGS_NO_TEST);
|
||||||
else
|
else
|
||||||
ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
|
ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
|
||||||
|
|
||||||
/* Do a clone3() with CLONE3_ARGS_SIZE_V0 - 8 in a new PID NS */
|
/* Do a clone3() with CLONE_ARGS_SIZE_VER0 - 8 in a new PID NS */
|
||||||
test_clone3(CLONE_NEWPID, CLONE3_ARGS_SIZE_V0 - 8, -EINVAL,
|
test_clone3(CLONE_NEWPID, CLONE_ARGS_SIZE_VER0 - 8, -EINVAL,
|
||||||
CLONE3_ARGS_NO_TEST);
|
CLONE3_ARGS_NO_TEST);
|
||||||
|
|
||||||
/* Do a clone3() with sizeof(struct clone_args) + 8 in a new PID NS */
|
/* Do a clone3() with sizeof(struct clone_args) + 8 in a new PID NS */
|
||||||
if (uid == 0)
|
if (uid == 0)
|
||||||
test_clone3(CLONE_NEWPID, sizeof(struct clone_args) + 8, 0,
|
test_clone3(CLONE_NEWPID, sizeof(struct __clone_args) + 8, 0,
|
||||||
CLONE3_ARGS_NO_TEST);
|
CLONE3_ARGS_NO_TEST);
|
||||||
else
|
else
|
||||||
ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
|
ksft_test_result_skip("Skipping clone3() with CLONE_NEWPID\n");
|
||||||
|
|
|
@ -44,13 +44,13 @@ static int call_clone3_set_tid(struct __test_metadata *_metadata,
|
||||||
int status;
|
int status;
|
||||||
pid_t pid = -1;
|
pid_t pid = -1;
|
||||||
|
|
||||||
struct clone_args args = {
|
struct __clone_args args = {
|
||||||
.exit_signal = SIGCHLD,
|
.exit_signal = SIGCHLD,
|
||||||
.set_tid = ptr_to_u64(set_tid),
|
.set_tid = ptr_to_u64(set_tid),
|
||||||
.set_tid_size = set_tid_size,
|
.set_tid_size = set_tid_size,
|
||||||
};
|
};
|
||||||
|
|
||||||
pid = sys_clone3(&args, sizeof(struct clone_args));
|
pid = sys_clone3(&args, sizeof(args));
|
||||||
if (pid < 0) {
|
if (pid < 0) {
|
||||||
TH_LOG("%s - Failed to create new process", strerror(errno));
|
TH_LOG("%s - Failed to create new process", strerror(errno));
|
||||||
return -errno;
|
return -errno;
|
||||||
|
|
|
@ -47,7 +47,7 @@ static void test_clone3_clear_sighand(void)
|
||||||
{
|
{
|
||||||
int ret;
|
int ret;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
struct clone_args args = {};
|
struct __clone_args args = {};
|
||||||
struct sigaction act;
|
struct sigaction act;
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|
|
@ -19,13 +19,11 @@
|
||||||
#define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup given the right permissions. */
|
#define CLONE_INTO_CGROUP 0x200000000ULL /* Clone into a specific cgroup given the right permissions. */
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef CLONE_ARGS_SIZE_VER0
|
|
||||||
#define CLONE_ARGS_SIZE_VER0 64
|
|
||||||
#endif
|
|
||||||
|
|
||||||
#ifndef __NR_clone3
|
#ifndef __NR_clone3
|
||||||
#define __NR_clone3 -1
|
#define __NR_clone3 -1
|
||||||
struct clone_args {
|
#endif
|
||||||
|
|
||||||
|
struct __clone_args {
|
||||||
__aligned_u64 flags;
|
__aligned_u64 flags;
|
||||||
__aligned_u64 pidfd;
|
__aligned_u64 pidfd;
|
||||||
__aligned_u64 child_tid;
|
__aligned_u64 child_tid;
|
||||||
|
@ -34,15 +32,21 @@ struct clone_args {
|
||||||
__aligned_u64 stack;
|
__aligned_u64 stack;
|
||||||
__aligned_u64 stack_size;
|
__aligned_u64 stack_size;
|
||||||
__aligned_u64 tls;
|
__aligned_u64 tls;
|
||||||
#define CLONE_ARGS_SIZE_VER1 80
|
#ifndef CLONE_ARGS_SIZE_VER0
|
||||||
|
#define CLONE_ARGS_SIZE_VER0 64 /* sizeof first published struct */
|
||||||
|
#endif
|
||||||
__aligned_u64 set_tid;
|
__aligned_u64 set_tid;
|
||||||
__aligned_u64 set_tid_size;
|
__aligned_u64 set_tid_size;
|
||||||
#define CLONE_ARGS_SIZE_VER2 88
|
#ifndef CLONE_ARGS_SIZE_VER1
|
||||||
|
#define CLONE_ARGS_SIZE_VER1 80 /* sizeof second published struct */
|
||||||
|
#endif
|
||||||
__aligned_u64 cgroup;
|
__aligned_u64 cgroup;
|
||||||
|
#ifndef CLONE_ARGS_SIZE_VER2
|
||||||
|
#define CLONE_ARGS_SIZE_VER2 88 /* sizeof third published struct */
|
||||||
|
#endif
|
||||||
};
|
};
|
||||||
#endif /* __NR_clone3 */
|
|
||||||
|
|
||||||
static pid_t sys_clone3(struct clone_args *args, size_t size)
|
static pid_t sys_clone3(struct __clone_args *args, size_t size)
|
||||||
{
|
{
|
||||||
fflush(stdout);
|
fflush(stdout);
|
||||||
fflush(stderr);
|
fflush(stderr);
|
||||||
|
@ -52,7 +56,7 @@ static pid_t sys_clone3(struct clone_args *args, size_t size)
|
||||||
static inline void test_clone3_supported(void)
|
static inline void test_clone3_supported(void)
|
||||||
{
|
{
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
struct clone_args args = {};
|
struct __clone_args args = {};
|
||||||
|
|
||||||
if (__NR_clone3 < 0)
|
if (__NR_clone3 < 0)
|
||||||
ksft_exit_skip("clone3() syscall is not supported\n");
|
ksft_exit_skip("clone3() syscall is not supported\n");
|
||||||
|
|
|
@ -46,14 +46,14 @@ static int call_clone3_set_tid(pid_t *set_tid,
|
||||||
int status;
|
int status;
|
||||||
pid_t pid = -1;
|
pid_t pid = -1;
|
||||||
|
|
||||||
struct clone_args args = {
|
struct __clone_args args = {
|
||||||
.flags = flags,
|
.flags = flags,
|
||||||
.exit_signal = SIGCHLD,
|
.exit_signal = SIGCHLD,
|
||||||
.set_tid = ptr_to_u64(set_tid),
|
.set_tid = ptr_to_u64(set_tid),
|
||||||
.set_tid_size = set_tid_size,
|
.set_tid_size = set_tid_size,
|
||||||
};
|
};
|
||||||
|
|
||||||
pid = sys_clone3(&args, sizeof(struct clone_args));
|
pid = sys_clone3(&args, sizeof(args));
|
||||||
if (pid < 0) {
|
if (pid < 0) {
|
||||||
ksft_print_msg("%s - Failed to create new process\n",
|
ksft_print_msg("%s - Failed to create new process\n",
|
||||||
strerror(errno));
|
strerror(errno));
|
||||||
|
|
|
@ -75,7 +75,7 @@ static int sys_waitid(int which, pid_t pid, int options)
|
||||||
|
|
||||||
pid_t create_child(int *pidfd, unsigned flags)
|
pid_t create_child(int *pidfd, unsigned flags)
|
||||||
{
|
{
|
||||||
struct clone_args args = {
|
struct __clone_args args = {
|
||||||
.flags = CLONE_PIDFD | flags,
|
.flags = CLONE_PIDFD | flags,
|
||||||
.exit_signal = SIGCHLD,
|
.exit_signal = SIGCHLD,
|
||||||
.pidfd = ptr_to_u64(pidfd),
|
.pidfd = ptr_to_u64(pidfd),
|
||||||
|
|
|
@ -3817,7 +3817,7 @@ TEST(user_notification_filter_empty)
|
||||||
long ret;
|
long ret;
|
||||||
int status;
|
int status;
|
||||||
struct pollfd pollfd;
|
struct pollfd pollfd;
|
||||||
struct clone_args args = {
|
struct __clone_args args = {
|
||||||
.flags = CLONE_FILES,
|
.flags = CLONE_FILES,
|
||||||
.exit_signal = SIGCHLD,
|
.exit_signal = SIGCHLD,
|
||||||
};
|
};
|
||||||
|
@ -3871,7 +3871,7 @@ TEST(user_notification_filter_empty_threaded)
|
||||||
long ret;
|
long ret;
|
||||||
int status;
|
int status;
|
||||||
struct pollfd pollfd;
|
struct pollfd pollfd;
|
||||||
struct clone_args args = {
|
struct __clone_args args = {
|
||||||
.flags = CLONE_FILES,
|
.flags = CLONE_FILES,
|
||||||
.exit_signal = SIGCHLD,
|
.exit_signal = SIGCHLD,
|
||||||
};
|
};
|
||||||
|
|
Loading…
Reference in a new issue