2 * Check decoding of mq_open, mq_timedsend, mq_notify, mq_timedreceive and
5 * Copyright (c) 2016 Eugene Syromyatnikov <evgsyr@gmail.com>
6 * Copyright (c) 2016-2017 The strace developers.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. The name of the author may not be used to endorse or promote products
18 * derived from this software without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
21 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
22 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
23 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
24 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
25 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
26 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
27 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
28 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
29 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 #include <asm/unistd.h>
36 #if defined __NR_mq_open && __NR_mq_timedsend && __NR_mq_timedreceive && \
37 __NR_mq_notify && __NR_mq_unlink
42 # include <inttypes.h>
51 # include "sigevent.h"
54 # define DUMPIO_READ 0
58 # define DUMPIO_WRITE 0
73 printstr(unsigned char start, unsigned int count)
78 for (i = 0; i < count; i++) {
79 printf("\\%hho", (unsigned char) (start + i));
84 #if DUMPIO_READ || DUMPIO_WRITE
86 dumpstr(unsigned char start, unsigned int count)
91 for (i = 0; i < count; i++) {
94 printf(" | %05x ", i);
98 printf("%02hhx ", (unsigned char) (start + i));
101 if ((i % 16 == 15) || (i == (count - 1))) {
103 printf("%*s", 3 * (15 - i % 16) +
104 ((i + 8) % 16) / 8, " ");
108 for (j = 0; j <= (i % 16); j++)
110 for (j = i % 16; j < 15; j++)
118 #endif /* DUMPIO_READ || DUMPIO_WRITE */
125 rc = syscall(__NR_mq_unlink, mq_name);
126 printf("mq_unlink(\"%s\") = %s\n", mq_name, sprintrc(rc));
128 puts("+++ exited with 0 +++");
132 do_send(int fd, char *msg, unsigned int msg_size, struct timespec *tmout,
139 rc = syscall(__NR_mq_timedsend, fd, msg, msg_size, 42,
142 printf("mq_timedsend(%d, ", fd);
143 printstr(MSG_START, msg_size > MSG_MAX_UNCUT ? MSG_MAX_UNCUT :
148 printf(", %u, 42, {tv_sec=%lld, tv_nsec=%llu}) = %s\n", msg_size,
149 (long long) tmout->tv_sec,
150 zero_extend_signed_to_ull(tmout->tv_nsec), sprintrc(rc));
156 perror_msg_and_skip("mq_timedsend");
159 dumpstr(MSG_START, msg_size);
165 do_recv(int fd, char *msg, unsigned int msg_size, struct timespec *tmout,
173 rc = syscall(__NR_mq_timedreceive, fd, msg, MSG_SIZE, &prio,
176 printf("mq_timedreceive(%d, ", fd);
178 printstr(MSG_START, rc > MSG_MAX_UNCUT ? MSG_MAX_UNCUT :
186 printf(", %u, [42], {tv_sec=%lld, tv_nsec=%llu}) = %s\n", MSG_SIZE,
187 (long long) tmout->tv_sec,
188 zero_extend_signed_to_ull(tmout->tv_nsec), sprintrc(rc));
194 perror_msg_and_skip("mq_timedreceive");
196 if ((rc >= 0) && ((unsigned long) rc != msg_size))
197 error_msg_and_skip("mq_timedreceive size mismatch"
198 ": expected %u, got %ld",
201 dumpstr(MSG_START, rc);
209 static const kernel_ulong_t bogus_zero =
210 (kernel_ulong_t) 0x8765432100000000ULL;
211 static const kernel_ulong_t bogus_oflags =
212 (kernel_ulong_t) 0xdefaced100000003ULL;
213 static const kernel_ulong_t bogus_mode =
214 (kernel_ulong_t) 0xdec0deadfacefeedULL;
215 static const kernel_ulong_t bogus_fd =
216 (kernel_ulong_t) 0xfeedfacedeadba5eULL;
217 static const kernel_ulong_t bogus_zero_size =
218 (sizeof(kernel_ulong_t) > sizeof(int)) ? (kernel_ulong_t) 0 :
219 (kernel_ulong_t) 0xface1e5500000000ULL;
220 static const kernel_ulong_t bogus_size =
221 (kernel_ulong_t) 0xbadc0dedda7a1057ULL;
222 static const kernel_ulong_t bogus_prio =
223 (kernel_ulong_t) 0xdec0ded1defaced3ULL;
224 static const struct timespec bogus_tmout_data = {
225 .tv_sec = (time_t) 0xdeadfacebeeff00dLL,
226 .tv_nsec = (long) 0xfacefee1deadfeedLL,
228 static const struct timespec future_tmout_data = {
229 .tv_sec = (time_t) 0x7ea1fade7e57faceLL,
230 .tv_nsec = 999999999,
232 struct_sigevent bogus_sev_data = {
233 .sigev_notify = 0xdefaced,
234 .sigev_signo = 0xfacefeed,
235 .sigev_value.sival_ptr = (unsigned long) 0xdeadbeefbadc0dedULL
240 kernel_long_t *bogus_attrs = tail_alloc(sizeof(*bogus_attrs) *
242 char *msg = tail_alloc(MSG_SIZE);
243 TAIL_ALLOC_OBJECT_CONST_PTR(unsigned, bogus_prio_ptr);
244 struct timespec *bogus_tmout = tail_memdup(&bogus_tmout_data,
245 sizeof(*bogus_tmout));
246 struct timespec *future_tmout = tail_memdup(&future_tmout_data,
247 sizeof(*future_tmout));
248 struct_sigevent *bogus_sev = tail_memdup(&bogus_sev_data,
253 fill_memory_ex(msg, MSG_SIZE, MSG_START, MSG_SIZE);
254 fill_memory_ex(bogus_attrs, sizeof(*bogus_attrs) * NUM_ATTRS,
260 /* Zero values, non-O_CREAT mode */
261 rc = syscall(__NR_mq_open, NULL, bogus_zero, bogus_mode, NULL);
262 printf("mq_open(NULL, O_RDONLY) = %s\n", sprintrc(rc));
264 /* O_CREAT parsing, other flags, bogs values */
265 rc = syscall(__NR_mq_open, msg, O_CREAT | bogus_oflags, bogus_mode,
267 printf("mq_open(%p, O_ACCMODE|O_CREAT, %#o, NULL) = %s\n",
268 msg, (unsigned short) bogus_mode, sprintrc(rc));
270 /* Partially invalid attributes structure */
271 rc = syscall(__NR_mq_open, msg, O_CREAT | bogus_oflags, bogus_mode,
273 printf("mq_open(%p, O_ACCMODE|O_CREAT, %#o, %p) = %s\n",
274 msg, (unsigned short) bogus_mode, bogus_attrs + 1, sprintrc(rc));
276 /* Valid attributes structure */
277 rc = syscall(__NR_mq_open, msg, O_CREAT | bogus_oflags, bogus_mode,
279 printf("mq_open(%p, O_ACCMODE|O_CREAT, %#o, {mq_flags=%#llx"
280 ", mq_maxmsg=%lld, mq_msgsize=%lld, mq_curmsgs=%lld}) = %s\n",
281 msg, (unsigned short) bogus_mode,
282 (unsigned long long) (kernel_ulong_t) bogus_attrs[0],
283 (long long) bogus_attrs[1],
284 (long long) bogus_attrs[2],
285 (long long) bogus_attrs[3], sprintrc(rc));
291 rc = syscall(__NR_mq_timedsend, bogus_zero, NULL, bogus_zero_size,
293 printf("mq_timedsend(0, NULL, 0, 0, NULL) = %s\n", sprintrc(rc));
295 /* Invalid pointers */
296 rc = syscall(__NR_mq_timedsend, bogus_fd, msg + MSG_SIZE, bogus_size,
297 bogus_prio, bogus_tmout + 1);
298 printf("mq_timedsend(%d, %p, %llu, %u, %p) = %s\n",
299 (int) bogus_fd, msg + MSG_SIZE, (unsigned long long) bogus_size,
300 (unsigned) bogus_prio, bogus_tmout + 1, sprintrc(rc));
302 /* Partially invalid message (memory only partially available) */
303 rc = syscall(__NR_mq_timedsend, bogus_fd, msg + MSG_SIZE - MSG_CUT,
304 MSG_SIZE, bogus_prio, bogus_tmout);
305 printf("mq_timedsend(%d, %p, %llu, %u, {tv_sec=%lld, tv_nsec=%llu})"
307 (int) bogus_fd, msg + MSG_SIZE - MSG_CUT,
308 (unsigned long long) MSG_SIZE, (unsigned) bogus_prio,
309 (long long) bogus_tmout->tv_sec,
310 zero_extend_signed_to_ull(bogus_tmout->tv_nsec), sprintrc(rc));
312 /* Fully valid message, uncut */
313 rc = syscall(__NR_mq_timedsend, bogus_fd, msg + MSG_SIZE - MSG_CUT,
314 MSG_CUT, bogus_prio, bogus_tmout);
315 errstr = sprintrc(rc);
316 printf("mq_timedsend(%d, ", (int) bogus_fd);
317 printstr(MSG_START + MSG_SIZE - MSG_CUT, MSG_CUT);
318 printf(", %llu, %u, {tv_sec=%lld, tv_nsec=%llu}) = %s\n",
319 (unsigned long long) MSG_CUT, (unsigned) bogus_prio,
320 (long long) bogus_tmout->tv_sec,
321 zero_extend_signed_to_ull(bogus_tmout->tv_nsec), errstr);
323 /* Partially invalid message, cut at maxstrlen */
324 rc = syscall(__NR_mq_timedsend, bogus_fd, msg + MSG_CUT, MSG_SIZE,
325 bogus_prio, bogus_tmout);
326 errstr = sprintrc(rc);
327 printf("mq_timedsend(%d, ", (int) bogus_fd);
328 printstr(MSG_START + MSG_CUT, MSG_MAX_UNCUT);
329 printf("..., %llu, %u, {tv_sec=%lld, tv_nsec=%llu}) = %s\n",
330 (unsigned long long) MSG_SIZE, (unsigned) bogus_prio,
331 (long long) bogus_tmout->tv_sec,
332 zero_extend_signed_to_ull(bogus_tmout->tv_nsec), errstr);
335 /* mq_timedreceive */
338 rc = syscall(__NR_mq_timedreceive, bogus_zero, NULL, bogus_zero_size,
340 printf("mq_timedreceive(0, NULL, 0, NULL, NULL) = %s\n", sprintrc(rc));
342 /* Invalid addresses */
343 rc = syscall(__NR_mq_timedreceive, bogus_fd, msg + MSG_SIZE, bogus_size,
344 bogus_prio_ptr + 1, bogus_tmout + 1);
345 printf("mq_timedreceive(%d, %p, %llu, %p, %p) = %s\n",
346 (int) bogus_fd, msg + MSG_SIZE, (unsigned long long) bogus_size,
347 bogus_prio_ptr + 1, bogus_tmout + 1, sprintrc(rc));
349 /* Invalid fd, valid msg pointer */
350 rc = syscall(__NR_mq_timedreceive, bogus_fd, msg, bogus_size,
351 bogus_prio_ptr, bogus_tmout);
352 printf("mq_timedreceive(%d, %p, %llu, %p, {tv_sec=%lld, tv_nsec=%llu}) "
354 (int) bogus_fd, msg, (unsigned long long) bogus_size,
355 bogus_prio_ptr, (long long) bogus_tmout->tv_sec,
356 zero_extend_signed_to_ull(bogus_tmout->tv_nsec), sprintrc(rc));
362 rc = syscall(__NR_mq_notify, bogus_zero, NULL);
363 printf("mq_notify(0, NULL) = %s\n", sprintrc(rc));
365 /* Invalid pointer */
366 rc = syscall(__NR_mq_notify, bogus_fd, bogus_sev + 1);
367 printf("mq_notify(%d, %p) = %s\n",
368 (int) bogus_fd, bogus_sev + 1, sprintrc(rc));
370 /* Invalid SIGEV_* */
371 rc = syscall(__NR_mq_notify, bogus_fd, bogus_sev);
372 printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
373 ", sigev_signo=%u, sigev_notify=%#x /* SIGEV_??? */}) = %s\n",
374 (int) bogus_fd, bogus_sev->sigev_value.sival_int,
375 bogus_sev->sigev_value.sival_ptr,
376 bogus_sev->sigev_signo, bogus_sev->sigev_notify,
380 bogus_sev->sigev_notify = SIGEV_NONE;
381 rc = syscall(__NR_mq_notify, bogus_fd, bogus_sev);
382 printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
383 ", sigev_signo=%u, sigev_notify=SIGEV_NONE}) = %s\n",
384 (int) bogus_fd, bogus_sev->sigev_value.sival_int,
385 bogus_sev->sigev_value.sival_ptr,
386 bogus_sev->sigev_signo, sprintrc(rc));
389 bogus_sev->sigev_notify = SIGEV_SIGNAL;
390 bogus_sev->sigev_signo = SIGALRM;
391 rc = syscall(__NR_mq_notify, bogus_fd, bogus_sev);
392 printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
393 ", sigev_signo=SIGALRM, sigev_notify=SIGEV_SIGNAL}) = %s\n",
394 (int) bogus_fd, bogus_sev->sigev_value.sival_int,
395 bogus_sev->sigev_value.sival_ptr, sprintrc(rc));
398 bogus_sev->sigev_notify = SIGEV_THREAD;
399 bogus_sev->sigev_un.sigev_thread.function =
400 (unsigned long) 0xdeadbeefbadc0dedULL;
401 bogus_sev->sigev_un.sigev_thread.attribute =
402 (unsigned long) 0xcafef00dfacefeedULL;
403 rc = syscall(__NR_mq_notify, bogus_fd, bogus_sev);
404 printf("mq_notify(%d, {sigev_value={sival_int=%d, sival_ptr=%#lx}"
405 ", sigev_signo=SIGALRM, sigev_notify=SIGEV_THREAD"
406 ", sigev_notify_function=%#lx, sigev_notify_attributes=%#lx})"
408 (int) bogus_fd, bogus_sev->sigev_value.sival_int,
409 bogus_sev->sigev_value.sival_ptr,
410 bogus_sev->sigev_un.sigev_thread.function,
411 bogus_sev->sigev_un.sigev_thread.attribute, sprintrc(rc));
416 rc = syscall(__NR_mq_unlink, NULL);
417 printf("mq_unlink(NULL) = %s\n", sprintrc(rc));
420 rc = syscall(__NR_mq_unlink, msg + MSG_SIZE);
421 printf("mq_unlink(%p) = %s\n", msg + MSG_SIZE, sprintrc(rc));
423 /* Long unterminated string */
424 rc = syscall(__NR_mq_unlink, msg);
425 errstr = sprintrc(rc);
426 printf("mq_unlink(%p) = %s\n", msg, errstr);
429 /* Sending and receiving test */
431 if (asprintf(&mq_name, "strace-mq_sendrecv-%u.sample", getpid()) < 0)
432 perror_msg_and_fail("asprintf");
434 # if DUMPIO_READ || DUMPIO_WRITE
438 bogus_attrs[2] = MSG_SIZE;
439 fd = rc = syscall(__NR_mq_open, mq_name,
440 O_CREAT|O_RDWR|O_NONBLOCK, S_IRWXU, bogus_attrs);
441 errstr = sprintrc(rc);
443 perror_msg_and_skip("mq_open");
446 # if DUMPIO_READ || DUMPIO_WRITE
448 error_msg_and_skip("mq_open returned fd other than 0");
450 fill_memory_ex(bogus_attrs, sizeof(*bogus_attrs) * NUM_ATTRS,
452 printf("mq_open(\"%s\", O_RDWR|O_CREAT|O_NONBLOCK, 0700"
453 ", {mq_flags=%#llx, mq_maxmsg=2, mq_msgsize=%u"
454 ", mq_curmsgs=%lld}) = %s\n",
455 mq_name, (unsigned long long) (kernel_ulong_t) bogus_attrs[0],
456 MSG_SIZE, (long long) bogus_attrs[3], errstr);
458 rc = syscall(__NR_mq_getsetattr, fd, NULL, bogus_attrs);
460 perror_msg_and_skip("mq_getsetattr");
461 if ((bogus_attrs[1] < 2) || (bogus_attrs[2] < MSG_SIZE))
462 error_msg_and_skip("mq too small");
464 do_send(fd, msg, MSG_CUT, future_tmout, false);
465 do_send(fd, msg, MSG_SIZE, future_tmout, true);
467 memset(msg, '\0', MSG_SIZE);
468 do_recv(fd, msg, MSG_CUT, future_tmout, false);
470 memset(msg, '\0', MSG_SIZE);
471 do_recv(fd, msg, MSG_SIZE, future_tmout, true);
478 SKIP_MAIN_UNDEFINED("__NR_mq_open && __NR_mq_timedsend && "
479 "__NR_mq_timedreceive && __NR_mq_notify && __NR_mq_unlink");