Michael Ellerman | 9a868f6 | 2018-03-27 23:01:44 +1100 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | // |
| 3 | // Security related flags and so on. |
| 4 | // |
| 5 | // Copyright 2018, Michael Ellerman, IBM Corporation. |
| 6 | |
| 7 | #include <linux/kernel.h> |
Michael Ellerman | 8ad3304 | 2018-03-27 23:01:48 +1100 | [diff] [blame] | 8 | #include <linux/device.h> |
Michael Ellerman | ff34835 | 2018-03-27 23:01:49 +1100 | [diff] [blame] | 9 | #include <linux/seq_buf.h> |
Michael Ellerman | 8ad3304 | 2018-03-27 23:01:48 +1100 | [diff] [blame] | 10 | |
Michal Suchanek | cb3d675 | 2018-04-24 14:15:57 +1000 | [diff] [blame] | 11 | #include <asm/debugfs.h> |
Michael Ellerman | 9a868f6 | 2018-03-27 23:01:44 +1100 | [diff] [blame] | 12 | #include <asm/security_features.h> |
Michal Suchanek | 2eea7f0 | 2018-04-24 14:15:55 +1000 | [diff] [blame] | 13 | #include <asm/setup.h> |
Michael Ellerman | 9a868f6 | 2018-03-27 23:01:44 +1100 | [diff] [blame] | 14 | |
| 15 | |
Mauricio Faria de Oliveira | e7347a8 | 2018-03-30 14:28:24 -0300 | [diff] [blame] | 16 | unsigned long powerpc_security_features __read_mostly = SEC_FTR_DEFAULT; |
Michael Ellerman | 8ad3304 | 2018-03-27 23:01:48 +1100 | [diff] [blame] | 17 | |
Michal Suchanek | 815069c | 2018-04-24 14:15:56 +1000 | [diff] [blame] | 18 | bool barrier_nospec_enabled; |
Michal Suchanek | 2eea7f0 | 2018-04-24 14:15:55 +1000 | [diff] [blame] | 19 | |
| 20 | static void enable_barrier_nospec(bool enable) |
| 21 | { |
| 22 | barrier_nospec_enabled = enable; |
| 23 | do_barrier_nospec_fixups(enable); |
| 24 | } |
| 25 | |
Michal Suchanek | cb3d675 | 2018-04-24 14:15:57 +1000 | [diff] [blame] | 26 | void setup_barrier_nospec(void) |
| 27 | { |
| 28 | bool enable; |
| 29 | |
| 30 | /* |
| 31 | * It would make sense to check SEC_FTR_SPEC_BAR_ORI31 below as well. |
| 32 | * But there's a good reason not to. The two flags we check below are |
| 33 | * both are enabled by default in the kernel, so if the hcall is not |
| 34 | * functional they will be enabled. |
| 35 | * On a system where the host firmware has been updated (so the ori |
| 36 | * functions as a barrier), but on which the hypervisor (KVM/Qemu) has |
| 37 | * not been updated, we would like to enable the barrier. Dropping the |
| 38 | * check for SEC_FTR_SPEC_BAR_ORI31 achieves that. The only downside is |
| 39 | * we potentially enable the barrier on systems where the host firmware |
| 40 | * is not updated, but that's harmless as it's a no-op. |
| 41 | */ |
| 42 | enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) && |
| 43 | security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR); |
| 44 | |
| 45 | enable_barrier_nospec(enable); |
| 46 | } |
| 47 | |
| 48 | #ifdef CONFIG_DEBUG_FS |
| 49 | static int barrier_nospec_set(void *data, u64 val) |
| 50 | { |
| 51 | switch (val) { |
| 52 | case 0: |
| 53 | case 1: |
| 54 | break; |
| 55 | default: |
| 56 | return -EINVAL; |
| 57 | } |
| 58 | |
| 59 | if (!!val == !!barrier_nospec_enabled) |
| 60 | return 0; |
| 61 | |
| 62 | enable_barrier_nospec(!!val); |
| 63 | |
| 64 | return 0; |
| 65 | } |
| 66 | |
| 67 | static int barrier_nospec_get(void *data, u64 *val) |
| 68 | { |
| 69 | *val = barrier_nospec_enabled ? 1 : 0; |
| 70 | return 0; |
| 71 | } |
| 72 | |
| 73 | DEFINE_SIMPLE_ATTRIBUTE(fops_barrier_nospec, |
| 74 | barrier_nospec_get, barrier_nospec_set, "%llu\n"); |
| 75 | |
| 76 | static __init int barrier_nospec_debugfs_init(void) |
| 77 | { |
| 78 | debugfs_create_file("barrier_nospec", 0600, powerpc_debugfs_root, NULL, |
| 79 | &fops_barrier_nospec); |
| 80 | return 0; |
| 81 | } |
| 82 | device_initcall(barrier_nospec_debugfs_init); |
| 83 | #endif /* CONFIG_DEBUG_FS */ |
| 84 | |
Michael Ellerman | 8ad3304 | 2018-03-27 23:01:48 +1100 | [diff] [blame] | 85 | ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf) |
| 86 | { |
Michael Ellerman | ff34835 | 2018-03-27 23:01:49 +1100 | [diff] [blame] | 87 | bool thread_priv; |
| 88 | |
| 89 | thread_priv = security_ftr_enabled(SEC_FTR_L1D_THREAD_PRIV); |
| 90 | |
| 91 | if (rfi_flush || thread_priv) { |
| 92 | struct seq_buf s; |
| 93 | seq_buf_init(&s, buf, PAGE_SIZE - 1); |
| 94 | |
| 95 | seq_buf_printf(&s, "Mitigation: "); |
| 96 | |
| 97 | if (rfi_flush) |
| 98 | seq_buf_printf(&s, "RFI Flush"); |
| 99 | |
| 100 | if (rfi_flush && thread_priv) |
| 101 | seq_buf_printf(&s, ", "); |
| 102 | |
| 103 | if (thread_priv) |
| 104 | seq_buf_printf(&s, "L1D private per thread"); |
| 105 | |
| 106 | seq_buf_printf(&s, "\n"); |
| 107 | |
| 108 | return s.len; |
| 109 | } |
| 110 | |
| 111 | if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) && |
| 112 | !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR)) |
| 113 | return sprintf(buf, "Not affected\n"); |
Michael Ellerman | 8ad3304 | 2018-03-27 23:01:48 +1100 | [diff] [blame] | 114 | |
| 115 | return sprintf(buf, "Vulnerable\n"); |
| 116 | } |
Michael Ellerman | 5698601 | 2018-03-27 23:01:52 +1100 | [diff] [blame] | 117 | |
| 118 | ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf) |
| 119 | { |
| 120 | if (!security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR)) |
| 121 | return sprintf(buf, "Not affected\n"); |
| 122 | |
Michal Suchanek | a377514 | 2018-05-28 15:19:14 +0200 | [diff] [blame^] | 123 | if (barrier_nospec_enabled) |
| 124 | return sprintf(buf, "Mitigation: __user pointer sanitization\n"); |
| 125 | |
Michael Ellerman | 5698601 | 2018-03-27 23:01:52 +1100 | [diff] [blame] | 126 | return sprintf(buf, "Vulnerable\n"); |
| 127 | } |
Michael Ellerman | d6fbe1c | 2018-03-27 23:01:53 +1100 | [diff] [blame] | 128 | |
| 129 | ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf) |
| 130 | { |
| 131 | bool bcs, ccd, ori; |
| 132 | struct seq_buf s; |
| 133 | |
| 134 | seq_buf_init(&s, buf, PAGE_SIZE - 1); |
| 135 | |
| 136 | bcs = security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED); |
| 137 | ccd = security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED); |
| 138 | ori = security_ftr_enabled(SEC_FTR_SPEC_BAR_ORI31); |
| 139 | |
| 140 | if (bcs || ccd) { |
| 141 | seq_buf_printf(&s, "Mitigation: "); |
| 142 | |
| 143 | if (bcs) |
| 144 | seq_buf_printf(&s, "Indirect branch serialisation (kernel only)"); |
| 145 | |
| 146 | if (bcs && ccd) |
| 147 | seq_buf_printf(&s, ", "); |
| 148 | |
| 149 | if (ccd) |
| 150 | seq_buf_printf(&s, "Indirect branch cache disabled"); |
| 151 | } else |
| 152 | seq_buf_printf(&s, "Vulnerable"); |
| 153 | |
| 154 | if (ori) |
| 155 | seq_buf_printf(&s, ", ori31 speculation barrier enabled"); |
| 156 | |
| 157 | seq_buf_printf(&s, "\n"); |
| 158 | |
| 159 | return s.len; |
| 160 | } |