Skip to content

Commit 43b970f

Browse files
WillLesterherbertx
authored andcommitted
crypto: cryptd - Use refcount_t for refcount
Reference counters are preferred to use refcount_t instead of atomic_t. This is because the implementation of refcount_t can prevent overflows and detect possible use-after-free. So convert atomic_t ref counters to refcount_t. Signed-off-by: Chuhong Yuan <hslester96@gmail.com> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 03a3bb7 commit 43b970f

1 file changed

Lines changed: 22 additions & 22 deletions

File tree

‎crypto/cryptd.c‎

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
#include <crypto/internal/aead.h>
1717
#include <crypto/internal/skcipher.h>
1818
#include <crypto/cryptd.h>
19-
#include <linux/atomic.h>
19+
#include <linux/refcount.h>
2020
#include <linux/err.h>
2121
#include <linux/init.h>
2222
#include <linux/kernel.h>
@@ -63,7 +63,7 @@ struct aead_instance_ctx {
6363
};
6464

6565
struct cryptd_skcipher_ctx {
66-
atomic_t refcnt;
66+
refcount_t refcnt;
6767
struct crypto_sync_skcipher *child;
6868
};
6969

@@ -72,7 +72,7 @@ struct cryptd_skcipher_request_ctx {
7272
};
7373

7474
struct cryptd_hash_ctx {
75-
atomic_t refcnt;
75+
refcount_t refcnt;
7676
struct crypto_shash *child;
7777
};
7878

@@ -82,7 +82,7 @@ struct cryptd_hash_request_ctx {
8282
};
8383

8484
struct cryptd_aead_ctx {
85-
atomic_t refcnt;
85+
refcount_t refcnt;
8686
struct crypto_aead *child;
8787
};
8888

@@ -127,7 +127,7 @@ static int cryptd_enqueue_request(struct cryptd_queue *queue,
127127
{
128128
int cpu, err;
129129
struct cryptd_cpu_queue *cpu_queue;
130-
atomic_t *refcnt;
130+
refcount_t *refcnt;
131131

132132
cpu = get_cpu();
133133
cpu_queue = this_cpu_ptr(queue->cpu_queue);
@@ -140,10 +140,10 @@ static int cryptd_enqueue_request(struct cryptd_queue *queue,
140140

141141
queue_work_on(cpu, cryptd_wq, &cpu_queue->work);
142142

143-
if (!atomic_read(refcnt))
143+
if (!refcount_read(refcnt))
144144
goto out_put_cpu;
145145

146-
atomic_inc(refcnt);
146+
refcount_inc(refcnt);
147147

148148
out_put_cpu:
149149
put_cpu();
@@ -270,13 +270,13 @@ static void cryptd_skcipher_complete(struct skcipher_request *req, int err)
270270
struct crypto_skcipher *tfm = crypto_skcipher_reqtfm(req);
271271
struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(tfm);
272272
struct cryptd_skcipher_request_ctx *rctx = skcipher_request_ctx(req);
273-
int refcnt = atomic_read(&ctx->refcnt);
273+
int refcnt = refcount_read(&ctx->refcnt);
274274

275275
local_bh_disable();
276276
rctx->complete(&req->base, err);
277277
local_bh_enable();
278278

279-
if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
279+
if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
280280
crypto_free_skcipher(tfm);
281281
}
282282

@@ -521,13 +521,13 @@ static void cryptd_hash_complete(struct ahash_request *req, int err)
521521
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
522522
struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(tfm);
523523
struct cryptd_hash_request_ctx *rctx = ahash_request_ctx(req);
524-
int refcnt = atomic_read(&ctx->refcnt);
524+
int refcnt = refcount_read(&ctx->refcnt);
525525

526526
local_bh_disable();
527527
rctx->complete(&req->base, err);
528528
local_bh_enable();
529529

530-
if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
530+
if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
531531
crypto_free_ahash(tfm);
532532
}
533533

@@ -772,13 +772,13 @@ static void cryptd_aead_crypt(struct aead_request *req,
772772

773773
out:
774774
ctx = crypto_aead_ctx(tfm);
775-
refcnt = atomic_read(&ctx->refcnt);
775+
refcnt = refcount_read(&ctx->refcnt);
776776

777777
local_bh_disable();
778778
compl(&req->base, err);
779779
local_bh_enable();
780780

781-
if (err != -EINPROGRESS && refcnt && atomic_dec_and_test(&ctx->refcnt))
781+
if (err != -EINPROGRESS && refcnt && refcount_dec_and_test(&ctx->refcnt))
782782
crypto_free_aead(tfm);
783783
}
784784

@@ -979,7 +979,7 @@ struct cryptd_skcipher *cryptd_alloc_skcipher(const char *alg_name,
979979
}
980980

981981
ctx = crypto_skcipher_ctx(tfm);
982-
atomic_set(&ctx->refcnt, 1);
982+
refcount_set(&ctx->refcnt, 1);
983983

984984
return container_of(tfm, struct cryptd_skcipher, base);
985985
}
@@ -997,15 +997,15 @@ bool cryptd_skcipher_queued(struct cryptd_skcipher *tfm)
997997
{
998998
struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
999999

1000-
return atomic_read(&ctx->refcnt) - 1;
1000+
return refcount_read(&ctx->refcnt) - 1;
10011001
}
10021002
EXPORT_SYMBOL_GPL(cryptd_skcipher_queued);
10031003

10041004
void cryptd_free_skcipher(struct cryptd_skcipher *tfm)
10051005
{
10061006
struct cryptd_skcipher_ctx *ctx = crypto_skcipher_ctx(&tfm->base);
10071007

1008-
if (atomic_dec_and_test(&ctx->refcnt))
1008+
if (refcount_dec_and_test(&ctx->refcnt))
10091009
crypto_free_skcipher(&tfm->base);
10101010
}
10111011
EXPORT_SYMBOL_GPL(cryptd_free_skcipher);
@@ -1029,7 +1029,7 @@ struct cryptd_ahash *cryptd_alloc_ahash(const char *alg_name,
10291029
}
10301030

10311031
ctx = crypto_ahash_ctx(tfm);
1032-
atomic_set(&ctx->refcnt, 1);
1032+
refcount_set(&ctx->refcnt, 1);
10331033

10341034
return __cryptd_ahash_cast(tfm);
10351035
}
@@ -1054,15 +1054,15 @@ bool cryptd_ahash_queued(struct cryptd_ahash *tfm)
10541054
{
10551055
struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
10561056

1057-
return atomic_read(&ctx->refcnt) - 1;
1057+
return refcount_read(&ctx->refcnt) - 1;
10581058
}
10591059
EXPORT_SYMBOL_GPL(cryptd_ahash_queued);
10601060

10611061
void cryptd_free_ahash(struct cryptd_ahash *tfm)
10621062
{
10631063
struct cryptd_hash_ctx *ctx = crypto_ahash_ctx(&tfm->base);
10641064

1065-
if (atomic_dec_and_test(&ctx->refcnt))
1065+
if (refcount_dec_and_test(&ctx->refcnt))
10661066
crypto_free_ahash(&tfm->base);
10671067
}
10681068
EXPORT_SYMBOL_GPL(cryptd_free_ahash);
@@ -1086,7 +1086,7 @@ struct cryptd_aead *cryptd_alloc_aead(const char *alg_name,
10861086
}
10871087

10881088
ctx = crypto_aead_ctx(tfm);
1089-
atomic_set(&ctx->refcnt, 1);
1089+
refcount_set(&ctx->refcnt, 1);
10901090

10911091
return __cryptd_aead_cast(tfm);
10921092
}
@@ -1104,15 +1104,15 @@ bool cryptd_aead_queued(struct cryptd_aead *tfm)
11041104
{
11051105
struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
11061106

1107-
return atomic_read(&ctx->refcnt) - 1;
1107+
return refcount_read(&ctx->refcnt) - 1;
11081108
}
11091109
EXPORT_SYMBOL_GPL(cryptd_aead_queued);
11101110

11111111
void cryptd_free_aead(struct cryptd_aead *tfm)
11121112
{
11131113
struct cryptd_aead_ctx *ctx = crypto_aead_ctx(&tfm->base);
11141114

1115-
if (atomic_dec_and_test(&ctx->refcnt))
1115+
if (refcount_dec_and_test(&ctx->refcnt))
11161116
crypto_free_aead(&tfm->base);
11171117
}
11181118
EXPORT_SYMBOL_GPL(cryptd_free_aead);

0 commit comments

Comments
 (0)