GNU Linux-libre 4.4.288-gnu1
[releases.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/cpu.h>
8 #include <linux/kernel.h>
9 #include <linux/debugfs.h>
10 #include <linux/device.h>
11 #include <linux/seq_buf.h>
12
13 #include <asm/debug.h>
14 #include <asm/asm-prototypes.h>
15 #include <asm/code-patching.h>
16 #include <asm/security_features.h>
17 #include <asm/setup.h>
18
19
20 unsigned long powerpc_security_features __read_mostly = SEC_FTR_DEFAULT;
21
22 enum count_cache_flush_type {
23         COUNT_CACHE_FLUSH_NONE  = 0x1,
24         COUNT_CACHE_FLUSH_SW    = 0x2,
25         COUNT_CACHE_FLUSH_HW    = 0x4,
26 };
27 static enum count_cache_flush_type count_cache_flush_type = COUNT_CACHE_FLUSH_NONE;
28 static bool link_stack_flush_enabled;
29
30 bool barrier_nospec_enabled;
31 static bool no_nospec;
32 static bool btb_flush_enabled;
33 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3S_64)
34 static bool no_spectrev2;
35 #endif
36
37 static void enable_barrier_nospec(bool enable)
38 {
39         barrier_nospec_enabled = enable;
40         do_barrier_nospec_fixups(enable);
41 }
42
43 void setup_barrier_nospec(void)
44 {
45         bool enable;
46
47         /*
48          * It would make sense to check SEC_FTR_SPEC_BAR_ORI31 below as well.
49          * But there's a good reason not to. The two flags we check below are
50          * both are enabled by default in the kernel, so if the hcall is not
51          * functional they will be enabled.
52          * On a system where the host firmware has been updated (so the ori
53          * functions as a barrier), but on which the hypervisor (KVM/Qemu) has
54          * not been updated, we would like to enable the barrier. Dropping the
55          * check for SEC_FTR_SPEC_BAR_ORI31 achieves that. The only downside is
56          * we potentially enable the barrier on systems where the host firmware
57          * is not updated, but that's harmless as it's a no-op.
58          */
59         enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
60                  security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR);
61
62         if (!no_nospec)
63                 enable_barrier_nospec(enable);
64 }
65
66 static int __init handle_nospectre_v1(char *p)
67 {
68         no_nospec = true;
69
70         return 0;
71 }
72 early_param("nospectre_v1", handle_nospectre_v1);
73
74 #ifdef CONFIG_DEBUG_FS
75 static int barrier_nospec_set(void *data, u64 val)
76 {
77         switch (val) {
78         case 0:
79         case 1:
80                 break;
81         default:
82                 return -EINVAL;
83         }
84
85         if (!!val == !!barrier_nospec_enabled)
86                 return 0;
87
88         enable_barrier_nospec(!!val);
89
90         return 0;
91 }
92
93 static int barrier_nospec_get(void *data, u64 *val)
94 {
95         *val = barrier_nospec_enabled ? 1 : 0;
96         return 0;
97 }
98
99 DEFINE_SIMPLE_ATTRIBUTE(fops_barrier_nospec,
100                         barrier_nospec_get, barrier_nospec_set, "%llu\n");
101
102 static __init int barrier_nospec_debugfs_init(void)
103 {
104         debugfs_create_file("barrier_nospec", 0600, powerpc_debugfs_root, NULL,
105                             &fops_barrier_nospec);
106         return 0;
107 }
108 device_initcall(barrier_nospec_debugfs_init);
109 #endif /* CONFIG_DEBUG_FS */
110
111 #if defined(CONFIG_PPC_FSL_BOOK3E) || defined(CONFIG_PPC_BOOK3S_64)
112 static int __init handle_nospectre_v2(char *p)
113 {
114         no_spectrev2 = true;
115
116         return 0;
117 }
118 early_param("nospectre_v2", handle_nospectre_v2);
119 #endif /* CONFIG_PPC_FSL_BOOK3E || CONFIG_PPC_BOOK3S_64 */
120
121 #ifdef CONFIG_PPC_FSL_BOOK3E
122 void setup_spectre_v2(void)
123 {
124         if (no_spectrev2)
125                 do_btb_flush_fixups();
126         else
127                 btb_flush_enabled = true;
128 }
129 #endif /* CONFIG_PPC_FSL_BOOK3E */
130
131 #ifdef CONFIG_PPC_BOOK3S_64
132 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
133 {
134         bool thread_priv;
135
136         thread_priv = security_ftr_enabled(SEC_FTR_L1D_THREAD_PRIV);
137
138         if (rfi_flush) {
139                 struct seq_buf s;
140                 seq_buf_init(&s, buf, PAGE_SIZE - 1);
141
142                 seq_buf_printf(&s, "Mitigation: RFI Flush");
143                 if (thread_priv)
144                         seq_buf_printf(&s, ", L1D private per thread");
145
146                 seq_buf_printf(&s, "\n");
147
148                 return s.len;
149         }
150
151         if (thread_priv)
152                 return sprintf(buf, "Vulnerable: L1D private per thread\n");
153
154         if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
155             !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
156                 return sprintf(buf, "Not affected\n");
157
158         return sprintf(buf, "Vulnerable\n");
159 }
160
161 ssize_t cpu_show_l1tf(struct device *dev, struct device_attribute *attr, char *buf)
162 {
163         return cpu_show_meltdown(dev, attr, buf);
164 }
165 #endif
166
167 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
168 {
169         struct seq_buf s;
170
171         seq_buf_init(&s, buf, PAGE_SIZE - 1);
172
173         if (security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR)) {
174                 if (barrier_nospec_enabled)
175                         seq_buf_printf(&s, "Mitigation: __user pointer sanitization");
176                 else
177                         seq_buf_printf(&s, "Vulnerable");
178
179                 if (security_ftr_enabled(SEC_FTR_SPEC_BAR_ORI31))
180                         seq_buf_printf(&s, ", ori31 speculation barrier enabled");
181
182                 seq_buf_printf(&s, "\n");
183         } else
184                 seq_buf_printf(&s, "Not affected\n");
185
186         return s.len;
187 }
188
189 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
190 {
191         struct seq_buf s;
192         bool bcs, ccd;
193
194         seq_buf_init(&s, buf, PAGE_SIZE - 1);
195
196         bcs = security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED);
197         ccd = security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED);
198
199         if (bcs || ccd) {
200                 seq_buf_printf(&s, "Mitigation: ");
201
202                 if (bcs)
203                         seq_buf_printf(&s, "Indirect branch serialisation (kernel only)");
204
205                 if (bcs && ccd)
206                         seq_buf_printf(&s, ", ");
207
208                 if (ccd)
209                         seq_buf_printf(&s, "Indirect branch cache disabled");
210
211                 if (link_stack_flush_enabled)
212                         seq_buf_printf(&s, ", Software link stack flush");
213
214         } else if (count_cache_flush_type != COUNT_CACHE_FLUSH_NONE) {
215                 seq_buf_printf(&s, "Mitigation: Software count cache flush");
216
217                 if (count_cache_flush_type == COUNT_CACHE_FLUSH_HW)
218                         seq_buf_printf(&s, " (hardware accelerated)");
219
220                 if (link_stack_flush_enabled)
221                         seq_buf_printf(&s, ", Software link stack flush");
222
223         } else if (btb_flush_enabled) {
224                 seq_buf_printf(&s, "Mitigation: Branch predictor state flush");
225         } else {
226                 seq_buf_printf(&s, "Vulnerable");
227         }
228
229         seq_buf_printf(&s, "\n");
230
231         return s.len;
232 }
233
234 #ifdef CONFIG_PPC_BOOK3S_64
235 /*
236  * Store-forwarding barrier support.
237  */
238
239 static enum stf_barrier_type stf_enabled_flush_types;
240 static bool no_stf_barrier;
241 bool stf_barrier;
242
243 static int __init handle_no_stf_barrier(char *p)
244 {
245         pr_info("stf-barrier: disabled on command line.");
246         no_stf_barrier = true;
247         return 0;
248 }
249
250 early_param("no_stf_barrier", handle_no_stf_barrier);
251
252 /* This is the generic flag used by other architectures */
253 static int __init handle_ssbd(char *p)
254 {
255         if (!p || strncmp(p, "auto", 5) == 0 || strncmp(p, "on", 2) == 0 ) {
256                 /* Until firmware tells us, we have the barrier with auto */
257                 return 0;
258         } else if (strncmp(p, "off", 3) == 0) {
259                 handle_no_stf_barrier(NULL);
260                 return 0;
261         } else
262                 return 1;
263
264         return 0;
265 }
266 early_param("spec_store_bypass_disable", handle_ssbd);
267
268 /* This is the generic flag used by other architectures */
269 static int __init handle_no_ssbd(char *p)
270 {
271         handle_no_stf_barrier(NULL);
272         return 0;
273 }
274 early_param("nospec_store_bypass_disable", handle_no_ssbd);
275
276 static void stf_barrier_enable(bool enable)
277 {
278         if (enable)
279                 do_stf_barrier_fixups(stf_enabled_flush_types);
280         else
281                 do_stf_barrier_fixups(STF_BARRIER_NONE);
282
283         stf_barrier = enable;
284 }
285
286 void setup_stf_barrier(void)
287 {
288         enum stf_barrier_type type;
289         bool enable, hv;
290
291         hv = cpu_has_feature(CPU_FTR_HVMODE);
292
293         /* Default to fallback in case fw-features are not available */
294         if (cpu_has_feature(CPU_FTR_ARCH_207S))
295                 type = STF_BARRIER_SYNC_ORI;
296         else if (cpu_has_feature(CPU_FTR_ARCH_206))
297                 type = STF_BARRIER_FALLBACK;
298         else
299                 type = STF_BARRIER_NONE;
300
301         enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
302                 (security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR) ||
303                  (security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) && hv));
304
305         if (type == STF_BARRIER_FALLBACK) {
306                 pr_info("stf-barrier: fallback barrier available\n");
307         } else if (type == STF_BARRIER_SYNC_ORI) {
308                 pr_info("stf-barrier: hwsync barrier available\n");
309         } else if (type == STF_BARRIER_EIEIO) {
310                 pr_info("stf-barrier: eieio barrier available\n");
311         }
312
313         stf_enabled_flush_types = type;
314
315         if (!no_stf_barrier)
316                 stf_barrier_enable(enable);
317 }
318
319 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
320 {
321         if (stf_barrier && stf_enabled_flush_types != STF_BARRIER_NONE) {
322                 const char *type;
323                 switch (stf_enabled_flush_types) {
324                 case STF_BARRIER_EIEIO:
325                         type = "eieio";
326                         break;
327                 case STF_BARRIER_SYNC_ORI:
328                         type = "hwsync";
329                         break;
330                 case STF_BARRIER_FALLBACK:
331                         type = "fallback";
332                         break;
333                 default:
334                         type = "unknown";
335                 }
336                 return sprintf(buf, "Mitigation: Kernel entry/exit barrier (%s)\n", type);
337         }
338
339         if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
340             !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
341                 return sprintf(buf, "Not affected\n");
342
343         return sprintf(buf, "Vulnerable\n");
344 }
345
346 #ifdef CONFIG_DEBUG_FS
347 static int stf_barrier_set(void *data, u64 val)
348 {
349         bool enable;
350
351         if (val == 1)
352                 enable = true;
353         else if (val == 0)
354                 enable = false;
355         else
356                 return -EINVAL;
357
358         /* Only do anything if we're changing state */
359         if (enable != stf_barrier)
360                 stf_barrier_enable(enable);
361
362         return 0;
363 }
364
365 static int stf_barrier_get(void *data, u64 *val)
366 {
367         *val = stf_barrier ? 1 : 0;
368         return 0;
369 }
370
371 DEFINE_SIMPLE_ATTRIBUTE(fops_stf_barrier, stf_barrier_get, stf_barrier_set, "%llu\n");
372
373 static __init int stf_barrier_debugfs_init(void)
374 {
375         debugfs_create_file("stf_barrier", 0600, powerpc_debugfs_root, NULL, &fops_stf_barrier);
376         return 0;
377 }
378 device_initcall(stf_barrier_debugfs_init);
379 #endif /* CONFIG_DEBUG_FS */
380
381 static void no_count_cache_flush(void)
382 {
383         count_cache_flush_type = COUNT_CACHE_FLUSH_NONE;
384         pr_info("count-cache-flush: software flush disabled.\n");
385 }
386
387 static void toggle_count_cache_flush(bool enable)
388 {
389         if (!security_ftr_enabled(SEC_FTR_FLUSH_COUNT_CACHE) &&
390             !security_ftr_enabled(SEC_FTR_FLUSH_LINK_STACK))
391                 enable = false;
392
393         if (!enable) {
394                 patch_instruction_site(&patch__call_flush_count_cache, PPC_INST_NOP);
395 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
396                 patch_instruction_site(&patch__call_kvm_flush_link_stack, PPC_INST_NOP);
397 #endif
398                 pr_info("link-stack-flush: software flush disabled.\n");
399                 link_stack_flush_enabled = false;
400                 no_count_cache_flush();
401                 return;
402         }
403
404         // This enables the branch from _switch to flush_count_cache
405         patch_branch_site(&patch__call_flush_count_cache,
406                           (u64)&flush_count_cache, BRANCH_SET_LINK);
407
408 #ifdef CONFIG_KVM_BOOK3S_HV_POSSIBLE
409         // This enables the branch from guest_exit_cont to kvm_flush_link_stack
410         patch_branch_site(&patch__call_kvm_flush_link_stack,
411                           (u64)&kvm_flush_link_stack, BRANCH_SET_LINK);
412 #endif
413
414         pr_info("link-stack-flush: software flush enabled.\n");
415         link_stack_flush_enabled = true;
416
417         // If we just need to flush the link stack, patch an early return
418         if (!security_ftr_enabled(SEC_FTR_FLUSH_COUNT_CACHE)) {
419                 patch_instruction_site(&patch__flush_link_stack_return, PPC_INST_BLR);
420                 no_count_cache_flush();
421                 return;
422         }
423
424         if (!security_ftr_enabled(SEC_FTR_BCCTR_FLUSH_ASSIST)) {
425                 count_cache_flush_type = COUNT_CACHE_FLUSH_SW;
426                 pr_info("count-cache-flush: full software flush sequence enabled.\n");
427                 return;
428         }
429
430         patch_instruction_site(&patch__flush_count_cache_return, PPC_INST_BLR);
431         count_cache_flush_type = COUNT_CACHE_FLUSH_HW;
432         pr_info("count-cache-flush: hardware assisted flush sequence enabled\n");
433 }
434
435 void setup_count_cache_flush(void)
436 {
437         bool enable = true;
438
439         if (no_spectrev2 || cpu_mitigations_off()) {
440                 if (security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED) ||
441                     security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED))
442                         pr_warn("Spectre v2 mitigations not fully under software control, can't disable\n");
443
444                 enable = false;
445         }
446
447         /*
448          * There's no firmware feature flag/hypervisor bit to tell us we need to
449          * flush the link stack on context switch. So we set it here if we see
450          * either of the Spectre v2 mitigations that aim to protect userspace.
451          */
452         if (security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED) ||
453             security_ftr_enabled(SEC_FTR_FLUSH_COUNT_CACHE))
454                 security_ftr_set(SEC_FTR_FLUSH_LINK_STACK);
455
456         toggle_count_cache_flush(enable);
457 }
458
459 #ifdef CONFIG_DEBUG_FS
460 static int count_cache_flush_set(void *data, u64 val)
461 {
462         bool enable;
463
464         if (val == 1)
465                 enable = true;
466         else if (val == 0)
467                 enable = false;
468         else
469                 return -EINVAL;
470
471         toggle_count_cache_flush(enable);
472
473         return 0;
474 }
475
476 static int count_cache_flush_get(void *data, u64 *val)
477 {
478         if (count_cache_flush_type == COUNT_CACHE_FLUSH_NONE)
479                 *val = 0;
480         else
481                 *val = 1;
482
483         return 0;
484 }
485
486 DEFINE_SIMPLE_ATTRIBUTE(fops_count_cache_flush, count_cache_flush_get,
487                         count_cache_flush_set, "%llu\n");
488
489 static __init int count_cache_flush_debugfs_init(void)
490 {
491         debugfs_create_file("count_cache_flush", 0600, powerpc_debugfs_root,
492                             NULL, &fops_count_cache_flush);
493         return 0;
494 }
495 device_initcall(count_cache_flush_debugfs_init);
496 #endif /* CONFIG_DEBUG_FS */
497 #endif /* CONFIG_PPC_BOOK3S_64 */