Skip to contentSkip to conversation

syscalls/prctl06: New test for prctl() with PR_{SET, GET}_NO_NEW_PRIVS

PATCH Superseded Yang Xu 2 participants 3 messages Jul 16, 2019 1 message not in the archive

lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): [email protected]

Yang Xu

author
#1 lore raw

PATCH v4 1/1 syscalls/prctl06: New test for prctl() with PR_{SET, GET}_NO_NEW_PRIVS

---
 include/lapi/prctl.h                          |   5 +
 runtest/syscalls                              |   1 +
 testcases/kernel/syscalls/prctl/.gitignore    |   2 +
 testcases/kernel/syscalls/prctl/prctl06.c     | 115 ++++++++++++++++++
 testcases/kernel/syscalls/prctl/prctl06.h     |  64 ++++++++++
 .../kernel/syscalls/prctl/prctl06_execve.c    |  45 +++++++
 6 files changed, 232 insertions(+)
 create mode 100644 testcases/kernel/syscalls/prctl/prctl06.c
 create mode 100644 testcases/kernel/syscalls/prctl/prctl06.h
 create mode 100644 testcases/kernel/syscalls/prctl/prctl06_execve.c
include/lapi/prctl.h +5 -0
index ad0b12bce..54b3da20f 100644
@@ -24,4 +24,9 @@
 # define PR_GET_CHILD_SUBREAPER	37
 #endif
 
+#ifndef PR_SET_NO_NEW_PRIVS
+# define PR_SET_NO_NEW_PRIVS 38
+# define PR_GET_NO_NEW_PRIVS 39
+#endif
+
 #endif /* LAPI_PRCTL_H__ */
runtest/syscalls +1 -0
index 42c200423..ef7af41b5 100644
@@ -865,6 +865,7 @@ prctl02 prctl02
 prctl03 prctl03
 prctl04 prctl04
 prctl05 prctl05
+prctl06 prctl06
 
 pread01 pread01
 pread01_64 pread01_64
testcases/kernel/syscalls/prctl/.gitignore +2 -0
index 9ecaf9854..ee994086f 100644
@@ -3,3 +3,5 @@
 /prctl03
 /prctl04
 /prctl05
+/prctl06
+/prctl06_execve
testcases/kernel/syscalls/prctl/prctl06.c new file +115 -0

115 lines not on this page · raw message

testcases/kernel/syscalls/prctl/prctl06.h new file +64 -0

64 lines not on this page · raw message

testcases/kernel/syscalls/prctl/prctl06_execve.c new file +45 -0

45 lines not on this page · raw message

Signature · 1 line
2.18.1
#2 lore raw

PATCH v4 1/1 syscalls/prctl06: New test for prctl() with PR_{SET, GET}_NO_NEW_PRIVS

Hi!
Pushed with two changes, thanks.

testcases/kernel/syscalls/prctl/prctl06.c +1 -1
index eafbedfef..0e1274a2a 100644
@@ -81,8 +81,8 @@ static void setup(void)
 
 	SAFE_CP(TESTBIN, TEST_REL_BIN_DIR);
 
-	SAFE_CHMOD(BIN_PATH, SUID_MODE);
 	SAFE_CHOWN(BIN_PATH, 0, 0);
+	SAFE_CHMOD(BIN_PATH, SUID_MODE);
 
 	TEST(prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0));
 	if (TST_RET == 0) {

This is actually important fix, you have to set the SUID bits last since they are cleared if you change owner of the file.

testcases/kernel/syscalls/prctl/prctl06.h +6 -4
index 72f9c4e5a..f5c66e809 100644
@@ -25,12 +25,14 @@
 #define BIN_PATH           MNTPOINT"/"TESTBIN
 #define SUID_MODE          (S_ISUID|S_ISGID|S_IXUSR|S_IXGRP|S_IXOTH)
 
-static int flag = 1;
-
 void check_proc_field(int val, char *name)
 {
+	static int flag = 1;
 	int field = 0;
 
+	if (!flag)
+		return;
+
 	TEST(FILE_LINES_SCANF(PROC_STATUS, "NoNewPrivs:%d", &field));
 	if (TST_RET == 1) {
 		tst_res(TCONF,
@@ -57,8 +59,8 @@ void check_no_new_privs(int val, char *name)
 		tst_res(TFAIL,
 			"%s prctl(PR_GET_NO_NEW_PRIVS) expected %d got %ld",
 			name, val, TST_RET);
-	if (flag)
-		check_proc_field(val, name);
+
+	check_proc_field(val, name);
 }
 
 #endif

This is merely cosmetic and moves the flag to the function so that it's not global.

Signature · 2 lines
Cyril Hrubis
[email protected]
author
#3 lore raw

PATCH v4 1/1 syscalls/prctl06: New test for prctl() with PR_{SET, GET}_NO_NEW_PRIVS

Quoted 20 lines

Hi!
Pushed with two changes, thanks.

testcases/kernel/syscalls/prctl/prctl06.c +1 -1
index eafbedfef..0e1274a2a 100644
@@ -81,8 +81,8 @@ static void setup(void)
 
  	SAFE_CP(TESTBIN, TEST_REL_BIN_DIR);
 
-	SAFE_CHMOD(BIN_PATH, SUID_MODE);
  	SAFE_CHOWN(BIN_PATH, 0, 0);
+	SAFE_CHMOD(BIN_PATH, SUID_MODE);
 
  	TEST(prctl(PR_GET_NO_NEW_PRIVS, 0, 0, 0, 0));
  	if (TST_RET == 0) {
Quoted 2 lines

This is actually important fix, you have to set the SUID bits last since they are cleared if you change owner of the file.

Hi Cyril

OK. I learn a lot from this case. Thanks for kindly reply.

Quoted 34 lines
testcases/kernel/syscalls/prctl/prctl06.h +6 -4
index 72f9c4e5a..f5c66e809 100644
@@ -25,12 +25,14 @@
  #define BIN_PATH           MNTPOINT"/"TESTBIN
  #define SUID_MODE          (S_ISUID|S_ISGID|S_IXUSR|S_IXGRP|S_IXOTH)
 
-static int flag = 1;
-
  void check_proc_field(int val, char *name)
  {
+	static int flag = 1;
  	int field = 0;
 
+	if (!flag)
+		return;
+
  	TEST(FILE_LINES_SCANF(PROC_STATUS, "NoNewPrivs:%d",&field));
  	if (TST_RET == 1) {
  		tst_res(TCONF,
@@ -57,8 +59,8 @@ void check_no_new_privs(int val, char *name)
  		tst_res(TFAIL,
  			"%s prctl(PR_GET_NO_NEW_PRIVS) expected %d got %ld",
  			name, val, TST_RET);
-	if (flag)
-		check_proc_field(val, name);
+
+	check_proc_field(val, name);
  }
 
  #endif
Quoted 2 lines

This is merely cosmetic and moves the flag to the function so that it's not global.

Yes. static local variable is better.