security.c 7.3 KB

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