From patchwork Fri Jan 3 04:04:35 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 198292 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B4043C32771 for ; Fri, 3 Jan 2020 04:05:07 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 8B5F022314 for ; Fri, 3 Jan 2020 04:05:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024307; bh=o4OzcvqIdyJ6YIW9hU9gmpOjoV0BHrX/rhFquRitSjY=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=pwebNgKdXhB2DXtX/n2EZItZZrO9/Ckcno7F0n3uxAxFS82nQlAmbWfdunebIXhsw K482f4dbf+ZDQTCIKdd9EiLqaJ/KD6CcLFQEhN0VBxQYW7MfSf+JftVl9be5IDme4Z CpPogWhquWCD0J8W6tcdiJnIFGPUDGd3DZbT1CEE= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726781AbgACEFG (ORCPT ); Thu, 2 Jan 2020 23:05:06 -0500 Received: from mail.kernel.org ([198.145.29.99]:39522 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726292AbgACEFG (ORCPT ); Thu, 2 Jan 2020 23:05:06 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 026B8222C3 for ; Fri, 3 Jan 2020 04:05:05 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024306; bh=o4OzcvqIdyJ6YIW9hU9gmpOjoV0BHrX/rhFquRitSjY=; h=From:To:Subject:Date:In-Reply-To:References:From; b=VhqmIvW8D/CI+nkHmZ26RnIBK+3o5vZaftBGfarEu5jLR1P9M1QOQH5dHPS3obY37 gPR5m00PskJvjsNSJRRDkWIwQCR+727knykPZwAwmiK/3CbX7oi9jPeyuU0/RbhhTL es2ULXDbP6macfNGHQJlcowdcXhpEV2Skd/lC5zs= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 1/6] crypto: hash - add support for new way of freeing instances Date: Thu, 2 Jan 2020 20:04:35 -0800 Message-Id: <20200103040440.12375-2-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103040440.12375-1-ebiggers@kernel.org> References: <20200103040440.12375-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Add support to shash and ahash for the new way of freeing instances (already used for skcipher, aead, and akcipher) where a ->free() method is installed to the instance struct itself. These methods are more strongly-typed than crypto_template::free(), which they replace. This will allow removing support for the old way of freeing instances. Signed-off-by: Eric Biggers --- crypto/ahash.c | 13 +++++++++++++ crypto/shash.c | 13 +++++++++++++ include/crypto/internal/hash.h | 2 ++ 3 files changed, 28 insertions(+) diff --git a/crypto/ahash.c b/crypto/ahash.c index c77717fcea8e..61e374d76b04 100644 --- a/crypto/ahash.c +++ b/crypto/ahash.c @@ -511,6 +511,18 @@ static unsigned int crypto_ahash_extsize(struct crypto_alg *alg) return crypto_alg_extsize(alg); } +static void crypto_ahash_free_instance(struct crypto_instance *inst) +{ + struct ahash_instance *ahash = ahash_instance(inst); + + if (!ahash->free) { + inst->tmpl->free(inst); + return; + } + + ahash->free(ahash); +} + #ifdef CONFIG_NET static int crypto_ahash_report(struct sk_buff *skb, struct crypto_alg *alg) { @@ -547,6 +559,7 @@ static void crypto_ahash_show(struct seq_file *m, struct crypto_alg *alg) static const struct crypto_type crypto_ahash_type = { .extsize = crypto_ahash_extsize, .init_tfm = crypto_ahash_init_tfm, + .free = crypto_ahash_free_instance, #ifdef CONFIG_PROC_FS .show = crypto_ahash_show, #endif diff --git a/crypto/shash.c b/crypto/shash.c index 4d6ccb59e126..2f6adb49727b 100644 --- a/crypto/shash.c +++ b/crypto/shash.c @@ -423,6 +423,18 @@ static int crypto_shash_init_tfm(struct crypto_tfm *tfm) return 0; } +static void crypto_shash_free_instance(struct crypto_instance *inst) +{ + struct shash_instance *shash = shash_instance(inst); + + if (!shash->free) { + inst->tmpl->free(inst); + return; + } + + shash->free(shash); +} + #ifdef CONFIG_NET static int crypto_shash_report(struct sk_buff *skb, struct crypto_alg *alg) { @@ -459,6 +471,7 @@ static void crypto_shash_show(struct seq_file *m, struct crypto_alg *alg) static const struct crypto_type crypto_shash_type = { .extsize = crypto_alg_extsize, .init_tfm = crypto_shash_init_tfm, + .free = crypto_shash_free_instance, #ifdef CONFIG_PROC_FS .show = crypto_shash_show, #endif diff --git a/include/crypto/internal/hash.h b/include/crypto/internal/hash.h index c84b7cb29887..c550386221bb 100644 --- a/include/crypto/internal/hash.h +++ b/include/crypto/internal/hash.h @@ -30,6 +30,7 @@ struct crypto_hash_walk { }; struct ahash_instance { + void (*free)(struct ahash_instance *inst); union { struct { char head[offsetof(struct ahash_alg, halg.base)]; @@ -40,6 +41,7 @@ struct ahash_instance { }; struct shash_instance { + void (*free)(struct shash_instance *inst); union { struct { char head[offsetof(struct shash_alg, base)]; From patchwork Fri Jan 3 04:04:39 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 198291 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id 6C483C33C8C for ; Fri, 3 Jan 2020 04:05:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 4239121D7D for ; Fri, 3 Jan 2020 04:05:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024309; bh=liCio2QV74JVGVp2/G1te0xWk+zuq3FW4EFkuMrqVxw=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=O2yBOvgxhgIh0Azu95Xe140hHAIgWhWuqDY1VBVzBNmHtpEaalFF1HVpt0CHQ1w4r 2uWCqTK4cS2qL9cGyOtQAGrga26DOpegeuP1DKSENvAb/7+MP21/rYIKE/ShcMPVhf Ped6UGzjPIUsjYUioQIDuBN7pR9hu/mE78X3VvoQ= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726504AbgACEFI (ORCPT ); Thu, 2 Jan 2020 23:05:08 -0500 Received: from mail.kernel.org ([198.145.29.99]:39554 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726292AbgACEFH (ORCPT ); Thu, 2 Jan 2020 23:05:07 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id BD1BE21D7D for ; Fri, 3 Jan 2020 04:05:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024306; bh=liCio2QV74JVGVp2/G1te0xWk+zuq3FW4EFkuMrqVxw=; h=From:To:Subject:Date:In-Reply-To:References:From; b=Pxq6ze28detlqZvSRB/iVggt2tqt3JfBIWpvqPbTon8bYfrhiSukO55At+gN2lv+h 3zpgz3smfhCkKM864CbmeGKv8I3/qW/98nqFPbyqIg8OngMH4wZPB3aPvZf7iKZ2pF U7O7v9SdEYa7gq5t48eH8UCWMcIDHgdC9o6ihFAc= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 5/6] crypto: algapi - remove crypto_template::{alloc, free}() Date: Thu, 2 Jan 2020 20:04:39 -0800 Message-Id: <20200103040440.12375-6-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103040440.12375-1-ebiggers@kernel.org> References: <20200103040440.12375-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers Now that all templates provide a ->create() method which creates an instance, installs a strongly-typed ->free() method directly to it, and registers it, the older ->alloc() and ->free() methods in 'struct crypto_template' are no longer used. Remove them. Signed-off-by: Eric Biggers --- crypto/aead.c | 5 ----- crypto/ahash.c | 5 ----- crypto/algapi.c | 5 ----- crypto/algboss.c | 12 +----------- crypto/shash.c | 5 ----- include/crypto/algapi.h | 2 -- 6 files changed, 1 insertion(+), 33 deletions(-) diff --git a/crypto/aead.c b/crypto/aead.c index 02a0db076d7e..7707d3223101 100644 --- a/crypto/aead.c +++ b/crypto/aead.c @@ -185,11 +185,6 @@ static void crypto_aead_free_instance(struct crypto_instance *inst) { struct aead_instance *aead = aead_instance(inst); - if (!aead->free) { - inst->tmpl->free(inst); - return; - } - aead->free(aead); } diff --git a/crypto/ahash.c b/crypto/ahash.c index 61e374d76b04..cd5d9847d513 100644 --- a/crypto/ahash.c +++ b/crypto/ahash.c @@ -515,11 +515,6 @@ static void crypto_ahash_free_instance(struct crypto_instance *inst) { struct ahash_instance *ahash = ahash_instance(inst); - if (!ahash->free) { - inst->tmpl->free(inst); - return; - } - ahash->free(ahash); } diff --git a/crypto/algapi.c b/crypto/algapi.c index 72592795c7e7..69605e21af92 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -65,11 +65,6 @@ static int crypto_check_alg(struct crypto_alg *alg) static void crypto_free_instance(struct crypto_instance *inst) { - if (!inst->alg.cra_type->free) { - inst->tmpl->free(inst); - return; - } - inst->alg.cra_type->free(inst); } diff --git a/crypto/algboss.c b/crypto/algboss.c index a62149d6c839..535f1f87e6c1 100644 --- a/crypto/algboss.c +++ b/crypto/algboss.c @@ -58,7 +58,6 @@ static int cryptomgr_probe(void *data) { struct cryptomgr_param *param = data; struct crypto_template *tmpl; - struct crypto_instance *inst; int err; tmpl = crypto_lookup_template(param->template); @@ -66,16 +65,7 @@ static int cryptomgr_probe(void *data) goto out; do { - if (tmpl->create) { - err = tmpl->create(tmpl, param->tb); - continue; - } - - inst = tmpl->alloc(param->tb); - if (IS_ERR(inst)) - err = PTR_ERR(inst); - else if ((err = crypto_register_instance(tmpl, inst))) - tmpl->free(inst); + err = tmpl->create(tmpl, param->tb); } while (err == -EAGAIN && !signal_pending(current)); crypto_tmpl_put(tmpl); diff --git a/crypto/shash.c b/crypto/shash.c index e05e75b0f402..70faf28b2d14 100644 --- a/crypto/shash.c +++ b/crypto/shash.c @@ -427,11 +427,6 @@ static void crypto_shash_free_instance(struct crypto_instance *inst) { struct shash_instance *shash = shash_instance(inst); - if (!shash->free) { - inst->tmpl->free(inst); - return; - } - shash->free(shash); } diff --git a/include/crypto/algapi.h b/include/crypto/algapi.h index c16c50f8dac1..e115f9215ed5 100644 --- a/include/crypto/algapi.h +++ b/include/crypto/algapi.h @@ -63,8 +63,6 @@ struct crypto_template { struct hlist_head instances; struct module *module; - struct crypto_instance *(*alloc)(struct rtattr **tb); - void (*free)(struct crypto_instance *inst); int (*create)(struct crypto_template *tmpl, struct rtattr **tb); char name[CRYPTO_MAX_ALG_NAME]; From patchwork Fri Jan 3 04:04:40 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 198290 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-10.1 required=3.0 tests=DKIMWL_WL_HIGH, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, INCLUDES_PATCH, MAILING_LIST_MULTI, SIGNED_OFF_BY, SPF_HELO_NONE, SPF_PASS, USER_AGENT_GIT autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id B9689C33C99 for ; Fri, 3 Jan 2020 04:05:09 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id 9074221D7D for ; Fri, 3 Jan 2020 04:05:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024309; bh=krCjoqWYb9JakE/BHd/58VVe+cWrL8n1ELLyCYUkXN0=; h=From:To:Subject:Date:In-Reply-To:References:List-ID:From; b=qLdFTiHUG+Q7mYm/kQKvtWePOwofTHPmHFtgTYSmkKM3Y8Yj6s5YmRzijkHEgJJLY 0PjCiRUS3o5I368FYUgxelq5yY7ZFMQ4dGsaj5E8Q9hkDtuTmf8Ka80nFhDmMuiVmi Uffze40BpzkMd1g/Lh2dHEF7nefsfXtaVkkj1lR8= Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726292AbgACEFI (ORCPT ); Thu, 2 Jan 2020 23:05:08 -0500 Received: from mail.kernel.org ([198.145.29.99]:39560 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1727210AbgACEFH (ORCPT ); Thu, 2 Jan 2020 23:05:07 -0500 Received: from sol.localdomain (c-24-5-143-220.hsd1.ca.comcast.net [24.5.143.220]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id ED17B222C3 for ; Fri, 3 Jan 2020 04:05:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1578024307; bh=krCjoqWYb9JakE/BHd/58VVe+cWrL8n1ELLyCYUkXN0=; h=From:To:Subject:Date:In-Reply-To:References:From; b=PcpiB5czGK71LceH5siTCzRnVYPXjSchrBLKTNvccI4ehIp1p31xcaBukgOrnEqut C1+Z0cHDti/gD+r4hPpuvigb/2XVtHIoUVGfnMxUZTZWKBfBi4QIo9W+WmUx7j1osB SYIStjuy4Kw4LG63/Pz7DC6CxCQ6AfinD5LnKgu8= From: Eric Biggers To: linux-crypto@vger.kernel.org Subject: [PATCH v2 6/6] crypto: algapi - enforce that all instances have a ->free() method Date: Thu, 2 Jan 2020 20:04:40 -0800 Message-Id: <20200103040440.12375-7-ebiggers@kernel.org> X-Mailer: git-send-email 2.24.1 In-Reply-To: <20200103040440.12375-1-ebiggers@kernel.org> References: <20200103040440.12375-1-ebiggers@kernel.org> MIME-Version: 1.0 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org From: Eric Biggers All instances need to have a ->free() method, but people could forget to set it and then not notice if the instance is never unregistered. To help detect this bug earlier, don't allow an instance without a ->free() method to be registered, and complain loudly if someone tries to do it. Signed-off-by: Eric Biggers --- crypto/aead.c | 3 +++ crypto/ahash.c | 3 +++ crypto/akcipher.c | 2 ++ crypto/shash.c | 3 +++ crypto/skcipher.c | 3 +++ 5 files changed, 14 insertions(+) diff --git a/crypto/aead.c b/crypto/aead.c index 7707d3223101..16991095270d 100644 --- a/crypto/aead.c +++ b/crypto/aead.c @@ -288,6 +288,9 @@ int aead_register_instance(struct crypto_template *tmpl, { int err; + if (WARN_ON(!inst->free)) + return -EINVAL; + err = aead_prepare_alg(&inst->alg); if (err) return err; diff --git a/crypto/ahash.c b/crypto/ahash.c index cd5d9847d513..68a0f0cb75c4 100644 --- a/crypto/ahash.c +++ b/crypto/ahash.c @@ -656,6 +656,9 @@ int ahash_register_instance(struct crypto_template *tmpl, { int err; + if (WARN_ON(!inst->free)) + return -EINVAL; + err = ahash_prepare_alg(&inst->alg); if (err) return err; diff --git a/crypto/akcipher.c b/crypto/akcipher.c index eeed6c151d2f..f866085c8a4a 100644 --- a/crypto/akcipher.c +++ b/crypto/akcipher.c @@ -147,6 +147,8 @@ EXPORT_SYMBOL_GPL(crypto_unregister_akcipher); int akcipher_register_instance(struct crypto_template *tmpl, struct akcipher_instance *inst) { + if (WARN_ON(!inst->free)) + return -EINVAL; akcipher_prepare_alg(&inst->alg); return crypto_register_instance(tmpl, akcipher_crypto_instance(inst)); } diff --git a/crypto/shash.c b/crypto/shash.c index 70faf28b2d14..c075b26c2a1d 100644 --- a/crypto/shash.c +++ b/crypto/shash.c @@ -577,6 +577,9 @@ int shash_register_instance(struct crypto_template *tmpl, { int err; + if (WARN_ON(!inst->free)) + return -EINVAL; + err = shash_prepare_alg(&inst->alg); if (err) return err; diff --git a/crypto/skcipher.c b/crypto/skcipher.c index 8c37243307aa..ba41f81fac0b 100644 --- a/crypto/skcipher.c +++ b/crypto/skcipher.c @@ -876,6 +876,9 @@ int skcipher_register_instance(struct crypto_template *tmpl, { int err; + if (WARN_ON(!inst->free)) + return -EINVAL; + err = skcipher_prepare_alg(&inst->alg); if (err) return err;