]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - arch/powerpc/kernel/security.c
powerpc/64s: Add support for a store forwarding barrier at kernel entry/exit
[mirror_ubuntu-bionic-kernel.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/device.h>
9 #include <linux/seq_buf.h>
10
11 #include <asm/debugfs.h>
12 #include <asm/security_features.h>
13
14
15 unsigned long powerpc_security_features __read_mostly = \
16 SEC_FTR_L1D_FLUSH_HV | \
17 SEC_FTR_L1D_FLUSH_PR | \
18 SEC_FTR_BNDS_CHK_SPEC_BAR | \
19 SEC_FTR_FAVOUR_SECURITY;
20
21
22 ssize_t cpu_show_meltdown(struct device *dev, struct device_attribute *attr, char *buf)
23 {
24 bool thread_priv;
25
26 thread_priv = security_ftr_enabled(SEC_FTR_L1D_THREAD_PRIV);
27
28 if (rfi_flush || thread_priv) {
29 struct seq_buf s;
30 seq_buf_init(&s, buf, PAGE_SIZE - 1);
31
32 seq_buf_printf(&s, "Mitigation: ");
33
34 if (rfi_flush)
35 seq_buf_printf(&s, "RFI Flush");
36
37 if (rfi_flush && thread_priv)
38 seq_buf_printf(&s, ", ");
39
40 if (thread_priv)
41 seq_buf_printf(&s, "L1D private per thread");
42
43 seq_buf_printf(&s, "\n");
44
45 return s.len;
46 }
47
48 if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
49 !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
50 return sprintf(buf, "Not affected\n");
51
52 return sprintf(buf, "Vulnerable\n");
53 }
54
55 ssize_t cpu_show_spectre_v1(struct device *dev, struct device_attribute *attr, char *buf)
56 {
57 if (!security_ftr_enabled(SEC_FTR_BNDS_CHK_SPEC_BAR))
58 return sprintf(buf, "Not affected\n");
59
60 return sprintf(buf, "Vulnerable\n");
61 }
62
63 ssize_t cpu_show_spectre_v2(struct device *dev, struct device_attribute *attr, char *buf)
64 {
65 bool bcs, ccd, ori;
66 struct seq_buf s;
67
68 seq_buf_init(&s, buf, PAGE_SIZE - 1);
69
70 bcs = security_ftr_enabled(SEC_FTR_BCCTRL_SERIALISED);
71 ccd = security_ftr_enabled(SEC_FTR_COUNT_CACHE_DISABLED);
72 ori = security_ftr_enabled(SEC_FTR_SPEC_BAR_ORI31);
73
74 if (bcs || ccd) {
75 seq_buf_printf(&s, "Mitigation: ");
76
77 if (bcs)
78 seq_buf_printf(&s, "Indirect branch serialisation (kernel only)");
79
80 if (bcs && ccd)
81 seq_buf_printf(&s, ", ");
82
83 if (ccd)
84 seq_buf_printf(&s, "Indirect branch cache disabled");
85 } else
86 seq_buf_printf(&s, "Vulnerable");
87
88 if (ori)
89 seq_buf_printf(&s, ", ori31 speculation barrier enabled");
90
91 seq_buf_printf(&s, "\n");
92
93 return s.len;
94 }
95
96 /*
97 * Store-forwarding barrier support.
98 */
99
100 static enum stf_barrier_type stf_enabled_flush_types;
101 static bool no_stf_barrier;
102 bool stf_barrier;
103
104 static int __init handle_no_stf_barrier(char *p)
105 {
106 pr_info("stf-barrier: disabled on command line.");
107 no_stf_barrier = true;
108 return 0;
109 }
110
111 early_param("no_stf_barrier", handle_no_stf_barrier);
112
113 /* This is the generic flag used by other architectures */
114 static int __init handle_ssbd(char *p)
115 {
116 if (!p || strncmp(p, "auto", 5) == 0 || strncmp(p, "on", 2) == 0 ) {
117 /* Until firmware tells us, we have the barrier with auto */
118 return 0;
119 } else if (strncmp(p, "off", 3) == 0) {
120 handle_no_stf_barrier(NULL);
121 return 0;
122 } else
123 return 1;
124
125 return 0;
126 }
127 early_param("spec_store_bypass_disable", handle_ssbd);
128
129 /* This is the generic flag used by other architectures */
130 static int __init handle_no_ssbd(char *p)
131 {
132 handle_no_stf_barrier(NULL);
133 return 0;
134 }
135 early_param("nospec_store_bypass_disable", handle_no_ssbd);
136
137 static void stf_barrier_enable(bool enable)
138 {
139 if (enable)
140 do_stf_barrier_fixups(stf_enabled_flush_types);
141 else
142 do_stf_barrier_fixups(STF_BARRIER_NONE);
143
144 stf_barrier = enable;
145 }
146
147 void setup_stf_barrier(void)
148 {
149 enum stf_barrier_type type;
150 bool enable, hv;
151
152 hv = cpu_has_feature(CPU_FTR_HVMODE);
153
154 /* Default to fallback in case fw-features are not available */
155 if (cpu_has_feature(CPU_FTR_ARCH_300))
156 type = STF_BARRIER_EIEIO;
157 else if (cpu_has_feature(CPU_FTR_ARCH_207S))
158 type = STF_BARRIER_SYNC_ORI;
159 else if (cpu_has_feature(CPU_FTR_ARCH_206))
160 type = STF_BARRIER_FALLBACK;
161 else
162 type = STF_BARRIER_NONE;
163
164 enable = security_ftr_enabled(SEC_FTR_FAVOUR_SECURITY) &&
165 (security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR) ||
166 (security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) && hv));
167
168 if (type == STF_BARRIER_FALLBACK) {
169 pr_info("stf-barrier: fallback barrier available\n");
170 } else if (type == STF_BARRIER_SYNC_ORI) {
171 pr_info("stf-barrier: hwsync barrier available\n");
172 } else if (type == STF_BARRIER_EIEIO) {
173 pr_info("stf-barrier: eieio barrier available\n");
174 }
175
176 stf_enabled_flush_types = type;
177
178 if (!no_stf_barrier)
179 stf_barrier_enable(enable);
180 }
181
182 ssize_t cpu_show_spec_store_bypass(struct device *dev, struct device_attribute *attr, char *buf)
183 {
184 if (stf_barrier && stf_enabled_flush_types != STF_BARRIER_NONE) {
185 const char *type;
186 switch (stf_enabled_flush_types) {
187 case STF_BARRIER_EIEIO:
188 type = "eieio";
189 break;
190 case STF_BARRIER_SYNC_ORI:
191 type = "hwsync";
192 break;
193 case STF_BARRIER_FALLBACK:
194 type = "fallback";
195 break;
196 default:
197 type = "unknown";
198 }
199 return sprintf(buf, "Mitigation: Kernel entry/exit barrier (%s)\n", type);
200 }
201
202 if (!security_ftr_enabled(SEC_FTR_L1D_FLUSH_HV) &&
203 !security_ftr_enabled(SEC_FTR_L1D_FLUSH_PR))
204 return sprintf(buf, "Not affected\n");
205
206 return sprintf(buf, "Vulnerable\n");
207 }
208
209 #ifdef CONFIG_DEBUG_FS
210 static int stf_barrier_set(void *data, u64 val)
211 {
212 bool enable;
213
214 if (val == 1)
215 enable = true;
216 else if (val == 0)
217 enable = false;
218 else
219 return -EINVAL;
220
221 /* Only do anything if we're changing state */
222 if (enable != stf_barrier)
223 stf_barrier_enable(enable);
224
225 return 0;
226 }
227
228 static int stf_barrier_get(void *data, u64 *val)
229 {
230 *val = stf_barrier ? 1 : 0;
231 return 0;
232 }
233
234 DEFINE_SIMPLE_ATTRIBUTE(fops_stf_barrier, stf_barrier_get, stf_barrier_set, "%llu\n");
235
236 static __init int stf_barrier_debugfs_init(void)
237 {
238 debugfs_create_file("stf_barrier", 0600, powerpc_debugfs_root, NULL, &fops_stf_barrier);
239 return 0;
240 }
241 device_initcall(stf_barrier_debugfs_init);
242 #endif /* CONFIG_DEBUG_FS */