pointer_auth.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /* SPDX-License-Identifier: GPL-2.0 */
  2. #ifndef __ASM_POINTER_AUTH_H
  3. #define __ASM_POINTER_AUTH_H
  4. #include <linux/bitops.h>
  5. #include <linux/prctl.h>
  6. #include <linux/random.h>
  7. #include <asm/cpufeature.h>
  8. #include <asm/memory.h>
  9. #include <asm/sysreg.h>
  10. /*
  11. * The EL0/EL1 pointer bits used by a pointer authentication code.
  12. * This is dependent on TBI0/TBI1 being enabled, or bits 63:56 would also apply.
  13. */
  14. #define ptrauth_user_pac_mask() GENMASK_ULL(54, vabits_actual)
  15. #define ptrauth_kernel_pac_mask() GENMASK_ULL(63, vabits_actual)
  16. #define PR_PAC_ENABLED_KEYS_MASK \
  17. (PR_PAC_APIAKEY | PR_PAC_APIBKEY | PR_PAC_APDAKEY | PR_PAC_APDBKEY)
  18. #ifdef CONFIG_ARM64_PTR_AUTH
  19. /*
  20. * Each key is a 128-bit quantity which is split across a pair of 64-bit
  21. * registers (Lo and Hi).
  22. */
  23. struct ptrauth_key {
  24. unsigned long lo, hi;
  25. };
  26. /*
  27. * We give each process its own keys, which are shared by all threads. The keys
  28. * are inherited upon fork(), and reinitialised upon exec*().
  29. */
  30. struct ptrauth_keys_user {
  31. struct ptrauth_key apia;
  32. struct ptrauth_key apib;
  33. struct ptrauth_key apda;
  34. struct ptrauth_key apdb;
  35. struct ptrauth_key apga;
  36. };
  37. #define __ptrauth_key_install_nosync(k, v) \
  38. do { \
  39. struct ptrauth_key __pki_v = (v); \
  40. write_sysreg_s(__pki_v.lo, SYS_ ## k ## KEYLO_EL1); \
  41. write_sysreg_s(__pki_v.hi, SYS_ ## k ## KEYHI_EL1); \
  42. } while (0)
  43. #ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
  44. struct ptrauth_keys_kernel {
  45. struct ptrauth_key apia;
  46. };
  47. static __always_inline void ptrauth_keys_init_kernel(struct ptrauth_keys_kernel *keys)
  48. {
  49. if (system_supports_address_auth())
  50. get_random_bytes(&keys->apia, sizeof(keys->apia));
  51. }
  52. static __always_inline void ptrauth_keys_switch_kernel(struct ptrauth_keys_kernel *keys)
  53. {
  54. if (!system_supports_address_auth())
  55. return;
  56. __ptrauth_key_install_nosync(APIA, keys->apia);
  57. isb();
  58. }
  59. #endif /* CONFIG_ARM64_PTR_AUTH_KERNEL */
  60. static inline void ptrauth_keys_install_user(struct ptrauth_keys_user *keys)
  61. {
  62. if (system_supports_address_auth()) {
  63. __ptrauth_key_install_nosync(APIB, keys->apib);
  64. __ptrauth_key_install_nosync(APDA, keys->apda);
  65. __ptrauth_key_install_nosync(APDB, keys->apdb);
  66. }
  67. if (system_supports_generic_auth())
  68. __ptrauth_key_install_nosync(APGA, keys->apga);
  69. }
  70. static inline void ptrauth_keys_init_user(struct ptrauth_keys_user *keys)
  71. {
  72. if (system_supports_address_auth()) {
  73. get_random_bytes(&keys->apia, sizeof(keys->apia));
  74. get_random_bytes(&keys->apib, sizeof(keys->apib));
  75. get_random_bytes(&keys->apda, sizeof(keys->apda));
  76. get_random_bytes(&keys->apdb, sizeof(keys->apdb));
  77. }
  78. if (system_supports_generic_auth())
  79. get_random_bytes(&keys->apga, sizeof(keys->apga));
  80. ptrauth_keys_install_user(keys);
  81. }
  82. extern int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg);
  83. extern int ptrauth_set_enabled_keys(struct task_struct *tsk, unsigned long keys,
  84. unsigned long enabled);
  85. extern int ptrauth_get_enabled_keys(struct task_struct *tsk);
  86. static __always_inline void ptrauth_enable(void)
  87. {
  88. if (!system_supports_address_auth())
  89. return;
  90. sysreg_clear_set(sctlr_el1, 0, (SCTLR_ELx_ENIA | SCTLR_ELx_ENIB |
  91. SCTLR_ELx_ENDA | SCTLR_ELx_ENDB));
  92. isb();
  93. }
  94. #define ptrauth_suspend_exit() \
  95. ptrauth_keys_install_user(&current->thread.keys_user)
  96. #define ptrauth_thread_init_user() \
  97. do { \
  98. ptrauth_keys_init_user(&current->thread.keys_user); \
  99. \
  100. /* enable all keys */ \
  101. if (system_supports_address_auth()) \
  102. ptrauth_set_enabled_keys(current, \
  103. PR_PAC_ENABLED_KEYS_MASK, \
  104. PR_PAC_ENABLED_KEYS_MASK); \
  105. } while (0)
  106. #define ptrauth_thread_switch_user(tsk) \
  107. ptrauth_keys_install_user(&(tsk)->thread.keys_user)
  108. #else /* CONFIG_ARM64_PTR_AUTH */
  109. #define ptrauth_enable()
  110. #define ptrauth_prctl_reset_keys(tsk, arg) (-EINVAL)
  111. #define ptrauth_set_enabled_keys(tsk, keys, enabled) (-EINVAL)
  112. #define ptrauth_get_enabled_keys(tsk) (-EINVAL)
  113. #define ptrauth_suspend_exit()
  114. #define ptrauth_thread_init_user()
  115. #define ptrauth_thread_switch_user(tsk)
  116. #endif /* CONFIG_ARM64_PTR_AUTH */
  117. #ifdef CONFIG_ARM64_PTR_AUTH_KERNEL
  118. #define ptrauth_thread_init_kernel(tsk) \
  119. ptrauth_keys_init_kernel(&(tsk)->thread.keys_kernel)
  120. #define ptrauth_thread_switch_kernel(tsk) \
  121. ptrauth_keys_switch_kernel(&(tsk)->thread.keys_kernel)
  122. #else
  123. #define ptrauth_thread_init_kernel(tsk)
  124. #define ptrauth_thread_switch_kernel(tsk)
  125. #endif /* CONFIG_ARM64_PTR_AUTH_KERNEL */
  126. #endif /* __ASM_POINTER_AUTH_H */