Skip to content

Commit df8a74f

Browse files
janeyasmingregkh
authored andcommitted
x86/cpu: Fix migration safety with X86_BUG_NULL_SEL
commit 415de44 upstream. Currently, Linux probes for X86_BUG_NULL_SEL unconditionally which makes it unsafe to migrate in a virtualised environment as the properties across the migration pool might differ. To be specific, the case which goes wrong is: 1. Zen1 (or earlier) and Zen2 (or later) in a migration pool 2. Linux boots on Zen2, probes and finds the absence of X86_BUG_NULL_SEL 3. Linux is then migrated to Zen1 Linux is now running on a X86_BUG_NULL_SEL-impacted CPU while believing that the bug is fixed. The only way to address the problem is to fully trust the "no longer affected" CPUID bit when virtualised, because in the above case it would be clear deliberately to indicate the fact "you might migrate to somewhere which has this behaviour". Zen3 adds the NullSelectorClearsBase CPUID bit to indicate that loading a NULL segment selector zeroes the base and limit fields, as well as just attributes. Zen2 also has this behaviour but doesn't have the NSCB bit. [ bp: Minor touchups. ] Signed-off-by: Jane Malalane <jane.malalane@citrix.com> Signed-off-by: Borislav Petkov <bp@suse.de> CC: <stable@vger.kernel.org> Link: https://lkml.kernel.org/r/20211021104744.24126-1-jane.malalane@citrix.com Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 115810a commit df8a74f

4 files changed

Lines changed: 42 additions & 7 deletions

File tree

‎arch/x86/kernel/cpu/amd.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1017,6 +1017,8 @@ static void init_amd(struct cpuinfo_x86 *c)
10171017
if (cpu_has(c, X86_FEATURE_IRPERF) &&
10181018
!cpu_has_amd_erratum(c, amd_erratum_1054))
10191019
msr_set_bit(MSR_K7_HWCR, MSR_K7_HWCR_IRPERF_EN_BIT);
1020+
1021+
check_null_seg_clears_base(c);
10201022
}
10211023

10221024
#ifdef CONFIG_X86_32

‎arch/x86/kernel/cpu/common.c‎

Lines changed: 37 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1391,9 +1391,8 @@ void __init early_cpu_init(void)
13911391
early_identify_cpu(&boot_cpu_data);
13921392
}
13931393

1394-
static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
1394+
static bool detect_null_seg_behavior(void)
13951395
{
1396-
#ifdef CONFIG_X86_64
13971396
/*
13981397
* Empirically, writing zero to a segment selector on AMD does
13991398
* not clear the base, whereas writing zero to a segment
@@ -1414,10 +1413,43 @@ static void detect_null_seg_behavior(struct cpuinfo_x86 *c)
14141413
wrmsrl(MSR_FS_BASE, 1);
14151414
loadsegment(fs, 0);
14161415
rdmsrl(MSR_FS_BASE, tmp);
1417-
if (tmp != 0)
1418-
set_cpu_bug(c, X86_BUG_NULL_SEG);
14191416
wrmsrl(MSR_FS_BASE, old_base);
1420-
#endif
1417+
return tmp == 0;
1418+
}
1419+
1420+
void check_null_seg_clears_base(struct cpuinfo_x86 *c)
1421+
{
1422+
/* BUG_NULL_SEG is only relevant with 64bit userspace */
1423+
if (!IS_ENABLED(CONFIG_X86_64))
1424+
return;
1425+
1426+
/* Zen3 CPUs advertise Null Selector Clears Base in CPUID. */
1427+
if (c->extended_cpuid_level >= 0x80000021 &&
1428+
cpuid_eax(0x80000021) & BIT(6))
1429+
return;
1430+
1431+
/*
1432+
* CPUID bit above wasn't set. If this kernel is still running
1433+
* as a HV guest, then the HV has decided not to advertize
1434+
* that CPUID bit for whatever reason. For example, one
1435+
* member of the migration pool might be vulnerable. Which
1436+
* means, the bug is present: set the BUG flag and return.
1437+
*/
1438+
if (cpu_has(c, X86_FEATURE_HYPERVISOR)) {
1439+
set_cpu_bug(c, X86_BUG_NULL_SEG);
1440+
return;
1441+
}
1442+
1443+
/*
1444+
* Zen2 CPUs also have this behaviour, but no CPUID bit.
1445+
* 0x18 is the respective family for Hygon.
1446+
*/
1447+
if ((c->x86 == 0x17 || c->x86 == 0x18) &&
1448+
detect_null_seg_behavior())
1449+
return;
1450+
1451+
/* All the remaining ones are affected */
1452+
set_cpu_bug(c, X86_BUG_NULL_SEG);
14211453
}
14221454

14231455
static void generic_identify(struct cpuinfo_x86 *c)
@@ -1453,8 +1485,6 @@ static void generic_identify(struct cpuinfo_x86 *c)
14531485

14541486
get_model_name(c); /* Default name */
14551487

1456-
detect_null_seg_behavior(c);
1457-
14581488
/*
14591489
* ESPFIX is a strange bug. All real CPUs have it. Paravirt
14601490
* systems that run Linux at CPL > 0 may or may not have the

‎arch/x86/kernel/cpu/cpu.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ extern int detect_extended_topology_early(struct cpuinfo_x86 *c);
7373
extern int detect_extended_topology(struct cpuinfo_x86 *c);
7474
extern int detect_ht_early(struct cpuinfo_x86 *c);
7575
extern void detect_ht(struct cpuinfo_x86 *c);
76+
extern void check_null_seg_clears_base(struct cpuinfo_x86 *c);
7677

7778
unsigned int aperfmperf_get_khz(int cpu);
7879

‎arch/x86/kernel/cpu/hygon.c‎

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -351,6 +351,8 @@ static void init_hygon(struct cpuinfo_x86 *c)
351351
/* Hygon CPUs don't reset SS attributes on SYSRET, Xen does. */
352352
if (!cpu_has(c, X86_FEATURE_XENPV))
353353
set_cpu_bug(c, X86_BUG_SYSRET_SS_ATTRS);
354+
355+
check_null_seg_clears_base(c);
354356
}
355357

356358
static void cpu_detect_tlb_hygon(struct cpuinfo_x86 *c)

0 commit comments

Comments
 (0)