pkeys.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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/mm_types.h> /* mm_struct, vma, etc... */
  15. #include <linux/pkeys.h> /* PKEY_* */
  16. #include <uapi/asm-generic/mman-common.h>
  17. #include <asm/cpufeature.h> /* boot_cpu_has, ... */
  18. #include <asm/mmu_context.h> /* vma_pkey() */
  19. #include <asm/fpu/internal.h> /* fpregs_active() */
  20. int __execute_only_pkey(struct mm_struct *mm)
  21. {
  22. int ret;
  23. /*
  24. * We do not want to go through the relatively costly
  25. * dance to set PKRU if we do not need to. Check it
  26. * first and assume that if the execute-only pkey is
  27. * write-disabled that we do not have to set it
  28. * ourselves. We need preempt off so that nobody
  29. * can make fpregs inactive.
  30. */
  31. preempt_disable();
  32. if (fpregs_active() &&
  33. !__pkru_allows_read(read_pkru(), PKEY_DEDICATED_EXECUTE_ONLY)) {
  34. preempt_enable();
  35. return PKEY_DEDICATED_EXECUTE_ONLY;
  36. }
  37. preempt_enable();
  38. ret = arch_set_user_pkey_access(current, PKEY_DEDICATED_EXECUTE_ONLY,
  39. PKEY_DISABLE_ACCESS);
  40. /*
  41. * If the PKRU-set operation failed somehow, just return
  42. * 0 and effectively disable execute-only support.
  43. */
  44. if (ret)
  45. return 0;
  46. return PKEY_DEDICATED_EXECUTE_ONLY;
  47. }
  48. static inline bool vma_is_pkey_exec_only(struct vm_area_struct *vma)
  49. {
  50. /* Do this check first since the vm_flags should be hot */
  51. if ((vma->vm_flags & (VM_READ | VM_WRITE | VM_EXEC)) != VM_EXEC)
  52. return false;
  53. if (vma_pkey(vma) != PKEY_DEDICATED_EXECUTE_ONLY)
  54. return false;
  55. return true;
  56. }
  57. /*
  58. * This is only called for *plain* mprotect calls.
  59. */
  60. int __arch_override_mprotect_pkey(struct vm_area_struct *vma, int prot, int pkey)
  61. {
  62. /*
  63. * Is this an mprotect_pkey() call? If so, never
  64. * override the value that came from the user.
  65. */
  66. if (pkey != -1)
  67. return pkey;
  68. /*
  69. * Look for a protection-key-drive execute-only mapping
  70. * which is now being given permissions that are not
  71. * execute-only. Move it back to the default pkey.
  72. */
  73. if (vma_is_pkey_exec_only(vma) &&
  74. (prot & (PROT_READ|PROT_WRITE))) {
  75. return 0;
  76. }
  77. /*
  78. * The mapping is execute-only. Go try to get the
  79. * execute-only protection key. If we fail to do that,
  80. * fall through as if we do not have execute-only
  81. * support.
  82. */
  83. if (prot == PROT_EXEC) {
  84. pkey = execute_only_pkey(vma->vm_mm);
  85. if (pkey > 0)
  86. return pkey;
  87. }
  88. /*
  89. * This is a vanilla, non-pkey mprotect (or we failed to
  90. * setup execute-only), inherit the pkey from the VMA we
  91. * are working on.
  92. */
  93. return vma_pkey(vma);
  94. }