Skip to content

Commit b1701fe

Browse files
pa1guptagregkh
authored andcommitted
x86/its: Enable Indirect Target Selection mitigation
commit f4818881c47fd91fcb6d62373c57c7844e3de1c0 upstream. Indirect Target Selection (ITS) is a bug in some pre-ADL Intel CPUs with eIBRS. It affects prediction of indirect branch and RETs in the lower half of cacheline. Due to ITS such branches may get wrongly predicted to a target of (direct or indirect) branch that is located in the upper half of the cacheline. Scope of impact =============== Guest/host isolation -------------------- When eIBRS is used for guest/host isolation, the indirect branches in the VMM may still be predicted with targets corresponding to branches in the guest. Intra-mode ---------- cBPF or other native gadgets can be used for intra-mode training and disclosure using ITS. User/kernel isolation --------------------- When eIBRS is enabled user/kernel isolation is not impacted. Indirect Branch Prediction Barrier (IBPB) ----------------------------------------- After an IBPB, indirect branches may be predicted with targets corresponding to direct branches which were executed prior to IBPB. This is mitigated by a microcode update. Add cmdline parameter indirect_target_selection=off|on|force to control the mitigation to relocate the affected branches to an ITS-safe thunk i.e. located in the upper half of cacheline. Also add the sysfs reporting. When retpoline mitigation is deployed, ITS safe-thunks are not needed, because retpoline sequence is already ITS-safe. Similarly, when call depth tracking (CDT) mitigation is deployed (retbleed=stuff), ITS safe return thunk is not used, as CDT prevents RSB-underflow. To not overcomplicate things, ITS mitigation is not supported with spectre-v2 lfence;jmp mitigation. Moreover, it is less practical to deploy lfence;jmp mitigation on ITS affected parts anyways. Signed-off-by: Pawan Gupta <[email protected]> Signed-off-by: Dave Hansen <[email protected]> Reviewed-by: Josh Poimboeuf <[email protected]> Reviewed-by: Alexandre Chartre <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent dbd8f17 commit b1701fe

File tree

5 files changed

+149
-3
lines changed

5 files changed

+149
-3
lines changed

Documentation/ABI/testing/sysfs-devices-system-cpu

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -514,6 +514,7 @@ Description: information about CPUs heterogeneity.
514514

515515
What: /sys/devices/system/cpu/vulnerabilities
516516
/sys/devices/system/cpu/vulnerabilities/gather_data_sampling
517+
/sys/devices/system/cpu/vulnerabilities/indirect_target_selection
517518
/sys/devices/system/cpu/vulnerabilities/itlb_multihit
518519
/sys/devices/system/cpu/vulnerabilities/l1tf
519520
/sys/devices/system/cpu/vulnerabilities/mds

Documentation/admin-guide/kernel-parameters.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2025,6 +2025,18 @@
20252025
different crypto accelerators. This option can be used
20262026
to achieve best performance for particular HW.
20272027

2028+
indirect_target_selection= [X86,Intel] Mitigation control for Indirect
2029+
Target Selection(ITS) bug in Intel CPUs. Updated
2030+
microcode is also required for a fix in IBPB.
2031+
2032+
on: Enable mitigation (default).
2033+
off: Disable mitigation.
2034+
force: Force the ITS bug and deploy default
2035+
mitigation.
2036+
2037+
For details see:
2038+
Documentation/admin-guide/hw-vuln/indirect-target-selection.rst
2039+
20282040
init= [KNL]
20292041
Format: <full_path>
20302042
Run specified binary instead of /sbin/init as init
@@ -3263,6 +3275,7 @@
32633275
expose users to several CPU vulnerabilities.
32643276
Equivalent to: if nokaslr then kpti=0 [ARM64]
32653277
gather_data_sampling=off [X86]
3278+
indirect_target_selection=off [X86]
32663279
kvm.nx_huge_pages=off [X86]
32673280
l1tf=off [X86]
32683281
mds=off [X86]

arch/x86/kernel/cpu/bugs.c

Lines changed: 125 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ static void __init srbds_select_mitigation(void);
4848
static void __init l1d_flush_select_mitigation(void);
4949
static void __init gds_select_mitigation(void);
5050
static void __init srso_select_mitigation(void);
51+
static void __init its_select_mitigation(void);
5152

5253
/* The base value of the SPEC_CTRL MSR without task-specific bits set */
5354
u64 x86_spec_ctrl_base;
@@ -66,6 +67,14 @@ static DEFINE_MUTEX(spec_ctrl_mutex);
6667

6768
void (*x86_return_thunk)(void) __ro_after_init = &__x86_return_thunk;
6869

70+
static void __init set_return_thunk(void *thunk)
71+
{
72+
if (x86_return_thunk != __x86_return_thunk)
73+
pr_warn("x86/bugs: return thunk changed\n");
74+
75+
x86_return_thunk = thunk;
76+
}
77+
6978
/* Update SPEC_CTRL MSR and its cached copy unconditionally */
7079
static void update_spec_ctrl(u64 val)
7180
{
@@ -174,6 +183,7 @@ void __init cpu_select_mitigations(void)
174183
*/
175184
srso_select_mitigation();
176185
gds_select_mitigation();
186+
its_select_mitigation();
177187
}
178188

179189
/*
@@ -1081,7 +1091,7 @@ static void __init retbleed_select_mitigation(void)
10811091
setup_force_cpu_cap(X86_FEATURE_UNRET);
10821092

10831093
if (IS_ENABLED(CONFIG_RETHUNK))
1084-
x86_return_thunk = retbleed_return_thunk;
1094+
set_return_thunk(retbleed_return_thunk);
10851095

10861096
if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
10871097
boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
@@ -1142,6 +1152,105 @@ static void __init retbleed_select_mitigation(void)
11421152
pr_info("%s\n", retbleed_strings[retbleed_mitigation]);
11431153
}
11441154

1155+
#undef pr_fmt
1156+
#define pr_fmt(fmt) "ITS: " fmt
1157+
1158+
enum its_mitigation_cmd {
1159+
ITS_CMD_OFF,
1160+
ITS_CMD_ON,
1161+
};
1162+
1163+
enum its_mitigation {
1164+
ITS_MITIGATION_OFF,
1165+
ITS_MITIGATION_ALIGNED_THUNKS,
1166+
};
1167+
1168+
static const char * const its_strings[] = {
1169+
[ITS_MITIGATION_OFF] = "Vulnerable",
1170+
[ITS_MITIGATION_ALIGNED_THUNKS] = "Mitigation: Aligned branch/return thunks",
1171+
};
1172+
1173+
static enum its_mitigation its_mitigation __ro_after_init = ITS_MITIGATION_ALIGNED_THUNKS;
1174+
1175+
static enum its_mitigation_cmd its_cmd __ro_after_init =
1176+
IS_ENABLED(CONFIG_MITIGATION_ITS) ? ITS_CMD_ON : ITS_CMD_OFF;
1177+
1178+
static int __init its_parse_cmdline(char *str)
1179+
{
1180+
if (!str)
1181+
return -EINVAL;
1182+
1183+
if (!IS_ENABLED(CONFIG_MITIGATION_ITS)) {
1184+
pr_err("Mitigation disabled at compile time, ignoring option (%s)", str);
1185+
return 0;
1186+
}
1187+
1188+
if (!strcmp(str, "off")) {
1189+
its_cmd = ITS_CMD_OFF;
1190+
} else if (!strcmp(str, "on")) {
1191+
its_cmd = ITS_CMD_ON;
1192+
} else if (!strcmp(str, "force")) {
1193+
its_cmd = ITS_CMD_ON;
1194+
setup_force_cpu_bug(X86_BUG_ITS);
1195+
} else {
1196+
pr_err("Ignoring unknown indirect_target_selection option (%s).", str);
1197+
}
1198+
1199+
return 0;
1200+
}
1201+
early_param("indirect_target_selection", its_parse_cmdline);
1202+
1203+
static void __init its_select_mitigation(void)
1204+
{
1205+
enum its_mitigation_cmd cmd = its_cmd;
1206+
1207+
if (!boot_cpu_has_bug(X86_BUG_ITS) || cpu_mitigations_off()) {
1208+
its_mitigation = ITS_MITIGATION_OFF;
1209+
return;
1210+
}
1211+
1212+
/* Exit early to avoid irrelevant warnings */
1213+
if (cmd == ITS_CMD_OFF) {
1214+
its_mitigation = ITS_MITIGATION_OFF;
1215+
goto out;
1216+
}
1217+
if (spectre_v2_enabled == SPECTRE_V2_NONE) {
1218+
pr_err("WARNING: Spectre-v2 mitigation is off, disabling ITS\n");
1219+
its_mitigation = ITS_MITIGATION_OFF;
1220+
goto out;
1221+
}
1222+
if (!IS_ENABLED(CONFIG_RETPOLINE) || !IS_ENABLED(CONFIG_RETHUNK)) {
1223+
pr_err("WARNING: ITS mitigation depends on retpoline and rethunk support\n");
1224+
its_mitigation = ITS_MITIGATION_OFF;
1225+
goto out;
1226+
}
1227+
if (IS_ENABLED(CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B)) {
1228+
pr_err("WARNING: ITS mitigation is not compatible with CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B\n");
1229+
its_mitigation = ITS_MITIGATION_OFF;
1230+
goto out;
1231+
}
1232+
if (boot_cpu_has(X86_FEATURE_RETPOLINE_LFENCE)) {
1233+
pr_err("WARNING: ITS mitigation is not compatible with lfence mitigation\n");
1234+
its_mitigation = ITS_MITIGATION_OFF;
1235+
goto out;
1236+
}
1237+
1238+
switch (cmd) {
1239+
case ITS_CMD_OFF:
1240+
its_mitigation = ITS_MITIGATION_OFF;
1241+
break;
1242+
case ITS_CMD_ON:
1243+
its_mitigation = ITS_MITIGATION_ALIGNED_THUNKS;
1244+
if (!boot_cpu_has(X86_FEATURE_RETPOLINE))
1245+
setup_force_cpu_cap(X86_FEATURE_INDIRECT_THUNK_ITS);
1246+
setup_force_cpu_cap(X86_FEATURE_RETHUNK);
1247+
set_return_thunk(its_return_thunk);
1248+
break;
1249+
}
1250+
out:
1251+
pr_info("%s\n", its_strings[its_mitigation]);
1252+
}
1253+
11451254
#undef pr_fmt
11461255
#define pr_fmt(fmt) "Spectre V2 : " fmt
11471256

@@ -2592,10 +2701,10 @@ static void __init srso_select_mitigation(void)
25922701

25932702
if (boot_cpu_data.x86 == 0x19) {
25942703
setup_force_cpu_cap(X86_FEATURE_SRSO_ALIAS);
2595-
x86_return_thunk = srso_alias_return_thunk;
2704+
set_return_thunk(srso_alias_return_thunk);
25962705
} else {
25972706
setup_force_cpu_cap(X86_FEATURE_SRSO);
2598-
x86_return_thunk = srso_return_thunk;
2707+
set_return_thunk(srso_return_thunk);
25992708
}
26002709
srso_mitigation = SRSO_MITIGATION_SAFE_RET;
26012710
} else {
@@ -2775,6 +2884,11 @@ static ssize_t rfds_show_state(char *buf)
27752884
return sysfs_emit(buf, "%s\n", rfds_strings[rfds_mitigation]);
27762885
}
27772886

2887+
static ssize_t its_show_state(char *buf)
2888+
{
2889+
return sysfs_emit(buf, "%s\n", its_strings[its_mitigation]);
2890+
}
2891+
27782892
static char *stibp_state(void)
27792893
{
27802894
if (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
@@ -2959,6 +3073,9 @@ static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr
29593073
case X86_BUG_RFDS:
29603074
return rfds_show_state(buf);
29613075

3076+
case X86_BUG_ITS:
3077+
return its_show_state(buf);
3078+
29623079
default:
29633080
break;
29643081
}
@@ -3038,4 +3155,9 @@ ssize_t cpu_show_reg_file_data_sampling(struct device *dev, struct device_attrib
30383155
{
30393156
return cpu_show_common(dev, attr, buf, X86_BUG_RFDS);
30403157
}
3158+
3159+
ssize_t cpu_show_indirect_target_selection(struct device *dev, struct device_attribute *attr, char *buf)
3160+
{
3161+
return cpu_show_common(dev, attr, buf, X86_BUG_ITS);
3162+
}
30413163
#endif

drivers/base/cpu.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -595,6 +595,12 @@ ssize_t __weak cpu_show_reg_file_data_sampling(struct device *dev,
595595
return sysfs_emit(buf, "Not affected\n");
596596
}
597597

598+
ssize_t __weak cpu_show_indirect_target_selection(struct device *dev,
599+
struct device_attribute *attr, char *buf)
600+
{
601+
return sysfs_emit(buf, "Not affected\n");
602+
}
603+
598604
static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL);
599605
static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL);
600606
static DEVICE_ATTR(spectre_v2, 0444, cpu_show_spectre_v2, NULL);
@@ -609,6 +615,7 @@ static DEVICE_ATTR(retbleed, 0444, cpu_show_retbleed, NULL);
609615
static DEVICE_ATTR(gather_data_sampling, 0444, cpu_show_gds, NULL);
610616
static DEVICE_ATTR(spec_rstack_overflow, 0444, cpu_show_spec_rstack_overflow, NULL);
611617
static DEVICE_ATTR(reg_file_data_sampling, 0444, cpu_show_reg_file_data_sampling, NULL);
618+
static DEVICE_ATTR(indirect_target_selection, 0444, cpu_show_indirect_target_selection, NULL);
612619

613620
static struct attribute *cpu_root_vulnerabilities_attrs[] = {
614621
&dev_attr_meltdown.attr,
@@ -625,6 +632,7 @@ static struct attribute *cpu_root_vulnerabilities_attrs[] = {
625632
&dev_attr_gather_data_sampling.attr,
626633
&dev_attr_spec_rstack_overflow.attr,
627634
&dev_attr_reg_file_data_sampling.attr,
635+
&dev_attr_indirect_target_selection.attr,
628636
NULL
629637
};
630638

include/linux/cpu.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,8 @@ extern ssize_t cpu_show_gds(struct device *dev,
7676
struct device_attribute *attr, char *buf);
7777
extern ssize_t cpu_show_reg_file_data_sampling(struct device *dev,
7878
struct device_attribute *attr, char *buf);
79+
extern ssize_t cpu_show_indirect_target_selection(struct device *dev,
80+
struct device_attribute *attr, char *buf);
7981

8082
extern __printf(4, 5)
8183
struct device *cpu_device_create(struct device *parent, void *drvdata,

0 commit comments

Comments
 (0)