Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6106895ybi; Wed, 31 Jul 2019 08:27:10 -0700 (PDT) X-Google-Smtp-Source: APXvYqxQ8WW65sbNHD6xSDkDdLd/c0l+nAKjkq6kar/FHXpq5K2xQuMlrx9m9xr0a/U2bXJaLLbX X-Received: by 2002:a17:902:2a27:: with SMTP id i36mr118926994plb.161.1564586830607; Wed, 31 Jul 2019 08:27:10 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564586830; cv=none; d=google.com; s=arc-20160816; b=E22+bMlBdRhPGbNkM0ozvN6Nng8ELzEZLk88Qtf+IJAo7gFdu86hY0KC2Qrb77Do+S berWZFlsQwjLMYfILHUrG0hck3h82e73TmgE52MRp5J3rb5FfKjmAEhrzJMUP24wkRwt LvXB9Ppj9u1rKzn8FJOG9oRoLpDc35Dgb93MslVosbiOyjfah4DMDj2u/9caHOTaqSzJ 3TwcRT5yonkv3NgDf5KBKt9pNH4LDMf47XVhaxhAiIahcQixdV6CGxW7Tba9mm8tiLn9 o/MPD7WQThz5jsYqNmbdAk4ymEXF5dxT5sqlVnwAsNpfLc0a0TJ8BAG59JgOsKNX+Ly4 rc6w== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=PzedmcWsRc3DhudY/wiWcVyq0AAagaASN0DGAs1EUfk=; b=Szg7hfbePdnPZHXGh9AhOeE8b0z4yYzmzprChseUVvrgYWghosHg58cbsKjz/0VhUT 73wLNHcDgNZ0ZQzFWGYhLZcmYvaaRFXBwxR5oTX5HqWtdnDr4ypU6fx//Aj5HqOUFz0Z tSpzL1Jp71wwWWXY9tDV6WuksbQElCp72FATtm1tehzSHaqswEBtX2H6YPAyTS/eThvH 35TBbwYJP8BIQueekzW5I/rh25ZmdEL7QsQuI4ejbfW6sZicTKO+UwdJzrPNYGAlXokz Uw5CxS0Ijk4pBLyYmNb1qvFSheMKRfWWWBbYIKV4VJIMSHfRhKUZsJci07H6+VNjiT9h x/1w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=oLYDLyPe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id z17si33040329pgl.560.2019.07.31.08.26.55; Wed, 31 Jul 2019 08:27:10 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=oLYDLyPe; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728808AbfGaPS7 (ORCPT + 99 others); Wed, 31 Jul 2019 11:18:59 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:37152 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728916AbfGaPSz (ORCPT ); Wed, 31 Jul 2019 11:18:55 -0400 Received: by mail-ed1-f67.google.com with SMTP id w13so66092487eds.4 for ; Wed, 31 Jul 2019 08:18:53 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=shutemov-name.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:date:message-id:in-reply-to:references :mime-version:content-transfer-encoding; bh=PzedmcWsRc3DhudY/wiWcVyq0AAagaASN0DGAs1EUfk=; b=oLYDLyPe+JzY2m1J7wjX7AgBe2E4rUQv0fXNMOTxiG0nu3Kabo9U15Ufg7K6WUQgmi RNNtxu0DlWJ+voFj2zV59ecQN7UXfIGdJz6l2QQg7hl1DF1htbBcFoFM/zAI2mPjcI1U l3fEHvv0/1gka708wHD2qXwLZrc051+nm/YjhW0tIhCa72L/Tien3Zer+cqAIsJMVqNK L0WPnYRQdIixCyrAGbQRcVHhSuhtXWLR41Rx6IQOFrIz+x0hmONqrQ/gZtqcXxab8960 yeuaFiFWFODRPxwK1POFLi1ZUqFHAQQefGlgCb9xVI6pv/BL6bFoNByecBEofA/OdSSY 7nrg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id:in-reply-to :references:mime-version:content-transfer-encoding; bh=PzedmcWsRc3DhudY/wiWcVyq0AAagaASN0DGAs1EUfk=; b=p0CXCasruWizUdMv+j6eMqDBdIWcRG+VfumnMHMiXAO/eWjr50m6UZZN7IjL5TS4Bh zYegihgeCrFPmfYITjzBkz49Mxir0s4aF8kxnXFL25aaU5t7fYL9wrYv2q2XSo8gOaWH rzmv2TI2W6gaJ5RLBBE3dCTw2MIZ/oo0TEKZh6Q3HaAMK+Q3Bsq4+YhHDbo706v+9J2g 4hsexTz7GEGxH3R5oPRyg7tSMnEWrOsBPqtAP9b7mj3NQzTGXWHTo5W/25ON89EgNyHU 7F8EJXWkherIRWzZ2coMn6kIUghExYs2Ayrtp1NL2g04cvQwbrJSTy/ASuu1KuPtRSzy u+fg== X-Gm-Message-State: APjAAAVspRjG2TV5G1Zeo6692ZCRWD6RhmMNUeIcDIUtnD6CH0wN9xC5 sK9ZaqUy/LBhMQ9YnrkHkMw= X-Received: by 2002:a17:906:c2c9:: with SMTP id ch9mr2839424ejb.167.1564586028666; Wed, 31 Jul 2019 08:13:48 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id a18sm9661518ejp.2.2019.07.31.08.13.47 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 08:13:47 -0700 (PDT) From: "Kirill A. Shutemov" X-Google-Original-From: "Kirill A. Shutemov" Received: by box.localdomain (Postfix, from userid 1000) id BF3F9103FDC; Wed, 31 Jul 2019 18:08:16 +0300 (+03) To: Andrew Morton , x86@kernel.org, Thomas Gleixner , Ingo Molnar , "H. Peter Anvin" , Borislav Petkov , Peter Zijlstra , Andy Lutomirski , David Howells Cc: Kees Cook , Dave Hansen , Kai Huang , Jacob Pan , Alison Schofield , linux-mm@kvack.org, kvm@vger.kernel.org, keyrings@vger.kernel.org, linux-kernel@vger.kernel.org, "Kirill A . Shutemov" Subject: [PATCHv2 30/59] keys/mktme: Program MKTME keys into the platform hardware Date: Wed, 31 Jul 2019 18:07:44 +0300 Message-Id: <20190731150813.26289-31-kirill.shutemov@linux.intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> References: <20190731150813.26289-1-kirill.shutemov@linux.intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Alison Schofield Finally, the keys are programmed into the hardware via each lead CPU. Every package has to be programmed successfully. There is no partial success allowed here. Here a retry scheme is included for two errors that may succeed on retry: MKTME_DEVICE_BUSY and MKTME_ENTROPY_ERROR. However, it's not clear if even those errors should be retried at this level. Perhaps they too, should be returned to user space for handling. Signed-off-by: Alison Schofield Signed-off-by: Kirill A. Shutemov --- security/keys/mktme_keys.c | 92 +++++++++++++++++++++++++++++++++++++- 1 file changed, 91 insertions(+), 1 deletion(-) diff --git a/security/keys/mktme_keys.c b/security/keys/mktme_keys.c index 272bff8591b7..3c641f3ee794 100644 --- a/security/keys/mktme_keys.c +++ b/security/keys/mktme_keys.c @@ -83,6 +83,96 @@ static const match_table_t mktme_token = { {OPT_ERROR, NULL} }; +struct mktme_hw_program_info { + struct mktme_key_program *key_program; + int *status; +}; + +struct mktme_err_table { + const char *msg; + bool retry; +}; + +static const struct mktme_err_table mktme_error[] = { +/* MKTME_PROG_SUCCESS */ {"KeyID was successfully programmed", false}, +/* MKTME_INVALID_PROG_CMD */ {"Invalid KeyID programming command", false}, +/* MKTME_ENTROPY_ERROR */ {"Insufficient entropy", true}, +/* MKTME_INVALID_KEYID */ {"KeyID not valid", false}, +/* MKTME_INVALID_ENC_ALG */ {"Invalid encryption algorithm chosen", false}, +/* MKTME_DEVICE_BUSY */ {"Failure to access key table", true}, +}; + +static int mktme_parse_program_status(int status[]) +{ + int cpu, sum = 0; + + /* Success: all CPU(s) programmed all key table(s) */ + for_each_cpu(cpu, mktme_leadcpus) + sum += status[cpu]; + if (!sum) + return MKTME_PROG_SUCCESS; + + /* Invalid Parameters: log the error and return the error. */ + for_each_cpu(cpu, mktme_leadcpus) { + switch (status[cpu]) { + case MKTME_INVALID_KEYID: + case MKTME_INVALID_PROG_CMD: + case MKTME_INVALID_ENC_ALG: + pr_err("mktme: %s\n", mktme_error[status[cpu]].msg); + return status[cpu]; + + default: + break; + } + } + /* + * Device Busy or Insufficient Entropy: do not log the + * error. These will be retried and if retries (time or + * count runs out) caller will log the error. + */ + for_each_cpu(cpu, mktme_leadcpus) { + if (status[cpu] == MKTME_DEVICE_BUSY) + return status[cpu]; + } + return MKTME_ENTROPY_ERROR; +} + +/* Program a single key using one CPU. */ +static void mktme_do_program(void *hw_program_info) +{ + struct mktme_hw_program_info *info = hw_program_info; + int cpu; + + cpu = smp_processor_id(); + info->status[cpu] = mktme_key_program(info->key_program); +} + +static int mktme_program_all_keytables(struct mktme_key_program *key_program) +{ + struct mktme_hw_program_info info; + int err, retries = 10; /* Maybe users should handle retries */ + + info.key_program = key_program; + info.status = kcalloc(num_possible_cpus(), sizeof(info.status[0]), + GFP_KERNEL); + + while (retries--) { + get_online_cpus(); + on_each_cpu_mask(mktme_leadcpus, mktme_do_program, + &info, 1); + put_online_cpus(); + + err = mktme_parse_program_status(info.status); + if (!err) /* Success */ + return err; + else if (!mktme_error[err].retry) /* Error no retry */ + return -ENOKEY; + } + /* Ran out of retries */ + pr_err("mktme: %s\n", mktme_error[err].msg); + return err; +} + /* Copy the payload to the HW programming structure and program this KeyID */ static int mktme_program_keyid(int keyid, u32 payload) { @@ -97,7 +187,7 @@ static int mktme_program_keyid(int keyid, u32 payload) kprog->keyid = keyid; kprog->keyid_ctrl = payload; - ret = MKTME_PROG_SUCCESS; /* Future programming call */ + ret = mktme_program_all_keytables(kprog); kmem_cache_free(mktme_prog_cache, kprog); return ret; } -- 2.21.0