]> xenbits.xensource.com Git - people/aperard/xen-unstable.git/commitdiff
x86: support data operand independent timing mode
authorJan Beulich <jbeulich@suse.com>
Tue, 14 Nov 2023 12:58:18 +0000 (13:58 +0100)
committerJan Beulich <jbeulich@suse.com>
Tue, 14 Nov 2023 12:58:18 +0000 (13:58 +0100)
[1] specifies a long list of instructions which are intended to exhibit
timing behavior independent of the data they operate on. On certain
hardware this independence is optional, controlled by a bit in a new
MSR. Provide a command line option to control the mode Xen and its
guests are to operate in, with a build time control over the default.
Longer term we may want to allow guests to control this.

Since Arm64 supposedly also has such a control, put command line option
and Kconfig control in common files.

[1] https://www.intel.com/content/www/us/en/developer/articles/technical/software-security-guidance/best-practices/data-operand-independent-timing-isa-guidance.html

Requested-by: Demi Marie Obenour <demi@invisiblethingslab.com>
Signed-off-by: Jan Beulich <jbeulich@suse.com>
Acked-by: Roger Pau Monné <roger.pau@citrix.com>
master commit: bad1ac345b1910b820b8a703ad1b9f66412ea844
master date: 2023-10-20 15:50:05 +0200

CHANGELOG.md
docs/misc/xen-command-line.pandoc
xen/arch/x86/Kconfig
xen/arch/x86/cpu/common.c
xen/arch/x86/include/asm/cpufeature.h
xen/common/Kconfig
xen/common/kernel.c
xen/include/xen/param.h

index bb0eceb69ab47fe8e94f17ceac5d8e0dd1507f19..3da238d5b9cb546d9da2d5e8d9318cc4a52a51b9 100644 (file)
@@ -10,6 +10,10 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/)
  - Ignore VCPUOP_set_singleshot_timer's VCPU_SSHOTTMR_future flag. The only
    known user doesn't use it properly, leading to in-guest breakage.
 
+### Added
+ - On x86, support for enforcing system-wide operation in Data Operand
+   Independent Timing Mode.
+
 ## [4.17.0](https://xenbits.xen.org/gitweb/?p=xen.git;a=shortlog;h=RELEASE-4.17.0) - 2022-12-12
 
 ### Changed
index c4afd51a8163cc6d4e023db291cb0dad825d3622..5ad24a70a96921fa6d26a2f3596f5bebb291628a 100644 (file)
@@ -770,6 +770,17 @@ Specify the size of the console debug trace buffer. By specifying `cpu:`
 additionally a trace buffer of the specified size is allocated per cpu.
 The debug trace feature is only enabled in debugging builds of Xen.
 
+### dit (x86/Intel)
+> `= <boolean>`
+
+> Default: `CONFIG_DIT_DEFAULT`
+
+Specify whether Xen and guests should operate in Data Independent Timing
+mode (Intel calls this DOITM, Data Operand Independent Timing Mode). Note
+that enabling this option cannot guarantee anything beyond what underlying
+hardware guarantees (with, where available and known to Xen, respective
+tweaks applied).
+
 ### dma_bits
 > `= <integer>`
 
index 2a5c3304e2b04d79a5d3045258491ad43d4460ac..ab47cc23acada12e5c951cde3ec5bab751bf6a87 100644 (file)
@@ -14,6 +14,7 @@ config X86
        select HAS_ALTERNATIVE
        select HAS_COMPAT
        select HAS_CPUFREQ
+       select HAS_DIT
        select HAS_EHCI
        select HAS_EX_TABLE
        select HAS_FAST_MULTIPLY
index ffa609930766cddfa80306c6601316e4abc0ed2f..54ea7fa831140cffd8a7c912a43096a83c20f110 100644 (file)
@@ -211,6 +211,28 @@ void ctxt_switch_levelling(const struct vcpu *next)
                alternative_vcall(ctxt_switch_masking, next);
 }
 
+static void setup_doitm(void)
+{
+    uint64_t msr;
+
+    if ( !cpu_has_doitm )
+        return;
+
+    /*
+     * We don't currently enumerate DOITM to guests.  As a conseqeuence, guest
+     * kernels will believe they're safe even when they are not.
+     *
+     * For now, set it unilaterally.  This prevents otherwise-correct crypto
+     * code from becoming vulnerable to timing sidechannels.
+     */
+
+    rdmsrl(MSR_UARCH_MISC_CTRL, msr);
+    msr |= UARCH_CTRL_DOITM;
+    if ( !opt_dit )
+        msr &= ~UARCH_CTRL_DOITM;
+    wrmsrl(MSR_UARCH_MISC_CTRL, msr);
+}
+
 bool_t opt_cpu_info;
 boolean_param("cpuinfo", opt_cpu_info);
 
@@ -596,6 +618,8 @@ void identify_cpu(struct cpuinfo_x86 *c)
 
                mtrr_bp_init();
        }
+
+       setup_doitm();
 }
 
 /* leaf 0xb SMT level */
index b818ef75c03c7921774ea4abc31a7b072a3d2241..9ef7756593702f8138041a23662c35dd292cd832 100644 (file)
 #define cpu_has_if_pschange_mc_no boot_cpu_has(X86_FEATURE_IF_PSCHANGE_MC_NO)
 #define cpu_has_tsx_ctrl        boot_cpu_has(X86_FEATURE_TSX_CTRL)
 #define cpu_has_taa_no          boot_cpu_has(X86_FEATURE_TAA_NO)
+#define cpu_has_doitm           boot_cpu_has(X86_FEATURE_DOITM)
 #define cpu_has_fb_clear        boot_cpu_has(X86_FEATURE_FB_CLEAR)
 #define cpu_has_rrsba           boot_cpu_has(X86_FEATURE_RRSBA)
 #define cpu_has_gds_ctrl        boot_cpu_has(X86_FEATURE_GDS_CTRL)
index 855c843113e3f5b229374866052a069bc9586a55..e7794cb7f681db8e4bd957185198a35137d86edd 100644 (file)
@@ -38,6 +38,9 @@ config HAS_COMPAT
 config HAS_DEVICE_TREE
        bool
 
+config HAS_DIT # Data Independent Timing
+       bool
+
 config HAS_EX_TABLE
        bool
 
@@ -172,6 +175,21 @@ config SPECULATIVE_HARDEN_GUEST_ACCESS
 
 endmenu
 
+config DIT_DEFAULT
+       bool "Data Independent Timing default"
+       depends on HAS_DIT
+       help
+         Hardware often surfaces instructions the timing of which is dependent
+         on the data they process.  Some of these instructions may be used in
+         timing sensitive environments, e.g. cryptography.  When such
+         instructions exist, hardware may further surface a control allowing
+         to make the behavior of such instructions independent of the data
+         they act upon.  Note the build time value can be overridden at runtime
+         using the "dit" command line option.
+
+         NB: Intel calls the feature DOITM (Data Operand Independent Timing
+             Mode).
+
 config HYPFS
        bool "Hypervisor file system support"
        default y
index 0e8abe0cf8a806decc11b8c1f995529b6cc34032..f64f7dab37611614322fcfd46baaca45a74d9bff 100644 (file)
 
 enum system_state system_state = SYS_STATE_early_boot;
 
+#ifdef CONFIG_HAS_DIT
+bool __ro_after_init opt_dit = IS_ENABLED(CONFIG_DIT_DEFAULT);
+boolean_param("dit", opt_dit);
+#endif
+
 xen_commandline_t saved_cmdline;
 static const char __initconst opt_builtin_cmdline[] = CONFIG_CMDLINE;
 
index 1b2c7db9548d80f7ebc66f63c98a77aa2710517d..93c3fe7cb797c1fc4790e377472427af809d6998 100644 (file)
@@ -184,6 +184,8 @@ extern struct param_hypfs __paramhypfs_start[], __paramhypfs_end[];
     string_param(_name, _var); \
     string_runtime_only_param(_name, _var)
 
+extern bool opt_dit;
+
 static inline void no_config_param(const char *cfg, const char *param,
                                    const char *s, const char *e)
 {