| Commit | Line | Data |
|---|---|---|
| b3f92a66 SK |
1 | /* |
| 2 | * Copyright (c) 2010, Stathis Kamperis | |
| 3 | * All rights reserved. | |
| 4 | * | |
| 5 | * Redistribution and use in source and binary forms, with or without | |
| 6 | * modification, are permitted provided that the following conditions | |
| 7 | * are met: | |
| 8 | * 1. Redistributions of source code must retain the above copyright | |
| 9 | * notice, this list of conditions and the following disclaimer. | |
| 10 | * 2. Redistributions in binary form must reproduce the above copyright | |
| 11 | * notice, this list of conditions and the following disclaimer in the | |
| 12 | * documentation and/or other materials provided with the distribution. | |
| 13 | * | |
| 14 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | |
| 15 | * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | |
| 16 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS | |
| 17 | * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE | |
| 18 | * COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, | |
| 19 | * INCIDENTAL, SPECIAL, EXEMPLARY OR CONSEQUENTIAL DAMAGES (INCLUDING, | |
| 20 | * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; | |
| 21 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED | |
| 22 | * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, | |
| 23 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT | |
| 24 | * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF | |
| 25 | * SUCH DAMAGE. | |
| 26 | */ | |
| 27 | ||
| 28 | #include <assert.h> | |
| 29 | #include <errno.h> | |
| 30 | #include <limits.h> /* for LONG_MAX */ | |
| 31 | #include <mqueue.h> | |
| 32 | #include <stdio.h> | |
| 33 | #include <stdlib.h> | |
| 34 | #include <string.h> /* memset() */ | |
| 35 | ||
| 815b9887 | 36 | #define MQNAME "/t_mq_send_dos" |
| b3f92a66 SK |
37 | |
| 38 | int | |
| 39 | main(void) | |
| 40 | { | |
| 41 | /* | |
| 42 | * Create a message queue with enormous capacity. | |
| 43 | * According to POSIX, mq_maxmsg field is of `long' data type. | |
| 44 | */ | |
| 45 | struct mq_attr attr; | |
| 46 | mqd_t md; | |
| 47 | ||
| 48 | memset(&attr, 0, sizeof(attr)); | |
| 815b9887 | 49 | attr.mq_maxmsg = LONG_MAX; /* Maximum number of messages. */ |
| 50 | attr.mq_msgsize = 1024; /* Maximum message size. */ | |
| b3f92a66 | 51 | |
| 815b9887 | 52 | md = mq_open(MQNAME, O_CREAT | O_EXCL | O_WRONLY, 0700, &attr); |
| 53 | if (md == (mqd_t)-1) { | |
| 54 | assert(errno == EINVAL); | |
| 55 | printf("passed\n"); | |
| 56 | return (EXIT_SUCCESS); | |
| 57 | } | |
| b3f92a66 | 58 | |
| 01e07384 | 59 | /* |
| 60 | * Start sending messages until we consume all memory. | |
| 61 | * | |
| 62 | * In OSes that implement message queues inside the kernel, | |
| 63 | * this could lead to a panic and a denial of service attack. | |
| 64 | * | |
| 65 | * Such implementations may be found in {Net, DragonFly}BSD, as | |
| 66 | * opposed to the userland mqueue incarnation of Solaris. | |
| 67 | */ | |
| 815b9887 | 68 | char buf[1024]; |
| b3f92a66 SK |
69 | long i; |
| 70 | ||
| 71 | for (i = 0; i < LONG_MAX; i++) | |
| 72 | assert(mq_send(md, buf, sizeof(buf), /* priority */ 0) != -1); | |
| 73 | ||
| 74 | assert(mq_close(md) != -1); | |
| 75 | assert(mq_unlink(MQNAME) != -1); | |
| 76 | ||
| 815b9887 | 77 | printf("passed (Oh My God!)\n"); |
| 78 | ||
| b3f92a66 SK |
79 | return (EXIT_SUCCESS); |
| 80 | } |