xref: /OK3568_Linux_fs/kernel/drivers/misc/lkdtm/cfi.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
1*4882a593Smuzhiyun // SPDX-License-Identifier: GPL-2.0
2*4882a593Smuzhiyun /*
3*4882a593Smuzhiyun  * This is for all the tests relating directly to Control Flow Integrity.
4*4882a593Smuzhiyun  */
5*4882a593Smuzhiyun #include "lkdtm.h"
6*4882a593Smuzhiyun 
7*4882a593Smuzhiyun static int called_count;
8*4882a593Smuzhiyun 
9*4882a593Smuzhiyun /* Function taking one argument, without a return value. */
lkdtm_increment_void(int * counter)10*4882a593Smuzhiyun static noinline void lkdtm_increment_void(int *counter)
11*4882a593Smuzhiyun {
12*4882a593Smuzhiyun 	(*counter)++;
13*4882a593Smuzhiyun }
14*4882a593Smuzhiyun 
15*4882a593Smuzhiyun /* Function taking one argument, returning int. */
lkdtm_increment_int(int * counter)16*4882a593Smuzhiyun static noinline int lkdtm_increment_int(int *counter)
17*4882a593Smuzhiyun {
18*4882a593Smuzhiyun 	(*counter)++;
19*4882a593Smuzhiyun 
20*4882a593Smuzhiyun 	return *counter;
21*4882a593Smuzhiyun }
22*4882a593Smuzhiyun /*
23*4882a593Smuzhiyun  * This tries to call an indirect function with a mismatched prototype.
24*4882a593Smuzhiyun  */
lkdtm_CFI_FORWARD_PROTO(void)25*4882a593Smuzhiyun void lkdtm_CFI_FORWARD_PROTO(void)
26*4882a593Smuzhiyun {
27*4882a593Smuzhiyun 	/*
28*4882a593Smuzhiyun 	 * Matches lkdtm_increment_void()'s prototype, but not
29*4882a593Smuzhiyun 	 * lkdtm_increment_int()'s prototype.
30*4882a593Smuzhiyun 	 */
31*4882a593Smuzhiyun 	void (*func)(int *);
32*4882a593Smuzhiyun 
33*4882a593Smuzhiyun 	pr_info("Calling matched prototype ...\n");
34*4882a593Smuzhiyun 	func = lkdtm_increment_void;
35*4882a593Smuzhiyun 	func(&called_count);
36*4882a593Smuzhiyun 
37*4882a593Smuzhiyun 	pr_info("Calling mismatched prototype ...\n");
38*4882a593Smuzhiyun 	func = (void *)lkdtm_increment_int;
39*4882a593Smuzhiyun 	func(&called_count);
40*4882a593Smuzhiyun 
41*4882a593Smuzhiyun 	pr_info("Fail: survived mismatched prototype function call!\n");
42*4882a593Smuzhiyun }
43