public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Yuan Sun <sunyuan3@huawei.com>
To: Jan Stancek <jstancek@redhat.com>
Cc: ltp-list@lists.sourceforge.net
Subject: Re: [LTP] [PATCH v5] containers: new testcase userns04
Date: Thu, 25 Jun 2015 09:39:45 +0800	[thread overview]
Message-ID: <558B5BE1.5040602@huawei.com> (raw)
In-Reply-To: <cfc70376b0c3835b040bef9125f2917926a56ec9.1435139859.git.jstancek@redhat.com>

Hi Jan,
     Excellent. I didn't think that TEST_LOOPING can't be used in this 
case. You are very
professional. I agree with you. Could you please merge the patch v5?
     Thank you very much for your help.
     Yuan


On 2015/6/24 18:09, Jan Stancek wrote:
> From: Yuan Sun <sunyuan3@huawei.com>
>
> If a namespace isn't another namespace's ancestor, the process in
> first namespace does not have the CAP_SYS_ADMIN capability in the
> second namespace and the setns() call fails.
>
> Signed-off-by: Yuan Sun <sunyuan3@huawei.com>
> Signed-off-by: Jan Stancek <jstancek@redhat.com>
> ---
>   runtest/containers                            |   1 +
>   testcases/kernel/containers/.gitignore        |   1 +
>   testcases/kernel/containers/userns/userns04.c | 151 ++++++++++++++++++++++++++
>   3 files changed, 153 insertions(+)
>   create mode 100644 testcases/kernel/containers/userns/userns04.c
>
> diff --git a/runtest/containers b/runtest/containers
> index bb1beb6..fd9c06a 100644
> --- a/runtest/containers
> +++ b/runtest/containers
> @@ -70,3 +70,4 @@ mountns04 mountns04
>   
>   userns01 userns01
>   userns02 userns02
> +userns04 userns04
> diff --git a/testcases/kernel/containers/.gitignore b/testcases/kernel/containers/.gitignore
> index e3c92c9..6c5785f 100644
> --- a/testcases/kernel/containers/.gitignore
> +++ b/testcases/kernel/containers/.gitignore
> @@ -5,3 +5,4 @@ mountns/mountns03
>   mountns/mountns04
>   userns/userns01
>   userns/userns02
> +userns/userns04
> diff --git a/testcases/kernel/containers/userns/userns04.c b/testcases/kernel/containers/userns/userns04.c
> new file mode 100644
> index 0000000..206c9ed
> --- /dev/null
> +++ b/testcases/kernel/containers/userns/userns04.c
> @@ -0,0 +1,151 @@
> +/*
> + * 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.
> + */
> +
> +/*
> + * Verify that:
> + *  If a namespace isn't another namespace's ancestor, the process in
> + *  first namespace does not have the CAP_SYS_ADMIN capability in the
> + *  second namespace and the setns() call fails.
> + */
> +
> +#define _GNU_SOURCE
> +#include <sys/wait.h>
> +#include <assert.h>
> +#include <stdio.h>
> +#include <stdlib.h>
> +#include <unistd.h>
> +#include <string.h>
> +#include <errno.h>
> +#include "test.h"
> +#include "userns_helper.h"
> +
> +char *TCID = "user_namespace4";
> +int TST_TOTAL = 1;
> +
> +static void setup(void)
> +{
> +	check_newuser();
> +	tst_tmpdir();
> +	TST_CHECKPOINT_INIT(NULL);
> +}
> +
> +static void cleanup(void)
> +{
> +	tst_rmdir();
> +}
> +
> +static int child_fn1(void *arg LTP_ATTRIBUTE_UNUSED)
> +{
> +	TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
> +	return 0;
> +}
> +
> +static int child_fn2(void *arg)
> +{
> +	int exit_val = 0;
> +	int ret;
> +
> +	ret = setns(((long)arg), CLONE_NEWUSER);
> +	if (ret != -1) {
> +		printf("child2 setns() unexpected success\n");
> +		exit_val = 1;
> +	} else if (errno != EPERM) {
> +		printf("child2 setns() unexpected error: (%d) %s\n",
> +			errno, strerror(errno));
> +		exit_val = 1;
> +	}
> +
> +	TST_SAFE_CHECKPOINT_WAIT(NULL, 1);
> +	return exit_val;
> +}
> +
> +static int wait4child(pid_t pid, const char *msg)
> +{
> +	int status;
> +
> +	if (waitpid(pid, &status, 0) == -1) {
> +		tst_brkm(TBROK | TERRNO, cleanup, "waitpid");
> +	} else if (WIFSIGNALED(status)) {
> +		tst_resm(TFAIL, "%s: child was killed with signal = %d",
> +			msg, WTERMSIG(status));
> +		return WTERMSIG(status);
> +	} else if (!WIFEXITED(status) || WEXITSTATUS(status) != 0) {
> +		tst_resm(TFAIL, "%s: child returns %d", msg, status);
> +		return WEXITSTATUS(status);
> +	}
> +
> +	return 0;
> +}
> +
> +static void test_cap_sys_admin(void)
> +{
> +	pid_t cpid1, cpid2, cpid3;
> +	char path[BUFSIZ];
> +	int fd, status;
> +
> +	/* child 1 */
> +	cpid1 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD,
> +		(void *)child_fn1, NULL);
> +	if (cpid1 < 0)
> +		tst_brkm(TFAIL | TERRNO, cleanup, "clone failed");
> +
> +	/* child 2 */
> +	sprintf(path, "/proc/%d/ns/user", cpid1);
> +	fd = SAFE_OPEN(cleanup, path, O_RDONLY, 0644);
> +	cpid2 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD,
> +		(void *)child_fn2, (void *)((long)fd));
> +	if (cpid2 < 0)
> +		tst_brkm(TFAIL | TERRNO, cleanup, "clone failed");
> +
> +	/* child 3 - throw-away process changing ns to child1 */
> +	switch (cpid3 = fork()) {
> +	case -1:
> +		tst_brkm(TBROK | TERRNO, cleanup, "fork");
> +	case 0:
> +		if (setns(fd, CLONE_NEWUSER) == -1) {
> +			printf("parent pid setns failure: (%d) %s",
> +				errno, strerror(errno));
> +			exit(1);
> +		}
> +		exit(0);
> +	}
> +
> +	TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
> +	TST_SAFE_CHECKPOINT_WAKE(cleanup, 1);
> +
> +	status = 0;
> +	status |= wait4child(cpid1, "child1");
> +	status |= wait4child(cpid2, "child2");
> +	status |= wait4child(cpid3, "child3");
> +	if (status == 0)
> +		tst_resm(TPASS, "The setns function works well.");
> +
> +	SAFE_CLOSE(cleanup, fd);
> +
> +}
> +
> +int main(int argc, char *argv[])
> +{
> +	int lc;
> +
> +	setup();
> +	tst_parse_opts(argc, argv, NULL, NULL);
> +
> +	for (lc = 0; TEST_LOOPING(lc); lc++) {
> +		tst_count = 0;
> +		test_cap_sys_admin();
> +	}
> +
> +	cleanup();
> +	tst_exit();
> +}


------------------------------------------------------------------------------
Monitor 25 network devices or servers for free with OpManager!
OpManager is web-based network management software that monitors 
network devices and physical & virtual servers, alerts via email & sms 
for fault. Monitor 25 devices for free with no restriction. Download now
http://ad.doubleclick.net/ddm/clk/292181274;119417398;o
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

  reply	other threads:[~2015-06-25  1:40 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-06-24 10:09 [LTP] [PATCH v5] containers: new testcase userns04 Jan Stancek
2015-06-25  1:39 ` Yuan Sun [this message]
2015-06-26 10:08   ` Jan Stancek

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=558B5BE1.5040602@huawei.com \
    --to=sunyuan3@huawei.com \
    --cc=jstancek@redhat.com \
    --cc=ltp-list@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox