Skip to content
Snippets Groups Projects
  1. Oct 11, 2022
    • Jason A. Donenfeld's avatar
      treewide: use get_random_bytes() when possible · 197173db
      Jason A. Donenfeld authored
      
      The prandom_bytes() function has been a deprecated inline wrapper around
      get_random_bytes() for several releases now, and compiles down to the
      exact same code. Replace the deprecated wrapper with a direct call to
      the real function. This was done as a basic find and replace.
      
      Reviewed-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      Reviewed-by: default avatarKees Cook <keescook@chromium.org>
      Reviewed-by: default avatarYury Norov <yury.norov@gmail.com>
      Reviewed-by: Christophe Leroy <christophe.leroy@csgroup.eu> # powerpc
      Acked-by: default avatarJakub Kicinski <kuba@kernel.org>
      Signed-off-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
      197173db
    • Jason A. Donenfeld's avatar
      treewide: use get_random_{u8,u16}() when possible, part 1 · 7e3cf084
      Jason A. Donenfeld authored
      
      Rather than truncate a 32-bit value to a 16-bit value or an 8-bit value,
      simply use the get_random_{u8,u16}() functions, which are faster than
      wasting the additional bytes from a 32-bit value. This was done
      mechanically with this coccinelle script:
      
      @@
      expression E;
      identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
      typedef u16;
      typedef __be16;
      typedef __le16;
      typedef u8;
      @@
      (
      - (get_random_u32() & 0xffff)
      + get_random_u16()
      |
      - (get_random_u32() & 0xff)
      + get_random_u8()
      |
      - (get_random_u32() % 65536)
      + get_random_u16()
      |
      - (get_random_u32() % 256)
      + get_random_u8()
      |
      - (get_random_u32() >> 16)
      + get_random_u16()
      |
      - (get_random_u32() >> 24)
      + get_random_u8()
      |
      - (u16)get_random_u32()
      + get_random_u16()
      |
      - (u8)get_random_u32()
      + get_random_u8()
      |
      - (__be16)get_random_u32()
      + (__be16)get_random_u16()
      |
      - (__le16)get_random_u32()
      + (__le16)get_random_u16()
      |
      - prandom_u32_max(65536)
      + get_random_u16()
      |
      - prandom_u32_max(256)
      + get_random_u8()
      |
      - E->inet_id = get_random_u32()
      + E->inet_id = get_random_u16()
      )
      
      @@
      identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
      typedef u16;
      identifier v;
      @@
      - u16 v = get_random_u32();
      + u16 v = get_random_u16();
      
      @@
      identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
      typedef u8;
      identifier v;
      @@
      - u8 v = get_random_u32();
      + u8 v = get_random_u8();
      
      @@
      identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
      typedef u16;
      u16 v;
      @@
      -  v = get_random_u32();
      +  v = get_random_u16();
      
      @@
      identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
      typedef u8;
      u8 v;
      @@
      -  v = get_random_u32();
      +  v = get_random_u8();
      
      // Find a potential literal
      @literal_mask@
      expression LITERAL;
      type T;
      identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
      position p;
      @@
      
              ((T)get_random_u32()@p & (LITERAL))
      
      // Examine limits
      @script:python add_one@
      literal << literal_mask.LITERAL;
      RESULT;
      @@
      
      value = None
      if literal.startswith('0x'):
              value = int(literal, 16)
      elif literal[0] in '123456789':
              value = int(literal, 10)
      if value is None:
              print("I don't know how to handle %s" % (literal))
              cocci.include_match(False)
      elif value < 256:
              coccinelle.RESULT = cocci.make_ident("get_random_u8")
      elif value < 65536:
              coccinelle.RESULT = cocci.make_ident("get_random_u16")
      else:
              print("Skipping large mask of %s" % (literal))
              cocci.include_match(False)
      
      // Replace the literal mask with the calculated result.
      @plus_one@
      expression literal_mask.LITERAL;
      position literal_mask.p;
      identifier add_one.RESULT;
      identifier FUNC;
      @@
      
      -       (FUNC()@p & (LITERAL))
      +       (RESULT() & LITERAL)
      
      Reviewed-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      Reviewed-by: default avatarKees Cook <keescook@chromium.org>
      Reviewed-by: default avatarYury Norov <yury.norov@gmail.com>
      Acked-by: default avatarJakub Kicinski <kuba@kernel.org>
      Acked-by: Toke Høiland-Jørgensen <toke@toke.dk> # for sch_cake
      Signed-off-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
      7e3cf084
    • Jason A. Donenfeld's avatar
      treewide: use prandom_u32_max() when possible, part 1 · 81895a65
      Jason A. Donenfeld authored
      
      Rather than incurring a division or requesting too many random bytes for
      the given range, use the prandom_u32_max() function, which only takes
      the minimum required bytes from the RNG and avoids divisions. This was
      done mechanically with this coccinelle script:
      
      @basic@
      expression E;
      type T;
      identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
      typedef u64;
      @@
      (
      - ((T)get_random_u32() % (E))
      + prandom_u32_max(E)
      |
      - ((T)get_random_u32() & ((E) - 1))
      + prandom_u32_max(E * XXX_MAKE_SURE_E_IS_POW2)
      |
      - ((u64)(E) * get_random_u32() >> 32)
      + prandom_u32_max(E)
      |
      - ((T)get_random_u32() & ~PAGE_MASK)
      + prandom_u32_max(PAGE_SIZE)
      )
      
      @multi_line@
      identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
      identifier RAND;
      expression E;
      @@
      
      -       RAND = get_random_u32();
              ... when != RAND
      -       RAND %= (E);
      +       RAND = prandom_u32_max(E);
      
      // Find a potential literal
      @literal_mask@
      expression LITERAL;
      type T;
      identifier get_random_u32 =~ "get_random_int|prandom_u32|get_random_u32";
      position p;
      @@
      
              ((T)get_random_u32()@p & (LITERAL))
      
      // Add one to the literal.
      @script:python add_one@
      literal << literal_mask.LITERAL;
      RESULT;
      @@
      
      value = None
      if literal.startswith('0x'):
              value = int(literal, 16)
      elif literal[0] in '123456789':
              value = int(literal, 10)
      if value is None:
              print("I don't know how to handle %s" % (literal))
              cocci.include_match(False)
      elif value == 2**32 - 1 or value == 2**31 - 1 or value == 2**24 - 1 or value == 2**16 - 1 or value == 2**8 - 1:
              print("Skipping 0x%x for cleanup elsewhere" % (value))
              cocci.include_match(False)
      elif value & (value + 1) != 0:
              print("Skipping 0x%x because it's not a power of two minus one" % (value))
              cocci.include_match(False)
      elif literal.startswith('0x'):
              coccinelle.RESULT = cocci.make_expr("0x%x" % (value + 1))
      else:
              coccinelle.RESULT = cocci.make_expr("%d" % (value + 1))
      
      // Replace the literal mask with the calculated result.
      @plus_one@
      expression literal_mask.LITERAL;
      position literal_mask.p;
      expression add_one.RESULT;
      identifier FUNC;
      @@
      
      -       (FUNC()@p & (LITERAL))
      +       prandom_u32_max(RESULT)
      
      @collapse_ret@
      type T;
      identifier VAR;
      expression E;
      @@
      
       {
      -       T VAR;
      -       VAR = (E);
      -       return VAR;
      +       return E;
       }
      
      @drop_var@
      type T;
      identifier VAR;
      @@
      
       {
      -       T VAR;
              ... when != VAR
       }
      
      Reviewed-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
      Reviewed-by: default avatarKees Cook <keescook@chromium.org>
      Reviewed-by: default avatarYury Norov <yury.norov@gmail.com>
      Reviewed-by: default avatarKP Singh <kpsingh@kernel.org>
      Reviewed-by: Jan Kara <jack@suse.cz> # for ext4 and sbitmap
      Reviewed-by: Christoph Böhmwalder <christoph.boehmwalder@linbit.com> # for drbd
      Acked-by: default avatarJakub Kicinski <kuba@kernel.org>
      Acked-by: Heiko Carstens <hca@linux.ibm.com> # for s390
      Acked-by: Ulf Hansson <ulf.hansson@linaro.org> # for mmc
      Acked-by: Darrick J. Wong <djwong@kernel.org> # for xfs
      Signed-off-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
      81895a65
  2. Oct 03, 2022
    • Alexander Potapenko's avatar
      crypto: kmsan: disable accelerated configs under KMSAN · 440fed95
      Alexander Potapenko authored
      KMSAN is unable to understand when initialized values come from assembly. 
      Disable accelerated configs in KMSAN builds to prevent false positive
      reports.
      
      Link: https://lkml.kernel.org/r/20220915150417.722975-27-glider@google.com
      
      
      Signed-off-by: default avatarAlexander Potapenko <glider@google.com>
      Cc: Alexander Viro <viro@zeniv.linux.org.uk>
      Cc: Alexei Starovoitov <ast@kernel.org>
      Cc: Andrey Konovalov <andreyknvl@gmail.com>
      Cc: Andrey Konovalov <andreyknvl@google.com>
      Cc: Andy Lutomirski <luto@kernel.org>
      Cc: Arnd Bergmann <arnd@arndb.de>
      Cc: Borislav Petkov <bp@alien8.de>
      Cc: Christoph Hellwig <hch@lst.de>
      Cc: Christoph Lameter <cl@linux.com>
      Cc: David Rientjes <rientjes@google.com>
      Cc: Dmitry Vyukov <dvyukov@google.com>
      Cc: Eric Biggers <ebiggers@google.com>
      Cc: Eric Biggers <ebiggers@kernel.org>
      Cc: Eric Dumazet <edumazet@google.com>
      Cc: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
      Cc: Herbert Xu <herbert@gondor.apana.org.au>
      Cc: Ilya Leoshkevich <iii@linux.ibm.com>
      Cc: Ingo Molnar <mingo@redhat.com>
      Cc: Jens Axboe <axboe@kernel.dk>
      Cc: Joonsoo Kim <iamjoonsoo.kim@lge.com>
      Cc: Kees Cook <keescook@chromium.org>
      Cc: Marco Elver <elver@google.com>
      Cc: Mark Rutland <mark.rutland@arm.com>
      Cc: Matthew Wilcox <willy@infradead.org>
      Cc: Michael S. Tsirkin <mst@redhat.com>
      Cc: Pekka Enberg <penberg@kernel.org>
      Cc: Peter Zijlstra <peterz@infradead.org>
      Cc: Petr Mladek <pmladek@suse.com>
      Cc: Stephen Rothwell <sfr@canb.auug.org.au>
      Cc: Steven Rostedt <rostedt@goodmis.org>
      Cc: Thomas Gleixner <tglx@linutronix.de>
      Cc: Vasily Gorbik <gor@linux.ibm.com>
      Cc: Vegard Nossum <vegard.nossum@oracle.com>
      Cc: Vlastimil Babka <vbabka@suse.cz>
      Signed-off-by: default avatarAndrew Morton <akpm@linux-foundation.org>
      440fed95
  3. Sep 24, 2022
  4. Sep 09, 2022
    • Ignat Korchagin's avatar
      crypto: akcipher - default implementation for setting a private key · bc155c6c
      Ignat Korchagin authored
      
      Changes from v1:
        * removed the default implementation from set_pub_key: it is assumed that
          an implementation must always have this callback defined as there are
          no use case for an algorithm, which doesn't need a public key
      
      Many akcipher implementations (like ECDSA) support only signature
      verifications, so they don't have all callbacks defined.
      
      Commit 78a0324f ("crypto: akcipher - default implementations for
      request callbacks") introduced default callbacks for sign/verify
      operations, which just return an error code.
      
      However, these are not enough, because before calling sign the caller would
      likely call set_priv_key first on the instantiated transform (as the
      in-kernel testmgr does). This function does not have a default stub, so the
      kernel crashes, when trying to set a private key on an akcipher, which
      doesn't support signature generation.
      
      I've noticed this, when trying to add a KAT vector for ECDSA signature to
      the testmgr.
      
      With this patch the testmgr returns an error in dmesg (as it should)
      instead of crashing the kernel NULL ptr dereference.
      
      Fixes: 78a0324f ("crypto: akcipher - default implementations for request callbacks")
      Signed-off-by: default avatarIgnat Korchagin <ignat@cloudflare.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      bc155c6c
  5. Sep 02, 2022
  6. Aug 26, 2022
  7. Aug 19, 2022
    • Robert Elliott's avatar
      crypto: testmgr - don't generate WARN for missing modules · a76bd86a
      Robert Elliott authored
      
      This userspace command:
          modprobe tcrypt
      or
          modprobe tcrypt mode=0
      
      runs all the tcrypt test cases numbered <200 (i.e., all the
      test cases calling tcrypt_test() and returning return values).
      
      Tests are sparsely numbered from 0 to 1000. For example:
          modprobe tcrypt mode=12
      tests sha512, and
          modprobe tcrypt mode=152
      tests rfc4543(gcm(aes))) - AES-GCM as GMAC
      
      The test manager generates WARNING crashdumps every time it attempts
      a test using an algorithm that is not available (not built-in to the
      kernel or available as a module):
      
          alg: skcipher: failed to allocate transform for ecb(arc4): -2
          ------------[ cut here ]-----------
          alg: self-tests for ecb(arc4) (ecb(arc4)) failed (rc=-2)
          WARNING: CPU: 9 PID: 4618 at crypto/testmgr.c:5777
      alg_test+0x30b/0x510
          [50 more lines....]
      
          ---[ end trace 0000000000000000 ]---
      
      If the kernel is compiled with CRYPTO_USER_API_ENABLE_OBSOLETE
      disabled (the default), then these algorithms are not compiled into
      the kernel or made into modules and trigger WARNINGs:
          arc4 tea xtea khazad anubis xeta seed
      
      Additionally, any other algorithms that are not enabled in .config
      will generate WARNINGs. In RHEL 9.0, for example, the default
      selection of algorithms leads to 16 WARNING dumps.
      
      One attempt to fix this was by modifying tcrypt_test() to check
      crypto_has_alg() and immediately return 0 if crypto_has_alg() fails,
      rather than proceed and return a non-zero error value that causes
      the caller (alg_test() in crypto/testmgr.c) to invoke WARN().
      That knocks out too many algorithms, though; some combinations
      like ctr(des3_ede) would work.
      
      Instead, change the condition on the WARN to ignore a return
      value is ENOENT, which is the value returned when the algorithm
      or combination of algorithms doesn't exist. Add a pr_warn to
      communicate that information in case the WARN is skipped.
      
      This approach allows algorithm tests to work that are combinations,
      not provided by one driver, like ctr(blowfish).
      
      Result - no more WARNINGs:
      modprobe tcrypt
      [  115.541765] tcrypt: testing md5
      [  115.556415] tcrypt: testing sha1
      [  115.570463] tcrypt: testing ecb(des)
      [  115.585303] cryptomgr: alg: skcipher: failed to allocate transform for ecb(des): -2
      [  115.593037] cryptomgr: alg: self-tests for ecb(des) using ecb(des) failed (rc=-2)
      [  115.593038] tcrypt: testing cbc(des)
      [  115.610641] cryptomgr: alg: skcipher: failed to allocate transform for cbc(des): -2
      [  115.618359] cryptomgr: alg: self-tests for cbc(des) using cbc(des) failed (rc=-2)
      ...
      
      Signed-off-by: default avatarRobert Elliott <elliott@hpe.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      a76bd86a
    • Lucas Segarra Fernandez's avatar
      crypto: testmgr - extend acomp tests for NULL destination buffer · 5a4c2936
      Lucas Segarra Fernandez authored
      
      Acomp API supports NULL destination buffer for compression
      and decompression requests. In such cases allocation is
      performed by API.
      
      Add test cases for crypto_acomp_compress() and crypto_acomp_decompress()
      with dst buffer allocated by API.
      
      Tests will only run if CONFIG_CRYPTO_MANAGER_EXTRA_TESTS=y.
      
      Signed-off-by: default avatarLucas Segarra Fernandez <lucas.segarra.fernandez@intel.com>
      Reviewed-by: default avatarGiovanni Cabiddu <giovanni.cabiddu@intel.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      5a4c2936
    • Jason Wang's avatar
      crypto: api - Fix comment typo · bc9d6dac
      Jason Wang authored
      
      The double `to' is duplicated in the comment, remove one.
      
      Signed-off-by: default avatarJason Wang <wangborong@cdjrlc.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      bc9d6dac
    • Dong Chuanjian's avatar
      crypto: drbg - remove unnecessary (void*) conversions · 66c8137f
      Dong Chuanjian authored
      
      remove unnecessary void* type casting
      
      v2:
      Turn assignments less than 75 characters into one line.
      
      Signed-off-by: default avatarDong Chuanjian <chuanjian@nfschina.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      66c8137f
    • Eric Biggers's avatar
      crypto: lib - move __crypto_xor into utils · 6e78ad0b
      Eric Biggers authored
      
      CRYPTO_LIB_CHACHA depends on CRYPTO for __crypto_xor, defined in
      crypto/algapi.c.  This is a layering violation because the dependencies
      should only go in the other direction (crypto/ => lib/crypto/).  Also
      the correct dependency would be CRYPTO_ALGAPI, not CRYPTO.  Fix this by
      moving __crypto_xor into the utils module in lib/crypto/.
      
      Note that CRYPTO_LIB_CHACHA_GENERIC selected XOR_BLOCKS, which is
      unrelated and unnecessary.  It was perhaps thought that XOR_BLOCKS was
      needed for __crypto_xor, but that's not the case.
      
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      Reviewed-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      6e78ad0b
    • Eric Biggers's avatar
      crypto: lib - create utils module and move __crypto_memneq into it · 7033b937
      Eric Biggers authored
      As requested at
      https://lore.kernel.org/r/YtEgzHuuMts0YBCz@gondor.apana.org.au
      
      , move
      __crypto_memneq into lib/crypto/ and put it under a new tristate.  The
      tristate is CRYPTO_LIB_UTILS, and it builds a module libcryptoutils.  As
      more crypto library utilities are being added, this creates a single
      place for them to go without cluttering up the main lib directory.
      
      The module's main file will be lib/crypto/utils.c.  However, leave
      memneq.c as its own file because of its nonstandard license.
      
      Signed-off-by: default avatarEric Biggers <ebiggers@google.com>
      Reviewed-by: default avatarJason A. Donenfeld <Jason@zx2c4.com>
      Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
      7033b937
  8. Aug 11, 2022
    • Linus Torvalds's avatar
      crypto: blake2b: effectively disable frame size warning · 1d3551ce
      Linus Torvalds authored
      It turns out that gcc-12.1 has some nasty problems with register
      allocation on a 32-bit x86 build for the 64-bit values used in the
      generic blake2b implementation, where the pattern of 64-bit rotates and
      xor operations ends up making gcc generate horrible code.
      
      As a result it ends up with a ridiculously large stack frame for all the
      spills it generates, resulting in the following build problem:
      
          crypto/blake2b_generic.c: In function ‘blake2b_compress_one_generic’:
          crypto/blake2b_generic.c:109:1: error: the frame size of 2640 bytes is larger than 2048 bytes [-Werror=frame-larger-than=]
      
      on the same test-case, clang ends up generating a stack frame that is
      just 296 bytes (and older gcc versions generate a slightly bigger one at
      428 bytes - still nowhere near that almost 3kB monster stack frame of
      gcc-12.1).
      
      The issue is fixed both in mainline and the GCC 12 release branch [1],
      but current release compilers end up failing the i386 allmodconfig build
      due to this issue.
      
      Disable the warning for now by simply raising the frame size for this
      one file, just to keep this issue from having people turn off WERROR.
      
      Link: https://lore.kernel.org/all/CAHk-=wjxqgeG2op+=W9sqgsWqCYnavC+SRfVyopu9-31S6xw+Q@mail.gmail.com/
      Link: https://gcc.gnu.org/bugzilla/show_bug.cgi?id=105930
      
       [1]
      Signed-off-by: default avatarLinus Torvalds <torvalds@linux-foundation.org>
      1d3551ce
  9. Aug 09, 2022
  10. Aug 03, 2022
  11. Aug 02, 2022
Loading