Re: [RFC PATCH] crypto: flush poison data

LABBE Corentin
2022-07-01T14:55:31Z

Le Fri, Jul 01, 2022 at 03:36:14PM +0100, Andre Przywara a écrit :
> On Fri, 1 Jul 2022 13:27:35 +0000
> Corentin Labbe <clabbe@baylibre.com> wrote:
>
> Hi,
>
> > On my Allwinner D1 nezha, the sun8i-ce fail self-tests due to:
> > alg: skcipher: cbc-des3-sun8i-ce encryption overran dst buffer on test vector 0
> >
> > In fact the buffer is not overran by device but by the dma_map_single() operation.
> >
> > To prevent any corruption of the poisoned data, simply flush them before
> > giving the buffer to the tested driver.
> >
> > Signed-off-by: Corentin Labbe <clabbe@baylibre.com>
> > ---
> >
> > Hello
> >
> > I put this patch as RFC, since this behavour happen only on non yet merged RISCV code.
> > (Mostly riscv: implement Zicbom-based CMO instructions + the t-head variant)
> >
> > Regards
> >
> > crypto/testmgr.c | 3 +++
> > 1 file changed, 3 insertions(+)
> >
> > diff --git a/crypto/testmgr.c b/crypto/testmgr.c
> > index c59bd9e07978..187163e2e593 100644
> > --- a/crypto/testmgr.c
> > +++ b/crypto/testmgr.c
> > @@ -19,6 +19,7 @@
> > #include <crypto/aead.h>
> > #include <crypto/hash.h>
> > #include <crypto/skcipher.h>
> > +#include <linux/cacheflush.h>
> > #include <linux/err.h>
> > #include <linux/fips.h>
> > #include <linux/module.h>
> > @@ -205,6 +206,8 @@ static void testmgr_free_buf(char *buf[XBUFSIZE])
> > static inline void testmgr_poison(void *addr, size_t len)
> > {
> > memset(addr, TESTMGR_POISON_BYTE, len);
> > + /* Be sure data is written to prevent corruption from some DMA sync */
> > + flush_icache_range((unsigned long)addr, (unsigned long)addr + len);
>
> As Ben already mentioned, this looks like having nothing to do with the I
> cache. I guess you picked that because it does the required cache cleaning
> and doesn't require a vma parameter?

The reality is simpler, I just copied what did drivers/crypto/xilinx/zynqmp-sha.c

>
> But more importantly: I think drivers shouldn't do explicit cache
> maintenance, this is what the DMA API is for.
> So if you get DMA corruption, then this points to some flaw in the DMA API
> usage: either the buffer belongs to the CPU, then the device must not write
> to it. Or the buffer belongs to the device, then the CPU cannot expect to
> write to that without that data potentially getting corrupted.

The device does nothing wrong, I removed all sun8i-ce device action (and kept DMA API actions) and the the whole buffer is still corrupted.
Anyway if the driver was doing something wrong, it should have fail on arm or arm64.

See my previous report https://lore.kernel.org/lkml/YllWTN+15CoskNBt@Red/ which show the problem (The invalidated size is bigger than the dma_sync length parameter)