Skip to content

Commit b2e689b

Browse files
committed
crypto: ahash - Disable request chaining
Disable hash request chaining in case a driver that copies an ahash_request object by hand accidentally triggers chaining. Reported-by: Manorit Chawdhry <[email protected]> Fixes: f2ffe5a ("crypto: hash - Add request chaining API") Signed-off-by: Herbert Xu <[email protected]> Tested-by: Manorit Chawdhry <[email protected]> Signed-off-by: Herbert Xu <[email protected]>
1 parent 9ae0c92 commit b2e689b

File tree

3 files changed

+7
-77
lines changed

3 files changed

+7
-77
lines changed

crypto/ahash.c

+1-75
Original file line numberDiff line numberDiff line change
@@ -315,16 +315,7 @@ EXPORT_SYMBOL_GPL(crypto_ahash_setkey);
315315

316316
static bool ahash_request_hasvirt(struct ahash_request *req)
317317
{
318-
struct ahash_request *r2;
319-
320-
if (ahash_request_isvirt(req))
321-
return true;
322-
323-
list_for_each_entry(r2, &req->base.list, base.list)
324-
if (ahash_request_isvirt(r2))
325-
return true;
326-
327-
return false;
318+
return ahash_request_isvirt(req);
328319
}
329320

330321
static int ahash_reqchain_virt(struct ahash_save_req_state *state,
@@ -472,7 +463,6 @@ static int ahash_do_req_chain(struct ahash_request *req,
472463
bool update = op == crypto_ahash_alg(tfm)->update;
473464
struct ahash_save_req_state *state;
474465
struct ahash_save_req_state state0;
475-
struct ahash_request *r2;
476466
u8 *page = NULL;
477467
int err;
478468

@@ -509,7 +499,6 @@ static int ahash_do_req_chain(struct ahash_request *req,
509499
state->offset = 0;
510500
state->nbytes = 0;
511501
INIT_LIST_HEAD(&state->head);
512-
list_splice_init(&req->base.list, &state->head);
513502

514503
if (page)
515504
sg_init_one(&state->sg, page, PAGE_SIZE);
@@ -540,9 +529,6 @@ static int ahash_do_req_chain(struct ahash_request *req,
540529

541530
out_set_chain:
542531
req->base.err = err;
543-
list_for_each_entry(r2, &req->base.list, base.list)
544-
r2->base.err = err;
545-
546532
return err;
547533
}
548534

@@ -551,19 +537,10 @@ int crypto_ahash_init(struct ahash_request *req)
551537
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
552538

553539
if (likely(tfm->using_shash)) {
554-
struct ahash_request *r2;
555540
int err;
556541

557542
err = crypto_shash_init(prepare_shash_desc(req, tfm));
558543
req->base.err = err;
559-
560-
list_for_each_entry(r2, &req->base.list, base.list) {
561-
struct shash_desc *desc;
562-
563-
desc = prepare_shash_desc(r2, tfm);
564-
r2->base.err = crypto_shash_init(desc);
565-
}
566-
567544
return err;
568545
}
569546

@@ -620,19 +597,10 @@ int crypto_ahash_update(struct ahash_request *req)
620597
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
621598

622599
if (likely(tfm->using_shash)) {
623-
struct ahash_request *r2;
624600
int err;
625601

626602
err = shash_ahash_update(req, ahash_request_ctx(req));
627603
req->base.err = err;
628-
629-
list_for_each_entry(r2, &req->base.list, base.list) {
630-
struct shash_desc *desc;
631-
632-
desc = ahash_request_ctx(r2);
633-
r2->base.err = shash_ahash_update(r2, desc);
634-
}
635-
636604
return err;
637605
}
638606

@@ -645,19 +613,10 @@ int crypto_ahash_final(struct ahash_request *req)
645613
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
646614

647615
if (likely(tfm->using_shash)) {
648-
struct ahash_request *r2;
649616
int err;
650617

651618
err = crypto_shash_final(ahash_request_ctx(req), req->result);
652619
req->base.err = err;
653-
654-
list_for_each_entry(r2, &req->base.list, base.list) {
655-
struct shash_desc *desc;
656-
657-
desc = ahash_request_ctx(r2);
658-
r2->base.err = crypto_shash_final(desc, r2->result);
659-
}
660-
661620
return err;
662621
}
663622

@@ -670,19 +629,10 @@ int crypto_ahash_finup(struct ahash_request *req)
670629
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
671630

672631
if (likely(tfm->using_shash)) {
673-
struct ahash_request *r2;
674632
int err;
675633

676634
err = shash_ahash_finup(req, ahash_request_ctx(req));
677635
req->base.err = err;
678-
679-
list_for_each_entry(r2, &req->base.list, base.list) {
680-
struct shash_desc *desc;
681-
682-
desc = ahash_request_ctx(r2);
683-
r2->base.err = shash_ahash_finup(r2, desc);
684-
}
685-
686636
return err;
687637
}
688638

@@ -757,19 +707,10 @@ int crypto_ahash_digest(struct ahash_request *req)
757707
struct crypto_ahash *tfm = crypto_ahash_reqtfm(req);
758708

759709
if (likely(tfm->using_shash)) {
760-
struct ahash_request *r2;
761710
int err;
762711

763712
err = shash_ahash_digest(req, prepare_shash_desc(req, tfm));
764713
req->base.err = err;
765-
766-
list_for_each_entry(r2, &req->base.list, base.list) {
767-
struct shash_desc *desc;
768-
769-
desc = prepare_shash_desc(r2, tfm);
770-
r2->base.err = shash_ahash_digest(r2, desc);
771-
}
772-
773714
return err;
774715
}
775716

@@ -1133,20 +1074,5 @@ int ahash_register_instance(struct crypto_template *tmpl,
11331074
}
11341075
EXPORT_SYMBOL_GPL(ahash_register_instance);
11351076

1136-
void ahash_request_free(struct ahash_request *req)
1137-
{
1138-
struct ahash_request *tmp;
1139-
struct ahash_request *r2;
1140-
1141-
if (unlikely(!req))
1142-
return;
1143-
1144-
list_for_each_entry_safe(r2, tmp, &req->base.list, base.list)
1145-
kfree_sensitive(r2);
1146-
1147-
kfree_sensitive(req);
1148-
}
1149-
EXPORT_SYMBOL_GPL(ahash_request_free);
1150-
11511077
MODULE_LICENSE("GPL");
11521078
MODULE_DESCRIPTION("Asynchronous cryptographic hash type");

include/crypto/hash.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
#include <linux/atomic.h>
1212
#include <linux/crypto.h>
13+
#include <linux/slab.h>
1314
#include <linux/string.h>
1415

1516
/* Set this bit for virtual address instead of SG list. */
@@ -581,7 +582,10 @@ static inline struct ahash_request *ahash_request_alloc_noprof(
581582
* ahash_request_free() - zeroize and free the request data structure
582583
* @req: request data structure cipher handle to be freed
583584
*/
584-
void ahash_request_free(struct ahash_request *req);
585+
static inline void ahash_request_free(struct ahash_request *req)
586+
{
587+
kfree_sensitive(req);
588+
}
585589

586590
static inline struct ahash_request *ahash_request_cast(
587591
struct crypto_async_request *req)

include/crypto/internal/hash.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ static inline struct crypto_shash *__crypto_shash_cast(struct crypto_tfm *tfm)
249249

250250
static inline bool ahash_request_chained(struct ahash_request *req)
251251
{
252-
return crypto_request_chained(&req->base);
252+
return false;
253253
}
254254

255255
static inline bool ahash_request_isvirt(struct ahash_request *req)

0 commit comments

Comments
 (0)