Skip to content

Commit f481888

Browse files
pa1guptahansendc
authored andcommitted
x86/its: Enable Indirect Target Selection mitigation
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]>
1 parent a75bf27 commit f481888

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
@@ -2202,6 +2202,18 @@
22022202
different crypto accelerators. This option can be used
22032203
to achieve best performance for particular HW.
22042204

2205+
indirect_target_selection= [X86,Intel] Mitigation control for Indirect
2206+
Target Selection(ITS) bug in Intel CPUs. Updated
2207+
microcode is also required for a fix in IBPB.
2208+
2209+
on: Enable mitigation (default).
2210+
off: Disable mitigation.
2211+
force: Force the ITS bug and deploy default
2212+
mitigation.
2213+
2214+
For details see:
2215+
Documentation/admin-guide/hw-vuln/indirect-target-selection.rst
2216+
22052217
init= [KNL]
22062218
Format: <full_path>
22072219
Run specified binary instead of /sbin/init as init
@@ -3693,6 +3705,7 @@
36933705
expose users to several CPU vulnerabilities.
36943706
Equivalent to: if nokaslr then kpti=0 [ARM64]
36953707
gather_data_sampling=off [X86]
3708+
indirect_target_selection=off [X86]
36963709
kvm.nx_huge_pages=off [X86]
36973710
l1tf=off [X86]
36983711
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;
@@ -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
{
@@ -178,6 +187,7 @@ void __init cpu_select_mitigations(void)
178187
*/
179188
srso_select_mitigation();
180189
gds_select_mitigation();
190+
its_select_mitigation();
181191
}
182192

183193
/*
@@ -1118,7 +1128,7 @@ static void __init retbleed_select_mitigation(void)
11181128
setup_force_cpu_cap(X86_FEATURE_RETHUNK);
11191129
setup_force_cpu_cap(X86_FEATURE_UNRET);
11201130

1121-
x86_return_thunk = retbleed_return_thunk;
1131+
set_return_thunk(retbleed_return_thunk);
11221132

11231133
if (boot_cpu_data.x86_vendor != X86_VENDOR_AMD &&
11241134
boot_cpu_data.x86_vendor != X86_VENDOR_HYGON)
@@ -1153,7 +1163,7 @@ static void __init retbleed_select_mitigation(void)
11531163
setup_force_cpu_cap(X86_FEATURE_RETHUNK);
11541164
setup_force_cpu_cap(X86_FEATURE_CALL_DEPTH);
11551165

1156-
x86_return_thunk = call_depth_return_thunk;
1166+
set_return_thunk(call_depth_return_thunk);
11571167
break;
11581168

11591169
default:
@@ -1187,6 +1197,115 @@ static void __init retbleed_select_mitigation(void)
11871197
pr_info("%s\n", retbleed_strings[retbleed_mitigation]);
11881198
}
11891199

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

@@ -2607,10 +2726,10 @@ static void __init srso_select_mitigation(void)
26072726

26082727
if (boot_cpu_data.x86 == 0x19) {
26092728
setup_force_cpu_cap(X86_FEATURE_SRSO_ALIAS);
2610-
x86_return_thunk = srso_alias_return_thunk;
2729+
set_return_thunk(srso_alias_return_thunk);
26112730
} else {
26122731
setup_force_cpu_cap(X86_FEATURE_SRSO);
2613-
x86_return_thunk = srso_return_thunk;
2732+
set_return_thunk(srso_return_thunk);
26142733
}
26152734
if (has_microcode)
26162735
srso_mitigation = SRSO_MITIGATION_SAFE_RET;
@@ -2800,6 +2919,11 @@ static ssize_t rfds_show_state(char *buf)
28002919
return sysfs_emit(buf, "%s\n", rfds_strings[rfds_mitigation]);
28012920
}
28022921

2922+
static ssize_t its_show_state(char *buf)
2923+
{
2924+
return sysfs_emit(buf, "%s\n", its_strings[its_mitigation]);
2925+
}
2926+
28032927
static char *stibp_state(void)
28042928
{
28052929
if (spectre_v2_in_eibrs_mode(spectre_v2_enabled) &&
@@ -2982,6 +3106,9 @@ static ssize_t cpu_show_common(struct device *dev, struct device_attribute *attr
29823106
case X86_BUG_RFDS:
29833107
return rfds_show_state(buf);
29843108

3109+
case X86_BUG_ITS:
3110+
return its_show_state(buf);
3111+
29853112
default:
29863113
break;
29873114
}
@@ -3061,6 +3188,11 @@ ssize_t cpu_show_reg_file_data_sampling(struct device *dev, struct device_attrib
30613188
{
30623189
return cpu_show_common(dev, attr, buf, X86_BUG_RFDS);
30633190
}
3191+
3192+
ssize_t cpu_show_indirect_target_selection(struct device *dev, struct device_attribute *attr, char *buf)
3193+
{
3194+
return cpu_show_common(dev, attr, buf, X86_BUG_ITS);
3195+
}
30643196
#endif
30653197

30663198
void __warn_thunk(void)

drivers/base/cpu.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -600,6 +600,7 @@ CPU_SHOW_VULN_FALLBACK(spec_rstack_overflow);
600600
CPU_SHOW_VULN_FALLBACK(gds);
601601
CPU_SHOW_VULN_FALLBACK(reg_file_data_sampling);
602602
CPU_SHOW_VULN_FALLBACK(ghostwrite);
603+
CPU_SHOW_VULN_FALLBACK(indirect_target_selection);
603604

604605
static DEVICE_ATTR(meltdown, 0444, cpu_show_meltdown, NULL);
605606
static DEVICE_ATTR(spectre_v1, 0444, cpu_show_spectre_v1, NULL);
@@ -616,6 +617,7 @@ static DEVICE_ATTR(spec_rstack_overflow, 0444, cpu_show_spec_rstack_overflow, NU
616617
static DEVICE_ATTR(gather_data_sampling, 0444, cpu_show_gds, NULL);
617618
static DEVICE_ATTR(reg_file_data_sampling, 0444, cpu_show_reg_file_data_sampling, NULL);
618619
static DEVICE_ATTR(ghostwrite, 0444, cpu_show_ghostwrite, NULL);
620+
static DEVICE_ATTR(indirect_target_selection, 0444, cpu_show_indirect_target_selection, NULL);
619621

620622
static struct attribute *cpu_root_vulnerabilities_attrs[] = {
621623
&dev_attr_meltdown.attr,
@@ -633,6 +635,7 @@ static struct attribute *cpu_root_vulnerabilities_attrs[] = {
633635
&dev_attr_gather_data_sampling.attr,
634636
&dev_attr_reg_file_data_sampling.attr,
635637
&dev_attr_ghostwrite.attr,
638+
&dev_attr_indirect_target_selection.attr,
636639
NULL
637640
};
638641

include/linux/cpu.h

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

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

0 commit comments

Comments
 (0)