1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * Copyright (c) International Business Machines Corp., 2001
4  */
5 
6 /*
7  * DESCRIPTION
8  * 1) The calling process does not have write permission on the message
9  *    queue, so msgsnd(2) fails and sets errno to EACCES.
10  * 2) msgsnd(2) fails and sets errno to EFAULT if the message buffer address
11  *    is invalid.
12  * 3) msgsnd(2) fails and sets errno to EINVAL if the queue ID is invalid.
13  * 4) msgsnd(2) fails and sets errno to EINVAL if the message type is not
14  *    positive (0).
15  * 5) msgsnd(2) fails and sets errno to EINVAL if the message type is not
16  *    positive (>0).
17  * 6) msgsnd(2) fails and sets errno to EINVAL if the message size is less
18  *    than zero.
19  */
20 
21 #include <errno.h>
22 #include <string.h>
23 #include <stdlib.h>
24 #include <sys/types.h>
25 #include <sys/ipc.h>
26 #include <sys/msg.h>
27 #include <pwd.h>
28 
29 #include "tst_test.h"
30 #include "tst_safe_sysv_ipc.h"
31 #include "libnewipc.h"
32 
33 static key_t msgkey;
34 static int queue_id = -1;
35 static int bad_id = -1;
36 static struct passwd *pw;
37 static struct buf {
38 	long type;
39 	char text[MSGSIZE];
40 } snd_buf[] = {
41 	{1, "hello"},
42 	{0, "hello"},
43 	{-1, "hello"}
44 };
45 
46 static struct tcase {
47 	int *id;
48 	struct buf *buffer;
49 	int msgsz;
50 	int exp_err;
51 	/*1: nobody expected  0: root expected */
52 	int exp_user;
53 } tcases[] = {
54 	{&queue_id, &snd_buf[0], MSGSIZE, EACCES, 1},
55 	{&queue_id, NULL, MSGSIZE, EFAULT, 0},
56 	{&bad_id, &snd_buf[0], MSGSIZE, EINVAL, 0},
57 	{&queue_id, &snd_buf[1], MSGSIZE, EINVAL, 0},
58 	{&queue_id, &snd_buf[2], MSGSIZE, EINVAL, 0},
59 	{&queue_id, &snd_buf[0], -1, EINVAL, 0}
60 };
61 
verify_msgsnd(struct tcase * tc)62 static void verify_msgsnd(struct tcase *tc)
63 {
64 	TEST(msgsnd(*tc->id, tc->buffer, tc->msgsz, 0));
65 	if (TST_RET != -1) {
66 		tst_res(TFAIL, "smgsnd() succeeded unexpectedly");
67 		return;
68 	}
69 
70 	if (TST_ERR == tc->exp_err) {
71 		tst_res(TPASS | TTERRNO, "msgsnd() failed as expected");
72 	} else {
73 		tst_res(TFAIL | TTERRNO, "msgsnd() failed unexpectedly,"
74 			" expected %s", tst_strerrno(tc->exp_err));
75 	}
76 }
77 
do_test(unsigned int n)78 static void do_test(unsigned int n)
79 {
80 	pid_t pid;
81 	struct tcase *tc = &tcases[n];
82 
83 	if (tc->exp_user == 0) {
84 		verify_msgsnd(tc);
85 		return;
86 	}
87 
88 	pid = SAFE_FORK();
89 	if (pid) {
90 		tst_reap_children();
91 	} else {
92 		SAFE_SETUID(pw->pw_uid);
93 		verify_msgsnd(tc);
94 	}
95 }
96 
setup(void)97 static void setup(void)
98 {
99 	msgkey = GETIPCKEY();
100 
101 	queue_id = SAFE_MSGGET(msgkey, IPC_CREAT | IPC_EXCL | MSG_RW);
102 
103 	pw = SAFE_GETPWNAM("nobody");
104 }
105 
cleanup(void)106 static void cleanup(void)
107 {
108 	if (queue_id != -1)
109 		SAFE_MSGCTL(queue_id, IPC_RMID, NULL);
110 }
111 
112 static struct tst_test test = {
113 	.needs_tmpdir = 1,
114 	.needs_root = 1,
115 	.forks_child = 1,
116 	.tcnt = ARRAY_SIZE(tcases),
117 	.setup = setup,
118 	.cleanup = cleanup,
119 	.test = do_test
120 };
121