From patchwork Thu Jun 7 14:37:04 2018 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Yu-cheng Yu X-Patchwork-Id: 10452283 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 10AEB60146 for ; Thu, 7 Jun 2018 14:41:18 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id DD3742956A for ; Thu, 7 Jun 2018 14:41:17 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id D89F4296C2; Thu, 7 Jun 2018 14:41:17 +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=-2.9 required=2.0 tests=BAYES_00, MAILING_LIST_MULTI, RCVD_IN_DNSWL_NONE autolearn=unavailable version=3.3.1 Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 76903294FC for ; Thu, 7 Jun 2018 14:41:13 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3E43E6B0270; Thu, 7 Jun 2018 10:40:45 -0400 (EDT) Delivered-To: linux-mm-outgoing@kvack.org Received: by kanga.kvack.org (Postfix, from userid 40) id D61826B0275; Thu, 7 Jun 2018 10:40:44 -0400 (EDT) X-Original-To: int-list-linux-mm@kvack.org X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B18186B0272; Thu, 7 Jun 2018 10:40:44 -0400 (EDT) X-Original-To: linux-mm@kvack.org X-Delivered-To: linux-mm@kvack.org Received: from mail-pg0-f69.google.com (mail-pg0-f69.google.com [74.125.83.69]) by kanga.kvack.org (Postfix) with ESMTP id 572566B0271 for ; Thu, 7 Jun 2018 10:40:44 -0400 (EDT) Received: by mail-pg0-f69.google.com with SMTP id q22-v6so3571186pgv.22 for ; Thu, 07 Jun 2018 07:40:44 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-original-authentication-results:x-gm-message-state:from:to:cc :subject:date:message-id:in-reply-to:references; bh=8hJo05Ybd977ikQjensQbC8efy6x+XrsvyLDESlDpLw=; b=lA4q5/IxXF8cuCG9YubIA/uszU57KLY0vMChDuXw5QaeWsE5Y4bk6+AKiO3RYtAZ+x zcULshGzfULrFNzej1ZK2s5OsfDMpR0hr1R+O3Ly5sZfMaFKDyNqdfo3E1xh/BNM+KCB pfL0/iDyK7Rb7I/FJwZpr+QZ7mkFB8GBwhH/tQ96BWPyGyWPytVpz6AZAVfm8oANp/Mg kJO+Rm66tHKKK5EjmBzh51lhihuBbCpgCUJA4wi4lOmYBUu6BzmaOO8LexIHic5/HBVb pMoEWsksnrL9KOkRehbq+eJT+siqiX46eLZQ3sLh9I6uHjRQscfdNn6p0XkD1iEwuXrG YQjQ== X-Original-Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Gm-Message-State: APt69E2t4KOwQpVUOQhwFCWONSzTxuhDeHPT80jXGIKSvE4nwTNgScnp 4cdV+OvGAW4Wa2iVWmWAn+fKsQtKZ0zrqt5ouTGFlpBEDeUol665nP6PRtt+JgkEeyy5ssygJML DF//j21ghqj5cqaitFI00MCW0u8dcrHzFEfxBRXFDkf0WRK3K3HmxFHiqCk5bpMAgYg== X-Received: by 2002:a62:fcb:: with SMTP id 72-v6mr2046563pfp.231.1528382443932; Thu, 07 Jun 2018 07:40:43 -0700 (PDT) X-Google-Smtp-Source: ADUXVKJm8CNvom2njS4mGdUsEK5ZKSdlpvXyeN2snYu/JlCKKzOkKQ5jQOwT8rNrJvN8/WoSru1B X-Received: by 2002:a62:fcb:: with SMTP id 72-v6mr2046504pfp.231.1528382443065; Thu, 07 Jun 2018 07:40:43 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1528382443; cv=none; d=google.com; s=arc-20160816; b=cFOi3v+EoMoOTqK3P2PWXmDAUlrz0oLOj7I78OcC4NxGLq5D8qlS/0MAmH9H/f5wbq C4dfMcqvBNJ1SK4/4VzranvZuKcdbQiVgWA3sd22IrTph3Ko4RYUA96lWeIRUNLYtphu gr+TRz3Rb4AdwS3Xd9yAz2wQZl0AIMpSrYkogCiLZJu1dW5qhht7C4szmMYs5+ewST+l 2Q13thz3Iwz1MWeIUJRUvdUQMtuMcqonP8CwJrsZ8J5rd5+RBB9D8YKb5X8veqse5lMt VXxRGOz8lg9I4WNN2FyF2GWUhR2LsLXnQZorjIUHyjyeiPYjgf7+vKosuZGPGQ5Cc54C 8XrA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=references:in-reply-to:message-id:date:subject:cc:to:from :arc-authentication-results; bh=8hJo05Ybd977ikQjensQbC8efy6x+XrsvyLDESlDpLw=; b=vYjswk/nzjPDiGv+GR7KurqXPLzM1KQhFfgzBivy9MpsE5nqyVom8743vzqfoMaPeK h/78Oplc8Xk0x8SFZFPaB4yXjXUA/ahw2C7B0/tDZIqoajl34s4/n3ZHcysBbDkAfknx bsYRIOjrPJCa6r5lfiuKLlvO1TVLwDId/i28N311Lq36DhR1zZE4/1WoQY1I+C3SG4Ue QwmIXQAtqWq3stFZftIb87eNyiNuVL/2fTFBiGmbZyx3mQwQ0Kr1CjM+6iLo5gdSQCex klyFzFfD6ezJMihZSPbxD0XtpzIGeyUv4TSJ9jqy3HqmyngCYAuL+uVTx3TWhw52qbcR vObw== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: from mga18.intel.com (mga18.intel.com. [134.134.136.126]) by mx.google.com with ESMTPS id b60-v6si54342625plc.270.2018.06.07.07.40.42 for (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 07 Jun 2018 07:40:43 -0700 (PDT) Received-SPF: pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.126 as permitted sender) client-ip=134.134.136.126; Authentication-Results: mx.google.com; spf=pass (google.com: domain of yu-cheng.yu@intel.com designates 134.134.136.126 as permitted sender) smtp.mailfrom=yu-cheng.yu@intel.com; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga008.jf.intel.com ([10.7.209.65]) by orsmga106.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> X-Bogosity: Ham, tests=bogofilter, spamicity=0.000000, version=1.2.4 Sender: owner-linux-mm@kvack.org Precedence: bulk X-Loop: owner-majordomo@kvack.org List-ID: X-Virus-Scanned: ClamAV using ClamSMTP 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);