blob: adb955fd9bdd9812e1ab29ac2735d8ea2b836997 [file] [log] [blame]
David Brazdil0f672f62019-12-10 10:32:29 +00001// SPDX-License-Identifier: GPL-2.0
2
Olivier Deprez157378f2022-04-04 15:47:50 +02003#include <linux/compat.h>
David Brazdil0f672f62019-12-10 10:32:29 +00004#include <linux/errno.h>
5#include <linux/prctl.h>
6#include <linux/random.h>
7#include <linux/sched.h>
8#include <asm/cpufeature.h>
9#include <asm/pointer_auth.h>
10
11int ptrauth_prctl_reset_keys(struct task_struct *tsk, unsigned long arg)
12{
Olivier Deprez157378f2022-04-04 15:47:50 +020013 struct ptrauth_keys_user *keys = &tsk->thread.keys_user;
David Brazdil0f672f62019-12-10 10:32:29 +000014 unsigned long addr_key_mask = PR_PAC_APIAKEY | PR_PAC_APIBKEY |
15 PR_PAC_APDAKEY | PR_PAC_APDBKEY;
16 unsigned long key_mask = addr_key_mask | PR_PAC_APGAKEY;
17
18 if (!system_supports_address_auth() && !system_supports_generic_auth())
19 return -EINVAL;
20
Olivier Deprez157378f2022-04-04 15:47:50 +020021 if (is_compat_thread(task_thread_info(tsk)))
22 return -EINVAL;
23
David Brazdil0f672f62019-12-10 10:32:29 +000024 if (!arg) {
Olivier Deprez157378f2022-04-04 15:47:50 +020025 ptrauth_keys_init_user(keys);
David Brazdil0f672f62019-12-10 10:32:29 +000026 return 0;
27 }
28
29 if (arg & ~key_mask)
30 return -EINVAL;
31
32 if (((arg & addr_key_mask) && !system_supports_address_auth()) ||
33 ((arg & PR_PAC_APGAKEY) && !system_supports_generic_auth()))
34 return -EINVAL;
35
36 if (arg & PR_PAC_APIAKEY)
37 get_random_bytes(&keys->apia, sizeof(keys->apia));
38 if (arg & PR_PAC_APIBKEY)
39 get_random_bytes(&keys->apib, sizeof(keys->apib));
40 if (arg & PR_PAC_APDAKEY)
41 get_random_bytes(&keys->apda, sizeof(keys->apda));
42 if (arg & PR_PAC_APDBKEY)
43 get_random_bytes(&keys->apdb, sizeof(keys->apdb));
44 if (arg & PR_PAC_APGAKEY)
45 get_random_bytes(&keys->apga, sizeof(keys->apga));
46
David Brazdil0f672f62019-12-10 10:32:29 +000047 return 0;
48}