[BUG] RISC-V: new tasks inherit the previous task's FPU state (fcsr) because initial mstatus.FS is set to Clean
I maintainer di solito rispondono entro 3 giorni
Valutazione
- Difficoltà
- 4/5
- Tempo stimato
- 3-5 giorni
- Idoneità per principianti
- 25/100
- Tipo di issue
- Bug
- Chiarezza
- Abbastanza chiara
- Stato di attività
- Attiva
- Stack tecnologico
- c
- Ambito
- operating-systems
Direzione di ricerca
Start with the FS setup in pxPortInitialiseStack() and the configENABLE_FPU block in portable/GCC/RISC-V/portASM.S, then trace portcontextSAVE_CONTEXT_INTERNAL and portcontextRESTORE_CONTEXT to see how the saved mstatus is restored. Decide how a new task's initial FS state should be encoded so its FPU state is defined, which the issue notes is a security-relevant design choice. Done means a fresh task reads frm and fflags as zero on RV32GC hardware or an emulator, with the maintainers having agreed on the approach.
Scritto dal modello di indicizzazione a partire dal testo della issue.
Descrizione
When configENABLE_FPU == 1, pxPortInitialiseStack() forces mstatus.FS to MSTATUS_FS_CLEAN in the initial stack frame of every new task:
/* portable/GCC/RISC-V/portASM.S */
#if( configENABLE_FPU == 1 )
/* Mark the FPU as clean in the mstatus value. */
li t1, ~MSTATUS_FS_MASK
and t0, t0, t1
li t1, MSTATUS_FS_CLEAN
or t0, t0, t1
#endif
No FPU context area is reserved in that initial stack frame, and neither fcsr nor f0-f31 is initialised. When a task that used floating point is switched out, portcontexSAVE_FPU_CONTEXT stores the FPU state but only reads the physical registers (it does not clear them), and portcontextSAVE_CONTEXT_INTERNAL writes FS=Clean back to the hardware mstatus. When the new task is restored, portcontextRESTORE_CONTEXT loads the new task's saved mstatus into t3, writes it to mstatus, and then skips portcontextRESTORE_FPU_CONTEXT because the saved FS is not Dirty:
load_x t3, 1 * portWORD_SIZE( sp )
csrw mstatus, t3
...
srl t1, t3, MSTATUS_FS_OFFSET
andi t1, t1, 3
addi t2, x0, 3
bne t1, t2, 6f /* If FPU status is not dirty, do not restore FPU registers. */
portcontextRESTORE_FPU_CONTEXT
6:
Consequently, a task that has never used the FPU starts with mstatus.FS = Clean while the physical fcsr and f0-f31 still hold the previous task's values. This contradicts the Privileged Specification definition of Clean (section 3.1.6.6, RISC-V Privileged Architecture 20211203):
When the status is Clean, the corresponding state is potentially different from the initial value, but matches the last value stored on a context swap.
A newly created task has no "last value stored on a context swap", so the Clean encoding is semantically invalid for it. The same section also states:
Changing the setting of FS has no effect on the contents of the floating-point register state.
If the status is Initial, the context must be set to an initial constant value on context restore to avoid a security hole, but this can be done without accessing memory.
Target
- Development board: hpm6750evk2
- Instruction Set Architecture: RV32GC
- Toolchain and version: GNU GCC V13.2.0
Host
- Host OS: Windows11
- Version: 25H2
** Impact **
- A new task inherits the previous task's
frm(rounding mode) andfflags(accrued exceptions). Its first floating-point operations may silently use a different rounding mode and non-zero exception flags. - This is a cross-task information leak. The spec explicitly describes an uninitialised Initial state as a "security hole"; using Clean without a saved image is at least as problematic.
Steps to reproduce
- Build with
configENABLE_FPU = 1for an RV32/RV64 core with F/D. - Create Task A that sets a non-default rounding mode (for example
frm = RTZviacsrw frm) and performs floating-point arithmetic, somstatus.FSbecomes Dirty. - Force a context switch (tick interrupt or
taskYIELD()) so Task A's FPU context is saved. - Create/start Task B. On its first run, read
frm/fcsr, or perform an FP operation that assumes the default RNE mode. - Observe that Task B sees RTZ and Task A's
fflagsinstead of the expectedfcsr = 0.
- Lingua principale
- C
- Stelle
- 4.6k
- Fork
- 1.6k
- Merge medio
- 2g 22h
- PR unite (30g)
- 4
Preparare l'ambiente
- Nessun Dockerfile né file Docker Compose
- Ha un modello di pull request
- Leggi la guida per i contributori
Come iniziare
- Leggi tutta la issue e poi la guida ai contributi del progetto.
- Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
- Fai un fork del repository e lavora su un branch.
- Apri una pull request che faccia riferimento al numero della issue.
Altre issue di FreeRTOS/FreeRTOS-Kernel
-
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
FreeRTOS/FreeRTOS-Kernel#1542 ·
I maintainer di solito rispondono entro 3 giorni
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 72/100
FreeRTOS/FreeRTOS-Kernel#1520 ·
I maintainer di solito rispondono entro 3 giorni
-
bug
Difficoltà 2/5 1-3 ore Idoneità per principianti 62/100
FreeRTOS/FreeRTOS-Kernel#1494 · 3 commenti ·
I maintainer di solito rispondono entro 3 giorni
-
enhancement
Difficoltà 5/5 Più di una settimana Idoneità per principianti 25/100
FreeRTOS/FreeRTOS-Kernel#1554 ·
I maintainer di solito rispondono entro 3 giorni
-
[BUG] event_wait_timed can't return falseForse già presa @TedLyngmo l’ha presa 39 giorni fa. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 38/100
FreeRTOS/FreeRTOS-Kernel#1546 ·
I maintainer di solito rispondono entro 3 giorni
Tutte le issue di FreeRTOS/FreeRTOS-Kernel
Issue simili
-
RecompLauncher supportAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
shinyquagsire23/OpenJKDF2#540 ·
I maintainer di solito rispondono entro 12 giorni
-
Prison work hall: laundry and kitchen stations stand at bare walls, the hatch has no counter or signAperta
Difficoltà 2/5 1-3 ore Idoneità per principianti 78/100
Life-Framework/Life-Framework#5939 ·
I maintainer di solito rispondono entro 1 giorno
-
autoconf failes to find libsodium even when present due to implicit function declarationForse già presa @coderjoe l’ha presa oggi. Apertabug
Difficoltà 2/5 1-3 ore Idoneità per principianti 68/100
I maintainer di solito rispondono entro 13 giorni
-
SessionInterfaceHandler is throwing a deprecation notice as a warning instead of a deprecationApertaBug Status: Needs Triage
Difficoltà 1/5 Meno di un'ora Idoneità per principianti 74/100
I maintainer di solito rispondono entro 1 giorno
-
enhancement_package: a site operand of another JSON type raises TypeError, not a named refusalApertabug python
Difficoltà 2/5 1-3 ore Idoneità per principianti 87/100
Jstar269/nakagawa-recomp#3521 ·
I maintainer di solito rispondono entro 1 giorno