crypto: crypto_user_stat: misc enhancement
cover crypto: crypto_user_stat: misc enhancement
Hello
This patchset fixes all reported problem by Eric biggers.
This patchset should be applied on top of Eric biggers patchset "crypto: crypto_user reporting fixes and cleanups"
Regards
Corentin Labbe (5):
crypto: crypto_user_stat: made crypto_user_stat optional
crypto: crypto_user_stat: convert all stats from u32 to u64
crypto: crypto_user_stat: split user space crypto stat structures
crypto: tool: getstat: convert user space example to the new
crypto_user_stat uapi
crypto: crypto_user_stat: fix use_after_free of struct xxx_request
crypto/Makefile | 3 +- crypto/ahash.c | 14 ++- crypto/algapi.c | 12 ++- crypto/crypto_user_stat.c | 134 +++++++++++++-------------- include/crypto/acompress.h | 30 +++--- include/crypto/aead.h | 30 +++--- include/crypto/akcipher.h | 56 ++++++----- include/crypto/hash.h | 25 ++--- include/crypto/internal/cryptouser.h | 17 ++++ include/crypto/kpp.h | 26 +++--- include/crypto/rng.h | 8 +- include/crypto/skcipher.h | 24 +++-- include/linux/crypto.h | 74 +++++++-------- include/uapi/linux/cryptouser.h | 103 ++++++++++++-------- tools/crypto/getstat.c | 54 +++++------ 15 files changed, 329 insertions(+), 281 deletions(-)
Signature · 1 line
2.18.1
PATCH 1/5 crypto: crypto_user_stat: made crypto_user_stat optional
Even if CRYPTO_STATS is set to n, some part of CRYPTO_STATS are compiled.
This patch made all part of crypto_user_stat uncompiled in that case.
--- crypto/Makefile | 3 ++- crypto/algapi.c | 2 ++ include/crypto/internal/cryptouser.h | 17 +++++++++++++++++ include/linux/crypto.h | 2 ++ 4 files changed, 23 insertions(+), 1 deletion(-)
crypto/Makefile +2 -1
@@ -54,7 +54,8 @@ cryptomgr-y := algboss.o testmgr.o obj-$(CONFIG_CRYPTO_MANAGER2) += cryptomgr.o obj-$(CONFIG_CRYPTO_USER) += crypto_user.o -crypto_user-y := crypto_user_base.o crypto_user_stat.o +crypto_user-y := crypto_user_base.o +crypto_user-$(CONFIG_CRYPTO_STATS) += crypto_user_stat.o obj-$(CONFIG_CRYPTO_CMAC) += cmac.o obj-$(CONFIG_CRYPTO_HMAC) += hmac.o obj-$(CONFIG_CRYPTO_VMAC) += vmac.o
crypto/algapi.c +2 -0
@@ -258,6 +258,7 @@ static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)list_add (&alg->cra_list, &crypto_alg_list);list_add (&larval->alg.cra_list, &crypto_alg_list); +#ifdef CONFIG_CRYPTO_STATS atomic_set (&alg->encrypt_cnt,0 );atomic_set (&alg->decrypt_cnt,0 );atomic64_set (&alg->encrypt_tlen,0 );
@@ -265,6 +266,7 @@ static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)atomic_set (&alg->verify_cnt,0 );atomic_set (&alg->cipher_err_cnt,0 );atomic_set (&alg->sign_cnt,0 ); +#endif out:return larval;
include/crypto/internal/cryptouser.h +17 -0
@@ -3,6 +3,23 @@struct crypto_alg *crypto_alg_match (struct crypto_user_alg *p,int exact); +#ifdef CONFIG_CRYPTO_STATS int crypto_dump_reportstat (struct sk_buff *skb,struct netlink_callback *cb);int crypto_reportstat (struct sk_buff *in_skb,struct nlmsghdr *in_nlh,struct nlattr **attrs);int crypto_dump_reportstat_done (struct netlink_callback *cb); +#else +static int crypto_dump_reportstat (struct sk_buff *skb,struct netlink_callback *cb) +{ +return -ENOTSUPP ; +} + +static int crypto_reportstat (struct sk_buff *in_skb,struct nlmsghdr *in_nlh,struct nlattr **attrs) +{ +return -ENOTSUPP ; +} + +static int crypto_dump_reportstat_done (struct netlink_callback *cb) +{ +return -ENOTSUPP ; +} +#endif
include/linux/crypto.h +2 -0
@@ -515,6 +515,7 @@ struct crypto_alg {struct module *cra_module; +#ifdef CONFIG_CRYPTO_STATS union { atomic_t encrypt_cnt; atomic_t compress_cnt;
@@ -552,6 +553,7 @@ struct crypto_alg { atomic_t compute_shared_secret_cnt; };atomic_t sign_cnt; +#endif }CRYPTO_MINALIGN_ATTR ;
Signature · 1 line
2.18.1
Re: [PATCH 1/5] crypto: crypto_user_stat: made crypto_user_stat optional
Hi Corentin,
Quoted 17 lines · Corentin Labbe
Even if
CRYPTO_STATSis set to n, some part ofCRYPTO_STATSare compiled.
This patch made all part ofcrypto_user_statuncompiled in that case.Signed-off-by Corentin Labbe <[email protected]>--- crypto/Makefile | 3 ++- crypto/algapi.c | 2 ++ include/crypto/internal/cryptouser.h | 17 +++++++++++++++++ include/linux/crypto.h | 2 ++ 4 files changed, 23 insertions(+), 1 deletion(-)crypto/Makefile +0
-0
crypto/Makefile cryptomgr-y := algboss.o testmgr.o · line 61
Quoted 9 lines
crypto/Makefile +2
-1@@ -54,7 +54,8 @@ cryptomgr-y := algboss.o testmgr.o obj-$(CONFIG_CRYPTO_MANAGER2) += cryptomgr.o obj-$(CONFIG_CRYPTO_USER) += crypto_user.o -crypto_user-y := crypto_user_base.o crypto_user_stat.o +crypto_user-y := crypto_user_base.o +crypto_user-$(CONFIG_CRYPTO_STATS) += crypto_user_stat.o obj-$(CONFIG_CRYPTO_CMAC) += cmac.o obj-$(CONFIG_CRYPTO_HMAC) += hmac.o obj-$(CONFIG_CRYPTO_VMAC) += vmac.o
Doesn't CONFIG_CRYPTO_STATS also need to depend on CONFIG_CRYPTO_USER? Currently you can enable it but without CRYPTO_USER the API won't be available.
config CRYPTO_STATS
bool "Crypto usage statistics for User-space"
should be:
config CRYPTO_STATS
bool "Crypto usage statistics for User-space" depends on CRYPTO_USER
Quoted 13 lines
include/linux/crypto.h +1
-0@@ -515,6 +515,7 @@ struct crypto_alg {struct module *cra_module; +#ifdef CONFIG_CRYPTO_STATS union { atomic_t encrypt_cnt; atomic_t compress_cnt;
include/linux/crypto.h struct crypto_alg { · line 556
Quoted 4 lines
include/linux/crypto.h +1
-0@@ -552,6 +553,7 @@ struct crypto_alg { atomic_t compute_shared_secret_cnt; };atomic_t sign_cnt; +#endif
This is a long ifdef, so use:
#endif /* CONFIG_CRYPTO_STATS */
- Eric
Re: [PATCH 1/5] crypto: crypto_user_stat: made crypto_user_stat optional
Quoted 62 lines · Eric Biggers
Hi Corentin,
Quoted 17 lines · Corentin Labbe
Even if
CRYPTO_STATSis set to n, some part ofCRYPTO_STATSare compiled.
This patch made all part ofcrypto_user_statuncompiled in that case.Signed-off-by Corentin Labbe <[email protected]>--- crypto/Makefile | 3 ++- crypto/algapi.c | 2 ++ include/crypto/internal/cryptouser.h | 17 +++++++++++++++++ include/linux/crypto.h | 2 ++ 4 files changed, 23 insertions(+), 1 deletion(-)crypto/Makefile +0
-0crypto/Makefile cryptomgr-y := algboss.o testmgr.o · line 61
Quoted 9 lines
crypto/Makefile +2
-1@@ -54,7 +54,8 @@ cryptomgr-y := algboss.o testmgr.o obj-$(CONFIG_CRYPTO_MANAGER2) += cryptomgr.o obj-$(CONFIG_CRYPTO_USER) += crypto_user.o -crypto_user-y := crypto_user_base.o crypto_user_stat.o +crypto_user-y := crypto_user_base.o +crypto_user-$(CONFIG_CRYPTO_STATS) += crypto_user_stat.o obj-$(CONFIG_CRYPTO_CMAC) += cmac.o obj-$(CONFIG_CRYPTO_HMAC) += hmac.o obj-$(CONFIG_CRYPTO_VMAC) += vmac.oDoesn't
CONFIG_CRYPTO_STATSalso need to depend onCONFIG_CRYPTO_USER? Currently you can enable it but withoutCRYPTO_USERthe API won't be available.config
CRYPTO_STATS
bool "Crypto usage statistics for User-space"should be:
config
CRYPTO_STATS
bool "Crypto usage statistics for User-space" depends onCRYPTO_USERQuoted 13 lines
include/linux/crypto.h +1
-0@@ -515,6 +515,7 @@ struct crypto_alg {struct module *cra_module; +#ifdef CONFIG_CRYPTO_STATS union { atomic_t encrypt_cnt; atomic_t compress_cnt;include/linux/crypto.h struct crypto_alg { · line 556
Quoted 4 lines
include/linux/crypto.h +1
-0@@ -552,6 +553,7 @@ struct crypto_alg { atomic_t compute_shared_secret_cnt; };atomic_t sign_cnt; +#endif This is a long ifdef, so use:
Quoted 3 lines
#endif /*
CONFIG_CRYPTO_STATS*/- Eric
I will fix that
Thanks
PATCH 2/5 crypto: crypto_user_stat: convert all stats from u32 to u64
In reply to Corentin LabbeAll the 32-bit fields need to be 64-bit. In some cases, UINT32_MAX crypto operations can be done in seconds.
--- crypto/algapi.c | 10 +-- crypto/crypto_user_stat.c | 114 +++++++++++++++----------------- include/crypto/acompress.h | 8 +-- include/crypto/aead.h | 8 +-- include/crypto/akcipher.h | 16 ++--- include/crypto/hash.h | 6 +- include/crypto/kpp.h | 12 ++-- include/crypto/rng.h | 8 +-- include/crypto/skcipher.h | 8 +-- include/linux/crypto.h | 46 ++++++------- include/uapi/linux/cryptouser.h | 38 +++++------ 11 files changed, 133 insertions(+), 141 deletions(-)
crypto/algapi.c +5 -5
@@ -259,13 +259,13 @@ static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)list_add (&larval->alg.cra_list, &crypto_alg_list);#ifdef CONFIG_CRYPTO_STATS -atomic_set (&alg->encrypt_cnt,0 ); -atomic_set (&alg->decrypt_cnt,0 ); +atomic64_set (&alg->encrypt_cnt,0 ); +atomic64_set (&alg->decrypt_cnt,0 );atomic64_set (&alg->encrypt_tlen,0 );atomic64_set (&alg->decrypt_tlen,0 ); -atomic_set (&alg->verify_cnt,0 ); -atomic_set (&alg->cipher_err_cnt,0 ); -atomic_set (&alg->sign_cnt,0 ); +atomic64_set (&alg->verify_cnt,0 ); +atomic64_set (&alg->cipher_err_cnt,0 ); +atomic64_set (&alg->sign_cnt,0 );#endif out:
crypto/crypto_user_stat.c +53 -61
239 lines not on this page · raw message
include/crypto/acompress.h +4 -4
@@ -240,9 +240,9 @@ static inline void crypto_stat_compress(struct acomp_req *req, int ret)struct crypto_acomp *tfm =crypto_acomp_reqtfm (req);if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic_inc (&tfm->base.__crt_alg->compress_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->compress_err_cnt); }else { -atomic_inc (&tfm->base.__crt_alg->compress_cnt); +atomic64_inc (&tfm->base.__crt_alg->compress_cnt);atomic64_add (req->slen, &tfm->base.__crt_alg->compress_tlen); }#endif
@@ -254,9 +254,9 @@ static inline void crypto_stat_decompress(struct acomp_req *req, int ret)struct crypto_acomp *tfm =crypto_acomp_reqtfm (req);if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic_inc (&tfm->base.__crt_alg->compress_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->compress_err_cnt); }else { -atomic_inc (&tfm->base.__crt_alg->decompress_cnt); +atomic64_inc (&tfm->base.__crt_alg->decompress_cnt);atomic64_add (req->slen, &tfm->base.__crt_alg->decompress_tlen); }#endif
include/crypto/aead.h +4 -4
@@ -312,9 +312,9 @@ static inline void crypto_stat_aead_encrypt(struct aead_request *req, int ret)struct crypto_aead *tfm =crypto_aead_reqtfm (req);if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic_inc (&tfm->base.__crt_alg->aead_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->aead_err_cnt); }else { -atomic_inc (&tfm->base.__crt_alg->encrypt_cnt); +atomic64_inc (&tfm->base.__crt_alg->encrypt_cnt);atomic64_add (req->cryptlen, &tfm->base.__crt_alg->encrypt_tlen); }#endif
@@ -326,9 +326,9 @@ static inline void crypto_stat_aead_decrypt(struct aead_request *req, int ret)struct crypto_aead *tfm =crypto_aead_reqtfm (req);if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic_inc (&tfm->base.__crt_alg->aead_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->aead_err_cnt); }else { -atomic_inc (&tfm->base.__crt_alg->decrypt_cnt); +atomic64_inc (&tfm->base.__crt_alg->decrypt_cnt);atomic64_add (req->cryptlen, &tfm->base.__crt_alg->decrypt_tlen); }#endif
include/crypto/akcipher.h +8 -8
@@ -278,9 +278,9 @@ static inline void crypto_stat_akcipher_encrypt(struct akcipher_request *req,struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req);if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic_inc (&tfm->base.__crt_alg->akcipher_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->akcipher_err_cnt); }else { -atomic_inc (&tfm->base.__crt_alg->encrypt_cnt); +atomic64_inc (&tfm->base.__crt_alg->encrypt_cnt);atomic64_add (req->src_len, &tfm->base.__crt_alg->encrypt_tlen); }#endif
@@ -293,9 +293,9 @@ static inline void crypto_stat_akcipher_decrypt(struct akcipher_request *req,struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req);if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic_inc (&tfm->base.__crt_alg->akcipher_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->akcipher_err_cnt); }else { -atomic_inc (&tfm->base.__crt_alg->decrypt_cnt); +atomic64_inc (&tfm->base.__crt_alg->decrypt_cnt);atomic64_add (req->src_len, &tfm->base.__crt_alg->decrypt_tlen); }#endif
@@ -308,9 +308,9 @@ static inline void crypto_stat_akcipher_sign(struct akcipher_request *req,struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req);if (ret && ret != -EINPROGRESS && ret != -EBUSY ) -atomic_inc (&tfm->base.__crt_alg->akcipher_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->akcipher_err_cnt);else -atomic_inc (&tfm->base.__crt_alg->sign_cnt); +atomic64_inc (&tfm->base.__crt_alg->sign_cnt);#endif }
@@ -321,9 +321,9 @@ static inline void crypto_stat_akcipher_verify(struct akcipher_request *req,struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req);if (ret && ret != -EINPROGRESS && ret != -EBUSY ) -atomic_inc (&tfm->base.__crt_alg->akcipher_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->akcipher_err_cnt);else -atomic_inc (&tfm->base.__crt_alg->verify_cnt); +atomic64_inc (&tfm->base.__crt_alg->verify_cnt);#endif }
include/crypto/hash.h +3 -3
@@ -418,7 +418,7 @@ static inline void crypto_stat_ahash_update(struct ahash_request *req, int ret)struct crypto_ahash *tfm =crypto_ahash_reqtfm (req);if (ret && ret != -EINPROGRESS && ret != -EBUSY ) -atomic_inc (&tfm->base.__crt_alg->hash_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->hash_err_cnt);else atomic64_add (req->nbytes, &tfm->base.__crt_alg->hash_tlen);#endif
@@ -430,9 +430,9 @@ static inline void crypto_stat_ahash_final(struct ahash_request *req, int ret)struct crypto_ahash *tfm =crypto_ahash_reqtfm (req);if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic_inc (&tfm->base.__crt_alg->hash_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->hash_err_cnt); }else { -atomic_inc (&tfm->base.__crt_alg->hash_cnt); +atomic64_inc (&tfm->base.__crt_alg->hash_cnt);atomic64_add (req->nbytes, &tfm->base.__crt_alg->hash_tlen); }#endif
include/crypto/kpp.h +6 -6
@@ -272,9 +272,9 @@ static inline void crypto_stat_kpp_set_secret(struct crypto_kpp *tfm, int ret) {#ifdef CONFIG_CRYPTO_STATS if (ret) -atomic_inc (&tfm->base.__crt_alg->kpp_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->kpp_err_cnt);else -atomic_inc (&tfm->base.__crt_alg->setsecret_cnt); +atomic64_inc (&tfm->base.__crt_alg->setsecret_cnt);#endif }
@@ -285,9 +285,9 @@ static inline void crypto_stat_kpp_generate_public_key(struct kpp_request *req,struct crypto_kpp *tfm =crypto_kpp_reqtfm (req);if (ret) -atomic_inc (&tfm->base.__crt_alg->kpp_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->kpp_err_cnt);else -atomic_inc (&tfm->base.__crt_alg->generate_public_key_cnt); +atomic64_inc (&tfm->base.__crt_alg->generate_public_key_cnt);#endif }
@@ -298,9 +298,9 @@ static inline void crypto_stat_kpp_compute_shared_secret(struct kpp_request *reqstruct crypto_kpp *tfm =crypto_kpp_reqtfm (req);if (ret) -atomic_inc (&tfm->base.__crt_alg->kpp_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->kpp_err_cnt);else -atomic_inc (&tfm->base.__crt_alg->compute_shared_secret_cnt); +atomic64_inc (&tfm->base.__crt_alg->compute_shared_secret_cnt);#endif }
include/crypto/rng.h +4 -4
@@ -126,9 +126,9 @@ static inline void crypto_stat_rng_seed(struct crypto_rng *tfm, int ret) {#ifdef CONFIG_CRYPTO_STATS if (ret && ret != -EINPROGRESS && ret != -EBUSY ) -atomic_inc (&tfm->base.__crt_alg->rng_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->rng_err_cnt);else -atomic_inc (&tfm->base.__crt_alg->seed_cnt); +atomic64_inc (&tfm->base.__crt_alg->seed_cnt);#endif }
@@ -137,9 +137,9 @@ static inline void crypto_stat_rng_generate(struct crypto_rng *tfm, {#ifdef CONFIG_CRYPTO_STATS if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic_inc (&tfm->base.__crt_alg->rng_err_cnt); +atomic64_inc (&tfm->base.__crt_alg->rng_err_cnt); }else { -atomic_inc (&tfm->base.__crt_alg->generate_cnt); +atomic64_inc (&tfm->base.__crt_alg->generate_cnt);atomic64_add (dlen, &tfm->base.__crt_alg->generate_tlen); }#endif
include/crypto/skcipher.h +4 -4
@@ -491,9 +491,9 @@ static inline void crypto_stat_skcipher_encrypt(struct skcipher_request *req, {#ifdef CONFIG_CRYPTO_STATS if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic_inc (&alg->cipher_err_cnt); +atomic64_inc (&alg->cipher_err_cnt); }else { -atomic_inc (&alg->encrypt_cnt); +atomic64_inc (&alg->encrypt_cnt);atomic64_add (req->cryptlen, &alg->encrypt_tlen); }#endif
@@ -504,9 +504,9 @@ static inline void crypto_stat_skcipher_decrypt(struct skcipher_request *req, {#ifdef CONFIG_CRYPTO_STATS if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic_inc (&alg->cipher_err_cnt); +atomic64_inc (&alg->cipher_err_cnt); }else { -atomic_inc (&alg->decrypt_cnt); +atomic64_inc (&alg->decrypt_cnt);atomic64_add (req->cryptlen, &alg->decrypt_tlen); }#endif
include/linux/crypto.h +23 -23
81 lines not on this page · raw message
include/uapi/linux/cryptouser.h +19 -19
@@ -79,11 +79,11 @@ struct crypto_user_alg {struct crypto_stat {char type[CRYPTO_MAX_NAME ];union { -__u32 stat_encrypt_cnt; -__u32 stat_compress_cnt; -__u32 stat_generate_cnt; -__u32 stat_hash_cnt; -__u32 stat_setsecret_cnt; +__u64 stat_encrypt_cnt; +__u64 stat_compress_cnt; +__u64 stat_generate_cnt; +__u64 stat_hash_cnt; +__u64 stat_setsecret_cnt; };union {__u64 stat_encrypt_tlen;
@@ -92,29 +92,29 @@ struct crypto_stat {__u64 stat_hash_tlen; };union { -__u32 stat_akcipher_err_cnt; -__u32 stat_cipher_err_cnt; -__u32 stat_compress_err_cnt; -__u32 stat_aead_err_cnt; -__u32 stat_hash_err_cnt; -__u32 stat_rng_err_cnt; -__u32 stat_kpp_err_cnt; +__u64 stat_akcipher_err_cnt; +__u64 stat_cipher_err_cnt; +__u64 stat_compress_err_cnt; +__u64 stat_aead_err_cnt; +__u64 stat_hash_err_cnt; +__u64 stat_rng_err_cnt; +__u64 stat_kpp_err_cnt; };union { -__u32 stat_decrypt_cnt; -__u32 stat_decompress_cnt; -__u32 stat_seed_cnt; -__u32 stat_generate_public_key_cnt; +__u64 stat_decrypt_cnt; +__u64 stat_decompress_cnt; +__u64 stat_seed_cnt; +__u64 stat_generate_public_key_cnt; };union {__u64 stat_decrypt_tlen;__u64 stat_decompress_tlen; };union { -__u32 stat_verify_cnt; -__u32 stat_compute_shared_secret_cnt; +__u64 stat_verify_cnt; +__u64 stat_compute_shared_secret_cnt; }; -__u32 stat_sign_cnt; +__u64 stat_sign_cnt; };struct crypto_report_larval {
Signature · 1 line
2.18.1
Re: [PATCH 2/5] crypto: crypto_user_stat: convert all stats from u32 to u64
Hi Corentin,
Quoted 65 lines · Corentin Labbe
All the 32-bit fields need to be 64-bit. In some cases,
UINT32_MAXcrypto operations can be done in seconds.Reported-by Eric Biggers <[email protected]>Signed-off-by Corentin Labbe <[email protected]>--- crypto/algapi.c | 10 +-- crypto/crypto_user_stat.c | 114 +++++++++++++++----------------- include/crypto/acompress.h | 8 +-- include/crypto/aead.h | 8 +-- include/crypto/akcipher.h | 16 ++--- include/crypto/hash.h | 6 +- include/crypto/kpp.h | 12 ++-- include/crypto/rng.h | 8 +-- include/crypto/skcipher.h | 8 +-- include/linux/crypto.h | 46 ++++++------- include/uapi/linux/cryptouser.h | 38 +++++------ 11 files changed, 133 insertions(+), 141 deletions(-)crypto/algapi.c +5
-5@@ -259,13 +259,13 @@ static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)list_add (&larval->alg.cra_list, &crypto_alg_list);#ifdef CONFIG_CRYPTO_STATS -atomic_set (&alg->encrypt_cnt,0 ); -atomic_set (&alg->decrypt_cnt,0 ); +atomic64_set (&alg->encrypt_cnt,0 ); +atomic64_set (&alg->decrypt_cnt,0 );atomic64_set (&alg->encrypt_tlen,0 );atomic64_set (&alg->decrypt_tlen,0 ); -atomic_set (&alg->verify_cnt,0 ); -atomic_set (&alg->cipher_err_cnt,0 ); -atomic_set (&alg->sign_cnt,0 ); +atomic64_set (&alg->verify_cnt,0 ); +atomic64_set (&alg->cipher_err_cnt,0 ); +atomic64_set (&alg->sign_cnt,0 );#endif out:crypto/crypto_user_stat.c +3
-5@@ -35,22 +35,21 @@ static int crypto_report_aead(struct sk_buff *skb, struct crypto_alg *alg) {struct crypto_stat raead;u64 v64; -u32 v32;memset (&raead,0 ,sizeof (raead));strscpy (raead.type,"aead" ,sizeof (raead.type)); - v32 =atomic_read (&alg->encrypt_cnt); - raead.stat_encrypt_cnt = v32; + v64 =atomic64_read (&alg->encrypt_cnt); + raead.stat_encrypt_cnt = v64; v64 =atomic64_read (&alg->encrypt_tlen); raead.stat_encrypt_tlen = v64; - v32 =atomic_read (&alg->decrypt_cnt); - raead.stat_decrypt_cnt = v32; + v64 =atomic64_read (&alg->decrypt_cnt);
crypto/crypto_user_stat.c static int crypto_report_aead(struct sk_buff *skb, struct crypto_alg *alg) · line 55
Quoted 10 lines
crypto/crypto_user_stat.c +3
-2+ raead.stat_decrypt_cnt = v64; v64 =atomic64_read (&alg->decrypt_tlen); raead.stat_decrypt_tlen = v64; - v32 =atomic_read (&alg->aead_err_cnt); - raead.stat_aead_err_cnt = v32; + v64 =atomic64_read (&alg->aead_err_cnt); + raead.stat_aead_err_cnt = v64;return nla_put (skb,CRYPTOCFGA_STAT_AEAD ,sizeof (raead), &raead); }
Why not assign the result of atomic64_read() directly? I don't see the point of the 'v64' variable.
- Eric
Re: [PATCH 2/5] crypto: crypto_user_stat: convert all stats from u32 to u64
Quoted 76 lines · Eric Biggers
Hi Corentin,
Quoted 73 lines · Corentin Labbe
All the 32-bit fields need to be 64-bit. In some cases,
UINT32_MAXcrypto operations can be done in seconds.Reported-by Eric Biggers <[email protected]>Signed-off-by Corentin Labbe <[email protected]>--- crypto/algapi.c | 10 +-- crypto/crypto_user_stat.c | 114 +++++++++++++++----------------- include/crypto/acompress.h | 8 +-- include/crypto/aead.h | 8 +-- include/crypto/akcipher.h | 16 ++--- include/crypto/hash.h | 6 +- include/crypto/kpp.h | 12 ++-- include/crypto/rng.h | 8 +-- include/crypto/skcipher.h | 8 +-- include/linux/crypto.h | 46 ++++++------- include/uapi/linux/cryptouser.h | 38 +++++------ 11 files changed, 133 insertions(+), 141 deletions(-)crypto/algapi.c +5
-5@@ -259,13 +259,13 @@ static struct crypto_larval *__crypto_register_alg(struct crypto_alg *alg)list_add (&larval->alg.cra_list, &crypto_alg_list);#ifdef CONFIG_CRYPTO_STATS -atomic_set (&alg->encrypt_cnt,0 ); -atomic_set (&alg->decrypt_cnt,0 ); +atomic64_set (&alg->encrypt_cnt,0 ); +atomic64_set (&alg->decrypt_cnt,0 );atomic64_set (&alg->encrypt_tlen,0 );atomic64_set (&alg->decrypt_tlen,0 ); -atomic_set (&alg->verify_cnt,0 ); -atomic_set (&alg->cipher_err_cnt,0 ); -atomic_set (&alg->sign_cnt,0 ); +atomic64_set (&alg->verify_cnt,0 ); +atomic64_set (&alg->cipher_err_cnt,0 ); +atomic64_set (&alg->sign_cnt,0 );#endif out:crypto/crypto_user_stat.c +6
-7@@ -35,22 +35,21 @@ static int crypto_report_aead(struct sk_buff *skb, struct crypto_alg *alg) {struct crypto_stat raead;u64 v64; -u32 v32;memset (&raead,0 ,sizeof (raead));strscpy (raead.type,"aead" ,sizeof (raead.type)); - v32 =atomic_read (&alg->encrypt_cnt); - raead.stat_encrypt_cnt = v32; + v64 =atomic64_read (&alg->encrypt_cnt); + raead.stat_encrypt_cnt = v64; v64 =atomic64_read (&alg->encrypt_tlen); raead.stat_encrypt_tlen = v64; - v32 =atomic_read (&alg->decrypt_cnt); - raead.stat_decrypt_cnt = v32; + v64 =atomic64_read (&alg->decrypt_cnt); + raead.stat_decrypt_cnt = v64; v64 =atomic64_read (&alg->decrypt_tlen); raead.stat_decrypt_tlen = v64; - v32 =atomic_read (&alg->aead_err_cnt); - raead.stat_aead_err_cnt = v32; + v64 =atomic64_read (&alg->aead_err_cnt); + raead.stat_aead_err_cnt = v64;
Quoted 6 lines
crypto/crypto_user_stat.c static int crypto_report_aead(struct sk_buff *skb, struct crypto_alg *alg) · line 55
Quoted 2 lines
crypto/crypto_user_stat.c +0
-0return nla_put (skb,CRYPTOCFGA_STAT_AEAD ,sizeof (raead), &raead); }Why not assign the result of
atomic64_read()directly? I don't see the point of the 'v64' variable.
Yes it will be more compact and easier to read
Thanks
PATCH 3/5 crypto: crypto_user_stat: split user space crypto stat structures
In reply to Corentin LabbeIt is cleaner to have each stat in their own structures.
--- crypto/crypto_user_stat.c | 20 +++---- include/uapi/linux/cryptouser.h | 101 ++++++++++++++++++++------------ 2 files changed, 73 insertions(+), 48 deletions(-)
crypto/crypto_user_stat.c +10 -10
@@ -33,7 +33,7 @@ struct crypto_dump_info {static int crypto_report_aead (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat raead; +struct crypto_stat_aead raead;u64 v64;memset (&raead,0 ,sizeof (raead));
@@ -56,7 +56,7 @@ static int crypto_report_aead(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_cipher (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rcipher; +struct crypto_stat_cipher rcipher;u64 v64;memset (&rcipher,0 ,sizeof (rcipher));
@@ -79,7 +79,7 @@ static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_comp (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rcomp; +struct crypto_stat_compress rcomp;u64 v64;memset (&rcomp,0 ,sizeof (rcomp));
@@ -101,7 +101,7 @@ static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_acomp (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat racomp; +struct crypto_stat_compress racomp;u64 v64;memset (&racomp,0 ,sizeof (racomp));
@@ -123,7 +123,7 @@ static int crypto_report_acomp(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_akcipher (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rakcipher; +struct crypto_stat_akcipher rakcipher;u64 v64;memset (&rakcipher,0 ,sizeof (rakcipher));
@@ -150,7 +150,7 @@ static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_kpp (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rkpp; +struct crypto_stat_kpp rkpp;u64 v;memset (&rkpp,0 ,sizeof (rkpp));
@@ -171,7 +171,7 @@ static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_ahash (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rhash; +struct crypto_stat_hash rhash;u64 v64;memset (&rhash,0 ,sizeof (rhash));
@@ -190,7 +190,7 @@ static int crypto_report_ahash(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_shash (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rhash; +struct crypto_stat_hash rhash;u64 v64;memset (&rhash,0 ,sizeof (rhash));
@@ -209,7 +209,7 @@ static int crypto_report_shash(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_rng (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rrng; +struct crypto_stat_rng rrng;u64 v64;memset (&rrng,0 ,sizeof (rrng));
@@ -248,7 +248,7 @@ static int crypto_reportstat_one(struct crypto_alg *alg,if (nla_put_u32 (skb,CRYPTOCFGA_PRIORITY_VAL , alg->cra_priority)) goto nla_put_failure;if (alg->cra_flags &CRYPTO_ALG_LARVAL ) { -struct crypto_stat rl; +struct crypto_stat_larval rl;memset (&rl,0 ,sizeof (rl));strscpy (rl.type,"larval" ,sizeof (rl.type));
include/uapi/linux/cryptouser.h +63 -38
110 lines not on this page · raw message
Signature · 1 line
2.18.1
Re: [PATCH 3/5] crypto: crypto_user_stat: split user space crypto stat structures
Hi Corentin,
Quoted 220 lines · Corentin Labbe
It is cleaner to have each stat in their own structures.
Signed-off-by Corentin Labbe <[email protected]>--- crypto/crypto_user_stat.c | 20 +++---- include/uapi/linux/cryptouser.h | 101 ++++++++++++++++++++------------ 2 files changed, 73 insertions(+), 48 deletions(-)crypto/crypto_user_stat.c +10
-10@@ -33,7 +33,7 @@ struct crypto_dump_info {static int crypto_report_aead (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat raead; +struct crypto_stat_aead raead;u64 v64;memset (&raead,0 ,sizeof (raead));@@ -56,7 +56,7 @@ static int crypto_report_aead(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_cipher (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rcipher; +struct crypto_stat_cipher rcipher;u64 v64;memset (&rcipher,0 ,sizeof (rcipher));@@ -79,7 +79,7 @@ static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_comp (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rcomp; +struct crypto_stat_compress rcomp;u64 v64;memset (&rcomp,0 ,sizeof (rcomp));@@ -101,7 +101,7 @@ static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_acomp (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat racomp; +struct crypto_stat_compress racomp;u64 v64;memset (&racomp,0 ,sizeof (racomp));@@ -123,7 +123,7 @@ static int crypto_report_acomp(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_akcipher (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rakcipher; +struct crypto_stat_akcipher rakcipher;u64 v64;memset (&rakcipher,0 ,sizeof (rakcipher));@@ -150,7 +150,7 @@ static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_kpp (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rkpp; +struct crypto_stat_kpp rkpp;u64 v;memset (&rkpp,0 ,sizeof (rkpp));@@ -171,7 +171,7 @@ static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_ahash (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rhash; +struct crypto_stat_hash rhash;u64 v64;memset (&rhash,0 ,sizeof (rhash));@@ -190,7 +190,7 @@ static int crypto_report_ahash(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_shash (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rhash; +struct crypto_stat_hash rhash;u64 v64;memset (&rhash,0 ,sizeof (rhash));@@ -209,7 +209,7 @@ static int crypto_report_shash(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_rng (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rrng; +struct crypto_stat_rng rrng;u64 v64;memset (&rrng,0 ,sizeof (rrng));@@ -248,7 +248,7 @@ static int crypto_reportstat_one(struct crypto_alg *alg,if (nla_put_u32 (skb,CRYPTOCFGA_PRIORITY_VAL , alg->cra_priority)) goto nla_put_failure;if (alg->cra_flags &CRYPTO_ALG_LARVAL ) { -struct crypto_stat rl; +struct crypto_stat_larval rl;memset (&rl,0 ,sizeof (rl));strscpy (rl.type,"larval" ,sizeof (rl.type));include/uapi/linux/cryptouser.h +63
-38@@ -76,47 +76,72 @@ struct crypto_user_alg {__u32 cru_flags; }; -struct crypto_stat { -char type[CRYPTO_MAX_NAME ]; -union { -__u64 stat_encrypt_cnt; -__u64 stat_compress_cnt; -__u64 stat_generate_cnt; -__u64 stat_hash_cnt; -__u64 stat_setsecret_cnt; - }; -union { -__u64 stat_encrypt_tlen; -__u64 stat_compress_tlen; -__u64 stat_generate_tlen; -__u64 stat_hash_tlen; - }; -union { -__u64 stat_akcipher_err_cnt; -__u64 stat_cipher_err_cnt; -__u64 stat_compress_err_cnt; -__u64 stat_aead_err_cnt; -__u64 stat_hash_err_cnt; -__u64 stat_rng_err_cnt; -__u64 stat_kpp_err_cnt; - }; -union { -__u64 stat_decrypt_cnt; -__u64 stat_decompress_cnt; -__u64 stat_seed_cnt; -__u64 stat_generate_public_key_cnt; - }; -union { -__u64 stat_decrypt_tlen; -__u64 stat_decompress_tlen; - }; -union { -__u64 stat_verify_cnt; -__u64 stat_compute_shared_secret_cnt; - }; +struct crypto_stat_cipher { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_encrypt_cnt; +__u64 stat_encrypt_tlen; +__u64 stat_decrypt_cnt; +__u64 stat_decrypt_tlen; +__u64 stat_cipher_err_cnt; +}; + +struct crypto_stat_aead { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_encrypt_cnt; +__u64 stat_encrypt_tlen; +__u64 stat_decrypt_cnt; +__u64 stat_decrypt_tlen; +__u64 stat_cipher_err_cnt; +__u64 stat_aead_err_cnt; +}; + +struct crypto_stat_akcipher { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_encrypt_cnt; +__u64 stat_encrypt_tlen; +__u64 stat_decrypt_cnt; +__u64 stat_decrypt_tlen; +__u64 stat_akcipher_err_cnt; +__u64 stat_verify_cnt;__u64 stat_sign_cnt; }; +struct crypto_stat_compress { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_compress_cnt; +__u64 stat_compress_tlen; +__u64 stat_decompress_cnt; +__u64 stat_decompress_tlen; +__u64 stat_compress_err_cnt; +}; + +struct crypto_stat_rng { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_generate_cnt; +__u64 stat_generate_tlen; +__u64 stat_rng_err_cnt; +__u64 stat_seed_cnt; +}; + +struct crypto_stat_hash { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_hash_cnt; +__u64 stat_hash_tlen; +__u64 stat_hash_err_cnt; +}; + +struct crypto_stat_kpp { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_setsecret_cnt; +__u64 stat_kpp_err_cnt; +__u64 stat_generate_public_key_cnt; +__u64 stat_compute_shared_secret_cnt; +}; + +struct crypto_stat_larval { +char type[CRYPTO_MAX_NAME ]; +}; +struct crypto_report_larval {char type[CRYPTO_MAX_NAME ]; };Signature · 1 line
2.18.1
Is there any particular reason this patch only changes the UAPI structures, not the internal structures in 'struct crypto_alg'?
- Eric
Re: [PATCH 3/5] crypto: crypto_user_stat: split user space crypto stat structures
Quoted 218 lines · Eric Biggers
Hi Corentin,
Quoted 215 lines · Corentin Labbe
It is cleaner to have each stat in their own structures.
Signed-off-by Corentin Labbe <[email protected]>--- crypto/crypto_user_stat.c | 20 +++---- include/uapi/linux/cryptouser.h | 101 ++++++++++++++++++++------------ 2 files changed, 73 insertions(+), 48 deletions(-)crypto/crypto_user_stat.c +10
-10@@ -33,7 +33,7 @@ struct crypto_dump_info {static int crypto_report_aead (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat raead; +struct crypto_stat_aead raead;u64 v64;memset (&raead,0 ,sizeof (raead));@@ -56,7 +56,7 @@ static int crypto_report_aead(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_cipher (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rcipher; +struct crypto_stat_cipher rcipher;u64 v64;memset (&rcipher,0 ,sizeof (rcipher));@@ -79,7 +79,7 @@ static int crypto_report_cipher(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_comp (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rcomp; +struct crypto_stat_compress rcomp;u64 v64;memset (&rcomp,0 ,sizeof (rcomp));@@ -101,7 +101,7 @@ static int crypto_report_comp(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_acomp (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat racomp; +struct crypto_stat_compress racomp;u64 v64;memset (&racomp,0 ,sizeof (racomp));@@ -123,7 +123,7 @@ static int crypto_report_acomp(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_akcipher (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rakcipher; +struct crypto_stat_akcipher rakcipher;u64 v64;memset (&rakcipher,0 ,sizeof (rakcipher));@@ -150,7 +150,7 @@ static int crypto_report_akcipher(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_kpp (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rkpp; +struct crypto_stat_kpp rkpp;u64 v;memset (&rkpp,0 ,sizeof (rkpp));@@ -171,7 +171,7 @@ static int crypto_report_kpp(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_ahash (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rhash; +struct crypto_stat_hash rhash;u64 v64;memset (&rhash,0 ,sizeof (rhash));@@ -190,7 +190,7 @@ static int crypto_report_ahash(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_shash (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rhash; +struct crypto_stat_hash rhash;u64 v64;memset (&rhash,0 ,sizeof (rhash));@@ -209,7 +209,7 @@ static int crypto_report_shash(struct sk_buff *skb, struct crypto_alg *alg)static int crypto_report_rng (struct sk_buff *skb,struct crypto_alg *alg) { -struct crypto_stat rrng; +struct crypto_stat_rng rrng;u64 v64;memset (&rrng,0 ,sizeof (rrng));@@ -248,7 +248,7 @@ static int crypto_reportstat_one(struct crypto_alg *alg,if (nla_put_u32 (skb,CRYPTOCFGA_PRIORITY_VAL , alg->cra_priority)) goto nla_put_failure;if (alg->cra_flags &CRYPTO_ALG_LARVAL ) { -struct crypto_stat rl; +struct crypto_stat_larval rl;memset (&rl,0 ,sizeof (rl));strscpy (rl.type,"larval" ,sizeof (rl.type));include/uapi/linux/cryptouser.h +63
-38@@ -76,47 +76,72 @@ struct crypto_user_alg {__u32 cru_flags; }; -struct crypto_stat { -char type[CRYPTO_MAX_NAME ]; -union { -__u64 stat_encrypt_cnt; -__u64 stat_compress_cnt; -__u64 stat_generate_cnt; -__u64 stat_hash_cnt; -__u64 stat_setsecret_cnt; - }; -union { -__u64 stat_encrypt_tlen; -__u64 stat_compress_tlen; -__u64 stat_generate_tlen; -__u64 stat_hash_tlen; - }; -union { -__u64 stat_akcipher_err_cnt; -__u64 stat_cipher_err_cnt; -__u64 stat_compress_err_cnt; -__u64 stat_aead_err_cnt; -__u64 stat_hash_err_cnt; -__u64 stat_rng_err_cnt; -__u64 stat_kpp_err_cnt; - }; -union { -__u64 stat_decrypt_cnt; -__u64 stat_decompress_cnt; -__u64 stat_seed_cnt; -__u64 stat_generate_public_key_cnt; - }; -union { -__u64 stat_decrypt_tlen; -__u64 stat_decompress_tlen; - }; -union { -__u64 stat_verify_cnt; -__u64 stat_compute_shared_secret_cnt; - }; +struct crypto_stat_cipher { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_encrypt_cnt; +__u64 stat_encrypt_tlen; +__u64 stat_decrypt_cnt; +__u64 stat_decrypt_tlen; +__u64 stat_cipher_err_cnt; +}; + +struct crypto_stat_aead { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_encrypt_cnt; +__u64 stat_encrypt_tlen; +__u64 stat_decrypt_cnt; +__u64 stat_decrypt_tlen; +__u64 stat_cipher_err_cnt; +__u64 stat_aead_err_cnt; +}; + +struct crypto_stat_akcipher { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_encrypt_cnt; +__u64 stat_encrypt_tlen; +__u64 stat_decrypt_cnt; +__u64 stat_decrypt_tlen; +__u64 stat_akcipher_err_cnt; +__u64 stat_verify_cnt;__u64 stat_sign_cnt; }; +struct crypto_stat_compress { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_compress_cnt; +__u64 stat_compress_tlen; +__u64 stat_decompress_cnt; +__u64 stat_decompress_tlen; +__u64 stat_compress_err_cnt; +}; + +struct crypto_stat_rng { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_generate_cnt; +__u64 stat_generate_tlen; +__u64 stat_rng_err_cnt; +__u64 stat_seed_cnt; +}; + +struct crypto_stat_hash { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_hash_cnt; +__u64 stat_hash_tlen; +__u64 stat_hash_err_cnt; +}; + +struct crypto_stat_kpp { +char type[CRYPTO_MAX_NAME ]; +__u64 stat_setsecret_cnt; +__u64 stat_kpp_err_cnt; +__u64 stat_generate_public_key_cnt; +__u64 stat_compute_shared_secret_cnt; +}; + +struct crypto_stat_larval { +char type[CRYPTO_MAX_NAME ]; +}; +struct crypto_report_larval {
Quoted 9 lines
Quoted 5 lines
include/uapi/linux/cryptouser.h +0
-0char type[CRYPTO_MAX_NAME ]; };Signature · 1 line
2.18.1Is there any particular reason this patch only changes the UAPI structures, not the internal structures in 'struct
crypto_alg'?
While changing it for user space will be clearly better, I didnt see any benefit for the internal ones. But perhaps it is cleaner to have the sames.
I will do it.
Regards
PATCH 4/5 crypto: tool: getstat: convert user space example to the new crypto_user_stat uapi
In reply to Corentin LabbeThis patch converts the getstat example tool to the recent changes done in crypto_user_stat
- changed all stats to u64
- separated struct stats for each crypto alg
--- tools/crypto/getstat.c | 54 +++++++++++++++++++++--------------------- 1 file changed, 27 insertions(+), 27 deletions(-)
tools/crypto/getstat.c +27 -27
@@ -152,53 +152,53 @@ static int get_stat(const char *drivername)if (tb[CRYPTOCFGA_STAT_HASH ]) {struct rtattr *rta = tb[CRYPTOCFGA_STAT_HASH ]; -struct crypto_stat *rhash = - (struct crypto_stat *)RTA_DATA (rta); -printf ("%s\tHash\n\tHash: %u bytes: %llu\n\tErrors: %u\n" , +struct crypto_stat_hash *rhash = + (struct crypto_stat_hash *)RTA_DATA (rta); +printf ("%s\tHash\n\tHash: %llu bytes: %llu\n\tErrors: %llu\n" , drivername, rhash->stat_hash_cnt, rhash->stat_hash_tlen, rhash->stat_hash_err_cnt); }else if (tb[CRYPTOCFGA_STAT_COMPRESS ]) {struct rtattr *rta = tb[CRYPTOCFGA_STAT_COMPRESS ]; -struct crypto_stat *rblk = - (struct crypto_stat *)RTA_DATA (rta); -printf ("%s\tCompress\n\tCompress: %u bytes: %llu\n\tDecompress: %u bytes: %llu\n\tErrors: %u\n" , +struct crypto_stat_compress *rblk = + (struct crypto_stat_compress *)RTA_DATA (rta); +printf ("%s\tCompress\n\tCompress: %llu bytes: %llu\n\tDecompress: %llu bytes: %llu\n\tErrors: %llu\n" , drivername, rblk->stat_compress_cnt, rblk->stat_compress_tlen, rblk->stat_decompress_cnt, rblk->stat_decompress_tlen, rblk->stat_compress_err_cnt); }else if (tb[CRYPTOCFGA_STAT_ACOMP ]) {struct rtattr *rta = tb[CRYPTOCFGA_STAT_ACOMP ]; -struct crypto_stat *rcomp = - (struct crypto_stat *)RTA_DATA (rta); -printf ("%s\tACompress\n\tCompress: %u bytes: %llu\n\tDecompress: %u bytes: %llu\n\tErrors: %u\n" , +struct crypto_stat_compress *rcomp = + (struct crypto_stat_compress *)RTA_DATA (rta); +printf ("%s\tACompress\n\tCompress: %llu bytes: %llu\n\tDecompress: %llu bytes: %llu\n\tErrors: %llu\n" , drivername, rcomp->stat_compress_cnt, rcomp->stat_compress_tlen, rcomp->stat_decompress_cnt, rcomp->stat_decompress_tlen, rcomp->stat_compress_err_cnt); }else if (tb[CRYPTOCFGA_STAT_AEAD ]) {struct rtattr *rta = tb[CRYPTOCFGA_STAT_AEAD ]; -struct crypto_stat *raead = - (struct crypto_stat *)RTA_DATA (rta); -printf ("%s\tAEAD\n\tEncrypt: %u bytes: %llu\n\tDecrypt: %u bytes: %llu\n\tErrors: %u\n" , +struct crypto_stat_aead *raead = + (struct crypto_stat_aead *)RTA_DATA (rta); +printf ("%s\tAEAD\n\tEncrypt: %llu bytes: %llu\n\tDecrypt: %llu bytes: %llu\n\tErrors: %llu\n" , drivername, raead->stat_encrypt_cnt, raead->stat_encrypt_tlen, raead->stat_decrypt_cnt, raead->stat_decrypt_tlen, raead->stat_aead_err_cnt); }else if (tb[CRYPTOCFGA_STAT_BLKCIPHER ]) {struct rtattr *rta = tb[CRYPTOCFGA_STAT_BLKCIPHER ]; -struct crypto_stat *rblk = - (struct crypto_stat *)RTA_DATA (rta); -printf ("%s\tCipher\n\tEncrypt: %u bytes: %llu\n\tDecrypt: %u bytes: %llu\n\tErrors: %u\n" , +struct crypto_stat_cipher *rblk = + (struct crypto_stat_cipher *)RTA_DATA (rta); +printf ("%s\tCipher\n\tEncrypt: %llu bytes: %llu\n\tDecrypt: %llu bytes: %llu\n\tErrors: %llu\n" , drivername, rblk->stat_encrypt_cnt, rblk->stat_encrypt_tlen, rblk->stat_decrypt_cnt, rblk->stat_decrypt_tlen, rblk->stat_cipher_err_cnt); }else if (tb[CRYPTOCFGA_STAT_AKCIPHER ]) {struct rtattr *rta = tb[CRYPTOCFGA_STAT_AKCIPHER ]; -struct crypto_stat *rblk = - (struct crypto_stat *)RTA_DATA (rta); - printf("%s\tAkcipher\n\tEncrypt: %u bytes: %llu\n\tDecrypt: %u bytes: %llu\n\tSign: %u\n\tVerify: %u\n\tErrors: %u\n", +struct crypto_stat_akcipher *rblk = + (struct crypto_stat_akcipher *)RTA_DATA (rta); + printf("%s\tAkcipher\n\tEncrypt: %llu bytes: %llu\n\tDecrypt: %llu bytes: %llu\n\tSign: %llu\n\tVerify: %llu\n\tErrors: %llu\n", drivername, rblk->stat_encrypt_cnt, rblk->stat_encrypt_tlen, rblk->stat_decrypt_cnt, rblk->stat_decrypt_tlen,
@@ -206,27 +206,27 @@ static int get_stat(const char *drivername) rblk->stat_akcipher_err_cnt); }else if (tb[CRYPTOCFGA_STAT_CIPHER ]) {struct rtattr *rta = tb[CRYPTOCFGA_STAT_CIPHER ]; -struct crypto_stat *rblk = - (struct crypto_stat *)RTA_DATA (rta); -printf ("%s\tcipher\n\tEncrypt: %u bytes: %llu\n\tDecrypt: %u bytes: %llu\n\tErrors: %u\n" , +struct crypto_stat_cipher *rblk = + (struct crypto_stat_cipher *)RTA_DATA (rta); +printf ("%s\tcipher\n\tEncrypt: %llu bytes: %llu\n\tDecrypt: %llu bytes: %llu\n\tErrors: %llu\n" , drivername, rblk->stat_encrypt_cnt, rblk->stat_encrypt_tlen, rblk->stat_decrypt_cnt, rblk->stat_decrypt_tlen, rblk->stat_cipher_err_cnt); }else if (tb[CRYPTOCFGA_STAT_RNG ]) {struct rtattr *rta = tb[CRYPTOCFGA_STAT_RNG ]; -struct crypto_stat *rrng = - (struct crypto_stat *)RTA_DATA (rta); -printf ("%s\tRNG\n\tSeed: %u\n\tGenerate: %u bytes: %llu\n\tErrors: %u\n" , +struct crypto_stat_rng *rrng = + (struct crypto_stat_rng *)RTA_DATA (rta); +printf ("%s\tRNG\n\tSeed: %llu\n\tGenerate: %llu bytes: %llu\n\tErrors: %llu\n" , drivername, rrng->stat_seed_cnt, rrng->stat_generate_cnt, rrng->stat_generate_tlen, rrng->stat_rng_err_cnt); }else if (tb[CRYPTOCFGA_STAT_KPP ]) {struct rtattr *rta = tb[CRYPTOCFGA_STAT_KPP ]; -struct crypto_stat *rkpp = - (struct crypto_stat *)RTA_DATA (rta); - printf("%s\tKPP\n\tSetsecret: %u\n\tGenerate public key: %u\n\tCompute_shared_secret: %u\n\tErrors: %u\n", +struct crypto_stat_kpp *rkpp = + (struct crypto_stat_kpp *)RTA_DATA (rta); + printf("%s\tKPP\n\tSetsecret: %llu\n\tGenerate public key: %llu\n\tCompute_shared_secret: %llu\n\tErrors: %llu\n", drivername, rkpp->stat_setsecret_cnt, rkpp->stat_generate_public_key_cnt,
Signature · 1 line
2.18.1
PATCH 5/5 crypto: crypto_user_stat: fix use_after_free of struct xxx_request
In reply to Corentin LabbeAll crypto_stats functions use the struct xxx_request for feeding stats, but in some case this structure could already be freed.
For fixing this, the needed parameters (len and alg) will be stored before the request being executed.
--- crypto/ahash.c | 14 ++++++++-- include/crypto/acompress.h | 30 ++++++++++---------- include/crypto/aead.h | 30 ++++++++++---------- include/crypto/akcipher.h | 56 ++++++++++++++++++-------------------- include/crypto/hash.h | 25 +++++++++-------- include/crypto/kpp.h | 22 +++++++-------- include/crypto/skcipher.h | 16 +++++++---- include/linux/crypto.h | 34 +++++++++++------------ 8 files changed, 118 insertions(+), 109 deletions(-)
crypto/ahash.c +11 -3
@@ -364,20 +364,26 @@ static int crypto_ahash_op(struct ahash_request *req,int crypto_ahash_final (struct ahash_request *req) { +struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int nbytes = req->nbytes;int ret; ret =crypto_ahash_op (req,crypto_ahash_reqtfm (req)->final); -crypto_stat_ahash_final (req, ret); +crypto_stat_ahash_final (nbytes, ret, alg);return ret; }EXPORT_SYMBOL_GPL (crypto_ahash_final);int crypto_ahash_finup (struct ahash_request *req) { +struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int nbytes = req->nbytes;int ret; ret =crypto_ahash_op (req,crypto_ahash_reqtfm (req)->finup); -crypto_stat_ahash_final (req, ret); +crypto_stat_ahash_final (nbytes, ret, alg);return ret; }EXPORT_SYMBOL_GPL (crypto_ahash_finup);
@@ -385,13 +391,15 @@ EXPORT_SYMBOL_GPL(crypto_ahash_finup);int crypto_ahash_digest (struct ahash_request *req) {struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int nbytes = req->nbytes;int ret;if (crypto_ahash_get_flags (tfm) &CRYPTO_TFM_NEED_KEY ) ret = -ENOKEY ;else ret =crypto_ahash_op (req, tfm->digest); -crypto_stat_ahash_final (req, ret); +crypto_stat_ahash_final (nbytes, ret, alg);return ret; }EXPORT_SYMBOL_GPL (crypto_ahash_digest);
include/crypto/acompress.h +16 -14
@@ -234,30 +234,28 @@ static inline void acomp_request_set_params(struct acomp_req *req, req->flags |=CRYPTO_ACOMP_ALLOC_OUTPUT ; } -static inline void crypto_stat_compress (struct acomp_req *req,int ret) +static inline void crypto_stat_compress (unsigned int slen,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_acomp *tfm =crypto_acomp_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&tfm->base.__crt_alg->compress_err_cnt); +atomic64_inc (&alg->compress_err_cnt); }else { -atomic64_inc (&tfm->base.__crt_alg->compress_cnt); -atomic64_add (req->slen, &tfm->base.__crt_alg->compress_tlen); +atomic64_inc (&alg->compress_cnt); +atomic64_add (slen, &alg->compress_tlen); }#endif } -static inline void crypto_stat_decompress (struct acomp_req *req,int ret) +static inline void crypto_stat_decompress (unsigned int slen,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_acomp *tfm =crypto_acomp_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&tfm->base.__crt_alg->compress_err_cnt); +atomic64_inc (&alg->compress_err_cnt); }else { -atomic64_inc (&tfm->base.__crt_alg->decompress_cnt); -atomic64_add (req->slen, &tfm->base.__crt_alg->decompress_tlen); +atomic64_inc (&alg->decompress_cnt); +atomic64_add (slen, &alg->decompress_tlen); }#endif }
@@ -274,10 +272,12 @@ static inline void crypto_stat_decompress(struct acomp_req *req, int ret)static inline int crypto_acomp_compress (struct acomp_req *req) {struct crypto_acomp *tfm =crypto_acomp_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int slen = req->slen;int ret; ret = tfm->compress (req); -crypto_stat_compress (req, ret); +crypto_stat_compress (slen, ret, alg);return ret; }
@@ -293,10 +293,12 @@ static inline int crypto_acomp_compress(struct acomp_req *req)static inline int crypto_acomp_decompress (struct acomp_req *req) {struct crypto_acomp *tfm =crypto_acomp_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int slen = req->slen;int ret; ret = tfm->decompress (req); -crypto_stat_decompress (req, ret); +crypto_stat_decompress (slen, ret, alg);return ret; }
include/crypto/aead.h +16 -14
@@ -306,30 +306,28 @@ static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)return __crypto_aead_cast (req->base.tfm); } -static inline void crypto_stat_aead_encrypt (struct aead_request *req,int ret) +static inline void crypto_stat_aead_encrypt (unsigned int cryptlen, +struct crypto_alg *alg,int ret) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_aead *tfm =crypto_aead_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&tfm->base.__crt_alg->aead_err_cnt); +atomic64_inc (&alg->aead_err_cnt); }else { -atomic64_inc (&tfm->base.__crt_alg->encrypt_cnt); -atomic64_add (req->cryptlen, &tfm->base.__crt_alg->encrypt_tlen); +atomic64_inc (&alg->encrypt_cnt); +atomic64_add (cryptlen, &alg->encrypt_tlen); }#endif } -static inline void crypto_stat_aead_decrypt (struct aead_request *req,int ret) +static inline void crypto_stat_aead_decrypt (unsigned int cryptlen, +struct crypto_alg *alg,int ret) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_aead *tfm =crypto_aead_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&tfm->base.__crt_alg->aead_err_cnt); +atomic64_inc (&alg->aead_err_cnt); }else { -atomic64_inc (&tfm->base.__crt_alg->decrypt_cnt); -atomic64_add (req->cryptlen, &tfm->base.__crt_alg->decrypt_tlen); +atomic64_inc (&alg->decrypt_cnt); +atomic64_add (cryptlen, &alg->decrypt_tlen); }#endif }
@@ -356,13 +354,15 @@ static inline void crypto_stat_aead_decrypt(struct aead_request *req, int ret)static inline int crypto_aead_encrypt (struct aead_request *req) {struct crypto_aead *aead =crypto_aead_reqtfm (req); +struct crypto_alg *alg = aead->base.__crt_alg; +unsigned int cryptlen = req->cryptlen;int ret;if (crypto_aead_get_flags (aead) &CRYPTO_TFM_NEED_KEY ) ret = -ENOKEY ;else ret =crypto_aead_alg (aead)->encrypt (req); -crypto_stat_aead_encrypt (req, ret); +crypto_stat_aead_encrypt (cryptlen, alg, ret);return ret; }
@@ -391,6 +391,8 @@ static inline int crypto_aead_encrypt(struct aead_request *req)static inline int crypto_aead_decrypt (struct aead_request *req) {struct crypto_aead *aead =crypto_aead_reqtfm (req); +struct crypto_alg *alg = aead->base.__crt_alg; +unsigned int cryptlen = req->cryptlen;int ret;if (crypto_aead_get_flags (aead) &CRYPTO_TFM_NEED_KEY )
@@ -399,7 +401,7 @@ static inline int crypto_aead_decrypt(struct aead_request *req) ret = -EINVAL ; else ret =crypto_aead_alg (aead)->decrypt (req); -crypto_stat_aead_decrypt (req, ret); +crypto_stat_aead_decrypt (cryptlen, alg, ret);return ret; }
include/crypto/akcipher.h +26 -30
125 lines not on this page · raw message
include/crypto/hash.h +13 -12
@@ -412,28 +412,26 @@ static inline void *ahash_request_ctx(struct ahash_request *req)int crypto_ahash_setkey (struct crypto_ahash *tfm,const u8 *key,unsigned int keylen); -static inline void crypto_stat_ahash_update (struct ahash_request *req,int ret) +static inline void crypto_stat_ahash_update (unsigned int nbytes,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) -atomic64_inc (&tfm->base.__crt_alg->hash_err_cnt); +atomic64_inc (&alg->hash_err_cnt);else -atomic64_add (req->nbytes, &tfm->base.__crt_alg->hash_tlen); +atomic64_add (nbytes, &alg->hash_tlen);#endif } -static inline void crypto_stat_ahash_final (struct ahash_request *req,int ret) +static inline void crypto_stat_ahash_final (unsigned int nbytes,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&tfm->base.__crt_alg->hash_err_cnt); +atomic64_inc (&alg->hash_err_cnt); }else { -atomic64_inc (&tfm->base.__crt_alg->hash_cnt); -atomic64_add (req->nbytes, &tfm->base.__crt_alg->hash_tlen); +atomic64_inc (&alg->hash_cnt); +atomic64_add (nbytes, &alg->hash_tlen); }#endif }
@@ -552,10 +550,13 @@ static inline int crypto_ahash_init(struct ahash_request *req)*/ static inline int crypto_ahash_update (struct ahash_request *req) { +struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int nbytes = req->nbytes;int ret; ret =crypto_ahash_reqtfm (req)->update (req); -crypto_stat_ahash_update (req, ret); +crypto_stat_ahash_update (nbytes, ret, alg);return ret; }
include/crypto/kpp.h +10 -12
@@ -278,29 +278,25 @@ static inline void crypto_stat_kpp_set_secret(struct crypto_kpp *tfm, int ret)#endif } -static inline void crypto_stat_kpp_generate_public_key (struct kpp_request *req, +static inline void crypto_stat_kpp_generate_public_key (struct crypto_alg *alg,int ret) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_kpp *tfm =crypto_kpp_reqtfm (req); -if (ret) -atomic64_inc (&tfm->base.__crt_alg->kpp_err_cnt); +atomic64_inc (&alg->kpp_err_cnt);else -atomic64_inc (&tfm->base.__crt_alg->generate_public_key_cnt); +atomic64_inc (&alg->generate_public_key_cnt);#endif } -static inline void crypto_stat_kpp_compute_shared_secret (struct kpp_request *req, +static inline void crypto_stat_kpp_compute_shared_secret (struct crypto_alg *alg,int ret) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_kpp *tfm =crypto_kpp_reqtfm (req); -if (ret) -atomic64_inc (&tfm->base.__crt_alg->kpp_err_cnt); +atomic64_inc (&alg->kpp_err_cnt);else -atomic64_inc (&tfm->base.__crt_alg->compute_shared_secret_cnt); +atomic64_inc (&alg->compute_shared_secret_cnt);#endif }
@@ -347,10 +343,11 @@ static inline int crypto_kpp_generate_public_key(struct kpp_request *req) {struct crypto_kpp *tfm =crypto_kpp_reqtfm (req);struct kpp_alg *alg =crypto_kpp_alg (tfm); +struct crypto_alg *calg = tfm->base.__crt_alg;int ret; ret = alg->generate_public_key (req); -crypto_stat_kpp_generate_public_key (req, ret); +crypto_stat_kpp_generate_public_key (calg, ret);return ret; }
@@ -368,10 +365,11 @@ static inline int crypto_kpp_compute_shared_secret(struct kpp_request *req) {struct crypto_kpp *tfm =crypto_kpp_reqtfm (req);struct kpp_alg *alg =crypto_kpp_alg (tfm); +struct crypto_alg *calg = tfm->base.__crt_alg;int ret; ret = alg->compute_shared_secret (req); -crypto_stat_kpp_compute_shared_secret (req, ret); +crypto_stat_kpp_compute_shared_secret (calg, ret);return ret; }
include/crypto/skcipher.h +10 -6
@@ -486,7 +486,7 @@ static inline struct crypto_sync_skcipher *crypto_sync_skcipher_reqtfm( return container_of(tfm, struct crypto_sync_skcipher, base); } -static inline void crypto_stat_skcipher_encrypt (struct skcipher_request *req, +static inline void crypto_stat_skcipher_encrypt (unsigned int cryptlen,int ret,struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS
@@ -494,12 +494,12 @@ static inline void crypto_stat_skcipher_encrypt(struct skcipher_request *req, atomic64_inc(&alg->cipher_err_cnt); }else {atomic64_inc (&alg->encrypt_cnt); -atomic64_add (req->cryptlen, &alg->encrypt_tlen); +atomic64_add (cryptlen, &alg->encrypt_tlen); }#endif } -static inline void crypto_stat_skcipher_decrypt (struct skcipher_request *req, +static inline void crypto_stat_skcipher_decrypt (unsigned int cryptlen,int ret,struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS
@@ -507,7 +507,7 @@ static inline void crypto_stat_skcipher_decrypt(struct skcipher_request *req, atomic64_inc(&alg->cipher_err_cnt); }else {atomic64_inc (&alg->decrypt_cnt); -atomic64_add (req->cryptlen, &alg->decrypt_tlen); +atomic64_add (cryptlen, &alg->decrypt_tlen); }#endif }
@@ -526,13 +526,15 @@ static inline void crypto_stat_skcipher_decrypt(struct skcipher_request *req,static inline int crypto_skcipher_encrypt (struct skcipher_request *req) {struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int cryptlen = req->cryptlen;int ret;if (crypto_skcipher_get_flags (tfm) &CRYPTO_TFM_NEED_KEY ) ret = -ENOKEY ;else ret = tfm->encrypt (req); -crypto_stat_skcipher_encrypt (req, ret, tfm->base.__crt_alg); +crypto_stat_skcipher_encrypt (cryptlen, ret, alg);return ret; }
@@ -550,13 +552,15 @@ static inline int crypto_skcipher_encrypt(struct skcipher_request *req)static inline int crypto_skcipher_decrypt (struct skcipher_request *req) {struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int cryptlen = req->cryptlen;int ret;if (crypto_skcipher_get_flags (tfm) &CRYPTO_TFM_NEED_KEY ) ret = -ENOKEY ;else ret = tfm->decrypt (req); -crypto_stat_skcipher_decrypt (req, ret, tfm->base.__crt_alg); +crypto_stat_skcipher_decrypt (cryptlen, ret, alg);return ret; }
include/linux/crypto.h +16 -18
@@ -975,34 +975,28 @@ static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(return __crypto_ablkcipher_cast (req->base.tfm); } -static inline void crypto_stat_ablkcipher_encrypt (struct ablkcipher_request *req, -int ret) +static inline void crypto_stat_ablkcipher_encrypt (unsigned int nbytes,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct ablkcipher_tfm *crt = -crypto_ablkcipher_crt (crypto_ablkcipher_reqtfm (req)); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&crt->base->base.__crt_alg->cipher_err_cnt); +atomic64_inc (&alg->cipher_err_cnt); }else { -atomic64_inc (&crt->base->base.__crt_alg->encrypt_cnt); -atomic64_add (req->nbytes, &crt->base->base.__crt_alg->encrypt_tlen); +atomic64_inc (&alg->encrypt_cnt); +atomic64_add (nbytes, &alg->encrypt_tlen); }#endif } -static inline void crypto_stat_ablkcipher_decrypt (struct ablkcipher_request *req, -int ret) +static inline void crypto_stat_ablkcipher_decrypt (unsigned int nbytes,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct ablkcipher_tfm *crt = -crypto_ablkcipher_crt (crypto_ablkcipher_reqtfm (req)); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&crt->base->base.__crt_alg->cipher_err_cnt); +atomic64_inc (&alg->cipher_err_cnt); }else { -atomic64_inc (&crt->base->base.__crt_alg->decrypt_cnt); -atomic64_add (req->nbytes, &crt->base->base.__crt_alg->decrypt_tlen); +atomic64_inc (&alg->decrypt_cnt); +atomic64_add (nbytes, &alg->decrypt_tlen); }#endif }
@@ -1022,10 +1016,12 @@ static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req) {struct ablkcipher_tfm *crt =crypto_ablkcipher_crt (crypto_ablkcipher_reqtfm (req)); +struct crypto_alg *alg = crt->base->base.__crt_alg; +unsigned int nbytes = req->nbytes;int ret; ret = crt->encrypt (req); -crypto_stat_ablkcipher_encrypt (req, ret); +crypto_stat_ablkcipher_encrypt (nbytes, ret, alg);return ret; }
@@ -1044,10 +1040,12 @@ static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req) {struct ablkcipher_tfm *crt =crypto_ablkcipher_crt (crypto_ablkcipher_reqtfm (req)); +struct crypto_alg *alg = crt->base->base.__crt_alg; +unsigned int nbytes = req->nbytes;int ret; ret = crt->decrypt (req); -crypto_stat_ablkcipher_decrypt (req, ret); +crypto_stat_ablkcipher_decrypt (nbytes, ret, alg);return ret; }
Signature · 1 line
2.18.1
Re: [PATCH 5/5] crypto: crypto_user_stat: fix use_after_free of struct xxx_request
Hi Corentin,
Quoted 29 lines · Corentin Labbe
All
crypto_statsfunctions use the structxxx_requestfor feeding stats, but in some case this structure could already be freed.For fixing this, the needed parameters (len and alg) will be stored before the request being executed.
Fixes cac5818c25d0 ("crypto: user - Implement a generic crypto statistics")Reported-by syzbot <[email protected]>Signed-off-by Corentin Labbe <[email protected]>--- crypto/ahash.c | 14 ++++++++-- include/crypto/acompress.h | 30 ++++++++++---------- include/crypto/aead.h | 30 ++++++++++---------- include/crypto/akcipher.h | 56 ++++++++++++++++++-------------------- include/crypto/hash.h | 25 +++++++++-------- include/crypto/kpp.h | 22 +++++++-------- include/crypto/skcipher.h | 16 +++++++---- include/linux/crypto.h | 34 +++++++++++------------ 8 files changed, 118 insertions(+), 109 deletions(-)crypto/ahash.c +1
-0@@ -364,20 +364,26 @@ static int crypto_ahash_op(struct ahash_request *req,int crypto_ahash_final (struct ahash_request *req) { +struct crypto_ahash *tfm =crypto_ahash_reqtfm (req);
crypto/ahash.c static int crypto_ahash_op(struct ahash_request *req, · line 376
Quoted 10 lines
crypto/ahash.c +3
-1+struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int nbytes = req->nbytes;int ret; ret =crypto_ahash_op (req,crypto_ahash_reqtfm (req)->final); -crypto_stat_ahash_final (req, ret); +crypto_stat_ahash_final (nbytes, ret, alg);return ret; }EXPORT_SYMBOL_GPL (crypto_ahash_final);
I'm not confident this is enough. If the request is being processed asynchronously, the completion callback could be used to signal another thread to go ahead and free the resources, including not only the request but also the 'tfm', which could even result in the last reference to the 'alg' being dropped and therefore the 'alg' being freed. If I'm wrong, then please explain why :-)
Note: I'd also prefer a solution that is more obviously zero-overhead in the !CONFIG_CRYPTO_STATS case.
- Eric
Quoted 592 lines
crypto/ahash.c +7
-2int crypto_ahash_finup (struct ahash_request *req) { +struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int nbytes = req->nbytes;int ret; ret =crypto_ahash_op (req,crypto_ahash_reqtfm (req)->finup); -crypto_stat_ahash_final (req, ret); +crypto_stat_ahash_final (nbytes, ret, alg);return ret; }EXPORT_SYMBOL_GPL (crypto_ahash_finup);@@ -385,13 +391,15 @@ EXPORT_SYMBOL_GPL(crypto_ahash_finup);int crypto_ahash_digest (struct ahash_request *req) {struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int nbytes = req->nbytes;int ret;if (crypto_ahash_get_flags (tfm) &CRYPTO_TFM_NEED_KEY ) ret = -ENOKEY ;else ret =crypto_ahash_op (req, tfm->digest); -crypto_stat_ahash_final (req, ret); +crypto_stat_ahash_final (nbytes, ret, alg);return ret; }EXPORT_SYMBOL_GPL (crypto_ahash_digest);include/crypto/acompress.h +16
-14@@ -234,30 +234,28 @@ static inline void acomp_request_set_params(struct acomp_req *req, req->flags |=CRYPTO_ACOMP_ALLOC_OUTPUT ; } -static inline void crypto_stat_compress (struct acomp_req *req,int ret) +static inline void crypto_stat_compress (unsigned int slen,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_acomp *tfm =crypto_acomp_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&tfm->base.__crt_alg->compress_err_cnt); +atomic64_inc (&alg->compress_err_cnt); }else { -atomic64_inc (&tfm->base.__crt_alg->compress_cnt); -atomic64_add (req->slen, &tfm->base.__crt_alg->compress_tlen); +atomic64_inc (&alg->compress_cnt); +atomic64_add (slen, &alg->compress_tlen); }#endif } -static inline void crypto_stat_decompress (struct acomp_req *req,int ret) +static inline void crypto_stat_decompress (unsigned int slen,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_acomp *tfm =crypto_acomp_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&tfm->base.__crt_alg->compress_err_cnt); +atomic64_inc (&alg->compress_err_cnt); }else { -atomic64_inc (&tfm->base.__crt_alg->decompress_cnt); -atomic64_add (req->slen, &tfm->base.__crt_alg->decompress_tlen); +atomic64_inc (&alg->decompress_cnt); +atomic64_add (slen, &alg->decompress_tlen); }#endif }@@ -274,10 +272,12 @@ static inline void crypto_stat_decompress(struct acomp_req *req, int ret)static inline int crypto_acomp_compress (struct acomp_req *req) {struct crypto_acomp *tfm =crypto_acomp_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int slen = req->slen;int ret; ret = tfm->compress (req); -crypto_stat_compress (req, ret); +crypto_stat_compress (slen, ret, alg);return ret; }@@ -293,10 +293,12 @@ static inline int crypto_acomp_compress(struct acomp_req *req)static inline int crypto_acomp_decompress (struct acomp_req *req) {struct crypto_acomp *tfm =crypto_acomp_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int slen = req->slen;int ret; ret = tfm->decompress (req); -crypto_stat_decompress (req, ret); +crypto_stat_decompress (slen, ret, alg);return ret; }include/crypto/aead.h +16
-14@@ -306,30 +306,28 @@ static inline struct crypto_aead *crypto_aead_reqtfm(struct aead_request *req)return __crypto_aead_cast (req->base.tfm); } -static inline void crypto_stat_aead_encrypt (struct aead_request *req,int ret) +static inline void crypto_stat_aead_encrypt (unsigned int cryptlen, +struct crypto_alg *alg,int ret) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_aead *tfm =crypto_aead_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&tfm->base.__crt_alg->aead_err_cnt); +atomic64_inc (&alg->aead_err_cnt); }else { -atomic64_inc (&tfm->base.__crt_alg->encrypt_cnt); -atomic64_add (req->cryptlen, &tfm->base.__crt_alg->encrypt_tlen); +atomic64_inc (&alg->encrypt_cnt); +atomic64_add (cryptlen, &alg->encrypt_tlen); }#endif } -static inline void crypto_stat_aead_decrypt (struct aead_request *req,int ret) +static inline void crypto_stat_aead_decrypt (unsigned int cryptlen, +struct crypto_alg *alg,int ret) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_aead *tfm =crypto_aead_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&tfm->base.__crt_alg->aead_err_cnt); +atomic64_inc (&alg->aead_err_cnt); }else { -atomic64_inc (&tfm->base.__crt_alg->decrypt_cnt); -atomic64_add (req->cryptlen, &tfm->base.__crt_alg->decrypt_tlen); +atomic64_inc (&alg->decrypt_cnt); +atomic64_add (cryptlen, &alg->decrypt_tlen); }#endif }@@ -356,13 +354,15 @@ static inline void crypto_stat_aead_decrypt(struct aead_request *req, int ret)static inline int crypto_aead_encrypt (struct aead_request *req) {struct crypto_aead *aead =crypto_aead_reqtfm (req); +struct crypto_alg *alg = aead->base.__crt_alg; +unsigned int cryptlen = req->cryptlen;int ret;if (crypto_aead_get_flags (aead) &CRYPTO_TFM_NEED_KEY ) ret = -ENOKEY ;else ret =crypto_aead_alg (aead)->encrypt (req); -crypto_stat_aead_encrypt (req, ret); +crypto_stat_aead_encrypt (cryptlen, alg, ret);return ret; }@@ -391,6 +391,8 @@ static inline int crypto_aead_encrypt(struct aead_request *req)static inline int crypto_aead_decrypt (struct aead_request *req) {struct crypto_aead *aead =crypto_aead_reqtfm (req); +struct crypto_alg *alg = aead->base.__crt_alg; +unsigned int cryptlen = req->cryptlen;int ret;if (crypto_aead_get_flags (aead) &CRYPTO_TFM_NEED_KEY )@@ -399,7 +401,7 @@ static inline int crypto_aead_decrypt(struct aead_request *req) ret = -EINVAL ; else ret =crypto_aead_alg (aead)->decrypt (req); -crypto_stat_aead_decrypt (req, ret); +crypto_stat_aead_decrypt (cryptlen, alg, ret);return ret; }include/crypto/akcipher.h +26
-30@@ -271,59 +271,49 @@ static inline unsigned int crypto_akcipher_maxsize(struct crypto_akcipher *tfm)return alg->max_size (tfm); } -static inline void crypto_stat_akcipher_encrypt (struct akcipher_request *req, -int ret) +static inline void crypto_stat_akcipher_encrypt (unsigned int src_len,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&tfm->base.__crt_alg->akcipher_err_cnt); +atomic64_inc (&alg->akcipher_err_cnt); }else { -atomic64_inc (&tfm->base.__crt_alg->encrypt_cnt); -atomic64_add (req->src_len, &tfm->base.__crt_alg->encrypt_tlen); +atomic64_inc (&alg->encrypt_cnt); +atomic64_add (src_len, &alg->encrypt_tlen); }#endif } -static inline void crypto_stat_akcipher_decrypt (struct akcipher_request *req, -int ret) +static inline void crypto_stat_akcipher_decrypt (unsigned int src_len,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&tfm->base.__crt_alg->akcipher_err_cnt); +atomic64_inc (&alg->akcipher_err_cnt); }else { -atomic64_inc (&tfm->base.__crt_alg->decrypt_cnt); -atomic64_add (req->src_len, &tfm->base.__crt_alg->decrypt_tlen); +atomic64_inc (&alg->decrypt_cnt); +atomic64_add (src_len, &alg->decrypt_tlen); }#endif } -static inline void crypto_stat_akcipher_sign (struct akcipher_request *req, -int ret) +static inline void crypto_stat_akcipher_sign (int ret,struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) -atomic64_inc (&tfm->base.__crt_alg->akcipher_err_cnt); +atomic64_inc (&alg->akcipher_err_cnt);else -atomic64_inc (&tfm->base.__crt_alg->sign_cnt); +atomic64_inc (&alg->sign_cnt);#endif } -static inline void crypto_stat_akcipher_verify (struct akcipher_request *req, -int ret) +static inline void crypto_stat_akcipher_verify (int ret,struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) -atomic64_inc (&tfm->base.__crt_alg->akcipher_err_cnt); +atomic64_inc (&alg->akcipher_err_cnt);else -atomic64_inc (&tfm->base.__crt_alg->verify_cnt); +atomic64_inc (&alg->verify_cnt);#endif }@@ -341,10 +331,12 @@ static inline int crypto_akcipher_encrypt(struct akcipher_request *req) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req);struct akcipher_alg *alg =crypto_akcipher_alg (tfm); +struct crypto_alg *calg = tfm->base.__crt_alg; +unsigned int src_len = req->src_len;int ret; ret = alg->encrypt (req); -crypto_stat_akcipher_encrypt (req, ret); +crypto_stat_akcipher_encrypt (src_len, ret, calg);return ret; }@@ -362,10 +354,12 @@ static inline int crypto_akcipher_decrypt(struct akcipher_request *req) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req);struct akcipher_alg *alg =crypto_akcipher_alg (tfm); +struct crypto_alg *calg = tfm->base.__crt_alg; +unsigned int src_len = req->src_len;int ret; ret = alg->decrypt (req); -crypto_stat_akcipher_decrypt (req, ret); +crypto_stat_akcipher_decrypt (src_len, ret, calg);return ret; }@@ -383,10 +377,11 @@ static inline int crypto_akcipher_sign(struct akcipher_request *req) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req);struct akcipher_alg *alg =crypto_akcipher_alg (tfm); +struct crypto_alg *calg = tfm->base.__crt_alg;int ret; ret = alg->sign (req); -crypto_stat_akcipher_sign (req, ret); +crypto_stat_akcipher_sign (ret, calg);return ret; }@@ -404,10 +399,11 @@ static inline int crypto_akcipher_verify(struct akcipher_request *req) {struct crypto_akcipher *tfm =crypto_akcipher_reqtfm (req);struct akcipher_alg *alg =crypto_akcipher_alg (tfm); +struct crypto_alg *calg = tfm->base.__crt_alg;int ret; ret = alg->verify (req); -crypto_stat_akcipher_verify (req, ret); +crypto_stat_akcipher_verify (ret, calg);return ret; }include/crypto/hash.h +13
-12@@ -412,28 +412,26 @@ static inline void *ahash_request_ctx(struct ahash_request *req)int crypto_ahash_setkey (struct crypto_ahash *tfm,const u8 *key,unsigned int keylen); -static inline void crypto_stat_ahash_update (struct ahash_request *req,int ret) +static inline void crypto_stat_ahash_update (unsigned int nbytes,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) -atomic64_inc (&tfm->base.__crt_alg->hash_err_cnt); +atomic64_inc (&alg->hash_err_cnt);else -atomic64_add (req->nbytes, &tfm->base.__crt_alg->hash_tlen); +atomic64_add (nbytes, &alg->hash_tlen);#endif } -static inline void crypto_stat_ahash_final (struct ahash_request *req,int ret) +static inline void crypto_stat_ahash_final (unsigned int nbytes,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&tfm->base.__crt_alg->hash_err_cnt); +atomic64_inc (&alg->hash_err_cnt); }else { -atomic64_inc (&tfm->base.__crt_alg->hash_cnt); -atomic64_add (req->nbytes, &tfm->base.__crt_alg->hash_tlen); +atomic64_inc (&alg->hash_cnt); +atomic64_add (nbytes, &alg->hash_tlen); }#endif }@@ -552,10 +550,13 @@ static inline int crypto_ahash_init(struct ahash_request *req)*/ static inline int crypto_ahash_update (struct ahash_request *req) { +struct crypto_ahash *tfm =crypto_ahash_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int nbytes = req->nbytes;int ret; ret =crypto_ahash_reqtfm (req)->update (req); -crypto_stat_ahash_update (req, ret); +crypto_stat_ahash_update (nbytes, ret, alg);return ret; }include/crypto/kpp.h +10
-12@@ -278,29 +278,25 @@ static inline void crypto_stat_kpp_set_secret(struct crypto_kpp *tfm, int ret)#endif } -static inline void crypto_stat_kpp_generate_public_key (struct kpp_request *req, +static inline void crypto_stat_kpp_generate_public_key (struct crypto_alg *alg,int ret) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_kpp *tfm =crypto_kpp_reqtfm (req); -if (ret) -atomic64_inc (&tfm->base.__crt_alg->kpp_err_cnt); +atomic64_inc (&alg->kpp_err_cnt);else -atomic64_inc (&tfm->base.__crt_alg->generate_public_key_cnt); +atomic64_inc (&alg->generate_public_key_cnt);#endif } -static inline void crypto_stat_kpp_compute_shared_secret (struct kpp_request *req, +static inline void crypto_stat_kpp_compute_shared_secret (struct crypto_alg *alg,int ret) {#ifdef CONFIG_CRYPTO_STATS -struct crypto_kpp *tfm =crypto_kpp_reqtfm (req); -if (ret) -atomic64_inc (&tfm->base.__crt_alg->kpp_err_cnt); +atomic64_inc (&alg->kpp_err_cnt);else -atomic64_inc (&tfm->base.__crt_alg->compute_shared_secret_cnt); +atomic64_inc (&alg->compute_shared_secret_cnt);#endif }@@ -347,10 +343,11 @@ static inline int crypto_kpp_generate_public_key(struct kpp_request *req) {struct crypto_kpp *tfm =crypto_kpp_reqtfm (req);struct kpp_alg *alg =crypto_kpp_alg (tfm); +struct crypto_alg *calg = tfm->base.__crt_alg;int ret; ret = alg->generate_public_key (req); -crypto_stat_kpp_generate_public_key (req, ret); +crypto_stat_kpp_generate_public_key (calg, ret);return ret; }@@ -368,10 +365,11 @@ static inline int crypto_kpp_compute_shared_secret(struct kpp_request *req) {struct crypto_kpp *tfm =crypto_kpp_reqtfm (req);struct kpp_alg *alg =crypto_kpp_alg (tfm); +struct crypto_alg *calg = tfm->base.__crt_alg;int ret; ret = alg->compute_shared_secret (req); -crypto_stat_kpp_compute_shared_secret (req, ret); +crypto_stat_kpp_compute_shared_secret (calg, ret);return ret; }include/crypto/skcipher.h +10
-6@@ -486,7 +486,7 @@ static inline struct crypto_sync_skcipher *crypto_sync_skcipher_reqtfm( return container_of(tfm, struct crypto_sync_skcipher, base); } -static inline void crypto_stat_skcipher_encrypt (struct skcipher_request *req, +static inline void crypto_stat_skcipher_encrypt (unsigned int cryptlen,int ret,struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS @@ -494,12 +494,12 @@ static inline void crypto_stat_skcipher_encrypt(struct skcipher_request *req, atomic64_inc(&alg->cipher_err_cnt); }else {atomic64_inc (&alg->encrypt_cnt); -atomic64_add (req->cryptlen, &alg->encrypt_tlen); +atomic64_add (cryptlen, &alg->encrypt_tlen); }#endif } -static inline void crypto_stat_skcipher_decrypt (struct skcipher_request *req, +static inline void crypto_stat_skcipher_decrypt (unsigned int cryptlen,int ret,struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS @@ -507,7 +507,7 @@ static inline void crypto_stat_skcipher_decrypt(struct skcipher_request *req, atomic64_inc(&alg->cipher_err_cnt); }else {atomic64_inc (&alg->decrypt_cnt); -atomic64_add (req->cryptlen, &alg->decrypt_tlen); +atomic64_add (cryptlen, &alg->decrypt_tlen); }#endif }@@ -526,13 +526,15 @@ static inline void crypto_stat_skcipher_decrypt(struct skcipher_request *req,static inline int crypto_skcipher_encrypt (struct skcipher_request *req) {struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int cryptlen = req->cryptlen;int ret;if (crypto_skcipher_get_flags (tfm) &CRYPTO_TFM_NEED_KEY ) ret = -ENOKEY ;else ret = tfm->encrypt (req); -crypto_stat_skcipher_encrypt (req, ret, tfm->base.__crt_alg); +crypto_stat_skcipher_encrypt (cryptlen, ret, alg);return ret; }@@ -550,13 +552,15 @@ static inline int crypto_skcipher_encrypt(struct skcipher_request *req)static inline int crypto_skcipher_decrypt (struct skcipher_request *req) {struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int cryptlen = req->cryptlen;int ret;if (crypto_skcipher_get_flags (tfm) &CRYPTO_TFM_NEED_KEY ) ret = -ENOKEY ;else ret = tfm->decrypt (req); -crypto_stat_skcipher_decrypt (req, ret, tfm->base.__crt_alg); +crypto_stat_skcipher_decrypt (cryptlen, ret, alg);return ret; }include/linux/crypto.h +16
-18@@ -975,34 +975,28 @@ static inline struct crypto_ablkcipher *crypto_ablkcipher_reqtfm(return __crypto_ablkcipher_cast (req->base.tfm); } -static inline void crypto_stat_ablkcipher_encrypt (struct ablkcipher_request *req, -int ret) +static inline void crypto_stat_ablkcipher_encrypt (unsigned int nbytes,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct ablkcipher_tfm *crt = -crypto_ablkcipher_crt (crypto_ablkcipher_reqtfm (req)); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&crt->base->base.__crt_alg->cipher_err_cnt); +atomic64_inc (&alg->cipher_err_cnt); }else { -atomic64_inc (&crt->base->base.__crt_alg->encrypt_cnt); -atomic64_add (req->nbytes, &crt->base->base.__crt_alg->encrypt_tlen); +atomic64_inc (&alg->encrypt_cnt); +atomic64_add (nbytes, &alg->encrypt_tlen); }#endif } -static inline void crypto_stat_ablkcipher_decrypt (struct ablkcipher_request *req, -int ret) +static inline void crypto_stat_ablkcipher_decrypt (unsigned int nbytes,int ret, +struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS -struct ablkcipher_tfm *crt = -crypto_ablkcipher_crt (crypto_ablkcipher_reqtfm (req)); -if (ret && ret != -EINPROGRESS && ret != -EBUSY ) { -atomic64_inc (&crt->base->base.__crt_alg->cipher_err_cnt); +atomic64_inc (&alg->cipher_err_cnt); }else { -atomic64_inc (&crt->base->base.__crt_alg->decrypt_cnt); -atomic64_add (req->nbytes, &crt->base->base.__crt_alg->decrypt_tlen); +atomic64_inc (&alg->decrypt_cnt); +atomic64_add (nbytes, &alg->decrypt_tlen); }#endif }@@ -1022,10 +1016,12 @@ static inline int crypto_ablkcipher_encrypt(struct ablkcipher_request *req) {struct ablkcipher_tfm *crt =crypto_ablkcipher_crt (crypto_ablkcipher_reqtfm (req)); +struct crypto_alg *alg = crt->base->base.__crt_alg; +unsigned int nbytes = req->nbytes;int ret; ret = crt->encrypt (req); -crypto_stat_ablkcipher_encrypt (req, ret); +crypto_stat_ablkcipher_encrypt (nbytes, ret, alg);return ret; }@@ -1044,10 +1040,12 @@ static inline int crypto_ablkcipher_decrypt(struct ablkcipher_request *req) {struct ablkcipher_tfm *crt =crypto_ablkcipher_crt (crypto_ablkcipher_reqtfm (req)); +struct crypto_alg *alg = crt->base->base.__crt_alg; +unsigned int nbytes = req->nbytes;int ret; ret = crt->decrypt (req); -crypto_stat_ablkcipher_decrypt (req, ret); +crypto_stat_ablkcipher_decrypt (nbytes, ret, alg);return ret; }Signature · 1 line
2.18.1
Re: [PATCH 5/5] crypto: crypto_user_stat: fix use_after_free of struct xxx_request
Quoted 49 lines · Eric Biggers
Hi Corentin,
Quoted 29 lines · Corentin Labbe
All
crypto_statsfunctions use the structxxx_requestfor feeding stats, but in some case this structure could already be freed.For fixing this, the needed parameters (len and alg) will be stored before the request being executed.
Fixes cac5818c25d0 ("crypto: user - Implement a generic crypto statistics")Reported-by syzbot <[email protected]>Signed-off-by Corentin Labbe <[email protected]>--- crypto/ahash.c | 14 ++++++++-- include/crypto/acompress.h | 30 ++++++++++---------- include/crypto/aead.h | 30 ++++++++++---------- include/crypto/akcipher.h | 56 ++++++++++++++++++-------------------- include/crypto/hash.h | 25 +++++++++-------- include/crypto/kpp.h | 22 +++++++-------- include/crypto/skcipher.h | 16 +++++++---- include/linux/crypto.h | 34 +++++++++++------------ 8 files changed, 118 insertions(+), 109 deletions(-)crypto/ahash.c +1
-0@@ -364,20 +364,26 @@ static int crypto_ahash_op(struct ahash_request *req,int crypto_ahash_final (struct ahash_request *req) { +struct crypto_ahash *tfm =crypto_ahash_reqtfm (req);crypto/ahash.c static int crypto_ahash_op(struct ahash_request *req, · line 376
Quoted 10 lines
crypto/ahash.c +3
-1+struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int nbytes = req->nbytes;int ret; ret =crypto_ahash_op (req,crypto_ahash_reqtfm (req)->final); -crypto_stat_ahash_final (req, ret); +crypto_stat_ahash_final (nbytes, ret, alg);return ret; }EXPORT_SYMBOL_GPL (crypto_ahash_final);I'm not confident this is enough. If the request is being processed asynchronously, the completion callback could be used to signal another thread to go ahead and free the resources, including not only the request but also the 'tfm', which could even result in the last reference to the 'alg' being dropped and therefore the 'alg' being freed. If I'm wrong, then please explain why :-)
Quoted 5 lines
Note: I'd also prefer a solution that is more obviously zero-overhead in the !CONFIG_CRYPTO_STATS case.
- Eric
I think the best solution is to grab a crypto_alg refcnt before the operation and release it after. So for example this will lead to:
include/crypto/skcipher.h +18 -6
@@ -486,7 +486,7 @@ static inline struct crypto_sync_skcipher *crypto_sync_skcipher_reqtfm( return container_of(tfm, struct crypto_sync_skcipher, base); } -static inline void crypto_stat_skcipher_encrypt (struct skcipher_request *req, +static inline void crypto_stat_skcipher_encrypt (unsigned int cryptlen,int ret,struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS
@@ -494,12 +494,13 @@ static inline void crypto_stat_skcipher_encrypt(struct skcipher_request *req, atomic64_inc(&alg->cipher_err_cnt); }else {atomic64_inc (&alg->encrypt_cnt); -atomic64_add (req->cryptlen, &alg->encrypt_tlen); +atomic64_add (cryptlen, &alg->encrypt_tlen); } +crypto_alg_put (alg);#endif } -static inline void crypto_stat_skcipher_decrypt (struct skcipher_request *req, +static inline void crypto_stat_skcipher_decrypt (unsigned int cryptlen,int ret,struct crypto_alg *alg) {#ifdef CONFIG_CRYPTO_STATS
@@ -507,8 +508,9 @@ static inline void crypto_stat_skcipher_decrypt(struct skcipher_request *req, atomic64_inc(&alg->cipher_err_cnt); }else {atomic64_inc (&alg->decrypt_cnt); -atomic64_add (req->cryptlen, &alg->decrypt_tlen); +atomic64_add (cryptlen, &alg->decrypt_tlen); } +crypto_alg_put (alg);#endif }
@@ -526,13 +528,18 @@ static inline void crypto_stat_skcipher_decrypt(struct skcipher_request *req,static inline int crypto_skcipher_encrypt (struct skcipher_request *req) {struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int cryptlen = req->cryptlen;int ret; +#ifdef CONFIG_CRYPTO_STATS +crypto_alg_get (alg); +#endif if (crypto_skcipher_get_flags (tfm) &CRYPTO_TFM_NEED_KEY ) ret = -ENOKEY ;else ret = tfm->encrypt (req); -crypto_stat_skcipher_encrypt (req, ret, tfm->base.__crt_alg); +crypto_stat_skcipher_encrypt (cryptlen, ret, alg);return ret; }
@@ -550,13 +557,18 @@ static inline int crypto_skcipher_encrypt(struct skcipher_request *req)static inline int crypto_skcipher_decrypt (struct skcipher_request *req) {struct crypto_skcipher *tfm =crypto_skcipher_reqtfm (req); +struct crypto_alg *alg = tfm->base.__crt_alg; +unsigned int cryptlen = req->cryptlen;int ret; +#ifdef CONFIG_CRYPTO_STATS +crypto_alg_get (alg); +#endif if (crypto_skcipher_get_flags (tfm) &CRYPTO_TFM_NEED_KEY ) ret = -ENOKEY ;else ret = tfm->decrypt (req); -crypto_stat_skcipher_decrypt (req, ret, tfm->base.__crt_alg); +crypto_stat_skcipher_decrypt (cryptlen, ret, alg);return ret; }
This should not have any overhead with !CONFIG_CRYPTO_STATS
The only drawback is that crypto_alg_get/crypto_alg_put need to be moved out of crypto/internal.h to include/linux/crypto.h
Herbert what do you think about that ?
Regards