pkeys.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. /*
  2. * Intel Memory Protection Keys management
  3. * Copyright (c) 2015, Intel Corporation.
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms and conditions of the GNU General Public License,
  7. * version 2, as published by the Free Software Foundation.
  8. *
  9. * This program is distributed in the hope it will be useful, but WITHOUT
  10. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  12. * more details.
  13. */
  14. #include <linux/debugfs.h> /* debugfs_create_u32() */
  15. #include <linux/mm_types.h> /* mm_struct, vma, etc... */
  16. #include <linux/pkeys.h> /* PKEY_* */
  17. #include <uapi/asm-generic/mman-common.h>
  18. #include <asm/cpufeature.h> /* boot_cpu_has, ... */
  19. #include <asm/mmu_context.h> /* vma_pkey() */
  20. int __execute_only_pkey(struct mm_struct *mm)
  21. {
  22. bool need_to_set_mm_pkey = false;
  23. int execute_only_pkey = mm->context.execute_only_pkey;
  24. int ret;
  25. /* Do we need to assign a pkey for mm's execute-only maps? */
  26. if (execute_only_pkey == -1) {
  27. /* Go allocate one to use, which might fail */
  28. execute_only_pkey = mm_pkey_alloc(mm);
  29. if (execute_only_pkey < 0)
  30. return -1;
  31. need_to_set_mm_pkey = true;
  32. }
  33. /*
  34. * We do not want to go through the relatively costly
  35. * dance to set PKRU if we do not need to. Check it
  36. * first and assume that if the execute-only pkey is
  37. * write-disabled that we do not have to set it
  38. * ourselves. We need preempt off so that nobody
  39. * can make fpregs inactive.
  40. */
  41. preempt_disable();
  42. if (!need_to_set_mm_pkey &&
  43. current->thread.fpu.initialized &&
  44. !__pkru_allows_read(read_pkru(), execute_only_pkey)) {
  45. preempt_enable();
  46. return execute_only_pkey;
  47. }
  48. preempt_enable();
  49. /*
  50. * Set up PKRU so that it denies access for everything
  51. * other than execution.
  52. */
  53. ret = arch_set_user_pkey_access(current, execute_only_pkey,
  54. PKEY_DISABLE_ACCESS);
  55. /*
  56. * If the PKRU-set operation failed somehow, just return
  57. * 0 and effectively disable execute-only support.
  58. */
  59. if (ret) {
  60. mm_set_pkey_free(mm, execute_only_pkey);
  61. return -1;
  62. }
  63. /* We got one, store it and use it from here on out */
  64. if (need_to_set_mm_pkey)
  65. mm->context.execute_only_pkey = execute_only_pkey;
  66. return execute_only_pkey;
  67. }
  68. static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
  69. {
  70. /* Do this check first since the vm_flags should be hot */
  71. if ((vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) != VM_EXEC)
  72. return false;
  73. if (vma_pkey(vma) != vma->vm_mm->context.execute_only_pkey)
  74. return false;
  75. return true;
  76. }
  77. /*
  78. * This is only called for *plain* mprotect calls.
  79. */
  80. int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, int pkey)
  81. {
  82. /*
  83. * Is this an mprotect_pkey() call? If so, never
  84. * override the value that came from the user.
  85. */
  86. if (pkey != -1)
  87. return pkey;
  88. /*
  89. * The mapping is execute-only. Go try to get the
  90. * execute-only protection key. If we fail to do that,
  91. * fall through as if we do not have execute-only
  92. * support in this mm.
  93. */
  94. if (prot == PROT_EXEC) {
  95. pkey = execute_only_pkey(vma->vm_mm);
  96. if (pkey > 0)
  97. return pkey;
  98. } else if (vma_is_pkey_exec_only(vma)) {
  99. /*
  100. * Protections are *not* PROT_EXEC, but the mapping
  101. * is using the exec-only pkey. This mapping was
  102. * PROT_EXEC and will no longer be. Move back to
  103. * the default pkey.
  104. */
  105. return ARCH_DEFAULT_PKEY;
  106. }
  107. /*
  108. * This is a vanilla, non-pkey mprotect (or we failed to
  109. * setup execute-only), inherit the pkey from the VMA we
  110. * are working on.
  111. */
  112. return vma_pkey(vma);
  113. }
  114. #define PKRU_AD_KEY(pkey) (PKRU_AD_BIT << ((pkey) * PKRU_BITS_PER_PKEY))
  115. /*
  116. * Make the default PKRU value (at execve() time) as restrictive
  117. * as possible. This ensures that any threads clone()'d early
  118. * in the process's lifetime will not accidentally get access
  119. * to data which is pkey-protected later on.
  120. */
  121. u32 init_pkru_value = PKRU_AD_KEY( 1) | PKRU_AD_KEY( 2) | PKRU_AD_KEY( 3) |
  122. PKRU_AD_KEY( 4) | PKRU_AD_KEY( 5) | PKRU_AD_KEY( 6) |
  123. PKRU_AD_KEY( 7) | PKRU_AD_KEY( 8) | PKRU_AD_KEY( 9) |
  124. PKRU_AD_KEY(10) | PKRU_AD_KEY(11) | PKRU_AD_KEY(12) |
  125. PKRU_AD_KEY(13) | PKRU_AD_KEY(14) | PKRU_AD_KEY(15);
  126. /*
  127. * Called from the FPU code when creating a fresh set of FPU
  128. * registers. This is called from a very specific context where
  129. * we know the FPU regstiers are safe for use and we can use PKRU
  130. * directly.
  131. */
  132. void copy_init_pkru_to_fpregs(void)
  133. {
  134. u32 init_pkru_value_snapshot = READ_ONCE(init_pkru_value);
  135. /*
  136. * Any write to PKRU takes it out of the XSAVE 'init
  137. * state' which increases context switch cost. Avoid
  138. * writing 0 when PKRU was already 0.
  139. */
  140. if (!init_pkru_value_snapshot && !read_pkru())
  141. return;
  142. /*
  143. * Override the PKRU state that came from 'init_fpstate'
  144. * with the baseline from the process.
  145. */
  146. write_pkru(init_pkru_value_snapshot);
  147. }
  148. static ssize_t init_pkru_read_file(struct file *file, char __user *user_buf,
  149. size_t count, loff_t *ppos)
  150. {
  151. char buf[32];
  152. unsigned int len;
  153. len = sprintf(buf, "0x%x\n", init_pkru_value);
  154. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  155. }
  156. static ssize_t init_pkru_write_file(struct file *file,
  157. const char __user *user_buf, size_t count, loff_t *ppos)
  158. {
  159. char buf[32];
  160. ssize_t len;
  161. u32 new_init_pkru;
  162. len = min(count, sizeof(buf) - 1);
  163. if (copy_from_user(buf, user_buf, len))
  164. return -EFAULT;
  165. /* Make the buffer a valid string that we can not overrun */
  166. buf[len] = '\0';
  167. if (kstrtouint(buf, 0, &new_init_pkru))
  168. return -EINVAL;
  169. /*
  170. * Don't allow insane settings that will blow the system
  171. * up immediately if someone attempts to disable access
  172. * or writes to pkey 0.
  173. */
  174. if (new_init_pkru & (PKRU_AD_BIT|PKRU_WD_BIT))
  175. return -EINVAL;
  176. WRITE_ONCE(init_pkru_value, new_init_pkru);
  177. return count;
  178. }
  179. static const struct file_operations fops_init_pkru = {
  180. .read = init_pkru_read_file,
  181. .write = init_pkru_write_file,
  182. .llseek = default_llseek,
  183. };
  184. static int __init create_init_pkru_value(void)
  185. {
  186. debugfs_create_file("init_pkru", S_IRUSR | S_IWUSR,
  187. arch_debugfs_dir, NULL, &fops_init_pkru);
  188. return 0;
  189. }
  190. late_initcall(create_init_pkru_value);
  191. static __init int setup_init_pkru(char *opt)
  192. {
  193. u32 new_init_pkru;
  194. if (kstrtouint(opt, 0, &new_init_pkru))
  195. return 1;
  196. WRITE_ONCE(init_pkru_value, new_init_pkru);
  197. return 1;
  198. }
  199. __setup("init_pkru=", setup_init_pkru);