Skip to content

Commit f3569fd

Browse files
keesherbertx
authored andcommitted
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>
1 parent 1299c9c commit f3569fd

2 files changed

Lines changed: 16 additions & 12 deletions

File tree

‎crypto/shash.c‎

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -73,13 +73,6 @@ int crypto_shash_setkey(struct crypto_shash *tfm, const u8 *key,
7373
}
7474
EXPORT_SYMBOL_GPL(crypto_shash_setkey);
7575

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-
8376
static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
8477
unsigned int len)
8578
{
@@ -88,11 +81,17 @@ static int shash_update_unaligned(struct shash_desc *desc, const u8 *data,
8881
unsigned long alignmask = crypto_shash_alignmask(tfm);
8982
unsigned int unaligned_len = alignmask + 1 -
9083
((unsigned long)data & alignmask);
91-
u8 ubuf[shash_align_buffer_size(unaligned_len, alignmask)]
92-
__aligned_largest;
84+
/*
85+
* We cannot count on __aligned() working for large values:
86+
* https://patchwork.kernel.org/patch/9507697/
87+
*/
88+
u8 ubuf[MAX_ALGAPI_ALIGNMASK * 2];
9389
u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
9490
int err;
9591

92+
if (WARN_ON(buf + unaligned_len > ubuf + sizeof(ubuf)))
93+
return -EINVAL;
94+
9695
if (unaligned_len > len)
9796
unaligned_len = len;
9897

@@ -124,11 +123,17 @@ static int shash_final_unaligned(struct shash_desc *desc, u8 *out)
124123
unsigned long alignmask = crypto_shash_alignmask(tfm);
125124
struct shash_alg *shash = crypto_shash_alg(tfm);
126125
unsigned int ds = crypto_shash_digestsize(tfm);
127-
u8 ubuf[shash_align_buffer_size(ds, alignmask)]
128-
__aligned_largest;
126+
/*
127+
* We cannot count on __aligned() working for large values:
128+
* https://patchwork.kernel.org/patch/9507697/
129+
*/
130+
u8 ubuf[MAX_ALGAPI_ALIGNMASK + HASH_MAX_DIGESTSIZE];
129131
u8 *buf = PTR_ALIGN(&ubuf[0], alignmask + 1);
130132
int err;
131133

134+
if (WARN_ON(buf + ds > ubuf + sizeof(ubuf)))
135+
return -EINVAL;
136+
132137
err = shash->final(desc, buf);
133138
if (err)
134139
goto out;

‎include/linux/compiler_types.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,6 @@ struct ftrace_likely_data {
198198
*/
199199
#define __pure __attribute__((pure))
200200
#define __aligned(x) __attribute__((aligned(x)))
201-
#define __aligned_largest __attribute__((aligned))
202201
#define __printf(a, b) __attribute__((format(printf, a, b)))
203202
#define __scanf(a, b) __attribute__((format(scanf, a, b)))
204203
#define __maybe_unused __attribute__((unused))

0 commit comments

Comments
 (0)