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