]> git.proxmox.com Git - mirror_ubuntu-hirsute-kernel.git/commitdiff
crypto: shash - Remove VLA usage in unaligned hashing
authorKees Cook <keescook@chromium.org>
Tue, 7 Aug 2018 21:18:42 +0000 (14:18 -0700)
committerHerbert Xu <herbert@gondor.apana.org.au>
Tue, 4 Sep 2018 03:37:03 +0000 (11:37 +0800)
In the quest to remove all stack VLA usage from the kernel[1], this uses
the newly defined max alignment to perform unaligned hashing to avoid
VLAs, and drops the helper function while adding sanity checks on the
resulting buffer sizes. Additionally, the __aligned_largest macro is
removed since this helper was the only user.

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

Signed-off-by: Kees Cook <keescook@chromium.org>
Signed-off-by: Herbert Xu <herbert@gondor.apana.org.au>
crypto/shash.c
include/linux/compiler_types.h

index 86d76b5c626ceed2d7404a124a37bbd7025316e9..d21f04d70dce46eb4d1a4ef7a328667b476082af 100644 (file)
@@ -73,13 +73,6 @@ int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
 }
 EXPORT_SYMBOL_GPL(crypto_shash_setkey);
 
-static inline unsigned int shash_align_buffer_size(unsigned len,
-                                                  unsigned long mask)
-{
-       typedef u8 __aligned_largest u8_aligned;
-       return len + (mask & ~(__alignof__(u8_aligned) - 1));
-}
-
 static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
                                  unsigned int len)
 {
@@ -88,11 +81,17 @@ static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
        unsigned long alignmask = crypto_shash_alignmask(tfm);
        unsigned int unaligned_len = alignmask + 1 -
                                     ((unsigned long)data & alignmask);
-       u8 ubuf[shash_align_buffer_size(unaligned_len, alignmask)]
-               __aligned_largest;
+       /*
+        * We cannot count on __aligned() working for large values:
+        * https://patchwork.kernel.org/patch/9507697/
+        */
+       u8 ubuf[MAX_ALGAPI_ALIGNMASK * 2];
        u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
        int err;
 
+       if (WARN_ON(buf + unaligned_len > ubuf + sizeof(ubuf)))
+               return -EINVAL;
+
        if (unaligned_len > len)
                unaligned_len = len;
 
@@ -124,11 +123,17 @@ static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
        unsigned long alignmask = crypto_shash_alignmask(tfm);
        struct shash_alg *shash = crypto_shash_alg(tfm);
        unsigned int ds = crypto_shash_digestsize(tfm);
-       u8 ubuf[shash_align_buffer_size(ds, alignmask)]
-               __aligned_largest;
+       /*
+        * We cannot count on __aligned() working for large values:
+        * https://patchwork.kernel.org/patch/9507697/
+        */
+       u8 ubuf[MAX_ALGAPI_ALIGNMASK + HASH_MAX_DIGESTSIZE];
        u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
        int err;
 
+       if (WARN_ON(buf + ds > ubuf + sizeof(ubuf)))
+               return -EINVAL;
+
        err = shash->final(desc, buf);
        if (err)
                goto out;
index 3525c179698c238c70145574c03e71620054777c..13fee5fe734b65c8682f250f1803cb2c9feae29a 100644 (file)
@@ -198,7 +198,6 @@ struct ftrace_likely_data {
  */
 #define __pure                 __attribute__((pure))
 #define __aligned(x)           __attribute__((aligned(x)))
-#define __aligned_largest      __attribute__((aligned))
 #define __printf(a, b)         __attribute__((format(printf, a, b)))
 #define __scanf(a, b)          __attribute__((format(scanf, a, b)))
 #define __maybe_unused         __attribute__((unused))