1f08c3bdfSopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later
2f08c3bdfSopenharmony_ci/*
3f08c3bdfSopenharmony_ci * Copyright 2019 Google LLC
4f08c3bdfSopenharmony_ci */
5f08c3bdfSopenharmony_ci
6f08c3bdfSopenharmony_ci/*
7f08c3bdfSopenharmony_ci * Regression test for commit 8088d3dd4d7c ("crypto: skcipher - fix crash
8f08c3bdfSopenharmony_ci * flushing dcache in error path").  This test verifies the kernel doesn't crash
9f08c3bdfSopenharmony_ci * when trying to encrypt a message with size not aligned to the block cipher's
10f08c3bdfSopenharmony_ci * block size, and where the destination buffer starts exactly at a page
11f08c3bdfSopenharmony_ci * boundary.  Based on the reproducer from the commit message.  Note that this
12f08c3bdfSopenharmony_ci * issue only reproduces on certain architectures, such as arm and arm64.
13f08c3bdfSopenharmony_ci *
14f08c3bdfSopenharmony_ci * On some older kernel without commit 160544075f2a ("crypto: scatterwalk - Hide
15f08c3bdfSopenharmony_ci * PageSlab call to optimise away flush_dcache_page") , it doesn't use
16f08c3bdfSopenharmony_ci * ARCH_IMPLEMENTS_FLUSH_DCACHE_PAGE macro. It can crash on all architectures.
17f08c3bdfSopenharmony_ci * Without skcipher walk interface, it is also a regresstion test for commit
18f08c3bdfSopenharmony_ci * 0868def3e410("crypto: blkcipher - fix crash flushing dcache in error path").
19f08c3bdfSopenharmony_ci */
20f08c3bdfSopenharmony_ci
21f08c3bdfSopenharmony_ci#include <errno.h>
22f08c3bdfSopenharmony_ci
23f08c3bdfSopenharmony_ci#include "tst_test.h"
24f08c3bdfSopenharmony_ci#include "tst_af_alg.h"
25f08c3bdfSopenharmony_ci
26f08c3bdfSopenharmony_cistatic void run(void)
27f08c3bdfSopenharmony_ci{
28f08c3bdfSopenharmony_ci	char buffer[4096] __attribute__((aligned(4096))) = { 0 };
29f08c3bdfSopenharmony_ci	int reqfd;
30f08c3bdfSopenharmony_ci
31f08c3bdfSopenharmony_ci	reqfd = tst_alg_setup_reqfd("skcipher", "cbc(aes-generic)", NULL, 16);
32f08c3bdfSopenharmony_ci
33f08c3bdfSopenharmony_ci	SAFE_WRITE(SAFE_WRITE_ALL, reqfd, buffer, 15);
34f08c3bdfSopenharmony_ci	/* with the bug, this crashed the kernel on some architectures */
35f08c3bdfSopenharmony_ci	TEST(read(reqfd, buffer, 15));
36f08c3bdfSopenharmony_ci
37f08c3bdfSopenharmony_ci	if (TST_RET == 0)
38f08c3bdfSopenharmony_ci		tst_res(TFAIL, "read() unexpectedly succeeded");
39f08c3bdfSopenharmony_ci	else if (TST_ERR == EINVAL)
40f08c3bdfSopenharmony_ci		tst_res(TPASS, "read() expectedly failed with EINVAL");
41f08c3bdfSopenharmony_ci	else
42f08c3bdfSopenharmony_ci		tst_res(TFAIL | TTERRNO, "read() failed with unexpected error");
43f08c3bdfSopenharmony_ci
44f08c3bdfSopenharmony_ci	close(reqfd);
45f08c3bdfSopenharmony_ci}
46f08c3bdfSopenharmony_ci
47f08c3bdfSopenharmony_cistatic struct tst_test test = {
48f08c3bdfSopenharmony_ci	.test_all = run,
49f08c3bdfSopenharmony_ci	.tags = (const struct tst_tag[]) {
50f08c3bdfSopenharmony_ci		{"linux-git", "8088d3dd4d7c"},
51f08c3bdfSopenharmony_ci		{"linux-git", "160544075f2a"},
52f08c3bdfSopenharmony_ci		{"linux-git", "0868def3e410"},
53f08c3bdfSopenharmony_ci		{}
54f08c3bdfSopenharmony_ci	}
55f08c3bdfSopenharmony_ci};
56