seg_cmp() returns the 64-bit difference of the two target addresses as
an int. When that difference has bit 31 set the order comes out
backwards, and when it is a multiple of 4GB the segments compare equal.
Nothing relied on the order so far, but the overlap check below does.
Compare the addresses instead.
A segment with memsz 0 passes validation, but the MD code takes the
first empty segment as the end of the list: on amd64 kexec_md_pages()
stops counting there, kexec_load_md() stops mapping targets and the
trampoline stops copying. An empty segment that sorts ahead of a large
one leaves kexec_load_md() without enough page table pages for the
staging area, and kexec_generate_page_tables() dereferences the NULL
page that vm_radix_iter_next() returns. With a smaller second segment
the load succeeds and the image is silently cut short.
Segments with overlapping targets are not rejected either. The
staging code moves every page whose physical address lies in a target
to the pindex that will be copied onto it, which assumes each target
page belongs to one segment only.
Return EINVAL for empty and overlapping segments, and add tests for
both and for the sort. The tests first check that a one page image can
be staged, since amd64 with LA57 rejects every image and the EINVAL
cases would otherwise pass without testing anything.
Sponsored by: Advanced Micro Devices, Inc.
Signed-off-by: Osvaldo Janeri Filho <ojanerif@amd.com>