• 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  */
5 
6 /*\
7  * [Description]
8  *
9  * Test for EEXIST, ENOENT, EACCES errors.
10  *
11  * 1) msgget(2) fails if a message queue exists for key and msgflg
12  *    specified both IPC_CREAT and IPC_EXCL.
13  * 2) msgget(2) fails if no message queue exists for key and msgflg
14  *    did not specify IPC_CREAT.
15  * 3) msgget(2) fails if a message queue exists for key, but the
16  *    calling process does not have permission to access the queue,
17  *    and does not have the CAP_IPC_OWNER capability.
18  *
19  */
20 #include <errno.h>
21 #include <stdlib.h>
22 #include <sys/types.h>
23 #include <sys/ipc.h>
24 #include <sys/msg.h>
25 #include <pwd.h>
26 
27 #include "tst_test.h"
28 #include "tst_safe_sysv_ipc.h"
29 #include "libnewipc.h"
30 
31 static key_t msgkey, msgkey1;
32 static int queue_id = -1;
33 static struct passwd *pw;
34 
35 static struct tcase {
36 	int *key;
37 	int flags;
38 	int exp_err;
39 	/*1: nobody expected  0: root expected */
40 	int exp_user;
41 } tcases[] = {
42 	{&msgkey, IPC_CREAT | IPC_EXCL, EEXIST, 0},
43 	{&msgkey1, IPC_PRIVATE, ENOENT, 0},
44 	{&msgkey1, IPC_EXCL, ENOENT, 0},
45 	{&msgkey, MSG_RD, EACCES, 1},
46 	{&msgkey, MSG_WR, EACCES, 1},
47 	{&msgkey, MSG_RW, EACCES, 1}
48 };
49 
verify_msgget(struct tcase * tc)50 static void verify_msgget(struct tcase *tc)
51 {
52 	TST_EXP_FAIL2(msgget(*tc->key, tc->flags), tc->exp_err, "msgget(%i, %i)",
53 		*tc->key, tc->flags);
54 }
55 
do_test(unsigned int n)56 static void do_test(unsigned int n)
57 {
58 	pid_t pid;
59 	struct tcase *tc = &tcases[n];
60 
61 	if (tc->exp_user == 0) {
62 		verify_msgget(tc);
63 	} else {
64 		pid = SAFE_FORK();
65 		if (pid) {
66 			tst_reap_children();
67 		} else {
68 			SAFE_SETUID(pw->pw_uid);
69 			verify_msgget(tc);
70 			exit(0);
71 		}
72 	}
73 }
74 
setup(void)75 static void setup(void)
76 {
77 	msgkey = GETIPCKEY();
78 	msgkey1 = GETIPCKEY();
79 
80 	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL);
81 
82 	pw = SAFE_GETPWNAM("nobody");
83 }
84 
cleanup(void)85 static void cleanup(void)
86 {
87 	if (queue_id != -1)
88 		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
89 }
90 
91 static struct tst_test test = {
92 	.needs_tmpdir = 1,
93 	.needs_root = 1,
94 	.forks_child = 1,
95 	.tcnt = ARRAY_SIZE(tcases),
96 	.setup = setup,
97 	.cleanup = cleanup,
98 	.test = do_test
99 };
100