syscalls/ipc: shmctl04: Test SHM_STAT_ANY with nobody user
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>
Reviewed-by: Cyril Hrubis <chrubis@suse.cz>
diff --git a/testcases/kernel/syscalls/ipc/shmctl/shmctl04.c b/testcases/kernel/syscalls/ipc/shmctl/shmctl04.c
index bf3c854..f4235ff 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,15 @@
#define SHM_SIZE 2048
static int shm_id = -1;
+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 +106,17 @@
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 +151,10 @@
static void setup(void)
{
+ struct passwd *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 +167,7 @@
static struct tst_test test = {
.setup = setup,
.cleanup = cleanup,
- .test_all = verify_shminfo,
+ .test = verify_shminfo,
+ .tcnt = ARRAY_SIZE(tests),
+ .needs_root = 1,
};