[PATCHv2 38/59] keys/mktme: Do not allow key creation in unsafe topologies

Kirill A. Shutemov
2019-07-31T15:19:23Z

From: Alison Schofield <alison.schofield@intel.com>

MKTME depends upon at least one online CPU capable of programming
each memory controller in the platform.

An unsafe topology for MKTME is a memory only package or a package
with no online CPUs. Key creation with unsafe topologies will fail
with EINVAL and a warning will be logged one time.
For example:
[ ] MKTME: no online CPU in proximity domain
[ ] MKTME: topology does not support key creation

These are recoverable errors. CPUs may be brought online that are
capable of programming a previously unprogrammable memory controller.

Signed-off-by: Alison Schofield <alison.schofield@intel.com>
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@linux.intel.com>
---
security/keys/mktme_keys.c | 36 ++++++++++++++++++++++++++++++------
1 file changed, 30 insertions(+), 6 deletions(-)

diff --git a/security/keys/mktme_keys.c b/security/keys/mktme_keys.c
index 6265b62801e9..70662e882674 100644
--- a/security/keys/mktme_keys.c
+++ b/security/keys/mktme_keys.c
@@ -23,6 +23,7 @@ static unsigned int mktme_available_keyids; /* Free Hardware KeyIDs */
static struct kmem_cache *mktme_prog_cache; /* Hardware programming cache */
static unsigned long *mktme_target_map; /* PCONFIG programming target */
static cpumask_var_t mktme_leadcpus; /* One CPU per PCONFIG target */
+static bool mktme_allow_keys; /* HW topology supports keys */

enum mktme_keyid_state {
KEYID_AVAILABLE, /* Available to be assigned */
@@ -253,32 +254,55 @@ static void mktme_destroy_key(struct key *key)
percpu_ref_kill(&encrypt_count[keyid]);
}

+static void mktme_update_pconfig_targets(void);
/* Key Service Method to create a new key. Payload is preparsed. */
int mktme_instantiate_key(struct key *key, struct key_preparsed_payload *prep)
{
u32 *payload = prep->payload.data[0];
unsigned long flags;
+ int ret = -ENOKEY;
int keyid;

spin_lock_irqsave(&mktme_lock, flags);
+
+ /* Topology supports key creation */
+ if (mktme_allow_keys)
+ goto get_key;
+
+ /* Topology unknown, check it. */
+ if (!mktme_hmat_evaluate()) {
+ ret = -EINVAL;
+ goto out_unlock;
+ }
+
+ /* Keys are now allowed. Update the programming targets. */
+ mktme_update_pconfig_targets();
+ mktme_allow_keys = true;
+
+get_key:
keyid = mktme_reserve_keyid(key);
spin_unlock_irqrestore(&mktme_lock, flags);
if (!keyid)
- return -ENOKEY;
+ goto out;

if (percpu_ref_init(&encrypt_count[keyid], mktme_percpu_ref_release,
0, GFP_KERNEL))
- goto err_out;
+ goto out_free_key;

- if (!mktme_program_keyid(keyid, *payload))
- return MKTME_PROG_SUCCESS;
+ ret = mktme_program_keyid(keyid, *payload);
+ if (ret == MKTME_PROG_SUCCESS)
+ goto out;

+ /* Key programming failed */
percpu_ref_exit(&encrypt_count[keyid]);
-err_out:
+
+out_free_key:
spin_lock_irqsave(&mktme_lock, flags);
mktme_release_keyid(keyid);
+out_unlock:
spin_unlock_irqrestore(&mktme_lock, flags);
- return -ENOKEY;
+out:
+ return ret;
}

/* Make sure arguments are correct for the TYPE of key requested */
--
2.21.0