1*49cdfc7eSAndroid Build Coastguard Worker // SPDX-License-Identifier: GPL-2.0-or-later
2*49cdfc7eSAndroid Build Coastguard Worker /*
3*49cdfc7eSAndroid Build Coastguard Worker * Copyright 2019 Google LLC
4*49cdfc7eSAndroid Build Coastguard Worker */
5*49cdfc7eSAndroid Build Coastguard Worker
6*49cdfc7eSAndroid Build Coastguard Worker /*
7*49cdfc7eSAndroid Build Coastguard Worker * Regression test for commit 8f9c46934848 ("crypto: authenc - fix parsing key
8*49cdfc7eSAndroid Build Coastguard Worker * with misaligned rta_len"). Based on the reproducer from the commit message.
9*49cdfc7eSAndroid Build Coastguard Worker */
10*49cdfc7eSAndroid Build Coastguard Worker
11*49cdfc7eSAndroid Build Coastguard Worker #include <errno.h>
12*49cdfc7eSAndroid Build Coastguard Worker
13*49cdfc7eSAndroid Build Coastguard Worker #include "tst_test.h"
14*49cdfc7eSAndroid Build Coastguard Worker #include "tst_af_alg.h"
15*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/socket.h"
16*49cdfc7eSAndroid Build Coastguard Worker
17*49cdfc7eSAndroid Build Coastguard Worker /*
18*49cdfc7eSAndroid Build Coastguard Worker * include after <sys/socket.h> (via tst_test.h), to work around dependency bug
19*49cdfc7eSAndroid Build Coastguard Worker * in old kernel headers (https://www.spinics.net/lists/netdev/msg171764.html)
20*49cdfc7eSAndroid Build Coastguard Worker */
21*49cdfc7eSAndroid Build Coastguard Worker #include <linux/rtnetlink.h>
22*49cdfc7eSAndroid Build Coastguard Worker
run(void)23*49cdfc7eSAndroid Build Coastguard Worker static void run(void)
24*49cdfc7eSAndroid Build Coastguard Worker {
25*49cdfc7eSAndroid Build Coastguard Worker struct {
26*49cdfc7eSAndroid Build Coastguard Worker struct rtattr attr;
27*49cdfc7eSAndroid Build Coastguard Worker uint32_t enckeylen;
28*49cdfc7eSAndroid Build Coastguard Worker char keys[1];
29*49cdfc7eSAndroid Build Coastguard Worker } __attribute__((packed)) key = {
30*49cdfc7eSAndroid Build Coastguard Worker .attr.rta_len = sizeof(key),
31*49cdfc7eSAndroid Build Coastguard Worker .attr.rta_type = 1 /* CRYPTO_AUTHENC_KEYA_PARAM */,
32*49cdfc7eSAndroid Build Coastguard Worker };
33*49cdfc7eSAndroid Build Coastguard Worker int algfd;
34*49cdfc7eSAndroid Build Coastguard Worker
35*49cdfc7eSAndroid Build Coastguard Worker algfd = tst_alg_setup("aead", "authenc(hmac(sha256),cbc(aes))",
36*49cdfc7eSAndroid Build Coastguard Worker NULL, 0);
37*49cdfc7eSAndroid Build Coastguard Worker tst_res(TINFO,
38*49cdfc7eSAndroid Build Coastguard Worker "Setting malformed authenc key. May crash buggy kernels.");
39*49cdfc7eSAndroid Build Coastguard Worker TEST(setsockopt(algfd, SOL_ALG, ALG_SET_KEY, &key, sizeof(key)));
40*49cdfc7eSAndroid Build Coastguard Worker if (TST_RET == 0)
41*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL, "setting malformed key unexpectedly succeeded");
42*49cdfc7eSAndroid Build Coastguard Worker else if (TST_ERR != EINVAL)
43*49cdfc7eSAndroid Build Coastguard Worker tst_res(TFAIL | TTERRNO,
44*49cdfc7eSAndroid Build Coastguard Worker "setting malformed key failed with unexpected error");
45*49cdfc7eSAndroid Build Coastguard Worker else
46*49cdfc7eSAndroid Build Coastguard Worker tst_res(TPASS, "didn't crash, and got EINVAL as expected");
47*49cdfc7eSAndroid Build Coastguard Worker }
48*49cdfc7eSAndroid Build Coastguard Worker
49*49cdfc7eSAndroid Build Coastguard Worker static struct tst_test test = {
50*49cdfc7eSAndroid Build Coastguard Worker .test_all = run,
51*49cdfc7eSAndroid Build Coastguard Worker .tags = (const struct tst_tag[]) {
52*49cdfc7eSAndroid Build Coastguard Worker {"linux-git", "8f9c46934848"},
53*49cdfc7eSAndroid Build Coastguard Worker {}
54*49cdfc7eSAndroid Build Coastguard Worker }
55*49cdfc7eSAndroid Build Coastguard Worker };
56