tls.c 7.0 KB

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