Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

crypto: shash - Remove VLA usage in unaligned hashing

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>

authored by

Kees Cook and committed by
Herbert Xu
f3569fd6 1299c9cf

+16 -12
+16 -11
crypto/shash.c
··· 73 73 } 74 74 EXPORT_SYMBOL_GPL(crypto_shash_setkey); 75 75 76 - static inline unsigned int shash_align_buffer_size(unsigned len, 77 - unsigned long mask) 78 - { 79 - typedef u8 __aligned_largest u8_aligned; 80 - return len + (mask & ~(__alignof__(u8_aligned) - 1)); 81 - } 82 - 83 76 static int shash_update_unaligned(struct shash_desc *desc, const u8 *data, 84 77 unsigned int len) 85 78 { ··· 81 88 unsigned long alignmask = crypto_shash_alignmask(tfm); 82 89 unsigned int unaligned_len = alignmask + 1 - 83 90 ((unsigned long)data & alignmask); 84 - u8 ubuf[shash_align_buffer_size(unaligned_len, alignmask)] 85 - __aligned_largest; 91 + /* 92 + * We cannot count on __aligned() working for large values: 93 + * https://patchwork.kernel.org/patch/9507697/ 94 + */ 95 + u8 ubuf[MAX_ALGAPI_ALIGNMASK * 2]; 86 96 u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1); 87 97 int err; 98 + 99 + if (WARN_ON(buf + unaligned_len > ubuf + sizeof(ubuf))) 100 + return -EINVAL; 88 101 89 102 if (unaligned_len > len) 90 103 unaligned_len = len; ··· 123 124 unsigned long alignmask = crypto_shash_alignmask(tfm); 124 125 struct shash_alg *shash = crypto_shash_alg(tfm); 125 126 unsigned int ds = crypto_shash_digestsize(tfm); 126 - u8 ubuf[shash_align_buffer_size(ds, alignmask)] 127 - __aligned_largest; 127 + /* 128 + * We cannot count on __aligned() working for large values: 129 + * https://patchwork.kernel.org/patch/9507697/ 130 + */ 131 + u8 ubuf[MAX_ALGAPI_ALIGNMASK + HASH_MAX_DIGESTSIZE]; 128 132 u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1); 129 133 int err; 134 + 135 + if (WARN_ON(buf + ds > ubuf + sizeof(ubuf))) 136 + return -EINVAL; 130 137 131 138 err = shash->final(desc, buf); 132 139 if (err)
-1
include/linux/compiler_types.h
··· 198 198 */ 199 199 #define __pure __attribute__((pure)) 200 200 #define __aligned(x) __attribute__((aligned(x))) 201 - #define __aligned_largest __attribute__((aligned)) 202 201 #define __printf(a, b) __attribute__((format(printf, a, b))) 203 202 #define __scanf(a, b) __attribute__((format(scanf, a, b))) 204 203 #define __maybe_unused __attribute__((unused))