Skip to contentSkip to conversation

userns03: setgroups can't be reenabled

PATCH New Yuan Sun 2 participants 2 messages Sep 28, 2015 – Oct 5, 2015

lore
Share & tools
lore

Yuan Sun

author
#0 lore raw

PATCH 1/1 userns03: setgroups can't be reenabled

If the setgroups file has the value "deny", then the setgroups(2) system call can't subsequently be reenabled (by writing "allow" to the file) in this user namespace. (Attempts to do so will fail with the error EPERM.)

---
 testcases/kernel/containers/userns/userns03.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
testcases/kernel/containers/userns/userns03.c +21 -4
index 26a710d..0602689 100644
@@ -61,7 +61,6 @@
 char *TCID = "user_namespace3";
 int TST_TOTAL = 1;
 static int cpid1, parentuid, parentgid;
-static bool setgroupstag = true;
 
 /*
  * child_fn1() - Inside a new user namespace
@@ -158,8 +157,6 @@ static void setup(void)
 	check_newuser();
 	tst_tmpdir();
 	TST_CHECKPOINT_INIT(NULL);
-	if (access("/proc/self/setgroups", F_OK) == 0)
-		setgroupstag = false;
 }
 
 int main(int argc, char *argv[])
@@ -168,6 +165,7 @@ int main(int argc, char *argv[])
 	char path[BUFSIZ];
 	int lc;
 	int fd;
+	int ret;
 
 	tst_parse_opts(argc, argv, NULL, NULL);
 	setup();
@@ -190,11 +188,30 @@ int main(int argc, char *argv[])
 			tst_brkm(TBROK | TERRNO, cleanup,
 				"cpid2 clone failed");
 
-		if (setgroupstag == false) {
+		if (access("/proc/self/setgroups", F_OK) == 0) {
 			sprintf(path, "/proc/%d/setgroups", cpid1);
 			fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
 			SAFE_WRITE(cleanup, 1, fd, "deny", 4);
 			SAFE_CLOSE(cleanup, fd);
+			/* If the setgroups file has the value "deny",
+			 * then the setgroups(2) system call can't
+			 * subsequently be reenabled (by writing "allow" to
+			 * the file) in this user namespace.  (Attempts to
+			 * do so will fail with the error EPERM.)
+			*/
+			fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
+			ret = write(fd, "allow", 5);
+
+			if (ret != -1) {
+				tst_brkm(TBROK | TERRNO, cleanup,
+					"write action should be failed");
+			} else if (errno != EPERM) {
+				tst_brkm(TBROK | TERRNO, cleanup,
+					"unexpected error: (%d) %s\n",
+					errno, strerror(errno));
+			}
+
+			SAFE_CLOSE(cleanup, fd);
 
 			sprintf(path, "/proc/%d/setgroups", cpid2);
 			fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
Signature · 1 line
1.9.1
#1 lore raw

PATCH 1/1 userns03: setgroups can't be reenabled

----- Original Message -----

Quoted 6 lines

From: "Yuan Sun" <[email protected]>
To: [email protected]
Cc: [email protected]
Sent: Monday, 28 September, 2015 6:57:56 AM
Subject: [PATCH] userns03: setgroups can't be reenabled

Quoted 6 lines

If the setgroups file has the value "deny", then the setgroups(2) system call can't subsequently be reenabled (by writing "allow" to the file) in this user namespace. (Attempts to do so will fail with the error EPERM.)

Pushed with some small changes (see comments below).

Regards,
Jan

Quoted 50 lines
---
 testcases/kernel/containers/userns/userns03.c | 25 +++++++++++++++++++++----
 1 file changed, 21 insertions(+), 4 deletions(-)
testcases/kernel/containers/userns/userns03.c +8 -4
index 26a710d..0602689 100644
@@ -61,7 +61,6 @@
 char *TCID = "user_namespace3";
 int TST_TOTAL = 1;
 static int cpid1, parentuid, parentgid;
-static bool setgroupstag = true;
 
 /*
  * child_fn1() - Inside a new user namespace
@@ -158,8 +157,6 @@ static void setup(void)
 	check_newuser();
 	tst_tmpdir();
 	TST_CHECKPOINT_INIT(NULL);
-	if (access("/proc/self/setgroups", F_OK) == 0)
-		setgroupstag = false;
 }
 
 int main(int argc, char *argv[])
@@ -168,6 +165,7 @@ int main(int argc, char *argv[])
 	char path[BUFSIZ];
 	int lc;
 	int fd;
+	int ret;
 
 	tst_parse_opts(argc, argv, NULL, NULL);
 	setup();
@@ -190,11 +188,30 @@ int main(int argc, char *argv[])
 			tst_brkm(TBROK | TERRNO, cleanup,
 				"cpid2 clone failed");
 
-		if (setgroupstag == false) {
+		if (access("/proc/self/setgroups", F_OK) == 0) {
 			sprintf(path, "/proc/%d/setgroups", cpid1);
 			fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
 			SAFE_WRITE(cleanup, 1, fd, "deny", 4);
 			SAFE_CLOSE(cleanup, fd);
+			/* If the setgroups file has the value "deny",
+			 * then the setgroups(2) system call can't
+			 * subsequently be reenabled (by writing "allow" to
+			 * the file) in this user namespace.  (Attempts to
+			 * do so will fail with the error EPERM.)
+			*/

testcases/kernel/containers/userns/userns03.c int main(int argc, char *argv[]) · line 211

Quoted 10 lines
testcases/kernel/containers/userns/userns03.c +10 -0
+			fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
+			ret = write(fd, "allow", 5);
+
+			if (ret != -1) {
+				tst_brkm(TBROK | TERRNO, cleanup,
+					"write action should be failed");
+			} else if (errno != EPERM) {
+				tst_brkm(TBROK | TERRNO, cleanup,
+					"unexpected error: (%d) %s\n",
+					errno, strerror(errno));

You already use TERRNO, so adding extra "errno, strerror(errno)" is not needed.

testcases/kernel/containers/userns/userns03.c int main(int argc, char *argv[]) · line 212

Quoted 1 line
testcases/kernel/containers/userns/userns03.c +1 -0
+			}

I added TPASS here.

Quoted 9 lines
testcases/kernel/containers/userns/userns03.c +2 -1
+
+			SAFE_CLOSE(cleanup, fd);
 
 			sprintf(path, "/proc/%d/setgroups", cpid2);
 			fd = SAFE_OPEN(cleanup, path, O_WRONLY, 0644);
--

1.9.1