1 // SPDX-License-Identifier: GPL-2.0-only
2 /*
3 * Copyright 2022 Google LLC
4 * Author: ramjiyani@google.com (Ramji Jiyani)
5 */
6
7 #include <linux/bsearch.h>
8 #include <linux/errno.h>
9 #include <linux/kernel.h>
10 #include <linux/printk.h>
11 #include <linux/string.h>
12
13 /*
14 * Build time generated header files
15 *
16 * gki_module_protected_exports.h -- Symbols protected from _export_ by unsigned modules
17 * gki_module_unprotected.h -- Symbols allowed to _access_ by unsigned modules
18 */
19 #include <generated/gki_module_protected_exports.h>
20 #include <generated/gki_module_unprotected.h>
21
22 #define MAX_STRCMP_LEN (max(MAX_UNPROTECTED_NAME_LEN, MAX_PROTECTED_EXPORTS_NAME_LEN))
23
24 /* bsearch() comparision callback */
cmp_name(const void * sym,const void * protected_sym)25 static int cmp_name(const void *sym, const void *protected_sym)
26 {
27 return strncmp(sym, protected_sym, MAX_STRCMP_LEN);
28 }
29
30 /**
31 * gki_is_module_protected_export - Is a symbol exported from a protected GKI module?
32 *
33 * @name: Symbol being checked against exported symbols from protected GKI modules
34 */
gki_is_module_protected_export(const char * name)35 bool gki_is_module_protected_export(const char *name)
36 {
37 if (NR_UNPROTECTED_SYMBOLS) {
38 return bsearch(name, gki_protected_exports_symbols, NR_PROTECTED_EXPORTS_SYMBOLS,
39 MAX_PROTECTED_EXPORTS_NAME_LEN, cmp_name) != NULL;
40 } else {
41 /*
42 * If there are no symbols in unprotected list; We don't need to
43 * protect exports as there is no KMI enforcement.
44 * Treat everything exportable in this case.
45 */
46 return false;
47 }
48 }
49
50 /**
51 * gki_is_module_unprotected_symbol - Is a symbol unprotected for unsigned module?
52 *
53 * @name: Symbol being checked in list of unprotected symbols
54 */
gki_is_module_unprotected_symbol(const char * name)55 bool gki_is_module_unprotected_symbol(const char *name)
56 {
57 if (NR_UNPROTECTED_SYMBOLS) {
58 return bsearch(name, gki_unprotected_symbols, NR_UNPROTECTED_SYMBOLS,
59 MAX_UNPROTECTED_NAME_LEN, cmp_name) != NULL;
60 } else {
61 /*
62 * If there are no symbols in unprotected list;
63 * there isn't a KMI enforcement for the kernel.
64 * Treat everything accessible in this case.
65 */
66 return true;
67 }
68 }
69