From patchwork Wed Feb 5 01:22:12 2025 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13960386 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 6417EC02193 for ; Wed, 5 Feb 2025 01:23:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 3FA6F280026; Tue, 4 Feb 2025 20:22:45 -0500 (EST) Received: by kanga.kvack.org (Postfix, from userid 40) id 3AC4D280016; Tue, 4 Feb 2025 20:22:45 -0500 (EST) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 20044280026; Tue, 4 Feb 2025 20:22:45 -0500 (EST) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0011.hostedemail.com [216.40.44.11]) by kanga.kvack.org (Postfix) with ESMTP id F2D3F280016 for ; Tue, 4 Feb 2025 20:22:44 -0500 (EST) Received: from smtpin28.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay02.hostedemail.com (Postfix) with ESMTP id AC7601203C8 for ; Wed, 5 Feb 2025 01:22:44 +0000 (UTC) X-FDA: 83084141448.28.5666638 Received: from mail-pl1-f182.google.com (mail-pl1-f182.google.com [209.85.214.182]) by imf29.hostedemail.com (Postfix) with ESMTP id BC54D120007 for ; Wed, 5 Feb 2025 01:22:42 +0000 (UTC) Authentication-Results: imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=opeys2V5; dmarc=none; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1738718562; a=rsa-sha256; cv=none; b=IicQpgC/mTZHgCyKeoHP9TNoZ6+UHHu8n/ZBPOHUnv5gbCjpBOdqSBQ5BOz58hSBpSi32V ROlquxysHF0y1QfGwFGSEfGxpa9529K3gSHW7rCLEr4K3Rwj6dJqx8A3fjRQ77QIUK9jiX w4RIBs11OoyLU+gBq1Kd9KRz35V6U8U= ARC-Authentication-Results: i=1; imf29.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=opeys2V5; dmarc=none; spf=pass (imf29.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.182 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=hostedemail.com; s=arc-20220608; t=1738718562; 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=tdDNQkSVTKtNTxsV0uSmAnlAiDQNW7ggaTdWphzDp8I=; b=53klCr04m21lmN01Isk8PdeXTUchwj7d1e40mtIxooxA+W2A8+VwQQYE5pOdBbXE4HZ4u8 II0oupujB85LyhQObRQKHaKkpviBM7mxpReFMK1NXBY6M/KqdnMyOIzlB+xgiB8p6yVVBw x5T6k7GGL0Tfp1kO1T9ZXRci3oC4NJY= Received: by mail-pl1-f182.google.com with SMTP id d9443c01a7336-21634338cfdso54760365ad.2 for ; Tue, 04 Feb 2025 17:22:42 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1738718562; x=1739323362; darn=kvack.org; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:from:to:cc:subject:date:message-id :reply-to; bh=tdDNQkSVTKtNTxsV0uSmAnlAiDQNW7ggaTdWphzDp8I=; b=opeys2V5JBt31Y/B4bB8KXRivolpV6wbLBz6Hw/ZL9GifvsOxl5udzmYwPZSCgQ4XC zeyNeDhvWwkliRSpetwWm4nKK50my95hB8R47acTLQwGlUv/9R4heyAVHhvrANth48/3 04jV1RX/Y4jW7rD2mAWvcRqu8cPSaYVL/7P0/6zgB/Sba+YJWzfWru1ShaJuX6MpdqhG Q0Opn53du/SP/+JiBFOxv7Dph1xT3M/vK/hV8v0R+4GUZPesMVbR6I6I/gfzuU5KrVaB gVI2crpJ3JvVnnM7onAI7wsU9ub4Em6U+QKvl16DVL9BvV1DbkOmPE8Vq1lOeeVbx9CR yc0A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1738718562; x=1739323362; h=cc:to:in-reply-to:references:message-id:content-transfer-encoding :mime-version:subject:date:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=tdDNQkSVTKtNTxsV0uSmAnlAiDQNW7ggaTdWphzDp8I=; b=EdQmesvPKfYt4BDbxJ+yAi8tg5GxKsnTcnFDVdMwI50zixl50VWBIKISZQlGGb1hiE 7LpmutoJFPhYQzo96uOXHBgikK3MzkNtJzuyx6diNE1bEK87md1IeMQTJgp93roCgFlC GrdDyv2+WOQ9SE/JYNa2QGTF2covYTZ0AJ/MgyTSLwE9Vt5+DcX1wzsHYFQn1Cl29rPI 8rDpA0qgmnXzkEkvqJO9uZqnKr2VwxUhfQ/8IsOmiQja3/fT0Ie7GstbOAt0lSQjvUFk 168fM8yNwFgxZvFtX4winOp6M+zCVAqK7heogPTfI88AErQbi5xuSlCg9leUrMOg4U8k yqYw== X-Forwarded-Encrypted: i=1; AJvYcCURjbUcvEr8L/HWENgJimN91Lt0YA/H9gre4hjcm3QAVdK6XotSy6YOuWD+1ZJ7NysqoJkj7A8Y5Q==@kvack.org X-Gm-Message-State: AOJu0Yy2tu/3v8rpb1+FgEYg3JtF/GcOpKoZySFOBqdLNsOIvkde1wUR 0Fve1vTIEm6eT6DyvDVwAdpD/Q03scrUI3qkJmChg3o8dLGihcwMaq5y7u1PpW0= X-Gm-Gg: ASbGncvCh9PiWApe20d1YYcN7L+IPl2xZuYHOjTv7Yep55A4MTwtvYmfPWC8ak9Rd+C g9YDBTCX3RB+OXMBDMqaGR8L7hr5Mw7ZjZwMJZXANcZOB61CgNMks7nqncCA5EjyjFk3bjcRhw4 ytgaNr6hat4H7fiAlu2rZZ2EfiChOy5STotbzzMA/vyf92FFccEMyYYX3ZHGDfLWVI/5FgO0XW6 2vsBfFboi/uJg+0tEbraJo+38LzWFNH0KA8jo8ranl8zaFWtTJx94+fZPln94dUoWtBvqA9FMeL 0TLoMiqymUKxjcFOO3uVmpSBJw== X-Google-Smtp-Source: AGHT+IGL7fIu4kBCNVhHF/oYTJgronEm0XH3kZauHP9nqwG/U0J2y3PAuF3fExMjCq+Sq89z969Y4g== X-Received: by 2002:a05:6a21:2d05:b0:1e1:c0d7:7502 with SMTP id adf61e73a8af0-1ede88b872dmr1661901637.37.1738718561611; Tue, 04 Feb 2025 17:22:41 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72fe69cec0fsm11457202b3a.137.2025.02.04.17.22.39 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 04 Feb 2025 17:22:41 -0800 (PST) From: Deepak Gupta Date: Tue, 04 Feb 2025 17:22:12 -0800 Subject: [PATCH v9 25/26] riscv: Documentation for shadow stack on riscv MIME-Version: 1.0 Message-Id: <20250204-v5_user_cfi_series-v9-25-b37a49c5205c@rivosinc.com> References: <20250204-v5_user_cfi_series-v9-0-b37a49c5205c@rivosinc.com> In-Reply-To: <20250204-v5_user_cfi_series-v9-0-b37a49c5205c@rivosinc.com> To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "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 Cc: linux-kernel@vger.kernel.org, linux-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-riscv@lists.infradead.org, devicetree@vger.kernel.org, linux-arch@vger.kernel.org, linux-doc@vger.kernel.org, linux-kselftest@vger.kernel.org, alistair.francis@wdc.com, richard.henderson@linaro.org, jim.shu@sifive.com, andybnac@gmail.com, kito.cheng@sifive.com, charlie@rivosinc.com, atishp@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, alexghiti@rivosinc.com, samitolvanen@google.com, broonie@kernel.org, rick.p.edgecombe@intel.com, Deepak Gupta X-Mailer: b4 0.14.0 X-Rspam-User: X-Rspamd-Queue-Id: BC54D120007 X-Rspamd-Server: rspam10 X-Stat-Signature: k1uyh5sj9sgdjpwyk9muuj3g368791og X-HE-Tag: 1738718562-168053 X-HE-Meta: U2FsdGVkX19OKKAutX3/kyEXKtV7ZQZiz7wafczlEWY0nHh1UcNJqWjWlxZK3YTSUTmUEl7sNMzE4PbvJQIyQ3Hzr31dY4We1COvIqFZiat0r2PbhpR+Gg/Mq6Y2ZD/uEzJybx+uasUggCfaaR4qTfhKGCfJLOiGymols93+RgUMZ91LuNpFBmkCyQGYgt3INvc7O2/fKCR8cd5HXKxDYFarw5QLdISvLre6BsK8CMk87xVPD+vBVbK2pVRBXip+3SHit1Z2QteQ2EJ8NrLU31K4Nud8pidoMyizDknXXU7OYiGfaGXUU0YGX/yCeEjaEJuX+k4xfp38cERJ2CGmzFv1MhGYLoiPC+DOdxM21JdYUvLboWNEp1Am95G9Wgdhf4N/KOGgK6R3HFBWrUDdxE7qYsJ237qzrb7lwPe8KuxbElhdRGdN3C6iB8MHrNkF8UgotM0rYqp2FoVBiI2ANGubDWMVBoiUvYDH6veAWSBJ7Qovj3OFeLpprX4vfUdmHm/yHvX/A1Pdyq/sPcXqq1Fhxx44Jo2b48h2aSbOdB411zkTFU1/7NGXZTz+4VAdW960Bf9nLFtLX450v2W/pz4zPf3SWaLjFIlMNmtBef5e+faIj6nXRFAcFJojWvawxazTl13ZxX1VDPFqG+lOtlm8Q9ceaU/J14egfTx1wZjJO3An5nM2vR41lPJXpKkLDJh9UERKWSLpH5IqBYJAk14vYavqHHedRgFNW5vZY3nRiq1I1OSo+3XpsQ1YgD8uzuHD6vNnC6A81kxZ/NL41HA9K7wM/ntlnP3Jyb3I0AAchaoAO7jOYyiymfgWGhikQSf7RDKVb1PRdX3Oum7C208QFdgj9tjGzXSX87hp99+g0N6VuaWjEU8nMYpf2tt2mJ1yhWPufTdvXY5SbuDUv4P1pvZaIZaeSmRHgxMZ0aB34FvH4F+pNupVlQWFB5JsBqXT2lelh+wi02aXrsW D1qJMRhC D377pWL6B4978lBsTRUJZU6aoN2YXfEeAm9bdf1q4N7Mk58X+aLT/2UTZSxygGvy+/ysDatVlmxGauAFVJSL540miMzyfuteNPD7BxXG2t3T+hltsNScIybVX7aTnYetLfZAus/syMt2wSpslJnOTRzJ/53tzlijhecE5FVLmv8P8iiWW8LHx5jMwjqEAbMse+cIQkV/1OD6vJOxlGTGlI76gh6/sitCkhcSn34LvpOWwn8amo8kKr4CWrl8ovDtcg+pI1SjJb7dxBIfxbitjbsM1DpV8oeDMiQLI+t9r4TkWBnjLywhJ6p9CwMfYFQ8fzYfZODhN3tfm1/dhzC4/hODraXJxVBreT3tI2d8Y4i/A8D5eZb3e2KrcrTvBdhXxesDwYyvlqJDI8o4boJr//vuR5F5Lgtg1boAAqxB156TVsFYBfecZiXAPLFtSnIBuw2VwdkGaRDkCl1Y= 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: Adding documentation on shadow stack for user mode on riscv and kernel interfaces exposed so that user tasks can enable it. Signed-off-by: Deepak Gupta --- Documentation/arch/riscv/index.rst | 1 + Documentation/arch/riscv/zicfiss.rst | 176 +++++++++++++++++++++++++++++++++++ 2 files changed, 177 insertions(+) diff --git a/Documentation/arch/riscv/index.rst b/Documentation/arch/riscv/index.rst index be7237b69682..e240eb0ceb70 100644 --- a/Documentation/arch/riscv/index.rst +++ b/Documentation/arch/riscv/index.rst @@ -15,6 +15,7 @@ RISC-V architecture vector cmodx zicfilp + zicfiss features diff --git a/Documentation/arch/riscv/zicfiss.rst b/Documentation/arch/riscv/zicfiss.rst new file mode 100644 index 000000000000..5ba389f15b3f --- /dev/null +++ b/Documentation/arch/riscv/zicfiss.rst @@ -0,0 +1,176 @@ +.. SPDX-License-Identifier: GPL-2.0 + +:Author: Deepak Gupta +:Date: 12 January 2024 + +========================================================= +Shadow stack to protect function returns on RISC-V Linux +========================================================= + +This document briefly describes the interface provided to userspace by Linux +to enable shadow stack for user mode applications on RISV-V + +1. Feature Overview +-------------------- + +Memory corruption issues usually result in to crashes, however when in hands of +an adversary and if used creatively can result into variety security issues. + +One of those security issues can be code re-use attacks on program where +adversary can use corrupt return addresses present on stack and chain them +together to perform return oriented programming (ROP) and thus compromising +control flow integrity (CFI) of the program. + +Return addresses live on stack and thus in read-write memory and thus are +susceptible to corruption and allows an adversary to reach any program counter +(PC) in address space. On RISC-V ``zicfiss`` extension provides an alternate +stack termed as shadow stack on which return addresses can be safely placed in +prolog of the function and retrieved in epilog. ``zicfiss`` extension makes +following changes: + +- PTE encodings for shadow stack virtual memory + An earlier reserved encoding in first stage translation i.e. + PTE.R=0, PTE.W=1, PTE.X=0 becomes PTE encoding for shadow stack pages. + +- ``sspush x1/x5`` instruction pushes (stores) ``x1/x5`` to shadow stack. + +- ``sspopchk x1/x5`` instruction pops (loads) from shadow stack and compares + with ``x1/x5`` and if un-equal, CPU raises ``software check exception`` with + ``*tval = 3`` + +Compiler toolchain makes sure that function prologue have ``sspush x1/x5`` to +save return address on shadow stack in addition to regular stack. Similarly +function epilogs have ``ld x5, offset(x2)`` followed by ``sspopchk x5`` to +ensure that popped value from regular stack matches with popped value from +shadow stack. + +2. Shadow stack protections and linux memory manager +----------------------------------------------------- + +As mentioned earlier, shadow stack get new page table encodings and thus have +some special properties assigned to them and instructions that operate on them +as below: + +- Regular stores to shadow stack memory raises access store faults. This way + shadow stack memory is protected from stray inadvertant writes. + +- Regular loads to shadow stack memory are allowed. This allows stack trace + utilities or backtrace functions to read true callstack (not tampered). + +- Only shadow stack instructions can generate shadow stack load or shadow stack + store. + +- Shadow stack load / shadow stack store on read-only memory raises AMO/store + page fault. Thus both ``sspush x1/x5`` and ``sspopchk x1/x5`` will raise AMO/ + store page fault. This simplies COW handling in kernel During fork, kernel + can convert shadow stack pages into read-only memory (as it does for regular + read-write memory) and as soon as subsequent ``sspush`` or ``sspopchk`` in + userspace is encountered, then kernel can perform COW. + +- Shadow stack load / shadow stack store on read-write, read-write-execute + memory raises an access fault. This is a fatal condition because shadow stack + should never be operating on read-write, read-write-execute memory. + +3. ELF and psABI +----------------- + +Toolchain sets up :c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_BCFI` for property +:c:macro:`GNU_PROPERTY_RISCV_FEATURE_1_AND` in notes section of the object file. + +4. Linux enabling +------------------ + +User space programs can have multiple shared objects loaded in its address space +and it's a difficult task to make sure all the dependencies have been compiled +with support of shadow stack. Thus it's left to dynamic loader to enable +shadow stack for the program. + +5. prctl() enabling +-------------------- + +:c:macro:`PR_SET_SHADOW_STACK_STATUS` / :c:macro:`PR_GET_SHADOW_STACK_STATUS` / +:c:macro:`PR_LOCK_SHADOW_STACK_STATUS` are three prctls added to manage shadow +stack enabling for tasks. prctls are arch agnostic and returns -EINVAL on other +arches. + +* prctl(PR_SET_SHADOW_STACK_STATUS, unsigned long arg) + +If arg1 :c:macro:`PR_SHADOW_STACK_ENABLE` and if CPU supports ``zicfiss`` then +kernel will enable shadow stack for the task. Dynamic loader can issue this +:c:macro:`prctl` once it has determined that all the objects loaded in address +space have support for shadow stack. Additionally if there is a +:c:macro:`dlopen` to an object which wasn't compiled with ``zicfiss``, dynamic +loader can issue this prctl with arg1 set to 0 (i.e. +:c:macro:`PR_SHADOW_STACK_ENABLE` being clear) + +* prctl(PR_GET_SHADOW_STACK_STATUS, unsigned long *arg) + +Returns current status of indirect branch tracking. If enabled it'll return +:c:macro:`PR_SHADOW_STACK_ENABLE`. + +* prctl(PR_LOCK_SHADOW_STACK_STATUS, unsigned long arg) + +Locks current status of shadow stack enabling on the task. User space may want +to run with strict security posture and wouldn't want loading of objects +without ``zicfiss`` support in it and thus would want to disallow disabling of +shadow stack on current task. In that case user space can use this prctl to +lock current settings. + +5. violations related to returns with shadow stack enabled +----------------------------------------------------------- + +Pertaining to shadow stack, CPU raises software check exception in following +condition: + +- On execution of ``sspopchk x1/x5``, ``x1/x5`` didn't match top of shadow + stack. If mismatch happens then cpu does ``*tval = 3`` and raise software + check exception. + +Linux kernel will treat this as :c:macro:`SIGSEV`` with code = +:c:macro:`SEGV_CPERR` and follow normal course of signal delivery. + +6. Shadow stack tokens +----------------------- +Regular stores on shadow stacks are not allowed and thus can't be tampered +with via arbitrary stray writes due to bugs. Method of pivoting / switching to +shadow stack is simply writing to csr ``CSR_SSP`` changes active shadow stack. +This can be problematic because usually value to be written to ``CSR_SSP`` will +be loaded somewhere in writeable memory and thus allows an adversary to +corruption bug in software to pivot to an any address in shadow stack range. +Shadow stack tokens can help mitigate this problem by making sure that: + +- When software is switching away from a shadow stack, shadow stack pointer + should be saved on shadow stack itself and call it ``shadow stack token`` + +- When software is switching to a shadow stack, it should read the + ``shadow stack token`` from shadow stack pointer and verify that + ``shadow stack token`` itself is pointer to shadow stack itself. + +- Once the token verification is done, software can perform the write to + ``CSR_SSP`` to switch shadow stack. + +Here software can be user mode task runtime itself which is managing various +contexts as part of single thread. Software can be kernel as well when kernel +has to deliver a signal to user task and must save shadow stack pointer. Kernel +can perform similar procedure by saving a token on user shadow stack itself. +This way whenever :c:macro:`sigreturn` happens, kernel can read the token and +verify the token and then switch to shadow stack. Using this mechanism, kernel +helps user task so that any corruption issue in user task is not exploited by +adversary by arbitrarily using :c:macro:`sigreturn`. Adversary will have to +make sure that there is a ``shadow stack token`` in addition to invoking +:c:macro:`sigreturn` + +7. Signal shadow stack +----------------------- +Following structure has been added to sigcontext for RISC-V:: + + struct __sc_riscv_cfi_state { + unsigned long ss_ptr; + }; + +As part of signal delivery, shadow stack token is saved on current shadow stack +itself and updated pointer is saved away in :c:macro:`ss_ptr` field in +:c:macro:`__sc_riscv_cfi_state` under :c:macro:`sigcontext`. Existing shadow +stack allocation is used for signal delivery. During :c:macro:`sigreturn`, +kernel will obtain :c:macro:`ss_ptr` from :c:macro:`sigcontext` and verify the +saved token on shadow stack itself and switch shadow stack.