diff mbox

Fix NULL dereference in gfn_to_hva_prot()

Message ID 20131001165836.GX11993@redhat.com (mailing list archive)
State New, archived
Headers show

Commit Message

Gleb Natapov Oct. 1, 2013, 4:58 p.m. UTC
gfn_to_memslot() can return NULL or invalid slot. We need to check slot
validity before accessing it.

Signed-off-by: Gleb Natapov <gleb@redhat.com>
--
			Gleb.
--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html

Comments

Paolo Bonzini Oct. 1, 2013, 5:03 p.m. UTC | #1
Il 01/10/2013 18:58, Gleb Natapov ha scritto:
> gfn_to_memslot() can return NULL or invalid slot. We need to check slot
> validity before accessing it.
> 
> Signed-off-by: Gleb Natapov <gleb@redhat.com>
> diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
> index 979bff4..a9dd682 100644
> --- a/virt/kvm/kvm_main.c
> +++ b/virt/kvm/kvm_main.c
> @@ -1064,10 +1064,12 @@ EXPORT_SYMBOL_GPL(gfn_to_hva);
>  unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable)
>  {
>  	struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
> -	if (writable)
> +	unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false);
> +
> +	if (!kvm_is_error_hva(hva) && writable)
>  		*writable = !memslot_is_readonly(slot);
>  
> -	return __gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL, false);
> +	return hva;
>  }
>  
>  static int kvm_read_hva(void *data, void __user *hva, int len)
> --
> 			Gleb.
> 

Reviewed-by: Paolo Bonzini <pbonzini@redhat.com>

--
To unsubscribe from this list: send the line "unsubscribe kvm" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html
diff mbox

Patch

diff --git a/virt/kvm/kvm_main.c b/virt/kvm/kvm_main.c
index 979bff4..a9dd682 100644
--- a/virt/kvm/kvm_main.c
+++ b/virt/kvm/kvm_main.c
@@ -1064,10 +1064,12 @@  EXPORT_SYMBOL_GPL(gfn_to_hva);
 unsigned long gfn_to_hva_prot(struct kvm *kvm, gfn_t gfn, bool *writable)
 {
 	struct kvm_memory_slot *slot = gfn_to_memslot(kvm, gfn);
-	if (writable)
+	unsigned long hva = __gfn_to_hva_many(slot, gfn, NULL, false);
+
+	if (!kvm_is_error_hva(hva) && writable)
 		*writable = !memslot_is_readonly(slot);
 
-	return __gfn_to_hva_many(gfn_to_memslot(kvm, gfn), gfn, NULL, false);
+	return hva;
 }
 
 static int kvm_read_hva(void *data, void __user *hva, int len)