Skip to content

Commit a681631

Browse files
dmatlacksean-jc
authored andcommitted
KVM: Introduce vcpu->wants_to_run
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]>
1 parent 438a496 commit a681631

File tree

9 files changed

+12
-8
lines changed

9 files changed

+12
-8
lines changed

arch/arm64/kvm/arm.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1099,7 +1099,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
10991099

11001100
vcpu_load(vcpu);
11011101

1102-
if (run->immediate_exit) {
1102+
if (!vcpu->wants_to_run) {
11031103
ret = -EINTR;
11041104
goto out;
11051105
}

arch/loongarch/kvm/vcpu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1266,7 +1266,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
12661266
kvm_complete_iocsr_read(vcpu, run);
12671267
}
12681268

1269-
if (run->immediate_exit)
1269+
if (!vcpu->wants_to_run)
12701270
return r;
12711271

12721272
/* Clear exit_reason */

arch/mips/kvm/mips.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -436,7 +436,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
436436
vcpu->mmio_needed = 0;
437437
}
438438

439-
if (vcpu->run->immediate_exit)
439+
if (!vcpu->wants_to_run)
440440
goto out;
441441

442442
lose_fpu(1);

arch/powerpc/kvm/powerpc.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1852,7 +1852,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
18521852

18531853
kvm_sigset_activate(vcpu);
18541854

1855-
if (run->immediate_exit)
1855+
if (!vcpu->wants_to_run)
18561856
r = -EINTR;
18571857
else
18581858
r = kvmppc_vcpu_run(vcpu);

arch/riscv/kvm/vcpu.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
760760
return ret;
761761
}
762762

763-
if (run->immediate_exit) {
763+
if (!vcpu->wants_to_run) {
764764
kvm_vcpu_srcu_read_unlock(vcpu);
765765
return -EINTR;
766766
}

arch/s390/kvm/kvm-s390.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5026,7 +5026,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
50265026
if (vcpu->kvm->arch.pv.dumping)
50275027
return -EINVAL;
50285028

5029-
if (kvm_run->immediate_exit)
5029+
if (!vcpu->wants_to_run)
50305030
return -EINTR;
50315031

50325032
if (kvm_run->kvm_valid_regs & ~KVM_SYNC_S390_VALID_FIELDS ||

arch/x86/kvm/x86.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11407,7 +11407,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
1140711407

1140811408
kvm_vcpu_srcu_read_lock(vcpu);
1140911409
if (unlikely(vcpu->arch.mp_state == KVM_MP_STATE_UNINITIALIZED)) {
11410-
if (kvm_run->immediate_exit) {
11410+
if (!vcpu->wants_to_run) {
1141111411
r = -EINTR;
1141211412
goto out;
1141311413
}
@@ -11485,7 +11485,7 @@ int kvm_arch_vcpu_ioctl_run(struct kvm_vcpu *vcpu)
1148511485
WARN_ON_ONCE(vcpu->mmio_needed);
1148611486
}
1148711487

11488-
if (kvm_run->immediate_exit) {
11488+
if (!vcpu->wants_to_run) {
1148911489
r = -EINTR;
1149011490
goto out;
1149111491
}

include/linux/kvm_host.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ struct kvm_vcpu {
378378
bool dy_eligible;
379379
} spin_loop;
380380
#endif
381+
bool wants_to_run;
381382
bool preempted;
382383
bool ready;
383384
bool scheduled_out;

virt/kvm/kvm_main.c

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4435,7 +4435,10 @@ static long kvm_vcpu_ioctl(struct file *filp,
44354435
synchronize_rcu();
44364436
put_pid(oldpid);
44374437
}
4438+
vcpu->wants_to_run = !READ_ONCE(vcpu->run->immediate_exit);
44384439
r = kvm_arch_vcpu_ioctl_run(vcpu);
4440+
vcpu->wants_to_run = false;
4441+
44394442
trace_kvm_userspace_exit(vcpu->run->exit_reason, r);
44404443
break;
44414444
}

0 commit comments

Comments
 (0)