pkeys.c 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * Intel Memory Protection Keys management
  4. * Copyright (c) 2015, Intel Corporation.
  5. */
  6. #include <linux/debugfs.h> /* debugfs_create_u32() */
  7. #include <linux/mm_types.h> /* mm_struct, vma, etc... */
  8. #include <linux/pkeys.h> /* PKEY_* */
  9. #include <uapi/asm-generic/mman-common.h>
  10. #include <asm/cpufeature.h> /* boot_cpu_has, ... */
  11. #include <asm/mmu_context.h> /* vma_pkey() */
  12. int __execute_only_pkey(struct mm_struct *mm)
  13. {
  14. bool need_to_set_mm_pkey = false;
  15. int execute_only_pkey = mm->context.execute_only_pkey;
  16. int ret;
  17. /* Do we need to assign a pkey for mm's execute-only maps? */
  18. if (execute_only_pkey == -1) {
  19. /* Go allocate one to use, which might fail */
  20. execute_only_pkey = mm_pkey_alloc(mm);
  21. if (execute_only_pkey < 0)
  22. return -1;
  23. need_to_set_mm_pkey = true;
  24. }
  25. /*
  26. * We do not want to go through the relatively costly
  27. * dance to set PKRU if we do not need to. Check it
  28. * first and assume that if the execute-only pkey is
  29. * write-disabled that we do not have to set it
  30. * ourselves.
  31. */
  32. if (!need_to_set_mm_pkey &&
  33. !__pkru_allows_read(read_pkru(), execute_only_pkey)) {
  34. return execute_only_pkey;
  35. }
  36. /*
  37. * Set up PKRU so that it denies access for everything
  38. * other than execution.
  39. */
  40. ret = arch_set_user_pkey_access(current, execute_only_pkey,
  41. PKEY_DISABLE_ACCESS);
  42. /*
  43. * If the PKRU-set operation failed somehow, just return
  44. * 0 and effectively disable execute-only support.
  45. */
  46. if (ret) {
  47. mm_set_pkey_free(mm, execute_only_pkey);
  48. return -1;
  49. }
  50. /* We got one, store it and use it from here on out */
  51. if (need_to_set_mm_pkey)
  52. mm->context.execute_only_pkey = execute_only_pkey;
  53. return execute_only_pkey;
  54. }
  55. static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
  56. {
  57. /* Do this check first since the vm_flags should be hot */
  58. if ((vma->vm_flags & VM_ACCESS_FLAGS) != VM_EXEC)
  59. return false;
  60. if (vma_pkey(vma) != vma->vm_mm->context.execute_only_pkey)
  61. return false;
  62. return true;
  63. }
  64. /*
  65. * This is only called for *plain* mprotect calls.
  66. */
  67. int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, int pkey)
  68. {
  69. /*
  70. * Is this an mprotect_pkey() call? If so, never
  71. * override the value that came from the user.
  72. */
  73. if (pkey != -1)
  74. return pkey;
  75. /*
  76. * The mapping is execute-only. Go try to get the
  77. * execute-only protection key. If we fail to do that,
  78. * fall through as if we do not have execute-only
  79. * support in this mm.
  80. */
  81. if (prot == PROT_EXEC) {
  82. pkey = execute_only_pkey(vma->vm_mm);
  83. if (pkey > 0)
  84. return pkey;
  85. } else if (vma_is_pkey_exec_only(vma)) {
  86. /*
  87. * Protections are *not* PROT_EXEC, but the mapping
  88. * is using the exec-only pkey. This mapping was
  89. * PROT_EXEC and will no longer be. Move back to
  90. * the default pkey.
  91. */
  92. return ARCH_DEFAULT_PKEY;
  93. }
  94. /*
  95. * This is a vanilla, non-pkey mprotect (or we failed to
  96. * setup execute-only), inherit the pkey from the VMA we
  97. * are working on.
  98. */
  99. return vma_pkey(vma);
  100. }
  101. #define PKRU_AD_MASK(pkey) (PKRU_AD_BIT << ((pkey) * PKRU_BITS_PER_PKEY))
  102. /*
  103. * Make the default PKRU value (at execve() time) as restrictive
  104. * as possible. This ensures that any threads clone()'d early
  105. * in the process's lifetime will not accidentally get access
  106. * to data which is pkey-protected later on.
  107. */
  108. u32 init_pkru_value = PKRU_AD_MASK( 1) | PKRU_AD_MASK( 2) |
  109. PKRU_AD_MASK( 3) | PKRU_AD_MASK( 4) |
  110. PKRU_AD_MASK( 5) | PKRU_AD_MASK( 6) |
  111. PKRU_AD_MASK( 7) | PKRU_AD_MASK( 8) |
  112. PKRU_AD_MASK( 9) | PKRU_AD_MASK(10) |
  113. PKRU_AD_MASK(11) | PKRU_AD_MASK(12) |
  114. PKRU_AD_MASK(13) | PKRU_AD_MASK(14) |
  115. PKRU_AD_MASK(15);
  116. static ssize_t init_pkru_read_file(struct file *file, char __user *user_buf,
  117. size_t count, loff_t *ppos)
  118. {
  119. char buf[32];
  120. unsigned int len;
  121. len = sprintf(buf, "0x%x\n", init_pkru_value);
  122. return simple_read_from_buffer(user_buf, count, ppos, buf, len);
  123. }
  124. static ssize_t init_pkru_write_file(struct file *file,
  125. const char __user *user_buf, size_t count, loff_t *ppos)
  126. {
  127. char buf[32];
  128. ssize_t len;
  129. u32 new_init_pkru;
  130. len = min(count, sizeof(buf) - 1);
  131. if (copy_from_user(buf, user_buf, len))
  132. return -EFAULT;
  133. /* Make the buffer a valid string that we can not overrun */
  134. buf[len] = '\0';
  135. if (kstrtouint(buf, 0, &new_init_pkru))
  136. return -EINVAL;
  137. /*
  138. * Don't allow insane settings that will blow the system
  139. * up immediately if someone attempts to disable access
  140. * or writes to pkey 0.
  141. */
  142. if (new_init_pkru & (PKRU_AD_BIT|PKRU_WD_BIT))
  143. return -EINVAL;
  144. WRITE_ONCE(init_pkru_value, new_init_pkru);
  145. return count;
  146. }
  147. static const struct file_operations fops_init_pkru = {
  148. .read = init_pkru_read_file,
  149. .write = init_pkru_write_file,
  150. .llseek = default_llseek,
  151. };
  152. static int __init create_init_pkru_value(void)
  153. {
  154. /* Do not expose the file if pkeys are not supported. */
  155. if (!cpu_feature_enabled(X86_FEATURE_OSPKE))
  156. return 0;
  157. debugfs_create_file("init_pkru", S_IRUSR | S_IWUSR,
  158. arch_debugfs_dir, NULL, &fops_init_pkru);
  159. return 0;
  160. }
  161. late_initcall(create_init_pkru_value);
  162. static __init int setup_init_pkru(char *opt)
  163. {
  164. u32 new_init_pkru;
  165. if (kstrtouint(opt, 0, &new_init_pkru))
  166. return 1;
  167. WRITE_ONCE(init_pkru_value, new_init_pkru);
  168. return 1;
  169. }
  170. __setup("init_pkru=", setup_init_pkru);