Add EPERM test for clone and clone3
PATCH v4 1/1 Add EPERM test for clone and clone3
Confirming EPERM is returned when CAP_SYS_ADMIN is removed from clone and clone3.
And for clone3 the set_tid_size is greater than 0.
--- runtest/syscalls | 2 + testcases/kernel/syscalls/clone/.gitignore | 1 + testcases/kernel/syscalls/clone/clone11.c | 74 +++++++++++++++++++++ testcases/kernel/syscalls/clone3/.gitignore | 1 + testcases/kernel/syscalls/clone3/clone304.c | 65 ++++++++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 testcases/kernel/syscalls/clone/clone11.c create mode 100644 testcases/kernel/syscalls/clone3/clone304.c
runtest/syscalls +2 -0
@@ -124,10 +124,12 @@ clone07 clone07 clone08 clone08 clone09 clone09 clone10 clone10 +clone11 clone11 clone301 clone301 clone302 clone302 clone303 clone303 +clone304 clone304 close01 close01 close02 close02
testcases/kernel/syscalls/clone/.gitignore +1 -0
@@ -8,3 +8,4 @@ /clone08 /clone09 /clone10 +/clone11
testcases/kernel/syscalls/clone/clone11.c new file +74 -0
74 lines not on this page · raw message
testcases/kernel/syscalls/clone3/.gitignore +1 -0
@@ -1,3 +1,4 @@ clone301 clone302 clone303 +clone304
testcases/kernel/syscalls/clone3/clone304.c new file +65 -0
65 lines not on this page · raw message
Signature · 5 lines
2.49.0 -- Mailing list info: https://lists.linux.it/listinfo/ltp
Quoted 164 lines · Stephen Bertram via ltp
Confirming EPERM is returned when
CAP_SYS_ADMINis removed from clone and clone3.
And for clone3 theset_tid_sizeis greater than 0.Signed-off-by Stephen Bertram <[email protected]>--- runtest/syscalls | 2 + testcases/kernel/syscalls/clone/.gitignore | 1 + testcases/kernel/syscalls/clone/clone11.c | 74 +++++++++++++++++++++ testcases/kernel/syscalls/clone3/.gitignore | 1 + testcases/kernel/syscalls/clone3/clone304.c | 65 ++++++++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 testcases/kernel/syscalls/clone/clone11.c create mode 100644 testcases/kernel/syscalls/clone3/clone304.cruntest/syscalls +2
-0@@ -124,10 +124,12 @@ clone07 clone07 clone08 clone08 clone09 clone09 clone10 clone10 +clone11 clone11 clone301 clone301 clone302 clone302 clone303 clone303 +clone304 clone304 close01 close01 close02 close02testcases/kernel/syscalls/clone/.gitignore +1
-0@@ -8,3 +8,4 @@ /clone08 /clone09 /clone10 +/clone11testcases/kernel/syscalls/clone/clone11.c new file +7
-0@@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2025 Stephen Bertram <[email protected]> +*/ + +/*\ +* This test verifies that :man2:`clone` fails with EPERM when
CAP_SYS_ADMIN
+ * has been dropped.
+ */testcases/kernel/syscalls/clone/clone11.c new file +35
-0+ +#define _GNU_SOURCE +#define DESC (x) .flags = x, .sflags = #x + +#include "tst_test.h" +#include "clone_platform.h" +#include "lapi/sched.h" + +static void *child_stack; +static int *child_pid; + +static struct tcase { +uint64_t flags; +char *sflags; +} tcases[] = { + {DESC (CLONE_NEWPID ) }, + {DESC (CLONE_NEWCGROUP ) }, + {DESC (CLONE_NEWIPC ) }, + {DESC (CLONE_NEWNET ) }, + {DESC (CLONE_NEWNS ) }, + {DESC (CLONE_NEWUTS ) }, +}; + +static int child_fn(void *arg LTP_ATTRIBUTE_UNUSED) +{ + *child_pid =getpid (); +exit (0 ); +} + +static void run (unsigned int n) +{ +struct tcase *tc = &tcases[n]; + + TST_EXP_FAIL(ltp_clone(tc->flags, child_fn, NULL, CHILD_STACK_SIZE, + child_stack), EPERM, "clone(%s) should fail withEPERM"
testcases/kernel/syscalls/clone/clone11.c new file +6
-0+ , tc->sflags); +} + +static void setup (void ) +{ + child_pid = SAFE_MMAP(NULL, sizeof(*child_pid), PROT_READ |
PROT_WRITE,testcases/kernel/syscalls/clone/clone11.c new file +24
-0+ MAP_SHARED | MAP_ANONYMOUS, -1, 0); +} + +static void cleanup (void ) +{ +if (child_pid) +SAFE_MUNMAP (child_pid,sizeof (*child_pid)); +} + +static struct tst_test test = { + .tcnt =ARRAY_SIZE (tcases), + .setup = setup, + .test = run, + .cleanup = cleanup, + .needs_root =1 , + .caps = (struct tst_cap []) { +TST_CAP (TST_CAP_DROP ,CAP_SYS_ADMIN ), + {}, + }, + .bufs = (struct tst_buffers []) { + {&child_stack, .size =CHILD_STACK_SIZE }, + {}, + }, +};testcases/kernel/syscalls/clone3/.gitignore +1
-0@@ -1,3 +1,4 @@ clone301 clone302 clone303 +clone304testcases/kernel/syscalls/clone3/clone304.c new file +7
-0@@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2025 Stephen Bertram <[email protected]> +*/ + +/*\ +* This test verifies that :man2:`clone3` fails with EPERM when
CAP_SYS_ADMIN
+ * has been dropped and ``clone_args.set_tid_size`` is greater than zero.
+ */testcases/kernel/syscalls/clone3/clone304.c new file +10
-0+ +#define _GNU_SOURCE +#define DESC (x) .flags = x, .sflags = #x + +#include "tst_test.h" +#include "lapi/sched.h" + +static struct clone_args args = {0 }; +static pid_t tid_array[4 ] = {0 ,0 ,0 ,0 }; +
testcases/kernel/syscalls/clone3/clone304.c line 29
Quoted 10 lines
testcases/kernel/syscalls/clone3/clone304.c new file +10
-0+static struct tcase { +uint64_t flags; +char *sflags; +} tcases[] = { + {DESC (CLONE_NEWPID ) }, + {DESC (CLONE_NEWCGROUP ) }, + {DESC (CLONE_NEWIPC ) }, + {DESC (CLONE_NEWNET ) }, + {DESC (CLONE_NEWNS ) }, + {DESC (CLONE_NEWUTS ) },
Hi Stephen,
You seem to have missed my point; maybe I did not describe it clearly.
- EPERM due to CLONE_NEW* without CAP_SYS_ADMIN is a long-standing permission check for namespace creation.
- EPERM due to set_tid_size > 0 without CAP_SYS_ADMIN is a clone3-specific path tied to PID namespace ownership and set_tid.
However, combining both in a single call confirms the “both conditions present”
case, but it can mask which check actually triggered EPERM if behavior changes or regresses.
What you need to do is to separate the situations, eliminating orthogonal impacts,
e.g.
- Unshares a new user namespace (and mount namespace to avoid surprises) in setup().
- Drops CAP_SYS_ADMIN using tst_cap_action() (via the .caps field). -
- Then runs three groups:
-
- - set_tid_size > 0 only (no CLONE_NEW*): expect EPERM
-
- - CLONE_NEW* only (set_tid_size = 0): expect EPERM
-
- - Both set_tid_size > 0 and CLONE_NEW*: expect EPERM
-
- We can achieve all of these only in one clone304 testcase.
+};
Quoted 44 lines
testcases/kernel/syscalls/clone3/clone304.c new file +8
-0+ +static void run (unsigned int n) +{ +struct tcase *tc = &tcases[n]; + + args.flags = tc->flags; + + TST_EXP_FAIL(clone3(&args, sizeof(args)), EPERM, "clone3(%s)should fail with EPERM"
testcases/kernel/syscalls/clone3/clone304.c new file +27
-1+ , tc->sflags); +} + +static void setup (void ) +{ +clone3_supported_by_kernel (); + +memset (&args,0 ,sizeof (args)); + args.set_tid = (uintptr_t )tid_array; +/* set_tid_size greater than zero - requires CAP_SYS_ADMIN */ + args.set_tid_size =4 ; +} + +static struct tst_test test = { + .tcnt =ARRAY_SIZE (tcases), + .setup = setup, + .test = run, + .needs_root =1 , + .caps = (struct tst_cap []) { +TST_CAP (TST_CAP_DROP ,CAP_SYS_ADMIN ), + {}, + }, + .bufs = (struct tst_buffers []) { + {&args, .size =sizeof (struct clone_args )}, + {}, + }, +}; --2.49.0
--
Mailing list info: https://lists.linux.it/listinfo/ltp
Signature · 5 lines
Regards, Li Wang -- Mailing list info: https://lists.linux.it/listinfo/ltp
Thanks Li.
It led to the clone tests at least which we also need :).
I have a better understanding after working with you on this and would like to follow up next week to close out the remaining issues.
thanks,
stephen
He/His/Him
Quoted 267 lines · Li Wang
Quoted 165 lines · Stephen Bertram via ltp
Confirming EPERM is returned when
CAP_SYS_ADMINis removed from clone and clone3.
And for clone3 theset_tid_sizeis greater than 0.Signed-off-by Stephen Bertram <[email protected]>--- runtest/syscalls | 2 + testcases/kernel/syscalls/clone/.gitignore | 1 + testcases/kernel/syscalls/clone/clone11.c | 74 +++++++++++++++++++++ testcases/kernel/syscalls/clone3/.gitignore | 1 + testcases/kernel/syscalls/clone3/clone304.c | 65 ++++++++++++++++++ 5 files changed, 143 insertions(+) create mode 100644 testcases/kernel/syscalls/clone/clone11.c create mode 100644 testcases/kernel/syscalls/clone3/clone304.cruntest/syscalls +2
-0@@ -124,10 +124,12 @@ clone07 clone07 clone08 clone08 clone09 clone09 clone10 clone10 +clone11 clone11 clone301 clone301 clone302 clone302 clone303 clone303 +clone304 clone304 close01 close01 close02 close02testcases/kernel/syscalls/clone/.gitignore +1
-0@@ -8,3 +8,4 @@ /clone08 /clone09 /clone10 +/clone11testcases/kernel/syscalls/clone/clone11.c new file +7
-0@@ -0,0 +1,74 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2025 Stephen Bertram <[email protected]> +*/ + +/*\ +* This test verifies that :man2:`clone` fails with EPERM when
CAP_SYS_ADMIN
+ * has been dropped.
+ */testcases/kernel/syscalls/clone/clone11.c new file +34
-0+ +#define _GNU_SOURCE +#define DESC (x) .flags = x, .sflags = #x + +#include "tst_test.h" +#include "clone_platform.h" +#include "lapi/sched.h" + +static void *child_stack; +static int *child_pid; + +static struct tcase { +uint64_t flags; +char *sflags; +} tcases[] = { + {DESC (CLONE_NEWPID ) }, + {DESC (CLONE_NEWCGROUP ) }, + {DESC (CLONE_NEWIPC ) }, + {DESC (CLONE_NEWNET ) }, + {DESC (CLONE_NEWNS ) }, + {DESC (CLONE_NEWUTS ) }, +}; + +static int child_fn(void *arg LTP_ATTRIBUTE_UNUSED) +{ + *child_pid =getpid (); +exit (0 ); +} + +static void run (unsigned int n) +{ +struct tcase *tc = &tcases[n]; + + TST_EXP_FAIL(ltp_clone(tc->flags, child_fn, NULL,
CHILD_STACK_SIZE,
+child_stack), EPERM, "clone(%s) should fail with
EPERM"testcases/kernel/syscalls/clone/clone11.c new file +6
-0+ , tc->sflags); +} + +static void setup (void ) +{ + child_pid = SAFE_MMAP(NULL, sizeof(*child_pid), PROT_READ |
PROT_WRITE,testcases/kernel/syscalls/clone/clone11.c new file +24
-0+ MAP_SHARED | MAP_ANONYMOUS, -1, 0); +} + +static void cleanup (void ) +{ +if (child_pid) +SAFE_MUNMAP (child_pid,sizeof (*child_pid)); +} + +static struct tst_test test = { + .tcnt =ARRAY_SIZE (tcases), + .setup = setup, + .test = run, + .cleanup = cleanup, + .needs_root =1 , + .caps = (struct tst_cap []) { +TST_CAP (TST_CAP_DROP ,CAP_SYS_ADMIN ), + {}, + }, + .bufs = (struct tst_buffers []) { + {&child_stack, .size =CHILD_STACK_SIZE }, + {}, + }, +};testcases/kernel/syscalls/clone3/.gitignore +1
-0@@ -1,3 +1,4 @@ clone301 clone302 clone303 +clone304testcases/kernel/syscalls/clone3/clone304.c new file +7
-0@@ -0,0 +1,65 @@ +// SPDX-License-Identifier: GPL-2.0-or-later +/* +* Copyright (c) 2025 Stephen Bertram <[email protected]> +*/ + +/*\ +* This test verifies that :man2:`clone3` fails with EPERM when
CAP_SYS_ADMIN
+ * has been dropped and ``clone_args.set_tid_size`` is greater than zero.
+ */testcases/kernel/syscalls/clone3/clone304.c new file +10
-0+ +#define _GNU_SOURCE +#define DESC (x) .flags = x, .sflags = #x + +#include "tst_test.h" +#include "lapi/sched.h" + +static struct clone_args args = {0 }; +static pid_t tid_array[4 ] = {0 ,0 ,0 ,0 }; +testcases/kernel/syscalls/clone3/clone304.c line 29
Quoted 10 lines
testcases/kernel/syscalls/clone3/clone304.c new file +10
-0+static struct tcase { +uint64_t flags; +char *sflags; +} tcases[] = { + {DESC (CLONE_NEWPID ) }, + {DESC (CLONE_NEWCGROUP ) }, + {DESC (CLONE_NEWIPC ) }, + {DESC (CLONE_NEWNET ) }, + {DESC (CLONE_NEWNS ) }, + {DESC (CLONE_NEWUTS ) },Hi Stephen,
You seem to have missed my point; maybe I did not describe it clearly.
- EPERM due to CLONE_NEW* without
CAP_SYS_ADMINis a long-standing permission check for namespace creation.- EPERM due to
set_tid_size> 0 withoutCAP_SYS_ADMINis a clone3-specific path tied to PID namespace ownership andset_tid.However, combining both in a single call confirms the “both conditions present”
case, but it can mask which check actually triggered EPERM if behavior changes or regresses.What you need to do is to separate the situations, eliminating orthogonal impacts,
e.g.- Unshares a new user namespace (and mount namespace to avoid surprises) in
setup().
- DropsCAP_SYS_ADMINusingtst_cap_action()(via the .caps field). -
- Then runs three groups:
-
- -set_tid_size> 0 only (no CLONE_NEW*): expect EPERM
-
- - CLONE_NEW* only (set_tid_size= 0): expect EPERM
-
- - Bothset_tid_size> 0 and CLONE_NEW*: expect EPERM
-
- We can achieve all of these only in one clone304 testcase.+};
Quoted 40 lines
testcases/kernel/syscalls/clone3/clone304.c new file +8
-0+ +static void run (unsigned int n) +{ +struct tcase *tc = &tcases[n]; + + args.flags = tc->flags; + + TST_EXP_FAIL(clone3(&args, sizeof(args)), EPERM, "clone3(%s)should fail with EPERM"
testcases/kernel/syscalls/clone3/clone304.c new file +27
-1+ , tc->sflags); +} + +static void setup (void ) +{ +clone3_supported_by_kernel (); + +memset (&args,0 ,sizeof (args)); + args.set_tid = (uintptr_t )tid_array; +/* set_tid_size greater than zero - requires CAP_SYS_ADMIN */ + args.set_tid_size =4 ; +} + +static struct tst_test test = { + .tcnt =ARRAY_SIZE (tcases), + .setup = setup, + .test = run, + .needs_root =1 , + .caps = (struct tst_cap []) { +TST_CAP (TST_CAP_DROP ,CAP_SYS_ADMIN ), + {}, + }, + .bufs = (struct tst_buffers []) { + {&args, .size =sizeof (struct clone_args )}, + {}, + }, +}; --2.49.0
Quoted 4 lines
--
Mailing list info: https://lists.linux.it/listinfo/ltp--
Regards,
Li Wang
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp