Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

xen: detect uninitialized xenbus in xenbus_init

If the xenstore page hasn't been allocated properly, reading the value
of the related hvm_param (HVM_PARAM_STORE_PFN) won't actually return
error. Instead, it will succeed and return zero. Instead of attempting
to xen_remap a bad guest physical address, detect this condition and
return early.

Note that although a guest physical address of zero for
HVM_PARAM_STORE_PFN is theoretically possible, it is not a good choice
and zero has never been validly used in that capacity.

Also recognize all bits set as an invalid value.

For 32-bit Linux, any pfn above ULONG_MAX would get truncated. Pfns
above ULONG_MAX should never be passed by the Xen tools to HVM guests
anyway, so check for this condition and return early.

Cc: stable@vger.kernel.org
Signed-off-by: Stefano Stabellini <stefano.stabellini@xilinx.com>
Reviewed-by: Juergen Gross <jgross@suse.com>
Reviewed-by: Jan Beulich <jbeulich@suse.com>
Link: https://lore.kernel.org/r/20211123210748.1910236-1-sstabellini@kernel.org
Signed-off-by: Boris Ostrovsky <boris.ostrovsky@oracle.com>

authored by

Stefano Stabellini and committed by
Boris Ostrovsky
36e8f60f de6da33e

+23
+23
drivers/xen/xenbus/xenbus_probe.c
··· 949 949 err = hvm_get_parameter(HVM_PARAM_STORE_PFN, &v); 950 950 if (err) 951 951 goto out_error; 952 + /* 953 + * Uninitialized hvm_params are zero and return no error. 954 + * Although it is theoretically possible to have 955 + * HVM_PARAM_STORE_PFN set to zero on purpose, in reality it is 956 + * not zero when valid. If zero, it means that Xenstore hasn't 957 + * been properly initialized. Instead of attempting to map a 958 + * wrong guest physical address return error. 959 + * 960 + * Also recognize all bits set as an invalid value. 961 + */ 962 + if (!v || !~v) { 963 + err = -ENOENT; 964 + goto out_error; 965 + } 966 + /* Avoid truncation on 32-bit. */ 967 + #if BITS_PER_LONG == 32 968 + if (v > ULONG_MAX) { 969 + pr_err("%s: cannot handle HVM_PARAM_STORE_PFN=%llx > ULONG_MAX\n", 970 + __func__, v); 971 + err = -EINVAL; 972 + goto out_error; 973 + } 974 + #endif 952 975 xen_store_gfn = (unsigned long)v; 953 976 xen_store_interface = 954 977 xen_remap(xen_store_gfn << XEN_PAGE_SHIFT,