Skip to contentSkip to conversation

Add SAFE_SEMCTL() and SAFE_SEMGET() macro

PATCH New Feiyu Zhu 2 participants 6 messages Dec 22, 2020 – Dec 23, 2020 Library code

lore
Share & tools
lore

Feiyu Zhu

author
#0 lore raw

PATCH 1/3 Add SAFE_SEMCTL() and SAFE_SEMGET() macro

---
 include/tst_safe_sysv_ipc.h | 14 +++++++++++++
 lib/tst_safe_sysv_ipc.c     | 48 +++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)
include/tst_safe_sysv_ipc.h +14 -0
index 3e0e50e..bb65326 100644
@@ -9,6 +9,7 @@
 #include <sys/ipc.h>
 #include <sys/msg.h>
 #include <sys/shm.h>
+#include <sys/sem.h>
 
 int safe_msgget(const char *file, const int lineno, key_t key, int msgflg);
 #define SAFE_MSGGET(key, msgflg) \
@@ -51,4 +52,17 @@ int safe_shmctl(const char *file, const int lineno, int shmid, int cmd,
 	(shmid) = ((cmd) == IPC_RMID ? -1 : (shmid)); \
 	tst_ret_;})
 
+int safe_semget(const char *file, const int lineno, key_t key, int nsems,
+		int semflg);
+#define SAFE_SEMGET(key, nsems, semflg) \
+	safe_semget(__FILE__, __LINE__, (key), (nsems), (semflg))
+
+int safe_semctl(const char *file, const int lineno, int semid, int semnum,
+		int cmd, ...);
+#define SAFE_SEMCTL(semid, semnum, cmd, ...) ({ \
+	int tst_ret_ = safe_semctl(__FILE__, __LINE__, (semid), (semnum), \
+				(cmd), ##__VA_ARGS__); \
+	(semid) = ((cmd) == IPC_RMID ? -1 : (semid)); \
+	tst_ret_; })
+
 #endif /* TST_SAFE_SYSV_IPC_H__ */
lib/tst_safe_sysv_ipc.c +48 -0

60 lines not on this page · raw message

Signature · 1 line
1.8.3.1
author
#1 lore raw

PATCH 2/3 lapi/sem.h: Add SEM_STAT_ANY

---
 include/lapi/sem.h | 15 +++++++++++++++
 1 file changed, 15 insertions(+)
 create mode 100644 include/lapi/sem.h
include/lapi/sem.h new file +15 -0
new file mode 100644
index 0000000..5d86a2f
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Feiyu Zhu <[email protected]>
+ */
+
+#ifndef LAPI_SEM_H
+#define LAPI_SEM_H
+
+#ifndef SEM_STAT_ANY
+# define SEM_STAT_ANY 20
+#endif
+
+#endif /* LAPI_SEM_H */
+
Signature · 1 line
1.8.3.1
#2 lore raw

PATCH 2/3 lapi/sem.h: Add SEM_STAT_ANY

Hi Feiyu

Quoted 12 lines
---
  include/lapi/sem.h | 15 +++++++++++++++
  1 file changed, 15 insertions(+)

create mode 100644 include/lapi/sem.h

include/lapi/sem.h new file +0 -0
new file mode 100644
index 0000000..5d86a2f

include/lapi/sem.h line 6

Quoted 6 lines
include/lapi/sem.h new file +6 -0
@@ -0,0 +1,15 @@
+// SPDX-License-Identifier: GPL-2.0-or-later
+/*
+ * Copyright (c) 2020 FUJITSU LIMITED. All rights reserved.
+ * Author: Feiyu Zhu<[email protected]>
+ */
+

I prefer to add #include <sys/sem.h> in here, so we only need to include lapi/sem.h in subsequent semctl09.c

Quoted 9 lines
include/lapi/sem.h new file +9 -0
+#ifndef LAPI_SEM_H
+#define LAPI_SEM_H
+
+#ifndef SEM_STAT_ANY
+# define SEM_STAT_ANY 20
+#endif
+
+#endif /* LAPI_SEM_H */
+
author
#3 lore raw

PATCH 3/3 syscalls/ipc: semctl09: add a test for SEM_STAT_ANY

In reply to Feiyu Zhu

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

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

We also test SEM_STAT_ANY by calling semctl() directly by syscall(), because glibc have a bug that caused fails to pass the buffer specified by the caller to the kernel.

We parse /proc/sysvipc/sem and check that the information is consistent with the content of seminfo structure.

---
 runtest/syscalls                                |   1 +
 runtest/syscalls-ipc                            |   1 +
 testcases/kernel/syscalls/ipc/semctl/.gitignore |   1 +
 testcases/kernel/syscalls/ipc/semctl/Makefile   |   2 +-
 testcases/kernel/syscalls/ipc/semctl/semctl09.c | 182 ++++++++++++++++++++++++
 5 files changed, 186 insertions(+), 1 deletion(-)
 create mode 100644 testcases/kernel/syscalls/ipc/semctl/semctl09.c
runtest/syscalls +1 -0
index 28174dd..1549916 100644
@@ -1179,6 +1179,7 @@ semctl05 semctl05
 semctl06 semctl06
 semctl07 semctl07
 semctl08 semctl08
+semctl09 semctl09
 
 semget01 semget01
 semget02 semget02
runtest/syscalls-ipc +1 -0
index 9524b1a..68fff40 100644
@@ -37,6 +37,7 @@ semctl05 semctl05
 semctl06 semctl06
 semctl07 semctl07
 semctl08 semctl08
+semctl09 semctl09
 
 semget01 semget01
 semget02 semget02
testcases/kernel/syscalls/ipc/semctl/.gitignore +1 -0
index 6189a04..87d8393 100644
@@ -6,3 +6,4 @@
 /semctl06
 /semctl07
 /semctl08
+/semctl09
testcases/kernel/syscalls/ipc/semctl/Makefile +1 -1
index f711e77..42f8a94 100644
@@ -8,6 +8,6 @@ LTPLIBS = ltpipc ltpnewipc
 include $(top_srcdir)/include/mk/testcases.mk
 
 semctl01 semctl02 semctl03 semctl04 semctl05 semctl06 semctl07: LTPLDLIBS = -lltpipc
-semctl08: LTPLDLIBS = -lltpnewipc
+semctl08 semctl09: LTPLDLIBS = -lltpnewipc
 
 include $(top_srcdir)/include/mk/generic_leaf_target.mk
testcases/kernel/syscalls/ipc/semctl/semctl09.c new file +182 -0

182 lines not on this page · raw message

Signature · 1 line
1.8.3.1
#4 lore raw

PATCH 3/3 syscalls/ipc: semctl09: add a test for SEM_STAT_ANY

Hi Feiyu

testcases/kernel/syscalls/ipc/semctl/semctl09.c line 43

Quoted 8 lines
testcases/kernel/syscalls/ipc/semctl/semctl09.c new file +8 -0
+#include<stdio.h>
+#include<pwd.h>
+#include<sys/sem.h>
+#include "tst_test.h"
+#include "tst_safe_sysv_ipc.h"
+#include "libnewipc.h"
+#include "lapi/semun.h"
+#include "lapi/sem.h"

I doubt do we really need two lapi headers, maybe we can remove semun.h and remove union struct definition into lapi/sem.h.

testcases/kernel/syscalls/ipc/semctl/semctl09.c

Quoted 9 lines
testcases/kernel/syscalls/ipc/semctl/semctl09.c new file +9 -0
+#include "lapi/syscalls.h"
+
+static int sem_id = -1;
+static uid_t nobody_uid, root_uid;
+static union semun un;
+
+static inline int tst_sys_semctl(int semid, int semnum, int cmd)
+{
+	return tst_syscall(__NR_semctl, semid, semnum, cmd,&un.buf);

It looks like semctl man-pages has wrong description. We should use sem_ds struct instead of seminfo struct. I have sent a patch[1] to man-pages community.

[1]https://github.com/linux-mailinglist-archives/linux-man.vger.kernel.org.0/commit/f2bda64c45a38ba7c895716908321f34ddd25cdc

testcases/kernel/syscalls/ipc/semctl/semctl09.c line 59

Quoted 7 lines
testcases/kernel/syscalls/ipc/semctl/semctl09.c new file +7 -0
+}
+
+static inline int tst_semctl(int semid, int semnum, int cmd)
+{
+	return semctl(semid, semnum, cmd,&un.buf);
+}
+

Since we test glibc and syscall, I think we should use test_variants like semop case.

Quoted 123 lines
testcases/kernel/syscalls/ipc/semctl/semctl09.c new file +123 -0
+static struct tcases {
+	uid_t *uid;
+	int (*test_semctl) ();
+	char *desc;
+} tests[] = {
+	{&nobody_uid, tst_sys_semctl, "with nobody user by syscall()",},
+	{&nobody_uid, tst_semctl, "with nobody user",},
+	{&root_uid, tst_sys_semctl, "with root user by syscall()",},
+	{&root_uid, tst_semctl, "with root user",}
+};
+
+static void parse_proc_sysvipc(struct seminfo *info)
+{
+	FILE *f = fopen("/proc/sysvipc/sem", "r");
+	int semset_cnt = 0;
+	int sem_cnt = 0;
+
+	/* Eat header */
+	for (;;) {
+		int c = fgetc(f);
+
+		if (c == '\n' || c == EOF)
+			break;
+	}
+
+	int nsems;
+	/*
+	 * Sum sem set, nsems for all elements listed, which should equal
+	 * the data returned in the seminfo structure.
+	 */
+	while (fscanf(f, "%*i %*i %*i %i %*i %*i %*i %*i %*i %*i",
+		&nsems)>  0){
+		semset_cnt++;
+		sem_cnt += nsems;
+	}
+
+	if (info->semusz != semset_cnt) {
+		tst_res(TFAIL, "semusz = %i, expected %i",
+				info->semusz, semset_cnt);
+	} else {
+		tst_res(TPASS, "semset_cnt = %i", semset_cnt);
+	}
+
+	if (info->semaem != sem_cnt) {
+		tst_res(TFAIL, "semaem = %i, expected %i",
+				info->semaem, sem_cnt);
+	} else {
+		tst_res(TPASS, "sen_cnt = %i", sem_cnt);
+	}
+
+	fclose(f);
+}
+
+static void verify_semctl(unsigned int n)
+{
+	struct tcases *tc =&tests[n];
+	int i, semid, cnt = 0;
+	struct seminfo info;
+	union semun arg;
+
+	tst_res(TINFO, "Test SEM_STAT_ANY %s", tc->desc);
+
+	SAFE_SETEUID(*tc->uid);
+
+	arg.__buf =&info;
+
+	TEST(semctl(sem_id, 0, SEM_INFO, arg));
+
+	if (TST_RET == -1) {
+		tst_res(TFAIL | TTERRNO, "semctl(sem_id, 0, SEM_INFO, ...)");
+		return;
+	}
+
+	semid = (*tc->test_semctl) (TST_RET, 0, SEM_STAT_ANY);
+
+	if (errno == EFAULT) {
+		tst_res(TFAIL, "SEM_STAT_ANY doesn't pass the buffer "
+				"specified by the caller to kernel");
+		return;
+	} else if (semid == -1) {
+		tst_res(TFAIL | TTERRNO, "SEM_INFO haven't returned a valid index");
+	} else {
+		tst_res(TPASS, "SEM_INFO returned valid index %li to semid %i",
+			TST_RET, semid);
+	}
+
+	for (i = 0; i<= TST_RET; i++) {
+		if (((*tc->test_semctl) (i, 0, SEM_STAT_ANY)) != -1)
+			cnt++;
+	}
+
+	if (cnt == info.semusz) {
+		tst_res(TPASS, "Counted used = %i", cnt);
+	} else {
+		tst_res(TFAIL, "Counted used = %i, semuse = %i",
+			cnt, info.semusz);
+	}
+
+	parse_proc_sysvipc(&info);
+}
+
+static void setup(void)
+{
+	struct passwd *ltpuser = SAFE_GETPWNAM("nobody");
+	nobody_uid = ltpuser->pw_uid;
+	root_uid = 0;
+
+	sem_id = SAFE_SEMGET(IPC_PRIVATE, 2, IPC_CREAT | 0600);
+}
+
+static void cleanup(void)
+{
+	if (sem_id>= 0)
+		SAFE_SEMCTL(sem_id, 0, IPC_RMID);
+}
+
+static struct tst_test test = {
+	.setup = setup,
+	.cleanup = cleanup,
+	.test = verify_semctl,
+	.tcnt = ARRAY_SIZE(tests),
+	.needs_root = 1,
+};
#5 lore raw

PATCH 1/3 Add SAFE_SEMCTL() and SAFE_SEMGET() macro

In reply to Feiyu Zhu

Hi Feiyu

Quoted 9 lines
lib/tst_safe_sysv_ipc.c +9 -0
+int safe_semctl(const char *file, const int lineno, int semid, int semnum,
+		int cmd, ...)
+{
+	int rval;
+	va_list va;
+	union semun un;
+
+	va_start(va, cmd);
+

lib/tst_safe_sysv_ipc.c int safe_shmctl(const char *file, const int lineno, int shmid, int cmd, · line 218

Quoted 10 lines
lib/tst_safe_sysv_ipc.c +10 -0
+	un = va_arg(va, union semun);
+
+	va_end(va);
+
+	rval = semctl(semid, semnum, cmd, un);
+
+	if (rval == -1) {
+		tst_brk_(file, lineno, TBROK | TERRNO,
+		"semctl(%i, %i, %i,...) failed", semid, semnum, cmd);
+	} else if (ret_check(cmd, rval)) {

It looks good to me,

BTW, we can add some cmds in ret_check function in separate patch.
From semctl man-pages, it looks like SETALL/SETVAL also return 0 on
success. Also for shmctl man-pages, SHM_LOCK/SHM_UNLOCK also return 0 on success. When we write these new cases in future, these cmds should be added into ret_check function.

Best Regards
Yang Xu

Quoted 8 lines
lib/tst_safe_sysv_ipc.c +7 -1
+		tst_brk_(file, lineno, TBROK | TERRNO,
+			"Invalid semctl(%i, %i, %i,...) return value %d", semid,
+			semnum, cmd, rval);
+	}
+
+	return rval;
+}
--