From patchwork Fri Dec 29 16:00:46 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Eric Biggers X-Patchwork-Id: 10137199 X-Patchwork-Delegate: herbert@gondor.apana.org.au Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 6ACDE60318 for ; Fri, 29 Dec 2017 16:05:02 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 5C9292897C for ; Fri, 29 Dec 2017 16:05:02 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 535CF29CBF; Fri, 29 Dec 2017 16:05:02 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, FREEMAIL_FROM, RCVD_IN_DNSWL_HI autolearn=unavailable version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 2D0512A079 for ; Fri, 29 Dec 2017 16:04:44 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1751159AbdL2QEh (ORCPT ); Fri, 29 Dec 2017 11:04:37 -0500 Received: from mail-io0-f194.google.com ([209.85.223.194]:34022 "EHLO mail-io0-f194.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750869AbdL2QEg (ORCPT ); Fri, 29 Dec 2017 11:04:36 -0500 Received: by mail-io0-f194.google.com with SMTP id q188so17296284iod.1; Fri, 29 Dec 2017 08:04:36 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:cc:subject:date:message-id; bh=mno/LNpPelmHoxx7jiGRZHOQfrqqtJ6wsSR7iaNOh6U=; b=BGO6+L9MBrExWqjNz2RIvzyG4qOmXpaVjJRBq0K9ij6W1TiiYNFNOO5r5Z1MNT2m88 yCQAZzpbZhNBO5z3ZPtka5sST4AZ3Es1ZLpgjYhJ9f83bUtbrcdXZ8W3M2CDmA/SW0Av KNlV4Y1KR25Bjvu+hJ/PaCqp44mDzmMonRD9HKja0NCpYCDj9Cem2ofX5RvdGaYm8fmh wWdFNgQ9YTrfLcnqY+GH2HrW1oAm/DM2lI8Dtdoogh584hqR5LFCeMjTxvab0y30DDPj jW5OTq67ecWfpo074ezNz7pAJiE3S4PZ9bmD3Rz+jL68vBWTxPk6qP+kkOktcLACR88e 8hcw== 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; bh=mno/LNpPelmHoxx7jiGRZHOQfrqqtJ6wsSR7iaNOh6U=; b=jIX1ITCb704K1l/FTBXYqU1hxnrOY33qE+y4FyBhw+BAL6e8Riwr3ZvcsuYjg/jNKP r+R1aJK+AHo4dJ/uNmK/qj16k1RNZpYzrbOcre3ZDfELxV5M8DEzZpox8AuKCuFga6UL E/Gk2j0ehb1Q3T5UikJYe+hgqKOutLC8nSDBfdkSgSCQatyHnR0fUqiesaX0r/2BwmqD T7Ub6Pam0apZGfiBP6NfbObyqJpsU+GmXfrfSRlYGTu60Tf3uUWxLWP1YwwgLYfHSyix FvwHvxrCqoXo/OpjfOxKLPsImZ30t11h1OcI+WoZOHnPmVInjU0hgM6gEwuREA/XAKtn WXtw== X-Gm-Message-State: AKGB3mJq7xubiEuQFB4N/dUbaRHlhONvbK0MUSHpiCKnrLkNYXE5ipWE A7Z4ldEJD23jOnmmRWbFsQ9mt/ke X-Google-Smtp-Source: ACJfBote3BntU3gYad1KXA7ZlNns8KrfB1FE00K1rRPnvMrtN7Y94+njg6OVHgRjNpbnjne2h+30hA== X-Received: by 10.107.197.197 with SMTP id v188mr44460366iof.279.1514563475720; Fri, 29 Dec 2017 08:04:35 -0800 (PST) Received: from zzz.Home (h184-60-19-231.mdsnwi.dsl.dynamic.tds.net. [184.60.19.231]) by smtp.gmail.com with ESMTPSA id 124sm13168847itw.30.2017.12.29.08.04.34 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Fri, 29 Dec 2017 08:04:34 -0800 (PST) From: Eric Biggers To: linux-crypto@vger.kernel.org, Herbert Xu Cc: "David S . Miller" , Elena Reshetova , linux-kernel@vger.kernel.org, Eric Biggers Subject: [PATCH] crypto: algapi - convert cra_refcnt to refcount_t Date: Fri, 29 Dec 2017 10:00:46 -0600 Message-Id: <20171229160046.23661-1-ebiggers3@gmail.com> X-Mailer: git-send-email 2.15.1 Sender: linux-crypto-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-crypto@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP From: Eric Biggers Reference counters should use refcount_t rather than atomic_t, since the refcount_t implementation can prevent overflows, reducing the exploitability of reference leak bugs. crypto_alg.cra_refcount is a reference counter with the usual semantics, so switch it over to refcount_t. Signed-off-by: Eric Biggers --- crypto/algapi.c | 8 ++++---- crypto/api.c | 2 +- crypto/crypto_user.c | 4 ++-- crypto/internal.h | 4 ++-- crypto/proc.c | 2 +- include/linux/crypto.h | 2 +- 6 files changed, 11 insertions(+), 11 deletions(-) diff --git a/crypto/algapi.c b/crypto/algapi.c index 60d7366ed343..8084a76e01d8 100644 --- a/crypto/algapi.c +++ b/crypto/algapi.c @@ -62,7 +62,7 @@ static int crypto_check_alg(struct crypto_alg *alg) if (alg->cra_priority < 0) return -EINVAL; - atomic_set(&alg->cra_refcnt, 1); + refcount_set(&alg->cra_refcnt, 1); return crypto_set_driver_name(alg); } @@ -224,7 +224,7 @@ static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg) if (!larval->adult) goto free_larval; - atomic_set(&larval->alg.cra_refcnt, 1); + refcount_set(&larval->alg.cra_refcnt, 1); memcpy(larval->alg.cra_driver_name, alg->cra_driver_name, CRYPTO_MAX_ALG_NAME); larval->alg.cra_priority = alg->cra_priority; @@ -399,7 +399,7 @@ int crypto_unregister_alg(struct crypto_alg *alg) if (ret) return ret; - BUG_ON(atomic_read(&alg->cra_refcnt) != 1); + BUG_ON(refcount_read(&alg->cra_refcnt) != 1); if (alg->cra_destroy) alg->cra_destroy(alg); @@ -490,7 +490,7 @@ void crypto_unregister_template(struct crypto_template *tmpl) up_write(&crypto_alg_sem); hlist_for_each_entry_safe(inst, n, list, list) { - BUG_ON(atomic_read(&inst->alg.cra_refcnt) != 1); + BUG_ON(refcount_read(&inst->alg.cra_refcnt) != 1); crypto_free_instance(inst); } crypto_remove_final(&users); diff --git a/crypto/api.c b/crypto/api.c index 6da802d7be67..70a894e52ff3 100644 --- a/crypto/api.c +++ b/crypto/api.c @@ -137,7 +137,7 @@ static struct crypto_alg *crypto_larval_add(const char *name, u32 type, if (IS_ERR(larval)) return ERR_CAST(larval); - atomic_set(&larval->alg.cra_refcnt, 2); + refcount_set(&larval->alg.cra_refcnt, 2); down_write(&crypto_alg_sem); alg = __crypto_alg_lookup(name, type, mask); diff --git a/crypto/crypto_user.c b/crypto/crypto_user.c index 0dbe2be7f783..5c291eedaa70 100644 --- a/crypto/crypto_user.c +++ b/crypto/crypto_user.c @@ -169,7 +169,7 @@ static int crypto_report_one(struct crypto_alg *alg, ualg->cru_type = 0; ualg->cru_mask = 0; ualg->cru_flags = alg->cra_flags; - ualg->cru_refcnt = atomic_read(&alg->cra_refcnt); + ualg->cru_refcnt = refcount_read(&alg->cra_refcnt); if (nla_put_u32(skb, CRYPTOCFGA_PRIORITY_VAL, alg->cra_priority)) goto nla_put_failure; @@ -387,7 +387,7 @@ static int crypto_del_alg(struct sk_buff *skb, struct nlmsghdr *nlh, goto drop_alg; err = -EBUSY; - if (atomic_read(&alg->cra_refcnt) > 2) + if (refcount_read(&alg->cra_refcnt) > 2) goto drop_alg; err = crypto_unregister_instance((struct crypto_instance *)alg); diff --git a/crypto/internal.h b/crypto/internal.h index ae65e5fcaa59..1388af6da85a 100644 --- a/crypto/internal.h +++ b/crypto/internal.h @@ -105,13 +105,13 @@ int crypto_type_has_alg(const char *name, const struct crypto_type *frontend, static inline struct crypto_alg *crypto_alg_get(struct crypto_alg *alg) { - atomic_inc(&alg->cra_refcnt); + refcount_inc(&alg->cra_refcnt); return alg; } static inline void crypto_alg_put(struct crypto_alg *alg) { - if (atomic_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy) + if (refcount_dec_and_test(&alg->cra_refcnt) && alg->cra_destroy) alg->cra_destroy(alg); } diff --git a/crypto/proc.c b/crypto/proc.c index 2cc10c96d753..822fcef6d91c 100644 --- a/crypto/proc.c +++ b/crypto/proc.c @@ -46,7 +46,7 @@ static int c_show(struct seq_file *m, void *p) seq_printf(m, "driver : %s\n", alg->cra_driver_name); seq_printf(m, "module : %s\n", module_name(alg->cra_module)); seq_printf(m, "priority : %d\n", alg->cra_priority); - seq_printf(m, "refcnt : %d\n", atomic_read(&alg->cra_refcnt)); + seq_printf(m, "refcnt : %u\n", refcount_read(&alg->cra_refcnt)); seq_printf(m, "selftest : %s\n", (alg->cra_flags & CRYPTO_ALG_TESTED) ? "passed" : "unknown"); diff --git a/include/linux/crypto.h b/include/linux/crypto.h index 78508ca4b108..231e59f90d32 100644 --- a/include/linux/crypto.h +++ b/include/linux/crypto.h @@ -447,7 +447,7 @@ struct crypto_alg { unsigned int cra_alignmask; int cra_priority; - atomic_t cra_refcnt; + refcount_t cra_refcnt; char cra_name[CRYPTO_MAX_ALG_NAME]; char cra_driver_name[CRYPTO_MAX_ALG_NAME];