Received: by 2002:a89:d88:0:b0:1fa:5c73:8e2d with SMTP id eb8csp370734lqb; Fri, 24 May 2024 00:16:46 -0700 (PDT) X-Forwarded-Encrypted: i=3; AJvYcCXdZoVXL5FMroZdCVk+1UBQ6zvQXaBqQM3Oi4tqG3tY41xALQX6cqK7nv5+ARPD3MnsTg2cP921TQYrllvv+DOXYFL7rcfuYM7IDyd2dg== X-Google-Smtp-Source: AGHT+IEV0a89fLsKdAVQmiQnER3wHaHJ2nF9kJEugEre/hU3z9AekOl6QF5+9RQoNGzLhdN3XMwn X-Received: by 2002:a37:f50e:0:b0:792:bc86:2d21 with SMTP id af79cd13be357-794ab10f892mr129720285a.57.1716535006623; Fri, 24 May 2024 00:16:46 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1716535006; cv=pass; d=google.com; s=arc-20160816; b=zmxgGawR/ePSznivPascx26WZY5f2vRYdefqBKTQn5VmPGnbGvW0GDy98Q+5MQ5aj9 punoanneldW7qv7bJQnE1Q8P4s4TdbobnoJQDacwsrD2pixdi5gs40buLc9EUimh3Ymn iOtkjdxbuLgfbqgF6H5OvvdAct5JpomEUNJv5MXFYOCRy2ypd/4tR98cVYKnkXkZFesf uPph9AUGwmYw/SCs0B+YSZprg1q/8Ry4MjSkccm7LeaG9ZNyYCglKq2ejATSEjLZsA5L cUeDKiCKNun5ufOv9z+MGV9C+9z5gbY3o6uQTzRi0sIdOCpeZDr5HI6z5ylrzoSIaBaK QMRQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:list-unsubscribe:list-subscribe:list-id:precedence :references:message-id:subject:cc:to:from:date:dkim-signature; bh=G5b7QgiVlAzfw1l+FA1jhqowDIFfn6XCF4+Yt8iGQO0=; fh=7nr4p7vBlvoRQaFv7KmfSQm4ti+68T4aWcRXv2O1Gq8=; b=tGTuL49tLpVYxmLQhaKQpep1XF/R5l7plQAzFBAchjwdA88mJxA0o95UU0HFfhCW1u TMII3cQVa2rbIX5X9Z/lVKhBPwJQbmG3yx6c1cWr4U+UfGmQJaFtYV/+kiibarfk31D1 nb1uIF2iasDz0UTFzfubfNiEfLmRdqxl4Zux0BAlCakyWENPlwSg8siKPITQuY0knwCW KLtdXcTldY7PK3COY+ZzyqHLsXbENGnB2hz96N6mrEXUNjvnupPLG+jLomdiuwRs6njX CIlf+I9Aj/yIdNos+SlNVlyUQSmNVMASU3lvN8xuaBFl6yRIOlKuLRatEmP4oyYf+AMJ D0cg==; dara=google.com ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=y2ijOrSi; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-188389-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-188389-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from ny.mirrors.kernel.org (ny.mirrors.kernel.org. [2604:1380:45d1:ec00::1]) by mx.google.com with ESMTPS id af79cd13be357-794abd15ea4si91799685a.272.2024.05.24.00.16.46 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 00:16:46 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel+bounces-188389-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) client-ip=2604:1380:45d1:ec00::1; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=y2ijOrSi; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-188389-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:45d1:ec00::1 as permitted sender) smtp.mailfrom="linux-kernel+bounces-188389-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ny.mirrors.kernel.org (Postfix) with ESMTPS id 0DFDB1C21474 for ; Fri, 24 May 2024 07:16:46 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id D182612883C; Fri, 24 May 2024 07:16:35 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="y2ijOrSi" Received: from mail-pg1-f176.google.com (mail-pg1-f176.google.com [209.85.215.176]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 280866EB51 for ; Fri, 24 May 2024 07:16:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.215.176 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716534994; cv=none; b=hudWMmLSsD4jmbT2oUEBst0alo/KxEHFKcEJlPqEnKGdtgANWZhhvxHRK4/LS9EKUEVD/5/CSfAvz0HwAmizuBgOS7Sc/8T/ujY+oBIE99jEfEkpY1xHy5ReT5w8M2PO57J7bkccIOwUDc5oHEM6e90Q7grd/C6e22ZQiofFl/M= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1716534994; c=relaxed/simple; bh=L5dtxTCrzKCyV3a12hQ+4vBbPtIXGLsw+Pi3J3zri8U=; h=Date:From:To:Cc:Subject:Message-ID:References:MIME-Version: Content-Type:Content-Disposition:In-Reply-To; b=p+ufz3CzT4yR2awseneWRVTmo4h5W5eoqE2GrK9i9xD606gG1HpW+DZ+TObiBXidD2SpcGMC60PcllM9rQbp3ynqt7cOl5X/QtiBnfs2WfOPTQp62Kpxnh9i4HRjTbK8RRngQFFDewjvSXj6u+MruvQxhuH6L9Vr0f7HhyZAkqM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=y2ijOrSi; arc=none smtp.client-ip=209.85.215.176 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-pg1-f176.google.com with SMTP id 41be03b00d2f7-6818811ef2dso399147a12.0 for ; Fri, 24 May 2024 00:16:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1716534992; x=1717139792; darn=vger.kernel.org; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date:from:to :cc:subject:date:message-id:reply-to; bh=G5b7QgiVlAzfw1l+FA1jhqowDIFfn6XCF4+Yt8iGQO0=; b=y2ijOrSiwn8kujMA0869ALapDPpI42dWw9fvOBWqX8Y8XIpcqYXkUkkubl7IuDmmgr UXgs/FYwWQ6cMuIo093RxaHWEmjtTlpcPKupttpXoO4fgnH95uo/yaxP5aiTe2K0XTK6 OB8Hf49CztFRHosI56opMlAGwTeRKeLa7jDUtXNV3n1p1lK8CwTzFsc2CcTBhW2OQl9c y5qGjjAYEsJqZqRGr4HQu2IUQ11hKqzITyzCBxl3gLhHVB9p4BNs9O6j/HB/qLxm07aS +zTKlMC5xlJAWs8LHA+/ivB1Cb7gpZt6kBbkLyaNpACXt8lTMYOy0j5JSg5MfxhV1IOJ uvgg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1716534992; x=1717139792; h=in-reply-to:content-transfer-encoding:content-disposition :mime-version:references:message-id:subject:cc:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=G5b7QgiVlAzfw1l+FA1jhqowDIFfn6XCF4+Yt8iGQO0=; b=j2yBvu6uB6LyUIo4V6Baub2Xsj0EfP7MN1zXPAtw1AxOcnADreru1a7hXjzCpCzoOl T6lQ4cou2i7WH5UlNdh9GABhUvNRUoqFxzkvEKp3jGLLRCg2V817/oy5wPCn1Hphc2zj 8Od0z5ogg8kjeT79KEVMvJhcsdf7uqxR23nZTmI+M8KfjvbXFRl8c692nt+BPL41WGua nfOPG4p2anmUNqeEIl1DiFThX796ZIAIRLw4fh5LRjJG3e8jGXjnL2xAtHFjdbtTnMkg OBMmDQgnq7q9bnSAtuNZSvOPtLC5JC2AJQWXcOwn1XnsdZDlmRwo0Y19aQP+kbtSBkl4 Vmtw== X-Forwarded-Encrypted: i=1; AJvYcCUHdkLixMuhs2zGEsamJQueXzhDlJKjWRpgXuOyQcMYqCNv3POrq4lgXdsmIP2+NntF6SUZ45/1hA4Uq8vclsyrOtER0MjLE+hGrGPO X-Gm-Message-State: AOJu0Yx4UF5iExsqDs8yK43dCkYI7wrf4hq3JtoHCjXGnbtU5rgqzKGY KkgPQbAgFTut+NN3URlJtIFpn4oM2TUxJ4LN+So8ByReESJz/ilBp1x6EdiOtM8= X-Received: by 2002:a17:90a:ec12:b0:2bf:5992:31ae with SMTP id 98e67ed59e1d1-2bf5ee1cb71mr1385379a91.20.1716534992281; Fri, 24 May 2024 00:16:32 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2bf5f9b28dcsm742083a91.52.2024.05.24.00.16.28 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 24 May 2024 00:16:31 -0700 (PDT) Date: Fri, 24 May 2024 00:16:27 -0700 From: Deepak Gupta To: Alexandre Ghiti Cc: Alexandre Ghiti , 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, bjorn@rivosinc.com, samuel.holland@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, corbet@lwn.net, 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, 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: Re: [PATCH v3 13/29] riscv mmu: write protect and shadow stack Message-ID: References: <20240403234054.2020347-1-debug@rivosinc.com> <20240403234054.2020347-14-debug@rivosinc.com> <276fa17b-cd62-433d-b0ec-fa98c65a46ca@ghiti.fr> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: On Thu, May 23, 2024 at 04:59:30PM +0200, Alexandre Ghiti wrote: >Hi Deepak, > >On Mon, May 13, 2024 at 7:32 PM Deepak Gupta wrote: >> >> On Sun, May 12, 2024 at 06:31:24PM +0200, Alexandre Ghiti wrote: >> >On 04/04/2024 01:35, Deepak Gupta wrote: >> >>`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, >> > >> > >> >I guess you mean "shadow stack pages" here. >> >> Yes I meant shadow stack pages. Will fix the message. >> >> > >> > >> >> 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 >> >>--- >> >> arch/riscv/include/asm/pgtable.h | 12 ++++++++++-- >> >> 1 file changed, 10 insertions(+), 2 deletions(-) >> >> >> >>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; > >Sorry I missed this ^. You need to use ptep_get() to get the value of >a pte. Noted. will fix it. >And why do you need the volatile here? I don't remember the reason. It's probably not needed here. But I am sure I was debugging something and trying everything. And this probably slipped sanitization before sending patches. Will fix it. > >> >>+ /* >> >>+ * 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 >> > >> > >> >Doesn't making the shadow stack page readable allow "normal" loads to >> >access the page? If it does, isn't that an issue (security-wise)? >> >> When shadow stack permissions are there (i.e. R=0, W=1, X=0), then also shadow stack is >> readable through "normal" loads. So nothing changes when it converts into a readonly page >> from page permissions perspective. >> >> Security-wise it's not a concern because from threat modeling perspective, if attacker had >> read-write primitives (via some bug in program) available to read and write address space >> of process/task; then they would have availiblity of return addresses on normal stack. It's >> the write primitive that is concerning and to be protected against. And that's why shadow stack >> is not writeable using "normal" stores. >> >> > > >Thanks for the explanation! > >With the use of ptep_get(), you can add: > >Reviewed-by: Alexandre Ghiti > >Thanks, > >Alex