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