16 Jul, 2020

1 commit

  • The flag CRYPTO_ALG_ASYNC is "inherited" in the sense that when a
    template is instantiated, the template will have CRYPTO_ALG_ASYNC set if
    any of the algorithms it uses has CRYPTO_ALG_ASYNC set.

    We'd like to add a second flag (CRYPTO_ALG_ALLOCATES_MEMORY) that gets
    "inherited" in the same way. This is difficult because the handling of
    CRYPTO_ALG_ASYNC is hardcoded everywhere. Address this by:

    - Add CRYPTO_ALG_INHERITED_FLAGS, which contains the set of flags that
    have these inheritance semantics.

    - Add crypto_algt_inherited_mask(), for use by template ->create()
    methods. It returns any of these flags that the user asked to be
    unset and thus must be passed in the 'mask' to crypto_grab_*().

    - Also modify crypto_check_attr_type() to handle computing the 'mask'
    so that most templates can just use this.

    - Make crypto_grab_*() propagate these flags to the template instance
    being created so that templates don't have to do this themselves.

    Make crypto/simd.c propagate these flags too, since it "wraps" another
    algorithm, similar to a template.

    Based on a patch by Mikulas Patocka
    (https://lore.kernel.org/r/alpine.LRH.2.02.2006301414580.30526@file01.intranet.prod.int.rdu2.redhat.com).

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     

08 May, 2020

1 commit


16 Apr, 2020

2 commits

  • There are two problems in crypto_spawn_alg. First of all it may
    return spawn->alg even if spawn->dead is set. This results in a
    double-free as detected by syzbot.

    Secondly the setting of the DYING flag is racy because we hold
    the read-lock instead of the write-lock. We should instead call
    crypto_shoot_alg in a safe manner by gaining a refcount, dropping
    the lock, and then releasing the refcount.

    This patch fixes both problems.

    Reported-by: syzbot+fc0674cde00b66844470@syzkaller.appspotmail.com
    Fixes: 4f87ee118d16 ("crypto: api - Do not zap spawn->alg")
    Fixes: 73669cc55646 ("crypto: api - Fix race condition in...")
    Cc:
    Signed-off-by: Herbert Xu

    Herbert Xu
     
  • Currently after any algorithm is registered and tested, there's an
    unnecessary request_module("cryptomgr") even if it's already loaded.
    Also, CRYPTO_MSG_ALG_LOADED is sent twice, and thus if the algorithm is
    "crct10dif", lib/crc-t10dif.c replaces the tfm twice rather than once.

    This occurs because CRYPTO_MSG_ALG_LOADED is sent using
    crypto_probing_notify(), which tries to load "cryptomgr" if the
    notification is not handled (NOTIFY_DONE). This doesn't make sense
    because "cryptomgr" doesn't handle this notification.

    Fix this by using crypto_notify() instead of crypto_probing_notify().

    Fixes: dd8b083f9a5e ("crypto: api - Introduce notifier for new crypto algorithms")
    Cc: # v4.20+
    Cc: Martin K. Petersen
    Signed-off-by: Eric Biggers
    Reviewed-by: Martin K. Petersen
    Signed-off-by: Herbert Xu

    Eric Biggers
     

09 Jan, 2020

6 commits

  • Now that all templates provide a ->create() method which creates an
    instance, installs a strongly-typed ->free() method directly to it, and
    registers it, the older ->alloc() and ->free() methods in
    'struct crypto_template' are no longer used. Remove them.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • Now that crypto_init_spawn() is only called by crypto_grab_spawn(),
    simplify things by moving its functionality into crypto_grab_spawn().

    In the process of doing this, also be more consistent about when the
    spawn and instance are updated, and remove the crypto_spawn::dropref
    flag since now it's always set.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • Remove lots of helper functions that were previously used for
    instantiating crypto templates, but are now unused:

    - crypto_get_attr_alg() and similar functions looked up an inner
    algorithm directly from a template parameter. These were replaced
    with getting the algorithm's name, then calling crypto_grab_*().

    - crypto_init_spawn2() and similar functions initialized a spawn, given
    an algorithm. Similarly, these were replaced with crypto_grab_*().

    - crypto_alloc_instance() and similar functions allocated an instance
    with a single spawn, given the inner algorithm. These aren't useful
    anymore since crypto_grab_*() need the instance allocated first.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • Currently, crypto_spawn::inst is first used temporarily to pass the
    instance to crypto_grab_spawn(). Then crypto_init_spawn() overwrites it
    with crypto_spawn::next, which shares the same union. Finally,
    crypto_spawn::inst is set again when the instance is registered.

    Make this less convoluted by just passing the instance as an argument to
    crypto_grab_spawn() instead.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • To allow further simplifying template ->create() functions, make
    crypto_grab_spawn() handle an ERR_PTR() name by passing back the error.

    For most templates, this will allow the result of crypto_attr_alg_name()
    to be passed directly to crypto_grab_*(), rather than first having to
    assign it to a variable [where it can then potentially be misused, as it
    was in the rfc7539 template prior to commit 5e27f38f1f3f ("crypto:
    chacha20poly1305 - set cra_name correctly")] and check it for error.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • Make crypto_drop_spawn() do nothing when the spawn hasn't been
    initialized with an algorithm yet. This will allow simplifying error
    handling in all the template ->create() functions, since on error they
    will be able to just call their usual "free instance" function, rather
    than having to handle dropping just the spawns that have been
    initialized so far.

    This does assume the spawn starts out zero-filled, but that's always the
    case since instances are allocated with kzalloc(). And some other code
    already assumes this anyway.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     

27 Dec, 2019

1 commit

  • This patch changes crypto_grab_spawn to retain the reference count
    on the algorithm. This is because the caller needs to access the
    algorithm parameters and without the reference count the algorithm
    can be freed at any time.

    The reference count will be subsequently dropped by the crypto API
    once the instance has been registered. The helper crypto_drop_spawn
    will also conditionally drop the reference count depending on whether
    it has been registered.

    Note that the code is actually added to crypto_init_spawn. However,
    unless the caller activates this by setting spawn->dropref beforehand
    then nothing happens. The only caller that sets dropref is currently
    crypto_grab_spawn.

    Once all legacy users of crypto_init_spawn disappear, then we can
    kill the dropref flag.

    Internally each instance will maintain a list of its spawns prior
    to registration. This memory used by this list is shared with
    other fields that are only used after registration. In order for
    this to work a new flag spawn->registered is added to indicate
    whether spawn->inst can be used.

    Fixes: d6ef2f198d4c ("crypto: api - Add crypto_grab_spawn primitive")
    Signed-off-by: Herbert Xu

    Herbert Xu
     

20 Dec, 2019

2 commits

  • Some of the algorithm unregistration functions return -ENOENT when asked
    to unregister a non-registered algorithm, while others always return 0
    or always return void. But no users check the return value, except for
    two of the bulk unregistration functions which print a message on error
    but still always return 0 to their caller, and crypto_del_alg() which
    calls crypto_unregister_instance() which always returns 0.

    Since unregistering a non-registered algorithm is always a kernel bug
    but there isn't anything callers should do to handle this situation at
    runtime, let's simplify things by making all the unregistration
    functions return void, and moving the error message into
    crypto_unregister_alg() and upgrading it to a WARN().

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • When CONFIG_CRYPTO_MANAGER_EXTRA_TESTS=y, the first lookup of an
    algorithm that needs to be instantiated using a template will always get
    the generic implementation, even when an accelerated one is available.

    This happens because the extra self-tests for the accelerated
    implementation allocate the generic implementation for comparison
    purposes, and then crypto_alg_tested() for the generic implementation
    "fulfills" the original request (i.e. sets crypto_larval::adult).

    This patch fixes this by only fulfilling the original request if
    we are currently the best outstanding larval as judged by the
    priority. If we're not the best then we will ask all waiters on
    that larval request to retry the lookup.

    Note that this patch introduces a behaviour change when the module
    providing the new algorithm is unregistered during the process.
    Previously we would have failed with ENOENT, after the patch we
    will instead redo the lookup.

    Fixes: 9a8a6b3f0950 ("crypto: testmgr - fuzz hashes against...")
    Fixes: d435e10e67be ("crypto: testmgr - fuzz skciphers against...")
    Fixes: 40153b10d91c ("crypto: testmgr - fuzz AEADs against...")
    Reported-by: Eric Biggers
    Signed-off-by: Herbert Xu
    Reviewed-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Herbert Xu
     

11 Dec, 2019

4 commits


17 Nov, 2019

1 commit


09 Jul, 2019

1 commit

  • Pull crypto updates from Herbert Xu:
    "Here is the crypto update for 5.3:

    API:
    - Test shash interface directly in testmgr
    - cra_driver_name is now mandatory

    Algorithms:
    - Replace arc4 crypto_cipher with library helper
    - Implement 5 way interleave for ECB, CBC and CTR on arm64
    - Add xxhash
    - Add continuous self-test on noise source to drbg
    - Update jitter RNG

    Drivers:
    - Add support for SHA204A random number generator
    - Add support for 7211 in iproc-rng200
    - Fix fuzz test failures in inside-secure
    - Fix fuzz test failures in talitos
    - Fix fuzz test failures in qat"

    * 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6: (143 commits)
    crypto: stm32/hash - remove interruptible condition for dma
    crypto: stm32/hash - Fix hmac issue more than 256 bytes
    crypto: stm32/crc32 - rename driver file
    crypto: amcc - remove memset after dma_alloc_coherent
    crypto: ccp - Switch to SPDX license identifiers
    crypto: ccp - Validate the the error value used to index error messages
    crypto: doc - Fix formatting of new crypto engine content
    crypto: doc - Add parameter documentation
    crypto: arm64/aes-ce - implement 5 way interleave for ECB, CBC and CTR
    crypto: arm64/aes-ce - add 5 way interleave routines
    crypto: talitos - drop icv_ool
    crypto: talitos - fix hash on SEC1.
    crypto: talitos - move struct talitos_edesc into talitos.h
    lib/scatterlist: Fix mapping iterator when sg->offset is greater than PAGE_SIZE
    crypto/NX: Set receive window credits to max number of CRBs in RxFIFO
    crypto: asymmetric_keys - select CRYPTO_HASH where needed
    crypto: serpent - mark __serpent_setkey_sbox noinline
    crypto: testmgr - dynamically allocate crypto_shash
    crypto: testmgr - dynamically allocate testvec_config
    crypto: talitos - eliminate unneeded 'done' functions at build time
    ...

    Linus Torvalds
     

13 Jun, 2019

1 commit

  • Now that all algorithms explicitly set cra_driver_name, make it required
    for algorithm registration and remove the code that generated a default
    cra_driver_name.

    Also add an explicit check that cra_name is set too, since that's
    obviously required too, yet it didn't seem to be checked anywhere.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     

31 May, 2019

1 commit

  • Based on 1 normalized pattern(s):

    this program is free software you can redistribute it and or modify
    it under the terms of the gnu general public license as published by
    the free software foundation either version 2 of the license or at
    your option any later version

    extracted by the scancode license scanner the SPDX license identifier

    GPL-2.0-or-later

    has been chosen to replace the boilerplate/reference in 3029 file(s).

    Signed-off-by: Thomas Gleixner
    Reviewed-by: Allison Randal
    Cc: linux-spdx@vger.kernel.org
    Link: https://lkml.kernel.org/r/20190527070032.746973796@linutronix.de
    Signed-off-by: Greg Kroah-Hartman

    Thomas Gleixner
     

30 May, 2019

1 commit


25 Jan, 2019

1 commit


11 Jan, 2019

2 commits

  • It took me a while to notice the bug where the adiantum template left
    crypto_spawn::inst == NULL, because this only caused problems in certain
    cases where algorithms are dynamically loaded/unloaded.

    More improvements are needed, but for now make crypto_init_spawn()
    reject this case and WARN(), so this type of bug will be noticed
    immediately in the future.

    Note: I checked all callers and the adiantum template was the only place
    that had this wrong. So this WARN shouldn't trigger anymore.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • Now that all "blkcipher" templates have been converted to "skcipher",
    crypto_alloc_instance() is no longer used. And it's not useful any
    longer as it creates an old-style weakly typed instance rather than a
    new-style strongly typed instance. So remove it, and now that the name
    is freed up rename crypto_alloc_instance2() to crypto_alloc_instance().

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     

07 Dec, 2018

6 commits


28 Sep, 2018

1 commit


04 Sep, 2018

2 commits

  • Introduce a facility that can be used to receive a notification
    callback when a new algorithm becomes available. This can be used by
    existing crypto registrations to trigger a switch from a software-only
    algorithm to a hardware-accelerated version.

    A new CRYPTO_MSG_ALG_LOADED state is introduced to the existing crypto
    notification chain, and the register/unregister functions are exported
    so they can be called by subsystems outside of crypto.

    Signed-off-by: Martin K. Petersen
    Suggested-by: Herbert Xu
    Signed-off-by: Martin K. Petersen
    Signed-off-by: Herbert Xu

    Martin K. Petersen
     
  • In the quest to remove all stack VLA usage from the kernel[1], this
    exposes a new general upper bound on crypto blocksize and alignmask
    (higher than for the existing cipher limits) for VLA removal,
    and introduces new checks.

    At present, the highest cra_alignmask in the kernel is 63. The highest
    cra_blocksize is 144 (SHA3_224_BLOCK_SIZE, 18 8-byte words). For the
    new blocksize limit, I went with 160 (20 8-byte words).

    [1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com

    Signed-off-by: Kees Cook
    Signed-off-by: Herbert Xu

    Kees Cook
     

21 Apr, 2018

1 commit

  • In preparation for the removal of VLAs[1] from crypto code.
    We create 2 new compile-time constants: all ciphers implemented
    in Linux have a block size less than or equal to 16 bytes and
    the most demanding hw require 16 bytes alignment for the block
    buffer.
    We also enforce these limits in crypto_check_alg when a new
    cipher is registered.

    [1] http://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.com

    Signed-off-by: Salvatore Mesoraca
    Signed-off-by: Herbert Xu

    Salvatore Mesoraca
     

31 Mar, 2018

1 commit

  • This patch reverts commit 9c521a200bc3 ("crypto: api - remove
    instance when test failed") and fixes the underlying problem
    in a different way.

    To recap, prior to the reverted commit, an instance that fails
    a self-test is kept around. However, it would satisfy any new
    lookups against its name and therefore the system may accumlulate
    an unbounded number of failed instances for the same algorithm
    name.

    The reverted commit fixed it by unregistering the instance. Hoever,
    this still does not prevent the creation of the same failed instance
    over and over again each time the name is looked up.

    This patch fixes it by keeping the failed instance around, just as
    we would if it were a normal algorithm. However, the lookup code
    has been udpated so that we do not attempt to create another
    instance as long as this failed one is still registered. Of course,
    you could still force a new creation by deleting the instance from
    user-space.

    A new error (ELIBBAD) has been commandeered for this purpose and
    will be returned when all registered algorithm of a given name
    have failed the self-test.

    Signed-off-by: Herbert Xu

    Herbert Xu
     

01 Feb, 2018

1 commit

  • Pull crypto updates from Herbert Xu:
    "API:
    - Enforce the setting of keys for keyed aead/hash/skcipher
    algorithms.
    - Add multibuf speed tests in tcrypt.

    Algorithms:
    - Improve performance of sha3-generic.
    - Add native sha512 support on arm64.
    - Add v8.2 Crypto Extentions version of sha3/sm3 on arm64.
    - Avoid hmac nesting by requiring underlying algorithm to be unkeyed.
    - Add cryptd_max_cpu_qlen module parameter to cryptd.

    Drivers:
    - Add support for EIP97 engine in inside-secure.
    - Add inline IPsec support to chelsio.
    - Add RevB core support to crypto4xx.
    - Fix AEAD ICV check in crypto4xx.
    - Add stm32 crypto driver.
    - Add support for BCM63xx platforms in bcm2835 and remove bcm63xx.
    - Add Derived Key Protocol (DKP) support in caam.
    - Add Samsung Exynos True RNG driver.
    - Add support for Exynos5250+ SoCs in exynos PRNG driver"

    * 'linus' of git://git.kernel.org/pub/scm/linux/kernel/git/herbert/crypto-2.6: (166 commits)
    crypto: picoxcell - Fix error handling in spacc_probe()
    crypto: arm64/sha512 - fix/improve new v8.2 Crypto Extensions code
    crypto: arm64/sm3 - new v8.2 Crypto Extensions implementation
    crypto: arm64/sha3 - new v8.2 Crypto Extensions implementation
    crypto: testmgr - add new testcases for sha3
    crypto: sha3-generic - export init/update/final routines
    crypto: sha3-generic - simplify code
    crypto: sha3-generic - rewrite KECCAK transform to help the compiler optimize
    crypto: sha3-generic - fixes for alignment and big endian operation
    crypto: aesni - handle zero length dst buffer
    crypto: artpec6 - remove select on non-existing CRYPTO_SHA384
    hwrng: bcm2835 - Remove redundant dev_err call in bcm2835_rng_probe()
    crypto: stm32 - remove redundant dev_err call in stm32_cryp_probe()
    crypto: axis - remove unnecessary platform_get_resource() error check
    crypto: testmgr - test misuse of result in ahash
    crypto: inside-secure - make function safexcel_try_push_requests static
    crypto: aes-generic - fix aes-generic regression on powerpc
    crypto: chelsio - Fix indentation warning
    crypto: arm64/sha1-ce - get rid of literal pool
    crypto: arm64/sha2-ce - move the round constant table to .rodata section
    ...

    Linus Torvalds
     

05 Jan, 2018

3 commits

  • There is a message posted to the crypto notifier chain when an algorithm
    is unregistered, and when a template is registered or unregistered. But
    nothing is listening for those messages; currently there are only
    listeners for the algorithm request and registration messages.

    Get rid of these unused notifications for now.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • Reference counters should use refcount_t rather than atomic_t, since the
    refcount_t implementation can prevent overflows, reducing the
    exploitability of reference leak bugs. crypto_alg.cra_refcount is a
    reference counter with the usual semantics, so switch it over to
    refcount_t.

    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers
     
  • syzkaller triggered a NULL pointer dereference in crypto_remove_spawns()
    via a program that repeatedly and concurrently requests AEADs
    "authenc(cmac(des3_ede-asm),pcbc-aes-aesni)" and hashes "cmac(des3_ede)"
    through AF_ALG, where the hashes are requested as "untested"
    (CRYPTO_ALG_TESTED is set in ->salg_mask but clear in ->salg_feat; this
    causes the template to be instantiated for every request).

    Although AF_ALG users really shouldn't be able to request an "untested"
    algorithm, the NULL pointer dereference is actually caused by a
    longstanding race condition where crypto_remove_spawns() can encounter
    an instance which has had spawn(s) "grabbed" but hasn't yet been
    registered, resulting in ->cra_users still being NULL.

    We probably should properly initialize ->cra_users earlier, but that
    would require updating many templates individually. For now just fix
    the bug in a simple way that can easily be backported: make
    crypto_remove_spawns() treat a NULL ->cra_users list as empty.

    Reported-by: syzbot
    Cc: stable@vger.kernel.org
    Signed-off-by: Eric Biggers
    Signed-off-by: Herbert Xu

    Eric Biggers