From: Yuan Sun <sunyuan3@huawei.com>
To: jstancek@redhat.com
Cc: ltp-list@lists.sourceforge.net
Subject: Re: [LTP] [PATCH V3] containers: new testcase userns04
Date: Wed, 17 Jun 2015 12:55:01 +0800 [thread overview]
Message-ID: <5580FDA5.80809@huawei.com> (raw)
In-Reply-To: <1434545793-3731-1-git-send-email-sunyuan3@huawei.com>
On 2015/6/17 20:56, Yuan Sun wrote:
> If a namespace isn't aother namespace's ancestor, the 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>
> ---
> testcases/kernel/containers/userns/userns04.c | 134 ++++++++++++++++++++++++++
> 1 file changed, 134 insertions(+)
> create mode 100644 testcases/kernel/containers/userns/userns04.c
>
> diff --git a/testcases/kernel/containers/userns/userns04.c b/testcases/kernel/containers/userns/userns04.c
> new file mode 100644
> index 0000000..c1690c9
> --- /dev/null
> +++ b/testcases/kernel/containers/userns/userns04.c
> @@ -0,0 +1,134 @@
> +/*
> + * 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 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();
> +}
> +
> +/*
> + * child_fn1() - Inside a new user namespace
> + */
> +static int child_fn1(void)
> +{
> + TST_SAFE_CHECKPOINT_WAIT(cleanup, 0);
It is my fault. I removed cleanup from child_fn1 and forgot to rm this
cleanup.
> + return 0;
> +}
> +
> +static int child_fn2(int *arg)
> +{
> + int exit_val;
> +
> + if (setns(*arg, CLONE_NEWUSER) == -1) {
> + printf("child2 setns() failure is expected\n");
> + exit_val = 0;
> + } else {
> + printf("chile2 setns() success isn't expected\n");
> + exit_val = 1;
> + }
> +
> + TST_SAFE_CHECKPOINT_WAIT(NULL, 1);
> + return exit_val;
> +}
> +
> +int main(int argc, char *argv[])
> +{
> + int cpid1status, cpid2status;
> + int cpid1, cpid2;
> + char path[BUFSIZ];
> + int fd;
> +
> + tst_parse_opts(argc, argv, NULL, NULL);
> + setup();
> +/*
> +TEST_LOOPING isn't used because the parent pid can't reset to the
> +previous namespace after it joins another namespace by setns.
> +*/
> + tst_count = 0;
> + cpid1 = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD,
> + (void *)child_fn1, NULL);
> +
> + if (cpid1 < 0)
> + tst_brkm(TFAIL | TERRNO, cleanup, "clone failed");
> +
> + 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, &fd);
> +
> + if (cpid2 < 0)
> + tst_brkm(TFAIL | TERRNO, cleanup, "clone failed");
> +
> + if (setns(fd, CLONE_NEWUSER) == -1)
> + tst_brkm(TFAIL, cleanup, "parent pid setns failure");
> + else
> + tst_resm(TINFO, "parent pid setns success");
> +
> + TST_SAFE_CHECKPOINT_WAKE(cleanup, 0);
> + TST_SAFE_CHECKPOINT_WAKE(cleanup, 1);
> +
> + if ((waitpid(cpid1, &cpid1status, 0) < 0) ||
> + (waitpid(cpid2, &cpid2status, 0) < 0))
> + tst_brkm(TBROK | TERRNO, cleanup,
> + "parent: waitpid failed.");
> +
> + if (WIFSIGNALED(cpid1status)) {
> + tst_resm(TFAIL, "child1 was killed with signal = %d",
> + WTERMSIG(cpid1status));
> + } else if (WIFEXITED(cpid1status) &&
> + WEXITSTATUS(cpid1status) != 0) {
> + tst_resm(TFAIL, "child1 exited abnormally");
> + }
> +
> + if (WIFSIGNALED(cpid2status)) {
> + tst_resm(TFAIL, "child2 was killed with signal = %d",
> + WTERMSIG(cpid2status));
> + } else if (WIFEXITED(cpid2status) &&
> + WEXITSTATUS(cpid2status) != 0) {
> + tst_resm(TFAIL, "child2 exited abnormally");
> + } else
> + tst_resm(TPASS, "The setns function works well.");
> +
> + cleanup();
> + tst_exit();
> +}
> +
------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list
prev parent reply other threads:[~2015-06-17 4:55 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-06-17 12:56 [LTP] [PATCH V3] containers: new testcase userns04 Yuan Sun
2015-06-17 4:55 ` Yuan Sun [this message]
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=5580FDA5.80809@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