1 /*
2 * Copyright (c) 2015 Elvira Khabirova <lineprinter0@gmail.com>
3 * Copyright (c) 2015-2016 Dmitry V. Levin <ldv@altlinux.org>
4 * Copyright (c) 2015-2018 The strace developers.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. The name of the author may not be used to endorse or promote products
16 * derived from this software without specific prior written permission.
17 *
18 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
19 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
20 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
21 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
22 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
23 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
27 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28 */
29
30 #include "tests.h"
31 #include <errno.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <sys/msg.h>
35
36 #include "xlat.h"
37 #include "xlat/resource_flags.h"
38
39 #ifndef MSG_STAT_ANY
40 # define MSG_STAT_ANY 13
41 #endif
42
43 /*
44 * Before glibc-2.22-122-gbe48165, ppc64 code tried to retrieve data
45 * provided in third argument of msgctl call (in case of IPC_SET cmd)
46 * which led to segmentation fault.
47 */
48 #undef TEST_MSGCTL_BOGUS_ADDR
49 #if defined __GLIBC__ && defined POWERPC64
50 # if !(defined __GLIBC_MINOR__) \
51 || ((__GLIBC__ << 16) + __GLIBC_MINOR__ < (2 << 16) + 23)
52 # define TEST_MSGCTL_BOGUS_ADDR 0
53 # endif
54 #endif /* __GLIBC__ && POWERPC64 */
55
56 #ifndef TEST_MSGCTL_BOGUS_ADDR
57 # define TEST_MSGCTL_BOGUS_ADDR 1
58 #endif
59
60 #if XLAT_RAW
61 # define str_ipc_excl_nowait "0xface1c00"
62 # define str_ipc_private "0"
63 # define str_ipc_rmid "0"
64 # define str_ipc_set "0x1"
65 # define str_ipc_stat "0x2"
66 # define str_msg_stat "0xb"
67 # define str_msg_info "0xc"
68 # define str_msg_stat_any "0xd"
69 # define str_ipc_64 "0x100"
70 # define str_bogus_cmd "0xdeadbeef"
71 #elif XLAT_VERBOSE
72 # define str_ipc_excl_nowait \
73 "0xface1c00 /\\* IPC_EXCL\\|IPC_NOWAIT\\|0xface1000 \\*/"
74 # define str_ipc_private "0 /\\* IPC_PRIVATE \\*/"
75 # define str_ipc_rmid "0 /\\* IPC_RMID \\*/"
76 # define str_ipc_set "0x1 /\\* IPC_SET \\*/"
77 # define str_ipc_stat "0x2 /\\* IPC_STAT \\*/"
78 # define str_msg_stat "0xb /\\* MSG_STAT \\*/"
79 # define str_msg_info "0xc /\\* MSG_INFO \\*/"
80 # define str_msg_stat_any "0xd /\\* MSG_STAT_ANY \\*/"
81 # define str_ipc_64 "0x100 /\\* IPC_64 \\*/"
82 # define str_bogus_cmd "0xdeadbeef /\\* MSG_\\?\\?\\? \\*/"
83 #else
84 # define str_ipc_excl_nowait "IPC_EXCL\\|IPC_NOWAIT\\|0xface1000"
85 # define str_ipc_private "IPC_PRIVATE"
86 # define str_ipc_rmid "IPC_RMID"
87 # define str_ipc_set "IPC_SET"
88 # define str_ipc_stat "IPC_STAT"
89 # define str_msg_stat "MSG_STAT"
90 # define str_msg_info "MSG_INFO"
91 # define str_msg_stat_any "MSG_STAT_ANY"
92 # define str_ipc_64 "IPC_64"
93 # define str_bogus_cmd "0xdeadbeef /\\* MSG_\\?\\?\\? \\*/"
94 #endif
95
96 static int id = -1;
97
98 static void
cleanup(void)99 cleanup(void)
100 {
101 msgctl(id, IPC_RMID, NULL);
102 printf("msgctl\\(%d, (%s\\|)?%s, NULL\\) += 0\n",
103 id, str_ipc_64, str_ipc_rmid);
104 id = -1;
105 }
106
107 int
main(void)108 main(void)
109 {
110 static const key_t private_key =
111 (key_t) (0xffffffff00000000ULL | IPC_PRIVATE);
112 static const key_t bogus_key = (key_t) 0xeca86420fdb9f531ULL;
113 static const int bogus_msgid = 0xfdb97531;
114 static const int bogus_cmd = 0xdeadbeef;
115 #if TEST_MSGCTL_BOGUS_ADDR
116 static void * const bogus_addr = (void *) -1L;
117 #endif
118 static const int bogus_flags = 0xface1e55 & ~IPC_CREAT;
119
120 int rc;
121 struct msqid_ds ds;
122
123 rc = msgget(bogus_key, bogus_flags);
124 printf("msgget\\(%#llx, %s\\|%#04o\\) = %s\n",
125 zero_extend_signed_to_ull(bogus_key),
126 str_ipc_excl_nowait,
127 bogus_flags & 0777, sprintrc_grep(rc));
128
129 id = msgget(private_key, 0600);
130 if (id < 0)
131 perror_msg_and_skip("msgget");
132 printf("msgget\\(%s, 0600\\) = %d\n", str_ipc_private, id);
133 atexit(cleanup);
134
135 rc = msgctl(bogus_msgid, bogus_cmd, NULL);
136 printf("msgctl\\(%d, (%s\\|)?%s, NULL\\) = %s\n",
137 bogus_msgid, str_ipc_64, str_bogus_cmd, sprintrc_grep(rc));
138
139 #if TEST_MSGCTL_BOGUS_ADDR
140 rc = msgctl(bogus_msgid, IPC_SET, bogus_addr);
141 printf("msgctl\\(%d, (%s\\|)?%s, %p\\) = %s\n",
142 bogus_msgid, str_ipc_64, str_ipc_set, bogus_addr,
143 sprintrc_grep(rc));
144 #endif
145
146 if (msgctl(id, IPC_STAT, &ds))
147 perror_msg_and_skip("msgctl IPC_STAT");
148 printf("msgctl\\(%d, (%s\\|)?%s, \\{msg_perm=\\{uid=%u"
149 ", gid=%u, mode=%#o, key=%u, cuid=%u, cgid=%u\\}, msg_stime=%u"
150 ", msg_rtime=%u, msg_ctime=%u, msg_qnum=%u, msg_qbytes=%u"
151 ", msg_lspid=%u, msg_lrpid=%u\\}\\) = 0\n",
152 id, str_ipc_64, str_ipc_stat,
153 (unsigned) ds.msg_perm.uid, (unsigned) ds.msg_perm.gid,
154 (unsigned) ds.msg_perm.mode, (unsigned) ds.msg_perm.__key,
155 (unsigned) ds.msg_perm.cuid, (unsigned) ds.msg_perm.cgid,
156 (unsigned) ds.msg_stime, (unsigned) ds.msg_rtime,
157 (unsigned) ds.msg_ctime, (unsigned) ds.msg_qnum,
158 (unsigned) ds.msg_qbytes, (unsigned) ds.msg_lspid,
159 (unsigned) ds.msg_lrpid);
160
161 if (msgctl(id, IPC_SET, &ds))
162 perror_msg_and_skip("msgctl IPC_SET");
163 printf("msgctl\\(%d, (%s\\|)?%s, \\{msg_perm=\\{uid=%u"
164 ", gid=%u, mode=%#o\\}, ...\\}\\) = 0\n",
165 id, str_ipc_64, str_ipc_set, (unsigned) ds.msg_perm.uid,
166 (unsigned) ds.msg_perm.gid, (unsigned) ds.msg_perm.mode);
167
168 rc = msgctl(0, MSG_INFO, &ds);
169 printf("msgctl\\(0, (%s\\|)?%s, %p\\) = %s\n",
170 str_ipc_64, str_msg_info, &ds, sprintrc_grep(rc));
171
172 rc = msgctl(id, MSG_STAT, &ds);
173 printf("msgctl\\(%d, (%s\\|)?%s, %p\\) = %s\n",
174 id, str_ipc_64, str_msg_stat, &ds, sprintrc_grep(rc));
175
176 rc = msgctl(id, MSG_STAT_ANY, &ds);
177 printf("msgctl\\(%d, (%s\\|)?%s, %p\\) = %s\n",
178 id, str_ipc_64, str_msg_stat_any, &ds, sprintrc_grep(rc));
179
180 return 0;
181 }
182