| Start/ | End/ | |||
| True | False | - | Line | Source |
| 1 | /* | |||
| 2 | * 32bit ptrace for x86-64. | |||
| 3 | * | |||
| 4 | * Copyright 2001,2002 Andi Kleen, SuSE Labs. | |||
| 5 | * Some parts copied from arch/i386/kernel/ptrace.c. See that file for earlier | |||
| 6 | * copyright. | |||
| 7 | * | |||
| 8 | * This allows to access 64bit processes too; but there is no way to see the extended | |||
| 9 | * register contents. | |||
| 10 | * | |||
| 11 | * $Id: ptrace32.c,v 1.16 2003/03/14 16:06:35 ak Exp $ | |||
| 12 | */ | |||
| 13 | ||||
| 14 | #include <linux/kernel.h> | |||
| 15 | #include <linux/stddef.h> | |||
| 16 | #include <linux/sched.h> | |||
| 17 | #include <linux/syscalls.h> | |||
| 18 | #include <linux/unistd.h> | |||
| 19 | #include <linux/mm.h> | |||
| 20 | #include <linux/ptrace.h> | |||
| 21 | #include <asm/ptrace.h> | |||
| 22 | #include <asm/compat.h> | |||
| 23 | #include <asm/uaccess.h> | |||
| 24 | #include <asm/user32.h> | |||
| 25 | #include <asm/user.h> | |||
| 26 | #include <asm/errno.h> | |||
| 27 | #include <asm/debugreg.h> | |||
| 28 | #include <asm/i387.h> | |||
| 29 | #include <asm/fpu32.h> | |||
| 30 | ||||
| 31 | /* | |||
| 32 | * Determines which flags the user has access to [1 = access, 0 = no access]. | |||
| 33 | * Prohibits changing ID(21), VIP(20), VIF(19), VM(17), IOPL(12-13), IF(9). | |||
| 34 | * Also masks reserved bits (31-22, 15, 5, 3, 1). | |||
| 35 | */ | |||
| 36 | #define FLAG_MASK 0x54dd5UL | |||
| 37 | ||||
| 38 | #define R32(l,q) \ | |||
| 39 | case offsetof(struct user32, regs.l): stack[offsetof(struct pt_regs, q)/8] = val; break | |||
| 40 | ||||
| 0 | 0 | - | 41 | static int putreg32(struct task_struct *child, unsigned regno, u32 val) |
| 42 | { | |||
| 43 | int i; | |||
| 44 | __u64 *stack = (__u64 *)task_pt_regs(child); | |||
| 45 | ||||
| 46 | switch (regno) { | |||
| 0 | - | 47 | case offsetof(struct user32, regs.fs): | |
| 0 | 0 | - | 48 | if (val && (val & 3) != 3) return -EIO; |
| 0 | - | 48 | T && T | |
| 0 | - | 48 | T && F | |
| 0 | - | 48 | F && _ | |
| 0 | - | 48 | return - 5 | |
| 49 | child->thread.fsindex = val & 0xffff; | |||
| 0 | - | 50 | break; | |
| 0 | - | 51 | case offsetof(struct user32, regs.gs): | |
| 0 | 0 | - | 52 | if (val && (val & 3) != 3) return -EIO; |
| 0 | - | 52 | T && T | |
| 0 | - | 52 | T && F | |
| 0 | - | 52 | F && _ | |
| 0 | - | 52 | return - 5 | |
| 53 | child->thread.gsindex = val & 0xffff; | |||
| 0 | - | 54 | break; | |
| 0 | - | 55 | case offsetof(struct user32, regs.ds): | |
| 0 | 0 | - | 56 | if (val && (val & 3) != 3) return -EIO; |
| 0 | - | 56 | T && T | |
| 0 | - | 56 | T && F | |
| 0 | - | 56 | F && _ | |
| 0 | - | 56 | return - 5 | |
| 57 | child->thread.ds = val & 0xffff; | |||
| 0 | - | 58 | break; | |
| 0 | - | 59 | case offsetof(struct user32, regs.es): | |
| 60 | child->thread.es = val & 0xffff; | |||
| 0 | - | 61 | break; | |
| 0 | - | 62 | case offsetof(struct user32, regs.ss): | |
| 0 | 0 | - | 63 | if ((val & 3) != 3) return -EIO; |
| 0 | - | 63 | return - 5 | |
| 64 | stack[offsetof(struct pt_regs, ss)/8] = val & 0xffff; | |||
| 0 | - | 65 | break; | |
| 0 | - | 66 | case offsetof(struct user32, regs.cs): | |
| 0 | 0 | - | 67 | if ((val & 3) != 3) return -EIO; |
| 0 | - | 67 | return - 5 | |
| 68 | stack[offsetof(struct pt_regs, cs)/8] = val & 0xffff; | |||
| 0 | - | 69 | break; | |
| 70 | ||||
| 0 | - | 71 | R32(ebx, rbx); | |
| 0 | - | 71 | break | |
| 0 | - | 72 | R32(ecx, rcx); | |
| 0 | - | 72 | break | |
| 0 | - | 73 | R32(edx, rdx); | |
| 0 | - | 73 | break | |
| 0 | - | 74 | R32(edi, rdi); | |
| 0 | - | 74 | break | |
| 0 | - | 75 | R32(esi, rsi); | |
| 0 | - | 75 | break | |
| 0 | - | 76 | R32(ebp, rbp); | |
| 0 | - | 76 | break | |
| 0 | - | 77 | R32(eax, rax); | |
| 0 | - | 77 | break | |
| 0 | - | 78 | R32(orig_eax, orig_rax); | |
| 0 | - | 78 | break | |
| 0 | - | 79 | R32(eip, rip); | |
| 0 | - | 79 | break | |
| 0 | - | 80 | R32(esp, rsp); | |
| 0 | - | 80 | break | |
| 81 | ||||
| 0 | - | 82 | case offsetof(struct user32, regs.eflags): { | |
| 83 | __u64 *flags = &stack[offsetof(struct pt_regs, eflags)/8]; | |||
| 84 | val &= FLAG_MASK; | |||
| 85 | *flags = val | (*flags & ~FLAG_MASK); | |||
| 0 | - | 86 | break; | |
| 87 | } | |||
| 88 | ||||
| 0 | - | 89 | case offsetof(struct user32, u_debugreg[4]): | |
| 0 | - | 90 | case offsetof(struct user32, u_debugreg[5]): | |
| 0 | - | 91 | return -EIO; | |
| 92 | ||||
| 0 | - | 93 | case offsetof(struct user32, u_debugreg[0]): | |
| 94 | child->thread.debugreg0 = val; | |||
| 0 | - | 95 | break; | |
| 96 | ||||
| 0 | - | 97 | case offsetof(struct user32, u_debugreg[1]): | |
| 98 | child->thread.debugreg1 = val; | |||
| 0 | - | 99 | break; | |
| 100 | ||||
| 0 | - | 101 | case offsetof(struct user32, u_debugreg[2]): | |
| 102 | child->thread.debugreg2 = val; | |||
| 0 | - | 103 | break; | |
| 104 | ||||
| 0 | - | 105 | case offsetof(struct user32, u_debugreg[3]): | |
| 106 | child->thread.debugreg3 = val; | |||
| 0 | - | 107 | break; | |
| 108 | ||||
| 0 | - | 109 | case offsetof(struct user32, u_debugreg[6]): | |
| 110 | child->thread.debugreg6 = val; | |||
| 0 | - | 111 | break; | |
| 112 | ||||
| 0 | - | 113 | case offsetof(struct user32, u_debugreg[7]): | |
| 114 | val &= ~DR_CONTROL_RESERVED; | |||
| 115 | /* See arch/i386/kernel/ptrace.c for an explanation of | |||
| 116 | * this awkward check.*/ | |||
| 0 | 0 | - | 117 | for(i=0; i<4; i++) |
| 0 | 0 | - | 118 | if ((0x5454 >> ((val >> (16 + 4*i)) & 0xf)) & 1) |
| 0 | - | 119 | return -EIO; | |
| 120 | child->thread.debugreg7 = val; | |||
| 0 | - | 121 | break; | |
| 122 | ||||
| 0 | - | 123 | default: | |
| 0 | 0 | - | 124 | if (regno > sizeof(struct user32) || (regno & 3)) |
| 0 | - | 124 | T || (_) | |
| 0 | - | 124 | F || (T) | |
| 0 | - | 124 | F || (F) | |
| 0 | - | 125 | return -EIO; | |
| 126 | ||||
| 127 | /* Other dummy fields in the virtual user structure are ignored */ | |||
| 0 | - | 128 | break; | |
| 129 | } | |||
| 0 | - | 130 | return 0; | |
| 131 | } | |||
| 132 | ||||
| 133 | #undef R32 | |||
| 134 | ||||
| 135 | #define R32(l,q) \ | |||
| 136 | case offsetof(struct user32, regs.l): *val = stack[offsetof(struct pt_regs, q)/8]; break | |||
| 137 | ||||
| 0 | 0 | - | 138 | static int getreg32(struct task_struct *child, unsigned regno, u32 *val) |
| 139 | { | |||
| 140 | __u64 *stack = (__u64 *)task_pt_regs(child); | |||
| 141 | ||||
| 142 | switch (regno) { | |||
| 0 | - | 143 | case offsetof(struct user32, regs.fs): | |
| 144 | *val = child->thread.fsindex; | |||
| 0 | - | 145 | break; | |
| 0 | - | 146 | case offsetof(struct user32, regs.gs): | |
| 147 | *val = child->thread.gsindex; | |||
| 0 | - | 148 | break; | |
| 0 | - | 149 | case offsetof(struct user32, regs.ds): | |
| 150 | *val = child->thread.ds; | |||
| 0 | - | 151 | break; | |
| 0 | - | 152 | case offsetof(struct user32, regs.es): | |
| 153 | *val = child->thread.es; | |||
| 0 | - | 154 | break; | |
| 155 | ||||
| 0 | - | 156 | R32(cs, cs); | |
| 0 | - | 156 | break | |
| 0 | - | 157 | R32(ss, ss); | |
| 0 | - | 157 | break | |
| 0 | - | 158 | R32(ebx, rbx); | |
| 0 | - | 158 | break | |
| 0 | - | 159 | R32(ecx, rcx); | |
| 0 | - | 159 | break | |
| 0 | - | 160 | R32(edx, rdx); | |
| 0 | - | 160 | break | |
| 0 | - | 161 | R32(edi, rdi); | |
| 0 | - | 161 | break | |
| 0 | - | 162 | R32(esi, rsi); | |
| 0 | - | 162 | break | |
| 0 | - | 163 | R32(ebp, rbp); | |
| 0 | - | 163 | break | |
| 0 | - | 164 | R32(eax, rax); | |
| 0 | - | 164 | break | |
| 0 | - | 165 | R32(orig_eax, orig_rax); | |
| 0 | - | 165 | break | |
| 0 | - | 166 | R32(eip, rip); | |
| 0 | - | 166 | break | |
| 0 | - | 167 | R32(eflags, eflags); | |
| 0 | - | 167 | break | |
| 0 | - | 168 | R32(esp, rsp); | |
| 0 | - | 168 | break | |
| 169 | ||||
| 0 | - | 170 | case offsetof(struct user32, u_debugreg[0]): | |
| 171 | *val = child->thread.debugreg0; | |||
| 0 | - | 172 | break; | |
| 0 | - | 173 | case offsetof(struct user32, u_debugreg[1]): | |
| 174 | *val = child->thread.debugreg1; | |||
| 0 | - | 175 | break; | |
| 0 | - | 176 | case offsetof(struct user32, u_debugreg[2]): | |
| 177 | *val = child->thread.debugreg2; | |||
| 0 | - | 178 | break; | |
| 0 | - | 179 | case offsetof(struct user32, u_debugreg[3]): | |
| 180 | *val = child->thread.debugreg3; | |||
| 0 | - | 181 | break; | |
| 0 | - | 182 | case offsetof(struct user32, u_debugreg[6]): | |
| 183 | *val = child->thread.debugreg6; | |||
| 0 | - | 184 | break; | |
| 0 | - | 185 | case offsetof(struct user32, u_debugreg[7]): | |
| 186 | *val = child->thread.debugreg7; | |||
| 0 | - | 187 | break; | |
| 188 | ||||
| 0 | - | 189 | default: | |
| 0 | 0 | - | 190 | if (regno > sizeof(struct user32) || (regno & 3)) |
| 0 | - | 190 | T || (_) | |
| 0 | - | 190 | F || (T) | |
| 0 | - | 190 | F || (F) | |
| 0 | - | 191 | return -EIO; | |
| 192 | ||||
| 193 | /* Other dummy fields in the virtual user structure are ignored */ | |||
| 194 | *val = 0; | |||
| 0 | - | 195 | break; | |
| 196 | } | |||
| 0 | - | 197 | return 0; | |
| 198 | } | |||
| 199 | ||||
| 200 | #undef R32 | |||
| 201 | ||||
| 0 | 0 | - | 202 | asmlinkage long sys32_ptrace(long request, u32 pid, u32 addr, u32 data) |
| 203 | { | |||
| 204 | struct task_struct *child; | |||
| 205 | struct pt_regs *childregs; | |||
| 206 | void __user *datap = compat_ptr(data); | |||
| 207 | int ret; | |||
| 208 | __u32 val; | |||
| 209 | ||||
| 210 | switch (request) { | |||
| 0 | - | 211 | default: | |
| 0 | - | 212 | return sys_ptrace(request, pid, addr, data); | |
| 213 | ||||
| 0 | - | 214 | case PTRACE_PEEKTEXT: | |
| 0 | - | 215 | case PTRACE_PEEKDATA: | |
| 0 | - | 216 | case PTRACE_POKEDATA: | |
| 0 | - | 217 | case PTRACE_POKETEXT: | |
| 0 | - | 218 | case PTRACE_POKEUSR: | |
| 0 | - | 219 | case PTRACE_PEEKUSR: | |
| 0 | - | 220 | case PTRACE_GETREGS: | |
| 0 | - | 221 | case PTRACE_SETREGS: | |
| 0 | - | 222 | case PTRACE_SETFPREGS: | |
| 0 | - | 223 | case PTRACE_GETFPREGS: | |
| 0 | - | 224 | case PTRACE_SETFPXREGS: | |
| 0 | - | 225 | case PTRACE_GETFPXREGS: | |
| 0 | - | 226 | case PTRACE_GETEVENTMSG: | |
| 0 | - | 227 | break; | |
| 228 | } | |||
| 229 | ||||
| 0 | 0 | - | 230 | if (request == PTRACE_TRACEME) |
| 0 | - | 231 | return ptrace_traceme(); | |
| 232 | ||||
| 233 | child = ptrace_get_task_struct(pid); | |||
| 0 | 0 | - | 234 | if (IS_ERR(child)) |
| 0 | - | 235 | return PTR_ERR(child); | |
| 236 | ||||
| 237 | ret = ptrace_check_attach(child, request == PTRACE_KILL); | |||
| 0 | 0 | - | 238 | if (ret < 0) |
| 0 | - | 239 | goto out; | |
| 240 | ||||
| 241 | childregs = task_pt_regs(child); | |||
| 242 | ||||
| 243 | switch (request) { | |||
| 0 | - | 244 | case PTRACE_PEEKDATA: | |
| 0 | - | 245 | case PTRACE_PEEKTEXT: | |
| 246 | ret = 0; | |||
| 0 | 0 | - | 247 | if (access_process_vm(child, addr, &val, sizeof(u32), 0)!=sizeof(u32)) |
| 248 | ret = -EIO; | |||
| 249 | else | |||
| 250 | ret = put_user(val, (unsigned int __user *)datap); | |||
| 0 | - | 251 | break; | |
| 252 | ||||
| 0 | - | 253 | case PTRACE_POKEDATA: | |
| 0 | - | 254 | case PTRACE_POKETEXT: | |
| 255 | ret = 0; | |||
| 0 | 0 | - | 256 | if (access_process_vm(child, addr, &data, sizeof(u32), 1)!=sizeof(u32)) |
| 257 | ret = -EIO; | |||
| 0 | - | 258 | break; | |
| 259 | ||||
| 0 | - | 260 | case PTRACE_PEEKUSR: | |
| 261 | ret = getreg32(child, addr, &val); | |||
| 0 | 0 | - | 262 | if (ret == 0) |
| 263 | ret = put_user(val, (__u32 __user *)datap); | |||
| 0 | - | 264 | break; | |
| 265 | ||||
| 0 | - | 266 | case PTRACE_POKEUSR: | |
| 267 | ret = putreg32(child, addr, data); | |||
| 0 | - | 268 | break; | |
| 269 | ||||
| 0 | - | 270 | case PTRACE_GETREGS: { /* Get all gp regs from the child. */ | |
| 271 | int i; | |||
| 0 | 0 | - | 272 | if (!access_ok(VERIFY_WRITE, datap, 16*4)) { |
| 273 | ret = -EIO; | |||
| 0 | - | 274 | break; | |
| 275 | } | |||
| 276 | ret = 0; | |||
| 0 | 0 | - | 277 | for ( i = 0; i <= 16*4 ; i += sizeof(__u32) ) { |
| 278 | getreg32(child, i, &val); | |||
| 279 | ret |= __put_user(val,(u32 __user *)datap); | |||
| 280 | datap += sizeof(u32); | |||
| 281 | } | |||
| 0 | - | 282 | break; | |
| 283 | } | |||
| 284 | ||||
| 0 | - | 285 | case PTRACE_SETREGS: { /* Set all gp regs in the child. */ | |
| 286 | unsigned long tmp; | |||
| 287 | int i; | |||
| 0 | 0 | - | 288 | if (!access_ok(VERIFY_READ, datap, 16*4)) { |
| 289 | ret = -EIO; | |||
| 0 | - | 290 | break; | |
| 291 | } | |||
| 292 | ret = 0; | |||
| 0 | 0 | - | 293 | for ( i = 0; i <= 16*4; i += sizeof(u32) ) { |
| 294 | ret |= __get_user(tmp, (u32 __user *)datap); | |||
| 295 | putreg32(child, i, tmp); | |||
| 296 | datap += sizeof(u32); | |||
| 297 | } | |||
| 0 | - | 298 | break; | |
| 299 | } | |||
| 300 | ||||
| 0 | - | 301 | case PTRACE_GETFPREGS: | |
| 302 | ret = -EIO; | |||
| 0 | 0 | - | 303 | if (!access_ok(VERIFY_READ, compat_ptr(data), |
| 304 | sizeof(struct user_i387_struct))) | |||
| 0 | - | 305 | break; | |
| 306 | save_i387_ia32(child, datap, childregs, 1); | |||
| 307 | ret = 0; | |||
| 0 | - | 308 | break; | |
| 309 | ||||
| 0 | - | 310 | case PTRACE_SETFPREGS: | |
| 311 | ret = -EIO; | |||
| 0 | 0 | - | 312 | if (!access_ok(VERIFY_WRITE, datap, |
| 313 | sizeof(struct user_i387_struct))) | |||
| 0 | - | 314 | break; | |
| 315 | ret = 0; | |||
| 316 | /* don't check EFAULT to be bug-to-bug compatible to i386 */ | |||
| 317 | restore_i387_ia32(child, datap, 1); | |||
| 0 | - | 318 | break; | |
| 319 | ||||
| 0 | - | 320 | case PTRACE_GETFPXREGS: { | |
| 321 | struct user32_fxsr_struct __user *u = datap; | |||
| 322 | init_fpu(child); | |||
| 323 | ret = -EIO; | |||
| 0 | 0 | - | 324 | if (!access_ok(VERIFY_WRITE, u, sizeof(*u))) |
| 0 | - | 325 | break; | |
| 326 | ret = -EFAULT; | |||
| 0 | 0 | - | 327 | if (__copy_to_user(u, &child->thread.i387.fxsave, sizeof(*u))) |
| 0 | - | 328 | break; | |
| 329 | ret = __put_user(childregs->cs, &u->fcs); | |||
| 330 | ret |= __put_user(child->thread.ds, &u->fos); | |||
| 0 | - | 331 | break; | |
| 332 | } | |||
| 0 | - | 333 | case PTRACE_SETFPXREGS: { | |
| 334 | struct user32_fxsr_struct __user *u = datap; | |||
| 335 | unlazy_fpu(child); | |||
| 0 | 0 | - | 335 | if (( child ) -> thread_info -> status &.. |
| 0 | 0 | - | 335 | do-while (0) |
| 336 | ret = -EIO; | |||
| 0 | 0 | - | 337 | if (!access_ok(VERIFY_READ, u, sizeof(*u))) |
| 0 | - | 338 | break; | |
| 339 | /* no checking to be bug-to-bug compatible with i386 */ | |||
| 340 | __copy_from_user(&child->thread.i387.fxsave, u, sizeof(*u)); | |||
| 341 | set_stopped_child_used_math(child); | |||
| 0 | 0 | - | 341 | do-while (0) |
| 342 | child->thread.i387.fxsave.mxcsr &= mxcsr_feature_mask; | |||
| 343 | ret = 0; | |||
| 0 | - | 344 | break; | |
| 345 | } | |||
| 346 | ||||
| 0 | - | 347 | case PTRACE_GETEVENTMSG: | |
| 348 | ret = put_user(child->ptrace_message,(unsigned int __user *)compat_ptr(data)); | |||
| 0 | - | 349 | break; | |
| 350 | ||||
| 0 | - | 351 | default: | |
| 352 | ret = -EINVAL; | |||
| 0 | - | 353 | break; | |
| 354 | } | |||
| 355 | ||||
| 356 | out: | |||
| 357 | put_task_struct(child); | |||
| 0 | - | 358 | return ret; | |
| 359 | } | |||
| 360 | ||||
| ***TER 0% (0/234) of SOURCE FILE ptrace32.c | ||||