All of lore.kernel.org
 help / color / mirror / Atom feed
* [LTP] [PATCH 1/3] syscalls/ipc: shmctl04: Test SHM_STAT_ANY with nobody user
@ 2020-12-08 10:07 Feiyu Zhu
  2020-12-08 10:07 ` [LTP] [PATCH 2/3] lapi/msg.h: Add MSG_STAT_ANY Feiyu Zhu
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Feiyu Zhu @ 2020-12-08 10:07 UTC (permalink / raw)
  To: ltp

Use the nobody user to test the SHM_STAT_ANY is not checked for read access.

Signed-off-by: Feiyu Zhu <zhufy.jy@cn.fujitsu.com>
---
 testcases/kernel/syscalls/ipc/shmctl/shmctl04.c | 26 +++++++++++++++++++++++--
 1 file changed, 24 insertions(+), 2 deletions(-)

diff --git a/testcases/kernel/syscalls/ipc/shmctl/shmctl04.c b/testcases/kernel/syscalls/ipc/shmctl/shmctl04.c
index bf3c854..9ba40f9 100644
--- a/testcases/kernel/syscalls/ipc/shmctl/shmctl04.c
+++ b/testcases/kernel/syscalls/ipc/shmctl/shmctl04.c
@@ -19,6 +19,7 @@
 
 #define _GNU_SOURCE
 #include <stdio.h>
+#include <pwd.h>
 #include "tst_test.h"
 #include "tst_safe_sysv_ipc.h"
 #include "libnewipc.h"
@@ -27,6 +28,16 @@
 #define SHM_SIZE 2048
 
 static int shm_id = -1;
+static struct passwd *ltpuser;
+static uid_t nobody_uid, root_uid;
+
+static struct tcases {
+	uid_t *uid;
+	char *desc;
+} tests[] = {
+	{&nobody_uid, "with nobody user"},
+	{&root_uid, "with root user"}
+};
 
 static void parse_proc_sysvipc(struct shm_info *info)
 {
@@ -96,12 +107,17 @@ static void parse_proc_sysvipc(struct shm_info *info)
 	fclose(f);
 }
 
-static void verify_shminfo(void)
+static void verify_shminfo(unsigned int n)
 {
+	struct tcases *tc = &tests[n];
 	struct shm_info info;
 	struct shmid_ds ds;
 	int i, shmid, cnt = 0;
 
+	tst_res(TINFO, "Test SHM_STAT_ANY %s", tc->desc);
+
+	SAFE_SETEUID(*tc->uid);
+
 	TEST(shmctl(0, SHM_INFO, (struct shmid_ds *)&info));
 
 	if (TST_RET == -1) {
@@ -136,6 +152,10 @@ static void verify_shminfo(void)
 
 static void setup(void)
 {
+	ltpuser = SAFE_GETPWNAM("nobody");
+	nobody_uid = ltpuser->pw_uid;
+	root_uid = 0;
+
 	shm_id = SAFE_SHMGET(IPC_PRIVATE, SHM_SIZE, IPC_CREAT | SHM_RW);
 }
 
@@ -148,5 +168,7 @@ static void cleanup(void)
 static struct tst_test test = {
 	.setup = setup,
 	.cleanup = cleanup,
-	.test_all = verify_shminfo,
+	.test = verify_shminfo,
+	.tcnt = ARRAY_SIZE(tests),
+	.needs_root = 1,
 };
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH 2/3] lapi/msg.h: Add MSG_STAT_ANY
  2020-12-08 10:07 [LTP] [PATCH 1/3] syscalls/ipc: shmctl04: Test SHM_STAT_ANY with nobody user Feiyu Zhu
@ 2020-12-08 10:07 ` Feiyu Zhu
  2020-12-08 10:07 ` [LTP] [PATCH 3/3] syscalls/ipc: msgctl06: Add a test for MSG_STAT_ANY Feiyu Zhu
  2020-12-08 15:37 ` [LTP] [PATCH 1/3] syscalls/ipc: shmctl04: Test SHM_STAT_ANY with nobody user Cyril Hrubis
  2 siblings, 0 replies; 5+ messages in thread
From: Feiyu Zhu @ 2020-12-08 10:07 UTC (permalink / raw)
  To: ltp

Signed-off-by: Feiyu Zhu <zhufy.jy@cn.fujitsu.com>
---
 include/lapi/msg.h | 4 ++++
 1 file changed, 4 insertions(+)

diff --git a/include/lapi/msg.h b/include/lapi/msg.h
index d649f33..0295111 100644
--- a/include/lapi/msg.h
+++ b/include/lapi/msg.h
@@ -12,4 +12,8 @@
 # define MSG_COPY  040000  /* copy (not remove) all queue messages */
 #endif
 
+#ifndef MSG_STAT_ANY
+# define MSG_STAT_ANY 13
+#endif
+
 #endif
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH 3/3] syscalls/ipc: msgctl06: Add a test for MSG_STAT_ANY
  2020-12-08 10:07 [LTP] [PATCH 1/3] syscalls/ipc: shmctl04: Test SHM_STAT_ANY with nobody user Feiyu Zhu
  2020-12-08 10:07 ` [LTP] [PATCH 2/3] lapi/msg.h: Add MSG_STAT_ANY Feiyu Zhu
@ 2020-12-08 10:07 ` Feiyu Zhu
  2020-12-08 15:46   ` Cyril Hrubis
  2020-12-08 15:37 ` [LTP] [PATCH 1/3] syscalls/ipc: shmctl04: Test SHM_STAT_ANY with nobody user Cyril Hrubis
  2 siblings, 1 reply; 5+ messages in thread
From: Feiyu Zhu @ 2020-12-08 10:07 UTC (permalink / raw)
  To: ltp

Validate the content of the msginfo structure and the return value.

The return value is highest used index to a kernel table, so we call
msgctl() with MSG_STAT_ANY which shouldn't fail if the value is correct.

We also parse /proc/sysvipc/msg and check that the information is
consistent with the content of msginfo structure.

It refers to shmctl04.

Signed-off-by: Feiyu Zhu <zhufy.jy@cn.fujitsu.com>
---
 runtest/syscalls                                |   1 +
 runtest/syscalls-ipc                            |   1 +
 testcases/kernel/syscalls/ipc/msgctl/.gitignore |   1 +
 testcases/kernel/syscalls/ipc/msgctl/msgctl06.c | 156 ++++++++++++++++++++++++
 4 files changed, 159 insertions(+)
 create mode 100644 testcases/kernel/syscalls/ipc/msgctl/msgctl06.c

diff --git a/runtest/syscalls b/runtest/syscalls
index 08ffd04..409800d 100644
--- a/runtest/syscalls
+++ b/runtest/syscalls
@@ -800,6 +800,7 @@ msgctl02 msgctl02
 msgctl03 msgctl03
 msgctl04 msgctl04
 msgctl05 msgctl05
+msgctl06 msgctl06
 msgstress01 msgstress01
 msgstress02 msgstress02
 msgstress03 msgstress03
diff --git a/runtest/syscalls-ipc b/runtest/syscalls-ipc
index 57878b4..9524b1a 100644
--- a/runtest/syscalls-ipc
+++ b/runtest/syscalls-ipc
@@ -3,6 +3,7 @@ msgctl02 msgctl02
 msgctl03 msgctl03
 msgctl04 msgctl04
 msgctl05 msgctl05
+msgctl06 msgctl06
 msgstress01 msgstress01
 msgstress02 msgstress02
 msgstress03 msgstress03
diff --git a/testcases/kernel/syscalls/ipc/msgctl/.gitignore b/testcases/kernel/syscalls/ipc/msgctl/.gitignore
index 0157f46..ed10a8d 100644
--- a/testcases/kernel/syscalls/ipc/msgctl/.gitignore
+++ b/testcases/kernel/syscalls/ipc/msgctl/.gitignore
@@ -3,4 +3,5 @@
 /msgctl03
 /msgctl04
 /msgctl05
+/msgctl06
 /msgctl12
diff --git a/testcases/kernel/syscalls/ipc/msgctl/msgctl06.c b/testcases/kernel/syscalls/ipc/msgctl/msgctl06.c
new file mode 100644
index 0000000..8d44e3a
--- /dev/null
+++ b/testcases/kernel/syscalls/ipc/msgctl/msgctl06.c
@@ -0,0 +1,156 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Feiyu Zhu <zhufy.jy@cn.fujitsu.com>
+ */
+/*
+ * Call msgctl() with MSG_INFO flag and check that:
+ *
+ * * The returned index points to a valid MSG by calling MSG_STAT_ANY
+ * * Also count that valid indexes < returned max index sums up to used_ids
+ * * And the data are consistent with /proc/sysvipc/msg
+ *
+ * There is a possible race between the call to the msgctl() and read from the
+ * proc file so this test cannot be run in parallel with any IPC testcases that
+ * adds or removes MSG queues.
+ *
+ * Note what we create a MSG segment in the test setup and send msg to make sure
+ * that there is@least one during the testrun.
+ */
+
+#include <stdio.h>
+#include <pwd.h>
+#include "tst_test.h"
+#include "tst_safe_sysv_ipc.h"
+#include "libnewipc.h"
+#include "lapi/msg.h"
+
+static int msg_id = -1;
+static struct passwd *ltpuser;
+static uid_t nobody_uid, root_uid;
+
+static struct tcases {
+	uid_t *uid;
+	char *desc;
+} tests[] = {
+	{&nobody_uid, "with nobody user"},
+	{&root_uid, "with root user"}
+};
+
+static void parse_proc_sysvipc(struct msginfo *info)
+{
+	FILE *f = fopen("/proc/sysvipc/msg", "r");
+	int used_ids = 0;
+	int queue_nums = 0;
+	int msg_bytes = 0;
+
+	/* Eat header */
+	for (;;) {
+		int c = fgetc(f);
+
+		if (c == '\n' || c == EOF)
+			break;
+	}
+
+	int msgid, cbytes, qnum;
+
+	/*
+	 * Sum queue and byte for all elements listed, which should equal
+	 * the data returned in the msginfo structure.
+	 */
+	while (fscanf(f, "%*i %i %*i %i %i %*i %*i %*i %*i %*i %*i %*i %*i %*i",
+			&msgid, &cbytes, &qnum) > 0){
+		used_ids++;
+		queue_nums += qnum;
+		msg_bytes += cbytes;
+	}
+
+	if (info->msgpool != used_ids) {
+		tst_res(TFAIL, "msgpool = %i, expected %i",
+			info->msgpool, used_ids);
+	} else {
+		tst_res(TPASS, "used_ids = %i", used_ids);
+	}
+
+	if (info->msgmap != queue_nums) {
+		tst_res(TFAIL, "msgmap = %i, expected %i",
+			info->msgpool, queue_nums);
+	} else {
+		tst_res(TPASS, "msgnums = %i", queue_nums);
+	}
+
+	if (info->msgtql != msg_bytes) {
+		tst_res(TFAIL, "msgtql = %i, expected %i",
+			info->msgtql, msg_bytes);
+	} else {
+		tst_res(TPASS, "msgbytes = %i", msg_bytes);
+	}
+
+	fclose(f);
+}
+
+static void verify_msgctl(unsigned int n)
+{
+	struct tcases *tc = &tests[n];
+	int i, msgid, cnt = 0;
+	struct msqid_ds buf;
+	struct msginfo info;
+
+	tst_res(TINFO, "Test MSG_STAT_ANY %s", tc->desc);
+
+	SAFE_SETEUID(*tc->uid);
+
+	TEST(msgctl(0, MSG_INFO, (struct msgid_ds *)&info));
+
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "msgctl(0, MSG_INFO, ...)");
+		return;
+	}
+
+	msgid = msgctl(TST_RET, MSG_STAT_ANY, &buf);
+
+	if (msgid == -1) {
+		tst_res(TFAIL | TTERRNO, "MSG_INFO haven't returned a valid index");
+	} else {
+		tst_res(TPASS, "MSG_INFO returned valid index %li to msgid %i",
+			TST_RET, msgid);
+	}
+
+	for (i = 0; i <= TST_RET; i++) {
+		if (msgctl(i, MSG_STAT_ANY, &buf) != -1)
+			cnt++;
+	}
+
+	if (cnt == info.msgpool) {
+		tst_res(TPASS, "Counted used = %i", cnt);
+	} else {
+		tst_res(TFAIL, "Counted used = %i, msgpool = %i",
+			cnt, info.msgpool);
+	}
+
+	parse_proc_sysvipc(&info);
+}
+
+static void setup(void)
+{
+	ltpuser = SAFE_GETPWNAM("nobody");
+	nobody_uid = ltpuser->pw_uid;
+	root_uid = 0;
+
+	msg_id = SAFE_MSGGET(IPC_PRIVATE, IPC_CREAT | MSG_RW);
+	SAFE_MSGSND(msg_id, "abcd", 4, 0);
+}
+
+static void cleanup(void)
+{
+	if (msg_id >= 0)
+		SAFE_MSGCTL(msg_id, IPC_RMID, NULL);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_msgctl,
+	.tcnt = ARRAY_SIZE(tests),
+	.needs_root = 1,
+};
-- 
1.8.3.1




^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [LTP] [PATCH 1/3] syscalls/ipc: shmctl04: Test SHM_STAT_ANY with nobody user
  2020-12-08 10:07 [LTP] [PATCH 1/3] syscalls/ipc: shmctl04: Test SHM_STAT_ANY with nobody user Feiyu Zhu
  2020-12-08 10:07 ` [LTP] [PATCH 2/3] lapi/msg.h: Add MSG_STAT_ANY Feiyu Zhu
  2020-12-08 10:07 ` [LTP] [PATCH 3/3] syscalls/ipc: msgctl06: Add a test for MSG_STAT_ANY Feiyu Zhu
@ 2020-12-08 15:37 ` Cyril Hrubis
  2 siblings, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2020-12-08 15:37 UTC (permalink / raw)
  To: ltp

Hi!
Pushed with a minor change, thanks.

> +static struct passwd *ltpuser;

I've moved this structure to the test setup() since there is no point in
having otherwise unused global variable.

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply	[flat|nested] 5+ messages in thread

* [LTP] [PATCH 3/3] syscalls/ipc: msgctl06: Add a test for MSG_STAT_ANY
  2020-12-08 10:07 ` [LTP] [PATCH 3/3] syscalls/ipc: msgctl06: Add a test for MSG_STAT_ANY Feiyu Zhu
@ 2020-12-08 15:46   ` Cyril Hrubis
  0 siblings, 0 replies; 5+ messages in thread
From: Cyril Hrubis @ 2020-12-08 15:46 UTC (permalink / raw)
  To: ltp

Hi!
Pushed with a few changes, thanks.

* Reformatted the top level description so that it's picked by the
  docparser

* Renamed the variables in the proc parsing function to make it more
  clear what they referring to

+ Fixed some typos.

Full diff:

diff --git a/testcases/kernel/syscalls/ipc/msgctl/msgctl06.c b/testcases/kernel/syscalls/ipc/msgctl/msgctl06.c
index c931bbfd3..99e44851c 100644
--- a/testcases/kernel/syscalls/ipc/msgctl/msgctl06.c
+++ b/testcases/kernel/syscalls/ipc/msgctl/msgctl06.c
@@ -3,7 +3,9 @@
  * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
  * Author: Feiyu Zhu <zhufy.jy@cn.fujitsu.com>
  */
-/*
+/*\
+ * [DESCRIPTION]
+ *
  * Call msgctl() with MSG_INFO flag and check that:
  *
  * * The returned index points to a valid MSG by calling MSG_STAT_ANY
@@ -16,7 +18,10 @@
  *
  * Note what we create a MSG segment in the test setup and send msg to make sure
  * that there is at least one during the testrun.
- */
+ *
+ * Also note that for MSG_INFO the members of the msginfo structure have
+ * completely different meaning than their names seems to suggest.
+\*/
 
 #include <stdio.h>
 #include <pwd.h>
@@ -40,8 +45,8 @@ static struct tcases {
 static void parse_proc_sysvipc(struct msginfo *info)
 {
 	FILE *f = fopen("/proc/sysvipc/msg", "r");
-	int used_ids = 0;
-	int queue_nums = 0;
+	int queue_cnt = 0;
+	int msg_cnt = 0;
 	int msg_bytes = 0;
 
 	/* Eat header */
@@ -52,38 +57,38 @@ static void parse_proc_sysvipc(struct msginfo *info)
 			break;
 	}
 
-	int msgid, cbytes, qnum;
+	int cbytes, msgs;
 
 	/*
 	 * Sum queue and byte for all elements listed, which should equal
 	 * the data returned in the msginfo structure.
 	 */
-	while (fscanf(f, "%*i %i %*i %i %i %*i %*i %*i %*i %*i %*i %*i %*i %*i",
-			&msgid, &cbytes, &qnum) > 0){
-		used_ids++;
-		queue_nums += qnum;
+	while (fscanf(f, "%*i %*i %*i %i %i %*i %*i %*i %*i %*i %*i %*i %*i %*i",
+	              &cbytes, &msgs) > 0){
+		queue_cnt++;
+		msg_cnt += msgs;
 		msg_bytes += cbytes;
 	}
 
-	if (info->msgpool != used_ids) {
+	if (info->msgpool != queue_cnt) {
 		tst_res(TFAIL, "msgpool = %i, expected %i",
-			info->msgpool, used_ids);
+			info->msgpool, queue_cnt);
 	} else {
-		tst_res(TPASS, "used_ids = %i", used_ids);
+		tst_res(TPASS, "queue_cnt = %i", queue_cnt);
 	}
 
-	if (info->msgmap != queue_nums) {
+	if (info->msgmap != msg_cnt) {
 		tst_res(TFAIL, "msgmap = %i, expected %i",
-			info->msgpool, queue_nums);
+			info->msgpool, msg_cnt);
 	} else {
-		tst_res(TPASS, "msgnums = %i", queue_nums);
+		tst_res(TPASS, "msg_cnt = %i", msg_cnt);
 	}
 
 	if (info->msgtql != msg_bytes) {
 		tst_res(TFAIL, "msgtql = %i, expected %i",
 			info->msgtql, msg_bytes);
 	} else {
-		tst_res(TPASS, "msgbytes = %i", msg_bytes);
+		tst_res(TPASS, "msg_bytes = %i", msg_bytes);
 	}
 
 	fclose(f);
@@ -100,7 +105,7 @@ static void verify_msgctl(unsigned int n)
 
 	SAFE_SETEUID(*tc->uid);
 
-	TEST(msgctl(0, MSG_INFO, (struct msgid_ds *)&info));
+	TEST(msgctl(0, MSG_INFO, (struct msqid_ds *)&info));
 
 	if (TST_RET == -1) {
 		tst_res(TFAIL | TTERRNO, "msgctl(0, MSG_INFO, ...)");

-- 
Cyril Hrubis
chrubis@suse.cz

^ permalink raw reply related	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2020-12-08 15:46 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-12-08 10:07 [LTP] [PATCH 1/3] syscalls/ipc: shmctl04: Test SHM_STAT_ANY with nobody user Feiyu Zhu
2020-12-08 10:07 ` [LTP] [PATCH 2/3] lapi/msg.h: Add MSG_STAT_ANY Feiyu Zhu
2020-12-08 10:07 ` [LTP] [PATCH 3/3] syscalls/ipc: msgctl06: Add a test for MSG_STAT_ANY Feiyu Zhu
2020-12-08 15:46   ` Cyril Hrubis
2020-12-08 15:37 ` [LTP] [PATCH 1/3] syscalls/ipc: shmctl04: Test SHM_STAT_ANY with nobody user Cyril Hrubis

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.