From patchwork Fri Mar 29 04:44:50 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13610062 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 5A38AC6FD1F for ; Fri, 29 Mar 2024 04:46:47 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id DBFF96B00B3; Fri, 29 Mar 2024 00:46:46 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id D716E6B00B4; Fri, 29 Mar 2024 00:46:46 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id B9C3D6B00B5; Fri, 29 Mar 2024 00:46:46 -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 975296B00B3 for ; Fri, 29 Mar 2024 00:46:46 -0400 (EDT) Received: from smtpin01.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay05.hostedemail.com (Postfix) with ESMTP id 5E09E410B3 for ; Fri, 29 Mar 2024 04:46:46 +0000 (UTC) X-FDA: 81948841212.01.9CD0782 Received: from mail-oi1-f177.google.com (mail-oi1-f177.google.com [209.85.167.177]) by imf14.hostedemail.com (Postfix) with ESMTP id 7D739100005 for ; Fri, 29 Mar 2024 04:46:44 +0000 (UTC) Authentication-Results: imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GesjQ7BA; dmarc=none; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.177 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=1711687604; 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=YmrHp67H+XCJtaWoTSn26HwuzF4qGVlhViFutzTbJ64=; b=HmGHOxhBXGdmvo2S40uYnaSpzA5NIVaOBbLPeu0EInpkQNiN0U9cv3GDkT48qRWtYcY8U+ jtOsgwDp4I/GK7aIZ+6GAjBInu6a3KLB6YENr+oULxJMLMuRa7JAqufr/N0UFV0eaU7C6C i96USUFzsjDVq+RjQDwp+vllUwPXqW4= ARC-Authentication-Results: i=1; imf14.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=GesjQ7BA; dmarc=none; spf=pass (imf14.hostedemail.com: domain of debug@rivosinc.com designates 209.85.167.177 as permitted sender) smtp.mailfrom=debug@rivosinc.com ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1711687604; a=rsa-sha256; cv=none; b=2nVUzAifRdT/lHY5iEVlTI41G8yF2ZfTAzdnO4JNn5LJKFlBNgXebHBCplwUKse7RjhkBs 7gvhvhDBmoVSzflwKNkxMGukHwm+6t13EBKE+iPqsGnHeKA8tnDsRz2szebQSjUQa8qSiJ 78kR4dV0aQdAWgoCXb3FD9QIvlQOWzU= Received: by mail-oi1-f177.google.com with SMTP id 5614622812f47-3bd4e6a7cb0so964977b6e.3 for ; Thu, 28 Mar 2024 21:46:44 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1711687603; x=1712292403; 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=YmrHp67H+XCJtaWoTSn26HwuzF4qGVlhViFutzTbJ64=; b=GesjQ7BAXE0Q5ygdh9rs8pVMNlimUNjKN9nkRQHxzwR0LOydiV91gpe95BbDER6tUU wzRrvz5VLC9d1BAuj6KQwSwPy/7CP8PCGQKo3TePFU5uDvTKSiWvLPdd7tinpDBNTfSH Iwr2O3OiKMh8f6Mere+UvkIHvtEFiSnjGe8oLyfDknwZbs0yB9CQ8yjV6S+qAV46uk+P WN37rBX79Ts5GGrZkbFguOMdEg/LjLfFZ134uQsgQyL9nh506UK+lUF3k27DcyVEpvoZ kbW7HFE7SXS6vpIMdSXCU3H2xL2ilf5GkL1qz81XtL4ip60f90Y/j+qRP/DYKpLfAZpV lqew== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1711687603; x=1712292403; 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=YmrHp67H+XCJtaWoTSn26HwuzF4qGVlhViFutzTbJ64=; b=vumpUn3FJrUdv45UkLVeMaemdIhKWDuE4QRtVv8fJS/0NyCMyUS7VNOWJMC2lM1XeZ mAHApd8a8JyDgIg+3/C9+v7+kchfUPQi43mHxjWEb+pt3+7X6KqZLG2GestIfgN10R0J /OHe1kjHSNsguOtITNKeJGrEwXexe84OGY/MzaO+vbHIrSpMJQkVXNui4wUZl0NcpkWk FU4ClbfLYzrbZm5S486uEcum8htcgHvNaAxbinOwe0H1EHikO8pcw54EPjZXh28cxJP4 4T1uw1zdXD55c+Sg1oHsgCj0l3DZnRzDO4LM7VZ3JhlAuT8g0qCLa3LEbrBTTTVWXzNY 3euA== X-Forwarded-Encrypted: i=1; AJvYcCXGRgQsv9njT9gUtf9qF1jtVGMHKxV9rgmARFBuEWJ0CqK/RPjUQHOML88ZwEVMjMvyH9WnFPwqfbEE2pKQKEhOcUs= X-Gm-Message-State: AOJu0Ywrs3gWWLVONRlapE1Zf+axqANao1+UWslieYlSOaZ4f/gJqCBO SCev5/3glF+lYN/aZKtq6UHn5T3hOIhddbRQSjpJP9WYe3A5n5uUNEH3dlgVvAY= X-Google-Smtp-Source: AGHT+IGNXVgsJhuruVdNoXrAX5QvhHzDZ/ypkAvKiPh6A+vsQQkyT8+weoD2+0apPvMAl8Da1TUaIg== X-Received: by 2002:a05:6808:648f:b0:3c3:d72b:3851 with SMTP id fh15-20020a056808648f00b003c3d72b3851mr1338208oib.7.1711687603411; Thu, 28 Mar 2024 21:46:43 -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.46.40 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 28 Mar 2024 21:46:43 -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 18/27] riscv: Implements arch argnostic indirect branch tracking prctls Date: Thu, 28 Mar 2024 21:44:50 -0700 Message-Id: <20240329044459.3990638-19-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-Rspamd-Server: rspam09 X-Rspamd-Queue-Id: 7D739100005 X-Stat-Signature: f5ktxeduff76je5do7p6nqxyggkh6bgj X-Rspam-User: X-HE-Tag: 1711687604-539844 X-HE-Meta: U2FsdGVkX1/3akzezYAwtl6ITCqJdPTIfARy7UtL404kH6HFuSigaOKX93RDhhXWy8QaZu5aXeSGymUNYzezjd3a32MJUHqFAnVDb/D0AxDF0XTbwThP9EY5+xHXCf5tOFjt01BDEdiOYi/ljudy/7Vpp2lFcTbTudHD/jbcr0QwAM1vGGW+KanJFVwnJXXiaI/bBucz/kS9pNE157kdBdQtXYz7zautXi8EO5o897Mm3+78Tk6gB/JKGwF72pCaeZqzOnCfVvMum0V6D3Ka/r4E16A4zOX+2z5l1y214OeL4b1od2t988v6efTg3QpLOGAYOaMOhwvw7d2g4g1/5f+gvVX1uwrw/4Mr4wekdpZRVx+7LODznlZihP2MpDnLZFUyurLg8ez74+2evzHU9s7/+gsj841+6en5MIn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Hl/6jBfyYNANu58Lng/zKdq0= 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 d6dfef22c274..105ceb8880c1 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; +}