[LTP] [PATCH v1 01/10] Refactor userns01 test

Richard Palethorpe rpalethorpe@suse.de
Tue Feb 28 11:46:12 CET 2023


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.


More information about the ltp mailing list