From patchwork Fri Mar 29 04:44:57 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610069 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 23391CD1283 for ; Fri, 29 Mar 2024 04:47:09 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id AB15A6B00C1; Fri, 29 Mar 2024 00:47:08 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id A61516B00C2; Fri, 29 Mar 2024 00:47:08 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 88EDC6B00C3; Fri, 29 Mar 2024 00:47:08 -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 68C3F6B00C1 for ; Fri, 29 Mar 2024 00:47:08 -0400 (EDT) Received: from smtpin04.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay07.hostedemail.com (Postfix) with ESMTP id 3637916112D for ; Fri, 29 Mar 2024 04:47:08 +0000 (UTC) X-FDA: 81948842136.04.3C082AF Received: from mail-oa1-f51.google.com (mail-oa1-f51.google.com [209.85.160.51]) by imf15.hostedemail.com (Postfix) with ESMTP id 5B15AA000A for ; Fri, 29 Mar 2024 04:47:06 +0000 (UTC) Authentication-Results: imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=YYsi9OhF; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.160.51 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=1711687626; 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=f5ifazHbpOl/UA3v79b+PzDDuljcd/dBpmVrUfFdj7c=; b=wdskGhMGnMlN/YKkj2szqXcZLbAMxJ0FN2W+7XAotuHK7gEwV77k+ISEpomsftGZQRjc7Q CasabfJuB6qmoTn85gONgv+guzZ3wiw8MWgdvqmTNVzb9lYK1HknVuQN6p9G8NhxPi4KOD EZpcUuzio4gHhomqAxGIsBzV7cY0Tbs= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687626; a=rsa-sha256; cv=none; b=z6oWamhsejVfA4UA4sMSXp3zvgNRXNNTmAWClCogA7s50jYc4lVupCr+XahyYFCG/7+qMy zVdn9vg/CFkF/qQJKL/o2ucj3iQ+cEFswHyAVwZG2hpke/eOb2mCjQ3GkQELzoqa2sd3PC PX3nlRHsiuleUDybe73bpT2BGEQ6wmU= ARC-Authentication-Results: i=1; imf15.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=YYsi9OhF; spf=pass (imf15.hostedemail.com: domain of debug@rivosinc.com designates 209.85.160.51 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none Received: by mail-oa1-f51.google.com with SMTP id 586e51a60fabf-2218a0f55e1so924401fac.1 for ; Thu, 28 Mar 2024 21:47:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687625; x=1712292425; 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=f5ifazHbpOl/UA3v79b+PzDDuljcd/dBpmVrUfFdj7c=; b=YYsi9OhF+9WxwhS5YRta8QxW3PLPzsF8BLo557Mv0Cv3Xx5ECM7jpVhLJop3JGrwbD iMYHELz2j9ftF4ww++5BuAwK4S8N/KjDF/0XuJd+f7EnbaOyYPvQv2Fl5Cgwe4eDZ6Gt LmDYju/LktsQ+HFkOjbV6gL187Gpeu2Kqvg6pGdYztAwMrqB/5Rkwjz68ENxbPjCQxFT wfWCboJFTcMvA1hBI5Y1SWyB4i2Q9fVWC1pncqWWZtDg3/u6DksEyjDeHZuP6+REQ06q aDaDEUIu34s4L5jf3nHdvN32BxmrQNg5/vQV/CrRylmH+cFCTcTJsnlVJONsaokAzHhF TG7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687625; x=1712292425; 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=f5ifazHbpOl/UA3v79b+PzDDuljcd/dBpmVrUfFdj7c=; b=El+td3JUIRNY1Vv/AKplfJNKex7z2Ax1/bBRN955je2hCLHktKRmj5hGyscfu4aApy AlWfxwS+6bkXHvF6z8kAhclwzssODSTC8jZtW0PofMZIjqp/DnZp0/tc1m2MJvyUFBdw 0kxyMaks14SvvmALtLfwxAhaD6NR7AZ0/OTODXohgUT0GLX5FMyi8Wt6PfxPvq5yI0a5 KVWOJDFQzs5fL34JfQ0pVfIhYlMR+aDUuzW8NAq8XjrlmbDZoudhcvPnBksDuaNB2+Ha pr1vqMTBxFyNnuSl+ZuvsMF8/y46z8je8wmlUIOcbwUDJ4z6eFhox0K1KLjkihPjhkj1 8pkw== X-Forwarded-Encrypted: i=1; AJvYcCVUnW0tTrPTmhWqHrYvv/T6FA1vmzdjB/JzhyElSAqlzoi9eMlC1GPckd3eBKAmUgzl1r5uZzEUSQv0kCVNocavqK0= X-Gm-Message-State: AOJu0YwE205g9Aj47R4GgUDPDGvGAT/dpwI+Cej/mbF7ff+BMipM+cQu cvde+/7TsCmvWbCnCt4CXCrBmccxZ3zaU/ehx3ALyyN+N2p6/7yhm1Narw+iL+0= X-Google-Smtp-Source: AGHT+IErdAfZ2YKd7ZlzSeS592zYA4TFUt0LCB93K8r2LIl39Tat2IOKwkbBJaRLu5ZvxofXUIE51w== X-Received: by 2002:a05:6870:3912:b0:229:819a:d2e6 with SMTP id b18-20020a056870391200b00229819ad2e6mr1199667oap.15.1711687625326; Thu, 28 Mar 2024 21:47:05 -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.47.02 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:47:04 -0700 (PDT) From: Deepak Gupta 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 25/27] riscv: Documentation for landing pad / indirect branch tracking Date: Thu, 28 Mar 2024 21:44:57 -0700 Message-Id: <20240329044459.3990638-26-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 X-Stat-Signature: j9kj1co1a9z5obo95zwr8sopgp8qw9yz X-Rspamd-Server: rspam10 X-Rspamd-Queue-Id: 5B15AA000A X-Rspam-User: X-HE-Tag: 1711687626-659119 X-HE-Meta: U2FsdGVkX19GOjeeDgh+fYTunbL2O9mqbbEoOHlTYeCUeuHVY3MQ7FaQPJkl22f4jUGksX0/ifzI1j8Dm1t5vkUnB/1UJ7xvtctMmb44yUpi0+wpkVe8/kAbA+sF8dudlKzMiWjPSLD71Ws4hy8NigOuLBIT28xAHmJKxDHTML/4gh3LG/yHIUcRvqsfck6/fycMoOy8bKcPgwiJNZU7cqvnan1zu63Dh/JP+4IMEUFe85q0UeMO2vyMQoYJhYOoldqDCUarZwB9LhSVXDmQuS2fao3b/qtj/j0J+hIiIDzFLeeMxiLpoMV3aZ/23DacL25sDT2Laujp/kfI8218iOvvvBPlUdhQSNc9GcMi4QhAzpYClBAgLJpETrWWEDfh7F6RmDrLBbO1cMughLIyIDANCLK63ndLNpKJjUD/4CFBWOA8w+5W2ori9dFwiAvrZCDwIoLBplOV9gXR3leSBnFO8y+X2ePGo5j8nxknY8c69JKrHjGJj8cohxBZpXyd+DTV1JQhQhtPFuY8BHNShktovFuth/fCh81nlMaKiEl94mTkZAAdF9L8XDIiWrFLUtxOedof7vv+N7QGwTCs69g7zlknqX7KZL27sPCfVVqbtLuybVE0lQOgginUJExap0DqCy4xvfEoAG+49UYXkNCaattjoqpcABC3anzwuIUTOKRfcxWMhmDmZHmO+DZtz2smq8SDZLldp/Mg80SP/6O9kihNv1Q4pp8XCxbOdikx7+Ma6Jnjc12yJ8v6eyTkbmt+RZSeHsNCI5DUg+JqFk1H0XZchkk4mFWaS1Z67jrjhgUJ87PdkhLJbsyC/rxihN8kWVT9S7/xTGlav8YRY6isy7YirLV7ZWdWN+5adDVjNiSLrlXK8cWZfmUNZRY0z+6ysGPA0Mt5lp8NAVERIt7E/Jm9yrwzzmwYKjIouODRMWXj347KiXyFg38roi+dbRZPKmTjy9eZUN4hW90 4BwjJTtW 1FS934aK1ueGsy0vEElwrtRlxuA7S9zXvGnEsMANmMMKOhiYLXhb61QzM0/om14t0YTIQAhb3bJ2l5bpEooy+txZtvdtvy72vEWwQGcW7Sif+sSJcnMIOq3IFdT9W4nUb7pTLxjiXRJzvLG188LehiSzJmyhOLEsFBFSLXLxiBke39sbrurMaBdd+3m75tso61G4lDzmlQNzM1FiEAkprBsYi0f+MjBjXrAykbNM5vDIKmmw= X-Bogosity: Ham, tests=bogofilter, spamicity=0.000020, 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 landing pad aka indirect branch tracking on riscv and kernel interfaces exposed so that user tasks can enable it. Signed-off-by: Deepak Gupta --- Documentation/arch/riscv/zicfilp.rst | 104 +++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 Documentation/arch/riscv/zicfilp.rst diff --git a/Documentation/arch/riscv/zicfilp.rst b/Documentation/arch/riscv/zicfilp.rst new file mode 100644 index 000000000000..3007c81f0465 --- /dev/null +++ b/Documentation/arch/riscv/zicfilp.rst @@ -0,0 +1,104 @@ +.. SPDX-License-Identifier: GPL-2.0 + +:Author: Deepak Gupta +:Date: 12 January 2024 + +==================================================== +Tracking indirect control transfers on RISC-V Linux +==================================================== + +This document briefly describes the interface provided to userspace by Linux +to enable indirect branch tracking 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 function pointers and chain them together to perform jump oriented +programming (JOP) or call oriented programming (COP) and thus compromising control +flow integrity (CFI) of the program. + +Function pointers live 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 zicfilp extension enforces a restriction on such indirect control transfers + + - indirect control transfers must land on a landing pad instruction `lpad`. + There are two exception to this rule + - rs1 = x1 or rs1 = x5, i.e. a return from a function and returns are + protected using shadow stack (see zicfiss.rst) + + - rs1 = x7. On RISC-V compiler usually does below to reach function + which is beyond the offset possible J-type instruction. + + "auipc x7, " + "jalr (x7)" + + Such form of indirect control transfer are still immutable and don't rely + on memory and thus rs1=x7 is exempted from tracking and considered software + guarded jumps. + +`lpad` instruction is pseudo of `auipc rd, ` and is a HINT nop. `lpad` +instruction must be aligned on 4 byte boundary and compares 20 bit immediate with x7. +If `imm_20bit` == 0, CPU don't perform any comparision with x7. If `imm_20bit` != 0, +then `imm_20bit` must match x7 else CPU will raise `software check exception` +(cause=18)with `*tval = 2`. + +Compiler can generate a hash over function signatures and setup them (truncated +to 20bit) in x7 at callsites and function proglogs can have `lpad` with same +function hash. This further reduces number of program counters a call site can +reach. + +2. ELF and psABI +----------------- + +Toolchain sets up `GNU_PROPERTY_RISCV_FEATURE_1_FCFI` for property +`GNU_PROPERTY_RISCV_FEATURE_1_AND` in notes section of the object file. + +3. 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 indirect branch. Thus it's left to dynamic loader to enable +indirect branch tracking for the program. + +4. prctl() enabling +-------------------- + +`PR_SET_INDIR_BR_LP_STATUS` / `PR_GET_INDIR_BR_LP_STATUS` / +`PR_LOCK_INDIR_BR_LP_STATUS` are three prctls added to manage indirect branch +tracking. prctls are arch agnostic and returns -EINVAL on other arches. + +`PR_SET_INDIR_BR_LP_STATUS`: If arg1 `PR_INDIR_BR_LP_ENABLE` and if CPU supports +`zicfilp` then kernel will enabled indirect branch tracking for the task. +Dynamic loader can issue this `prctl` once it has determined that all the objects +loaded in address space support indirect branch tracking. Additionally if there is +a `dlopen` to an object which wasn't compiled with `zicfilp`, dynamic loader can +issue this prctl with arg1 set to 0 (i.e. `PR_INDIR_BR_LP_ENABLE` being clear) + +`PR_GET_INDIR_BR_LP_STATUS`: Returns current status of indirect branch tracking. +If enabled it'll return `PR_INDIR_BR_LP_ENABLE` + +`PR_LOCK_INDIR_BR_LP_STATUS`: Locks current status of indirect branch tracking on +the task. User space may want to run with strict security posture and wouldn't want +loading of objects without `zicfilp` support in it and thus would want to disallow +disabling of indirect branch tracking. In that case user space can use this prctl +to lock current settings. + +5. violations related to indirect branch tracking +-------------------------------------------------- + +Pertaining to indirect branch tracking, CPU raises software check exception in +following conditions + - missing `lpad` after indirect call / jmp + - `lpad` not on 4 byte boundary + - `imm_20bit` embedded in `lpad` instruction doesn't match with `x7` + +In all 3 cases, `*tval = 2` is captured and software check exception is raised +(cause=18) + +Linux kernel will treat this as `SIGSEV`` with code = `SEGV_CPERR` and follow +normal course of signal delivery.