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