diff mbox series

[v1,01/10] Refactor userns01 test

Message ID 20230215101615.27534-2-andrea.cervesato@suse.com
State Accepted
Headers show
Series Remove libclone support from userns testing suite | expand

Commit Message

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

Comments

Richard Palethorpe Feb. 28, 2023, 10:46 a.m. UTC | #1
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
Richard Palethorpe Feb. 28, 2023, 10:52 a.m. UTC | #2
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
diff mbox series

Patch

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),
 		{}