Skip to content

Commit c27fa53

Browse files
bjorn-rivospalmer-dabbelt
authored andcommitted
riscv: Fix vector state restore in rt_sigreturn()
The RISC-V Vector specification states in "Appendix D: Calling Convention for Vector State" [1] that "Executing a system call causes all caller-saved vector registers (v0-v31, vl, vtype) and vstart to become unspecified.". In the RISC-V kernel this is called "discarding the vstate". Returning from a signal handler via the rt_sigreturn() syscall, vector discard is also performed. However, this is not an issue since the vector state should be restored from the sigcontext, and therefore not care about the vector discard. The "live state" is the actual vector register in the running context, and the "vstate" is the vector state of the task. A dirty live state, means that the vstate and live state are not in synch. When vectorized user_from_copy() was introduced, an bug sneaked in at the restoration code, related to the discard of the live state. An example when this go wrong: 1. A userland application is executing vector code 2. The application receives a signal, and the signal handler is entered. 3. The application returns from the signal handler, using the rt_sigreturn() syscall. 4. The live vector state is discarded upon entering the rt_sigreturn(), and the live state is marked as "dirty", indicating that the live state need to be synchronized with the current vstate. 5. rt_sigreturn() restores the vstate, except the Vector registers, from the sigcontext 6. rt_sigreturn() restores the Vector registers, from the sigcontext, and now the vectorized user_from_copy() is used. The dirty live state from the discard is saved to the vstate, making the vstate corrupt. 7. rt_sigreturn() returns to the application, which crashes due to corrupted vstate. Note that the vectorized user_from_copy() is invoked depending on the value of CONFIG_RISCV_ISA_V_UCOPY_THRESHOLD. Default is 768, which means that vlen has to be larger than 128b for this bug to trigger. The fix is simply to mark the live state as non-dirty/clean prior performing the vstate restore. Link: https://github.com/riscv/riscv-isa-manual/releases/download/riscv-isa-release-8abdb41-2024-03-26/unpriv-isa-asciidoc.pdf # [1] Reported-by: Charlie Jenkins <[email protected]> Reported-by: Vineet Gupta <[email protected]> Fixes: c2a658d ("riscv: lib: vectorize copy_to_user/copy_from_user") Signed-off-by: Björn Töpel <[email protected]> Reviewed-by: Andy Chiu <[email protected]> Tested-by: Vineet Gupta <[email protected]> Link: https://lore.kernel.org/r/[email protected] Cc: [email protected] Signed-off-by: Palmer Dabbelt <[email protected]>
1 parent 0ffe1ae commit c27fa53

File tree

1 file changed

+8
-7
lines changed

1 file changed

+8
-7
lines changed

arch/riscv/kernel/signal.c

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec)
119119
struct __sc_riscv_v_state __user *state = sc_vec;
120120
void __user *datap;
121121

122+
/*
123+
* Mark the vstate as clean prior performing the actual copy,
124+
* to avoid getting the vstate incorrectly clobbered by the
125+
* discarded vector state.
126+
*/
127+
riscv_v_vstate_set_restore(current, regs);
128+
122129
/* Copy everything of __sc_riscv_v_state except datap. */
123130
err = __copy_from_user(&current->thread.vstate, &state->v_state,
124131
offsetof(struct __riscv_v_ext_state, datap));
@@ -133,13 +140,7 @@ static long __restore_v_state(struct pt_regs *regs, void __user *sc_vec)
133140
* Copy the whole vector content from user space datap. Use
134141
* copy_from_user to prevent information leak.
135142
*/
136-
err = copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize);
137-
if (unlikely(err))
138-
return err;
139-
140-
riscv_v_vstate_set_restore(current, regs);
141-
142-
return err;
143+
return copy_from_user(current->thread.vstate.datap, datap, riscv_v_vsize);
143144
}
144145
#else
145146
#define save_v_state(task, regs) (0)

0 commit comments

Comments
 (0)