KVM: do not kfree error pointer
PATCH 1/3 KVM: do not kfree error pointer
We should avoid kfree()ing error pointer in kvm_vcpu_ioctl() and kvm_arch_vcpu_ioctl().
Signed-off-by Guo Chao <[email protected]>
--- arch/x86/kvm/x86.c | 19 ++++++------------- virt/kvm/kvm_main.c | 2 ++ 2 files changed, 8 insertions(+), 13 deletions(-)
arch/x86/kvm/x86.c +6 -13
@@ -2687,14 +2687,11 @@ long kvm_arch_vcpu_ioctl(struct file *filp,break ; }case KVM_SET_LAPIC : { - r = -EINVAL ;if (!vcpu->arch.apic) goto out; u.lapic =memdup_user (argp,sizeof (*u.lapic)); -if (IS_ERR (u.lapic)) { - r =PTR_ERR (u.lapic); - goto out; - } +if (IS_ERR (u.lapic)) +return PTR_ERR (u.lapic); r =kvm_vcpu_ioctl_set_lapic (vcpu, u.lapic);if (r)
@@ -2875,10 +2872,8 @@ long kvm_arch_vcpu_ioctl(struct file *filp, }case KVM_SET_XSAVE : { u.xsave =memdup_user (argp,sizeof (*u.xsave)); -if (IS_ERR (u.xsave)) { - r =PTR_ERR (u.xsave); - goto out; - } +if (IS_ERR (u.xsave)) +return PTR_ERR (u.xsave); r =kvm_vcpu_ioctl_x86_set_xsave (vcpu, u.xsave);break ;
@@ -2900,10 +2895,8 @@ long kvm_arch_vcpu_ioctl(struct file *filp, }case KVM_SET_XCRS : { u.xcrs =memdup_user (argp,sizeof (*u.xcrs)); -if (IS_ERR (u.xcrs)) { - r =PTR_ERR (u.xcrs); - goto out; - } +if (IS_ERR (u.xcrs)) +return PTR_ERR (u.xcrs); r =kvm_vcpu_ioctl_x86_set_xcrs (vcpu, u.xcrs);break ;
virt/kvm/kvm_main.c +2 -0
@@ -1954,6 +1954,7 @@ out_free2: kvm_sregs =memdup_user (argp,sizeof (*kvm_sregs));if (IS_ERR (kvm_sregs)) { r =PTR_ERR (kvm_sregs); + kvm_sregs =NULL ; goto out; } r =kvm_arch_vcpu_ioctl_set_sregs (vcpu, kvm_sregs);
@@ -2054,6 +2055,7 @@ out_free2: fpu =memdup_user (argp,sizeof (*fpu));if (IS_ERR (fpu)) { r =PTR_ERR (fpu); + fpu =NULL ; goto out; } r =kvm_arch_vcpu_ioctl_set_fpu (vcpu, fpu);
Signature · 1 line
1.7.9.5
PATCH 2/3 KVM: X86: fix return value of kvm_vm_ioctl_set_tss_addr()
Return value of this function will be that of ioctl().
#include <stdio.h>
#include <linux/kvm.h>
int main () {
int fd;
fd = open ("/dev/kvm", 0);
fd = ioctl (fd, KVM_CREATE_VM, 0);
ioctl (fd, KVM_SET_TSS_ADDR, 0xfffff000);
perror ("");
return 0;
}Output is "Operation not permitted". That's not what we want.
Return -EINVAL in this case.
Signed-off-by Guo Chao <[email protected]>
--- arch/x86/kvm/x86.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-)
arch/x86/kvm/x86.c +1 -1
@@ -2944,7 +2944,7 @@ static int kvm_vm_ioctl_set_tss_addr(struct kvm *kvm, unsigned long addr)int ret;if (addr > (unsigned int )(-3 *PAGE_SIZE )) -return -1 ; +return -EINVAL ; ret = kvm_x86_ops->set_tss_addr (kvm, addr);return ret; }
Signature · 1 line
1.7.9.5
PATCH 3/3 KVM: remove unnecessary return value check
In reply to Guo ChaoNo need to check return value before breaking switch.
Signed-off-by Guo Chao <[email protected]>
--- arch/x86/kvm/x86.c | 32 -------------------------------- virt/kvm/kvm_main.c | 30 ------------------------------ 2 files changed, 62 deletions(-)
arch/x86/kvm/x86.c +0 -32
@@ -2694,9 +2694,6 @@ long kvm_arch_vcpu_ioctl(struct file *filp,return PTR_ERR (u.lapic); r =kvm_vcpu_ioctl_set_lapic (vcpu, u.lapic); -if (r) - goto out; - r =0 ;break ; }case KVM_INTERRUPT : {
@@ -2706,16 +2703,10 @@ long kvm_arch_vcpu_ioctl(struct file *filp,if (copy_from_user (&irq, argp,sizeof irq)) goto out; r =kvm_vcpu_ioctl_interrupt (vcpu, &irq); -if (r) - goto out; - r =0 ;break ; }case KVM_NMI : { r =kvm_vcpu_ioctl_nmi (vcpu); -if (r) - goto out; - r =0 ;break ; }case KVM_SET_CPUID : {
@@ -2726,8 +2717,6 @@ long kvm_arch_vcpu_ioctl(struct file *filp,if (copy_from_user (&cpuid, cpuid_arg,sizeof cpuid)) goto out; r =kvm_vcpu_ioctl_set_cpuid (vcpu, &cpuid, cpuid_arg->entries); -if (r) - goto out;break ; }case KVM_SET_CPUID2 : {
@@ -2739,8 +2728,6 @@ long kvm_arch_vcpu_ioctl(struct file *filp, goto out; r =kvm_vcpu_ioctl_set_cpuid2 (vcpu, &cpuid, cpuid_arg->entries); -if (r) - goto out;break ; }case KVM_GET_CPUID2 : {
@@ -3205,8 +3192,6 @@ long kvm_arch_vm_ioctl(struct file *filp,switch (ioctl) {case KVM_SET_TSS_ADDR : r =kvm_vm_ioctl_set_tss_addr (kvm, arg); -if (r <0 ) - goto out;break ;case KVM_SET_IDENTITY_MAP_ADDR : {u64 ident_addr;
@@ -3215,14 +3200,10 @@ long kvm_arch_vm_ioctl(struct file *filp,if (copy_from_user (&ident_addr, argp,sizeof ident_addr)) goto out; r =kvm_vm_ioctl_set_identity_map_addr (kvm, ident_addr); -if (r <0 ) - goto out;break ; }case KVM_SET_NR_MMU_PAGES : r =kvm_vm_ioctl_set_nr_mmu_pages (kvm, arg); -if (r) - goto out;break ;case KVM_GET_NR_MMU_PAGES : r =kvm_vm_ioctl_get_nr_mmu_pages (kvm);
@@ -3313,8 +3294,6 @@ long kvm_arch_vm_ioctl(struct file *filp, r =0 ; get_irqchip_out:kfree (chip); -if (r) - goto out;break ; }case KVM_SET_IRQCHIP : {
@@ -3336,8 +3315,6 @@ long kvm_arch_vm_ioctl(struct file *filp, r =0 ; set_irqchip_out:kfree (chip); -if (r) - goto out;break ; }case KVM_GET_PIT : {
@@ -3364,9 +3341,6 @@ long kvm_arch_vm_ioctl(struct file *filp,if (!kvm->arch.vpit) goto out; r =kvm_vm_ioctl_set_pit (kvm, &u.ps); -if (r) - goto out; - r =0 ;break ; }case KVM_GET_PIT2 : {
@@ -3390,9 +3364,6 @@ long kvm_arch_vm_ioctl(struct file *filp,if (!kvm->arch.vpit) goto out; r =kvm_vm_ioctl_set_pit2 (kvm, &u.ps2); -if (r) - goto out; - r =0 ;break ; }case KVM_REINJECT_CONTROL : {
@@ -3401,9 +3372,6 @@ long kvm_arch_vm_ioctl(struct file *filp,if (copy_from_user (&control, argp,sizeof (control))) goto out; r =kvm_vm_ioctl_reinject (kvm, &control); -if (r) - goto out; - r =0 ;break ; }case KVM_XEN_HVM_CONFIG : {
virt/kvm/kvm_main.c +0 -30
@@ -1929,10 +1929,6 @@ out_free1: goto out; } r =kvm_arch_vcpu_ioctl_set_regs (vcpu, kvm_regs); -if (r) - goto out_free2; - r =0 ; -out_free2:kfree (kvm_regs);break ; }
@@ -1958,9 +1954,6 @@ out_free2: goto out; } r =kvm_arch_vcpu_ioctl_set_sregs (vcpu, kvm_sregs); -if (r) - goto out; - r =0 ;break ; }case KVM_GET_MP_STATE : {
@@ -1982,9 +1975,6 @@ out_free2:if (copy_from_user (&mp_state, argp,sizeof mp_state)) goto out; r =kvm_arch_vcpu_ioctl_set_mpstate (vcpu, &mp_state); -if (r) - goto out; - r =0 ;break ; }case KVM_TRANSLATE : {
@@ -2009,9 +1999,6 @@ out_free2:if (copy_from_user (&dbg, argp,sizeof dbg)) goto out; r =kvm_arch_vcpu_ioctl_set_guest_debug (vcpu, &dbg); -if (r) - goto out; - r =0 ;break ; }case KVM_SET_SIGNAL_MASK : {
@@ -2059,9 +2046,6 @@ out_free2: goto out; } r =kvm_arch_vcpu_ioctl_set_fpu (vcpu, fpu); -if (r) - goto out; - r =0 ;break ; }default :
@@ -2131,8 +2115,6 @@ static long kvm_vm_ioctl(struct file *filp,switch (ioctl) {case KVM_CREATE_VCPU : r =kvm_vm_ioctl_create_vcpu (kvm, arg); -if (r <0 ) - goto out;break ;case KVM_SET_USER_MEMORY_REGION : {struct kvm_userspace_memory_region kvm_userspace_mem;
@@ -2143,8 +2125,6 @@ static long kvm_vm_ioctl(struct file *filp, goto out; r =kvm_vm_ioctl_set_memory_region (kvm, &kvm_userspace_mem,1 ); -if (r) - goto out;break ; }case KVM_GET_DIRTY_LOG : {
@@ -2154,8 +2134,6 @@ static long kvm_vm_ioctl(struct file *filp,if (copy_from_user (&log, argp,sizeof log)) goto out; r =kvm_vm_ioctl_get_dirty_log (kvm, &log); -if (r) - goto out;break ; }#ifdef KVM_COALESCED_MMIO_PAGE_OFFSET
@@ -2165,9 +2143,6 @@ static long kvm_vm_ioctl(struct file *filp,if (copy_from_user (&zone, argp,sizeof zone)) goto out; r =kvm_vm_ioctl_register_coalesced_mmio (kvm, &zone); -if (r) - goto out; - r =0 ;break ; }case KVM_UNREGISTER_COALESCED_MMIO : {
@@ -2176,9 +2151,6 @@ static long kvm_vm_ioctl(struct file *filp,if (copy_from_user (&zone, argp,sizeof zone)) goto out; r =kvm_vm_ioctl_unregister_coalesced_mmio (kvm, &zone); -if (r) - goto out; - r =0 ; break; }#endif
@@ -2287,8 +2259,6 @@ static long kvm_vm_compat_ioctl(struct file *filp, log.dirty_bitmap =compat_ptr (compat_log.dirty_bitmap); r =kvm_vm_ioctl_get_dirty_log (kvm, &log); -if (r) - goto out;break ; }default :
Signature · 1 line
1.7.9.5
Marcelo Tosatti
AppliedQuoted 4 lines · Guo Chao
We should avoid kfree()ing error pointer in
kvm_vcpu_ioctl()andkvm_arch_vcpu_ioctl().Signed-off-by Guo Chao <[email protected]>
Applied all, thanks.