Skip to content

Commit c19650d

Browse files
horiagherbertx
authored andcommitted
crypto: caam - fix DMA mapping of stack memory
Roland reports the following issue and provides a root cause analysis: "On a v4.19 i.MX6 system with IMA and CONFIG_DMA_API_DEBUG enabled, a warning is generated when accessing files on a filesystem for which IMA measurement is enabled: ------------[ cut here ]------------ WARNING: CPU: 0 PID: 1 at kernel/dma/debug.c:1181 check_for_stack.part.9+0xd0/0x120 caam_jr 2101000.jr0: DMA-API: device driver maps memory from stack [addr=b668049e] Modules linked in: CPU: 0 PID: 1 Comm: switch_root Not tainted 4.19.0-20181214-1 raspberrypi#2 Hardware name: Freescale i.MX6 Quad/DualLite (Device Tree) Backtrace: [<c010efb8>] (dump_backtrace) from [<c010f2d0>] (show_stack+0x20/0x24) [<c010f2b0>] (show_stack) from [<c08b04f4>] (dump_stack+0xa0/0xcc) [<c08b0454>] (dump_stack) from [<c012b610>] (__warn+0xf0/0x108) [<c012b520>] (__warn) from [<c012b680>] (warn_slowpath_fmt+0x58/0x74) [<c012b62c>] (warn_slowpath_fmt) from [<c0199acc>] (check_for_stack.part.9+0xd0/0x120) [<c01999fc>] (check_for_stack.part.9) from [<c019a040>] (debug_dma_map_page+0x144/0x174) [<c0199efc>] (debug_dma_map_page) from [<c065f7f4>] (ahash_final_ctx+0x5b4/0xcf0) [<c065f240>] (ahash_final_ctx) from [<c065b3c4>] (ahash_final+0x1c/0x20) [<c065b3a8>] (ahash_final) from [<c03fe278>] (crypto_ahash_op+0x38/0x80) [<c03fe240>] (crypto_ahash_op) from [<c03fe2e0>] (crypto_ahash_final+0x20/0x24) [<c03fe2c0>] (crypto_ahash_final) from [<c03f19a8>] (ima_calc_file_hash+0x29c/0xa40) [<c03f170c>] (ima_calc_file_hash) from [<c03f2b24>] (ima_collect_measurement+0x1dc/0x240) [<c03f2948>] (ima_collect_measurement) from [<c03f0a60>] (process_measurement+0x4c4/0x6b8) [<c03f059c>] (process_measurement) from [<c03f0cdc>] (ima_file_check+0x88/0xa4) [<c03f0c54>] (ima_file_check) from [<c02d8adc>] (path_openat+0x5d8/0x1364) [<c02d8504>] (path_openat) from [<c02dad24>] (do_filp_open+0x84/0xf0) [<c02daca0>] (do_filp_open) from [<c02cf50c>] (do_open_execat+0x84/0x1b0) [<c02cf488>] (do_open_execat) from [<c02d1058>] (__do_execve_file+0x43c/0x890) [<c02d0c1c>] (__do_execve_file) from [<c02d1770>] (sys_execve+0x44/0x4c) [<c02d172c>] (sys_execve) from [<c0101000>] (ret_fast_syscall+0x0/0x28) ---[ end trace 3455789a10e3aefd ]--- The cause is that the struct ahash_request *req is created as a stack-local variable up in the stack (presumably somewhere in the IMA implementation), then passed down into the CAAM driver, which tries to dma_single_map the req->result (indirectly via map_seq_out_ptr_result) in order to make that buffer available for the CAAM to store the result of the following hash operation. The calling code doesn't know how req will be used by the CAAM driver, and there could be other such occurrences where stack memory is passed down to the CAAM driver. Therefore we should rather fix this issue in the CAAM driver where the requirements are known." Fix this problem by: -instructing the crypto engine to write the final hash in state->caam_ctx -subsequently memcpy-ing the final hash into req->result Cc: <stable@vger.kernel.org> # v4.19+ Reported-by: Roland Hieber <rhi@pengutronix.de> Signed-off-by: Horia Geantă <horia.geanta@nxp.com> Tested-by: Roland Hieber <rhi@pengutronix.de> Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
1 parent 5a22b19 commit c19650d

1 file changed

Lines changed: 21 additions & 64 deletions

File tree

‎drivers/crypto/caam/caamhash.c‎

Lines changed: 21 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -184,18 +184,6 @@ static inline int map_seq_out_ptr_ctx(u32 *desc, struct device *jrdev,
184184
return 0;
185185
}
186186

187-
/* Map req->result, and append seq_out_ptr command that points to it */
188-
static inline dma_addr_t map_seq_out_ptr_result(u32 *desc, struct device *jrdev,
189-
u8 *result, int digestsize)
190-
{
191-
dma_addr_t dst_dma;
192-
193-
dst_dma = dma_map_single(jrdev, result, digestsize, DMA_FROM_DEVICE);
194-
append_seq_out_ptr(desc, dst_dma, digestsize, 0);
195-
196-
return dst_dma;
197-
}
198-
199187
/* Map current buffer in state (if length > 0) and put it in link table */
200188
static inline int buf_map_to_sec4_sg(struct device *jrdev,
201189
struct sec4_sg_entry *sec4_sg,
@@ -568,15 +556,13 @@ static int acmac_setkey(struct crypto_ahash *ahash, const u8 *key,
568556

569557
/*
570558
* ahash_edesc - s/w-extended ahash descriptor
571-
* @dst_dma: physical mapped address of req->result
572559
* @sec4_sg_dma: physical mapped address of h/w link table
573560
* @src_nents: number of segments in input scatterlist
574561
* @sec4_sg_bytes: length of dma mapped sec4_sg space
575562
* @hw_desc: the h/w job descriptor followed by any referenced link tables
576563
* @sec4_sg: h/w link table
577564
*/
578565
struct ahash_edesc {
579-
dma_addr_t dst_dma;
580566
dma_addr_t sec4_sg_dma;
581567
int src_nents;
582568
int sec4_sg_bytes;
@@ -592,8 +578,6 @@ static inline void ahash_unmap(struct device *dev,
592578

593579
if (edesc->src_nents)
594580
dma_unmap_sg(dev, req->src, edesc->src_nents, DMA_TO_DEVICE);
595-
if (edesc->dst_dma)
596-
dma_unmap_single(dev, edesc->dst_dma, dst_len, DMA_FROM_DEVICE);
597581

598582
if (edesc->sec4_sg_bytes)
599583
dma_unmap_single(dev, edesc->sec4_sg_dma,
@@ -628,9 +612,9 @@ static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
628612
struct ahash_edesc *edesc;
629613
struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
630614
int digestsize = crypto_ahash_digestsize(ahash);
615+
struct caam_hash_state *state = ahash_request_ctx(req);
631616
#ifdef DEBUG
632617
struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
633-
struct caam_hash_state *state = ahash_request_ctx(req);
634618

635619
dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
636620
#endif
@@ -639,17 +623,14 @@ static void ahash_done(struct device *jrdev, u32 *desc, u32 err,
639623
if (err)
640624
caam_jr_strstatus(jrdev, err);
641625

642-
ahash_unmap(jrdev, edesc, req, digestsize);
626+
ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
627+
memcpy(req->result, state->caam_ctx, digestsize);
643628
kfree(edesc);
644629

645630
#ifdef DEBUG
646631
print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
647632
DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
648633
ctx->ctx_len, 1);
649-
if (req->result)
650-
print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
651-
DUMP_PREFIX_ADDRESS, 16, 4, req->result,
652-
digestsize, 1);
653634
#endif
654635

655636
req->base.complete(&req->base, err);
@@ -697,9 +678,9 @@ static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err,
697678
struct ahash_edesc *edesc;
698679
struct crypto_ahash *ahash = crypto_ahash_reqtfm(req);
699680
int digestsize = crypto_ahash_digestsize(ahash);
681+
struct caam_hash_state *state = ahash_request_ctx(req);
700682
#ifdef DEBUG
701683
struct caam_hash_ctx *ctx = crypto_ahash_ctx(ahash);
702-
struct caam_hash_state *state = ahash_request_ctx(req);
703684

704685
dev_err(jrdev, "%s %d: err 0x%x\n", __func__, __LINE__, err);
705686
#endif
@@ -708,17 +689,14 @@ static void ahash_done_ctx_src(struct device *jrdev, u32 *desc, u32 err,
708689
if (err)
709690
caam_jr_strstatus(jrdev, err);
710691

711-
ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_TO_DEVICE);
692+
ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
693+
memcpy(req->result, state->caam_ctx, digestsize);
712694
kfree(edesc);
713695

714696
#ifdef DEBUG
715697
print_hex_dump(KERN_ERR, "ctx@"__stringify(__LINE__)": ",
716698
DUMP_PREFIX_ADDRESS, 16, 4, state->caam_ctx,
717699
ctx->ctx_len, 1);
718-
if (req->result)
719-
print_hex_dump(KERN_ERR, "result@"__stringify(__LINE__)": ",
720-
DUMP_PREFIX_ADDRESS, 16, 4, req->result,
721-
digestsize, 1);
722700
#endif
723701

724702
req->base.complete(&req->base, err);
@@ -991,7 +969,7 @@ static int ahash_final_ctx(struct ahash_request *req)
991969
edesc->sec4_sg_bytes = sec4_sg_bytes;
992970

993971
ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
994-
edesc->sec4_sg, DMA_TO_DEVICE);
972+
edesc->sec4_sg, DMA_BIDIRECTIONAL);
995973
if (ret)
996974
goto unmap_ctx;
997975

@@ -1011,14 +989,7 @@ static int ahash_final_ctx(struct ahash_request *req)
1011989

1012990
append_seq_in_ptr(desc, edesc->sec4_sg_dma, ctx->ctx_len + buflen,
1013991
LDST_SGF);
1014-
1015-
edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
1016-
digestsize);
1017-
if (dma_mapping_error(jrdev, edesc->dst_dma)) {
1018-
dev_err(jrdev, "unable to map dst\n");
1019-
ret = -ENOMEM;
1020-
goto unmap_ctx;
1021-
}
992+
append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
1022993

1023994
#ifdef DEBUG
1024995
print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
@@ -1031,7 +1002,7 @@ static int ahash_final_ctx(struct ahash_request *req)
10311002

10321003
return -EINPROGRESS;
10331004
unmap_ctx:
1034-
ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1005+
ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
10351006
kfree(edesc);
10361007
return ret;
10371008
}
@@ -1085,7 +1056,7 @@ static int ahash_finup_ctx(struct ahash_request *req)
10851056
edesc->src_nents = src_nents;
10861057

10871058
ret = ctx_map_to_sec4_sg(jrdev, state, ctx->ctx_len,
1088-
edesc->sec4_sg, DMA_TO_DEVICE);
1059+
edesc->sec4_sg, DMA_BIDIRECTIONAL);
10891060
if (ret)
10901061
goto unmap_ctx;
10911062

@@ -1099,13 +1070,7 @@ static int ahash_finup_ctx(struct ahash_request *req)
10991070
if (ret)
11001071
goto unmap_ctx;
11011072

1102-
edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
1103-
digestsize);
1104-
if (dma_mapping_error(jrdev, edesc->dst_dma)) {
1105-
dev_err(jrdev, "unable to map dst\n");
1106-
ret = -ENOMEM;
1107-
goto unmap_ctx;
1108-
}
1073+
append_seq_out_ptr(desc, state->ctx_dma, digestsize, 0);
11091074

11101075
#ifdef DEBUG
11111076
print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
@@ -1118,7 +1083,7 @@ static int ahash_finup_ctx(struct ahash_request *req)
11181083

11191084
return -EINPROGRESS;
11201085
unmap_ctx:
1121-
ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
1086+
ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_BIDIRECTIONAL);
11221087
kfree(edesc);
11231088
return ret;
11241089
}
@@ -1177,10 +1142,8 @@ static int ahash_digest(struct ahash_request *req)
11771142

11781143
desc = edesc->hw_desc;
11791144

1180-
edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
1181-
digestsize);
1182-
if (dma_mapping_error(jrdev, edesc->dst_dma)) {
1183-
dev_err(jrdev, "unable to map dst\n");
1145+
ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1146+
if (ret) {
11841147
ahash_unmap(jrdev, edesc, req, digestsize);
11851148
kfree(edesc);
11861149
return -ENOMEM;
@@ -1195,7 +1158,7 @@ static int ahash_digest(struct ahash_request *req)
11951158
if (!ret) {
11961159
ret = -EINPROGRESS;
11971160
} else {
1198-
ahash_unmap(jrdev, edesc, req, digestsize);
1161+
ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
11991162
kfree(edesc);
12001163
}
12011164

@@ -1237,12 +1200,9 @@ static int ahash_final_no_ctx(struct ahash_request *req)
12371200
append_seq_in_ptr(desc, state->buf_dma, buflen, 0);
12381201
}
12391202

1240-
edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
1241-
digestsize);
1242-
if (dma_mapping_error(jrdev, edesc->dst_dma)) {
1243-
dev_err(jrdev, "unable to map dst\n");
1203+
ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1204+
if (ret)
12441205
goto unmap;
1245-
}
12461206

12471207
#ifdef DEBUG
12481208
print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
@@ -1253,7 +1213,7 @@ static int ahash_final_no_ctx(struct ahash_request *req)
12531213
if (!ret) {
12541214
ret = -EINPROGRESS;
12551215
} else {
1256-
ahash_unmap(jrdev, edesc, req, digestsize);
1216+
ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
12571217
kfree(edesc);
12581218
}
12591219

@@ -1464,12 +1424,9 @@ static int ahash_finup_no_ctx(struct ahash_request *req)
14641424
goto unmap;
14651425
}
14661426

1467-
edesc->dst_dma = map_seq_out_ptr_result(desc, jrdev, req->result,
1468-
digestsize);
1469-
if (dma_mapping_error(jrdev, edesc->dst_dma)) {
1470-
dev_err(jrdev, "unable to map dst\n");
1427+
ret = map_seq_out_ptr_ctx(desc, jrdev, state, digestsize);
1428+
if (ret)
14711429
goto unmap;
1472-
}
14731430

14741431
#ifdef DEBUG
14751432
print_hex_dump(KERN_ERR, "jobdesc@"__stringify(__LINE__)": ",
@@ -1480,7 +1437,7 @@ static int ahash_finup_no_ctx(struct ahash_request *req)
14801437
if (!ret) {
14811438
ret = -EINPROGRESS;
14821439
} else {
1483-
ahash_unmap(jrdev, edesc, req, digestsize);
1440+
ahash_unmap_ctx(jrdev, edesc, req, digestsize, DMA_FROM_DEVICE);
14841441
kfree(edesc);
14851442
}
14861443

0 commit comments

Comments
 (0)