tls.c 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273
  1. #include <linux/kernel.h>
  2. #include <linux/errno.h>
  3. #include <linux/sched.h>
  4. #include <linux/user.h>
  5. #include <linux/regset.h>
  6. #include <linux/syscalls.h>
  7. #include <asm/uaccess.h>
  8. #include <asm/desc.h>
  9. #include <asm/ldt.h>
  10. #include <asm/processor.h>
  11. #include <asm/proto.h>
  12. #include "tls.h"
  13. /*
  14. * sys_alloc_thread_area: get a yet unused TLS descriptor index.
  15. */
  16. static int get_free_idx(void)
  17. {
  18. struct thread_struct *t = &current->thread;
  19. int idx;
  20. for (idx = 0; idx < GDT_ENTRY_TLS_ENTRIES; idx++)
  21. if (desc_empty(&t->tls_array[idx]))
  22. return idx + GDT_ENTRY_TLS_MIN;
  23. return -ESRCH;
  24. }
  25. static bool tls_desc_okay(const struct user_desc *info)
  26. {
  27. /*
  28. * For historical reasons (i.e. no one ever documented how any
  29. * of the segmentation APIs work), user programs can and do
  30. * assume that a struct user_desc that's all zeros except for
  31. * entry_number means "no segment at all". This never actually
  32. * worked. In fact, up to Linux 3.19, a struct user_desc like
  33. * this would create a 16-bit read-write segment with base and
  34. * limit both equal to zero.
  35. *
  36. * That was close enough to "no segment at all" until we
  37. * hardened this function to disallow 16-bit TLS segments. Fix
  38. * it up by interpreting these zeroed segments the way that they
  39. * were almost certainly intended to be interpreted.
  40. *
  41. * The correct way to ask for "no segment at all" is to specify
  42. * a user_desc that satisfies LDT_empty. To keep everything
  43. * working, we accept both.
  44. *
  45. * Note that there's a similar kludge in modify_ldt -- look at
  46. * the distinction between modes 1 and 0x11.
  47. */
  48. if (LDT_empty(info) || LDT_zero(info))
  49. return true;
  50. /*
  51. * espfix is required for 16-bit data segments, but espfix
  52. * only works for LDT segments.
  53. */
  54. if (!info->seg_32bit)
  55. return false;
  56. /* Only allow data segments in the TLS array. */
  57. if (info->contents > 1)
  58. return false;
  59. /*
  60. * Non-present segments with DPL 3 present an interesting attack
  61. * surface. The kernel should handle such segments correctly,
  62. * but TLS is very difficult to protect in a sandbox, so prevent
  63. * such segments from being created.
  64. *
  65. * If userspace needs to remove a TLS entry, it can still delete
  66. * it outright.
  67. */
  68. if (info->seg_not_present)
  69. return false;
  70. return true;
  71. }
  72. static void set_tls_desc(struct task_struct *p, int idx,
  73. const struct user_desc *info, int n)
  74. {
  75. struct thread_struct *t = &p->thread;
  76. struct desc_struct *desc = &t->tls_array[idx - GDT_ENTRY_TLS_MIN];
  77. int cpu;
  78. /*
  79. * We must not get preempted while modifying the TLS.
  80. */
  81. cpu = get_cpu();
  82. while (n-- > 0) {
  83. if (LDT_empty(info) || LDT_zero(info))
  84. desc->a = desc->b = 0;
  85. else
  86. fill_ldt(desc, info);
  87. ++info;
  88. ++desc;
  89. }
  90. if (t == &current->thread)
  91. load_TLS(t, cpu);
  92. put_cpu();
  93. }
  94. /*
  95. * Set a given TLS descriptor:
  96. */
  97. int do_set_thread_area(struct task_struct *p, int idx,
  98. struct user_desc __user *u_info,
  99. int can_allocate)
  100. {
  101. struct user_desc info;
  102. if (copy_from_user(&info, u_info, sizeof(info)))
  103. return -EFAULT;
  104. if (!tls_desc_okay(&info))
  105. return -EINVAL;
  106. if (idx == -1)
  107. idx = info.entry_number;
  108. /*
  109. * index -1 means the kernel should try to find and
  110. * allocate an empty descriptor:
  111. */
  112. if (idx == -1 && can_allocate) {
  113. idx = get_free_idx();
  114. if (idx < 0)
  115. return idx;
  116. if (put_user(idx, &u_info->entry_number))
  117. return -EFAULT;
  118. }
  119. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  120. return -EINVAL;
  121. set_tls_desc(p, idx, &info, 1);
  122. return 0;
  123. }
  124. SYSCALL_DEFINE1(set_thread_area, struct user_desc __user *, u_info)
  125. {
  126. return do_set_thread_area(current, -1, u_info, 1);
  127. }
  128. /*
  129. * Get the current Thread-Local Storage area:
  130. */
  131. static void fill_user_desc(struct user_desc *info, int idx,
  132. const struct desc_struct *desc)
  133. {
  134. memset(info, 0, sizeof(*info));
  135. info->entry_number = idx;
  136. info->base_addr = get_desc_base(desc);
  137. info->limit = get_desc_limit(desc);
  138. info->seg_32bit = desc->d;
  139. info->contents = desc->type >> 2;
  140. info->read_exec_only = !(desc->type & 2);
  141. info->limit_in_pages = desc->g;
  142. info->seg_not_present = !desc->p;
  143. info->useable = desc->avl;
  144. #ifdef CONFIG_X86_64
  145. info->lm = desc->l;
  146. #endif
  147. }
  148. int do_get_thread_area(struct task_struct *p, int idx,
  149. struct user_desc __user *u_info)
  150. {
  151. struct user_desc info;
  152. if (idx == -1 && get_user(idx, &u_info->entry_number))
  153. return -EFAULT;
  154. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  155. return -EINVAL;
  156. fill_user_desc(&info, idx,
  157. &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
  158. if (copy_to_user(u_info, &info, sizeof(info)))
  159. return -EFAULT;
  160. return 0;
  161. }
  162. SYSCALL_DEFINE1(get_thread_area, struct user_desc __user *, u_info)
  163. {
  164. return do_get_thread_area(current, -1, u_info);
  165. }
  166. int regset_tls_active(struct task_struct *target,
  167. const struct user_regset *regset)
  168. {
  169. struct thread_struct *t = &target->thread;
  170. int n = GDT_ENTRY_TLS_ENTRIES;
  171. while (n > 0 && desc_empty(&t->tls_array[n - 1]))
  172. --n;
  173. return n;
  174. }
  175. int regset_tls_get(struct task_struct *target, const struct user_regset *regset,
  176. unsigned int pos, unsigned int count,
  177. void *kbuf, void __user *ubuf)
  178. {
  179. const struct desc_struct *tls;
  180. if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
  181. (pos % sizeof(struct user_desc)) != 0 ||
  182. (count % sizeof(struct user_desc)) != 0)
  183. return -EINVAL;
  184. pos /= sizeof(struct user_desc);
  185. count /= sizeof(struct user_desc);
  186. tls = &target->thread.tls_array[pos];
  187. if (kbuf) {
  188. struct user_desc *info = kbuf;
  189. while (count-- > 0)
  190. fill_user_desc(info++, GDT_ENTRY_TLS_MIN + pos++,
  191. tls++);
  192. } else {
  193. struct user_desc __user *u_info = ubuf;
  194. while (count-- > 0) {
  195. struct user_desc info;
  196. fill_user_desc(&info, GDT_ENTRY_TLS_MIN + pos++, tls++);
  197. if (__copy_to_user(u_info++, &info, sizeof(info)))
  198. return -EFAULT;
  199. }
  200. }
  201. return 0;
  202. }
  203. int regset_tls_set(struct task_struct *target, const struct user_regset *regset,
  204. unsigned int pos, unsigned int count,
  205. const void *kbuf, const void __user *ubuf)
  206. {
  207. struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
  208. const struct user_desc *info;
  209. int i;
  210. if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
  211. (pos % sizeof(struct user_desc)) != 0 ||
  212. (count % sizeof(struct user_desc)) != 0)
  213. return -EINVAL;
  214. if (kbuf)
  215. info = kbuf;
  216. else if (__copy_from_user(infobuf, ubuf, count))
  217. return -EFAULT;
  218. else
  219. info = infobuf;
  220. for (i = 0; i < count / sizeof(struct user_desc); i++)
  221. if (!tls_desc_okay(info + i))
  222. return -EINVAL;
  223. set_tls_desc(target,
  224. GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
  225. info, count / sizeof(struct user_desc));
  226. return 0;
  227. }