The user ID and group ID, which are inside a container, can
be modified by its parent process.

Signed-off-by: Yuan Sun <sunyu...@huawei.com>
---
 runtest/containers                            |   1 +
 testcases/kernel/containers/.gitignore        |   1 +
 testcases/kernel/containers/userns/userns02.c | 113 ++++++++++++++++++++++++++
 3 files changed, 115 insertions(+)
 create mode 100644 testcases/kernel/containers/userns/userns02.c

diff --git a/runtest/containers b/runtest/containers
index ca10372..bb1beb6 100644
--- a/runtest/containers
+++ b/runtest/containers
@@ -69,3 +69,4 @@ mountns03 mountns03
 mountns04 mountns04
 
 userns01 userns01
+userns02 userns02
diff --git a/testcases/kernel/containers/.gitignore 
b/testcases/kernel/containers/.gitignore
index 4478b53..e3c92c9 100644
--- a/testcases/kernel/containers/.gitignore
+++ b/testcases/kernel/containers/.gitignore
@@ -4,3 +4,4 @@ mountns/mountns02
 mountns/mountns03
 mountns/mountns04
 userns/userns01
+userns/userns02
diff --git a/testcases/kernel/containers/userns/userns02.c 
b/testcases/kernel/containers/userns/userns02.c
new file mode 100644
index 0000000..6a4b36d
--- /dev/null
+++ b/testcases/kernel/containers/userns/userns02.c
@@ -0,0 +1,113 @@
+/*
+ * 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:
+ *  The user ID and group ID, which are inside a container, can be modified
+ * by its parent process.
+ */
+
+#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_namespace2";
+int TST_TOTAL = 1;
+
+int childpid;
+int parentuid;
+int parentgid;
+char path[BUFSIZ];
+char content[BUFSIZ];
+static int fd;
+/*
+ * child_fn1() - Inside a new user namespace
+ */
+static int child_fn1(void)
+{
+       int exit_val;
+       int uid, gid;
+
+       TST_SAFE_CHECKPOINT_WAIT(NULL, 0);
+       uid = geteuid();
+       gid = getegid();
+
+       printf("USERNS test is running in a new user namespace.\n");
+       if (uid == 100 && gid == 100) {
+               printf("Got expected uid and gid.\n");
+               exit_val = 0;
+       } else {
+               printf("Got unexpected result of uid=%d gid=%d\n", uid, gid);
+               exit_val = 1;
+       }
+
+       return exit_val;
+}
+
+static void setup(void)
+{
+       TST_CHECKPOINT_INIT(NULL);
+       check_newuser();
+}
+
+int main(int argc, char *argv[])
+{
+       int status;
+       int lc;
+
+       tst_parse_opts(argc, argv, NULL, NULL);
+       setup();
+
+       for (lc = 0; TEST_LOOPING(lc); lc++) {
+               tst_count = 0;
+               childpid = ltp_clone_quick(CLONE_NEWUSER | SIGCHLD,
+                       (void *)child_fn1, NULL);
+
+               if (childpid < 0)
+                       tst_brkm(TFAIL | TERRNO, NULL, "clone failed");
+
+               parentuid = geteuid();
+               parentgid = getegid();
+               sprintf(path, "/proc/%d/uid_map", childpid);
+               sprintf(content, "100 %d 1", parentuid);
+               fd = SAFE_OPEN(NULL, path, O_WRONLY, 0644);
+               SAFE_WRITE(NULL, 1, fd, content, strlen(content));
+               sprintf(path, "/proc/%d/gid_map", childpid);
+               sprintf(content, "100 %d 1", parentgid);
+               fd = SAFE_OPEN(NULL, path, O_WRONLY, 0644);
+               SAFE_WRITE(NULL, 1, fd, content, strlen(content));
+
+               TST_SAFE_CHECKPOINT_WAKE(NULL, 0);
+
+               if (waitpid(childpid, &status, 0) < 0)
+                       tst_resm(TBROK | TERRNO, "parent: waitpid failed.");
+
+               if (WIFEXITED(status) && WEXITSTATUS(status) != 0)
+                       tst_resm(TFAIL, "child exited abnormally");
+               else if (WIFSIGNALED(status)) {
+                       tst_resm(TFAIL, "child was killed with signal = %d",
+                               WTERMSIG(status));
+               }
+
+       }
+       tst_resm(TPASS, "the uid and the gid are right inside the container");
+       tst_exit();
+}
+
-- 
1.9.1


------------------------------------------------------------------------------
_______________________________________________
Ltp-list mailing list
Ltp-list@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/ltp-list

Reply via email to