xref: /openbmc/linux/drivers/misc/lkdtm/lkdtm.h (revision dd5e5554)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef __LKDTM_H
3 #define __LKDTM_H
4 
5 #define pr_fmt(fmt) "lkdtm: " fmt
6 
7 #include <linux/kernel.h>
8 
9 extern char *lkdtm_kernel_info;
10 
11 #define pr_expected_config(kconfig)				\
12 do {								\
13 	if (IS_ENABLED(kconfig)) 				\
14 		pr_err("Unexpected! This %s was built with " #kconfig "=y\n", \
15 			lkdtm_kernel_info);			\
16 	else							\
17 		pr_warn("This is probably expected, since this %s was built *without* " #kconfig "=y\n", \
18 			lkdtm_kernel_info);			\
19 } while (0)
20 
21 #ifndef MODULE
22 int lkdtm_check_bool_cmdline(const char *param);
23 #define pr_expected_config_param(kconfig, param)		\
24 do {								\
25 	if (IS_ENABLED(kconfig)) {				\
26 		switch (lkdtm_check_bool_cmdline(param)) {	\
27 		case 0:						\
28 			pr_warn("This is probably expected, since this %s was built with " #kconfig "=y but booted with '" param "=N'\n", \
29 				lkdtm_kernel_info);		\
30 			break;					\
31 		case 1:						\
32 			pr_err("Unexpected! This %s was built with " #kconfig "=y and booted with '" param "=Y'\n", \
33 				lkdtm_kernel_info);		\
34 			break;					\
35 		default:					\
36 			pr_err("Unexpected! This %s was built with " #kconfig "=y (and booted without '" param "' specified)\n", \
37 				lkdtm_kernel_info);		\
38 		}						\
39 	} else {						\
40 		switch (lkdtm_check_bool_cmdline(param)) {	\
41 		case 0:						\
42 			pr_warn("This is probably expected, as this %s was built *without* " #kconfig "=y and booted with '" param "=N'\n", \
43 				lkdtm_kernel_info);		\
44 			break;					\
45 		case 1:						\
46 			pr_err("Unexpected! This %s was built *without* " #kconfig "=y but booted with '" param "=Y'\n", \
47 				lkdtm_kernel_info);		\
48 			break;					\
49 		default:					\
50 			pr_err("This is probably expected, since this %s was built *without* " #kconfig "=y (and booted without '" param "' specified)\n", \
51 				lkdtm_kernel_info);		\
52 			break;					\
53 		}						\
54 	}							\
55 } while (0)
56 #else
57 #define pr_expected_config_param(kconfig, param) pr_expected_config(kconfig)
58 #endif
59 
60 /* Crash types. */
61 struct crashtype {
62 	const char *name;
63 	void (*func)(void);
64 };
65 
66 #define CRASHTYPE(_name)			\
67 	{					\
68 		.name = __stringify(_name),	\
69 		.func = lkdtm_ ## _name,	\
70 	}
71 
72 /* Category's collection of crashtypes. */
73 struct crashtype_category {
74 	struct crashtype *crashtypes;
75 	size_t len;
76 };
77 
78 /* Each category's crashtypes list. */
79 extern struct crashtype_category bugs_crashtypes;
80 extern struct crashtype_category heap_crashtypes;
81 extern struct crashtype_category perms_crashtypes;
82 extern struct crashtype_category refcount_crashtypes;
83 extern struct crashtype_category usercopy_crashtypes;
84 extern struct crashtype_category stackleak_crashtypes;
85 extern struct crashtype_category cfi_crashtypes;
86 extern struct crashtype_category fortify_crashtypes;
87 extern struct crashtype_category powerpc_crashtypes;
88 
89 /* Each category's init/exit routines. */
90 void __init lkdtm_bugs_init(int *recur_param);
91 void __init lkdtm_heap_init(void);
92 void __exit lkdtm_heap_exit(void);
93 void __init lkdtm_perms_init(void);
94 void __init lkdtm_usercopy_init(void);
95 void __exit lkdtm_usercopy_exit(void);
96 
97 /* Special declaration for function-in-rodata. */
98 void lkdtm_rodata_do_nothing(void);
99 
100 #endif
101