]> git.ipfire.org Git - thirdparty/kernel/stable.git/blob - arch/powerpc/kernel/security.c
powerpc64s: Show ori31 availability in spectre_v1 sysfs file not v2
[thirdparty/kernel/stable.git] / arch / powerpc / kernel / security.c
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>
8 #include <linux/debugfs.h>
9 #include <linux/device.h>
10 #include <linux/seq_buf.h>
11
12 #include <asm/debug.h>
13 #include <asm/security_features.h>
14 #include <asm/setup.h>
15
16
17 unsigned long powerpc_security_features __read_mostly = SEC_FTR_DEFAULT;
18
19 bool barrier_nospec_enabled;
20
21 static void enable_barrier_nospec(bool enable)
22 {
23 barrier_nospec_enabled = enable;
24 do_barrier_nospec_fixups(enable);
25 }
26
27 void setup_barrier_nospec(void)
28 {
29 bool enable;
30
31 /*
32 * It would make sense to check SEC_FTR_SPEC_BAR_ORI31 below as well.
33 * But there's a good reason not to. The two flags we check below are
34 * both are enabled by default in the kernel, so if the hcall is not
35 * functional they will be enabled.
36 * On a system where the host firmware has been updated (so the ori
37 * functions as a barrier), but on which the hypervisor (KVM/Qemu) has
38 * not been updated, we would like to enable the barrier. Dropping the
39 * check for SEC_FTR_SPEC_BAR_ORI31 achieves that. The only downside is
40 * we potentially enable the barrier on systems where the host firmware
41 * is not updated, but that's harmless as it's a no-op.
42 */
43 enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
44 security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR);
45
46 enable_barrier_nospec(enable);
47 }
48
49 #ifdef CONFIG_DEBUG_FS
50 static int barrier_nospec_set(void *data, u64 val)
51 {
52 switch (val) {
53 case 0:
54 case 1:
55 break;
56 default:
57 return -EINVAL;
58 }
59
60 if (!!val == !!barrier_nospec_enabled)
61 return 0;
62
63 enable_barrier_nospec(!!val);
64
65 return 0;
66 }
67
68 static int barrier_nospec_get(void *data, u64 *val)
69 {
70 *val = barrier_nospec_enabled ? 1 : 0;
71 return 0;
72 }
73
74 DEFINE_SIMPLE_ATTRIBUTE(fops_barrier_nospec,
75 barrier_nospec_get, barrier_nospec_set, "%llu\n");
76
77 static __init int barrier_nospec_debugfs_init(void)
78 {
79 debugfs_create_file("barrier_nospec", 0600, powerpc_debugfs_root, NULL,
80 &fops_barrier_nospec);
81 return 0;
82 }
83 device_initcall(barrier_nospec_debugfs_init);
84 #endif /* CONFIG_DEBUG_FS */
85
86 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
87 {
88 bool thread_priv;
89
90 thread_priv = security_ftr_enabled(SEC_FTR_L1D_THREAD_PRIV);
91
92 if (rfi_flush || thread_priv) {
93 struct seq_buf s;
94 seq_buf_init(&s, buf, PAGE_SIZE - 1);
95
96 seq_buf_printf(&s, "Mitigation: ");
97
98 if (rfi_flush)
99 seq_buf_printf(&s, "RFI Flush");
100
101 if (rfi_flush && thread_priv)
102 seq_buf_printf(&s, ", ");
103
104 if (thread_priv)
105 seq_buf_printf(&s, "L1D private per thread");
106
107 seq_buf_printf(&s, "\n");
108
109 return s.len;
110 }
111
112 if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
113 !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
114 return sprintf(buf, "Not affected\n");
115
116 return sprintf(buf, "Vulnerable\n");
117 }
118
119 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
120 {
121 struct seq_buf s;
122
123 seq_buf_init(&s, buf, PAGE_SIZE - 1);
124
125 if (security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR)) {
126 if (barrier_nospec_enabled)
127 seq_buf_printf(&s, "Mitigation: __user pointer sanitization");
128 else
129 seq_buf_printf(&s, "Vulnerable");
130
131 if (security_ftr_enabled(SEC_FTR_SPEC_BAR_ORI31))
132 seq_buf_printf(&s, ", ori31 speculation barrier enabled");
133
134 seq_buf_printf(&s, "\n");
135 } else
136 seq_buf_printf(&s, "Not affected\n");
137
138 return s.len;
139 }
140
141 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
142 {
143 struct seq_buf s;
144 bool bcs, ccd;
145
146 seq_buf_init(&s, buf, PAGE_SIZE - 1);
147
148 bcs = security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED);
149 ccd = security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED);
150
151 if (bcs || ccd) {
152 seq_buf_printf(&s, "Mitigation: ");
153
154 if (bcs)
155 seq_buf_printf(&s, "Indirect branch serialisation (kernel only)");
156
157 if (bcs && ccd)
158 seq_buf_printf(&s, ", ");
159
160 if (ccd)
161 seq_buf_printf(&s, "Indirect branch cache disabled");
162 } else
163 seq_buf_printf(&s, "Vulnerable");
164
165 seq_buf_printf(&s, "\n");
166
167 return s.len;
168 }
169
170 /*
171 * Store-forwarding barrier support.
172 */
173
174 static enum stf_barrier_type stf_enabled_flush_types;
175 static bool no_stf_barrier;
176 bool stf_barrier;
177
178 static int __init handle_no_stf_barrier(char *p)
179 {
180 pr_info("stf-barrier: disabled on command line.");
181 no_stf_barrier = true;
182 return 0;
183 }
184
185 early_param("no_stf_barrier", handle_no_stf_barrier);
186
187 /* This is the generic flag used by other architectures */
188 static int __init handle_ssbd(char *p)
189 {
190 if (!p || strncmp(p, "auto", 5) == 0 || strncmp(p, "on", 2) == 0 ) {
191 /* Until firmware tells us, we have the barrier with auto */
192 return 0;
193 } else if (strncmp(p, "off", 3) == 0) {
194 handle_no_stf_barrier(NULL);
195 return 0;
196 } else
197 return 1;
198
199 return 0;
200 }
201 early_param("spec_store_bypass_disable", handle_ssbd);
202
203 /* This is the generic flag used by other architectures */
204 static int __init handle_no_ssbd(char *p)
205 {
206 handle_no_stf_barrier(NULL);
207 return 0;
208 }
209 early_param("nospec_store_bypass_disable", handle_no_ssbd);
210
211 static void stf_barrier_enable(bool enable)
212 {
213 if (enable)
214 do_stf_barrier_fixups(stf_enabled_flush_types);
215 else
216 do_stf_barrier_fixups(STF_BARRIER_NONE);
217
218 stf_barrier = enable;
219 }
220
221 void setup_stf_barrier(void)
222 {
223 enum stf_barrier_type type;
224 bool enable, hv;
225
226 hv = cpu_has_feature(CPU_FTR_HVMODE);
227
228 /* Default to fallback in case fw-features are not available */
229 if (cpu_has_feature(CPU_FTR_ARCH_300))
230 type = STF_BARRIER_EIEIO;
231 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
232 type = STF_BARRIER_SYNC_ORI;
233 else if (cpu_has_feature(CPU_FTR_ARCH_206))
234 type = STF_BARRIER_FALLBACK;
235 else
236 type = STF_BARRIER_NONE;
237
238 enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
239 (security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR) ||
240 (security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) && hv));
241
242 if (type == STF_BARRIER_FALLBACK) {
243 pr_info("stf-barrier: fallback barrier available\n");
244 } else if (type == STF_BARRIER_SYNC_ORI) {
245 pr_info("stf-barrier: hwsync barrier available\n");
246 } else if (type == STF_BARRIER_EIEIO) {
247 pr_info("stf-barrier: eieio barrier available\n");
248 }
249
250 stf_enabled_flush_types = type;
251
252 if (!no_stf_barrier)
253 stf_barrier_enable(enable);
254 }
255
256 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
257 {
258 if (stf_barrier && stf_enabled_flush_types != STF_BARRIER_NONE) {
259 const char *type;
260 switch (stf_enabled_flush_types) {
261 case STF_BARRIER_EIEIO:
262 type = "eieio";
263 break;
264 case STF_BARRIER_SYNC_ORI:
265 type = "hwsync";
266 break;
267 case STF_BARRIER_FALLBACK:
268 type = "fallback";
269 break;
270 default:
271 type = "unknown";
272 }
273 return sprintf(buf, "Mitigation: Kernel entry/exit barrier (%s)\n", type);
274 }
275
276 if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
277 !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
278 return sprintf(buf, "Not affected\n");
279
280 return sprintf(buf, "Vulnerable\n");
281 }
282
283 #ifdef CONFIG_DEBUG_FS
284 static int stf_barrier_set(void *data, u64 val)
285 {
286 bool enable;
287
288 if (val == 1)
289 enable = true;
290 else if (val == 0)
291 enable = false;
292 else
293 return -EINVAL;
294
295 /* Only do anything if we're changing state */
296 if (enable != stf_barrier)
297 stf_barrier_enable(enable);
298
299 return 0;
300 }
301
302 static int stf_barrier_get(void *data, u64 *val)
303 {
304 *val = stf_barrier ? 1 : 0;
305 return 0;
306 }
307
308 DEFINE_SIMPLE_ATTRIBUTE(fops_stf_barrier, stf_barrier_get, stf_barrier_set, "%llu\n");
309
310 static __init int stf_barrier_debugfs_init(void)
311 {
312 debugfs_create_file("stf_barrier", 0600, powerpc_debugfs_root, NULL, &fops_stf_barrier);
313 return 0;
314 }
315 device_initcall(stf_barrier_debugfs_init);
316 #endif /* CONFIG_DEBUG_FS */