blob: 3eb9c45f28d72c5125fcca412df4229ccb7aba0b [file] [log] [blame]
Michael Ellerman9a868f62018-03-27 23:01:44 +11001// 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 Ellerman8ad33042018-03-27 23:01:48 +11008#include <linux/device.h>
Michael Ellermanff348352018-03-27 23:01:49 +11009#include <linux/seq_buf.h>
Michael Ellerman8ad33042018-03-27 23:01:48 +110010
Michal Suchanekcb3d6752018-04-24 14:15:57 +100011#include <asm/debugfs.h>
Michael Ellerman9a868f62018-03-27 23:01:44 +110012#include <asm/security_features.h>
Michal Suchanek2eea7f02018-04-24 14:15:55 +100013#include <asm/setup.h>
Michael Ellerman9a868f62018-03-27 23:01:44 +110014
15
Mauricio Faria de Oliveirae7347a82018-03-30 14:28:24 -030016unsigned long powerpc_security_features __read_mostly = SEC_FTR_DEFAULT;
Michael Ellerman8ad33042018-03-27 23:01:48 +110017
Michal Suchanek815069c2018-04-24 14:15:56 +100018bool barrier_nospec_enabled;
Michal Suchanek2eea7f02018-04-24 14:15:55 +100019
20static void enable_barrier_nospec(bool enable)
21{
22 barrier_nospec_enabled = enable;
23 do_barrier_nospec_fixups(enable);
24}
25
Michal Suchanekcb3d6752018-04-24 14:15:57 +100026void 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
49static 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
67static int barrier_nospec_get(void *data, u64 *val)
68{
69 *val = barrier_nospec_enabled ? 1 : 0;
70 return 0;
71}
72
73DEFINE_SIMPLE_ATTRIBUTE(fops_barrier_nospec,
74 barrier_nospec_get, barrier_nospec_set, "%llu\n");
75
76static __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}
82device_initcall(barrier_nospec_debugfs_init);
83#endif /* CONFIG_DEBUG_FS */
84
Michael Ellerman8ad33042018-03-27 23:01:48 +110085ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
86{
Michael Ellermanff348352018-03-27 23:01:49 +110087 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 Ellerman8ad33042018-03-27 23:01:48 +1100114
115 return sprintf(buf, "Vulnerable\n");
116}
Michael Ellerman56986012018-03-27 23:01:52 +1100117
118ssize_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 Suchaneka3775142018-05-28 15:19:14 +0200123 if (barrier_nospec_enabled)
124 return sprintf(buf, "Mitigation: __user pointer sanitization\n");
125
Michael Ellerman56986012018-03-27 23:01:52 +1100126 return sprintf(buf, "Vulnerable\n");
127}
Michael Ellermand6fbe1c2018-03-27 23:01:53 +1100128
129ssize_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}