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