From patchwork Wed Apr 3 23:35:07 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13616814 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 AD48FCD1292 for ; Wed, 3 Apr 2024 23:42:32 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 369696B00BD; Wed, 3 Apr 2024 19:42:32 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 31C126B00BE; Wed, 3 Apr 2024 19:42:32 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 11DEA6B00BF; Wed, 3 Apr 2024 19:42:32 -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 E52C56B00BD for ; Wed, 3 Apr 2024 19:42:31 -0400 (EDT) Received: from smtpin11.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay06.hostedemail.com (Postfix) with ESMTP id B64E1A0F83 for ; Wed, 3 Apr 2024 23:42:31 +0000 (UTC) X-FDA: 81969847302.11.56EFC33 Received: from mail-pf1-f173.google.com (mail-pf1-f173.google.com [209.85.210.173]) by imf06.hostedemail.com (Postfix) with ESMTP id 15589180019 for ; Wed, 3 Apr 2024 23:42:29 +0000 (UTC) Authentication-Results: imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=azBBlXyH; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.173 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=1712187750; 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=nZuOqDvC0Z7orZ7kqn38837045KjWIfnwy63b75kPss=; b=4I2NHhqrYmRCysxb9PxL9oL9/uNEB2Czo1pTyD+/lZhxbMdxruovmQQi51Fag1BcosJ6+T n5BscUjfvp+X5kWHJLZkGJ0ba2hMOLwwLm8nrxKJuN7Shwyr5xmSmES6cNKv8JkHXeCRbt ecUJEwcDMwPS8cCmKfzDv0GiFh9t3JM= ARC-Authentication-Results: i=1; imf06.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=azBBlXyH; dmarc=none; spf=pass (imf06.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.173 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1712187750; a=rsa-sha256; cv=none; b=HzJvF8JP+3DhkYMx4LE49Pz+amJbkIqN+4lRKLvnQ3HW9A6BgdrLusi8nBBMzQ4cqfcFJW 5Fr7QPkInF33t54sQW+DhnsRjxg6QZGro1NmBT8VziHSoR6zfJU7IDDwdC4cc7y+i7biEM gYDMaWDnK72JwW4LKOYTWR9efQAYxLA= Received: by mail-pf1-f173.google.com with SMTP id d2e1a72fcca58-6e6b6f86975so284452b3a.1 for ; Wed, 03 Apr 2024 16:42:29 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1712187749; x=1712792549; 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=nZuOqDvC0Z7orZ7kqn38837045KjWIfnwy63b75kPss=; b=azBBlXyHjpVERx3HyLoooSaOqvir6pFnMvsHcRwpuHe8Pm84+SzYYqwJyEywbncOGW +UC+73KLCG83aZ9GsYjBqSesgV9vZjcq8em1b1rgm2JthbDay5kzIiq5JbiDBIOfgvpZ jG4CVdHHiVlUZq4D6gH1NgjjIL0K6u7WRHwnqOV72KurGEtsPX34H4AkaRHVy0Edr91C Yip5wHn3kkdjUbk7QYrSFUaCddPKAEJzwZHAR4qUt26KZZXuRcmlSivg+vK4uPP2Jc0Z WzfYSm/Cy3qIzHWICN/feQ2ldqF1BQTR37JltNRyt7cPFYAZuoe25amDfO+ZuxkfnhBU 8w5Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1712187749; x=1712792549; 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=nZuOqDvC0Z7orZ7kqn38837045KjWIfnwy63b75kPss=; b=GxXej8IY9WkSmd3kjLFIhIwLf3rTBuzQ0CPycLWfL/wT8aqGg+gCDTdqefvy0H1j1B 8kMfgcZj22bCqA4bFbnErkVItlXRLfrf1FZ+LcjjUl18acqsrPlnE6Sw0GdGmXZhMK8e ScAEWLYruKa1Rrgk0ubpSXVxL5zYea3//33s9H+5upH8BBl2q8Ck33RdXHhExz1Nqv0G FW+NL5nIjYdS/VqKH3cTm2erb+mwB0pPo2bLi0RRqSuQzQXjQcBNruM/utec1hz7AbEb yPWA1FMkHpjac+lRHiHUb6gBbgRYQi1qPg5p9WDaHO3zt9izOYFndxVqvasnvijCJwTL 7q3w== X-Forwarded-Encrypted: i=1; AJvYcCVvX8KdDgtvKrNtGmVR6agvFRyzOzsgh1NFjS8KC3PP3HeGHGEZfdxURmlPSBz/kF4Ib4Wuwnt3KAcQlBX8m6EsrbA= X-Gm-Message-State: AOJu0Yy0rYSmLU2YDT1TX6UjL4GDNnUymOmEd8AXOwK0NbQkhsEP4EDN 3oeRpTUqIZjV7ZGR3sdRmGawoIU/irFAhrJcS2QofGNmNTAe3LtnajJosLGnhTs= X-Google-Smtp-Source: AGHT+IFlDR3O+4GEi6icblP42gxGgn6vi8m6/NIcpSF/j+yTm57ooaZOA9iB5WMfSYbFBKo9qpApOQ== X-Received: by 2002:a05:6a20:3953:b0:1a3:466d:d33 with SMTP id r19-20020a056a20395300b001a3466d0d33mr1558419pzg.9.1712187748866; Wed, 03 Apr 2024 16:42:28 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id b18-20020a170902d51200b001deeac592absm13899117plg.180.2024.04.03.16.42.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 03 Apr 2024 16:42:28 -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, conor@kernel.org Cc: 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, 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 v3 19/29] riscv: Implements arch agnostic indirect branch tracking prctls Date: Wed, 3 Apr 2024 16:35:07 -0700 Message-ID: <20240403234054.2020347-20-debug@rivosinc.com> X-Mailer: git-send-email 2.43.2 In-Reply-To: <20240403234054.2020347-1-debug@rivosinc.com> References: <20240403234054.2020347-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspam-User: X-Stat-Signature: 6my6pibrmtkrgacsj5fger3oyysjd54k X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: 15589180019 X-HE-Tag: 1712187749-645281 X-HE-Meta: U2FsdGVkX1/lTM/OOrnEo/LH1iD7NJAVv9s3EgO4n0f9PBn2kLEv+JHJBMP10dtDMxM+gTi5Sknk9XKsMaWor70NoN8r5HXScIUw9SDWs8b/rcE/btLRUeKo+rmyr3WDf71i/FW/y3RI6sLKVb39F5gJGmjUUkK0tLi9fZlfc+ziKigpiDuf/LoT8bpMG8/AV24mO4jKH/iLSCDTqDFiL84+tKcZz92EHBbgIAVitvW6D9wRe3YvnOAK2EmpaLE6o31Psx6v8VjqfvGH/xaTrezoQuUpe+QqCQI7/DSTGIeztcpzclZEpxtAiW2lBTEtaUkgqoyLRKjgjBUCpq/VWjkjPbb5D4KpdqshV2VUQTxqZEFFF1MYuvwDIpho62JyR+ZPdlVrV9HwZGEoNhfCmfTMAX6gpVX+6sQwrRxzRQp15Mq9enTtuJsiahVOhX+u/XziWJhNOeL3tq4G3Nsfb3BXrpccvhz3TsuJE6GrapqJj1UA54/JXRMrHf10RTO2RDgf8Ihh+KSFHK51LmffT0zQXWD5r+h9PbeC+RpJ8Rvwq1/DANL64JCVwdiCCxV4F2Xp0iaw5tneOoAc/t6jvE3k8MSbMY8WG6EnJMKXIEk3WHf2FZsxtT/GiRBf88fSsr8l5Da+3FZfTUvqzz3ypQ7Mvm5U2Ny7PNFk7nq4cUOnByW0+vVSbqwyRGjO+QaYpv9QX2CELPsSlJ3ndCwarrlI581utke5CZN7cTACKn59v654TbNQ9ME40TLK1kkv+feHFn3x/azfgQ73COnPjQlOee/ohqWGcn5hHxMnINkzuyNA8fQoNi4Ehn5VJpa1C7mfsacNFsPHU6r/idFWuV1QtmqAjR/DtJpersicZx0N+x6HKAhQAMDjmVb8hfxdLU00buOraL42BbcLMNkjYI8JbbMXkAL/YQPQiJy1vN7rbFOQZgtFwHid5Ndzrdd8tE6Otx6oU1Teo6voCKf r8DaTuRP X5MFvT6dJ3gGhEF8bWrlmE1hEZ3OqvHlNXp/dZmUQbG8boqkyU8JCrRl1RVi0gyIsIWeRCLhA6NNbQ2yyRCj5Gm2a32rOXXFWK4DbHilriG5y6mfbouZPpEbaznb3cR2QwLUDdMNDkbFovW88Z8bHRrRTWbi+KlmacYNhGtNbH5D9ajZ1cTxNDB3ntoLXBey0kdVCkZUc3QnxCSsPRuPTP25BgZ2Kxfa2mNizFoo4ZT+tCSg= 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: prctls implemented are: PR_SET_INDIR_BR_LP_STATUS, PR_GET_INDIR_BR_LP_STATUS and PR_LOCK_INDIR_BR_LP_STATUS. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 22 ++++++++- arch/riscv/kernel/process.c | 5 +++ arch/riscv/kernel/usercfi.c | 76 ++++++++++++++++++++++++++++++++ 3 files changed, 102 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index a168ae0fa5d8..8accdc8ec164 100644 --- a/arch/riscv/include/asm/usercfi.h +++ b/arch/riscv/include/asm/usercfi.h @@ -16,7 +16,9 @@ struct kernel_clone_args; struct cfi_status { unsigned long ubcfi_en : 1; /* Enable for backward cfi. */ unsigned long ubcfi_locked : 1; - unsigned long rsvd : ((sizeof(unsigned long)*8) - 2); + unsigned long ufcfi_en : 1; /* Enable for forward cfi. Note that ELP goes in sstatus */ + unsigned long ufcfi_locked : 1; + unsigned long rsvd : ((sizeof(unsigned long)*8) - 4); unsigned long user_shdw_stk; /* Current user shadow stack pointer */ unsigned long shdw_stk_base; /* Base address of shadow stack */ unsigned long shdw_stk_size; /* size of shadow stack */ @@ -30,6 +32,9 @@ void set_active_shstk(struct task_struct *task, unsigned long shstk_addr); bool is_shstk_enabled(struct task_struct *task); bool is_shstk_locked(struct task_struct *task); void set_shstk_status(struct task_struct *task, bool enable); +bool is_indir_lp_enabled(struct task_struct *task); +bool is_indir_lp_locked(struct task_struct *task); +void set_indir_lp_status(struct task_struct *task, bool enable); #define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) @@ -72,6 +77,21 @@ static inline void set_shstk_status(struct task_struct *task, bool enable) } +static inline bool is_indir_lp_enabled(struct task_struct *task) +{ + return false; +} + +static inline bool is_indir_lp_locked(struct task_struct *task) +{ + return false; +} + +static inline void set_indir_lp_status(struct task_struct *task, bool enable) +{ + +} + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 3fb8b23f629b..ebed7589c51a 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -152,6 +152,11 @@ void start_thread(struct pt_regs *regs, unsigned long pc, set_shstk_status(current, false); set_shstk_base(current, 0, 0); set_active_shstk(current, 0); + /* + * disable indirect branch tracking on exec. + * libc will enable it later via prctl. + */ + set_indir_lp_status(current, false); #ifdef CONFIG_64BIT regs->status &= ~SR_UXL; diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index cdedf1f78b3e..13920b9d86f3 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -69,6 +69,32 @@ void set_shstk_lock(struct task_struct *task) task->thread_info.user_cfi_state.ubcfi_locked = 1; } +bool is_indir_lp_enabled(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ufcfi_en ? true : false; +} + +bool is_indir_lp_locked(struct task_struct *task) +{ + return task->thread_info.user_cfi_state.ufcfi_locked ? true : false; +} + +void set_indir_lp_status(struct task_struct *task, bool enable) +{ + task->thread_info.user_cfi_state.ufcfi_en = enable ? 1 : 0; + + if (enable) + task->thread_info.envcfg |= ENVCFG_LPE; + else + task->thread_info.envcfg &= ~ENVCFG_LPE; + + csr_write(CSR_ENVCFG, task->thread_info.envcfg); +} + +void set_indir_lp_lock(struct task_struct *task) +{ + task->thread_info.user_cfi_state.ufcfi_locked = 1; +} /* * If size is 0, then to be compatible with regular stack we want it to be as big as * regular stack. Else PAGE_ALIGN it and return back @@ -375,3 +401,53 @@ int arch_lock_shadow_stack_status(struct task_struct *task, return 0; } + +int arch_get_indir_br_lp_status(struct task_struct *t, unsigned long __user *status) +{ + unsigned long fcfi_status = 0; + + if (!cpu_supports_indirect_br_lp_instr()) + return -EINVAL; + + /* indirect branch tracking is enabled on the task or not */ + fcfi_status |= (is_indir_lp_enabled(t) ? PR_INDIR_BR_LP_ENABLE : 0); + + return copy_to_user(status, &fcfi_status, sizeof(fcfi_status)) ? -EFAULT : 0; +} + +int arch_set_indir_br_lp_status(struct task_struct *t, unsigned long status) +{ + bool enable_indir_lp = false; + + if (!cpu_supports_indirect_br_lp_instr()) + return -EINVAL; + + /* indirect branch tracking is locked and further can't be modified by user */ + if (is_indir_lp_locked(t)) + return -EINVAL; + + /* Reject unknown flags */ + if (status & ~PR_INDIR_BR_LP_ENABLE) + return -EINVAL; + + enable_indir_lp = (status & PR_INDIR_BR_LP_ENABLE) ? true : false; + set_indir_lp_status(t, enable_indir_lp); + + return 0; +} + +int arch_lock_indir_br_lp_status(struct task_struct *task, + unsigned long arg) +{ + /* + * If indirect branch tracking is not supported or not enabled on task, + * nothing to lock here + */ + if (!cpu_supports_indirect_br_lp_instr() || + !is_indir_lp_enabled(task)) + return -EINVAL; + + set_indir_lp_lock(task); + + return 0; +}