• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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é (Matthieu.Fertre@irisa.fr)
6  * Copyright (c) 2018 Cyril Hrubis <chrubis@suse.cz>
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 
19 static int msg_id1 = -1;
20 static int msg_id2 = -1;
21 static int msg_id3 = -1;
22 static int bad_q = -1;
23 
24 struct msqid_ds q_buf;
25 
26 struct tcase {
27 	int *msg_id;
28 	int cmd;
29 	struct msqid_ds *buf;
30 	int error;
31 } tc[] = {
32 	/* EACCES - there is no read permission for the queue */
33 	{&msg_id1, IPC_STAT, &q_buf, EACCES},
34 	/* EFAULT - the structure address is invalid - IPC_STAT */
35 	{&msg_id2, IPC_STAT, (struct msqid_ds *)-1, EFAULT},
36 	/* EFAULT - the structure address is invalid - IPC_SET */
37 	{&msg_id2, IPC_SET, (struct msqid_ds *)-1, EFAULT},
38 	/* EINVAL - the command (-1) is invalid */
39 	{&msg_id2, -1, &q_buf, EINVAL},
40 	/* EINVAL - the queue id is invalid - IPC_STAT */
41 	{&bad_q, IPC_STAT, &q_buf, EINVAL},
42 	/* EINVAL - the queue id is invalid - IPC_SET */
43 	{&bad_q, IPC_SET, &q_buf, EINVAL},
44 	/* EPERM - cannot delete root owned queue */
45 	{&msg_id3, IPC_RMID, NULL, EPERM},
46 };
47 
verify_msgctl(unsigned int i)48 static void verify_msgctl(unsigned int i)
49 {
50 	TEST(msgctl(*(tc[i].msg_id), tc[i].cmd, tc[i].buf));
51 
52 	if (TST_RET != -1) {
53 		tst_res(TFAIL, "msgctl() returned %li", TST_RET);
54 		return;
55 	}
56 
57 	if (TST_ERR == tc[i].error) {
58 		tst_res(TPASS | TTERRNO, "msgctl(%i, %i, %p)",
59 			*tc[i].msg_id, tc[i].cmd, tc[i].buf);
60 		return;
61 	}
62 
63 	tst_res(TFAIL | TTERRNO, "msgctl(%i, %i, %p) expected %s",
64 		*tc[i].msg_id, tc[i].cmd, tc[i].buf, tst_strerrno(tc[i].error));
65 }
66 
setup(void)67 static void setup(void)
68 {
69 	key_t msgkey1, msgkey2;
70 	struct passwd *ltpuser;
71 
72 	msg_id3 = SAFE_MSGGET(IPC_PRIVATE, IPC_CREAT | MSG_RW);
73 
74 	ltpuser = SAFE_GETPWNAM("nobody");
75 	SAFE_SETEUID(ltpuser->pw_uid);
76 
77 	msgkey1 = GETIPCKEY();
78 	msgkey2 = GETIPCKEY();
79 
80 	msg_id1 = SAFE_MSGGET(msgkey1, IPC_CREAT | IPC_EXCL);
81 	msg_id2 = SAFE_MSGGET(msgkey2, IPC_CREAT | IPC_EXCL | MSG_RD | MSG_WR);
82 }
83 
cleanup(void)84 static void cleanup(void)
85 {
86 	if (msg_id1 >= 0)
87 		SAFE_MSGCTL(msg_id1, IPC_RMID, NULL);
88 
89 	if (msg_id2 >= 0)
90 		SAFE_MSGCTL(msg_id2, IPC_RMID, NULL);
91 
92 	if (msg_id3 >= 0) {
93 		SAFE_SETEUID(0);
94 		SAFE_MSGCTL(msg_id3, IPC_RMID, NULL);
95 	}
96 }
97 
98 static struct tst_test test = {
99 	.setup = setup,
100 	.cleanup = cleanup,
101 	.test = verify_msgctl,
102 	.tcnt = ARRAY_SIZE(tc),
103 	.needs_tmpdir = 1,
104 	.needs_root = 1,
105 };
106