1 /* SPDX-License-Identifier: GPL-2.0+ */
2 /*
3  * Copyright (C) 2018 IBM Corporation
4  */
5 #include <linux/efi.h>
6 #include <linux/module.h>
7 #include <linux/ima.h>
8 
9 extern struct boot_params boot_params;
10 
get_sb_mode(void)11 static enum efi_secureboot_mode get_sb_mode(void)
12 {
13 	efi_guid_t efi_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
14 	efi_status_t status;
15 	unsigned long size;
16 	u8 secboot, setupmode;
17 
18 	size = sizeof(secboot);
19 
20 	if (!efi_rt_services_supported(EFI_RT_SUPPORTED_GET_VARIABLE)) {
21 		pr_info("ima: secureboot mode unknown, no efi\n");
22 		return efi_secureboot_mode_unknown;
23 	}
24 
25 	/* Get variable contents into buffer */
26 	status = efi.get_variable(L"SecureBoot", &efi_variable_guid,
27 				  NULL, &size, &secboot);
28 	if (status == EFI_NOT_FOUND) {
29 		pr_info("ima: secureboot mode disabled\n");
30 		return efi_secureboot_mode_disabled;
31 	}
32 
33 	if (status != EFI_SUCCESS) {
34 		pr_info("ima: secureboot mode unknown\n");
35 		return efi_secureboot_mode_unknown;
36 	}
37 
38 	size = sizeof(setupmode);
39 	status = efi.get_variable(L"SetupMode", &efi_variable_guid,
40 				  NULL, &size, &setupmode);
41 
42 	if (status != EFI_SUCCESS)	/* ignore unknown SetupMode */
43 		setupmode = 0;
44 
45 	if (secboot == 0 || setupmode == 1) {
46 		pr_info("ima: secureboot mode disabled\n");
47 		return efi_secureboot_mode_disabled;
48 	}
49 
50 	pr_info("ima: secureboot mode enabled\n");
51 	return efi_secureboot_mode_enabled;
52 }
53 
arch_ima_get_secureboot(void)54 bool arch_ima_get_secureboot(void)
55 {
56 	static enum efi_secureboot_mode sb_mode;
57 	static bool initialized;
58 
59 	if (!initialized && efi_enabled(EFI_BOOT)) {
60 		sb_mode = boot_params.secure_boot;
61 
62 		if (sb_mode == efi_secureboot_mode_unset)
63 			sb_mode = get_sb_mode();
64 		initialized = true;
65 	}
66 
67 	if (sb_mode == efi_secureboot_mode_enabled)
68 		return true;
69 	else
70 		return false;
71 }
72 
73 /* secureboot arch rules */
74 static const char * const sb_arch_rules[] = {
75 #if !IS_ENABLED(CONFIG_KEXEC_SIG)
76 	"appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig",
77 #endif /* CONFIG_KEXEC_SIG */
78 	"measure func=KEXEC_KERNEL_CHECK",
79 #if !IS_ENABLED(CONFIG_MODULE_SIG)
80 	"appraise func=MODULE_CHECK appraise_type=imasig",
81 #endif
82 	"measure func=MODULE_CHECK",
83 	NULL
84 };
85 
arch_get_ima_policy(void)86 const char * const *arch_get_ima_policy(void)
87 {
88 	if (IS_ENABLED(CONFIG_IMA_ARCH_POLICY) && arch_ima_get_secureboot()) {
89 		if (IS_ENABLED(CONFIG_MODULE_SIG))
90 			set_module_sig_enforced();
91 		return sb_arch_rules;
92 	}
93 	return NULL;
94 }
95