Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2001, 2002 Jeff Dike (jdike@karaya.com) |
| 3 | * Licensed under the GPL |
| 4 | */ |
| 5 | |
| 6 | #include "linux/config.h" |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 7 | #include "linux/sched.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 8 | #include "linux/slab.h" |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 9 | #include "linux/types.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 10 | #include "asm/uaccess.h" |
| 11 | #include "asm/ptrace.h" |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 12 | #include "asm/smp.h" |
| 13 | #include "asm/ldt.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 14 | #include "choose-mode.h" |
| 15 | #include "kern.h" |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 16 | #include "mode_kern.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 17 | |
| 18 | #ifdef CONFIG_MODE_TT |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 19 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | extern int modify_ldt(int func, void *ptr, unsigned long bytecount); |
| 21 | |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 22 | static int do_modify_ldt_tt(int func, void *ptr, unsigned long bytecount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 23 | { |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 24 | return modify_ldt(func, ptr, bytecount); |
| 25 | } |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 26 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | #endif |
| 28 | |
| 29 | #ifdef CONFIG_MODE_SKAS |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 30 | |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 31 | #include "skas.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 32 | #include "skas_ptrace.h" |
| 33 | |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 34 | static int do_modify_ldt_skas(int func, void *ptr, unsigned long bytecount) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 35 | { |
| 36 | struct ptrace_ldt ldt; |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 37 | u32 cpu; |
| 38 | int res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 40 | ldt = ((struct ptrace_ldt) { .func = func, |
| 41 | .ptr = ptr, |
| 42 | .bytecount = bytecount }); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 43 | |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 44 | cpu = get_cpu(); |
| 45 | res = ptrace(PTRACE_LDT, userspace_pid[cpu], 0, (unsigned long) &ldt); |
| 46 | put_cpu(); |
| 47 | |
| 48 | return res; |
| 49 | } |
| 50 | #endif |
| 51 | |
| 52 | int sys_modify_ldt(int func, void __user *ptr, unsigned long bytecount) |
| 53 | { |
| 54 | struct user_desc info; |
| 55 | int res = 0; |
| 56 | void *buf = NULL; |
| 57 | void *p = NULL; /* What we pass to host. */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 58 | |
| 59 | switch(func){ |
| 60 | case 1: |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 61 | case 0x11: /* write_ldt */ |
| 62 | /* Do this check now to avoid overflows. */ |
| 63 | if (bytecount != sizeof(struct user_desc)) { |
| 64 | res = -EINVAL; |
| 65 | goto out; |
| 66 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 67 | |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 68 | if(copy_from_user(&info, ptr, sizeof(info))) { |
| 69 | res = -EFAULT; |
| 70 | goto out; |
| 71 | } |
| 72 | |
| 73 | p = &info; |
| 74 | break; |
| 75 | case 0: |
| 76 | case 2: /* read_ldt */ |
| 77 | |
| 78 | /* The use of info avoids kmalloc on the write case, not on the |
| 79 | * read one. */ |
| 80 | buf = kmalloc(bytecount, GFP_KERNEL); |
| 81 | if (!buf) { |
| 82 | res = -ENOMEM; |
| 83 | goto out; |
| 84 | } |
| 85 | p = buf; |
Paolo 'Blaisorblade' Giarrusso | 36decba | 2005-09-21 18:38:57 +0200 | [diff] [blame] | 86 | break; |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 87 | default: |
| 88 | res = -ENOSYS; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 89 | goto out; |
| 90 | } |
| 91 | |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 92 | res = CHOOSE_MODE_PROC(do_modify_ldt_tt, do_modify_ldt_skas, func, |
| 93 | p, bytecount); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 94 | if(res < 0) |
| 95 | goto out; |
| 96 | |
| 97 | switch(func){ |
| 98 | case 0: |
| 99 | case 2: |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 100 | /* Modify_ldt was for reading and returned the number of read |
| 101 | * bytes.*/ |
| 102 | if(copy_to_user(ptr, p, res)) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 103 | res = -EFAULT; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 104 | break; |
| 105 | } |
| 106 | |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 107 | out: |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 108 | kfree(buf); |
Paolo 'Blaisorblade' Giarrusso | 2e5e559 | 2005-07-14 00:33:37 -0700 | [diff] [blame] | 109 | return res; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 110 | } |