syscalls/ipc: shmctl04: Test SHM_STAT_ANY with nobody user
PATCH 1/3 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.
--- testcases/kernel/syscalls/ipc/shmctl/shmctl04.c | 26 +++++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/ipc/shmctl/shmctl04.c +24 -2
@@ -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 2048static 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 , };
Signature · 1 line
1.8.3.1
PATCH 2/3 lapi/msg.h: Add MSG_STAT_ANY
--- include/lapi/msg.h | 4 ++++ 1 file changed, 4 insertions(+)
include/lapi/msg.h +4 -0
@@ -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
Signature · 1 line
1.8.3.1
PATCH 3/3 syscalls/ipc: msgctl06: Add a test for MSG_STAT_ANY
In reply to Feiyu ZhuValidate 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.
--- 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
runtest/syscalls +1 -0
@@ -800,6 +800,7 @@ msgctl02 msgctl02 msgctl03 msgctl03 msgctl04 msgctl04 msgctl05 msgctl05 +msgctl06 msgctl06 msgstress01 msgstress01 msgstress02 msgstress02 msgstress03 msgstress03
runtest/syscalls-ipc +1 -0
@@ -3,6 +3,7 @@ msgctl02 msgctl02 msgctl03 msgctl03 msgctl04 msgctl04 msgctl05 msgctl05 +msgctl06 msgctl06 msgstress01 msgstress01 msgstress02 msgstress02 msgstress03 msgstress03
testcases/kernel/syscalls/ipc/msgctl/.gitignore +1 -0
@@ -3,4 +3,5 @@ /msgctl03 /msgctl04 /msgctl05 +/msgctl06 /msgctl12
testcases/kernel/syscalls/ipc/msgctl/msgctl06.c new file +156 -0
156 lines not on this page · raw message
Signature · 1 line
1.8.3.1
PATCH 3/3 syscalls/ipc: msgctl06: Add a test for MSG_STAT_ANY
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:
testcases/kernel/syscalls/ipc/msgctl/msgctl06.c +22 -17
@@ -3,7 +3,9 @@* Copyright (c) 2020 FUJITSU LIMITED. All rights reserved. * Author: Feiyu Zhu <[email protected]> */ -/* +/*\ +* [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, ...)" );
Signature · 2 lines
Cyril Hrubis [email protected]
PATCH 1/3 syscalls/ipc: shmctl04: Test SHM_STAT_ANY with nobody user
In reply to Feiyu ZhuHi!
Pushed with a minor change, thanks.
Quoted 1 line
+static struct passwd *ltpuser;
I've moved this structure to the test setup() since there is no point in having otherwise unused global variable.
Signature · 2 lines
Cyril Hrubis [email protected]