xref: /openbmc/linux/arch/arm64/kernel/pointer_auth.c (revision 4a075bd4)
1 // SPDX-License-Identifier: GPL-2.0
2 
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 
10 int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg)
11 {
12 	struct ptrauth_keys *keys = &tsk->thread.keys_user;
13 	unsigned long addr_key_mask = PR_PAC_APIAKEY | PR_PAC_APIBKEY |
14 				      PR_PAC_APDAKEY | PR_PAC_APDBKEY;
15 	unsigned long key_mask = addr_key_mask | PR_PAC_APGAKEY;
16 
17 	if (!system_supports_address_auth() && !system_supports_generic_auth())
18 		return -EINVAL;
19 
20 	if (!arg) {
21 		ptrauth_keys_init(keys);
22 		ptrauth_keys_switch(keys);
23 		return 0;
24 	}
25 
26 	if (arg & ~key_mask)
27 		return -EINVAL;
28 
29 	if (((arg & addr_key_mask) && !system_supports_address_auth()) ||
30 	    ((arg & PR_PAC_APGAKEY) && !system_supports_generic_auth()))
31 		return -EINVAL;
32 
33 	if (arg & PR_PAC_APIAKEY)
34 		get_random_bytes(&keys->apia, sizeof(keys->apia));
35 	if (arg & PR_PAC_APIBKEY)
36 		get_random_bytes(&keys->apib, sizeof(keys->apib));
37 	if (arg & PR_PAC_APDAKEY)
38 		get_random_bytes(&keys->apda, sizeof(keys->apda));
39 	if (arg & PR_PAC_APDBKEY)
40 		get_random_bytes(&keys->apdb, sizeof(keys->apdb));
41 	if (arg & PR_PAC_APGAKEY)
42 		get_random_bytes(&keys->apga, sizeof(keys->apga));
43 
44 	ptrauth_keys_switch(keys);
45 
46 	return 0;
47 }
48