All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH v1 00/10] Remove libclone support from userns testing suite
@ 2023-02-15 10:16 Andrea Cervesato via ltp
  2023-02-15 10:16 ` [LTP] [PATCH v1 01/10] Refactor userns01 test Andrea Cervesato via ltp
                   ` (9 more replies)
  0 siblings, 10 replies; 14+ messages in thread
From: Andrea Cervesato via ltp @ 2023-02-15 10:16 UTC (permalink / raw)
  To: ltp

Removed ltp_clone_quick from userns testing suite and simplified code a bit.

Andrea Cervesato (10):
  Refactor userns01 test
  Refactor userns02 test
  Refactor userns03 test
  Refactor userns04 test
  Refactor userns05 test
  Refactor userns06 test
  Refactor userns07 test
  Remove check_newuser from userns testing suite
  Remove libclone dependency from userns suite
  Delete userns_helper.h from userns suite

 testcases/kernel/containers/userns/Makefile   |  2 +-
 testcases/kernel/containers/userns/common.h   | 20 +-----
 testcases/kernel/containers/userns/userns01.c | 27 +++----
 testcases/kernel/containers/userns/userns02.c | 31 +++-----
 testcases/kernel/containers/userns/userns03.c | 70 +++++++------------
 testcases/kernel/containers/userns/userns04.c | 45 +++++-------
 testcases/kernel/containers/userns/userns05.c | 48 +++++--------
 testcases/kernel/containers/userns/userns06.c | 41 +++++------
 .../containers/userns/userns06_capcheck.c     | 19 +++--
 testcases/kernel/containers/userns/userns07.c | 67 ++++++++++--------
 .../kernel/containers/userns/userns_helper.h  | 59 ----------------
 11 files changed, 146 insertions(+), 283 deletions(-)
 delete mode 100644 testcases/kernel/containers/userns/userns_helper.h

-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 14+ messages in thread

* [LTP] [PATCH v1 01/10] Refactor userns01 test
  2023-02-15 10:16 [LTP] [PATCH v1 00/10] Remove libclone support from userns testing suite Andrea Cervesato via ltp
@ 2023-02-15 10:16 ` Andrea Cervesato via ltp
  2023-02-28 10:46   ` Richard Palethorpe
  2023-02-15 10:16 ` [LTP] [PATCH v1 02/10] Refactor userns02 test Andrea Cervesato via ltp
                   ` (8 subsequent siblings)
  9 siblings, 1 reply; 14+ messages in thread
From: Andrea Cervesato via ltp @ 2023-02-15 10:16 UTC (permalink / raw)
  To: ltp

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/containers/userns/userns01.c | 27 +++++++------------
 1 file changed, 10 insertions(+), 17 deletions(-)

diff --git a/testcases/kernel/containers/userns/userns01.c b/testcases/kernel/containers/userns/userns01.c
index 8ed7a9f41..cbe0da245 100644
--- a/testcases/kernel/containers/userns/userns01.c
+++ b/testcases/kernel/containers/userns/userns01.c
@@ -20,9 +20,9 @@
 #define _GNU_SOURCE
 
 #include <stdio.h>
-#include "common.h"
 #include "config.h"
 #include <sys/capability.h>
+#include "lapi/sched.h"
 
 #define OVERFLOWUIDPATH "/proc/sys/kernel/overflowuid"
 #define OVERFLOWGIDPATH "/proc/sys/kernel/overflowgid"
@@ -30,10 +30,7 @@
 static long overflowuid;
 static long overflowgid;
 
-/*
- * child_fn1() - Inside a new user namespace
- */
-static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
+static void child_fn1(void)
 {
 	int uid, gid;
 	cap_t caps;
@@ -45,10 +42,8 @@ static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
 
 	tst_res(TINFO, "USERNS test is running in a new user namespace.");
 
-	if (uid != overflowuid || gid != overflowgid)
-		tst_res(TFAIL, "got unexpected uid=%d gid=%d", uid, gid);
-	else
-		tst_res(TPASS, "got expected uid and gid");
+	TST_EXP_EQ_LI(uid, overflowuid);
+	TST_EXP_EQ_LI(gid, overflowgid);
 
 	caps = cap_get_proc();
 
@@ -68,31 +63,29 @@ static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
 		tst_res(TFAIL, "unexpected effective/permitted caps at %d", i);
 	else
 		tst_res(TPASS, "expected capabilities");
-
-	return 0;
 }
 
 static void setup(void)
 {
-	check_newuser();
-
 	SAFE_FILE_SCANF(OVERFLOWUIDPATH, "%ld", &overflowuid);
 	SAFE_FILE_SCANF(OVERFLOWGIDPATH, "%ld", &overflowgid);
 }
 
 static void run(void)
 {
-	int pid;
+	const struct tst_clone_args args = { CLONE_NEWUSER, SIGCHLD };
 
-	pid = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, child_fn1, NULL);
-	if (pid < 0)
-		tst_brk(TBROK | TTERRNO, "clone failed");
+	if (!SAFE_CLONE(&args)) {
+		child_fn1();
+		return;
+	}
 }
 
 static struct tst_test test = {
 	.setup = setup,
 	.test_all = run,
 	.needs_root = 1,
+	.forks_child = 1,
 	.caps = (struct tst_cap []) {
 		TST_CAP(TST_CAP_DROP, CAP_NET_RAW),
 		{}
-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [LTP] [PATCH v1 02/10] Refactor userns02 test
  2023-02-15 10:16 [LTP] [PATCH v1 00/10] Remove libclone support from userns testing suite Andrea Cervesato via ltp
  2023-02-15 10:16 ` [LTP] [PATCH v1 01/10] Refactor userns01 test Andrea Cervesato via ltp
@ 2023-02-15 10:16 ` Andrea Cervesato via ltp
  2023-02-15 10:16 ` [LTP] [PATCH v1 03/10] Refactor userns03 test Andrea Cervesato via ltp
                   ` (7 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: Andrea Cervesato via ltp @ 2023-02-15 10:16 UTC (permalink / raw)
  To: ltp

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/containers/userns/userns02.c | 31 +++++++------------
 1 file changed, 11 insertions(+), 20 deletions(-)

diff --git a/testcases/kernel/containers/userns/userns02.c b/testcases/kernel/containers/userns/userns02.c
index dd784000e..9d445d986 100644
--- a/testcases/kernel/containers/userns/userns02.c
+++ b/testcases/kernel/containers/userns/userns02.c
@@ -14,13 +14,10 @@
 #define _GNU_SOURCE
 
 #include <stdio.h>
-#include "common.h"
 #include "tst_test.h"
+#include "lapi/sched.h"
 
-/*
- * child_fn1() - Inside a new user namespace
- */
-static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
+static void child_fn1(void)
 {
 	int uid, gid;
 
@@ -29,29 +26,23 @@ static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
 	uid = geteuid();
 	gid = getegid();
 
-	if (uid == 100 && gid == 100)
-		tst_res(TPASS, "got expected uid and gid");
-	else
-		tst_res(TFAIL, "got unexpected uid=%d gid=%d", uid, gid);
-
-	return 0;
-}
-
-static void setup(void)
-{
-	check_newuser();
+	TST_EXP_EQ_LI(uid, 100);
+	TST_EXP_EQ_LI(gid, 100);
 }
 
 static void run(void)
 {
+	const struct tst_clone_args args = { CLONE_NEWUSER, SIGCHLD };
 	int childpid;
 	int parentuid;
 	int parentgid;
 	char path[BUFSIZ];
 
-	childpid = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, child_fn1, NULL);
-	if (childpid < 0)
-		tst_brk(TBROK | TTERRNO, "clone failed");
+	childpid = SAFE_CLONE(&args);
+	if (!childpid) {
+		child_fn1();
+		return;
+	}
 
 	parentuid = geteuid();
 	parentgid = getegid();
@@ -71,9 +62,9 @@ static void run(void)
 }
 
 static struct tst_test test = {
-	.setup = setup,
 	.test_all = run,
 	.needs_root = 1,
+	.forks_child = 1,
 	.needs_checkpoints = 1,
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_USER_NS",
-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [LTP] [PATCH v1 03/10] Refactor userns03 test
  2023-02-15 10:16 [LTP] [PATCH v1 00/10] Remove libclone support from userns testing suite Andrea Cervesato via ltp
  2023-02-15 10:16 ` [LTP] [PATCH v1 01/10] Refactor userns01 test Andrea Cervesato via ltp
  2023-02-15 10:16 ` [LTP] [PATCH v1 02/10] Refactor userns02 test Andrea Cervesato via ltp
@ 2023-02-15 10:16 ` Andrea Cervesato via ltp
  2023-02-15 10:16 ` [LTP] [PATCH v1 04/10] Refactor userns04 test Andrea Cervesato via ltp
                   ` (6 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: Andrea Cervesato via ltp @ 2023-02-15 10:16 UTC (permalink / raw)
  To: ltp

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/containers/userns/userns03.c | 70 +++++++------------
 1 file changed, 24 insertions(+), 46 deletions(-)

diff --git a/testcases/kernel/containers/userns/userns03.c b/testcases/kernel/containers/userns/userns03.c
index b90cc09ba..4dd3f1a73 100644
--- a/testcases/kernel/containers/userns/userns03.c
+++ b/testcases/kernel/containers/userns/userns03.c
@@ -32,8 +32,9 @@
 
 #include <stdio.h>
 #include <stdbool.h>
-#include "common.h"
 #include "tst_test.h"
+#include "lapi/sched.h"
+#include "common.h"
 
 #define CHILD1UID 0
 #define CHILD1GID 0
@@ -42,23 +43,12 @@
 #define UID_MAP 0
 #define GID_MAP 1
 
-static int cpid1;
-static int parentuid;
-static int parentgid;
-
-/*
- * child_fn1() - Inside a new user namespace
- */
-static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
+static void child_fn1(void)
 {
 	TST_CHECKPOINT_WAIT(0);
-	return 0;
 }
 
-/*
- * child_fn2() - Inside a new user namespace
- */
-static int child_fn2(LTP_ATTRIBUTE_UNUSED void *arg)
+static void child_fn2(int cpid1, int parentuid, int parentgid)
 {
 	int uid, gid;
 	char cpid1uidpath[BUFSIZ];
@@ -70,12 +60,8 @@ static int child_fn2(LTP_ATTRIBUTE_UNUSED void *arg)
 	uid = geteuid();
 	gid = getegid();
 
-	tst_res(TINFO, "uid=%d, gid=%d", uid, gid);
-
-	if (uid != CHILD2UID || gid != CHILD2GID)
-		tst_res(TFAIL, "unexpected uid=%d gid=%d", uid, gid);
-	else
-		tst_res(TPASS, "expected uid and gid");
+	TST_EXP_EQ_LI(uid, CHILD2UID);
+	TST_EXP_EQ_LI(gid, CHILD2GID);
 
 	/* Get the uid parameters of the child_fn2 process */
 	SAFE_FILE_SCANF("/proc/self/uid_map", "%d %d %d", &idinsidens, &idoutsidens, &length);
@@ -127,32 +113,31 @@ static int child_fn2(LTP_ATTRIBUTE_UNUSED void *arg)
 
 	TST_CHECKPOINT_WAKE(0);
 	TST_CHECKPOINT_WAKE(1);
-
-	return 0;
-}
-
-static void setup(void)
-{
-	check_newuser();
 }
 
 static void run(void)
 {
-	pid_t cpid2;
+	const struct tst_clone_args args = { CLONE_NEWUSER, SIGCHLD };
+	pid_t cpid1, cpid2;
+	uid_t parentuid;
+	gid_t parentgid;
 	char path[BUFSIZ];
 	int fd;
-	int ret;
 
 	parentuid = geteuid();
 	parentgid = getegid();
 
-	cpid1 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, child_fn1, NULL);
-	if (cpid1 < 0)
-		tst_brk(TBROK | TTERRNO, "cpid1 clone failed");
+	cpid1 = SAFE_CLONE(&args);
+	if (!cpid1) {
+		child_fn1();
+		return;
+	}
 
-	cpid2 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, child_fn2, NULL);
-	if (cpid2 < 0)
-		tst_brk(TBROK | TTERRNO, "cpid2 clone failed");
+	cpid2 = SAFE_CLONE(&args);
+	if (!cpid2) {
+		child_fn2(cpid1, parentuid, parentgid);
+		return;
+	}
 
 	if (access("/proc/self/setgroups", F_OK) == 0) {
 		sprintf(path, "/proc/%d/setgroups", cpid1);
@@ -168,19 +153,12 @@ static void run(void)
 		 * do so will fail with the error EPERM.)
 		 */
 
-		/* test that setgroups can't be re-enabled */
-		fd = SAFE_OPEN(path, O_WRONLY, 0644);
-		ret = write(fd, "allow", 5);
-
-		if (ret != -1)
-			tst_brk(TBROK, "write action should fail");
-		else if (errno != EPERM)
-			tst_brk(TBROK | TTERRNO, "unexpected error");
+		tst_res(TINFO, "Check if setgroups can be re-enabled");
 
+		fd = SAFE_OPEN(path, O_WRONLY, 0644);
+		TST_EXP_FAIL(write(fd, "allow", 5), EPERM);
 		SAFE_CLOSE(fd);
 
-		tst_res(TPASS, "setgroups can't be re-enabled");
-
 		sprintf(path, "/proc/%d/setgroups", cpid2);
 
 		fd = SAFE_OPEN(path, O_WRONLY, 0644);
@@ -198,9 +176,9 @@ static void run(void)
 }
 
 static struct tst_test test = {
-	.setup = setup,
 	.test_all = run,
 	.needs_root = 1,
+	.forks_child = 1,
 	.needs_checkpoints = 1,
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_USER_NS",
-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [LTP] [PATCH v1 04/10] Refactor userns04 test
  2023-02-15 10:16 [LTP] [PATCH v1 00/10] Remove libclone support from userns testing suite Andrea Cervesato via ltp
                   ` (2 preceding siblings ...)
  2023-02-15 10:16 ` [LTP] [PATCH v1 03/10] Refactor userns03 test Andrea Cervesato via ltp
@ 2023-02-15 10:16 ` Andrea Cervesato via ltp
  2023-02-15 10:16 ` [LTP] [PATCH v1 05/10] Refactor userns05 test Andrea Cervesato via ltp
                   ` (5 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: Andrea Cervesato via ltp @ 2023-02-15 10:16 UTC (permalink / raw)
  To: ltp

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/containers/userns/userns04.c | 45 +++++++------------
 1 file changed, 17 insertions(+), 28 deletions(-)

diff --git a/testcases/kernel/containers/userns/userns04.c b/testcases/kernel/containers/userns/userns04.c
index d8639502e..58ae7c302 100644
--- a/testcases/kernel/containers/userns/userns04.c
+++ b/testcases/kernel/containers/userns/userns04.c
@@ -15,56 +15,46 @@
 #define _GNU_SOURCE
 
 #include <stdio.h>
-#include "common.h"
 #include "tst_test.h"
-#include "lapi/syscalls.h"
+#include "lapi/sched.h"
 
-static void setup(void)
-{
-	check_newuser();
-	tst_syscall(__NR_setns, -1, 0);
-}
-
-static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
+static void child_fn1(void)
 {
 	TST_CHECKPOINT_WAIT(0);
-	return 0;
 }
 
-static int child_fn2(void *arg)
+static void child_fn2(int fd)
 {
-	TEST(tst_syscall(__NR_setns, ((long)arg), CLONE_NEWUSER));
-	if (TST_RET != -1 || TST_ERR != EPERM)
-		tst_res(TFAIL | TERRNO, "child2 setns() error");
-	else
-		tst_res(TPASS, "child2 setns() failed as expected");
-
+	TST_EXP_FAIL(setns(fd, CLONE_NEWUSER), EPERM);
 	TST_CHECKPOINT_WAIT(1);
-
-	return 0;
 }
 
 static void run(void)
 {
+	const struct tst_clone_args args = { CLONE_NEWUSER, SIGCHLD };
 	pid_t cpid1, cpid2, cpid3;
 	char path[BUFSIZ];
 	int fd;
 
-	cpid1 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, (void *)child_fn1, NULL);
-	if (cpid1 < 0)
-		tst_brk(TBROK | TTERRNO, "clone failed");
+	cpid1 = SAFE_CLONE(&args);
+	if (!cpid1) {
+		child_fn1();
+		return;
+	}
 
 	sprintf(path, "/proc/%d/ns/user", cpid1);
 
 	fd = SAFE_OPEN(path, O_RDONLY, 0644);
-	cpid2 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, (void *)child_fn2, (void *)((long)fd));
-	if (cpid2 < 0)
-		tst_brk(TBROK | TTERRNO, "clone failed");
 
-	/* child 3 - throw-away process changing ns to child1 */
+	cpid2 = SAFE_CLONE(&args);
+	if (!cpid2) {
+		child_fn2(fd);
+		return;
+	}
+
 	cpid3 = SAFE_FORK();
 	if (!cpid3) {
-		TST_EXP_PASS(tst_syscall(__NR_setns, fd, CLONE_NEWUSER));
+		TST_EXP_PASS(setns(fd, CLONE_NEWUSER));
 		return;
 	}
 
@@ -75,7 +65,6 @@ static void run(void)
 }
 
 static struct tst_test test = {
-	.setup = setup,
 	.test_all = run,
 	.needs_root = 1,
 	.forks_child = 1,
-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [LTP] [PATCH v1 05/10] Refactor userns05 test
  2023-02-15 10:16 [LTP] [PATCH v1 00/10] Remove libclone support from userns testing suite Andrea Cervesato via ltp
                   ` (3 preceding siblings ...)
  2023-02-15 10:16 ` [LTP] [PATCH v1 04/10] Refactor userns04 test Andrea Cervesato via ltp
@ 2023-02-15 10:16 ` Andrea Cervesato via ltp
  2023-02-15 10:16 ` [LTP] [PATCH v1 06/10] Refactor userns06 test Andrea Cervesato via ltp
                   ` (4 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: Andrea Cervesato via ltp @ 2023-02-15 10:16 UTC (permalink / raw)
  To: ltp

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/containers/userns/userns05.c | 48 ++++++++-----------
 1 file changed, 19 insertions(+), 29 deletions(-)

diff --git a/testcases/kernel/containers/userns/userns05.c b/testcases/kernel/containers/userns/userns05.c
index 4c16694b1..36f48fda5 100644
--- a/testcases/kernel/containers/userns/userns05.c
+++ b/testcases/kernel/containers/userns/userns05.c
@@ -18,15 +18,12 @@
 
 #include <stdio.h>
 #include "tst_test.h"
+#include "lapi/sched.h"
 #include "common.h"
 
-/*
- * child_fn1() - Inside a new user namespace
- */
-static int child_fn1(void)
+static void child_fn1(void)
 {
 	TST_CHECKPOINT_WAIT(0);
-	return 0;
 }
 
 static unsigned int getusernsidbypid(int pid)
@@ -47,14 +44,18 @@ static unsigned int getusernsidbypid(int pid)
 
 static void run(void)
 {
+	const struct tst_clone_args args1 = { .exit_signal = SIGCHLD };
+	const struct tst_clone_args args2 = { CLONE_NEWUSER, SIGCHLD };
 	int cpid1, cpid2, cpid3;
 	unsigned int parentuserns, cpid1userns, cpid2userns, newparentuserns;
 
 	parentuserns = getusernsidbypid(getpid());
 
-	cpid1 = ltp_clone_quick(SIGCHLD, (void *)child_fn1, NULL);
-	if (cpid1 < 0)
-		tst_brk(TBROK | TTERRNO, "clone failed");
+	cpid1 = SAFE_CLONE(&args1);
+	if (!cpid1) {
+		child_fn1();
+		return;
+	}
 
 	cpid1userns = getusernsidbypid(cpid1);
 
@@ -64,23 +65,20 @@ static void run(void)
 	 * CLONE_NEWUSER flag is a member of the same user namespace as its
 	 * parent
 	 */
-	if (parentuserns != cpid1userns)
-		tst_res(TFAIL, "userns:parent should be equal to cpid1");
-	else
-		tst_res(TPASS, "userns:parent is equal to cpid1");
+	TST_EXP_EQ_LI(parentuserns, cpid1userns);
 
-	cpid2 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, (void *)child_fn1, NULL);
-	if (cpid2 < 0)
-		tst_brk(TBROK | TTERRNO, "clone failed");
+	cpid2 = SAFE_CLONE(&args2);
+	if (!cpid2) {
+		child_fn1();
+		return;
+	}
 
 	cpid2userns = getusernsidbypid(cpid2);
 
 	TST_CHECKPOINT_WAKE(0);
 
-	if (parentuserns == cpid2userns)
-		tst_res(TFAIL, "userns:parent should be not equal to cpid2");
-	else
-		tst_res(TPASS, "userns:parent is not equal to cpid2");
+	TST_EXP_EXPR(parentuserns != cpid2userns,
+		"parent namespace != child namespace");
 
 	cpid3 = SAFE_FORK();
 	if (!cpid3) {
@@ -91,20 +89,12 @@ static void run(void)
 		 * is moved into a new user namespace which is not shared
 		 * with any previously existing process
 		 */
-		if (parentuserns == newparentuserns)
-			tst_res(TFAIL, "unshared namespaces with same id");
-		else
-			tst_res(TPASS, "unshared namespaces with different id");
+		TST_EXP_EXPR(parentuserns != newparentuserns,
+			"parent namespace != unshared child namespace");
 	}
 }
 
-static void setup(void)
-{
-	check_newuser();
-}
-
 static struct tst_test test = {
-	.setup = setup,
 	.test_all = run,
 	.needs_root = 1,
 	.forks_child = 1,
-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [LTP] [PATCH v1 06/10] Refactor userns06 test
  2023-02-15 10:16 [LTP] [PATCH v1 00/10] Remove libclone support from userns testing suite Andrea Cervesato via ltp
                   ` (4 preceding siblings ...)
  2023-02-15 10:16 ` [LTP] [PATCH v1 05/10] Refactor userns05 test Andrea Cervesato via ltp
@ 2023-02-15 10:16 ` Andrea Cervesato via ltp
  2023-02-15 10:16 ` [LTP] [PATCH v1 07/10] Refactor userns07 test Andrea Cervesato via ltp
                   ` (3 subsequent siblings)
  9 siblings, 0 replies; 14+ messages in thread
From: Andrea Cervesato via ltp @ 2023-02-15 10:16 UTC (permalink / raw)
  To: ltp

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/containers/userns/userns06.c | 41 ++++++++-----------
 .../containers/userns/userns06_capcheck.c     | 19 ++++-----
 2 files changed, 25 insertions(+), 35 deletions(-)

diff --git a/testcases/kernel/containers/userns/userns06.c b/testcases/kernel/containers/userns/userns06.c
index 39f02e38a..a2c51f725 100644
--- a/testcases/kernel/containers/userns/userns06.c
+++ b/testcases/kernel/containers/userns/userns06.c
@@ -20,6 +20,7 @@
 #define _GNU_SOURCE
 
 #include <stdio.h>
+#include "lapi/sched.h"
 #include "common.h"
 
 #define TEST_APP "userns06_capcheck"
@@ -29,7 +30,7 @@
 #define CHILD2UID 200
 #define CHILD2GID 200
 
-static int child_fn1(void)
+static void child_fn1(void)
 {
 	char *const args[] = { TEST_APP, "privileged", NULL };
 	int ret;
@@ -39,11 +40,9 @@ static int child_fn1(void)
 	ret = execv(args[0], args);
 	if (ret == -1)
 		tst_brk(TBROK | TERRNO, "execv: unexpected error");
-
-	return 0;
 }
 
-static int child_fn2(void)
+static void child_fn2(void)
 {
 	int uid, gid, ret;
 	char *const args[] = { TEST_APP, "unprivileged", NULL };
@@ -53,27 +52,17 @@ static int child_fn2(void)
 	uid = geteuid();
 	gid = getegid();
 
-	if (uid != CHILD2UID || gid != CHILD2GID) {
-		tst_res(TFAIL, "unexpected uid=%d gid=%d", uid, gid);
-		return 1;
-	}
-
-	tst_res(TPASS, "expected uid and gid");
+	TST_EXP_EQ_LI(uid, CHILD2UID);
+	TST_EXP_EQ_LI(gid, CHILD2GID);
 
 	ret = execv(args[0], args);
 	if (ret == -1)
 		tst_brk(TBROK | TERRNO, "execv: unexpected error");
-
-	return 0;
-}
-
-static void setup(void)
-{
-	check_newuser();
 }
 
 static void run(void)
 {
+	const struct tst_clone_args args = { CLONE_NEWUSER, SIGCHLD };
 	pid_t cpid1;
 	pid_t cpid2;
 	int parentuid;
@@ -84,13 +73,17 @@ static void run(void)
 	parentuid = geteuid();
 	parentgid = getegid();
 
-	cpid1 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, (void *)child_fn1, NULL);
-	if (cpid1 < 0)
-		tst_brk(TBROK | TTERRNO, "cpid1 clone failed");
+	cpid1 = SAFE_CLONE(&args);
+	if (!cpid1) {
+		child_fn1();
+		return;
+	}
 
-	cpid2 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, (void *)child_fn2, NULL);
-	if (cpid2 < 0)
-		tst_brk(TBROK | TTERRNO, "cpid2 clone failed");
+	cpid2 = SAFE_CLONE(&args);
+	if (!cpid2) {
+		child_fn2();
+		return;
+	}
 
 	if (access("/proc/self/setgroups", F_OK) == 0) {
 		sprintf(path, "/proc/%d/setgroups", cpid1);
@@ -117,9 +110,9 @@ static void run(void)
 }
 
 static struct tst_test test = {
-	.setup = setup,
 	.test_all = run,
 	.needs_root = 1,
+	.forks_child = 1,
 	.needs_checkpoints = 1,
 	.resource_files = (const char *[]) {
 		TEST_APP,
diff --git a/testcases/kernel/containers/userns/userns06_capcheck.c b/testcases/kernel/containers/userns/userns06_capcheck.c
index c048ef63f..8669657b9 100644
--- a/testcases/kernel/containers/userns/userns06_capcheck.c
+++ b/testcases/kernel/containers/userns/userns06_capcheck.c
@@ -24,8 +24,8 @@ int main(int argc, char *argv[])
 {
 	cap_t caps;
 	int i, last_cap;
-	cap_flag_value_t flag_val;
-	cap_flag_value_t expected_flag = 1;
+	cap_flag_value_t cap_flag;
+	cap_flag_value_t expected_cap_flag = 1;
 
 	tst_reinit();
 
@@ -35,24 +35,21 @@ int main(int argc, char *argv[])
 	SAFE_FILE_SCANF("/proc/sys/kernel/cap_last_cap", "%d", &last_cap);
 
 	if (strcmp("privileged", argv[1]))
-		expected_flag = 0;
+		expected_cap_flag = 0;
 
 	caps = cap_get_proc();
 
 	for (i = 0; i <= last_cap; i++) {
-		cap_get_flag(caps, i, CAP_EFFECTIVE, &flag_val);
-		if (flag_val != expected_flag)
+		cap_get_flag(caps, i, CAP_EFFECTIVE, &cap_flag);
+		if (cap_flag != expected_cap_flag)
 			break;
 
-		cap_get_flag(caps, i, CAP_PERMITTED, &flag_val);
-		if (flag_val != expected_flag)
+		cap_get_flag(caps, i, CAP_PERMITTED, &cap_flag);
+		if (cap_flag != expected_cap_flag)
 			break;
 	}
 
-	if (flag_val != expected_flag)
-		tst_res(TFAIL, "unexpected effective/permitted caps at %d", i);
-	else
-		tst_res(TPASS, "expected caps at %d", i);
+	TST_EXP_EQ_LI(cap_flag, expected_cap_flag);
 
 	return 0;
 }
-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [LTP] [PATCH v1 07/10] Refactor userns07 test
  2023-02-15 10:16 [LTP] [PATCH v1 00/10] Remove libclone support from userns testing suite Andrea Cervesato via ltp
                   ` (5 preceding siblings ...)
  2023-02-15 10:16 ` [LTP] [PATCH v1 06/10] Refactor userns06 test Andrea Cervesato via ltp
@ 2023-02-15 10:16 ` Andrea Cervesato via ltp
  2023-02-28 11:23   ` Richard Palethorpe
  2023-02-15 10:16 ` [LTP] [PATCH v1 08/10] Remove check_newuser from userns testing suite Andrea Cervesato via ltp
                   ` (2 subsequent siblings)
  9 siblings, 1 reply; 14+ messages in thread
From: Andrea Cervesato via ltp @ 2023-02-15 10:16 UTC (permalink / raw)
  To: ltp

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/containers/userns/userns07.c | 67 +++++++++++--------
 1 file changed, 38 insertions(+), 29 deletions(-)

diff --git a/testcases/kernel/containers/userns/userns07.c b/testcases/kernel/containers/userns/userns07.c
index 4659de636..3a693b8e3 100644
--- a/testcases/kernel/containers/userns/userns07.c
+++ b/testcases/kernel/containers/userns/userns07.c
@@ -16,80 +16,89 @@
 #include <sys/wait.h>
 #include "common.h"
 #include "tst_test.h"
+#include "lapi/sched.h"
 
 #define MAXNEST 32
 
-static void setup(void)
-{
-	check_newuser();
-}
+static int *level;
 
-static int child_fn1(void *arg)
+static void child_fn1(void)
 {
-	pid_t cpid1;
-	long level = (long)arg;
-	int status;
+	const struct tst_clone_args args = { CLONE_NEWUSER, SIGCHLD };
+	pid_t cpid;
 	int parentuid;
 	int parentgid;
+	int status;
 
 	TST_CHECKPOINT_WAIT(0);
 
-	if (level == MAXNEST) {
+	if (*level == MAXNEST) {
 		tst_res(TPASS, "nested all children");
-		return 0;
+		return;
 	}
 
-	cpid1 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, (void *)child_fn1, (void *)(level + 1));
-	if (cpid1 < 0) {
-		tst_res(TFAIL | TERRNO, "level %ld, unexpected error", level);
-		return 1;
+	cpid = SAFE_CLONE(&args);
+	if (!cpid) {
+		*level += 1;
+		child_fn1();
+		return;
 	}
 
 	parentuid = geteuid();
 	parentgid = getegid();
 
-	updatemap(cpid1, UID_MAP, 0, parentuid);
-	updatemap(cpid1, GID_MAP, 0, parentgid);
+	updatemap(cpid, UID_MAP, 0, parentuid);
+	updatemap(cpid, GID_MAP, 0, parentgid);
 
 	TST_CHECKPOINT_WAKE(0);
 
-	SAFE_WAITPID(cpid1, &status, 0);
-
-	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
-		tst_brk(TBROK, "child %s", tst_strstatus(status));
-
-	return 0;
+	SAFE_WAITPID(cpid, &status, 0);
 }
 
 static void run(void)
 {
-	pid_t cpid1;
+	const struct tst_clone_args args = { CLONE_NEWUSER, SIGCHLD };
+	pid_t cpid;
 	int parentuid;
 	int parentgid;
 	char path[BUFSIZ];
 
-	cpid1 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, (void *)child_fn1, (void *)0);
-	if (cpid1 < 0)
-		tst_brk(TBROK | TTERRNO, "clone failed");
+	cpid = SAFE_CLONE(&args);
+	if (!cpid) {
+		child_fn1();
+		return;
+	}
 
 	parentuid = geteuid();
 	parentgid = getegid();
 
 	if (access("/proc/self/setgroups", F_OK) == 0) {
-		sprintf(path, "/proc/%d/setgroups", cpid1);
+		sprintf(path, "/proc/%d/setgroups", cpid);
 		SAFE_FILE_PRINTF(path, "deny");
 	}
 
-	updatemap(cpid1, UID_MAP, 0, parentuid);
-	updatemap(cpid1, GID_MAP, 0, parentgid);
+	updatemap(cpid, UID_MAP, 0, parentuid);
+	updatemap(cpid, GID_MAP, 0, parentgid);
 
 	TST_CHECKPOINT_WAKE(0);
 }
 
+static void setup(void)
+{
+	level = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
+}
+
+static void cleanup(void)
+{
+	SAFE_MUNMAP(level, sizeof(int));
+}
+
 static struct tst_test test = {
 	.setup = setup,
+	.cleanup = cleanup,
 	.test_all = run,
 	.needs_root = 1,
+	.forks_child = 1,
 	.needs_checkpoints = 1,
 	.needs_kconfigs = (const char *[]) {
 		"CONFIG_USER_NS",
-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [LTP] [PATCH v1 08/10] Remove check_newuser from userns testing suite
  2023-02-15 10:16 [LTP] [PATCH v1 00/10] Remove libclone support from userns testing suite Andrea Cervesato via ltp
                   ` (6 preceding siblings ...)
  2023-02-15 10:16 ` [LTP] [PATCH v1 07/10] Refactor userns07 test Andrea Cervesato via ltp
@ 2023-02-15 10:16 ` Andrea Cervesato via ltp
  2023-02-15 10:16 ` [LTP] [PATCH v1 09/10] Remove libclone dependency from userns suite Andrea Cervesato via ltp
  2023-02-15 10:16 ` [LTP] [PATCH v1 10/10] Delete userns_helper.h " Andrea Cervesato via ltp
  9 siblings, 0 replies; 14+ messages in thread
From: Andrea Cervesato via ltp @ 2023-02-15 10:16 UTC (permalink / raw)
  To: ltp

CLONE_NEWUSER is supported from kernel 3.8 and LTP supports all
kernels >= 3.10. For this reason, we can remove any check for
CLONE_NEWUSER support and to make userns common.h a bit simpler.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/containers/userns/common.h | 20 +-------------------
 1 file changed, 1 insertion(+), 19 deletions(-)

diff --git a/testcases/kernel/containers/userns/common.h b/testcases/kernel/containers/userns/common.h
index 635d0f190..9b3a47b69 100644
--- a/testcases/kernel/containers/userns/common.h
+++ b/testcases/kernel/containers/userns/common.h
@@ -8,35 +8,17 @@
 #define COMMON_H
 
 #include "tst_test.h"
-#include "lapi/sched.h"
 
 #define UID_MAP 0
 #define GID_MAP 1
 
-static int dummy_child(void *v)
-{
-	(void)v;
-	return 0;
-}
-
-static inline void check_newuser(void)
-{
-	int pid, status;
-
-	pid = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, dummy_child, NULL);
-	if (pid == -1)
-		tst_brk(TCONF | TTERRNO, "CLONE_NEWUSER not supported");
-
-	SAFE_WAIT(&status);
-}
-
 static inline void updatemap(int cpid, int type, int idnum, int parentmappid)
 {
 	char path[BUFSIZ];
 	char content[BUFSIZ];
 	int fd;
 
-	switch(type) {
+	switch (type) {
 	case UID_MAP:
 		sprintf(path, "/proc/%d/uid_map", cpid);
 		break;
-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [LTP] [PATCH v1 09/10] Remove libclone dependency from userns suite
  2023-02-15 10:16 [LTP] [PATCH v1 00/10] Remove libclone support from userns testing suite Andrea Cervesato via ltp
                   ` (7 preceding siblings ...)
  2023-02-15 10:16 ` [LTP] [PATCH v1 08/10] Remove check_newuser from userns testing suite Andrea Cervesato via ltp
@ 2023-02-15 10:16 ` Andrea Cervesato via ltp
  2023-02-15 10:16 ` [LTP] [PATCH v1 10/10] Delete userns_helper.h " Andrea Cervesato via ltp
  9 siblings, 0 replies; 14+ messages in thread
From: Andrea Cervesato via ltp @ 2023-02-15 10:16 UTC (permalink / raw)
  To: ltp

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 testcases/kernel/containers/userns/Makefile | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/testcases/kernel/containers/userns/Makefile b/testcases/kernel/containers/userns/Makefile
index 80681096d..018ab000b 100644
--- a/testcases/kernel/containers/userns/Makefile
+++ b/testcases/kernel/containers/userns/Makefile
@@ -21,6 +21,6 @@ top_srcdir		?= ../../../..
 include $(top_srcdir)/include/mk/testcases.mk
 include $(abs_srcdir)/../Makefile.inc
 
-LDLIBS			:= -lclone $(LDLIBS) $(CAP_LIBS)
+LDLIBS			:= $(LDLIBS) $(CAP_LIBS)
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* [LTP] [PATCH v1 10/10] Delete userns_helper.h from userns suite
  2023-02-15 10:16 [LTP] [PATCH v1 00/10] Remove libclone support from userns testing suite Andrea Cervesato via ltp
                   ` (8 preceding siblings ...)
  2023-02-15 10:16 ` [LTP] [PATCH v1 09/10] Remove libclone dependency from userns suite Andrea Cervesato via ltp
@ 2023-02-15 10:16 ` Andrea Cervesato via ltp
  9 siblings, 0 replies; 14+ messages in thread
From: Andrea Cervesato via ltp @ 2023-02-15 10:16 UTC (permalink / raw)
  To: ltp

userns_helper.h has been replaced by common.h inside the userns testing
suite.

Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
---
 .../kernel/containers/userns/userns_helper.h  | 59 -------------------
 1 file changed, 59 deletions(-)
 delete mode 100644 testcases/kernel/containers/userns/userns_helper.h

diff --git a/testcases/kernel/containers/userns/userns_helper.h b/testcases/kernel/containers/userns/userns_helper.h
deleted file mode 100644
index be47690ea..000000000
--- a/testcases/kernel/containers/userns/userns_helper.h
+++ /dev/null
@@ -1,59 +0,0 @@
-/*
- * Copyright (c) Huawei Technologies Co., Ltd., 2015
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- *  (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
- * the GNU General Public License for more details.
- */
-
-#include "../libclone/libclone.h"
-#include "test.h"
-#include "safe_macros.h"
-#include <stdbool.h>
-
-#define UID_MAP 0
-#define GID_MAP 1
-
-static int dummy_child(void *v)
-{
-	(void) v;
-	return 0;
-}
-
-static int check_newuser(void)
-{
-	int pid, status;
-
-	pid = do_clone_unshare_test(T_CLONE, CLONE_NEWUSER, dummy_child, NULL);
-	if (pid == -1)
-		tst_brkm(TCONF | TERRNO, NULL, "CLONE_NEWUSER not supported");
-	SAFE_WAIT(NULL, &status);
-
-	return 0;
-}
-
-LTP_ATTRIBUTE_UNUSED static int updatemap(int cpid, bool type, int idnum,
-	int parentmappid, void (*cleanup)(void))
-{
-	char path[BUFSIZ];
-	char content[BUFSIZ];
-	int fd;
-
-	if (type == UID_MAP)
-		sprintf(path, "/proc/%d/uid_map", cpid);
-	else if (type == GID_MAP)
-		sprintf(path, "/proc/%d/gid_map", cpid);
-	else
-		tst_brkm(TBROK, cleanup, "invalid type parameter");
-
-	sprintf(content, "%d %d 1", idnum, parentmappid);
-	fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
-	SAFE_WRITE(cleanup, SAFE_WRITE_ALL, fd, content, strlen(content));
-	SAFE_CLOSE(cleanup, fd);
-	return 0;
-}
-- 
2.35.3


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply related	[flat|nested] 14+ messages in thread

* Re: [LTP] [PATCH v1 01/10] Refactor userns01 test
  2023-02-15 10:16 ` [LTP] [PATCH v1 01/10] Refactor userns01 test Andrea Cervesato via ltp
@ 2023-02-28 10:46   ` Richard Palethorpe
  2023-02-28 10:52     ` Richard Palethorpe
  0 siblings, 1 reply; 14+ messages in thread
From: Richard Palethorpe @ 2023-02-28 10:46 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: ltp

Hello,

Andrea Cervesato via ltp <ltp@lists.linux.it> writes:

> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
> ---
>  testcases/kernel/containers/userns/userns01.c | 27 +++++++------------
>  1 file changed, 10 insertions(+), 17 deletions(-)
>
> diff --git a/testcases/kernel/containers/userns/userns01.c b/testcases/kernel/containers/userns/userns01.c
> index 8ed7a9f41..cbe0da245 100644
> --- a/testcases/kernel/containers/userns/userns01.c
> +++ b/testcases/kernel/containers/userns/userns01.c
> @@ -20,9 +20,9 @@
>  #define _GNU_SOURCE
>  
>  #include <stdio.h>
> -#include "common.h"
>  #include "config.h"
>  #include <sys/capability.h>
> +#include "lapi/sched.h"
>  
>  #define OVERFLOWUIDPATH "/proc/sys/kernel/overflowuid"
>  #define OVERFLOWGIDPATH "/proc/sys/kernel/overflowgid"
> @@ -30,10 +30,7 @@
>  static long overflowuid;
>  static long overflowgid;
>  
> -/*
> - * child_fn1() - Inside a new user namespace
> - */
> -static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
> +static void child_fn1(void)
>  {
>  	int uid, gid;
>  	cap_t caps;
> @@ -45,10 +42,8 @@ static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
>  
>  	tst_res(TINFO, "USERNS test is running in a new user namespace.");
>  
> -	if (uid != overflowuid || gid != overflowgid)
> -		tst_res(TFAIL, "got unexpected uid=%d gid=%d", uid, gid);
> -	else
> -		tst_res(TPASS, "got expected uid and gid");
> +	TST_EXP_EQ_LI(uid, overflowuid);
> +	TST_EXP_EQ_LI(gid, overflowgid);
>  
>  	caps = cap_get_proc();
>  
> @@ -68,31 +63,29 @@ static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
>  		tst_res(TFAIL, "unexpected effective/permitted caps at %d", i);
>  	else
>  		tst_res(TPASS, "expected capabilities");
> -
> -	return 0;
>  }
>  
>  static void setup(void)
>  {
> -	check_newuser();

User namespaces have been in the kernel a long time, but they can be
disabled at compile time.

So we need to check for CONFIG_USER_NS in the kernel config.

> -
>  	SAFE_FILE_SCANF(OVERFLOWUIDPATH, "%ld", &overflowuid);
>  	SAFE_FILE_SCANF(OVERFLOWGIDPATH, "%ld", &overflowgid);
>  }
>  
>  static void run(void)
>  {
> -	int pid;
> +	const struct tst_clone_args args = { CLONE_NEWUSER, SIGCHLD };
>  
> -	pid = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, child_fn1, NULL);
> -	if (pid < 0)
> -		tst_brk(TBROK | TTERRNO, "clone failed");
> +	if (!SAFE_CLONE(&args)) {
> +		child_fn1();
> +		return;
> +	}
>  }
>  
>  static struct tst_test test = {
>  	.setup = setup,
>  	.test_all = run,
>  	.needs_root = 1,
> +	.forks_child = 1,
>  	.caps = (struct tst_cap []) {
>  		TST_CAP(TST_CAP_DROP, CAP_NET_RAW),
>  		{}
> -- 
> 2.35.3


-- 
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [LTP] [PATCH v1 01/10] Refactor userns01 test
  2023-02-28 10:46   ` Richard Palethorpe
@ 2023-02-28 10:52     ` Richard Palethorpe
  0 siblings, 0 replies; 14+ messages in thread
From: Richard Palethorpe @ 2023-02-28 10:52 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: ltp

Hello,

Richard Palethorpe <rpalethorpe@suse.de> writes:

> Hello,
>
> Andrea Cervesato via ltp <ltp@lists.linux.it> writes:
>
>> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
>> ---
>>  testcases/kernel/containers/userns/userns01.c | 27 +++++++------------
>>  1 file changed, 10 insertions(+), 17 deletions(-)
>>
>> diff --git a/testcases/kernel/containers/userns/userns01.c b/testcases/kernel/containers/userns/userns01.c
>> index 8ed7a9f41..cbe0da245 100644
>> --- a/testcases/kernel/containers/userns/userns01.c
>> +++ b/testcases/kernel/containers/userns/userns01.c
>> @@ -20,9 +20,9 @@
>>  #define _GNU_SOURCE
>>  
>>  #include <stdio.h>
>> -#include "common.h"
>>  #include "config.h"
>>  #include <sys/capability.h>
>> +#include "lapi/sched.h"
>>  
>>  #define OVERFLOWUIDPATH "/proc/sys/kernel/overflowuid"
>>  #define OVERFLOWGIDPATH "/proc/sys/kernel/overflowgid"
>> @@ -30,10 +30,7 @@
>>  static long overflowuid;
>>  static long overflowgid;
>>  
>> -/*
>> - * child_fn1() - Inside a new user namespace
>> - */
>> -static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
>> +static void child_fn1(void)
>>  {
>>  	int uid, gid;
>>  	cap_t caps;
>> @@ -45,10 +42,8 @@ static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
>>  
>>  	tst_res(TINFO, "USERNS test is running in a new user namespace.");
>>  
>> -	if (uid != overflowuid || gid != overflowgid)
>> -		tst_res(TFAIL, "got unexpected uid=%d gid=%d", uid, gid);
>> -	else
>> -		tst_res(TPASS, "got expected uid and gid");
>> +	TST_EXP_EQ_LI(uid, overflowuid);
>> +	TST_EXP_EQ_LI(gid, overflowgid);
>>  
>>  	caps = cap_get_proc();
>>  
>> @@ -68,31 +63,29 @@ static int child_fn1(LTP_ATTRIBUTE_UNUSED void *arg)
>>  		tst_res(TFAIL, "unexpected effective/permitted caps at %d", i);
>>  	else
>>  		tst_res(TPASS, "expected capabilities");
>> -
>> -	return 0;
>>  }
>>  
>>  static void setup(void)
>>  {
>> -	check_newuser();
>
> User namespaces have been in the kernel a long time, but they can be
> disabled at compile time.
>
> So we need to check for CONFIG_USER_NS in the kernel config.

Ah, ignore that, I see it's already there.

>
>> -
>>  	SAFE_FILE_SCANF(OVERFLOWUIDPATH, "%ld", &overflowuid);
>>  	SAFE_FILE_SCANF(OVERFLOWGIDPATH, "%ld", &overflowgid);
>>  }
>>  
>>  static void run(void)
>>  {
>> -	int pid;
>> +	const struct tst_clone_args args = { CLONE_NEWUSER, SIGCHLD };
>>  
>> -	pid = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, child_fn1, NULL);
>> -	if (pid < 0)
>> -		tst_brk(TBROK | TTERRNO, "clone failed");
>> +	if (!SAFE_CLONE(&args)) {
>> +		child_fn1();
>> +		return;
>> +	}
>>  }
>>  
>>  static struct tst_test test = {
>>  	.setup = setup,
>>  	.test_all = run,
>>  	.needs_root = 1,
>> +	.forks_child = 1,
>>  	.caps = (struct tst_cap []) {
>>  		TST_CAP(TST_CAP_DROP, CAP_NET_RAW),
>>  		{}
>> -- 
>> 2.35.3


-- 
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: [LTP] [PATCH v1 07/10] Refactor userns07 test
  2023-02-15 10:16 ` [LTP] [PATCH v1 07/10] Refactor userns07 test Andrea Cervesato via ltp
@ 2023-02-28 11:23   ` Richard Palethorpe
  0 siblings, 0 replies; 14+ messages in thread
From: Richard Palethorpe @ 2023-02-28 11:23 UTC (permalink / raw)
  To: Andrea Cervesato; +Cc: ltp

Hello,

All merged, thanks!

This one I made some changes to, please see below.

Andrea Cervesato via ltp <ltp@lists.linux.it> writes:

> Signed-off-by: Andrea Cervesato <andrea.cervesato@suse.com>
> ---
>  testcases/kernel/containers/userns/userns07.c | 67 +++++++++++--------
>  1 file changed, 38 insertions(+), 29 deletions(-)
>
> diff --git a/testcases/kernel/containers/userns/userns07.c b/testcases/kernel/containers/userns/userns07.c
> index 4659de636..3a693b8e3 100644
> --- a/testcases/kernel/containers/userns/userns07.c
> +++ b/testcases/kernel/containers/userns/userns07.c
> @@ -16,80 +16,89 @@
>  #include <sys/wait.h>
>  #include "common.h"
>  #include "tst_test.h"
> +#include "lapi/sched.h"
>  
>  #define MAXNEST 32
>  
> -static void setup(void)
> -{
> -	check_newuser();
> -}
> +static int *level;

AFAICT we do not need this variable and to use mmap. We can just pass an
int to child_fn1.

Unless you wanted to check at the end of the test that level == 32?
However you weren't doing that, so I just removed it.

>  
> -static int child_fn1(void *arg)
> +static void child_fn1(void)
>  {
> -	pid_t cpid1;
> -	long level = (long)arg;
> -	int status;
> +	const struct tst_clone_args args = { CLONE_NEWUSER, SIGCHLD };
> +	pid_t cpid;
>  	int parentuid;
>  	int parentgid;
> +	int status;
>  
>  	TST_CHECKPOINT_WAIT(0);
>  
> -	if (level == MAXNEST) {
> +	if (*level == MAXNEST) {
>  		tst_res(TPASS, "nested all children");
> -		return 0;
> +		return;
>  	}
>  
> -	cpid1 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, (void *)child_fn1, (void *)(level + 1));
> -	if (cpid1 < 0) {
> -		tst_res(TFAIL | TERRNO, "level %ld, unexpected error", level);
> -		return 1;
> +	cpid = SAFE_CLONE(&args);
> +	if (!cpid) {
> +		*level += 1;
> +		child_fn1();
> +		return;
>  	}
>  
>  	parentuid = geteuid();
>  	parentgid = getegid();
>  
> -	updatemap(cpid1, UID_MAP, 0, parentuid);
> -	updatemap(cpid1, GID_MAP, 0, parentgid);
> +	updatemap(cpid, UID_MAP, 0, parentuid);
> +	updatemap(cpid, GID_MAP, 0, parentgid);
>  
>  	TST_CHECKPOINT_WAKE(0);
>  
> -	SAFE_WAITPID(cpid1, &status, 0);
> -
> -	if (!WIFEXITED(status) || WEXITSTATUS(status) != 0)
> -		tst_brk(TBROK, "child %s", tst_strstatus(status));
> -
> -	return 0;
> +	SAFE_WAITPID(cpid, &status, 0);

replaced with tst_reap_children.

>  }
>  
>  static void run(void)
>  {
> -	pid_t cpid1;
> +	const struct tst_clone_args args = { CLONE_NEWUSER, SIGCHLD };
> +	pid_t cpid;
>  	int parentuid;
>  	int parentgid;
>  	char path[BUFSIZ];
>  
> -	cpid1 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD, (void *)child_fn1, (void *)0);
> -	if (cpid1 < 0)
> -		tst_brk(TBROK | TTERRNO, "clone failed");
> +	cpid = SAFE_CLONE(&args);
> +	if (!cpid) {
> +		child_fn1();
> +		return;
> +	}
>  
>  	parentuid = geteuid();
>  	parentgid = getegid();
>  
>  	if (access("/proc/self/setgroups", F_OK) == 0) {
> -		sprintf(path, "/proc/%d/setgroups", cpid1);
> +		sprintf(path, "/proc/%d/setgroups", cpid);
>  		SAFE_FILE_PRINTF(path, "deny");
>  	}
>  
> -	updatemap(cpid1, UID_MAP, 0, parentuid);
> -	updatemap(cpid1, GID_MAP, 0, parentgid);
> +	updatemap(cpid, UID_MAP, 0, parentuid);
> +	updatemap(cpid, GID_MAP, 0, parentgid);
>  
>  	TST_CHECKPOINT_WAKE(0);
>  }
>  
> +static void setup(void)
> +{
> +	level = SAFE_MMAP(NULL, sizeof(int), PROT_READ | PROT_WRITE, MAP_SHARED | MAP_ANONYMOUS, -1, 0);
> +}
> +
> +static void cleanup(void)
> +{
> +	SAFE_MUNMAP(level, sizeof(int));
> +}
> +
>  static struct tst_test test = {
>  	.setup = setup,
> +	.cleanup = cleanup,
>  	.test_all = run,
>  	.needs_root = 1,
> +	.forks_child = 1,
>  	.needs_checkpoints = 1,
>  	.needs_kconfigs = (const char *[]) {
>  		"CONFIG_USER_NS",
> -- 
> 2.35.3


-- 
Thank you,
Richard.

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2023-02-28 11:28 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2023-02-15 10:16 [LTP] [PATCH v1 00/10] Remove libclone support from userns testing suite Andrea Cervesato via ltp
2023-02-15 10:16 ` [LTP] [PATCH v1 01/10] Refactor userns01 test Andrea Cervesato via ltp
2023-02-28 10:46   ` Richard Palethorpe
2023-02-28 10:52     ` Richard Palethorpe
2023-02-15 10:16 ` [LTP] [PATCH v1 02/10] Refactor userns02 test Andrea Cervesato via ltp
2023-02-15 10:16 ` [LTP] [PATCH v1 03/10] Refactor userns03 test Andrea Cervesato via ltp
2023-02-15 10:16 ` [LTP] [PATCH v1 04/10] Refactor userns04 test Andrea Cervesato via ltp
2023-02-15 10:16 ` [LTP] [PATCH v1 05/10] Refactor userns05 test Andrea Cervesato via ltp
2023-02-15 10:16 ` [LTP] [PATCH v1 06/10] Refactor userns06 test Andrea Cervesato via ltp
2023-02-15 10:16 ` [LTP] [PATCH v1 07/10] Refactor userns07 test Andrea Cervesato via ltp
2023-02-28 11:23   ` Richard Palethorpe
2023-02-15 10:16 ` [LTP] [PATCH v1 08/10] Remove check_newuser from userns testing suite Andrea Cervesato via ltp
2023-02-15 10:16 ` [LTP] [PATCH v1 09/10] Remove libclone dependency from userns suite Andrea Cervesato via ltp
2023-02-15 10:16 ` [LTP] [PATCH v1 10/10] Delete userns_helper.h " Andrea Cervesato via ltp

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.