1. 16 4月, 2020 2 次提交
    • I
      crypto: caam - fix use-after-free KASAN issue for AEAD algorithms · 5ed1e8b8
      Iuliana Prodan 提交于
      Here's the KASAN report:
      BUG: KASAN: use-after-free in aead_crypt_done+0x60/0xd8
      Read of size 1 at addr ffff00002303f014 by task swapper/0/0
      
      CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.6.0-rc1-00162-gfcb90d51 #58
      Hardware name: LS1046A RDB Board (DT)
      Call trace:
       dump_backtrace+0x0/0x260
       show_stack+0x14/0x20
       dump_stack+0xe8/0x144
       print_address_description.isra.11+0x64/0x348
       __kasan_report+0x11c/0x230
       kasan_report+0xc/0x18
       __asan_load1+0x5c/0x68
       aead_crypt_done+0x60/0xd8
       caam_jr_dequeue+0x390/0x608
       tasklet_action_common.isra.13+0x1ec/0x230
       tasklet_action+0x24/0x30
       efi_header_end+0x1a4/0x370
       irq_exit+0x114/0x128
       __handle_domain_irq+0x80/0xe0
       gic_handle_irq+0x50/0xa0
       el1_irq+0xb8/0x180
       _raw_spin_unlock_irq+0x2c/0x78
       finish_task_switch+0xa4/0x2f8
       __schedule+0x3a4/0x890
       schedule_idle+0x28/0x50
       do_idle+0x22c/0x338
       cpu_startup_entry+0x24/0x40
       rest_init+0xf8/0x10c
       arch_call_rest_init+0xc/0x14
       start_kernel+0x774/0x7b4
      
      Allocated by task 263:
       save_stack+0x24/0xb0
       __kasan_kmalloc.isra.10+0xc4/0xe0
       kasan_kmalloc+0xc/0x18
       __kmalloc+0x178/0x2b8
       aead_edesc_alloc+0x1b4/0xbf0
       ipsec_gcm_encrypt+0xd4/0x140
       crypto_aead_encrypt+0x50/0x68
       test_aead_vec_cfg+0x498/0xec0
       test_aead_vec+0x110/0x200
       alg_test_aead+0xfc/0x680
       alg_test.part.44+0x114/0x4a0
       alg_test+0x1c/0x60
       cryptomgr_test+0x34/0x58
       kthread+0x1b8/0x1c0
       ret_from_fork+0x10/0x18
      
      Freed by task 0:
       save_stack+0x24/0xb0
       __kasan_slab_free+0x10c/0x188
       kasan_slab_free+0x10/0x18
       kfree+0x7c/0x298
       aead_crypt_done+0x58/0xd8
       caam_jr_dequeue+0x390/0x608
       tasklet_action_common.isra.13+0x1ec/0x230
       tasklet_action+0x24/0x30
       efi_header_end+0x1a4/0x370
      
      The buggy address belongs to the object at ffff00002303f000
       which belongs to the cache dma-kmalloc-128 of size 128
      The buggy address is located 20 bytes inside of
       128-byte region [ffff00002303f000, ffff00002303f080)
      The buggy address belongs to the page:
      page:fffffe00006c0fc0 refcount:1 mapcount:0 mapping:ffff00093200c000 index:0x0
      flags: 0xffff00000000200(slab)
      raw: 0ffff00000000200 dead000000000100 dead000000000122 ffff00093200c000
      raw: 0000000000000000 0000000080100010 00000001ffffffff 0000000000000000
      page dumped because: kasan: bad access detected
      
      Memory state around the buggy address:
       ffff00002303ef00: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
       ffff00002303ef80: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
      >ffff00002303f000: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                               ^
       ffff00002303f080: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
       ffff00002303f100: fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc fc
      
      Fixes: 1c240226 ("crypto: caam - add crypto_engine support for AEAD algorithms")
      Signed-off-by: NIuliana Prodan <iuliana.prodan@nxp.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      5ed1e8b8
    • I
      crypto: caam - fix use-after-free KASAN issue for SKCIPHER algorithms · 5af4e8d4
      Iuliana Prodan 提交于
      Here's the KASAN report:
      BUG: KASAN: use-after-free in skcipher_crypt_done+0xe8/0x1a8
      Read of size 1 at addr ffff00002304001c by task swapper/0/0
      
      CPU: 0 PID: 0 Comm: swapper/0 Not tainted 5.6.0-rc1-00162-gfcb90d51 #57
      Hardware name: LS1046A RDB Board (DT)
      Call trace:
       dump_backtrace+0x0/0x260
       show_stack+0x14/0x20
       dump_stack+0xe8/0x144
       print_address_description.isra.11+0x64/0x348
       __kasan_report+0x11c/0x230
       kasan_report+0xc/0x18
       __asan_load1+0x5c/0x68
       skcipher_crypt_done+0xe8/0x1a8
       caam_jr_dequeue+0x390/0x608
       tasklet_action_common.isra.13+0x1ec/0x230
       tasklet_action+0x24/0x30
       efi_header_end+0x1a4/0x370
       irq_exit+0x114/0x128
       __handle_domain_irq+0x80/0xe0
       gic_handle_irq+0x50/0xa0
       el1_irq+0xb8/0x180
       _raw_spin_unlock_irq+0x2c/0x78
       finish_task_switch+0xa4/0x2f8
       __schedule+0x3a4/0x890
       schedule_idle+0x28/0x50
       do_idle+0x22c/0x338
       cpu_startup_entry+0x24/0x40
       rest_init+0xf8/0x10c
       arch_call_rest_init+0xc/0x14
       start_kernel+0x774/0x7b4
      
      Allocated by task 263:
       save_stack+0x24/0xb0
       __kasan_kmalloc.isra.10+0xc4/0xe0
       kasan_kmalloc+0xc/0x18
       __kmalloc+0x178/0x2b8
       skcipher_edesc_alloc+0x21c/0x1018
       skcipher_encrypt+0x84/0x150
       crypto_skcipher_encrypt+0x50/0x68
       test_skcipher_vec_cfg+0x4d4/0xc10
       test_skcipher_vec+0xf8/0x1d8
       alg_test_skcipher+0xec/0x230
       alg_test.part.44+0x114/0x4a0
       alg_test+0x1c/0x60
       cryptomgr_test+0x34/0x58
       kthread+0x1b8/0x1c0
       ret_from_fork+0x10/0x18
      
      Freed by task 0:
       save_stack+0x24/0xb0
       __kasan_slab_free+0x10c/0x188
       kasan_slab_free+0x10/0x18
       kfree+0x7c/0x298
       skcipher_crypt_done+0xe0/0x1a8
       caam_jr_dequeue+0x390/0x608
       tasklet_action_common.isra.13+0x1ec/0x230
       tasklet_action+0x24/0x30
       efi_header_end+0x1a4/0x370
      
      The buggy address belongs to the object at ffff000023040000
       which belongs to the cache dma-kmalloc-512 of size 512
      The buggy address is located 28 bytes inside of
       512-byte region [ffff000023040000, ffff000023040200)
      The buggy address belongs to the page:
      page:fffffe00006c1000 refcount:1 mapcount:0 mapping:ffff00093200c400 index:0x0 compound_mapcount: 0
      flags: 0xffff00000010200(slab|head)
      raw: 0ffff00000010200 dead000000000100 dead000000000122 ffff00093200c400
      raw: 0000000000000000 0000000080100010 00000001ffffffff 0000000000000000
      page dumped because: kasan: bad access detected
      
      Memory state around the buggy address:
       ffff00002303ff00: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
       ffff00002303ff80: ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff ff
      >ffff000023040000: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
                                  ^
       ffff000023040080: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
       ffff000023040100: fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb fb
      
      Fixes: ee38767f ("crypto: caam - support crypto_engine framework for SKCIPHER algorithms")
      Signed-off-by: NIuliana Prodan <iuliana.prodan@nxp.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      5af4e8d4
  2. 06 3月, 2020 1 次提交
  3. 22 2月, 2020 4 次提交
  4. 09 1月, 2020 1 次提交
    • E
      crypto: remove CRYPTO_TFM_RES_BAD_KEY_LEN · 674f368a
      Eric Biggers 提交于
      The CRYPTO_TFM_RES_BAD_KEY_LEN flag was apparently meant as a way to
      make the ->setkey() functions provide more information about errors.
      
      However, no one actually checks for this flag, which makes it pointless.
      
      Also, many algorithms fail to set this flag when given a bad length key.
      Reviewing just the generic implementations, this is the case for
      aes-fixed-time, cbcmac, echainiv, nhpoly1305, pcrypt, rfc3686, rfc4309,
      rfc7539, rfc7539esp, salsa20, seqiv, and xcbc.  But there are probably
      many more in arch/*/crypto/ and drivers/crypto/.
      
      Some algorithms can even set this flag when the key is the correct
      length.  For example, authenc and authencesn set it when the key payload
      is malformed in any way (not just a bad length), the atmel-sha and ccree
      drivers can set it if a memory allocation fails, and the chelsio driver
      sets it for bad auth tag lengths, not just bad key lengths.
      
      So even if someone actually wanted to start checking this flag (which
      seems unlikely, since it's been unused for a long time), there would be
      a lot of work needed to get it working correctly.  But it would probably
      be much better to go back to the drawing board and just define different
      return values, like -EINVAL if the key is invalid for the algorithm vs.
      -EKEYREJECTED if the key was rejected by a policy like "no weak keys".
      That would be much simpler, less error-prone, and easier to test.
      
      So just remove this flag.
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Reviewed-by: NHoria Geantă <horia.geanta@nxp.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      674f368a
  5. 30 8月, 2019 1 次提交
    • A
      crypto: caam - make CAAM_PTR_SZ dynamic · 1a3daadc
      Andrey Smirnov 提交于
      In order to be able to configure CAAM pointer size at run-time, which
      needed to support i.MX8MQ, which is 64-bit SoC with 32-bit pointer
      size, convert CAAM_PTR_SZ to refer to a global variable of the same
      name ("caam_ptr_sz") and adjust the rest of the code accordingly. No
      functional change intended.
      Signed-off-by: NAndrey Smirnov <andrew.smirnov@gmail.com>
      Cc: Chris Spencer <christopher.spencer@sea.co.uk>
      Cc: Cory Tusar <cory.tusar@zii.aero>
      Cc: Chris Healy <cphealy@gmail.com>
      Cc: Lucas Stach <l.stach@pengutronix.de>
      Cc: Horia Geantă <horia.geanta@nxp.com>
      Cc: Aymen Sghaier <aymen.sghaier@nxp.com>
      Cc: Leonard Crestez <leonard.crestez@nxp.com>
      Cc: linux-crypto@vger.kernel.org
      Cc: linux-kernel@vger.kernel.org
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      1a3daadc
  6. 22 8月, 2019 1 次提交
  7. 09 8月, 2019 7 次提交
  8. 20 6月, 2019 2 次提交
    • H
      crypto: caam - update IV using HW support · 334d37c9
      Horia Geantă 提交于
      Modify drivers to perform skcipher IV update using the crypto engine,
      instead of performing the operation in SW.
      
      Besides being more efficient, this also fixes IV update for CTR mode.
      
      Output HW S/G table is appended with an entry pointing to the same
      IV buffer used as input (which is now mapped BIDIRECTIONAL).
      
      AS (Algorithm State) parameter of the OPERATION command is changed
      from INIFINAL to INIT in descriptors used by ctr(aes), cbc(aes).
      This is needed since in case FINAL bit is set, HW skips IV updating
      in the Context Register for the last data block.
      Signed-off-by: NHoria Geantă <horia.geanta@nxp.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      334d37c9
    • H
      crypto: caam - use len instead of nents for bulding HW S/G table · 059d73ee
      Horia Geantă 提交于
      Currently, conversion of SW S/G table into HW S/G layout relies on
      nents returned by sg_nents_for_len(sg, len).
      However this leaves the possibility of HW S/G referencing more data
      then needed: since buffer length in HW S/G entries is filled using
      sg_dma_len(sg), the last entry in HW S/G table might have a length
      that is bigger than needed for the crypto request.
      
      This way of S/G table conversion is fine, unless after converting a table
      more entries have to be appended to the HW S/G table.
      In this case, crypto engine would access data from the S/G entry having
      the incorrect length, instead of advancing in the S/G table.
      This situation doesn't exist, but the upcoming implementation of
      IV update for skcipher algorithms needs to add a S/G entry after
      req->dst S/G (corresponding to output IV).
      Signed-off-by: NHoria Geantă <horia.geanta@nxp.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      059d73ee
  9. 06 6月, 2019 1 次提交
    • A
      crypto: caam - limit output IV to CBC to work around CTR mode DMA issue · ed527b13
      Ard Biesheuvel 提交于
      The CAAM driver currently violates an undocumented and slightly
      controversial requirement imposed by the crypto stack that a buffer
      referred to by the request structure via its virtual address may not
      be modified while any scatterlists passed via the same request
      structure are mapped for inbound DMA.
      
      This may result in errors like
      
        alg: aead: decryption failed on test 1 for gcm_base(ctr-aes-caam,ghash-generic): ret=74
        alg: aead: Failed to load transform for gcm(aes): -2
      
      on non-cache coherent systems, due to the fact that the GCM driver
      passes an IV buffer by virtual address which shares a cacheline with
      the auth_tag buffer passed via a scatterlist, resulting in corruption
      of the auth_tag when the IV is updated while the DMA mapping is live.
      
      Since the IV that is returned to the caller is only valid for CBC mode,
      and given that the in-kernel users of CBC (such as CTS) don't trigger the
      same issue as the GCM driver, let's just disable the output IV generation
      for all modes except CBC for the time being.
      
      Fixes: 854b06f7 ("crypto: caam - properly set IV after {en,de}crypt")
      Cc: Horia Geanta <horia.geanta@nxp.com>
      Cc: Iuliana Prodan <iuliana.prodan@nxp.com>
      Reported-by: NSascha Hauer <s.hauer@pengutronix.de>
      Cc: <stable@vger.kernel.org>
      Signed-off-by: NArd Biesheuvel <ard.biesheuvel@linaro.org>
      Reviewed-by: NHoria Geanta <horia.geanta@nxp.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      ed527b13
  10. 30 5月, 2019 4 次提交
  11. 23 5月, 2019 4 次提交
    • S
      crypto: caam - print debugging hex dumps after unmapping · bb992bc4
      Sascha Hauer 提交于
      For encryption the destination pointer was still mapped, so the hex dump
      may be wrong. The IV still contained the input IV while printing instead
      of the output IV as intended.
      
      For decryption the destination pointer was still mapped, so the hex dump
      may be wrong. The IV dump was correct.
      
      Do the hex dumps consistenly after the buffers have been unmapped and
      in case of IV copied to their final destination.
      Signed-off-by: NSascha Hauer <s.hauer@pengutronix.de>
      Reviewed-by: NHoria Geantă <horia.geanta@nxp.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      bb992bc4
    • H
      crypto: caam - convert top level drivers to libraries · 1b46c90c
      Horia Geantă 提交于
      Currently we allow top level code, i.e. that which sits between the
      low level (HW-specific) drivers and crypto API, to be built as several
      drivers: caamalg, caamhash, caam_pkc, caamrng, caamalg_qi.
      
      There is no advantage in this, more it interferes with adding support
      for deferred probing (there are no corresponding devices and thus
      no bus).
      
      Convert these drivers and call init() / exit() manually at the right
      time.
      Move algorithms initialization at JR probe / remove time:
      -the first probed JR registers the crypto algs
      -the last removed JR unregisters the crypto algs
      
      Note: caam_qi_init() is called before JR platform devices creation
      (of_populate_bus()), such that QI interface is initialized when
      the caam/qi algorithms are registered in the JR driver (by calling
      caam_qi_algapi_init().
      
      While here, fix the Kconfig entries under CRYPTO_DEV_FSL_CAAM_JR
      to be aligned.
      Signed-off-by: NHoria Geantă <horia.geanta@nxp.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      1b46c90c
    • H
      crypto: caam - fix S/G table passing page boundary · a5e5c133
      Horia Geantă 提交于
      According to CAAM RM:
      -crypto engine reads 4 S/G entries (64 bytes) at a time,
      even if the S/G table has fewer entries
      -it's the responsibility of the user / programmer to make sure
      this HW behaviour has no side effect
      
      The drivers do not take care of this currently, leading to IOMMU faults
      when the S/G table ends close to a page boundary - since only one page
      is DMA mapped, while CAAM's DMA engine accesses two pages.
      
      Fix this by rounding up the number of allocated S/G table entries
      to a multiple of 4.
      Note that in case of two *contiguous* S/G tables, only the last table
      might needs extra entries.
      Signed-off-by: NHoria Geantă <horia.geanta@nxp.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      a5e5c133
    • H
      crypto: caam - avoid S/G table fetching for AEAD zero-length output · dcd9c76e
      Horia Geantă 提交于
      When enabling IOMMU support, the following issue becomes visible
      in the AEAD zero-length case.
      
      Even though the output sequence length is set to zero, the crypto engine
      tries to prefetch 4 S/G table entries (since SGF bit is set
      in SEQ OUT PTR command - which is either generated in SW in case of
      caam/jr or in HW in case of caam/qi, caam/qi2).
      The DMA read operation will trigger an IOMMU fault since the address in
      the SEQ OUT PTR is "dummy" (set to zero / not obtained via DMA API
      mapping).
      
      1. In case of caam/jr, avoid the IOMMU fault by clearing the SGF bit
      in SEQ OUT PTR command.
      
      2. In case of caam/qi - setting address, bpid, length to zero for output
      entry in the compound frame has a special meaning (cf. CAAM RM):
      "Output frame = Unspecified, Input address = Y. A unspecified frame is
      indicated by an unused SGT entry (an entry in which the Address, Length,
      and BPID fields are all zero). SEC obtains output buffers from BMan as
      prescribed by the preheader."
      
      Since no output buffers are needed, modify the preheader by setting
      (ABS = 1, ADDBUF = 0):
      -"ABS = 1 means obtain the number of buffers in ADDBUF (0 or 1) from
      the pool POOL ID"
      -ADDBUF: "If ABS is set, ADD BUF specifies whether to allocate
      a buffer or not"
      
      3. In case of caam/qi2, since engine:
      -does not support FLE[FMT]=2'b11 ("unused" entry) mentioned in DPAA2 RM
      -requires output entry to be present, even if not used
      the solution chosen is to leave output frame list entry zeroized.
      
      Fixes: 763069ba ("crypto: caam - handle zero-length AEAD output")
      Signed-off-by: NHoria Geantă <horia.geanta@nxp.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      dcd9c76e
  12. 09 5月, 2019 1 次提交
  13. 25 4月, 2019 1 次提交
    • H
      crypto: caam/jr - update gcm detection logic · df80bfd3
      Horia Geantă 提交于
      GCM detection logic has to change for two reasons:
      -some CAAM instantiations with Era < 10, even though they have AES LP,
      they now support GCM mode
      -Era 10 upwards, there is a dedicated bit in AESA_VERSION[AESA_MISC]
      field for GCM support
      
      For Era 9 and earlier, all AES accelerator versions support GCM,
      except for AES LP (CHAVID_LS[AESVID]=3) with revision CRNR[AESRN] < 8.
      
      For Era 10 and later, bit 9 of the AESA_VERSION register should be used
      to detect GCM support in AES accelerator.
      
      Note: caam/qi and caam/qi2 are drivers for QI (Queue Interface), which
      is used in DPAA-based SoCs; for now, we rely on CAAM having an AES HP
      and this AES accelerator having support for GCM.
      Signed-off-by: NHoria Geantă <horia.geanta@nxp.com>
      Reviewed-by: NIuliana Prodan <iuliana.prodan@nxp.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      df80bfd3
  14. 18 4月, 2019 1 次提交
  15. 07 3月, 2019 1 次提交
  16. 15 2月, 2019 3 次提交
  17. 08 2月, 2019 1 次提交
  18. 01 2月, 2019 1 次提交
  19. 10 1月, 2019 1 次提交
  20. 20 11月, 2018 1 次提交
    • E
      crypto: chacha20-generic - refactor to allow varying number of rounds · 1ca1b917
      Eric Biggers 提交于
      In preparation for adding XChaCha12 support, rename/refactor
      chacha20-generic to support different numbers of rounds.  The
      justification for needing XChaCha12 support is explained in more detail
      in the patch "crypto: chacha - add XChaCha12 support".
      
      The only difference between ChaCha{8,12,20} are the number of rounds
      itself; all other parts of the algorithm are the same.  Therefore,
      remove the "20" from all definitions, structures, functions, files, etc.
      that will be shared by all ChaCha versions.
      
      Also make ->setkey() store the round count in the chacha_ctx (previously
      chacha20_ctx).  The generic code then passes the round count through to
      chacha_block().  There will be a ->setkey() function for each explicitly
      allowed round count; the encrypt/decrypt functions will be the same.  I
      decided not to do it the opposite way (same ->setkey() function for all
      round counts, with different encrypt/decrypt functions) because that
      would have required more boilerplate code in architecture-specific
      implementations of ChaCha and XChaCha.
      Reviewed-by: NArd Biesheuvel <ard.biesheuvel@linaro.org>
      Acked-by: NMartin Willi <martin@strongswan.org>
      Signed-off-by: NEric Biggers <ebiggers@google.com>
      Signed-off-by: NHerbert Xu <herbert@gondor.apana.org.au>
      1ca1b917
  21. 16 11月, 2018 1 次提交