xen/x86: Fix memory leak in vcpu_create() error path
authorAndrew Cooper <andrew.cooper3@citrix.com>
Mon, 28 Sep 2020 14:25:44 +0000 (15:25 +0100)
committerAndrew Cooper <andrew.cooper3@citrix.com>
Mon, 21 Dec 2020 14:11:25 +0000 (14:11 +0000)
commitd162f36848c4a98a782cc05820b0aa7ec1ae297d
tree999983bda831f08db8735d4103efa72dd2fdf8f8
parent6131dab5f2c8059a0fc7fd884bc6d4ff78ba44c2
xen/x86: Fix memory leak in vcpu_create() error path

Various paths in vcpu_create() end up calling paging_update_paging_modes(),
which eventually allocate a monitor pagetable if one doesn't exist.

However, an error in vcpu_create() results in the vcpu being cleaned up
locally, and not put onto the domain's vcpu list.  Therefore, the monitor
table is not freed by {hap,shadow}_teardown()'s loop.  This is caught by
assertions later that we've successfully freed the entire hap/shadow memory
pool.

The per-vcpu loops in domain teardown logic is conceptually wrong, but exist
due to insufficient existing structure in the existing logic.

Break paging_vcpu_teardown() out of paging_teardown(), with mirrored breakouts
in the hap/shadow code, and use it from arch_vcpu_create()'s error path.  This
fixes the memory leak.

The new {hap,shadow}_vcpu_teardown() must be idempotent, and are written to be
as tolerable as possible, with the minimum number of safety checks possible.
In particular, drop the mfn_valid() check - if these fields are junk, then Xen
is going to explode anyway.

Reported-by: Michał Leszczyński <michal.leszczynski@cert.pl>
Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
xen/arch/x86/domain.c
xen/arch/x86/mm/hap/hap.c
xen/arch/x86/mm/paging.c
xen/arch/x86/mm/shadow/common.c
xen/include/asm-x86/hap.h
xen/include/asm-x86/paging.h
xen/include/asm-x86/shadow.h