From mboxrd@z Thu Jan 1 00:00:00 1970 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 6CC27C369AB for ; Thu, 24 Apr 2025 12:51:45 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3C24C6B00B3; Thu, 24 Apr 2025 08:51:44 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 34BE16B00B4; Thu, 24 Apr 2025 08:51:44 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1C6596B00B5; Thu, 24 Apr 2025 08:51:44 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0017.hostedemail.com [216.40.44.17]) by kanga.kvack.org (Postfix) with ESMTP id F035A6B00B3 for ; Thu, 24 Apr 2025 08:51:43 -0400 (EDT) Received: from smtpin20.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay10.hostedemail.com (Postfix) with ESMTP id 3BD5BC02D3 for ; Thu, 24 Apr 2025 12:51:44 +0000 (UTC) X-FDA: 83368924128.20.7E4FC67 Received: from mail-wm1-f41.google.com (mail-wm1-f41.google.com [209.85.128.41]) by imf17.hostedemail.com (Postfix) with ESMTP id 542664000F for ; Thu, 24 Apr 2025 12:51:42 +0000 (UTC) Authentication-Results: imf17.hostedemail.com; dkim=pass header.d=ventanamicro.com header.s=google header.b="VR/ib5MG"; dmarc=none; spf=pass (imf17.hostedemail.com: domain of rkrcmar@ventanamicro.com designates 209.85.128.41 as permitted sender) smtp.mailfrom=rkrcmar@ventanamicro.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1745499102; a=rsa-sha256; cv=none; b=C/CqSskfjrZ7jQzRrV6vQixtb/NUxHelTNM505ZbJp5mrD9gHY6Qmq/b1CeRThTQ0i8HHf VTY6s3YwdUV1XD9KOtNzU2KxFQBb5WlEPs7NZELBcA6poCmN4Td7ZaWpSsrIIgubpaKAW3 GL4ZK4OTGYd61thezbwBehlBjLK3dtk= ARC-Authentication-Results: i=1; imf17.hostedemail.com; dkim=pass header.d=ventanamicro.com header.s=google header.b="VR/ib5MG"; dmarc=none; spf=pass (imf17.hostedemail.com: domain of rkrcmar@ventanamicro.com designates 209.85.128.41 as permitted sender) smtp.mailfrom=rkrcmar@ventanamicro.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1745499102; 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-type: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references:dkim-signature; bh=MLukpWT5NNOxSvsbTFh0JQPpU9SQzhdcC+2BWmCpzxc=; b=tuMdOuPGT/A0v/LDUspfBKFd3kf0qXtfDSMVON4Gz6SjIA0epycRR0c9HeiaX2PbFpETi9 BHnu/25E7/zdWZVebWrc7gCwC/fWedM3Ab4d/kUxiuIN46aGpkGQ8ao8v/gw5uvjAjA6d7 m0Uxb1Y5UgUxomPOnACSRglKkiiz0Es= Received: by mail-wm1-f41.google.com with SMTP id 5b1f17b1804b1-43d4ff56136so1387535e9.3 for ; Thu, 24 Apr 2025 05:51:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ventanamicro.com; s=google; t=1745499101; x=1746103901; darn=kvack.org; h=in-reply-to:references:from:to:cc:subject:message-id:date :content-transfer-encoding:mime-version:from:to:cc:subject:date :message-id:reply-to; bh=MLukpWT5NNOxSvsbTFh0JQPpU9SQzhdcC+2BWmCpzxc=; b=VR/ib5MGTXlo9GLKExxxf5/8RovBHRKMOu3n8Ld98XUyRZnLS65Zl8Yu3LPs6/WCzZ 6jgLznhpchUhd7xt2Ud4/M522AhAnW9yyTNUu89L+mearY4Jx8SisoBcV4RAf/c9Q6LH 7tsy0qOUKB2ih0nqTJtmWrDmzAfnf4aDLwn5uMB3WMkbCfu57EOuf3J7tbwzZw83iMou Xf+hNdU7yGtCuyZGVzS71CoiSlK7KB1bPNOqT0RdoWM3bIqz1m2sc0JXwKfCp1n9g/W3 NDlDlmO71rCyPEntZOXKJ04NVRVqzbngPhmQ5XCTGWNOuvGvln0FrBpoBWZRun92walC AQdQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1745499101; x=1746103901; h=in-reply-to:references:from:to:cc:subject:message-id:date :content-transfer-encoding:mime-version:x-gm-message-state:from:to :cc:subject:date:message-id:reply-to; bh=MLukpWT5NNOxSvsbTFh0JQPpU9SQzhdcC+2BWmCpzxc=; b=t5hY8+Q6K2Wv/5kkRX/b2fC8jnFf8E6fP9j+nqIwndhDgcv3PEvmoN71yTx/RRuiF1 ADBOTmQPxbndc53PgDjzQBXFiGGtRqptcMW+rL/gDWaAuCrGazzsaGh+M1IZTUQeGdD1 Blckj+tmrpANyLmG/iu2ep+Z2uO7po9EdRFwc54lgR8xwG73ZMS5hjAoOI/D8GuHSkMi WvgbzNn+TylZRuGUXo8CbrIybK1Tt7NwZ/SIgofwWLsl8OBrP+qSqkcwAJaArNanpu6v qaqU0qiOT7YSWKRhijsISC1OxYGXyCIDA2AdVtmd94zwRkZacGs2pEwpzFinbAGjGUba yKkA== X-Forwarded-Encrypted: i=1; AJvYcCVbqgABKsmDcTRbSEUy2vOOTsVtrpnEu/LqRmBKvzIYhTnWcip272lyGC2QtHfvUvz/EitYNCqfJg==@kvack.org X-Gm-Message-State: AOJu0YwOjK2ij7HE9Pf/hbxQMQwWRhGB8Yt2Uv4jgk3wpSO86/spBV3B OpURAvWjVdumuaPVmoetZp/Ppal1XE+RCXQTgVJXp+w0pRC3UJCF0qEFKyLORJQ= X-Gm-Gg: ASbGncvjlMmPPVh2coP53YVVwRXMX4oZO/PH7ChNyex8re2fgSJ6xRALs77pO7ggS02 HniHWhKPn/SZW/l8WbO4AOAMFdDmtUKjgNJe7YsiPq17/2juwsInQKRNLPryGPlW7mY7QR+7Rcp jZ5KxnL9QEMLfXkNdJTCPyPMeZqEWHf+SLPnSe8Q9WQln1A5uSwylD+1v0l9ADFe9lGPZHqTVJU Po4+3pvvstSHREMF3mNZkJ6ZaL1MECubJnE/zIXSaXyHwmtGmx43Fo1qaMlr957W7jIYrD+4vJS 78kUmUpMi65FjUWlQm1HtCmQC9y7ujzY/AvffnZiRNpMZYKR X-Google-Smtp-Source: AGHT+IEM7f4Ug05qJLQixhJCwrNUQhZVnfUcKsq0i+OggaoW7x6aAvFNMmOAKB/VOVrygSxaqnTFQw== X-Received: by 2002:a05:600c:3d89:b0:439:8c80:6aee with SMTP id 5b1f17b1804b1-4409bd8d808mr9055605e9.4.1745499100564; Thu, 24 Apr 2025 05:51:40 -0700 (PDT) Received: from localhost ([2a02:8308:a00c:e200:b30c:ee4d:9e10:6a46]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4409d29b8efsm20710185e9.6.2025.04.24.05.51.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 24 Apr 2025 05:51:40 -0700 (PDT) Mime-Version: 1.0 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=UTF-8 Date: Thu, 24 Apr 2025 14:51:39 +0200 Message-Id: Subject: Re: [PATCH v12 10/28] riscv/mm: Implement map_shadow_stack() syscall Cc: "Thomas Gleixner" , "Ingo Molnar" , "Borislav Petkov" , "Dave Hansen" , , "H. Peter Anvin" , "Andrew Morton" , "Liam R. Howlett" , "Vlastimil Babka" , "Lorenzo Stoakes" , "Paul Walmsley" , "Palmer Dabbelt" , "Albert Ou" , "Conor Dooley" , "Rob Herring" , "Krzysztof Kozlowski" , "Arnd Bergmann" , "Christian Brauner" , "Peter Zijlstra" , "Oleg Nesterov" , "Eric Biederman" , "Kees Cook" , "Jonathan Corbet" , "Shuah Khan" , "Jann Horn" , "Conor Dooley" , , , , , , , , , , , , , , , , , , , , , , "Zong Li" , "linux-riscv" To: "Deepak Gupta" From: =?utf-8?q?Radim_Kr=C4=8Dm=C3=A1=C5=99?= References: <20250314-v5_user_cfi_series-v12-0-e51202b53138@rivosinc.com> <20250314-v5_user_cfi_series-v12-10-e51202b53138@rivosinc.com> In-Reply-To: X-Rspamd-Queue-Id: 542664000F X-Rspam-User: X-Rspamd-Server: rspam07 X-Stat-Signature: 3na58asm4zzyagdpzszmidnq8st1ybar X-HE-Tag: 1745499102-628999 X-HE-Meta: U2FsdGVkX1+BkM6tEWhKnmoirObgp8YpQTr4UrtlWx7OjvRFV5cXteR5lUsP5uPcOHofFOaczfM7iVezA2ch4jS/Lhv2EJE/zVnaNWhybVRZPfTKkV2ykpJ6Pal2WTC9nnIS3gzdjlxLjNKvciNaI5d3x3gPqmmaIgCd9ERCqDVJY5WsvTcKGlsyzZW8JQ0jxLZao5rXCEQ87u2l55GX5/0C3l+7uRGTV28dlsYzIwO72Et98fh3C31CYqmA33i4nOJvyW5ie6lG+DvyGVlNOdH3yx0zAagJDcZ64VXR6xW4YEvCurJIH5F9E7fegthRv2oWoONYI5Z4gj10N6NO0is8+Lie1glmNiKDiPT9prVs2sA+uN27xzBECxaY0WwMPs8C++CfXTjhSd4svmJ+qlOKPPTHV85wfocAkion1vHlt2kRu/YbKY6M9PiTD/OtJofO+IhtCE5qt2S2Tq2rPSk8hQzJnZJ4IBrEP+wIFMt27ZcqyT1A5FPnpgsQI18qZmgSSH+UTAJwq8t6vnFwxQR4wmL+l6+9vTnfUWHttCNVl1MskvjUpmzxRZV0Q6CG/lRluhqvja7CxNLhZagDkshC6BsjslvcyROcp5XSLKC4Q6VAeBNysl75DTDtHXBa6y4DfEbUiKieERUcpAxzOv4/vZaxBAb9jh1AxivoyS9PrQMxZleyrDkMSL4KyVhHqKv+37qwc8r9jGq1eNvnW0S0Q4LHOkwto+HJu62QWbWSPjn7R4lKyDGBadMcmn2/pmgEXEWyHnsCxCO2ODPzaxWaNvc4K3uStYztqGhraekjdQ5T5lzMSjoLf+oyZsINGWW1VOG//6yFJLH33nPFdbQvAXhArlZb9DphCjay4y1rSfAacRGW0+nLpp+e+d4QbEWSDASQ6UG14W7IalZdjbpNjAjOF5x8NVw2D4zQ4NHi3PLfy4tUs5JtGEhNVdY6C4OZsyy+ARX1Hc+RKFC uMdppSY3 jlZZ942O9apMwKqLidHxgyJ/t/fwPGdIj7uRCOx1uKkURune1S3akDLdXo1Xm01DefMuSzP9de2BV9IpmVcUaLcX5BL+ZC+RI7w3bFpjTjRLUJo9te2u9uLlrUZhCsCT5t8Lyl+6tJVUovdhNJGvlkvExjU9kuo19ku96G4SjYEkCaGMcdBrTvlfKqXf8H7eMxOh4jbSdrXRwpEhW4RaU32QsTMNrti3Pp1JFU9H0tyeBY+HU47GAM+kB1gJHyLFnubKILQyotWiTFjZMb1Mj+rYFpzKIeLRYL1Ir9kdoP+KAsG8CY5HZpHhwHIRl4jE+nRTrIGG0QIDxt/jw5ai+OngW7TRc+kV+78hmhPG3vVMAMvAMuU/LyXyTC0tPZ7BTdNKkrDKGlHz1H0GOF/Pia7onCHduWxmLuPYogy6mtsIy0lEIA9rg3im+rA5bGHB+ahHRwZhFTnSRBhH2Q/VgicVs+AGlghfuzZRYRzFl3Tb6Iuq+3i204d7q8r74jujizzvkZEJpzEZOs7XpPanX8gsqxzIswhJs1cin2Z51S9DK+3UvD/rUExRfVlnEftKDy41vp00chVM8m/U0hZ0zc6/S4FIPwaJusS++2T24S9olTT/f8qEwmyiqDeRRElh7pvo9wYmk7sbmR+4pThO936oK3GeOgo3JhADOTtVuDlYid08169+TuGx/UqraDXHEBzcFsywkpqDCbYo= 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: List-Subscribe: List-Unsubscribe: 2025-04-23T20:16:58-07:00, Deepak Gupta : > On Thu, Apr 10, 2025 at 11:56:44AM +0200, Radim Kr=C4=8Dm=C3=A1=C5=99 wro= te: >>2025-03-14T14:39:29-07:00, Deepak Gupta : >>> As discussed extensively in the changelog for the addition of this >>> syscall on x86 ("x86/shstk: Introduce map_shadow_stack syscall") the >>> existing mmap() and madvise() syscalls do not map entirely well onto th= e >>> security requirements for shadow stack memory since they lead to window= s >>> where memory is allocated but not yet protected or stacks which are not >>> properly and safely initialised. Instead a new syscall map_shadow_stack= () >>> has been defined which allocates and initialises a shadow stack page. >>> >>> This patch implements this syscall for riscv. riscv doesn't require tok= en >>> to be setup by kernel because user mode can do that by itself. However = to >>> provide compatibility and portability with other architectues, user mod= e >>> can specify token set flag. >> >>RISC-V shadow stack could use mmap() and madvise() perfectly well. > > Deviating from what other arches are doing will create more thrash. I exp= ect > there will be merging of common logic between x86, arm64 and riscv. Infac= t I > did post one such RFC patch set last year (didn't follow up on it). Using > `mmap/madvise` defeats that purpose of creating common logic between arch= es. > > There are pitfalls as mentioned with respect to mmap/madivse because of > unique nature of shadow stack. And thus it was accepted to create a new s= yscall > to create such mappings. RISC-V will stick to that. Ok. >>> diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c >>> +static noinline unsigned long amo_user_shstk(unsigned long *addr, unsi= gned long val) >>> +{ >>> + /* >>> + * Never expect -1 on shadow stack. Expect return addresses and zero >>> + */ >>> + unsigned long swap =3D -1; >>> + __enable_user_access(); >>> + asm goto( >>> + ".option push\n" >>> + ".option arch, +zicfiss\n" >> >>Shouldn't compiler accept ssamoswap.d opcode even without zicfiss arch? > > Its illegal instruction if shadow stack aren't available. Current toolcha= in > emits it only if zicfiss is specified in march. Oof, I'll look why arch is being used like that, thanks. (I thought arch is only for compiler generated code, so assembly mnemonics would always be defined if the compiler knows them.) >> >>> + ".option pop\n" >>> + : [swap] "=3Dr" (swap), [addr] "+A" (*addr) >>> + : [val] "r" (val) >>> + : "memory" >>> + : fault >>> + ); >>> + __disable_user_access(); >>> + return swap; >>> +fault: >>> + __disable_user_access(); >>> + return -1; >> >>I think we should return 0 and -EFAULT. >>We can ignore the swapped value, or return it through a pointer. > > Consumer of this detects -1 and then return -EFAULT. > We would eventually need this when creating shadow stack tokens for > kernel shadow stack. I believe `-1` is safe return value which can't > be construed as negative kernel address (-EFAULT will be) I believe it as well, but I don't see a reason why we need to risk it when we can return the stack value though a pointer and have simple success/failure return value. >>> +} >>> + >>> +static unsigned long allocate_shadow_stack(unsigned long addr, unsigne= d long size, >>> + unsigned long token_offset, bool set_tok) >>> +{ >>> + int flags =3D MAP_ANONYMOUS | MAP_PRIVATE; >> >>Is MAP_GROWSDOWN pointless? > > Not sure. Didn't see that in x86 or arm64 shadow stack creation. > Let me know if its useful. It is for automated growing of the stack. I think that the default stack is pointlessly large already, and if other arches don't do it, so we can probably follow their design here as well... >>> + struct mm_struct *mm =3D current->mm; >>> + unsigned long populate, tok_loc =3D 0; >>> + >>> + if (addr) >>> + flags |=3D MAP_FIXED_NOREPLACE; >>> + >>> + mmap_write_lock(mm); >>> + addr =3D do_mmap(NULL, addr, size, PROT_READ, flags, >> >>PROT_READ implies VM_READ, so won't this select PAGE_COPY in the >>protection_map instead of PAGE_SHADOWSTACK? > > PROT_READ is pointless here and redundant. I haven't checked if I remove = it > what happens. > > `VM_SHADOW_STACK` takes precedence (take a look at pte_mkwrite and pmd_mk= write. > Only way `VM_SHADOW_STACK` is possible in vmflags is via `map_shadow_stac= k` or > `fork/clone` on existing task with shadow stack enabled. > > In a nutshell user can't specify `VM_SHADOW_STACK` directly (indirectly v= ia > map_shadow_stack syscall or fork/clone) . But if set in vmaflags then it'= ll > take precedence. Yeah, I don't like that ugly special case at all, so I was hoping we could somehow avoid it. :)