crypto: algif - add and use sock_kzfree_s() instead of memzero_explicit()
Subsystems (3)
PATCH 1/1 crypto: algif - add and use sock_kzfree_s() instead of memzero_explicit()
Commit e1bd95bf7c25 ("crypto: algif - zeroize IV buffer") and 2a6af25befd0 ("crypto: algif - zeroize message digest buffer") added memzero_explicit() calls on buffers that are later on passed back to sock_kfree_s().
This is a discussed follow-up that, instead, extends the sock API and adds sock_kzfree_s(), which internally uses kzfree() instead of kfree() for passing the buffers back to slab.
Having sock_kzfree_s() allows to keep the changes more minimal by just having a drop-in replacement instead of adding memzero_explicit() calls everywhere before sock_kfree_s().
In kzfree(), the compiler is not allowed to optimize the memset() away and thus there's no need for memzero_explicit(). Both, sock_kfree_s() and sock_kzfree_s() are wrappers for __sock_kfree_s() and call into kfree() resp. kzfree(); here, __sock_kfree_s() needs to be explicitly inlined as we want the compiler to optimize the call and condition away and thus it produces e.g. on x86_64 the _same_ assembler output for sock_kfree_s() before and after, and thus also allows for avoiding code duplication.
--- crypto/algif_hash.c | 6 ++---- crypto/algif_skcipher.c | 3 +-- include/net/sock.h | 1 + net/core/sock.c | 24 ++++++++++++++++++++---- 4 files changed, 24 insertions(+), 10 deletions(-)
crypto/algif_hash.c +2 -4
@@ -258,10 +258,8 @@ static void hash_sock_destruct(struct sock *sk)struct alg_sock *ask =alg_sk (sk);struct hash_ctx *ctx = ask->private; -memzero_explicit (ctx->result, -crypto_ahash_digestsize (crypto_ahash_reqtfm (&ctx->req))); -sock_kfree_s (sk, ctx->result, -crypto_ahash_digestsize (crypto_ahash_reqtfm (&ctx->req))); +sock_kzfree_s (sk, ctx->result, +crypto_ahash_digestsize (crypto_ahash_reqtfm (&ctx->req)));sock_kfree_s (sk, ctx, ctx->len);af_alg_release_parent (sk); }
crypto/algif_skcipher.c +1 -2
@@ -566,8 +566,7 @@ static void skcipher_sock_destruct(struct sock *sk)struct crypto_ablkcipher *tfm =crypto_ablkcipher_reqtfm (&ctx->req);skcipher_free_sgl (sk); -memzero_explicit (ctx->iv,crypto_ablkcipher_ivsize (tfm)); -sock_kfree_s (sk, ctx->iv,crypto_ablkcipher_ivsize (tfm)); +sock_kzfree_s (sk, ctx->iv,crypto_ablkcipher_ivsize (tfm));sock_kfree_s (sk, ctx, ctx->len);af_alg_release_parent (sk); }
include/net/sock.h +1 -0
@@ -1588,6 +1588,7 @@ struct sk_buff *sock_alloc_send_pskb(struct sock *sk, unsigned long header_len, int *errcode, int max_page_order);void *sock_kmalloc (struct sock *sk,int size,gfp_t priority);void sock_kfree_s (struct sock *sk,void *mem,int size); +void sock_kzfree_s (struct sock *sk,void *mem,int size);void sk_send_sigurg (struct sock *sk);/*
net/core/sock.c +20 -4
@@ -1713,18 +1713,34 @@ void *sock_kmalloc(struct sock *sk, int size, gfp_t priority) }EXPORT_SYMBOL (sock_kmalloc); -/* -* Free an option memory block. +/* Free an option memory block. Note, we actually want the inline +* here as this allows gcc to detect the nullify and fold away the +* condition entirely. */ -void sock_kfree_s (struct sock *sk,void *mem,int size) +static inline void __sock_kfree_s (struct sock *sk,void *mem,int size, +const bool nullify) {if (WARN_ON_ONCE (!mem))return ; -kfree (mem); +if (nullify) +kzfree (mem); +else +kfree (mem);atomic_sub (size, &sk->sk_omem_alloc); } + +void sock_kfree_s (struct sock *sk,void *mem,int size) +{ +__sock_kfree_s (sk, mem, size, false); +}EXPORT_SYMBOL (sock_kfree_s); +void sock_kzfree_s (struct sock *sk,void *mem,int size) +{ +__sock_kfree_s (sk, mem, size, true); +} +EXPORT_SYMBOL (sock_kzfree_s); +/* It is almost wait_for_tcp_memory minus release_sock/lock_sock. I think, these locks should be removed for datagram sockets. */
Signature · 1 line
1.9.3
Herbert Xu
maintainerQuoted 23 lines · Daniel Borkmann
Commit
e1bd95bf7c25("crypto: algif - zeroize IV buffer") and2a6af25befd0("crypto: algif - zeroize message digest buffer") addedmemzero_explicit()calls on buffers that are later on passed back tosock_kfree_s().This is a discussed follow-up that, instead, extends the sock API and adds
sock_kzfree_s(), which internally useskzfree()instead ofkfree()for passing the buffers back to slab.Having
sock_kzfree_s()allows to keep the changes more minimal by just having a drop-in replacement instead of addingmemzero_explicit()calls everywhere beforesock_kfree_s().In
kzfree(), the compiler is not allowed to optimize thememset()away and thus there's no need formemzero_explicit(). Both,sock_kfree_s()andsock_kzfree_s()are wrappers for__sock_kfree_s()and call intokfree()resp.kzfree(); here,__sock_kfree_s()needs to be explicitly inlined as we want the compiler to optimize the call and condition away and thus it produces e.g. onx86_64the _same_ assembler output forsock_kfree_s()before and after, and thus also allows for avoiding code duplication.
Quoted 2 lines
Cc David S. Miller <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>
Dave, any comment on this patch?
Thanks,
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt
Herbert Xu
maintainerQuoted 23 lines · Daniel Borkmann
Commit
e1bd95bf7c25("crypto: algif - zeroize IV buffer") and2a6af25befd0("crypto: algif - zeroize message digest buffer") addedmemzero_explicit()calls on buffers that are later on passed back tosock_kfree_s().This is a discussed follow-up that, instead, extends the sock API and adds
sock_kzfree_s(), which internally useskzfree()instead ofkfree()for passing the buffers back to slab.Having
sock_kzfree_s()allows to keep the changes more minimal by just having a drop-in replacement instead of addingmemzero_explicit()calls everywhere beforesock_kfree_s().In
kzfree(), the compiler is not allowed to optimize thememset()away and thus there's no need formemzero_explicit(). Both,sock_kfree_s()andsock_kzfree_s()are wrappers for__sock_kfree_s()and call intokfree()resp.kzfree(); here,__sock_kfree_s()needs to be explicitly inlined as we want the compiler to optimize the call and condition away and thus it produces e.g. onx86_64the _same_ assembler output forsock_kfree_s()before and after, and thus also allows for avoiding code duplication.
Quoted 2 lines
Cc David S. Miller <[email protected]>Signed-off-by Daniel Borkmann <[email protected]>
Patch applied. Thanks!
Signature · 3 lines
Email: Herbert Xu <[email protected]> Home Page: http://gondor.apana.org.au/~herbert/ PGP Key: http://gondor.apana.org.au/~herbert/pubkey.txt