Commit 4e234eed authored by Kees Cook's avatar Kees Cook Committed by Herbert Xu

crypto: tcrypt - Remove VLA usage

In the quest to remove all stack VLA usage from the kernel[1], this
allocates the return code buffers before starting jiffie timers, rather
than using stack space for the array. Additionally cleans up some exit
paths and make sure that the num_mb module_param() is used only once
per execution to avoid possible races in the value changing.

[1] https://lkml.kernel.org/r/CA+55aFzCG-zNmZwX4A2FQpadafLfEzK6CC=qPXydAacU1RqZWA@mail.gmail.comSigned-off-by: default avatarKees Cook <keescook@chromium.org>
Signed-off-by: default avatarHerbert Xu <herbert@gondor.apana.org.au>
parent e99ce921
...@@ -158,9 +158,9 @@ struct test_mb_aead_data { ...@@ -158,9 +158,9 @@ struct test_mb_aead_data {
}; };
static int do_mult_aead_op(struct test_mb_aead_data *data, int enc, static int do_mult_aead_op(struct test_mb_aead_data *data, int enc,
u32 num_mb) u32 num_mb, int *rc)
{ {
int i, rc[num_mb], err = 0; int i, err = 0;
/* Fire up a bunch of concurrent requests */ /* Fire up a bunch of concurrent requests */
for (i = 0; i < num_mb; i++) { for (i = 0; i < num_mb; i++) {
...@@ -188,18 +188,26 @@ static int test_mb_aead_jiffies(struct test_mb_aead_data *data, int enc, ...@@ -188,18 +188,26 @@ static int test_mb_aead_jiffies(struct test_mb_aead_data *data, int enc,
{ {
unsigned long start, end; unsigned long start, end;
int bcount; int bcount;
int ret; int ret = 0;
int *rc;
rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
if (!rc)
return -ENOMEM;
for (start = jiffies, end = start + secs * HZ, bcount = 0; for (start = jiffies, end = start + secs * HZ, bcount = 0;
time_before(jiffies, end); bcount++) { time_before(jiffies, end); bcount++) {
ret = do_mult_aead_op(data, enc, num_mb); ret = do_mult_aead_op(data, enc, num_mb, rc);
if (ret) if (ret)
return ret; goto out;
} }
pr_cont("%d operations in %d seconds (%ld bytes)\n", pr_cont("%d operations in %d seconds (%ld bytes)\n",
bcount * num_mb, secs, (long)bcount * blen * num_mb); bcount * num_mb, secs, (long)bcount * blen * num_mb);
return 0;
out:
kfree(rc);
return ret;
} }
static int test_mb_aead_cycles(struct test_mb_aead_data *data, int enc, static int test_mb_aead_cycles(struct test_mb_aead_data *data, int enc,
...@@ -208,10 +216,15 @@ static int test_mb_aead_cycles(struct test_mb_aead_data *data, int enc, ...@@ -208,10 +216,15 @@ static int test_mb_aead_cycles(struct test_mb_aead_data *data, int enc,
unsigned long cycles = 0; unsigned long cycles = 0;
int ret = 0; int ret = 0;
int i; int i;
int *rc;
rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
if (!rc)
return -ENOMEM;
/* Warm-up run. */ /* Warm-up run. */
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
ret = do_mult_aead_op(data, enc, num_mb); ret = do_mult_aead_op(data, enc, num_mb, rc);
if (ret) if (ret)
goto out; goto out;
} }
...@@ -221,7 +234,7 @@ static int test_mb_aead_cycles(struct test_mb_aead_data *data, int enc, ...@@ -221,7 +234,7 @@ static int test_mb_aead_cycles(struct test_mb_aead_data *data, int enc,
cycles_t start, end; cycles_t start, end;
start = get_cycles(); start = get_cycles();
ret = do_mult_aead_op(data, enc, num_mb); ret = do_mult_aead_op(data, enc, num_mb, rc);
end = get_cycles(); end = get_cycles();
if (ret) if (ret)
...@@ -230,11 +243,11 @@ static int test_mb_aead_cycles(struct test_mb_aead_data *data, int enc, ...@@ -230,11 +243,11 @@ static int test_mb_aead_cycles(struct test_mb_aead_data *data, int enc,
cycles += end - start; cycles += end - start;
} }
out:
if (ret == 0)
pr_cont("1 operation in %lu cycles (%d bytes)\n", pr_cont("1 operation in %lu cycles (%d bytes)\n",
(cycles + 4) / (8 * num_mb), blen); (cycles + 4) / (8 * num_mb), blen);
out:
kfree(rc);
return ret; return ret;
} }
...@@ -705,9 +718,10 @@ struct test_mb_ahash_data { ...@@ -705,9 +718,10 @@ struct test_mb_ahash_data {
char *xbuf[XBUFSIZE]; char *xbuf[XBUFSIZE];
}; };
static inline int do_mult_ahash_op(struct test_mb_ahash_data *data, u32 num_mb) static inline int do_mult_ahash_op(struct test_mb_ahash_data *data, u32 num_mb,
int *rc)
{ {
int i, rc[num_mb], err = 0; int i, err = 0;
/* Fire up a bunch of concurrent requests */ /* Fire up a bunch of concurrent requests */
for (i = 0; i < num_mb; i++) for (i = 0; i < num_mb; i++)
...@@ -731,18 +745,26 @@ static int test_mb_ahash_jiffies(struct test_mb_ahash_data *data, int blen, ...@@ -731,18 +745,26 @@ static int test_mb_ahash_jiffies(struct test_mb_ahash_data *data, int blen,
{ {
unsigned long start, end; unsigned long start, end;
int bcount; int bcount;
int ret; int ret = 0;
int *rc;
rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
if (!rc)
return -ENOMEM;
for (start = jiffies, end = start + secs * HZ, bcount = 0; for (start = jiffies, end = start + secs * HZ, bcount = 0;
time_before(jiffies, end); bcount++) { time_before(jiffies, end); bcount++) {
ret = do_mult_ahash_op(data, num_mb); ret = do_mult_ahash_op(data, num_mb, rc);
if (ret) if (ret)
return ret; goto out;
} }
pr_cont("%d operations in %d seconds (%ld bytes)\n", pr_cont("%d operations in %d seconds (%ld bytes)\n",
bcount * num_mb, secs, (long)bcount * blen * num_mb); bcount * num_mb, secs, (long)bcount * blen * num_mb);
return 0;
out:
kfree(rc);
return ret;
} }
static int test_mb_ahash_cycles(struct test_mb_ahash_data *data, int blen, static int test_mb_ahash_cycles(struct test_mb_ahash_data *data, int blen,
...@@ -751,10 +773,15 @@ static int test_mb_ahash_cycles(struct test_mb_ahash_data *data, int blen, ...@@ -751,10 +773,15 @@ static int test_mb_ahash_cycles(struct test_mb_ahash_data *data, int blen,
unsigned long cycles = 0; unsigned long cycles = 0;
int ret = 0; int ret = 0;
int i; int i;
int *rc;
rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
if (!rc)
return -ENOMEM;
/* Warm-up run. */ /* Warm-up run. */
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
ret = do_mult_ahash_op(data, num_mb); ret = do_mult_ahash_op(data, num_mb, rc);
if (ret) if (ret)
goto out; goto out;
} }
...@@ -764,7 +791,7 @@ static int test_mb_ahash_cycles(struct test_mb_ahash_data *data, int blen, ...@@ -764,7 +791,7 @@ static int test_mb_ahash_cycles(struct test_mb_ahash_data *data, int blen,
cycles_t start, end; cycles_t start, end;
start = get_cycles(); start = get_cycles();
ret = do_mult_ahash_op(data, num_mb); ret = do_mult_ahash_op(data, num_mb, rc);
end = get_cycles(); end = get_cycles();
if (ret) if (ret)
...@@ -773,11 +800,11 @@ static int test_mb_ahash_cycles(struct test_mb_ahash_data *data, int blen, ...@@ -773,11 +800,11 @@ static int test_mb_ahash_cycles(struct test_mb_ahash_data *data, int blen,
cycles += end - start; cycles += end - start;
} }
out:
if (ret == 0)
pr_cont("1 operation in %lu cycles (%d bytes)\n", pr_cont("1 operation in %lu cycles (%d bytes)\n",
(cycles + 4) / (8 * num_mb), blen); (cycles + 4) / (8 * num_mb), blen);
out:
kfree(rc);
return ret; return ret;
} }
...@@ -1118,9 +1145,9 @@ struct test_mb_skcipher_data { ...@@ -1118,9 +1145,9 @@ struct test_mb_skcipher_data {
}; };
static int do_mult_acipher_op(struct test_mb_skcipher_data *data, int enc, static int do_mult_acipher_op(struct test_mb_skcipher_data *data, int enc,
u32 num_mb) u32 num_mb, int *rc)
{ {
int i, rc[num_mb], err = 0; int i, err = 0;
/* Fire up a bunch of concurrent requests */ /* Fire up a bunch of concurrent requests */
for (i = 0; i < num_mb; i++) { for (i = 0; i < num_mb; i++) {
...@@ -1148,18 +1175,26 @@ static int test_mb_acipher_jiffies(struct test_mb_skcipher_data *data, int enc, ...@@ -1148,18 +1175,26 @@ static int test_mb_acipher_jiffies(struct test_mb_skcipher_data *data, int enc,
{ {
unsigned long start, end; unsigned long start, end;
int bcount; int bcount;
int ret; int ret = 0;
int *rc;
rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
if (!rc)
return -ENOMEM;
for (start = jiffies, end = start + secs * HZ, bcount = 0; for (start = jiffies, end = start + secs * HZ, bcount = 0;
time_before(jiffies, end); bcount++) { time_before(jiffies, end); bcount++) {
ret = do_mult_acipher_op(data, enc, num_mb); ret = do_mult_acipher_op(data, enc, num_mb, rc);
if (ret) if (ret)
return ret; goto out;
} }
pr_cont("%d operations in %d seconds (%ld bytes)\n", pr_cont("%d operations in %d seconds (%ld bytes)\n",
bcount * num_mb, secs, (long)bcount * blen * num_mb); bcount * num_mb, secs, (long)bcount * blen * num_mb);
return 0;
out:
kfree(rc);
return ret;
} }
static int test_mb_acipher_cycles(struct test_mb_skcipher_data *data, int enc, static int test_mb_acipher_cycles(struct test_mb_skcipher_data *data, int enc,
...@@ -1168,10 +1203,15 @@ static int test_mb_acipher_cycles(struct test_mb_skcipher_data *data, int enc, ...@@ -1168,10 +1203,15 @@ static int test_mb_acipher_cycles(struct test_mb_skcipher_data *data, int enc,
unsigned long cycles = 0; unsigned long cycles = 0;
int ret = 0; int ret = 0;
int i; int i;
int *rc;
rc = kcalloc(num_mb, sizeof(*rc), GFP_KERNEL);
if (!rc)
return -ENOMEM;
/* Warm-up run. */ /* Warm-up run. */
for (i = 0; i < 4; i++) { for (i = 0; i < 4; i++) {
ret = do_mult_acipher_op(data, enc, num_mb); ret = do_mult_acipher_op(data, enc, num_mb, rc);
if (ret) if (ret)
goto out; goto out;
} }
...@@ -1181,7 +1221,7 @@ static int test_mb_acipher_cycles(struct test_mb_skcipher_data *data, int enc, ...@@ -1181,7 +1221,7 @@ static int test_mb_acipher_cycles(struct test_mb_skcipher_data *data, int enc,
cycles_t start, end; cycles_t start, end;
start = get_cycles(); start = get_cycles();
ret = do_mult_acipher_op(data, enc, num_mb); ret = do_mult_acipher_op(data, enc, num_mb, rc);
end = get_cycles(); end = get_cycles();
if (ret) if (ret)
...@@ -1190,11 +1230,11 @@ static int test_mb_acipher_cycles(struct test_mb_skcipher_data *data, int enc, ...@@ -1190,11 +1230,11 @@ static int test_mb_acipher_cycles(struct test_mb_skcipher_data *data, int enc,
cycles += end - start; cycles += end - start;
} }
out:
if (ret == 0)
pr_cont("1 operation in %lu cycles (%d bytes)\n", pr_cont("1 operation in %lu cycles (%d bytes)\n",
(cycles + 4) / (8 * num_mb), blen); (cycles + 4) / (8 * num_mb), blen);
out:
kfree(rc);
return ret; return ret;
} }
...@@ -1606,7 +1646,7 @@ static inline int tcrypt_test(const char *alg) ...@@ -1606,7 +1646,7 @@ static inline int tcrypt_test(const char *alg)
return ret; return ret;
} }
static int do_test(const char *alg, u32 type, u32 mask, int m) static int do_test(const char *alg, u32 type, u32 mask, int m, u32 num_mb)
{ {
int i; int i;
int ret = 0; int ret = 0;
...@@ -1621,7 +1661,7 @@ static int do_test(const char *alg, u32 type, u32 mask, int m) ...@@ -1621,7 +1661,7 @@ static int do_test(const char *alg, u32 type, u32 mask, int m)
} }
for (i = 1; i < 200; i++) for (i = 1; i < 200; i++)
ret += do_test(NULL, 0, 0, i); ret += do_test(NULL, 0, 0, i, num_mb);
break; break;
case 1: case 1:
...@@ -2903,7 +2943,7 @@ static int __init tcrypt_mod_init(void) ...@@ -2903,7 +2943,7 @@ static int __init tcrypt_mod_init(void)
goto err_free_tv; goto err_free_tv;
} }
err = do_test(alg, type, mask, mode); err = do_test(alg, type, mask, mode, num_mb);
if (err) { if (err) {
printk(KERN_ERR "tcrypt: one or more tests failed!\n"); printk(KERN_ERR "tcrypt: one or more tests failed!\n");
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment