tls.c 7.5 KB

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