Received: by 2002:ac0:a5b6:0:0:0:0:0 with SMTP id m51-v6csp2322015imm; Thu, 7 Jun 2018 08:45:43 -0700 (PDT) X-Google-Smtp-Source: ADUXVKKMe2V2HWK23q0wEHLzZ3aoJXy0Ze5Lhp9lp0+BJieq3WGRPT0Sky/cDEyvYZJEhpP3dLZx X-Received: by 2002:a63:6807:: with SMTP id d7-v6mr1962553pgc.7.1528386343431; Thu, 07 Jun 2018 08:45:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528386343; cv=none; d=google.com; s=arc-20160816; b=qcpeo4OVJaOlHXu0HB+PKfJ8fbFg126+mnHow19MXfmQunlRW0q9/y+sM8XYfeLwxp 3ZlDzGjzyTC5Sc+L8FkePO3C/xpQ+RVR0kE5/mmqRZg18AkivOfDwIl9Olmv7AiQlmQF aMXeTHkswC0Z+0TsrNfZQtvs1IHS7b6qYL+DFDl0qLpKGjfycElGDWMGUkoqMPcCjasS Ixcc199ao3IqMDWWyZU6gfFrV4lgd6qkngZAHKAt6LqSQG93OxwnoR0BqH8yrYqjw9Tl Rb3PNiTX4AFMwxpRmq+L2E7Gz0iOp3KofIZmUuaL/RQgUjl3CGWa0vdN/zN9cGYvK/0u 5V6g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:references:in-reply-to:message-id:date :subject:cc:to:from:arc-authentication-results; bh=8hJo05Ybd977ikQjensQbC8efy6x+XrsvyLDESlDpLw=; b=HYL2Cyd9WGZxRGg4mb9tE0Qszag2HhQMyyUgU3J0IuIIjbLaqpZxkFgDnI5e2nC7qU AT+Iy9gzJfyeuSxL4d4gZFb7lCqm/6IiSDzW15JXGpdmIujzqOt/kqqD7c5qcxvR048T IZHaly58LmZSFveOneFOF+X32biJULUsAmdLjqI8/KPP/Sg268lp8jspkCgHKL7PCYhw 9xWYgZY0ZHUbyCi9HyeY3/IteV4+/TqeyqNjvVc7ss/IoZaRP5GeMlkio2YrDWNVp38Y ftuPYSCZ5b5RXJ6Fs8M+IHEi33pTzRZW9Oe+tY82PfA4HBqKtGlh2nCb/Fppg8dZrFWY eOwA== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id f3-v6si16918232pgp.496.2018.06.07.08.45.29; Thu, 07 Jun 2018 08:45:43 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S934542AbeFGOko (ORCPT + 99 others); Thu, 7 Jun 2018 10:40:44 -0400 Received: from mga09.intel.com ([134.134.136.24]:36282 "EHLO mga09.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S934512AbeFGOkj (ORCPT ); Thu, 7 Jun 2018 10:40:39 -0400 X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga102.jf.intel.com with ESMTP/TLS/DHE-RSA-AES256-GCM-SHA384; 07 Jun 2018 07:40:34 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="5.49,486,1520924400"; d="scan'208";a="47542310" Received: from 2b52.sc.intel.com ([143.183.136.51]) by orsmga008.jf.intel.com with ESMTP; 07 Jun 2018 07:40:34 -0700 From: Yu-cheng Yu To: linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , "H.J. Lu" , Vedvyas Shanbhogue , "Ravi V. Shankar" , Dave Hansen , Andy Lutomirski , Jonathan Corbet , Oleg Nesterov , Arnd Bergmann , Mike Kravetz Cc: Yu-cheng Yu Subject: [PATCH 8/9] x86/cet: Handle shadow stack page fault Date: Thu, 7 Jun 2018 07:37:04 -0700 Message-Id: <20180607143705.3531-9-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.15.1 In-Reply-To: <20180607143705.3531-1-yu-cheng.yu@intel.com> References: <20180607143705.3531-1-yu-cheng.yu@intel.com> Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When a task does fork(), its shadow stack must be duplicated for the child. However, the child may not actually use all pages of of the copied shadow stack. This patch implements a flow that is similar to copy-on-write of an anonymous page, but for shadow stack memory. A shadow stack PTE needs to be RO and dirty. We use this dirty bit requirement to effect the copying of shadow stack pages. In copy_one_pte(), we clear the dirty bit from the shadow stack PTE. On the next shadow stack access to the PTE, a page fault occurs. At that time, we then copy/re-use the page and fix the PTE. Signed-off-by: Yu-cheng Yu --- mm/memory.c | 32 +++++++++++++++++++++++++++++--- 1 file changed, 29 insertions(+), 3 deletions(-) diff --git a/mm/memory.c b/mm/memory.c index 01f5464e0fd2..275c7fb3fc96 100644 --- a/mm/memory.c +++ b/mm/memory.c @@ -1022,7 +1022,7 @@ copy_one_pte(struct mm_struct *dst_mm, struct mm_struct *src_mm, * in the parent and the child */ if (is_cow_mapping(vm_flags)) { - ptep_set_wrprotect(src_mm, addr, src_pte); + ptep_set_wrprotect_flush(vma, addr, src_pte); pte = pte_wrprotect(pte); } @@ -2444,7 +2444,13 @@ static inline void wp_page_reuse(struct vm_fault *vmf) flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); entry = pte_mkyoung(vmf->orig_pte); - entry = maybe_mkwrite(pte_mkdirty(entry), vma); + + if (is_shstk_mapping(vma->vm_flags)) + entry = pte_mkdirty_shstk(entry); + else + entry = pte_mkdirty(entry); + + entry = maybe_mkwrite(entry, vma); if (ptep_set_access_flags(vma, vmf->address, vmf->pte, entry, 1)) update_mmu_cache(vma, vmf->address, vmf->pte); pte_unmap_unlock(vmf->pte, vmf->ptl); @@ -2517,7 +2523,11 @@ static int wp_page_copy(struct vm_fault *vmf) } flush_cache_page(vma, vmf->address, pte_pfn(vmf->orig_pte)); entry = mk_pte(new_page, vma->vm_page_prot); - entry = maybe_mkwrite(pte_mkdirty(entry), vma); + if (is_shstk_mapping(vma->vm_flags)) + entry = pte_mkdirty_shstk(entry); + else + entry = pte_mkdirty(entry); + entry = maybe_mkwrite(entry, vma); /* * Clear the pte entry and flush it first, before updating the * pte with the new entry. This will avoid a race condition @@ -3192,6 +3202,14 @@ static int do_anonymous_page(struct vm_fault *vmf) mem_cgroup_commit_charge(page, memcg, false, false); lru_cache_add_active_or_unevictable(page, vma); setpte: + /* + * If this is within a shadow stack mapping, mark + * the PTE dirty. We don't use pte_mkdirty(), + * because the PTE must have _PAGE_DIRTY_HW set. + */ + if (is_shstk_mapping(vma->vm_flags)) + entry = pte_mkdirty_shstk(entry); + set_pte_at(vma->vm_mm, vmf->address, vmf->pte, entry); /* No need to invalidate - it was non-present before */ @@ -3974,6 +3992,14 @@ static int handle_pte_fault(struct vm_fault *vmf) entry = vmf->orig_pte; if (unlikely(!pte_same(*vmf->pte, entry))) goto unlock; + + /* + * Shadow stack PTEs are copy-on-access, so do_wp_page() + * handling on them no matter if we have write fault or not. + */ + if (is_shstk_mapping(vmf->vma->vm_flags)) + return do_wp_page(vmf); + if (vmf->flags & FAULT_FLAG_WRITE) { if (!pte_write(entry)) return do_wp_page(vmf); -- 2.15.1