[PATCH, RFC 37/62] keys/mktme: Do not allow key creation in unsafe topologies

From: Kirill A. Shutemov
Date: Wed May 08 2019 - 10:50:05 EST


From: Alison Schofield <alison.schofield@xxxxxxxxx>

MKTME feature 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,
or an unprogrammable memory controller may be removed from the
platform.

Signed-off-by: Alison Schofield <alison.schofield@xxxxxxxxx>
Signed-off-by: Kirill A. Shutemov <kirill.shutemov@xxxxxxxxxxxxxxx>
---
security/keys/mktme_keys.c | 39 ++++++++++++++++++++++++++++++--------
1 file changed, 31 insertions(+), 8 deletions(-)

diff --git a/security/keys/mktme_keys.c b/security/keys/mktme_keys.c
index f5fc6cccc81b..734e1d28eb24 100644
--- a/security/keys/mktme_keys.c
+++ b/security/keys/mktme_keys.c
@@ -26,6 +26,7 @@ cpumask_var_t mktme_leadcpus; /* One lead CPU per pconfig target */
static bool mktme_storekeys; /* True if key payloads may be stored */
unsigned long *mktme_bitmap_user_type; /* Shows presence of user type keys */
struct mktme_payload *mktme_key_store; /* Payload storage if allowed */
+bool mktme_allow_keys; /* True when topology supports keys */

/* 1:1 Mapping between Userspace Keys (struct key) and Hardware KeyIDs */
struct mktme_mapping {
@@ -278,33 +279,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)
{
struct mktme_payload *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)) {
- mktme_store_payload(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.20.1