kexec_file: split KEXEC_VERIFY_SIG into KEXEC_SIG and KEXEC_SIG_FORCE
[linux-2.6-microblaze.git] / arch / x86 / kernel / ima_arch.c
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
11 static enum efi_secureboot_mode get_sb_mode(void)
12 {
13         efi_char16_t efi_SecureBoot_name[] = L"SecureBoot";
14         efi_guid_t efi_variable_guid = EFI_GLOBAL_VARIABLE_GUID;
15         efi_status_t status;
16         unsigned long size;
17         u8 secboot;
18
19         size = sizeof(secboot);
20
21         if (!efi_enabled(EFI_RUNTIME_SERVICES)) {
22                 pr_info("ima: secureboot mode unknown, no efi\n");
23                 return efi_secureboot_mode_unknown;
24         }
25
26         /* Get variable contents into buffer */
27         status = efi.get_variable(efi_SecureBoot_name, &efi_variable_guid,
28                                   NULL, &size, &secboot);
29         if (status == EFI_NOT_FOUND) {
30                 pr_info("ima: secureboot mode disabled\n");
31                 return efi_secureboot_mode_disabled;
32         }
33
34         if (status != EFI_SUCCESS) {
35                 pr_info("ima: secureboot mode unknown\n");
36                 return efi_secureboot_mode_unknown;
37         }
38
39         if (secboot == 0) {
40                 pr_info("ima: secureboot mode disabled\n");
41                 return efi_secureboot_mode_disabled;
42         }
43
44         pr_info("ima: secureboot mode enabled\n");
45         return efi_secureboot_mode_enabled;
46 }
47
48 bool arch_ima_get_secureboot(void)
49 {
50         static enum efi_secureboot_mode sb_mode;
51         static bool initialized;
52
53         if (!initialized && efi_enabled(EFI_BOOT)) {
54                 sb_mode = boot_params.secure_boot;
55
56                 if (sb_mode == efi_secureboot_mode_unset)
57                         sb_mode = get_sb_mode();
58                 initialized = true;
59         }
60
61         if (sb_mode == efi_secureboot_mode_enabled)
62                 return true;
63         else
64                 return false;
65 }
66
67 /* secureboot arch rules */
68 static const char * const sb_arch_rules[] = {
69 #if !IS_ENABLED(CONFIG_KEXEC_SIG)
70         "appraise func=KEXEC_KERNEL_CHECK appraise_type=imasig",
71 #endif /* CONFIG_KEXEC_SIG */
72         "measure func=KEXEC_KERNEL_CHECK",
73 #if !IS_ENABLED(CONFIG_MODULE_SIG)
74         "appraise func=MODULE_CHECK appraise_type=imasig",
75 #endif
76         "measure func=MODULE_CHECK",
77         NULL
78 };
79
80 const char * const *arch_get_ima_policy(void)
81 {
82         if (IS_ENABLED(CONFIG_IMA_ARCH_POLICY) && arch_ima_get_secureboot()) {
83                 if (IS_ENABLED(CONFIG_MODULE_SIG))
84                         set_module_sig_enforced();
85                 return sb_arch_rules;
86         }
87         return NULL;
88 }