FSCL - FPU Side-Channel Leakage
================================

FPU Side-Channel Leakage (FSCL) is a hardware vulnerability on LoongArch
processors. Operations involving FPU/vector registers may cause data
leakage through the high bits of vector registers.


Affected processors
-------------------
The following processors are not affected:

   - Processors that do not support LSX and LASX (CPUCFG2.LSX=0,
     CPUCFG2.LASX=0)

   - Processors with hardware fixes:
     CPUCFG7.FP_CLEAN=1 or CPUCFG7.FP_NO_L1DLEAK=1

Whether the current processor is affected can be queried via sysfs, see
:ref:`fscl_sys_info`. Not all processors are affected by all FSCL
variants, but the mitigations for all variants are identical, so the
kernel treats them as a single vulnerability.

Problem
-------

The FPU/LSX/LASX instructions on LoongArch processors share a unified
encoding. Different instructions operate on registers of different bit
widths, which may cause data leakage through the high bits of vector
registers. The source of leaked data may be the L1 data cache or
residual register state from other processes. The leaked data is random
in nature and does not cause program execution errors or state changes.

Since buffers may be shared between hyper-threads, cross-hyper-thread
attacks are possible.


Attack scenarios
----------------

FSCL attacks can be launched by unprivileged user-space applications.
The attack requires the attacker and victim to be scheduled on the same
physical core. The FSCL vulnerability does not allow the attacker to
control the memory target address. Therefore, the attack is purely
sample-based. Malicious operations can forward this speculative data to
leakage components, from which the value can be inferred through cache
side-channel attacks.

.. _fscl_sys_info:

FSCL system information
-----------------------

The Linux kernel reports the current FSCL status via the sysfs
interface. The relevant file is:

/sys/devices/system/cpu/vulnerabilities/fscl

The possible values for this file are:

  .. list-table::

     * - 'Not affected'
       - The processor is not affected
     * - 'Vulnerable'
       - The processor is vulnerable, but no mitigation enabled
     * - 'Mitigation: FPU Side Channel Leakage bug was eliminated by flushing context'
       - AUTO mode: mitigates the issue by flushing the L1 cache and
         floating-point registers when the kernel returns to user space
     * - 'Mitigation: FPU Side Channel Leakage bug was eliminated by disable SMT and flushing context'
       - FLUSH_NOSMT mode: mitigates the issue by flushing the L1 cache
         and floating-point registers when the kernel returns to user
         space, and disabling Simultaneous Multi-Threading


Mitigation mechanism
--------------------

At boot time, the kernel detects affected CPUs via the CPUCFG7
register. If the CPU is vulnerable and mitigations are not globally
disabled, the kernel selects a mitigation strategy based on hardware
capabilities and kernel command-line parameters.

.. _flushing:

Residual data flushing
^^^^^^^^^^^^^^^^^^^^^^

  During kernel-to-user and user-to-kernel transitions, the kernel
  conditionally flushes the L1D cache and/or vector registers based on
  context:

  - If exiting from FPU/LSX/LASX exception handling: flush L1D and
    vector registers.

  - If exiting from a non-FPU exception: disable FPU/LSX/LASX to
    prevent future leakage.

.. _virt_mechanism:

Virtualization mitigation
^^^^^^^^^^^^^^^^^^^^^^^^^^

  - Before entering a guest, perform residual data cleanup operations.
    This only covers the host-to-guest transition, i.e., preventing
    data leakage from the host to the guest, but does not protect
    within the guest. The guest must have its own protection measures.

Three mitigation modes:

FSCL_MITIGATION_OFF
^^^^^^^^^^^^^^^^^^^

  No mitigation. The system is vulnerable to cross-thread FPU
  side-channel attacks.

FSCL_MITIGATION_AUTO (default)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  During kernel-to-user and user-to-kernel transitions, the kernel
  conditionally flushes the L1D cache and/or vector registers based on
  context:

  - If exiting from FPU/LSX/LASX exception handling: flush L1D and
    vector registers.

  - If exiting from a non-FPU exception: disable FPU/LSX/LASX to
    prevent future leakage.

FSCL_MITIGATION_FLUSH_NOSMT (disable SMT + force flush)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

  Based on FSCL_MITIGATION_AUTO, with SMT disabled:

  This mode provides full security guarantees at the cost of potential
  performance loss from disabling SMT.

.. _fscl_smt_control

SMT control
^^^^^^^^^^^

  All FSCL variants can be exploited via cross-hyper-thread attacks.
  This means that on affected CPUs, SMT must be disabled to achieve
  full protection.

  Disabling SMT may have a significant impact on performance, though
  the degree depends on the workload type.

.. _fscl_mitigation_control_command_line:

Mitigation control on the kernel command line
---------------------------------------------

The kernel command line allows controlling FSCL mitigations at boot time
via the following options:

  ``fscl=off``
    Disable the mitigation entirely. The system is vulnerable.

  ``fscl=auto``
    Enable AUTO mode (default). SMT-aware dynamic mitigation.

  ``fscl=auto,nosmt``
    Force FLUSH_NOSMT mode. Disables SMT and performs unconditional
    flushing on every kernel-to-user transition.

The global ``mitigations=`` parameter also affects FSCL:

  ``mitigations=off``
    Equivalent to ``fscl=off``.

  ``mitigations=auto``
    Equivalent to ``fscl=auto``.

  ``mitigations=auto,nosmt``
    Equivalent to ``fscl=auto,nosmt``.


Mitigation selection guide
--------------------------

1. Trusted userspace
^^^^^^^^^^^^^^^^^^^^

   If all user-space applications come from trusted sources and do not
   execute externally-provided untrusted code, the mitigation can be
   disabled.

2. Virtualization with trusted guests
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

   The same considerations apply as for trusted userspace above.

3. Virtualization with untrusted guests
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

   If FSCL mitigation is enabled and SMT is disabled, guest-to-host and
   guest-to-guest attacks can be prevented.

.. _fscl_default_mitigations:

Default mitigations
-------------------

  The default mitigation for vulnerable processors is:

  - Enable CPU buffer flushing

  The kernel does not force-disable SMT by default, which means SMT
  systems remain vulnerable when running untrusted code.
