Received: by 2002:a25:ad19:0:0:0:0:0 with SMTP id y25csp6109528ybi; Wed, 31 Jul 2019 08:29:45 -0700 (PDT) X-Google-Smtp-Source: APXvYqypSTcqdySQt8o0ANcEpb87PpXy/Gl4Laobeb2woiMHmG0qttusIYD089lmRxbM/AvQJIbM X-Received: by 2002:a17:90a:3247:: with SMTP id k65mr3530513pjb.49.1564586984851; Wed, 31 Jul 2019 08:29:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1564586984; cv=none; d=google.com; s=arc-20160816; b=zMXstJa70F/w5zGJKEz9fZyptWZWZHDtuQ5NDFXccq4tDsPDWC8/Qcuu933odiA+Vh wO2ojeplOrh/3Il02hQX3NhVBOZSPFrUlXh8x8Ppf2nWngcAdOV1DjkCwrJYZE6ERwd2 OUzi6VzlGOUkPioLDyiM92SF1BEvFFWqRt1d7DMcL4eqMs1i41a5XqVjQF96LKZGbspy P4UwHhzf5N+yISrF917z0icqKrQ7B9vMVxlNpVvVOAEAY27+ldvHAIGhAHJ/heJ9EwNJ NMczV4U3vuqLTWiumDgVeLnK29OoQ5V7K1IGHmd+wpALMZ3cvHDRQ9XGd5uahmenrWHA 4rWQ== 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=bD/5ZInA5DBfNF/OoolUD2uLCIaOC1DGsUwSCxp+njs=; b=dWuTXSnteFzEMXIBi7dnYMAHCg27tkrE4BQyEEmvLBtkRupre/MtcXgpxaUBUiMWvg K6W2Xu4825MnnYuziiQIl9GMQN4f5jJOAsXlw00lZ18YlyEUF1KD2z7iGr6Rcg0nB5MZ ctcUtUNfqopx1y4qtKniF6gLOoheIpRh9DERjDs6Kdta6JJ0QtQvSGB8ORFAu7mURmGq LqmVc8m4AxidUoEkWCYD/MRj3kd0uoF1LOVXj2qaBo3d6/+JjzMiBbsg3rLTA7k9HXWt z5I3ERGcNyhXol5+uI/BkvvpQjw1HvmouTgXwjTtgKe85PriQ1rX/XuBQBBYr+/F2k3e OmTA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@shutemov-name.20150623.gappssmtp.com header.s=20150623 header.b=nvqdVZ7h; 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 e5si30103017pgc.29.2019.07.31.08.29.29; Wed, 31 Jul 2019 08:29:44 -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=nvqdVZ7h; 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 S1730037AbfGaPU3 (ORCPT + 99 others); Wed, 31 Jul 2019 11:20:29 -0400 Received: from mail-ed1-f67.google.com ([209.85.208.67]:41761 "EHLO mail-ed1-f67.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728787AbfGaPU0 (ORCPT ); Wed, 31 Jul 2019 11:20:26 -0400 Received: by mail-ed1-f67.google.com with SMTP id p15so66014153eds.8 for ; Wed, 31 Jul 2019 08:20:24 -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=bD/5ZInA5DBfNF/OoolUD2uLCIaOC1DGsUwSCxp+njs=; b=nvqdVZ7hy8prTTJcbUZBwkmbx2dG13l4tJOjU/HGiWPNPCIzhkiP7BT5p8Q5xWDndr 7DeAzS8uAbr6+O45Oq+aNxw5yIDCtTUXGB0id3g7Z8mpUiISvVh8+Qdpp6w9Mrz78JiC 0VezF9Twz7YfGrVqrIO2CaQOxV5IahFa9jvmh6yBk00rx18AqNPPp2V83WMw5/RHJxZK V7Ij2TTHXT4PuakKHcAQZMB0LBshUp7ZbQOCUWMzIrguS1msbfDi5NilYa6XZJJc6A82 xAVNYZPhVRKGVGB4dKsMF59lYW4aPwm4ovfr9mL7D2xAkl0s4z/mmm5UiL+UO1fObzyu 5wPg== 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=bD/5ZInA5DBfNF/OoolUD2uLCIaOC1DGsUwSCxp+njs=; b=tXTf+oXfHKtaP0sNzDxz3NXjtpAfesrjzTQPbSQg1jrSJZ9j64oltBZxI5Kej4rF09 lsGZS+h+43y4VYrMt9oGnlBf0fBam6ZExeM5oklKy4OaP30NFQ8HffU8810LdQgg+uBm Vxx6eau2ZYs6aYHN+SiXH7WRgvQrKX75VYXZ/V6XYiwQQg8aL0irBWVlUoUoesD6Yz1+ yc43sd4gO8QIFGkrz7D2algF1W+GcnoNXxH43SQgJkec4hhM8SGUKETyO8Vpa9Qckk99 Wbg2DKG11Se0amZwsKXngHVb5VBli/9eknWIX4ikKMPxQXVTTStcPNgrhEud8t3gIXBn 6Tag== X-Gm-Message-State: APjAAAVeCJs6GniBht5JWc+eJSNyphfyJ2PafN1ao8bMMkulEq+upCcK SrhlD1A7xYL8tagBmW6icNQ= X-Received: by 2002:a05:6402:6d0:: with SMTP id n16mr25572624edy.168.1564586037300; Wed, 31 Jul 2019 08:13:57 -0700 (PDT) Received: from box.localdomain ([86.57.175.117]) by smtp.gmail.com with ESMTPSA id t2sm17397627eda.95.2019.07.31.08.13.52 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 31 Jul 2019 08:13:54 -0700 (PDT) From: "Kirill A. Shutemov" X-Google-Original-From: "Kirill A. Shutemov" Received: by box.localdomain (Postfix, from userid 1000) id C65041044A6; 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 31/59] keys/mktme: Set up a percpu_ref_count for MKTME keys Date: Wed, 31 Jul 2019 18:07:45 +0300 Message-Id: <20190731150813.26289-32-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 The MKTME key service needs to keep usage counts on the encryption keys in order to know when it is safe to free a key for reuse. percpu_ref_count applies well here because the key service will take the initial reference and typically hold that reference while the intermediary references are get/put. The intermediaries in this case will be encrypted VMA's, Align the percpu_ref_init and percpu_ref_kill with the key service instantiate and destroy methods respectively. Signed-off-by: Alison Schofield Signed-off-by: Kirill A. Shutemov --- security/keys/mktme_keys.c | 39 +++++++++++++++++++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/security/keys/mktme_keys.c b/security/keys/mktme_keys.c index 3c641f3ee794..18cb57be5193 100644 --- a/security/keys/mktme_keys.c +++ b/security/keys/mktme_keys.c @@ -8,6 +8,7 @@ #include #include #include +#include #include #include #include @@ -71,6 +72,26 @@ int mktme_keyid_from_key(struct key *key) return 0; } +struct percpu_ref *encrypt_count; +void mktme_percpu_ref_release(struct percpu_ref *ref) +{ + unsigned long flags; + int keyid; + + for (keyid = 1; keyid <= mktme_nr_keyids(); keyid++) { + if (&encrypt_count[keyid] == ref) + break; + } + if (&encrypt_count[keyid] != ref) { + pr_debug("%s: invalid ref counter\n", __func__); + return; + } + percpu_ref_exit(ref); + spin_lock_irqsave(&mktme_lock, flags); + mktme_release_keyid(keyid); + spin_unlock_irqrestore(&mktme_lock, flags); +} + enum mktme_opt_id { OPT_ERROR, OPT_TYPE, @@ -199,8 +220,10 @@ static void mktme_destroy_key(struct key *key) unsigned long flags; spin_lock_irqsave(&mktme_lock, flags); - mktme_release_keyid(keyid); + mktme_map[keyid].key = NULL; + mktme_map[keyid].state = KEYID_REF_KILLED; spin_unlock_irqrestore(&mktme_lock, flags); + percpu_ref_kill(&encrypt_count[keyid]); } /* Key Service Method to create a new key. Payload is preparsed. */ @@ -216,9 +239,15 @@ int mktme_instantiate_key(struct key *key, struct key_preparsed_payload *prep) if (!keyid) return -ENOKEY; + if (percpu_ref_init(&encrypt_count[keyid], mktme_percpu_ref_release, + 0, GFP_KERNEL)) + goto err_out; + if (!mktme_program_keyid(keyid, *payload)) return MKTME_PROG_SUCCESS; + percpu_ref_exit(&encrypt_count[keyid]); +err_out: spin_lock_irqsave(&mktme_lock, flags); mktme_release_keyid(keyid); spin_unlock_irqrestore(&mktme_lock, flags); @@ -405,10 +434,18 @@ static int __init init_mktme(void) /* Initialize first programming targets */ mktme_update_pconfig_targets(); + /* Reference counters to protect in use KeyIDs */ + encrypt_count = kvcalloc(mktme_nr_keyids() + 1, sizeof(encrypt_count[0]), + GFP_KERNEL); + if (!encrypt_count) + goto free_targets; + ret = register_key_type(&key_type_mktme); if (!ret) return ret; /* SUCCESS */ + kvfree(encrypt_count); +free_targets: free_cpumask_var(mktme_leadcpus); bitmap_free(mktme_target_map); free_cache: -- 2.21.0