From: "Jan Beulich" Subject: [RFC] PV guest kernel address randomization Date: Wed, 01 Jun 2016 02:22:05 -0600 Message-ID: <574EB74D02000078000F03E9@prv-mh.provo.novell.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=__PartD7E1C33D.0__=" Return-path: Received: from mail6.bemta6.messagelabs.com ([85.158.143.247]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1b81Pm-0005w7-VL for xen-devel@lists.xenproject.org; Wed, 01 Jun 2016 08:22:11 +0000 List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" To: xen-devel List-Id: xen-devel@lists.xenproject.org This is a MIME message. If you are reading this text, you may want to consider changing to a mail reader or gateway that understands how to properly handle MIME multipart messages. --=__PartD7E1C33D.0__= Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: quoted-printable Content-Disposition: inline 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 --=__PartD7E1C33D.0__= Content-Type: text/plain; name="libelf-generalize-store-field.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="libelf-generalize-store-field.patch" =0AInstead of elf_load_bsdsyms() cooking its own variant, let's just = make=0Athe generic thing cope with different bitness (paralleling = elf_uval()=0Aand alike).=0A=0A--- a/xen/common/libelf/libelf-loader.c=0A+++= b/xen/common/libelf/libelf-loader.c=0A@@ -273,14 +273,6 @@ static void = elf_load_bsdsyms(struct elf_=0A if ( !elf->bsd_symtab_pstart )=0A = return;=0A =0A-#define elf_store_field_bitness(_elf, _hdr, _elm, _val) = \=0A-do { = \=0A- if ( elf_64bit(_elf) ) = \=0A- elf_store_field(_elf, _hdr, e64._elm, _val); = \=0A- else = \=0A- elf_store_field(_elf, _hdr, e32._elm, _val); = \=0A-} while ( 0 )=0A-=0A #define SYMTAB_INDEX 1=0A #define STRTAB_INDE= X 2=0A =0A@@ -308,16 +300,16 @@ do {=0A elf_uval(elf= , elf->ehdr, e_ehsize));=0A =0A /* Set the offset to the shdr array. = */=0A- elf_store_field_bitness(elf, header_handle, e_shoff,=0A- = offsetof(typeof(header.elf_header), section));=0A+ = elf_store_field(elf, header_handle, e_shoff,=0A+ = offsetof(typeof(header.elf_header), section));=0A =0A /* Set the right = number of section headers. */=0A- elf_store_field_bitness(elf, = header_handle, e_shnum, ELF_BSDSYM_SECTIONS);=0A+ elf_store_field(elf, = header_handle, e_shnum, ELF_BSDSYM_SECTIONS);=0A =0A /* Clear a couple = of fields we don't use. */=0A- elf_store_field_bitness(elf, header_handl= e, e_phoff, 0);=0A- elf_store_field_bitness(elf, header_handle, = e_phentsize, 0);=0A- elf_store_field_bitness(elf, header_handle, = e_phnum, 0);=0A+ elf_store_field(elf, header_handle, e_phoff, 0);=0A+ = elf_store_field(elf, header_handle, e_phentsize, 0);=0A+ elf_store_fiel= d(elf, header_handle, e_phnum, 0);=0A =0A /* Zero the undefined = section. */=0A section_handle =3D ELF_MAKE_HANDLE(elf_shdr,=0A@@ = -354,10 +346,9 @@ do {=0A }=0A =0A /* Adjust the sh_offset and = sh_link of the copied section header. */=0A- elf_store_field_bitness(elf= , section_handle, sh_offset,=0A- symtab_base - = elf_header_base);=0A- elf_store_field_bitness(elf, section_handle, = sh_link,=0A- STRTAB_INDEX);=0A+ elf_store_fie= ld(elf, section_handle, sh_offset,=0A+ symtab_base - = elf_header_base);=0A+ elf_store_field(elf, section_handle, sh_link, = STRTAB_INDEX);=0A =0A /* Calculate the guest address where strtab is = loaded. */=0A strtab_base =3D elf_round_up(elf, symtab_base +=0A@@ = -387,8 +378,8 @@ do {=0A return;=0A }=0A =0A- elf_store_fiel= d_bitness(elf, section_handle, sh_offset,=0A- = strtab_base - elf_header_base);=0A+ elf_store_field(elf, section_handle,= sh_offset,=0A+ strtab_base - elf_header_base);=0A =0A = /* Store the whole size (including headers and loaded sections). */=0A = header.size =3D strtab_base + elf_uval(elf, section_handle, sh_size) = -=0A@@ -409,7 +400,6 @@ do {=0A =0A #undef SYMTAB_INDEX=0A #undef = STRTAB_INDEX=0A-#undef elf_store_field_bitness=0A }=0A =0A void elf_parse_b= inary(struct elf_binary *elf)=0A--- a/xen/include/xen/libelf.h=0A+++ = b/xen/include/xen/libelf.h=0A@@ -303,9 +303,13 @@ bool elf_access_ok(struct= elf_binary * e=0A /* Stores a value at a particular PTRVAL. */=0A =0A = #define elf_store_field(elf, hdr, elm, val) = \=0A- (elf_store_val((elf), ELF__HANDLE_FIELD_TYPE(hdr, elm), = \=0A- ELF_HANDLE_PTRVAL(hdr) + ELF__HANDLE_FIELD_= OFFSET(hdr, elm), \=0A- (val)))=0A+ ((ELFCLASS64 = =3D=3D (elf)->class) \=0A+ ? = elf_store_val(elf, ELF__HANDLE_FIELD_TYPE(hdr, e64.elm), \=0A+ = ELF_HANDLE_PTRVAL(hdr) + \=0A+ = ELF__HANDLE_FIELD_OFFSET(hdr, e64.elm), val) \=0A+ = : elf_store_val(elf, ELF__HANDLE_FIELD_TYPE(hdr, e32.elm), = \=0A+ ELF_HANDLE_PTRVAL(hdr) + = \=0A+ ELF__HANDLE_FIELD_OFFSET(hdr, e32.elm), = val))=0A /* Stores a 32/64-bit field. hdr is a HANDLE and elm is the = field name. */=0A =0A =0A --=__PartD7E1C33D.0__= Content-Type: text/plain; name="libelf-randomize-base.patch" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="libelf-randomize-base.patch" --- a/tools/xcutils/readnotes.c=0A+++ b/tools/xcutils/readnotes.c=0A@@ = -162,6 +162,12 @@ static unsigned print_notes(struct elf_b=0A = case XEN_ELFNOTE_PHYS32_ENTRY:=0A print_numeric_note(= "PHYS32_ENTRY", elf , note);=0A break;=0A+ = case XEN_ELFNOTE_RANDOMIZE_LIMIT:=0A+ print_numeric_note(= "RANDOMIZE_LIMIT", elf, note);=0A+ break;=0A+ = case XEN_ELFNOTE_RANDOMIZE_STRIDE:=0A+ print_numeric_note(= "RANDOMIZE_STRIDE", elf, note);=0A+ break;=0A = default:=0A printf("unknown note type %#x\n",=0A = (unsigned)elf_uval(elf, note, type));=0A--- a/xen/common/lib= elf/libelf-dominfo.c=0A+++ b/xen/common/libelf/libelf-dominfo.c=0A@@ = -109,6 +109,8 @@ elf_errorstatus elf_xen_parse_note(struc=0A = [XEN_ELFNOTE_INIT_P2M] =3D { "INIT_P2M", 0},=0A [XEN_ELFNOTE_PADDR_= OFFSET] =3D { "PADDR_OFFSET", 0},=0A [XEN_ELFNOTE_HV_START_LOW] = =3D { "HV_START_LOW", 0},=0A+ [XEN_ELFNOTE_RANDOMIZE_LIMIT] =3D { = "RANDOMIZE_LIMIT", 0},=0A+ [XEN_ELFNOTE_RANDOMIZE_STRIDE] =3D { = "RANDOMIZE_STRIDE", 0},=0A [XEN_ELFNOTE_XEN_VERSION] =3D { = "XEN_VERSION", 1},=0A [XEN_ELFNOTE_GUEST_OS] =3D { "GUEST_OS", = 1},=0A [XEN_ELFNOTE_GUEST_VERSION] =3D { "GUEST_VERSION", 1},=0A@@ = -203,6 +205,12 @@ elf_errorstatus elf_xen_parse_note(struc=0A case = XEN_ELFNOTE_HV_START_LOW:=0A parms->virt_hv_start_low =3D val;=0A = break;=0A+ case XEN_ELFNOTE_RANDOMIZE_LIMIT:=0A+ = parms->randomize_limit =3D val;=0A+ break;=0A+ case XEN_ELFNOTE_R= ANDOMIZE_STRIDE:=0A+ parms->randomize_stride =3D val;=0A+ = break;=0A =0A case XEN_ELFNOTE_FEATURES:=0A if ( elf_xen_parse_= features(str, parms->f_supported,=0A@@ -404,6 +412,8 @@ static elf_errorsta= tus elf_xen_note_chec=0A static elf_errorstatus elf_xen_addr_calc_check(str= uct elf_binary *elf,=0A struct = elf_dom_parms *parms)=0A {=0A+ uint64_t r =3D 0;=0A+=0A if ( = (parms->elf_paddr_offset !=3D UNSET_ADDR) &&=0A (parms->virt_base = =3D=3D UNSET_ADDR) )=0A {=0A@@ -418,6 +428,9 @@ static elf_errorstatus = elf_xen_addr_calc=0A parms->virt_base =3D 0;=0A elf_msg(elf= , "%s: VIRT_BASE unset, using 0x%" PRIx64 "\n",=0A = __FUNCTION__, parms->virt_base);=0A+=0A+ /* Disable randomization = if we had to guess virt_base. */=0A+ parms->randomize_stride =3D = 0;=0A }=0A =0A /*=0A@@ -439,6 +452,49 @@ static elf_errorstatus = elf_xen_addr_calc=0A __FUNCTION__, parms->elf_paddr_offset)= ;=0A }=0A =0A+ if ( parms->randomize_limit !=3D UNSET_ADDR && = parms->randomize_stride )=0A+ {=0A+ unsigned int i, n;=0A+=0A+ = if ( parms->randomize_limit < elf->pstart )=0A+ r =3D = (elf->pstart - parms->randomize_limit) /=0A+ parms->randomiz= e_stride;=0A+ else if ( parms->randomize_limit > elf->pend )=0A+ = r =3D (parms->randomize_limit - elf->pend) /=0A+ = parms->randomize_stride;=0A+=0A+ r =3D (get_random() % (r + 1)) * = parms->randomize_stride;=0A+=0A+ if ( parms->randomize_limit < = elf->pstart )=0A+ r =3D -r;=0A+=0A+ elf_msg(elf, "%s: = randomization displacement %#" PRIx64 "\n",=0A+ __FUNCTION__= , r);=0A+=0A+ elf->pstart +=3D r;=0A+ elf->pend +=3D r;=0A+ = parms->virt_hypercall +=3D r;=0A+=0A+ /* Adjust program = headers for elf_load_binary() (also see there). */=0A+ n =3D = elf_uval(elf, elf->ehdr, e_phnum);=0A+ for ( i =3D 0; i < n; ++i = )=0A+ {=0A+ ELF_HANDLE_DECL(elf_phdr) phdr =3D elf_phdr_b= y_index(elf, i);=0A+=0A+ if ( !elf_access_ok(elf, ELF_HANDLE_PTR= VAL(phdr), 1) )=0A+ break;=0A+ if ( !elf_phdr_is_= loadable(elf, phdr) )=0A+ continue;=0A+ = elf_store_field(elf, phdr, p_paddr,=0A+ = elf_uval(elf, phdr, p_paddr) + r);=0A+ /*=0A+ * = Don't update p_vaddr: We don't use it, and at least various=0A+ = * 64-bit Linux versions have their per-CPU data outside the=0A+ = * "normal" VA range (and that VA must not be adjusted).=0A+ = */=0A+ }=0A+ }=0A+=0A parms->virt_offset =3D parms->virt_base= - parms->elf_paddr_offset;=0A parms->virt_kstart =3D elf->pstart + = parms->virt_offset;=0A parms->virt_kend =3D elf->pend + parms->virt= _offset;=0A@@ -446,6 +502,8 @@ static elf_errorstatus elf_xen_addr_calc=0A = if ( parms->virt_entry =3D=3D UNSET_ADDR )=0A parms->virt_entry= =3D elf_uval(elf, elf->ehdr, e_entry);=0A =0A+ parms->virt_entry +=3D = r;=0A+=0A if ( parms->bsd_symtab )=0A {=0A elf_parse_bsdsym= s(elf, elf->pend);=0A@@ -504,6 +562,7 @@ elf_errorstatus elf_xen_parse(stru= ct elf=0A parms->p2m_base =3D UNSET_ADDR;=0A parms->elf_paddr_offse= t =3D UNSET_ADDR;=0A parms->phys_entry =3D UNSET_ADDR32;=0A+ = parms->randomize_limit =3D UNSET_ADDR;=0A =0A /* Find and parse elf = notes. */=0A count =3D elf_phdr_count(elf);=0A--- a/xen/common/libelf/l= ibelf-private.h=0A+++ b/xen/common/libelf/libelf-private.h=0A@@ -86,6 = +86,8 @@ do { strncpy((d),(s),sizeof((d))-1);=0A (d)[sizeof((d))-1] = =3D '\0'; \=0A } while (0)=0A =0A+#define get_random() = ((unsigned int)rand())=0A+=0A #endif=0A =0A #undef memcpy=0A--- a/xen/inclu= de/public/elfnote.h=0A+++ b/xen/include/public/elfnote.h=0A@@ -210,9 = +210,23 @@=0A #define XEN_ELFNOTE_PHYS32_ENTRY 18=0A =0A /*=0A+ * The = physical address boundary of the randomization range. The=0A+ * other = range boundary is the physical address the image got linked=0A+ * for = (i.e. the lowest ELF program header physical address), and the=0A+ * value = here may be both below or above that value.=0A+ */=0A+#define XEN_ELFNOTE_R= ANDOMIZE_LIMIT 19=0A+=0A+/*=0A+ * The stride to use for base address = randomization. A value of zero=0A+ * here has the same effect as this = note being absent.=0A+ */=0A+#define XEN_ELFNOTE_RANDOMIZE_STRIDE = 20=0A+=0A+/*=0A * The number of the highest elfnote defined.=0A = */=0A-#define XEN_ELFNOTE_MAX XEN_ELFNOTE_PHYS32_ENTRY=0A+#define = XEN_ELFNOTE_MAX XEN_ELFNOTE_RANDOMIZE_STRIDE=0A =0A /*=0A * System = information exported through crash notes.=0A--- a/xen/include/xen/libelf.h= =0A+++ b/xen/include/xen/libelf.h=0A@@ -435,6 +435,8 @@ struct elf_dom_parm= s {=0A uint64_t virt_hv_start_low;=0A uint64_t p2m_base;=0A = uint64_t elf_paddr_offset;=0A+ uint64_t randomize_limit;=0A+ = uint64_t randomize_stride;=0A uint32_t f_supported[XENFEAT_NR_SUBMAPS];= =0A uint32_t f_required[XENFEAT_NR_SUBMAPS];=0A uint32_t phys_entry= ;=0A --=__PartD7E1C33D.0__= Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: base64 Content-Disposition: inline X19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX19fX18KWGVuLWRldmVs IG1haWxpbmcgbGlzdApYZW4tZGV2ZWxAbGlzdHMueGVuLm9yZwpodHRwOi8vbGlzdHMueGVuLm9y Zy94ZW4tZGV2ZWwK --=__PartD7E1C33D.0__=--