1// SPDX-License-Identifier: GPL-2.0-or-later
2/*
3 * Copyright 2019 Google LLC
4 * Copyright (c) Linux Test Project, 2019-2021
5 */
6
7/*
8 * Regression test for commit bb2964810233 ("crypto: vmac - separate tfm and
9 * request context").  This test verifies that a VMAC transform can be used by
10 * multiple concurrent hash requests without crashing the kernel.  Based on the
11 * reproducer from the commit message.
12 */
13
14#include <stdio.h>
15#include <sys/wait.h>
16
17#include "tst_test.h"
18#include "tst_af_alg.h"
19
20static void test_with_symm_enc_algs(const char *symm_enc_algname)
21{
22	int algfd, reqfd;
23	char buf[256] = { 0 };
24	char vmac_algname[64];
25	pid_t pid;
26	int status;
27	int i;
28
29	sprintf(vmac_algname, "vmac64(%s)", symm_enc_algname);
30	if (!tst_have_alg("hash", vmac_algname)) {
31		sprintf(vmac_algname, "vmac(%s)", symm_enc_algname);
32		if (!tst_have_alg("hash", vmac_algname))
33			return;
34	}
35	algfd = tst_alg_setup("hash", vmac_algname, NULL, 16);
36
37	tst_res(TINFO, "Starting vmac hashing test.  May crash buggy kernels.");
38
39	pid = SAFE_FORK();
40
41	reqfd = tst_alg_accept(algfd);
42
43	for (i = 0; i < 500000; i++)
44		SAFE_WRITE(SAFE_WRITE_ALL, reqfd, buf, sizeof(buf));
45
46	close(reqfd);
47
48	if (pid != 0) {
49		SAFE_WAIT(&status);
50		if (WIFEXITED(status) && WEXITSTATUS(status) == 0)
51			tst_res(TPASS, "didn't crash");
52		else if (WIFSIGNALED(status) && WTERMSIG(status) == SIGKILL)
53			tst_res(TFAIL, "crashed");
54		else
55			tst_brk(TBROK, "child %s", tst_strstatus(status));
56
57		close(algfd);
58	}
59}
60
61/* try several different symmetric encryption algorithms */
62static const char * const symm_enc_algs[] = {
63	"aes",
64	"sm4",
65	"sm4-generic",
66};
67
68static void do_test(unsigned int i)
69{
70	test_with_symm_enc_algs(symm_enc_algs[i]);
71}
72
73static struct tst_test test = {
74	.test = do_test,
75	.tcnt = ARRAY_SIZE(symm_enc_algs),
76	.forks_child = 1,
77	.tags = (const struct tst_tag[]) {
78		{"linux-git", "bb2964810233"},
79		{}
80	}
81};
82