From patchwork Thu Sep 12 23:16:39 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13802843 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 B1569EEE271 for ; Thu, 12 Sep 2024 23:18:37 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 41CA76B00B6; Thu, 12 Sep 2024 19:18:37 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 3CB2B6B00B8; Thu, 12 Sep 2024 19:18:37 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 1D1038D0002; Thu, 12 Sep 2024 19:18:37 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0010.hostedemail.com [216.40.44.10]) by kanga.kvack.org (Postfix) with ESMTP id F23046B00B6 for ; Thu, 12 Sep 2024 19:18:36 -0400 (EDT) Received: from smtpin27.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay09.hostedemail.com (Postfix) with ESMTP id AB96E80DA1 for ; Thu, 12 Sep 2024 23:18:36 +0000 (UTC) X-FDA: 82557652632.27.B3CA878 Received: from mail-pl1-f178.google.com (mail-pl1-f178.google.com [209.85.214.178]) by imf03.hostedemail.com (Postfix) with ESMTP id D8EEB20011 for ; Thu, 12 Sep 2024 23:18:34 +0000 (UTC) Authentication-Results: imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kNjve1Tr; dmarc=none; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.178 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=1726182997; 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=KwRJ4ewhEVAckpzni0eOC4W3kOUznT5QSphy/YvwFF4=; b=2F5CfpLH4L0DtGZ62aHDCAvEZj8+p/+f5+OOtzHCXJmvPDKYxuvdHhVcY2K/hfFV8LXtuh 78R/2TTJf6v3fqnmVlaHYT7jqUXxKFp7bD1b0O3Wn/7PWuf/u+KqaWbUO1CtB/XQzT6CQW hzwkhul32ZNr2mQA0KIeeo9w/z57dj8= ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1726182997; a=rsa-sha256; cv=none; b=wnBCQDITItoMYzSSLQh9I45JdUdFVLLtfx2vEt7jRgfprMd8fKTUwxEpsPmjgiVMxy9xl7 wOQ62gL87N56LJ4P8PgKfAiY/cbPtOlMPMRgVBFLF25S1SoyWhjsqyN+hDQnqvyASFdpeq HoifF8bZ0UCrHdGa1zl8FUeTXoInjEI= ARC-Authentication-Results: i=1; imf03.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=kNjve1Tr; dmarc=none; spf=pass (imf03.hostedemail.com: domain of debug@rivosinc.com designates 209.85.214.178 as permitted sender) smtp.mailfrom=debug@rivosinc.com Received: by mail-pl1-f178.google.com with SMTP id d9443c01a7336-20570b42f24so17948735ad.1 for ; Thu, 12 Sep 2024 16:18:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1726183114; x=1726787914; 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=KwRJ4ewhEVAckpzni0eOC4W3kOUznT5QSphy/YvwFF4=; b=kNjve1Tr5hYbPgEtkJKYJaVZrrxDLK++ozxV2cNqCxpZBxkEtipFmqQ3DlCHepvnYR p5jglkbtyvkNiCViPhwz/O0LWyaegQRnwvsZv7xJPBg1sburkySpgT31ByLSZwotPc3x rCv+wGv5H/XsFOLJDhq3j0awiN/Chhl0TbVVoa9t2ch5iYG3BGk5F0kbr6dsFuvo9yqb o3twgn+Z+a+p6KxxdqimqoOU4vr8nARPD3gmYn9P/KfaUpgywm2haUu1GDEhFLhiv4/Q V6mIrrcQrk/LfrH00A3FKM/mXvTfINdNVNZpwM21lkOr7MTCDdAkpwsZxLi10o0XvNdu VJLw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1726183114; x=1726787914; 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=KwRJ4ewhEVAckpzni0eOC4W3kOUznT5QSphy/YvwFF4=; b=kx7hwMjzKpnwg4OmhcMedVXqBZJTA7g61O65oGec9YhW7pS0g3AVThF/Lpw/xGZ+9+ T6/U5gbzb1XFuXMfJFjWWdaAn6RNgc2i0sO+gvxxUA5NV4Cd/PXpNOsVzYmi2XUFIn/y zQQkmk8C3Fd4F56FYnmbH9X6O4k2mxExcN0Ak4uu/GqSq70LY8aSyMuYIhSV+IxpjHmr LgAyqT/CaEoGt/T4Uexo+2m8HyahacPFMRwhmdpNDBOBVIeSH2RvRCuWKt2xmy2KOxNd 7wZKxC8GYc1pW2weWY+OoEmraFr6eKHnKOjVuv7eFIf8syD7Qa5fm1vSnynqS/dP2xHz 27oQ== X-Forwarded-Encrypted: i=1; AJvYcCUvHPNp8Yb37dj371zCnak+UPQ0IgNebPlqn8+LmcAbmYcOzF0SBQpm0J0Mh2W8ZLDhwKyH49long==@kvack.org X-Gm-Message-State: AOJu0YzZmz46RigNsXD4Sr2ZAMoMEjjlc06jumI7CBbtDq5wdegnWTv0 h3oxZn+/fjSUeqpWL/qpJ6UE8VILO1M0lCR9aDNaWnjIsR6aeNtCllKDcM6iblo= X-Google-Smtp-Source: AGHT+IG1JMYVnuZDDv5vrKY0Ap6nzQ8EAROaZEkaQsrbR4Aftj/1uKtP8Rx6b8tiDt3qNH/8ZNKatg== X-Received: by 2002:a17:90b:4c0c:b0:2d3:c0e5:cbac with SMTP id 98e67ed59e1d1-2db9ffa11e4mr4871259a91.7.1726183113476; Thu, 12 Sep 2024 16:18:33 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id 98e67ed59e1d1-2db6c1ac69asm3157591a91.0.2024.09.12.16.18.29 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 12 Sep 2024 16:18:33 -0700 (PDT) From: Deepak Gupta To: paul.walmsley@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-fsdevel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Cc: corbet@lwn.net, palmer@dabbelt.com, aou@eecs.berkeley.edu, robh@kernel.org, krzk+dt@kernel.org, oleg@redhat.com, tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, x86@kernel.org, hpa@zytor.com, peterz@infradead.org, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, kees@kernel.org, Liam.Howlett@oracle.com, vbabka@suse.cz, lorenzo.stoakes@oracle.com, shuah@kernel.org, brauner@kernel.org, samuel.holland@sifive.com, debug@rivosinc.com, andy.chiu@sifive.com, jerry.shih@sifive.com, greentime.hu@sifive.com, charlie@rivosinc.com, evan@rivosinc.com, cleger@rivosinc.com, xiao.w.wang@intel.com, ajones@ventanamicro.com, anup@brainfault.org, mchitale@ventanamicro.com, atishp@rivosinc.com, sameo@rivosinc.com, bjorn@rivosinc.com, alexghiti@rivosinc.com, david@redhat.com, libang.li@antgroup.com, jszhang@kernel.org, leobras@redhat.com, guoren@kernel.org, samitolvanen@google.com, songshuaishuai@tinylab.org, costa.shul@redhat.com, bhe@redhat.com, zong.li@sifive.com, puranjay@kernel.org, namcaov@gmail.com, antonb@tenstorrent.com, sorear@fastmail.com, quic_bjorande@quicinc.com, ancientmodern4@gmail.com, ben.dooks@codethink.co.uk, quic_zhonhan@quicinc.com, cuiyunhui@bytedance.com, yang.lee@linux.alibaba.com, ke.zhao@shingroup.cn, sunilvl@ventanamicro.com, tanzhasanwork@gmail.com, schwab@suse.de, dawei.li@shingroup.cn, rppt@kernel.org, willy@infradead.org, usama.anjum@collabora.com, osalvador@suse.de, ryan.roberts@arm.com, andrii@kernel.org, alx@kernel.org, catalin.marinas@arm.com, broonie@kernel.org, revest@chromium.org, bgray@linux.ibm.com, deller@gmx.de, zev@bewilderbeest.net Subject: [PATCH v4 20/30] riscv: Implements arch agnostic indirect branch tracking prctls Date: Thu, 12 Sep 2024 16:16:39 -0700 Message-ID: <20240912231650.3740732-21-debug@rivosinc.com> X-Mailer: git-send-email 2.45.0 In-Reply-To: <20240912231650.3740732-1-debug@rivosinc.com> References: <20240912231650.3740732-1-debug@rivosinc.com> MIME-Version: 1.0 X-Rspamd-Server: rspam07 X-Rspamd-Queue-Id: D8EEB20011 X-Stat-Signature: 4npadq14uw3sig8t4zztxks88ac8fogg X-Rspam-User: X-HE-Tag: 1726183114-777805 X-HE-Meta: U2FsdGVkX1/NLZxn5zciP5nwINE7QJMC8cspeXHghSevbckrbn529M4TcSIjZnOLCzoI3ZKfzI7IhVCo5Qh8q8SUutKrdqREA5bZg2vCyF+2QWCNZCLSneQyPPOHAbniEECxI1erpIGxJ3KOMDfVGv24UyNnSgtjS+uX5WOTrbPlBkTLwvuzjy5zWFI91QS2+rVIPx+QGJh/v8XrqbdXgq8BB80ziZjs5zf6Yu6hGg5/cOsN0CQIRSUjHLTe4l6fDmsReWn+elzAPnKLmxFEh/zd92XSjnvLiQtVWPM7kEuk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JEo3hG7ZP/Narx3S14ec0fTRNTD219zaGvFSQnmymIwKKrXFSGEjv3jeu 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 | 28 +++++++++++- arch/riscv/kernel/process.c | 5 +++ arch/riscv/kernel/usercfi.c | 76 ++++++++++++++++++++++++++++++++ 3 files changed, 108 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 6d806fbb283d..20a9102cce51 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 */ @@ -33,6 +35,10 @@ bool is_shstk_locked(struct task_struct *task); bool is_shstk_allocated(struct task_struct *task); void set_shstk_lock(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); +void set_indir_lp_lock(struct task_struct *task); #define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) @@ -90,6 +96,26 @@ 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) +{ + +} + +void set_indir_lp_lock(struct task_struct *task) +{ + +} + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index b64baa0235ea..f3c5b8f2c869 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -150,6 +150,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 c77abe552c88..8da509afdbe9 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 @@ -371,3 +397,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; +}