Skip to content

Commit 76021e9

Browse files
committed
KVM: Protect vcpu->pid dereference via debugfs with RCU
Wrap the vcpu->pid dereference in the debugfs hook vcpu_get_pid() with proper RCU read (un)lock. Unlike the code in kvm_vcpu_ioctl(), vcpu_get_pid() is not a simple access; the pid pointer is passed to pid_nr() and fully dereferenced if the pointer is non-NULL. Failure to acquire RCU could result in use-after-free of the old pid if a different task invokes KVM_RUN and puts the last reference to the old vcpu->pid between vcpu_get_pid() reading the pointer and dereferencing it in pid_nr(). Fixes: e36de87 ("KVM: debugfs: expose pid of vcpu threads") Link: https://lore.kernel.org/r/20230211010719.982919-1-seanjc@google.com Signed-off-by: Sean Christopherson <seanjc@google.com>
1 parent b9846a6 commit 76021e9

File tree

1 file changed

+4
-1
lines changed

1 file changed

+4
-1
lines changed

virt/kvm/kvm_main.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3870,7 +3870,10 @@ static int create_vcpu_fd(struct kvm_vcpu *vcpu)
38703870
static int vcpu_get_pid(void *data, u64 *val)
38713871
{
38723872
struct kvm_vcpu *vcpu = data;
3873-
*val = pid_nr(rcu_access_pointer(vcpu->pid));
3873+
3874+
rcu_read_lock();
3875+
*val = pid_nr(rcu_dereference(vcpu->pid));
3876+
rcu_read_unlock();
38743877
return 0;
38753878
}
38763879

0 commit comments

Comments
 (0)