PV guest kernel address randomization
RFC 1/1 PV guest kernel address randomization
All,
Linux, at least for some arches, has been doing this for a while, yet the way it's implemented means kernels running in PV Xen guests can't leverage that functionality. Obviously the whole relocation process could be carried out in the guest, but it seems like Xen (for Dom0) or libxc (for DomU) are in much a better position to do at least the initial placement right at a randomized address. That's what the attached patch attempts to implement (which appears to be working fine from the limited testing I can do without a full Linux side implementation) via two new ELF notes; the other attached patch is a mechanical prereq, just for reference.
The more interesting question is that of who to have do the necessary relocations. Native Linux derives a custom format table from the ELF binary, which it attaches to the end of the ELF image. Having Xen consume such a custom format (which may change) doesn't seem like a good idea. It being attached to the end also means that it won't get loaded by the domain loaders, so for it to become consumable by the guest kernel it would need to be made part of the actual ELF image (as e.g. a new section).
The alternative I see would be to require full symbol tables and relocation sections to be retained in the ELF binary, in which case Xen / libxc could do the relocations, and no guest OS changes beyond the respective build logic adjustment and the addition of the ELF notes would be necessary.
Keeping in mind history, either guest adjustment may be a difficult sell to Linux maintainers. How would other OSes (e.g. the BSDs) view this? Do people have preferences either way, or other suggestions (other than "PV is almost dead, let's not touch it")?
Jan
Instead of elf_load_bsdsyms() cooking its own variant, let's just make the generic thing cope with different bitness (paralleling elf_uval() and alike).
xen/common/libelf/libelf-loader.c +11 -21
@@ -273,14 +273,6 @@ static void elf_load_bsdsyms(struct elf_if ( !elf->bsd_symtab_pstart )return ; -#define elf_store_field_bitness (_elf, _hdr, _elm, _val) \ -do { \ - if ( elf_64bit(_elf) ) \ - elf_store_field(_elf, _hdr, e64._elm, _val); \ - else \ - elf_store_field(_elf, _hdr, e32._elm, _val); \ -} while ( 0 ) -#define SYMTAB_INDEX 1#define STRTAB_INDEX 2
@@ -308,16 +300,16 @@ do { elf_uval(elf, elf->ehdr, e_ehsize));/* Set the offset to the shdr array. */ -elf_store_field_bitness (elf, header_handle, e_shoff, - offsetof(typeof(header.elf_header), section)); +elf_store_field (elf, header_handle, e_shoff, + offsetof(typeof(header.elf_header), section));/* Set the right number of section headers. */ -elf_store_field_bitness (elf, header_handle, e_shnum,ELF_BSDSYM_SECTIONS ); +elf_store_field (elf, header_handle, e_shnum,ELF_BSDSYM_SECTIONS );/* Clear a couple of fields we don't use. */ -elf_store_field_bitness (elf, header_handle, e_phoff,0 ); -elf_store_field_bitness (elf, header_handle, e_phentsize,0 ); -elf_store_field_bitness (elf, header_handle, e_phnum,0 ); +elf_store_field (elf, header_handle, e_phoff,0 ); +elf_store_field (elf, header_handle, e_phentsize,0 ); +elf_store_field (elf, header_handle, e_phnum,0 );/* Zero the undefined section. */ section_handle = ELF_MAKE_HANDLE(elf_shdr,
@@ -354,10 +346,9 @@ do { }/* Adjust the sh_offset and sh_link of the copied section header. */ -elf_store_field_bitness (elf, section_handle, sh_offset, - symtab_base - elf_header_base); -elf_store_field_bitness (elf, section_handle, sh_link, -STRTAB_INDEX ); +elf_store_field (elf, section_handle, sh_offset, + symtab_base - elf_header_base); +elf_store_field (elf, section_handle, sh_link,STRTAB_INDEX );/* Calculate the guest address where strtab is loaded. */ strtab_base = elf_round_up(elf, symtab_base +
@@ -387,8 +378,8 @@ do {return ; } -elf_store_field_bitness (elf, section_handle, sh_offset, - strtab_base - elf_header_base); +elf_store_field (elf, section_handle, sh_offset, + strtab_base - elf_header_base);/* Store the whole size (including headers and loaded sections). */ header.size = strtab_base + elf_uval(elf, section_handle, sh_size) -
@@ -409,7 +400,6 @@ do {#undef SYMTAB_INDEX#undef STRTAB_INDEX -#undef elf_store_field_bitness }void elf_parse_binary (struct elf_binary *elf)
xen/include/xen/libelf.h +7 -3
@@ -303,9 +303,13 @@ bool elf_access_ok(struct elf_binary * e/* Stores a value at a particular PTRVAL. */ #define elf_store_field (elf, hdr, elm, val) \ - (elf_store_val((elf), ELF__HANDLE_FIELD_TYPE(hdr, elm), \ - ELF_HANDLE_PTRVAL(hdr) + ELF__HANDLE_FIELD_OFFSET(hdr, elm), \ - (val))) + ((ELFCLASS64 == (elf)->class) \ + ? elf_store_val(elf, ELF__HANDLE_FIELD_TYPE(hdr, e64.elm), \ + ELF_HANDLE_PTRVAL(hdr) + \ + ELF__HANDLE_FIELD_OFFSET(hdr, e64.elm), val) \ + : elf_store_val(elf, ELF__HANDLE_FIELD_TYPE(hdr, e32.elm), \ + ELF_HANDLE_PTRVAL(hdr) + \ + ELF__HANDLE_FIELD_OFFSET(hdr, e32.elm), val))/* Stores a 32/64-bit field. hdr is a HANDLE and elm is the field name. */
tools/xcutils/readnotes.c +6 -0
@@ -162,6 +162,12 @@ static unsigned print_notes(struct elf_bcase XEN_ELFNOTE_PHYS32_ENTRY :print_numeric_note ("PHYS32_ENTRY" , elf , note);break ; +case XEN_ELFNOTE_RANDOMIZE_LIMIT : +print_numeric_note ("RANDOMIZE_LIMIT" , elf, note); +break ; +case XEN_ELFNOTE_RANDOMIZE_STRIDE : +print_numeric_note ("RANDOMIZE_STRIDE" , elf, note); +break ;default :printf ("unknown note type %#x\n" , (unsigned )elf_uval (elf, note, type));
xen/common/libelf/libelf-dominfo.c +59 -0
101 lines not on this page · raw message
xen/common/libelf/libelf-private.h +2 -0
@@ -86,6 +86,8 @@ do { strncpy((d),(s),sizeof((d))-1); (d)[sizeof ((d))-1 ] ='\0' ; \ } while (0) +#define get_random () ((unsigned int)rand()) +#endif #undef memcpy
xen/include/public/elfnote.h +15 -1
@@ -210,9 +210,23 @@#define XEN_ELFNOTE_PHYS32_ENTRY 18/* +* The physical address boundary of the randomization range. The +* other range boundary is the physical address the image got linked +* for (i.e. the lowest ELF program header physical address), and the +* value here may be both below or above that value. +*/ +#define XEN_ELFNOTE_RANDOMIZE_LIMIT 19 + +/* +* The stride to use for base address randomization. A value of zero +* here has the same effect as this note being absent. +*/ +#define XEN_ELFNOTE_RANDOMIZE_STRIDE 20 + +/* * The number of the highest elfnote defined. */ -#define XEN_ELFNOTE_MAX XEN_ELFNOTE_PHYS32_ENTRY +#define XEN_ELFNOTE_MAX XEN_ELFNOTE_RANDOMIZE_STRIDE/* * System information exported through crash notes.
xen/include/xen/libelf.h +2 -0
@@ -435,6 +435,8 @@ struct elf_dom_parms {uint64_t virt_hv_start_low;uint64_t p2m_base;uint64_t elf_paddr_offset; +uint64_t randomize_limit; +uint64_t randomize_stride;uint32_t f_supported[XENFEAT_NR_SUBMAPS ];uint32_t f_required[XENFEAT_NR_SUBMAPS ];uint32_t phys_entry;
_______________________________________________
Xen-devel mailing list
[email protected]
http://lists.xen.org/xen-devel