From patchwork Thu Apr 20 15:26:37 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Jan Beulich X-Patchwork-Id: 9690815 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id A40B860383 for ; Thu, 20 Apr 2017 15:28:55 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 9C726200E7 for ; Thu, 20 Apr 2017 15:28:55 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 90ECD2846C; Thu, 20 Apr 2017 15:28:55 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.2 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_MED autolearn=ham version=3.3.1 Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) (using TLSv1.2 with cipher AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id D508F200E7 for ; Thu, 20 Apr 2017 15:28:54 +0000 (UTC) Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d1Dyg-0007eW-9l; Thu, 20 Apr 2017 15:26:38 +0000 Received: from mail6.bemta6.messagelabs.com ([193.109.254.103]) by lists.xenproject.org with esmtp (Exim 4.84_2) (envelope-from ) id 1d1Dye-0007eP-9b for xen-devel@lists.xenproject.org; Thu, 20 Apr 2017 15:26:36 +0000 Received: from [193.109.254.147] by server-6.bemta-6.messagelabs.com id DF/EE-03920-B23D8F85; Thu, 20 Apr 2017 15:26:35 +0000 X-Brightmail-Tracker: H4sIAAAAAAAAA+NgFtrNIsWRWlGSWpSXmKPExsXS6fjDS1fr8o8 Ig+WvFS2+b5nM5MDocfjDFZYAxijWzLyk/IoE1oznEwILmu0rJn7pZmtgfK7fxcjJISSQJzG/ YSIriM0rYCex+cBsNhBbQsBQ4un762A2i4CqxPOpn8FsNgF1ibZn24HqOThEBAwkzh1NAgkzC 1RItH/YywYSFhYIlZg2OQViup1E37F7YGFeAUGJvzuEIartJBaues48gZF7FkJmFpIMhK0l8f DXLRYIW1ti2cLXzCDlzALSEsv/cUCEHSR6mpYzoSoBsb0lZr6+wrSAkWMVo0ZxalFZapGuoYV eUlFmekZJbmJmjq6hgZlebmpxcWJ6ak5iUrFecn7uJkZgQDIAwQ7GmxsDDjFKcjApifKqzf0R IcSXlJ9SmZFYnBFfVJqTWnyIUYaDQ0mC985FoJxgUWp6akVaZg4wNmDSEhw8SiK8beeB0rzFB Ym5xZnpEKlTjLoc75Z+eM8kxJKXn5cqJc57HGSGAEhRRmke3AhYnF5ilJUS5mUEOkqIpyC1KD ezBFX+FaM4B6OSMO9bkCk8mXklcJteAR3BBHTEWT+wI0oSEVJSDYwtXM17Ptt7mc+Y3jkp+HF 8ouOvhy0+iZbfGI7+iRWWeOS2eWqvetfD9xHZM8W1VbeK6fJrlvHMcbZRONQZ7sApFOaVny6n +NG53+NZqlrR/qzgJRnfbZYZZrm3e94PuVHK0avebRqzZ2L9raR1cx533asu7/8oG+Uq98R5y 2XxZQF/xTUzlViKMxINtZiLihMBG0o2084CAAA= X-Env-Sender: JBeulich@suse.com X-Msg-Ref: server-11.tower-27.messagelabs.com!1492701992!67621021!1 X-Originating-IP: [137.65.248.74] X-SpamReason: No, hits=0.0 required=7.0 tests= X-StarScan-Received: X-StarScan-Version: 9.4.12; banners=-,-,- X-VirusChecked: Checked Received: (qmail 10699 invoked from network); 20 Apr 2017 15:26:34 -0000 Received: from prv-mh.provo.novell.com (HELO prv-mh.provo.novell.com) (137.65.248.74) by server-11.tower-27.messagelabs.com with DHE-RSA-AES256-GCM-SHA384 encrypted SMTP; 20 Apr 2017 15:26:34 -0000 Received: from INET-PRV-MTA by prv-mh.provo.novell.com with Novell_GroupWise; Thu, 20 Apr 2017 09:26:31 -0600 Message-Id: <58F8EF4D0200007800152882@prv-mh.provo.novell.com> X-Mailer: Novell GroupWise Internet Agent 14.2.1 Date: Thu, 20 Apr 2017 09:26:37 -0600 From: "Jan Beulich" To: "xen-devel" References: <58F8EF4D0200007800152882@prv-mh.provo.novell.com> Mime-Version: 1.0 Cc: Andrew Cooper , Julien Grall , Jann Horn Subject: [Xen-devel] [PATCH] x86/mm: also flush TLB when putting writable foreign page reference X-BeenThere: xen-devel@lists.xen.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xen.org Sender: "Xen-devel" X-Virus-Scanned: ClamAV using ClamSMTP Jann's explanation of the problem: "start situation: - domain A and domain B are PV domains - domain A and B both have currently scheduled vCPUs, and the vCPUs are not scheduled away - domain A has XSM_TARGET access to domain B - page X is owned by domain B and has no mappings - page X is zeroed steps: - domain A uses do_mmu_update() to map page X in domain A as writable - domain A accesses page X through the new PTE, creating a TLB entry - domain A removes its mapping of page X - type count of page X goes to 0 - tlbflush_timestamp of page X is bumped - domain B maps page X as L1 pagetable - type of page X changes to PGT_l1_page_table - TLB flush is forced using domain_dirty_cpumask of domain B - page X is mapped as L1 pagetable in domain B At this point, domain B's vCPUs are guaranteed to have no incorrectly-typed stale TLB entries for page X, but AFAICS domain A's vCPUs can still have stale TLB entries that map page X as writable, permitting domain A to control a live pagetable of domain B." Domain A necessarily is Dom0 (DomU-s with XSM_TARGET permission are being created only for HVM domains, but domain B needs to be PV here), so this is not a security issue, but nevertheless seems desirable to correct. Reported-by: Jann Horn Signed-off-by: Jan Beulich x86/mm: also flush TLB when putting writable foreign page reference Jann's explanation of the problem: "start situation: - domain A and domain B are PV domains - domain A and B both have currently scheduled vCPUs, and the vCPUs are not scheduled away - domain A has XSM_TARGET access to domain B - page X is owned by domain B and has no mappings - page X is zeroed steps: - domain A uses do_mmu_update() to map page X in domain A as writable - domain A accesses page X through the new PTE, creating a TLB entry - domain A removes its mapping of page X - type count of page X goes to 0 - tlbflush_timestamp of page X is bumped - domain B maps page X as L1 pagetable - type of page X changes to PGT_l1_page_table - TLB flush is forced using domain_dirty_cpumask of domain B - page X is mapped as L1 pagetable in domain B At this point, domain B's vCPUs are guaranteed to have no incorrectly-typed stale TLB entries for page X, but AFAICS domain A's vCPUs can still have stale TLB entries that map page X as writable, permitting domain A to control a live pagetable of domain B." Domain A necessarily is Dom0 (DomU-s with XSM_TARGET permission are being created only for HVM domains, but domain B needs to be PV here), so this is not a security issue, but nevertheless seems desirable to correct. Reported-by: Jann Horn Signed-off-by: Jan Beulich --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -602,6 +602,20 @@ static inline void guest_get_eff_kern_l1 TOGGLE_MODE(); } +static const cpumask_t *get_flush_tlb_mask(const struct page_info *page, + const struct domain *d) +{ + cpumask_t *mask = this_cpu(scratch_cpumask); + + BUG_ON(in_irq()); + cpumask_copy(mask, d->domain_dirty_cpumask); + + /* Don't flush if the timestamp is old enough */ + tlbflush_filter(mask, page->tlbflush_timestamp); + + return mask; +} + const char __section(".bss.page_aligned.const") __aligned(PAGE_SIZE) zero_page[PAGE_SIZE]; @@ -1266,6 +1280,23 @@ void put_page_from_l1e(l1_pgentry_t l1e, if ( (l1e_get_flags(l1e) & _PAGE_RW) && ((l1e_owner == pg_owner) || !paging_mode_external(pg_owner)) ) { + /* + * Don't leave stale writable TLB entries in the unmapping domain's + * page tables, to prevent them allowing access to pages required to + * be read-only (e.g. after pg_owner changed them to page table or + * segment descriptor pages). + */ + if ( unlikely(l1e_owner != pg_owner) ) + { + const cpumask_t *mask = get_flush_tlb_mask(page, l1e_owner); + + if ( !cpumask_empty(mask) ) + { + perfc_incr(need_flush_tlb_flush); + flush_tlb_mask(mask); + } + } + put_page_and_type(page); } else @@ -2545,13 +2576,7 @@ static int __get_page_type(struct page_i * may be unnecessary (e.g., page was GDT/LDT) but those * circumstances should be very rare. */ - cpumask_t *mask = this_cpu(scratch_cpumask); - - BUG_ON(in_irq()); - cpumask_copy(mask, d->domain_dirty_cpumask); - - /* Don't flush if the timestamp is old enough */ - tlbflush_filter(mask, page->tlbflush_timestamp); + const cpumask_t *mask = get_flush_tlb_mask(page, d); if ( unlikely(!cpumask_empty(mask)) && /* Shadow mode: track only writable pages. */ --- a/xen/arch/x86/mm.c +++ b/xen/arch/x86/mm.c @@ -602,6 +602,20 @@ static inline void guest_get_eff_kern_l1 TOGGLE_MODE(); } +static const cpumask_t *get_flush_tlb_mask(const struct page_info *page, + const struct domain *d) +{ + cpumask_t *mask = this_cpu(scratch_cpumask); + + BUG_ON(in_irq()); + cpumask_copy(mask, d->domain_dirty_cpumask); + + /* Don't flush if the timestamp is old enough */ + tlbflush_filter(mask, page->tlbflush_timestamp); + + return mask; +} + const char __section(".bss.page_aligned.const") __aligned(PAGE_SIZE) zero_page[PAGE_SIZE]; @@ -1266,6 +1280,23 @@ void put_page_from_l1e(l1_pgentry_t l1e, if ( (l1e_get_flags(l1e) & _PAGE_RW) && ((l1e_owner == pg_owner) || !paging_mode_external(pg_owner)) ) { + /* + * Don't leave stale writable TLB entries in the unmapping domain's + * page tables, to prevent them allowing access to pages required to + * be read-only (e.g. after pg_owner changed them to page table or + * segment descriptor pages). + */ + if ( unlikely(l1e_owner != pg_owner) ) + { + const cpumask_t *mask = get_flush_tlb_mask(page, l1e_owner); + + if ( !cpumask_empty(mask) ) + { + perfc_incr(need_flush_tlb_flush); + flush_tlb_mask(mask); + } + } + put_page_and_type(page); } else @@ -2545,13 +2576,7 @@ static int __get_page_type(struct page_i * may be unnecessary (e.g., page was GDT/LDT) but those * circumstances should be very rare. */ - cpumask_t *mask = this_cpu(scratch_cpumask); - - BUG_ON(in_irq()); - cpumask_copy(mask, d->domain_dirty_cpumask); - - /* Don't flush if the timestamp is old enough */ - tlbflush_filter(mask, page->tlbflush_timestamp); + const cpumask_t *mask = get_flush_tlb_mask(page, d); if ( unlikely(!cpumask_empty(mask)) && /* Shadow mode: track only writable pages. */