Skip to content

Commit

Permalink
KVM: Introduce vcpu->wants_to_run
Browse files Browse the repository at this point in the history
Introduce vcpu->wants_to_run to indicate when a vCPU is in its core run
loop, i.e. when the vCPU is running the KVM_RUN ioctl and immediate_exit
was not set.

Replace all references to vcpu->run->immediate_exit with
!vcpu->wants_to_run to avoid TOCTOU races with userspace. For example, a
malicious userspace could invoked KVM_RUN with immediate_exit=true and
then after KVM reads it to set wants_to_run=false, flip it to false.
This would result in the vCPU running in KVM_RUN with
wants_to_run=false. This wouldn't cause any real bugs today but is a
dangerous landmine.

Signed-off-by: David Matlack <[email protected]>
Link: https://lore.kernel.org/r/[email protected]
Signed-off-by: Sean Christopherson <[email protected]>
  • Loading branch information
dmatlack authored and sean-jc committed Jun 18, 2024
1 parent 438a496 commit a681631
Show file tree
Hide file tree
Showing 9 changed files with 12 additions and 8 deletions.
2 changes: 1 addition & 1 deletion arch/arm64/kvm/arm.c
Original file line number Diff line number Diff line change
Expand Up @@ -1099,7 +1099,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)

vcpu_load(vcpu);

if (run->immediate_exit) {
if (!vcpu->wants_to_run) {
ret = -EINTR;
goto out;
}
Expand Down
2 changes: 1 addition & 1 deletion arch/loongarch/kvm/vcpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -1266,7 +1266,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
kvm_complete_iocsr_read(vcpu, run);
}

if (run->immediate_exit)
if (!vcpu->wants_to_run)
return r;

/* Clear exit_reason */
Expand Down
2 changes: 1 addition & 1 deletion arch/mips/kvm/mips.c
Original file line number Diff line number Diff line change
Expand Up @@ -436,7 +436,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
vcpu->mmio_needed = 0;
}

if (vcpu->run->immediate_exit)
if (!vcpu->wants_to_run)
goto out;

lose_fpu(1);
Expand Down
2 changes: 1 addition & 1 deletion arch/powerpc/kvm/powerpc.c
Original file line number Diff line number Diff line change
Expand Up @@ -1852,7 +1852,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)

kvm_sigset_activate(vcpu);

if (run->immediate_exit)
if (!vcpu->wants_to_run)
r = -EINTR;
else
r = kvmppc_vcpu_run(vcpu);
Expand Down
2 changes: 1 addition & 1 deletion arch/riscv/kvm/vcpu.c
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
return ret;
}

if (run->immediate_exit) {
if (!vcpu->wants_to_run) {
kvm_vcpu_srcu_read_unlock(vcpu);
return -EINTR;
}
Expand Down
2 changes: 1 addition & 1 deletion arch/s390/kvm/kvm-s390.c
Original file line number Diff line number Diff line change
Expand Up @@ -5026,7 +5026,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
if (vcpu->kvm->arch.pv.dumping)
return -EINVAL;

if (kvm_run->immediate_exit)
if (!vcpu->wants_to_run)
return -EINTR;

if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||
Expand Down
4 changes: 2 additions & 2 deletions arch/x86/kvm/x86.c
Original file line number Diff line number Diff line change
Expand Up @@ -11407,7 +11407,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)

kvm_vcpu_srcu_read_lock(vcpu);
if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
if (kvm_run->immediate_exit) {
if (!vcpu->wants_to_run) {
r = -EINTR;
goto out;
}
Expand Down Expand Up @@ -11485,7 +11485,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
WARN_ON_ONCE(vcpu->mmio_needed);
}

if (kvm_run->immediate_exit) {
if (!vcpu->wants_to_run) {
r = -EINTR;
goto out;
}
Expand Down
1 change: 1 addition & 0 deletions include/linux/kvm_host.h
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ struct kvm_vcpu {
bool dy_eligible;
} spin_loop;
#endif
bool wants_to_run;
bool preempted;
bool ready;
bool scheduled_out;
Expand Down
3 changes: 3 additions & 0 deletions virt/kvm/kvm_main.c
Original file line number Diff line number Diff line change
Expand Up @@ -4435,7 +4435,10 @@ static long kvm_vcpu_ioctl(struct file *filp,
synchronize_rcu();
put_pid(oldpid);
}
vcpu->wants_to_run = !READ_ONCE(vcpu->run->immediate_exit);
r = kvm_arch_vcpu_ioctl_run(vcpu);
vcpu->wants_to_run = false;

trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
break;
}
Expand Down

0 comments on commit a681631

Please sign in to comment.