tls.c 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316
  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. unsigned short __maybe_unused sel, modified_sel;
  103. if (copy_from_user(&info, u_info, sizeof(info)))
  104. return -EFAULT;
  105. if (!tls_desc_okay(&info))
  106. return -EINVAL;
  107. if (idx == -1)
  108. idx = info.entry_number;
  109. /*
  110. * index -1 means the kernel should try to find and
  111. * allocate an empty descriptor:
  112. */
  113. if (idx == -1 && can_allocate) {
  114. idx = get_free_idx();
  115. if (idx < 0)
  116. return idx;
  117. if (put_user(idx, &u_info->entry_number))
  118. return -EFAULT;
  119. }
  120. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  121. return -EINVAL;
  122. set_tls_desc(p, idx, &info, 1);
  123. /*
  124. * If DS, ES, FS, or GS points to the modified segment, forcibly
  125. * refresh it. Only needed on x86_64 because x86_32 reloads them
  126. * on return to user mode.
  127. */
  128. modified_sel = (idx << 3) | 3;
  129. if (p == current) {
  130. #ifdef CONFIG_X86_64
  131. savesegment(ds, sel);
  132. if (sel == modified_sel)
  133. loadsegment(ds, sel);
  134. savesegment(es, sel);
  135. if (sel == modified_sel)
  136. loadsegment(es, sel);
  137. savesegment(fs, sel);
  138. if (sel == modified_sel)
  139. loadsegment(fs, sel);
  140. savesegment(gs, sel);
  141. if (sel == modified_sel)
  142. load_gs_index(sel);
  143. #endif
  144. #ifdef CONFIG_X86_32_LAZY_GS
  145. savesegment(gs, sel);
  146. if (sel == modified_sel)
  147. loadsegment(gs, sel);
  148. #endif
  149. } else {
  150. #ifdef CONFIG_X86_64
  151. if (p->thread.fsindex == modified_sel)
  152. p->thread.fsbase = info.base_addr;
  153. if (p->thread.gsindex == modified_sel)
  154. p->thread.gsbase = info.base_addr;
  155. #endif
  156. }
  157. return 0;
  158. }
  159. SYSCALL_DEFINE1(set_thread_area, struct user_desc __user *, u_info)
  160. {
  161. return do_set_thread_area(current, -1, u_info, 1);
  162. }
  163. /*
  164. * Get the current Thread-Local Storage area:
  165. */
  166. static void fill_user_desc(struct user_desc *info, int idx,
  167. const struct desc_struct *desc)
  168. {
  169. memset(info, 0, sizeof(*info));
  170. info->entry_number = idx;
  171. info->base_addr = get_desc_base(desc);
  172. info->limit = get_desc_limit(desc);
  173. info->seg_32bit = desc->d;
  174. info->contents = desc->type >> 2;
  175. info->read_exec_only = !(desc->type & 2);
  176. info->limit_in_pages = desc->g;
  177. info->seg_not_present = !desc->p;
  178. info->useable = desc->avl;
  179. #ifdef CONFIG_X86_64
  180. info->lm = desc->l;
  181. #endif
  182. }
  183. int do_get_thread_area(struct task_struct *p, int idx,
  184. struct user_desc __user *u_info)
  185. {
  186. struct user_desc info;
  187. if (idx == -1 && get_user(idx, &u_info->entry_number))
  188. return -EFAULT;
  189. if (idx < GDT_ENTRY_TLS_MIN || idx > GDT_ENTRY_TLS_MAX)
  190. return -EINVAL;
  191. fill_user_desc(&info, idx,
  192. &p->thread.tls_array[idx - GDT_ENTRY_TLS_MIN]);
  193. if (copy_to_user(u_info, &info, sizeof(info)))
  194. return -EFAULT;
  195. return 0;
  196. }
  197. SYSCALL_DEFINE1(get_thread_area, struct user_desc __user *, u_info)
  198. {
  199. return do_get_thread_area(current, -1, u_info);
  200. }
  201. int regset_tls_active(struct task_struct *target,
  202. const struct user_regset *regset)
  203. {
  204. struct thread_struct *t = &target->thread;
  205. int n = GDT_ENTRY_TLS_ENTRIES;
  206. while (n > 0 && desc_empty(&t->tls_array[n - 1]))
  207. --n;
  208. return n;
  209. }
  210. int regset_tls_get(struct task_struct *target, const struct user_regset *regset,
  211. unsigned int pos, unsigned int count,
  212. void *kbuf, void __user *ubuf)
  213. {
  214. const struct desc_struct *tls;
  215. if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
  216. (pos % sizeof(struct user_desc)) != 0 ||
  217. (count % sizeof(struct user_desc)) != 0)
  218. return -EINVAL;
  219. pos /= sizeof(struct user_desc);
  220. count /= sizeof(struct user_desc);
  221. tls = &target->thread.tls_array[pos];
  222. if (kbuf) {
  223. struct user_desc *info = kbuf;
  224. while (count-- > 0)
  225. fill_user_desc(info++, GDT_ENTRY_TLS_MIN + pos++,
  226. tls++);
  227. } else {
  228. struct user_desc __user *u_info = ubuf;
  229. while (count-- > 0) {
  230. struct user_desc info;
  231. fill_user_desc(&info, GDT_ENTRY_TLS_MIN + pos++, tls++);
  232. if (__copy_to_user(u_info++, &info, sizeof(info)))
  233. return -EFAULT;
  234. }
  235. }
  236. return 0;
  237. }
  238. int regset_tls_set(struct task_struct *target, const struct user_regset *regset,
  239. unsigned int pos, unsigned int count,
  240. const void *kbuf, const void __user *ubuf)
  241. {
  242. struct user_desc infobuf[GDT_ENTRY_TLS_ENTRIES];
  243. const struct user_desc *info;
  244. int i;
  245. if (pos >= GDT_ENTRY_TLS_ENTRIES * sizeof(struct user_desc) ||
  246. (pos % sizeof(struct user_desc)) != 0 ||
  247. (count % sizeof(struct user_desc)) != 0)
  248. return -EINVAL;
  249. if (kbuf)
  250. info = kbuf;
  251. else if (__copy_from_user(infobuf, ubuf, count))
  252. return -EFAULT;
  253. else
  254. info = infobuf;
  255. for (i = 0; i < count / sizeof(struct user_desc); i++)
  256. if (!tls_desc_okay(info + i))
  257. return -EINVAL;
  258. set_tls_desc(target,
  259. GDT_ENTRY_TLS_MIN + (pos / sizeof(struct user_desc)),
  260. info, count / sizeof(struct user_desc));
  261. return 0;
  262. }