pointer_auth.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // SPDX-License-Identifier: GPL-2.0
  2. #include <linux/compat.h>
  3. #include <linux/errno.h>
  4. #include <linux/prctl.h>
  5. #include <linux/random.h>
  6. #include <linux/sched.h>
  7. #include <asm/cpufeature.h>
  8. #include <asm/pointer_auth.h>
  9. int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg)
  10. {
  11. struct ptrauth_keys_user *keys = &tsk->thread.keys_user;
  12. unsigned long addr_key_mask = PR_PAC_APIAKEY | PR_PAC_APIBKEY |
  13. PR_PAC_APDAKEY | PR_PAC_APDBKEY;
  14. unsigned long key_mask = addr_key_mask | PR_PAC_APGAKEY;
  15. if (!system_supports_address_auth() && !system_supports_generic_auth())
  16. return -EINVAL;
  17. if (is_compat_thread(task_thread_info(tsk)))
  18. return -EINVAL;
  19. if (!arg) {
  20. ptrauth_keys_init_user(keys);
  21. return 0;
  22. }
  23. if (arg & ~key_mask)
  24. return -EINVAL;
  25. if (((arg & addr_key_mask) && !system_supports_address_auth()) ||
  26. ((arg & PR_PAC_APGAKEY) && !system_supports_generic_auth()))
  27. return -EINVAL;
  28. if (arg & PR_PAC_APIAKEY)
  29. get_random_bytes(&keys->apia, sizeof(keys->apia));
  30. if (arg & PR_PAC_APIBKEY)
  31. get_random_bytes(&keys->apib, sizeof(keys->apib));
  32. if (arg & PR_PAC_APDAKEY)
  33. get_random_bytes(&keys->apda, sizeof(keys->apda));
  34. if (arg & PR_PAC_APDBKEY)
  35. get_random_bytes(&keys->apdb, sizeof(keys->apdb));
  36. if (arg & PR_PAC_APGAKEY)
  37. get_random_bytes(&keys->apga, sizeof(keys->apga));
  38. ptrauth_keys_install_user(keys);
  39. return 0;
  40. }
  41. static u64 arg_to_enxx_mask(unsigned long arg)
  42. {
  43. u64 sctlr_enxx_mask = 0;
  44. WARN_ON(arg & ~PR_PAC_ENABLED_KEYS_MASK);
  45. if (arg & PR_PAC_APIAKEY)
  46. sctlr_enxx_mask |= SCTLR_ELx_ENIA;
  47. if (arg & PR_PAC_APIBKEY)
  48. sctlr_enxx_mask |= SCTLR_ELx_ENIB;
  49. if (arg & PR_PAC_APDAKEY)
  50. sctlr_enxx_mask |= SCTLR_ELx_ENDA;
  51. if (arg & PR_PAC_APDBKEY)
  52. sctlr_enxx_mask |= SCTLR_ELx_ENDB;
  53. return sctlr_enxx_mask;
  54. }
  55. int ptrauth_set_enabled_keys(struct task_struct *tsk, unsigned long keys,
  56. unsigned long enabled)
  57. {
  58. u64 sctlr;
  59. if (!system_supports_address_auth())
  60. return -EINVAL;
  61. if (is_compat_thread(task_thread_info(tsk)))
  62. return -EINVAL;
  63. if ((keys & ~PR_PAC_ENABLED_KEYS_MASK) || (enabled & ~keys))
  64. return -EINVAL;
  65. preempt_disable();
  66. sctlr = tsk->thread.sctlr_user;
  67. sctlr &= ~arg_to_enxx_mask(keys);
  68. sctlr |= arg_to_enxx_mask(enabled);
  69. tsk->thread.sctlr_user = sctlr;
  70. if (tsk == current)
  71. update_sctlr_el1(sctlr);
  72. preempt_enable();
  73. return 0;
  74. }
  75. int ptrauth_get_enabled_keys(struct task_struct *tsk)
  76. {
  77. int retval = 0;
  78. if (!system_supports_address_auth())
  79. return -EINVAL;
  80. if (is_compat_thread(task_thread_info(tsk)))
  81. return -EINVAL;
  82. if (tsk->thread.sctlr_user & SCTLR_ELx_ENIA)
  83. retval |= PR_PAC_APIAKEY;
  84. if (tsk->thread.sctlr_user & SCTLR_ELx_ENIB)
  85. retval |= PR_PAC_APIBKEY;
  86. if (tsk->thread.sctlr_user & SCTLR_ELx_ENDA)
  87. retval |= PR_PAC_APDAKEY;
  88. if (tsk->thread.sctlr_user & SCTLR_ELx_ENDB)
  89. retval |= PR_PAC_APDBKEY;
  90. return retval;
  91. }