Message ID | 20240329044459.3990638-13-debug@rivosinc.com (mailing list archive) |
---|---|
State | New |
Headers | show
Return-Path: <owner-linux-mm@kvack.org> 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 6B333C6FD1F for <linux-mm@archiver.kernel.org>; Fri, 29 Mar 2024 04:46:29 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id E59D36B00A7; Fri, 29 Mar 2024 00:46:28 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id E0AA16B00A8; Fri, 29 Mar 2024 00:46:28 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9AA46B00A9; Fri, 29 Mar 2024 00:46:28 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0014.hostedemail.com [216.40.44.14]) by kanga.kvack.org (Postfix) with ESMTP id 946736B00A7 for <linux-mm@kvack.org>; Fri, 29 Mar 2024 00:46:28 -0400 (EDT) Received: from smtpin15.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id 64C5B8113B for <linux-mm@kvack.org>; Fri, 29 Mar 2024 04:46:28 +0000 (UTC) X-FDA: 81948840456.15.A0438E2 Received: from mail-pf1-f181.google.com (mail-pf1-f181.google.com [209.85.210.181]) by imf14.hostedemail.com (Postfix) with ESMTP id 5CF19100002 for <linux-mm@kvack.org>; Fri, 29 Mar 2024 04:46:26 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LiIWK3xI; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687586; a=rsa-sha256; cv=none; b=ScsAGkkInQSXsymPmedNpsR48erA/yXzeVT7oQ8VqNnLRP3by5NcYTp1I4moAv1m5VH4kN aASrqWBBM2TtCfcQlKg2Cxny41c1vB/vnRjq3acC3+ujvM0XNBM6nA/5JB2TvxOIhnWixp nL2Yczem1v8sZrty7RSKuoTcf9AaU2g= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=LiIWK3xI; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.181 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1711687586; h=from:from:sender:reply-to:subject:subject:date:date: message-id:message-id:to:to:cc:cc:mime-version:mime-version: content-type:content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=+4XcZbT52YyPYTbBbJHKVVBDZyhFzr+R5No8SlxzOH0=; b=x0pKGWhywvY6u+wDf7yPl+ZXAYVQxiPw4U92vlEu+rENce1IspkCONuuRuGBLlFu6PTAyC 4bYfU+0FjSbyv+zlPXcGqeuFBRwSO/d4MvxGkndTGP8SSCBgwVtgOcsyPNBl+XedlqA69G cOf8t4QXK8tlaqxpJOuyaARgloQd9yI= Received: by mail-pf1-f181.google.com with SMTP id d2e1a72fcca58-6e6ee9e3cffso1196634b3a.1 for <linux-mm@kvack.org>; Thu, 28 Mar 2024 21:46:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687585; x=1712292385; darn=kvack.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=+4XcZbT52YyPYTbBbJHKVVBDZyhFzr+R5No8SlxzOH0=; b=LiIWK3xIHSEQtX4bxE/j9KpvIYIxpzHqInr4ZwXsKIqdglpEn20VV+GgT9Lyoi+3Hq Heb142SUcujRy+NF73GkkexVj0lrra5Nwwut3vmMwe/xlkKxYq6V1+oAYqFZHk2jFH/H mjxQmVkiA9xIT+PkbVlyGDktVsVNW1pEfu+ISo1qlRN0GJVBv3mP3uTPaEdk41mYcyF8 neDvZkqIENaQvoHNSA7ILBsHlDcNkjZ2LeNMvs9bEj73+P0TYgv8VYUB5PY7arshVf0+ 4RaNIioX7k0KTLhXxN5CnrKxfQgz9mAqfhhkrNs7whn3Mu/1jBg0GPGAA/2oMO9jjPpC M+Hw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687585; x=1712292385; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=+4XcZbT52YyPYTbBbJHKVVBDZyhFzr+R5No8SlxzOH0=; b=oIdglR9swUiCSS6aulgmcgTUzPI+vhmeuPee/PUZ2bgI5GAOzAWQA6pSJLmfZqwTl5 EnBrbA5iVbgNJmrA18vZQrWW5VlHoIlI/vxNFIqN9s/qRptN+WHCBhccPaYIA9Bh6EWy G0hazHZlppDdfSN06r9FUp80Di5jP9B0NM7aczphbB139i6EfHmHM+Z1xRvjeeWmp6BL 6LCNlFhNXFPSUbSMkQSE0yM4bN32FQqv8Xkm2+78pa8B5NwekRdiYtfLVmGUEQjFnFAw 981H+ojbzneLnYzoN/H0MskwazbaG8BSMG2DLd6APDpb2EpU4YdtKPeXrNn3MYFwTDoD 8czg== X-Forwarded-Encrypted: i=1; AJvYcCWsccHWJSv853zWNMnPq0jnzy4EjVX/ESkdc+vmXbFxNIoIhuNcdhYAsk4qgzNOMgI3/LzzHgbhACtNB/haMtVDqHI= X-Gm-Message-State: AOJu0YwD02xQjqVtcnijGct1RKd0EiQP4k06MjJxMB2DUovB/UScp+QU c+YnQsgLQVl7JPlccVYl2n2S9Db+hiLOXkf6w4G4pXeEJn97USxzlVYOeWcaMn0= X-Google-Smtp-Source: AGHT+IGU4KJtKBTHpZlkqt9cg1BlBHp1APuo4lcElwp7mwPxz8+nlm5g5TQdTRKqVuVUt4gfjoQ+Lw== X-Received: by 2002:a05:6a00:a29:b0:6ea:df49:c8b1 with SMTP id p41-20020a056a000a2900b006eadf49c8b1mr2139229pfh.13.1711687585128; Thu, 28 Mar 2024 21:46:25 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id i18-20020aa78b52000000b006ea7e972947sm2217120pfd.130.2024.03.28.21.46.22 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:24 -0700 (PDT) From: Deepak Gupta <debug@rivosinc.com> To: paul.walmsley@sifive.com, rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com, samuel.holland@sifive.com, palmer@sifive.com, conor@kernel.org, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, devicetree@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, tech-j-ext@lists.risc-v.org, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh+dt@kernel.org, krzysztof.kozlowski+dt@linaro.org, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, Liam.Howlett@oracle.com, vbabka@suse.cz, lstoakes@gmail.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, hankuan.chen@sifive.com, greentime.hu@sifive.com, evan@rivosinc.com, xiao.w.wang@intel.com, charlie@rivosinc.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, dbarboza@ventanamicro.com, sameo@rivosinc.com, shikemeng@huaweicloud.com, willy@infradead.org, vincent.chen@sifive.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, gerg@kernel.org, heiko@sntech.de, bhe@redhat.com, jeeheng.sia@starfivetech.com, cyy@cyyself.name, maskray@google.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bgray@linux.ibm.com, mpe@ellerman.id.au, baruch@tkos.co.il, alx@kernel.org, david@redhat.com, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, shr@devkernel.io, deller@gmx.de, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com Subject: [PATCH v2 12/27] riscv mmu: write protect and shadow stack Date: Thu, 28 Mar 2024 21:44:44 -0700 Message-Id: <20240329044459.3990638-13-debug@rivosinc.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20240329044459.3990638-1-debug@rivosinc.com> References: <20240329044459.3990638-1-debug@rivosinc.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Rspamd-Server: rspam08 X-Rspamd-Queue-Id: 5CF19100002 X-Stat-Signature: syi5fqzh5nbgsxmejzmy8wobfa9oydrd X-Rspam-User: X-HE-Tag: 1711687586-232416 X-HE-Meta: U2FsdGVkX1822Og5iaNYfq1KkeBO8K9t4KsxFBC4oYHqJCJ4BwhDiNVHKkaVe7jR8HJqHcJAs8MXFnR8l4sb631cBqbEopg9KkJJqXsjelxDKiPJOorcL4T4Yv/IB0QJt1FabAAUcbBdTCWU3nZni5n8Xh54A7Fz7Aw2TXGp0433zIv8JNmPrtiY/KBHhedn/gYu5+wJnA4S/Sk1e6DFFb4dbyQsB9+jl9ktWjHqiK5YKYxcIyCojRsF06zfFYMzw5SrjkDWNBimRWxaxR01IF7iCq+kkOwWT5C/X441FNZJJ+EGdBlyhAQ081jjoE+qV5Nq3udDCFvrAg3lOu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WEXt1/0NSNwrMzvdCxKdmE75IHbq+keq2X8VcmFVy3i0xQhT81rVjFAHIv5nvcmABzJy8rkotaZIHIIULAJB1dN4/+/0CFNS4= 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: <linux-mm.kvack.org> List-Subscribe: <mailto:majordomo@kvack.org> List-Unsubscribe: <mailto:majordomo@kvack.org> |
Series |
riscv control-flow integrity for usermode
|
expand
|
diff --git a/arch/riscv/include/asm/pgtable.h b/arch/riscv/include/asm/pgtable.h index 9b837239d3e8..7a1c2a98d272 100644 --- a/arch/riscv/include/asm/pgtable.h +++ b/arch/riscv/include/asm/pgtable.h @@ -398,7 +398,7 @@ static inline int pte_special(pte_t pte) static inline pte_t pte_wrprotect(pte_t pte) { - return __pte(pte_val(pte) & ~(_PAGE_WRITE)); + return __pte((pte_val(pte) & ~(_PAGE_WRITE)) | (_PAGE_READ)); } /* static inline pte_t pte_mkread(pte_t pte) */ @@ -581,7 +581,15 @@ static inline pte_t ptep_get_and_clear(struct mm_struct *mm, static inline void ptep_set_wrprotect(struct mm_struct *mm, unsigned long address, pte_t *ptep) { - atomic_long_and(~(unsigned long)_PAGE_WRITE, (atomic_long_t *)ptep); + volatile pte_t read_pte = *ptep; + /* + * ptep_set_wrprotect can be called for shadow stack ranges too. + * shadow stack memory is XWR = 010 and thus clearing _PAGE_WRITE will lead to + * encoding 000b which is wrong encoding with V = 1. This should lead to page fault + * but we dont want this wrong configuration to be set in page tables. + */ + atomic_long_set((atomic_long_t *)ptep, + ((pte_val(read_pte) & ~(unsigned long)_PAGE_WRITE) | _PAGE_READ)); } #define __HAVE_ARCH_PTEP_CLEAR_YOUNG_FLUSH
`fork` implements copy on write (COW) by making pages readonly in child and parent both. ptep_set_wrprotect and pte_wrprotect clears _PAGE_WRITE in PTE. Assumption is that page is readable and on fault copy on write happens. To implement COW on such pages, clearing up W bit makes them XWR = 000. This will result in wrong PTE setting which says no perms but V=1 and PFN field pointing to final page. Instead desired behavior is to turn it into a readable page, take an access (load/store) fault on sspush/sspop (shadow stack) and then perform COW on such pages. This way regular reads would still be allowed and not lead to COW maintaining current behavior of COW on non-shadow stack but writeable memory. On the other hand it doesn't interfere with existing COW for read-write memory. Assumption is always that _PAGE_READ must have been set and thus setting _PAGE_READ is harmless. Signed-off-by: Deepak Gupta <debug@rivosinc.com> --- arch/riscv/include/asm/pgtable.h | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-)