From patchwork Thu Sep 29 22:29:19 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Edgecombe, Rick P" X-Patchwork-Id: 12994676 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from kanga.kvack.org (kanga.kvack.org [205.233.56.17]) by smtp.lore.kernel.org (Postfix) with ESMTP id B07A8C43217 for ; Thu, 29 Sep 2022 22:30:39 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3AC338D0010; Thu, 29 Sep 2022 18:30:39 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 383D78D000C; Thu, 29 Sep 2022 18:30:39 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 24C0A8D0010; Thu, 29 Sep 2022 18:30:39 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 1693D8D000C for ; Thu, 29 Sep 2022 18:30:39 -0400 (EDT) Received: from smtpin08.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id F0C00C138A for ; Thu, 29 Sep 2022 22:30:38 +0000 (UTC) X-FDA: 79966568556.08.3586AD3 Received: from mga02.intel.com (mga02.intel.com [134.134.136.20]) by imf11.hostedemail.com (Postfix) with ESMTP id 6526540002 for ; Thu, 29 Sep 2022 22:30:37 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1664490637; x=1696026637; h=from:to:cc:subject:date:message-id:in-reply-to: references; bh=epNKIV9ZZ/lINSB0y0Sxnd07+7mgCJcP2nqXsQVHPjA=; b=m56ZyZZWIzCqsVD82xD0VnwJEknaeT4U8nzhTlCewblK2bcrNG16QfDg ifDcNXc9km9T1ocbbgO6WV1gGSWS1Ez/A1vRY4VMSpCNSqsffEcN4qsWl WdU0xeAckTCJJZwXwEbHLTOsJYy7hU55/xcqx1OUnpcfWtrS8Yi0qVfoa i+zgXCsJ/kK9k3SvxXy2BMxdhtUJFaJ/6QK1heIaT4YQ6rjDRLHMKrazC 5z60KgyO8L5Pt3at7RD5hs62RJ1/d8EPqEGIEvEI4LAi3SmJNVlKzn/S+ Wse38ZflAmvq4MGh7v7v4JF31imeRsD16vtzgICcLIOzzrGIIWHhYWDG6 w==; X-IronPort-AV: E=McAfee;i="6500,9779,10485"; a="289207498" X-IronPort-AV: E=Sophos;i="5.93,356,1654585200"; d="scan'208";a="289207498" Received: from fmsmga004.fm.intel.com ([10.253.24.48]) by orsmga101.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2022 15:30:36 -0700 X-IronPort-AV: E=McAfee;i="6500,9779,10485"; a="691016293" X-IronPort-AV: E=Sophos;i="5.93,356,1654585200"; d="scan'208";a="691016293" Received: from sergungo-mobl.amr.corp.intel.com (HELO rpedgeco-desk.amr.corp.intel.com) ([10.251.25.88]) by fmsmga004-auth.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 29 Sep 2022 15:30:28 -0700 From: Rick Edgecombe To: x86@kernel.org, "H . Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H . J . Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V . Shankar" , Weijiang Yang , "Kirill A . Shutemov" , joao.moreira@intel.com, John Allen , kcc@google.com, eranian@google.com, rppt@kernel.org, jamorris@linux.microsoft.com, dethoma@microsoft.com Cc: rick.p.edgecombe@intel.com Subject: [PATCH v2 22/39] mm: Don't allow write GUPs to shadow stack memory Date: Thu, 29 Sep 2022 15:29:19 -0700 Message-Id: <20220929222936.14584-23-rick.p.edgecombe@intel.com> X-Mailer: git-send-email 2.17.1 In-Reply-To: <20220929222936.14584-1-rick.p.edgecombe@intel.com> References: <20220929222936.14584-1-rick.p.edgecombe@intel.com> ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1664490637; a=rsa-sha256; cv=none; b=vZNrHVMbhDsf/3NSj3xYI0QASYOkfYp6DDuVl+ux+XmH/CYEk3k36xh1kjgZPGEq6DOAsF 5mq6KMbSSREHPdId23vMbbDuhr1L+clTi9KUERowzPe5dOcmgCT0O2rAdpjJlZyUQ8pMqj usmUOUwdaQUlIRYydMtJIks/Mhe/DCc= ARC-Authentication-Results: i=1; imf11.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=m56ZyZZW; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1664490637; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:content-type: content-transfer-encoding:in-reply-to:in-reply-to: references:references:dkim-signature; bh=5v/NkEKqPgfcFAdBfY0++rQv21lgwOcZnH+kYMbRqsk=; b=Zy4AUVuJ1F4JhPt4AdZFozEwKCDtip1lPHQ4hm7zgP359WRlZuJi6BLuQQefPkPl+xOSxr X+SpELfSR/mroak9Lsd99czLQoibvukN2A954ofJoIvIXQ9QeYlKgX0B4Z8z2FU/GFGKHD PsjeyZ7AZr7/QoMd/5BovsIxAKaPVEk= X-Stat-Signature: q15y1dpr1ba9af9mzcj5kof9s9dow6fk X-Rspamd-Queue-Id: 6526540002 X-Rspamd-Server: rspam04 Authentication-Results: imf11.hostedemail.com; dkim=none ("invalid DKIM record") header.d=intel.com header.s=Intel header.b=m56ZyZZW; dmarc=pass (policy=none) header.from=intel.com; spf=pass (imf11.hostedemail.com: domain of rick.p.edgecombe@intel.com designates 134.134.136.20 as permitted sender) smtp.mailfrom=rick.p.edgecombe@intel.com X-Rspam-User: X-HE-Tag: 1664490637-811562 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: Shadow stack memory is writable only in very specific, controlled ways. However, since it is writable, the kernel treats it as such. As a result there remain many ways for userspace to trigger the kernel to write to shadow stack's via get_user_pages(, FOLL_WRITE) operations. To make this a little less exposed, block writable GUPs for shadow stack VMAs. Still allow FOLL_FORCE to write through shadow stack protections, as it does for read-only protections. Signed-off-by: Rick Edgecombe --- v2: - New patch arch/x86/include/asm/pgtable.h | 3 +++ mm/gup.c | 2 +- 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/arch/x86/include/asm/pgtable.h b/arch/x86/include/asm/pgtable.h index 7a769c4dbc1c..2e6a5ee70034 100644 --- a/arch/x86/include/asm/pgtable.h +++ b/arch/x86/include/asm/pgtable.h @@ -1633,6 +1633,9 @@ static inline bool __pte_access_permitted(unsigned long pteval, bool write) { unsigned long need_pte_bits = _PAGE_PRESENT|_PAGE_USER; + if (write && (pteval & (_PAGE_RW | _PAGE_DIRTY)) == _PAGE_DIRTY) + return 0; + if (write) need_pte_bits |= _PAGE_RW; diff --git a/mm/gup.c b/mm/gup.c index 5abdaf487460..56da98f3335c 100644 --- a/mm/gup.c +++ b/mm/gup.c @@ -1043,7 +1043,7 @@ static int check_vma_flags(struct vm_area_struct *vma, unsigned long gup_flags) return -EFAULT; if (write) { - if (!(vm_flags & VM_WRITE)) { + if (!(vm_flags & VM_WRITE) || (vm_flags & VM_SHADOW_STACK)) { if (!(gup_flags & FOLL_FORCE)) return -EFAULT; /*