Skip to content

Commit

Permalink
mm, treewide: rename kzfree() to kfree_sensitive()
Browse files Browse the repository at this point in the history
As said by Linus:

  A symmetric naming is only helpful if it implies symmetries in use.
  Otherwise it's actively misleading.

  In "kzalloc()", the z is meaningful and an important part of what the
  caller wants.

  In "kzfree()", the z is actively detrimental, because maybe in the
  future we really _might_ want to use that "memfill(0xdeadbeef)" or
  something. The "zero" part of the interface isn't even _relevant_.

The main reason that kzfree() exists is to clear sensitive information
that should not be leaked to other future users of the same memory
objects.

Rename kzfree() to kfree_sensitive() to follow the example of the recently
added kvfree_sensitive() and make the intention of the API more explicit.
In addition, memzero_explicit() is used to clear the memory to make sure
that it won't get optimized away by the compiler.

The renaming is done by using the command sequence:

  git grep -w --name-only kzfree |\
  xargs sed -i 's/kzfree/kfree_sensitive/'

followed by some editing of the kfree_sensitive() kerneldoc and adding
a kzfree backward compatibility macro in slab.h.

[[email protected]: fs/crypto/inline_crypt.c needs linux/slab.h]
[[email protected]: fix fs/crypto/inline_crypt.c some more]

Suggested-by: Joe Perches <[email protected]>
Signed-off-by: Waiman Long <[email protected]>
Signed-off-by: Andrew Morton <[email protected]>
Acked-by: David Howells <[email protected]>
Acked-by: Michal Hocko <[email protected]>
Acked-by: Johannes Weiner <[email protected]>
Cc: Jarkko Sakkinen <[email protected]>
Cc: James Morris <[email protected]>
Cc: "Serge E. Hallyn" <[email protected]>
Cc: Joe Perches <[email protected]>
Cc: Matthew Wilcox <[email protected]>
Cc: David Rientjes <[email protected]>
Cc: Dan Carpenter <[email protected]>
Cc: "Jason A . Donenfeld" <[email protected]>
Link: http://lkml.kernel.org/r/[email protected]
Signed-off-by: Linus Torvalds <[email protected]>
  • Loading branch information
Waiman-Long authored and torvalds committed Aug 7, 2020
1 parent 57c720d commit 453431a
Show file tree
Hide file tree
Showing 114 changed files with 323 additions and 320 deletions.
4 changes: 2 additions & 2 deletions arch/s390/crypto/prng.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ static void prng_tdes_deinstantiate(void)
{
pr_debug("The prng module stopped "
"after running in triple DES mode\n");
kzfree(prng_data);
kfree_sensitive(prng_data);
}


Expand Down Expand Up @@ -442,7 +442,7 @@ static int __init prng_sha512_instantiate(void)
static void prng_sha512_deinstantiate(void)
{
pr_debug("The prng module stopped after running in SHA-512 mode\n");
kzfree(prng_data);
kfree_sensitive(prng_data);
}


Expand Down
2 changes: 1 addition & 1 deletion arch/x86/power/hibernate.c
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ static int get_e820_md5(struct e820_table *table, void *buf)
if (crypto_shash_digest(desc, (u8 *)table, size, buf))
ret = -EINVAL;

kzfree(desc);
kfree_sensitive(desc);

free_tfm:
crypto_free_shash(tfm);
Expand Down
2 changes: 1 addition & 1 deletion crypto/adiantum.c
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ static int adiantum_setkey(struct crypto_skcipher *tfm, const u8 *key,
keyp += NHPOLY1305_KEY_SIZE;
WARN_ON(keyp != &data->derived_keys[ARRAY_SIZE(data->derived_keys)]);
out:
kzfree(data);
kfree_sensitive(data);
return err;
}

Expand Down
4 changes: 2 additions & 2 deletions crypto/ahash.c
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ static int ahash_setkey_unaligned(struct crypto_ahash *tfm, const u8 *key,
alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
memcpy(alignbuffer, key, keylen);
ret = tfm->setkey(tfm, alignbuffer, keylen);
kzfree(buffer);
kfree_sensitive(buffer);
return ret;
}

Expand Down Expand Up @@ -302,7 +302,7 @@ static void ahash_restore_req(struct ahash_request *req, int err)
req->priv = NULL;

/* Free the req->priv.priv from the ADJUSTED request. */
kzfree(priv);
kfree_sensitive(priv);
}

static void ahash_notify_einprogress(struct ahash_request *req)
Expand Down
2 changes: 1 addition & 1 deletion crypto/api.c
Original file line number Diff line number Diff line change
Expand Up @@ -571,7 +571,7 @@ void crypto_destroy_tfm(void *mem, struct crypto_tfm *tfm)
alg->cra_exit(tfm);
crypto_exit_ops(tfm);
crypto_mod_put(alg);
kzfree(mem);
kfree_sensitive(mem);
}
EXPORT_SYMBOL_GPL(crypto_destroy_tfm);

Expand Down
4 changes: 2 additions & 2 deletions crypto/asymmetric_keys/verify_pefile.c
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ static int pefile_digest_pe(const void *pebuf, unsigned int pelen,
}

error:
kzfree(desc);
kfree_sensitive(desc);
error_no_desc:
crypto_free_shash(tfm);
kleave(" = %d", ret);
Expand Down Expand Up @@ -447,6 +447,6 @@ int verify_pefile_signature(const void *pebuf, unsigned pelen,
ret = pefile_digest_pe(pebuf, pelen, &ctx);

error:
kzfree(ctx.digest);
kfree_sensitive(ctx.digest);
return ret;
}
2 changes: 1 addition & 1 deletion crypto/deflate.c
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,7 @@ static void __deflate_exit(void *ctx)
static void deflate_free_ctx(struct crypto_scomp *tfm, void *ctx)
{
__deflate_exit(ctx);
kzfree(ctx);
kfree_sensitive(ctx);
}

static void deflate_exit(struct crypto_tfm *tfm)
Expand Down
10 changes: 5 additions & 5 deletions crypto/drbg.c
Original file line number Diff line number Diff line change
Expand Up @@ -1218,19 +1218,19 @@ static inline void drbg_dealloc_state(struct drbg_state *drbg)
{
if (!drbg)
return;
kzfree(drbg->Vbuf);
kfree_sensitive(drbg->Vbuf);
drbg->Vbuf = NULL;
drbg->V = NULL;
kzfree(drbg->Cbuf);
kfree_sensitive(drbg->Cbuf);
drbg->Cbuf = NULL;
drbg->C = NULL;
kzfree(drbg->scratchpadbuf);
kfree_sensitive(drbg->scratchpadbuf);
drbg->scratchpadbuf = NULL;
drbg->reseed_ctr = 0;
drbg->d_ops = NULL;
drbg->core = NULL;
if (IS_ENABLED(CONFIG_CRYPTO_FIPS)) {
kzfree(drbg->prev);
kfree_sensitive(drbg->prev);
drbg->prev = NULL;
drbg->fips_primed = false;
}
Expand Down Expand Up @@ -1701,7 +1701,7 @@ static int drbg_fini_hash_kernel(struct drbg_state *drbg)
struct sdesc *sdesc = (struct sdesc *)drbg->priv_data;
if (sdesc) {
crypto_free_shash(sdesc->shash.tfm);
kzfree(sdesc);
kfree_sensitive(sdesc);
}
drbg->priv_data = NULL;
return 0;
Expand Down
8 changes: 4 additions & 4 deletions crypto/ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ static u64 *ecc_alloc_digits_space(unsigned int ndigits)

static void ecc_free_digits_space(u64 *space)
{
kzfree(space);
kfree_sensitive(space);
}

static struct ecc_point *ecc_alloc_point(unsigned int ndigits)
Expand Down Expand Up @@ -101,9 +101,9 @@ static void ecc_free_point(struct ecc_point *p)
if (!p)
return;

kzfree(p->x);
kzfree(p->y);
kzfree(p);
kfree_sensitive(p->x);
kfree_sensitive(p->y);
kfree_sensitive(p);
}

static void vli_clear(u64 *vli, unsigned int ndigits)
Expand Down
2 changes: 1 addition & 1 deletion crypto/ecdh.c
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ static int ecdh_compute_value(struct kpp_request *req)

/* fall through */
free_all:
kzfree(shared_secret);
kfree_sensitive(shared_secret);
free_pubkey:
kfree(public_key);
return ret;
Expand Down
2 changes: 1 addition & 1 deletion crypto/gcm.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ static int crypto_gcm_setkey(struct crypto_aead *aead, const u8 *key,
CRYPTO_TFM_REQ_MASK);
err = crypto_ahash_setkey(ghash, (u8 *)&data->hash, sizeof(be128));
out:
kzfree(data);
kfree_sensitive(data);
return err;
}

Expand Down
4 changes: 2 additions & 2 deletions crypto/gf128mul.c
Original file line number Diff line number Diff line change
Expand Up @@ -304,8 +304,8 @@ void gf128mul_free_64k(struct gf128mul_64k *t)
int i;

for (i = 0; i < 16; i++)
kzfree(t->t[i]);
kzfree(t);
kfree_sensitive(t->t[i]);
kfree_sensitive(t);
}
EXPORT_SYMBOL(gf128mul_free_64k);

Expand Down
2 changes: 1 addition & 1 deletion crypto/jitterentropy-kcapi.c
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ void *jent_zalloc(unsigned int len)

void jent_zfree(void *ptr)
{
kzfree(ptr);
kfree_sensitive(ptr);
}

int jent_fips_enabled(void)
Expand Down
2 changes: 1 addition & 1 deletion crypto/rng.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ int crypto_rng_reset(struct crypto_rng *tfm, const u8 *seed, unsigned int slen)
err = crypto_rng_alg(tfm)->seed(tfm, seed, slen);
crypto_stats_rng_seed(alg, err);
out:
kzfree(buf);
kfree_sensitive(buf);
return err;
}
EXPORT_SYMBOL_GPL(crypto_rng_reset);
Expand Down
6 changes: 3 additions & 3 deletions crypto/rsa-pkcs1pad.c
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ static int pkcs1pad_encrypt_sign_complete(struct akcipher_request *req, int err)
sg_copy_from_buffer(req->dst,
sg_nents_for_len(req->dst, ctx->key_size),
out_buf, ctx->key_size);
kzfree(out_buf);
kfree_sensitive(out_buf);

out:
req->dst_len = ctx->key_size;
Expand Down Expand Up @@ -322,7 +322,7 @@ static int pkcs1pad_decrypt_complete(struct akcipher_request *req, int err)
out_buf + pos, req->dst_len);

done:
kzfree(req_ctx->out_buf);
kfree_sensitive(req_ctx->out_buf);

return err;
}
Expand Down Expand Up @@ -500,7 +500,7 @@ static int pkcs1pad_verify_complete(struct akcipher_request *req, int err)
req->dst_len) != 0)
err = -EKEYREJECTED;
done:
kzfree(req_ctx->out_buf);
kfree_sensitive(req_ctx->out_buf);

return err;
}
Expand Down
2 changes: 1 addition & 1 deletion crypto/seqiv.c
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ static void seqiv_aead_encrypt_complete2(struct aead_request *req, int err)
memcpy(req->iv, subreq->iv, crypto_aead_ivsize(geniv));

out:
kzfree(subreq->iv);
kfree_sensitive(subreq->iv);
}

static void seqiv_aead_encrypt_complete(struct crypto_async_request *base,
Expand Down
2 changes: 1 addition & 1 deletion crypto/shash.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static int shash_setkey_unaligned(struct crypto_shash *tfm, const u8 *key,
alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
memcpy(alignbuffer, key, keylen);
err = shash->setkey(tfm, alignbuffer, keylen);
kzfree(buffer);
kfree_sensitive(buffer);
return err;
}

Expand Down
2 changes: 1 addition & 1 deletion crypto/skcipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -592,7 +592,7 @@ static int skcipher_setkey_unaligned(struct crypto_skcipher *tfm,
alignbuffer = (u8 *)ALIGN((unsigned long)buffer, alignmask + 1);
memcpy(alignbuffer, key, keylen);
ret = cipher->setkey(tfm, alignbuffer, keylen);
kzfree(buffer);
kfree_sensitive(buffer);
return ret;
}

Expand Down
6 changes: 3 additions & 3 deletions crypto/testmgr.c
Original file line number Diff line number Diff line change
Expand Up @@ -1744,7 +1744,7 @@ static int test_hash_vs_generic_impl(const char *driver,
kfree(vec.plaintext);
kfree(vec.digest);
crypto_free_shash(generic_tfm);
kzfree(generic_desc);
kfree_sensitive(generic_desc);
return err;
}
#else /* !CONFIG_CRYPTO_MANAGER_EXTRA_TESTS */
Expand Down Expand Up @@ -3665,7 +3665,7 @@ static int drbg_cavs_test(const struct drbg_testvec *test, int pr,
if (IS_ERR(drng)) {
printk(KERN_ERR "alg: drbg: could not allocate DRNG handle for "
"%s\n", driver);
kzfree(buf);
kfree_sensitive(buf);
return -ENOMEM;
}

Expand Down Expand Up @@ -3712,7 +3712,7 @@ static int drbg_cavs_test(const struct drbg_testvec *test, int pr,

outbuf:
crypto_free_rng(drng);
kzfree(buf);
kfree_sensitive(buf);
return ret;
}

Expand Down
2 changes: 1 addition & 1 deletion crypto/zstd.c
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ static void __zstd_exit(void *ctx)
static void zstd_free_ctx(struct crypto_scomp *tfm, void *ctx)
{
__zstd_exit(ctx);
kzfree(ctx);
kfree_sensitive(ctx);
}

static void zstd_exit(struct crypto_tfm *tfm)
Expand Down
2 changes: 1 addition & 1 deletion drivers/crypto/allwinner/sun8i-ce/sun8i-ce-cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -254,7 +254,7 @@ static int sun8i_ce_cipher(struct skcipher_request *areq)
offset = areq->cryptlen - ivsize;
if (rctx->op_dir & CE_DECRYPTION) {
memcpy(areq->iv, backup_iv, ivsize);
kzfree(backup_iv);
kfree_sensitive(backup_iv);
} else {
scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
ivsize, 0);
Expand Down
2 changes: 1 addition & 1 deletion drivers/crypto/allwinner/sun8i-ss/sun8i-ss-cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ static int sun8i_ss_cipher(struct skcipher_request *areq)
if (rctx->op_dir & SS_DECRYPTION) {
memcpy(areq->iv, backup_iv, ivsize);
memzero_explicit(backup_iv, ivsize);
kzfree(backup_iv);
kfree_sensitive(backup_iv);
} else {
scatterwalk_map_and_copy(areq->iv, areq->dst, offset,
ivsize, 0);
Expand Down
4 changes: 2 additions & 2 deletions drivers/crypto/amlogic/amlogic-gxl-cipher.c
Original file line number Diff line number Diff line change
Expand Up @@ -252,8 +252,8 @@ static int meson_cipher(struct skcipher_request *areq)
}
}
theend:
kzfree(bkeyiv);
kzfree(backup_iv);
kfree_sensitive(bkeyiv);
kfree_sensitive(backup_iv);

return err;
}
Expand Down
2 changes: 1 addition & 1 deletion drivers/crypto/atmel-ecc.c
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ static void atmel_ecdh_done(struct atmel_i2c_work_data *work_data, void *areq,

/* fall through */
free_work_data:
kzfree(work_data);
kfree_sensitive(work_data);
kpp_request_complete(req, status);
}

Expand Down
28 changes: 14 additions & 14 deletions drivers/crypto/caam/caampkc.c
Original file line number Diff line number Diff line change
Expand Up @@ -854,14 +854,14 @@ static int caam_rsa_dec(struct akcipher_request *req)

static void caam_rsa_free_key(struct caam_rsa_key *key)
{
kzfree(key->d);
kzfree(key->p);
kzfree(key->q);
kzfree(key->dp);
kzfree(key->dq);
kzfree(key->qinv);
kzfree(key->tmp1);
kzfree(key->tmp2);
kfree_sensitive(key->d);
kfree_sensitive(key->p);
kfree_sensitive(key->q);
kfree_sensitive(key->dp);
kfree_sensitive(key->dq);
kfree_sensitive(key->qinv);
kfree_sensitive(key->tmp1);
kfree_sensitive(key->tmp2);
kfree(key->e);
kfree(key->n);
memset(key, 0, sizeof(*key));
Expand Down Expand Up @@ -1018,17 +1018,17 @@ static void caam_rsa_set_priv_key_form(struct caam_rsa_ctx *ctx,
return;

free_dq:
kzfree(rsa_key->dq);
kfree_sensitive(rsa_key->dq);
free_dp:
kzfree(rsa_key->dp);
kfree_sensitive(rsa_key->dp);
free_tmp2:
kzfree(rsa_key->tmp2);
kfree_sensitive(rsa_key->tmp2);
free_tmp1:
kzfree(rsa_key->tmp1);
kfree_sensitive(rsa_key->tmp1);
free_q:
kzfree(rsa_key->q);
kfree_sensitive(rsa_key->q);
free_p:
kzfree(rsa_key->p);
kfree_sensitive(rsa_key->p);
}

static int caam_rsa_set_priv_key(struct crypto_akcipher *tfm, const void *key,
Expand Down
6 changes: 3 additions & 3 deletions drivers/crypto/cavium/cpt/cptvf_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ static void cleanup_worker_threads(struct cpt_vf *cptvf)
for (i = 0; i < cptvf->nr_queues; i++)
tasklet_kill(&cwqe_info->vq_wqe[i].twork);

kzfree(cwqe_info);
kfree_sensitive(cwqe_info);
cptvf->wqe_info = NULL;
}

Expand All @@ -88,7 +88,7 @@ static void free_pending_queues(struct pending_qinfo *pqinfo)
continue;

/* free single queue */
kzfree((queue->head));
kfree_sensitive((queue->head));

queue->front = 0;
queue->rear = 0;
Expand Down Expand Up @@ -189,7 +189,7 @@ static void free_command_queues(struct cpt_vf *cptvf,
chunk->head = NULL;
chunk->dma_addr = 0;
hlist_del(&chunk->nextchunk);
kzfree(chunk);
kfree_sensitive(chunk);
}

queue->nchunks = 0;
Expand Down
Loading

0 comments on commit 453431a

Please sign in to comment.