Skip to content

Commit 68d59e9

Browse files
pa1guptagregkh
authored andcommitted
x86/its: Enable Indirect Target Selection mitigation
commit f481888 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 5100004 commit 68d59e9

File tree

5 files changed

+155
-4
lines changed

5 files changed

+155
-4
lines changed

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

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

512512
What: /sys/devices/system/cpu/vulnerabilities
513513
/sys/devices/system/cpu/vulnerabilities/gather_data_sampling
514+
/sys/devices/system/cpu/vulnerabilities/indirect_target_selection
514515
/sys/devices/system/cpu/vulnerabilities/itlb_multihit
515516
/sys/devices/system/cpu/vulnerabilities/l1tf
516517
/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
@@ -2149,6 +2149,18 @@
21492149
different crypto accelerators. This option can be used
21502150
to achieve best performance for particular HW.
21512151

2152+
indirect_target_selection= [X86,Intel] Mitigation control for Indirect
2153+
Target Selection(ITS) bug in Intel CPUs. Updated
2154+
microcode is also required for a fix in IBPB.
2155+
2156+
on: Enable mitigation (default).
2157+
off: Disable mitigation.
2158+
force: Force the ITS bug and deploy default
2159+
mitigation.
2160+
2161+
For details see:
2162+
Documentation/admin-guide/hw-vuln/indirect-target-selection.rst
2163+
21522164
init= [KNL]
21532165
Format: <full_path>
21542166
Run specified binary instead of /sbin/init as init
@@ -3510,6 +3522,7 @@
35103522
expose users to several CPU vulnerabilities.
35113523
Equivalent to: if nokaslr then kpti=0 [ARM64]
35123524
gather_data_sampling=off [X86]
3525+
indirect_target_selection=off [X86]
35133526
kvm.nx_huge_pages=off [X86]
35143527
l1tf=off [X86]
35153528
mds=off [X86]

arch/x86/kernel/cpu/bugs.c

Lines changed: 136 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ static void __init srbds_select_mitigation(void);
4949
static void __init l1d_flush_select_mitigation(void);
5050
static void __init srso_select_mitigation(void);
5151
static void __init gds_select_mitigation(void);
52+
static void __init its_select_mitigation(void);
5253

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

6869
void (*x86_return_thunk)(void) __ro_after_init = __x86_return_thunk;
6970

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

180190
/*
@@ -1104,7 +1114,7 @@ static void __init retbleed_select_mitigation(void)
11041114
setup_force_cpu_cap(X86_FEATURE_RETHUNK);
11051115
setup_force_cpu_cap(X86_FEATURE_UNRET);
11061116

1107-
x86_return_thunk = retbleed_return_thunk;
1117+
set_return_thunk(retbleed_return_thunk);
11081118

11091119
if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
11101120
boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
@@ -1139,7 +1149,7 @@ static void __init retbleed_select_mitigation(void)
11391149
setup_force_cpu_cap(X86_FEATURE_RETHUNK);
11401150
setup_force_cpu_cap(X86_FEATURE_CALL_DEPTH);
11411151

1142-
x86_return_thunk = call_depth_return_thunk;
1152+
set_return_thunk(call_depth_return_thunk);
11431153
break;
11441154

11451155
default:
@@ -1173,6 +1183,115 @@ static void __init retbleed_select_mitigation(void)
11731183
pr_info("%s\n", retbleed_strings[retbleed_mitigation]);
11741184
}
11751185

1186+
#undef pr_fmt
1187+
#define pr_fmt(fmt) "ITS: " fmt
1188+
1189+
enum its_mitigation_cmd {
1190+
ITS_CMD_OFF,
1191+
ITS_CMD_ON,
1192+
};
1193+
1194+
enum its_mitigation {
1195+
ITS_MITIGATION_OFF,
1196+
ITS_MITIGATION_ALIGNED_THUNKS,
1197+
ITS_MITIGATION_RETPOLINE_STUFF,
1198+
};
1199+
1200+
static const char * const its_strings[] = {
1201+
[ITS_MITIGATION_OFF] = "Vulnerable",
1202+
[ITS_MITIGATION_ALIGNED_THUNKS] = "Mitigation: Aligned branch/return thunks",
1203+
[ITS_MITIGATION_RETPOLINE_STUFF] = "Mitigation: Retpolines, Stuffing RSB",
1204+
};
1205+
1206+
static enum its_mitigation its_mitigation __ro_after_init = ITS_MITIGATION_ALIGNED_THUNKS;
1207+
1208+
static enum its_mitigation_cmd its_cmd __ro_after_init =
1209+
IS_ENABLED(CONFIG_MITIGATION_ITS) ? ITS_CMD_ON : ITS_CMD_OFF;
1210+
1211+
static int __init its_parse_cmdline(char *str)
1212+
{
1213+
if (!str)
1214+
return -EINVAL;
1215+
1216+
if (!IS_ENABLED(CONFIG_MITIGATION_ITS)) {
1217+
pr_err("Mitigation disabled at compile time, ignoring option (%s)", str);
1218+
return 0;
1219+
}
1220+
1221+
if (!strcmp(str, "off")) {
1222+
its_cmd = ITS_CMD_OFF;
1223+
} else if (!strcmp(str, "on")) {
1224+
its_cmd = ITS_CMD_ON;
1225+
} else if (!strcmp(str, "force")) {
1226+
its_cmd = ITS_CMD_ON;
1227+
setup_force_cpu_bug(X86_BUG_ITS);
1228+
} else {
1229+
pr_err("Ignoring unknown indirect_target_selection option (%s).", str);
1230+
}
1231+
1232+
return 0;
1233+
}
1234+
early_param("indirect_target_selection", its_parse_cmdline);
1235+
1236+
static void __init its_select_mitigation(void)
1237+
{
1238+
enum its_mitigation_cmd cmd = its_cmd;
1239+
1240+
if (!boot_cpu_has_bug(X86_BUG_ITS) || cpu_mitigations_off()) {
1241+
its_mitigation = ITS_MITIGATION_OFF;
1242+
return;
1243+
}
1244+
1245+
/* Retpoline+CDT mitigates ITS, bail out */
1246+
if (boot_cpu_has(X86_FEATURE_RETPOLINE) &&
1247+
boot_cpu_has(X86_FEATURE_CALL_DEPTH)) {
1248+
its_mitigation = ITS_MITIGATION_RETPOLINE_STUFF;
1249+
goto out;
1250+
}
1251+
1252+
/* Exit early to avoid irrelevant warnings */
1253+
if (cmd == ITS_CMD_OFF) {
1254+
its_mitigation = ITS_MITIGATION_OFF;
1255+
goto out;
1256+
}
1257+
if (spectre_v2_enabled == SPECTRE_V2_NONE) {
1258+
pr_err("WARNING: Spectre-v2 mitigation is off, disabling ITS\n");
1259+
its_mitigation = ITS_MITIGATION_OFF;
1260+
goto out;
1261+
}
1262+
if (!IS_ENABLED(CONFIG_MITIGATION_RETPOLINE) ||
1263+
!IS_ENABLED(CONFIG_MITIGATION_RETHUNK)) {
1264+
pr_err("WARNING: ITS mitigation depends on retpoline and rethunk support\n");
1265+
its_mitigation = ITS_MITIGATION_OFF;
1266+
goto out;
1267+
}
1268+
if (IS_ENABLED(CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B)) {
1269+
pr_err("WARNING: ITS mitigation is not compatible with CONFIG_DEBUG_FORCE_FUNCTION_ALIGN_64B\n");
1270+
its_mitigation = ITS_MITIGATION_OFF;
1271+
goto out;
1272+
}
1273+
if (boot_cpu_has(X86_FEATURE_RETPOLINE_LFENCE)) {
1274+
pr_err("WARNING: ITS mitigation is not compatible with lfence mitigation\n");
1275+
its_mitigation = ITS_MITIGATION_OFF;
1276+
goto out;
1277+
}
1278+
1279+
switch (cmd) {
1280+
case ITS_CMD_OFF:
1281+
its_mitigation = ITS_MITIGATION_OFF;
1282+
break;
1283+
case ITS_CMD_ON:
1284+
its_mitigation = ITS_MITIGATION_ALIGNED_THUNKS;
1285+
if (!boot_cpu_has(X86_FEATURE_RETPOLINE))
1286+
setup_force_cpu_cap(X86_FEATURE_INDIRECT_THUNK_ITS);
1287+
setup_force_cpu_cap(X86_FEATURE_RETHUNK);
1288+
set_return_thunk(its_return_thunk);
1289+
break;
1290+
}
1291+
out:
1292+
pr_info("%s\n", its_strings[its_mitigation]);
1293+
}
1294+
11761295
#undef pr_fmt
11771296
#define pr_fmt(fmt) "Spectre V2 : " fmt
11781297

@@ -2624,10 +2743,10 @@ static void __init srso_select_mitigation(void)
26242743

26252744
if (boot_cpu_data.x86 == 0x19) {
26262745
setup_force_cpu_cap(X86_FEATURE_SRSO_ALIAS);
2627-
x86_return_thunk = srso_alias_return_thunk;
2746+
set_return_thunk(srso_alias_return_thunk);
26282747
} else {
26292748
setup_force_cpu_cap(X86_FEATURE_SRSO);
2630-
x86_return_thunk = srso_return_thunk;
2749+
set_return_thunk(srso_return_thunk);
26312750
}
26322751
if (has_microcode)
26332752
srso_mitigation = SRSO_MITIGATION_SAFE_RET;
@@ -2802,6 +2921,11 @@ static ssize_t rfds_show_state(char *buf)
28022921
return sysfs_emit(buf, "%s\n", rfds_strings[rfds_mitigation]);
28032922
}
28042923

2924+
static ssize_t its_show_state(char *buf)
2925+
{
2926+
return sysfs_emit(buf, "%s\n", its_strings[its_mitigation]);
2927+
}
2928+
28052929
static char *stibp_state(void)
28062930
{
28072931
if (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
@@ -2984,6 +3108,9 @@ static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr
29843108
case X86_BUG_RFDS:
29853109
return rfds_show_state(buf);
29863110

3111+
case X86_BUG_ITS:
3112+
return its_show_state(buf);
3113+
29873114
default:
29883115
break;
29893116
}
@@ -3063,6 +3190,11 @@ ssize_t cpu_show_reg_file_data_sampling(struct device *dev, struct device_attrib
30633190
{
30643191
return cpu_show_common(dev, attr, buf, X86_BUG_RFDS);
30653192
}
3193+
3194+
ssize_t cpu_show_indirect_target_selection(struct device *dev, struct device_attribute *attr, char *buf)
3195+
{
3196+
return cpu_show_common(dev, attr, buf, X86_BUG_ITS);
3197+
}
30663198
#endif
30673199

30683200
void __warn_thunk(void)

drivers/base/cpu.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -599,6 +599,7 @@ CPU_SHOW_VULN_FALLBACK(retbleed);
599599
CPU_SHOW_VULN_FALLBACK(spec_rstack_overflow);
600600
CPU_SHOW_VULN_FALLBACK(gds);
601601
CPU_SHOW_VULN_FALLBACK(reg_file_data_sampling);
602+
CPU_SHOW_VULN_FALLBACK(indirect_target_selection);
602603

603604
static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL);
604605
static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL);
@@ -614,6 +615,7 @@ static DEVICE_ATTR(retbleed, 0444, cpu_show_retbleed, NULL);
614615
static DEVICE_ATTR(spec_rstack_overflow, 0444, cpu_show_spec_rstack_overflow, NULL);
615616
static DEVICE_ATTR(gather_data_sampling, 0444, cpu_show_gds, NULL);
616617
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);
617619

618620
static struct attribute *cpu_root_vulnerabilities_attrs[] = {
619621
&dev_attr_meltdown.attr,
@@ -630,6 +632,7 @@ static struct attribute *cpu_root_vulnerabilities_attrs[] = {
630632
&dev_attr_spec_rstack_overflow.attr,
631633
&dev_attr_gather_data_sampling.attr,
632634
&dev_attr_reg_file_data_sampling.attr,
635+
&dev_attr_indirect_target_selection.attr,
633636
NULL
634637
};
635638

include/linux/cpu.h

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

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

0 commit comments

Comments
 (0)