pointer_auth.h 4.5 KB

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