| 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 |
#include "ldt_keeper.h" |
|---|
| 44 |
|
|---|
| 45 |
#include <string.h> |
|---|
| 46 |
#include <stdlib.h> |
|---|
| 47 |
#include <errno.h> |
|---|
| 48 |
#include <fcntl.h> |
|---|
| 49 |
#include <sys/mman.h> |
|---|
| 50 |
#include <sys/types.h> |
|---|
| 51 |
#include <stdio.h> |
|---|
| 52 |
#include <unistd.h> |
|---|
| 53 |
#ifdef __linux__ |
|---|
| 54 |
#include <asm/unistd.h> |
|---|
| 55 |
#include <asm/ldt.h> |
|---|
| 56 |
|
|---|
| 57 |
#include <linux/version.h> |
|---|
| 58 |
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,5,47) |
|---|
| 59 |
#define modify_ldt_ldt_s user_desc |
|---|
| 60 |
#endif |
|---|
| 61 |
|
|---|
| 62 |
#ifdef __cplusplus |
|---|
| 63 |
extern "C" { |
|---|
| 64 |
#endif |
|---|
| 65 |
|
|---|
| 66 |
#if defined(__GLIBC__) && (__GLIBC__ < 2 || (__GLIBC__ == 2 && __GLIBC_MINOR__ == 0)) |
|---|
| 67 |
_syscall3( int, modify_ldt, int, func, void *, ptr, unsigned long, bytecount ); |
|---|
| 68 |
#else |
|---|
| 69 |
int modify_ldt(int func, void *ptr, unsigned long bytecount); |
|---|
| 70 |
#endif |
|---|
| 71 |
#ifdef __cplusplus |
|---|
| 72 |
} |
|---|
| 73 |
#endif |
|---|
| 74 |
#else |
|---|
| 75 |
#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) |
|---|
| 76 |
#include <machine/segments.h> |
|---|
| 77 |
#include <machine/sysarch.h> |
|---|
| 78 |
#endif |
|---|
| 79 |
|
|---|
| 80 |
#ifdef __svr4__ |
|---|
| 81 |
#include <sys/segment.h> |
|---|
| 82 |
#include <sys/sysi86.h> |
|---|
| 83 |
|
|---|
| 84 |
|
|---|
| 85 |
#ifdef __cplusplus |
|---|
| 86 |
extern "C" { |
|---|
| 87 |
#endif |
|---|
| 88 |
int sysi86(int, void*); |
|---|
| 89 |
#ifdef __cplusplus |
|---|
| 90 |
} |
|---|
| 91 |
#endif |
|---|
| 92 |
|
|---|
| 93 |
#ifndef NUMSYSLDTS |
|---|
| 94 |
#define NUMSYSLDTS 6 |
|---|
| 95 |
#endif |
|---|
| 96 |
|
|---|
| 97 |
#define TEB_SEL_IDX NUMSYSLDTS |
|---|
| 98 |
#endif |
|---|
| 99 |
|
|---|
| 100 |
#define LDT_ENTRIES 8192 |
|---|
| 101 |
#define LDT_ENTRY_SIZE 8 |
|---|
| 102 |
#pragma pack(4) |
|---|
| 103 |
struct modify_ldt_ldt_s { |
|---|
| 104 |
unsigned int entry_number; |
|---|
| 105 |
unsigned long base_addr; |
|---|
| 106 |
unsigned int limit; |
|---|
| 107 |
unsigned int seg_32bit:1; |
|---|
| 108 |
unsigned int contents:2; |
|---|
| 109 |
unsigned int read_exec_only:1; |
|---|
| 110 |
unsigned int limit_in_pages:1; |
|---|
| 111 |
unsigned int seg_not_present:1; |
|---|
| 112 |
unsigned int useable:1; |
|---|
| 113 |
}; |
|---|
| 114 |
|
|---|
| 115 |
#define MODIFY_LDT_CONTENTS_DATA 0 |
|---|
| 116 |
#define MODIFY_LDT_CONTENTS_STACK 1 |
|---|
| 117 |
#define MODIFY_LDT_CONTENTS_CODE 2 |
|---|
| 118 |
#endif |
|---|
| 119 |
|
|---|
| 120 |
|
|---|
| 121 |
|
|---|
| 122 |
#define LDT_SEL(idx) ((idx) << 3 | 1 << 2 | 3) |
|---|
| 123 |
|
|---|
| 124 |
|
|---|
| 125 |
#if defined(__FreeBSD__) && defined(LDT_AUTO_ALLOC) |
|---|
| 126 |
#define TEB_SEL_IDX LDT_AUTO_ALLOC |
|---|
| 127 |
#endif |
|---|
| 128 |
|
|---|
| 129 |
#ifndef TEB_SEL_IDX |
|---|
| 130 |
#define TEB_SEL_IDX 17 |
|---|
| 131 |
#endif |
|---|
| 132 |
|
|---|
| 133 |
static unsigned int fs_ldt = TEB_SEL_IDX; |
|---|
| 134 |
|
|---|
| 135 |
|
|---|
| 136 |
|
|---|
| 137 |
|
|---|
| 138 |
|
|---|
| 139 |
|
|---|
| 140 |
|
|---|
| 141 |
#ifdef __cplusplus |
|---|
| 142 |
extern "C" |
|---|
| 143 |
#endif |
|---|
| 144 |
void Setup_FS_Segment(void) |
|---|
| 145 |
{ |
|---|
| 146 |
unsigned int ldt_desc = LDT_SEL(fs_ldt); |
|---|
| 147 |
|
|---|
| 148 |
__asm__ __volatile__( |
|---|
| 149 |
"movl %0,%%eax; movw %%ax, %%fs" : : "r" (ldt_desc) |
|---|
| 150 |
:"eax" |
|---|
| 151 |
); |
|---|
| 152 |
} |
|---|
| 153 |
|
|---|
| 154 |
|
|---|
| 155 |
#if 0 |
|---|
| 156 |
#ifdef __linux__ |
|---|
| 157 |
|
|---|
| 158 |
|
|---|
| 159 |
static int LDT_Modify( int func, struct modify_ldt_ldt_s *ptr, |
|---|
| 160 |
unsigned long count ) |
|---|
| 161 |
{ |
|---|
| 162 |
int res; |
|---|
| 163 |
#ifdef __PIC__ |
|---|
| 164 |
__asm__ __volatile__( "pushl %%ebx\n\t" |
|---|
| 165 |
"movl %2,%%ebx\n\t" |
|---|
| 166 |
"int $0x80\n\t" |
|---|
| 167 |
"popl %%ebx" |
|---|
| 168 |
: "=a" (res) |
|---|
| 169 |
: "0" (__NR_modify_ldt), |
|---|
| 170 |
"r" (func), |
|---|
| 171 |
"c" (ptr), |
|---|
| 172 |
"d"(16) |
|---|
| 173 |
:"esi" ); |
|---|
| 174 |
#else |
|---|
| 175 |
__asm__ __volatile__("int $0x80" |
|---|
| 176 |
: "=a" (res) |
|---|
| 177 |
: "0" (__NR_modify_ldt), |
|---|
| 178 |
"b" (func), |
|---|
| 179 |
"c" (ptr), |
|---|
| 180 |
"d"(16) |
|---|
| 181 |
:"esi"); |
|---|
| 182 |
#endif |
|---|
| 183 |
if (res >= 0) return res; |
|---|
| 184 |
errno = -res; |
|---|
| 185 |
return -1; |
|---|
| 186 |
} |
|---|
| 187 |
#endif |
|---|
| 188 |
#endif |
|---|
| 189 |
|
|---|
| 190 |
#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) |
|---|
| 191 |
static void LDT_EntryToBytes( unsigned long *buffer, const struct modify_ldt_ldt_s *content ) |
|---|
| 192 |
{ |
|---|
| 193 |
*buffer++ = ((content->base_addr & 0x0000ffff) << 16) | |
|---|
| 194 |
(content->limit & 0x0ffff); |
|---|
| 195 |
*buffer = (content->base_addr & 0xff000000) | |
|---|
| 196 |
((content->base_addr & 0x00ff0000)>>16) | |
|---|
| 197 |
(content->limit & 0xf0000) | |
|---|
| 198 |
(content->contents << 10) | |
|---|
| 199 |
((content->read_exec_only == 0) << 9) | |
|---|
| 200 |
((content->seg_32bit != 0) << 22) | |
|---|
| 201 |
((content->limit_in_pages != 0) << 23) | |
|---|
| 202 |
0xf000; |
|---|
| 203 |
} |
|---|
| 204 |
#endif |
|---|
| 205 |
|
|---|
| 206 |
void* fs_seg=0; |
|---|
| 207 |
|
|---|
| 208 |
ldt_fs_t* Setup_LDT_Keeper(void) |
|---|
| 209 |
{ |
|---|
| 210 |
struct modify_ldt_ldt_s array; |
|---|
| 211 |
int ret; |
|---|
| 212 |
ldt_fs_t* ldt_fs = (ldt_fs_t*) malloc(sizeof(ldt_fs_t)); |
|---|
| 213 |
|
|---|
| 214 |
if (!ldt_fs) |
|---|
| 215 |
return NULL; |
|---|
| 216 |
|
|---|
| 217 |
ldt_fs->fd = open("/dev/zero", O_RDWR); |
|---|
| 218 |
if(ldt_fs->fd<0){ |
|---|
| 219 |
perror( "Cannot open /dev/zero for READ+WRITE. Check permissions! error: "); |
|---|
| 220 |
return NULL; |
|---|
| 221 |
} |
|---|
| 222 |
fs_seg= |
|---|
| 223 |
ldt_fs->fs_seg = mmap(NULL, getpagesize(), PROT_READ | PROT_WRITE, MAP_PRIVATE, |
|---|
| 224 |
ldt_fs->fd, 0); |
|---|
| 225 |
if (ldt_fs->fs_seg == (void*)-1) |
|---|
| 226 |
{ |
|---|
| 227 |
perror("ERROR: Couldn't allocate memory for fs segment"); |
|---|
| 228 |
close(ldt_fs->fd); |
|---|
| 229 |
free(ldt_fs); |
|---|
| 230 |
return NULL; |
|---|
| 231 |
} |
|---|
| 232 |
*(void**)((char*)ldt_fs->fs_seg+0x18) = ldt_fs->fs_seg; |
|---|
| 233 |
memset(&array, 0, sizeof(array)); |
|---|
| 234 |
array.base_addr=(int)ldt_fs->fs_seg; |
|---|
| 235 |
array.entry_number=TEB_SEL_IDX; |
|---|
| 236 |
array.limit=array.base_addr+getpagesize()-1; |
|---|
| 237 |
array.seg_32bit=1; |
|---|
| 238 |
array.read_exec_only=0; |
|---|
| 239 |
array.seg_not_present=0; |
|---|
| 240 |
array.contents=MODIFY_LDT_CONTENTS_DATA; |
|---|
| 241 |
array.limit_in_pages=0; |
|---|
| 242 |
#ifdef __linux__ |
|---|
| 243 |
|
|---|
| 244 |
ret=modify_ldt(0x1, &array, sizeof(struct modify_ldt_ldt_s)); |
|---|
| 245 |
if(ret<0) |
|---|
| 246 |
{ |
|---|
| 247 |
perror("install_fs"); |
|---|
| 248 |
printf("Couldn't install fs segment, expect segfault\n"); |
|---|
| 249 |
} |
|---|
| 250 |
#endif |
|---|
| 251 |
|
|---|
| 252 |
#if defined(__NetBSD__) || defined(__FreeBSD__) || defined(__OpenBSD__) || defined(__DragonFly__) |
|---|
| 253 |
{ |
|---|
| 254 |
unsigned long d[2]; |
|---|
| 255 |
|
|---|
| 256 |
LDT_EntryToBytes( d, &array ); |
|---|
| 257 |
#if defined(__FreeBSD__) && defined(LDT_AUTO_ALLOC) |
|---|
| 258 |
ret = i386_set_ldt(LDT_AUTO_ALLOC, (union descriptor *)d, 1); |
|---|
| 259 |
array.entry_number = ret; |
|---|
| 260 |
fs_ldt = ret; |
|---|
| 261 |
#else |
|---|
| 262 |
ret = i386_set_ldt(array.entry_number, (union descriptor *)d, 1); |
|---|
| 263 |
#endif |
|---|
| 264 |
if (ret < 0) |
|---|
| 265 |
{ |
|---|
| 266 |
perror("install_fs"); |
|---|
| 267 |
printf("Couldn't install fs segment, expect segfault\n"); |
|---|
| 268 |
printf("Did you reconfigure the kernel with \"options USER_LDT\"?\n"); |
|---|
| 269 |
} |
|---|
| 270 |
} |
|---|
| 271 |
#endif |
|---|
| 272 |
|
|---|
| 273 |
#if defined(__svr4__) |
|---|
| 274 |
{ |
|---|
| 275 |
struct ssd ssd; |
|---|
| 276 |
ssd.sel = LDT_SEL(TEB_SEL_IDX); |
|---|
| 277 |
ssd.bo = array.base_addr; |
|---|
| 278 |
ssd.ls = array.limit - array.base_addr; |
|---|
| 279 |
ssd.acc1 = ((array.read_exec_only == 0) << 1) | |
|---|
| 280 |
(array.contents << 2) | |
|---|
| 281 |
0xf0; |
|---|
| 282 |
ssd.acc2 = 0x4; |
|---|
| 283 |
if (sysi86(SI86DSCR, &ssd) < 0) { |
|---|
| 284 |
perror("sysi86(SI86DSCR)"); |
|---|
| 285 |
printf("Couldn't install fs segment, expect segfault\n"); |
|---|
| 286 |
} |
|---|
| 287 |
} |
|---|
| 288 |
#endif |
|---|
| 289 |
|
|---|
| 290 |
Setup_FS_Segment(); |
|---|
| 291 |
|
|---|
| 292 |
ldt_fs->prev_struct = (char*)malloc(sizeof(char) * 8); |
|---|
| 293 |
*(void**)array.base_addr = ldt_fs->prev_struct; |
|---|
| 294 |
|
|---|
| 295 |
return ldt_fs; |
|---|
| 296 |
} |
|---|
| 297 |
|
|---|
| 298 |
void Restore_LDT_Keeper(ldt_fs_t* ldt_fs) |
|---|
| 299 |
{ |
|---|
| 300 |
if (ldt_fs == NULL || ldt_fs->fs_seg == 0) |
|---|
| 301 |
return; |
|---|
| 302 |
free(ldt_fs->prev_struct); |
|---|
| 303 |
munmap((char*)ldt_fs->fs_seg, getpagesize()); |
|---|
| 304 |
ldt_fs->fs_seg = 0; |
|---|
| 305 |
close(ldt_fs->fd); |
|---|
| 306 |
free(ldt_fs); |
|---|
| 307 |
} |
|---|