xref: /aosp_15_r20/external/ltp/testcases/kernel/syscalls/ipc/msgctl/msgctl04.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  *    03/2001 - Written by Wayne Boyer
5  *    12/03/2008 Matthieu Fertré ([email protected])
6  * Copyright (c) 2018 Cyril Hrubis <[email protected]>
7  */
8 /*
9  * Test for EACCES, EFAULT and EINVAL errors using a variety of incorrect
10  * calls.
11  */
12 #include <errno.h>
13 #include <pwd.h>
14 
15 #include "tst_test.h"
16 #include "tst_safe_sysv_ipc.h"
17 #include "libnewipc.h"
18 #include "lapi/syscalls.h"
19 
20 static int msg_id1 = -1;
21 static int msg_id2 = -1;
22 static int msg_id3 = -1;
23 static int bad_q = -1;
24 
25 struct msqid_ds q_buf;
26 
libc_msgctl(int msqid,int cmd,void * buf)27 static int libc_msgctl(int msqid, int cmd, void *buf)
28 {
29 	return msgctl(msqid, cmd, buf);
30 }
31 
sys_msgctl(int msqid,int cmd,void * buf)32 static int sys_msgctl(int msqid, int cmd, void *buf)
33 {
34 	return tst_syscall(__NR_msgctl, msqid, cmd, buf);
35 }
36 
37 struct tcase {
38 	int *msg_id;
39 	int cmd;
40 	struct msqid_ds *buf;
41 	int error;
42 } tc[] = {
43 	/* EACCES - there is no read permission for the queue */
44 	{&msg_id1, IPC_STAT, &q_buf, EACCES},
45 	/* EFAULT - the structure address is invalid - IPC_STAT */
46 	{&msg_id2, IPC_STAT, (struct msqid_ds *)-1, EFAULT},
47 	/* EFAULT - the structure address is invalid - IPC_SET */
48 	{&msg_id2, IPC_SET, (struct msqid_ds *)-1, EFAULT},
49 	/* EINVAL - the command (-1) is invalid */
50 	{&msg_id2, -1, &q_buf, EINVAL},
51 	/* EINVAL - the queue id is invalid - IPC_STAT */
52 	{&bad_q, IPC_STAT, &q_buf, EINVAL},
53 	/* EINVAL - the queue id is invalid - IPC_SET */
54 	{&bad_q, IPC_SET, &q_buf, EINVAL},
55 	/* EPERM - cannot delete root owned queue */
56 	{&msg_id3, IPC_RMID, NULL, EPERM},
57 };
58 
59 static struct test_variants {
60 	int (*msgctl)(int msqid, int cmd, void *buf);
61 	char *desc;
62 } variants[] = {
63 	{ .msgctl = libc_msgctl, .desc = "libc msgctl()"},
64 #if (__NR_msgctl != __LTP__NR_INVALID_SYSCALL)
65 	{ .msgctl = sys_msgctl,  .desc = "__NR_msgctl syscall"},
66 #endif
67 };
68 
verify_msgctl(unsigned int i)69 static void verify_msgctl(unsigned int i)
70 {
71 	struct test_variants *tv = &variants[tst_variant];
72 
73 	if (tc[i].error == EFAULT &&
74 		tv->msgctl == libc_msgctl) {
75 		tst_res(TCONF, "EFAULT is skipped for libc variant");
76 		return;
77 	}
78 
79 	TST_EXP_FAIL(tv->msgctl(*(tc[i].msg_id), tc[i].cmd, tc[i].buf),
80 	             tc[i].error,
81 	             "msgctl(%i, %i, %p)",
82 	             *(tc[i].msg_id), tc[i].cmd, tc[i].buf);
83 }
84 
setup(void)85 static void setup(void)
86 {
87 	struct test_variants *tv = &variants[tst_variant];
88 
89 	key_t msgkey1, msgkey2;
90 	struct passwd *ltpuser;
91 
92 	msg_id3 = SAFE_MSGGET(IPC_PRIVATE, IPC_CREAT | MSG_RW);
93 
94 	ltpuser = SAFE_GETPWNAM("nobody");
95 	SAFE_SETEUID(ltpuser->pw_uid);
96 
97 	msgkey1 = GETIPCKEY();
98 	msgkey2 = GETIPCKEY();
99 
100 	msg_id1 = SAFE_MSGGET(msgkey1, IPC_CREAT | IPC_EXCL);
101 	msg_id2 = SAFE_MSGGET(msgkey2, IPC_CREAT | IPC_EXCL | MSG_RD | MSG_WR);
102 
103 	tst_res(TINFO, "Testing variant: %s", tv->desc);
104 }
105 
cleanup(void)106 static void cleanup(void)
107 {
108 	if (msg_id1 >= 0)
109 		SAFE_MSGCTL(msg_id1, IPC_RMID, NULL);
110 
111 	if (msg_id2 >= 0)
112 		SAFE_MSGCTL(msg_id2, IPC_RMID, NULL);
113 
114 	if (msg_id3 >= 0) {
115 		SAFE_SETEUID(0);
116 		SAFE_MSGCTL(msg_id3, IPC_RMID, NULL);
117 	}
118 }
119 
120 static struct tst_test test = {
121 	.setup = setup,
122 	.cleanup = cleanup,
123 	.test = verify_msgctl,
124 	.tcnt = ARRAY_SIZE(tc),
125 	.test_variants = ARRAY_SIZE(variants),
126 	.needs_tmpdir = 1,
127 	.needs_root = 1,
128 };
129