From patchwork Tue Oct 29 23:44:19 2024 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Deepak Gupta X-Patchwork-Id: 13855735 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 A8CE4D7494D for ; Tue, 29 Oct 2024 23:45:16 +0000 (UTC) Received: by kanga.kvack.org (Postfix) id 799BB6B00B6; Tue, 29 Oct 2024 19:45:09 -0400 (EDT) Received: by kanga.kvack.org (Postfix, from userid 40) id 7476A6B00B7; Tue, 29 Oct 2024 19:45:09 -0400 (EDT) X-Delivered-To: int-list-linux-mm@kvack.org Received: by kanga.kvack.org (Postfix, from userid 63042) id 589326B00B9; Tue, 29 Oct 2024 19:45:09 -0400 (EDT) X-Delivered-To: linux-mm@kvack.org Received: from relay.hostedemail.com (smtprelay0015.hostedemail.com [216.40.44.15]) by kanga.kvack.org (Postfix) with ESMTP id 2C4A76B00B6 for ; Tue, 29 Oct 2024 19:45:09 -0400 (EDT) Received: from smtpin21.hostedemail.com (a10.router.float.18 [10.200.18.1]) by unirelay01.hostedemail.com (Postfix) with ESMTP id E1B1D1C23C4 for ; Tue, 29 Oct 2024 23:45:08 +0000 (UTC) X-FDA: 82728272340.21.3EAD87E Received: from mail-pf1-f176.google.com (mail-pf1-f176.google.com [209.85.210.176]) by imf28.hostedemail.com (Postfix) with ESMTP id A3EDBC0014 for ; Tue, 29 Oct 2024 23:44:41 +0000 (UTC) Authentication-Results: imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=OLLs4O7q; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 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=1730245427; 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=KPxKGzxEi/0hgJefbaFEQhmtveG3vMbuHajNfmroGxQ=; b=KrSKinTyZPIWAp2H754qNGaZeNxr0ib7LkwjEtUidEbJrD1EUWN2384xiopiNP2B725jeJ 581Mdzipjwz9cnXNtWIKO6bMLC/C0lro5YnOu5M5AmFCo7LA1Zclm2b1/Y8sVlsQIz5s43 inrKoJMvh4dSyNT86cR98fLuooB5D2A= ARC-Authentication-Results: i=1; imf28.hostedemail.com; dkim=pass header.d=rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b=OLLs4O7q; spf=pass (imf28.hostedemail.com: domain of debug@rivosinc.com designates 209.85.210.176 as permitted sender) smtp.mailfrom=debug@rivosinc.com; dmarc=none ARC-Seal: i=1; s=arc-20220608; d=hostedemail.com; t=1730245427; a=rsa-sha256; cv=none; b=iun9sI1evEqnCrOwnzALPMQY/J8ok691i76A5B2KHUnIWTK5khTJ6n4T2jg2F1uN9kktZO 9NLNDRUSAVnYR1jOTM1ucCHizA10Ri69CJvnX4gEbqhY+SwB+M8AN3t5DFLp5GjA9haa+2 ToibPzCknv391F3EngTzj0lHUInTllM= Received: by mail-pf1-f176.google.com with SMTP id d2e1a72fcca58-71e8235f0b6so4598659b3a.3 for ; Tue, 29 Oct 2024 16:45:06 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1730245506; x=1730850306; 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=KPxKGzxEi/0hgJefbaFEQhmtveG3vMbuHajNfmroGxQ=; b=OLLs4O7qVXWziyq11CCQTD+/0yM4zTiM6o+lidkGIQPtBygDt8GJqes8mmEqe6ymdk Cte4I3mWZ6HjR3BtwHduZgZd6mLEbCfRSuVNIBGBm8tiYL06ZnVLM7VpZWPoHCLRIhou 7xGsfj8dy392dw2M3dff4CyskGSoaqtzL8iItdEr9hh7M/R4RPWRSF58nZJ/9UYzVR7A 7rsWWd6xoY0m75jhny9P6u4xeyJvNvWUlovD3XPsRHvtG23KfVwMIIOAu1zG/BePv6ud FQ7C3c5lYL0a4vRtlrGUPpIHvORHcmT3+GVOLc/ItjGL5WIsO09mksfdncsGYtLPD6fL MQ/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1730245506; x=1730850306; 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=KPxKGzxEi/0hgJefbaFEQhmtveG3vMbuHajNfmroGxQ=; b=cj0o2z52RYSy75M9Q3L1JpK6HCqILhGeb9UVQjU3AzagEqMVwdFtF7OO+84OWEql6p xGJnp59cqIPHupSNuURstb48IOoTf0p23qynlxUtNyRfHGnpCT8lC+fIoRnaXtYWDgw/ G4s90XqqbTL25Ho2YrJY3+1tUwrkb4MPlrORFHPDaPV8DpHGYION0AVzv87qbf5I7YQh kALHBMKWXvrVoyiXgkdFK1lbibWzbIuFZgG37sM4xYqDovgZd8kPr2WLyulze4CUekdE W0x53eLnKmuYuJ2s96Huc1eTPKvnOXf+mQsJB/wy5KmulIPwbE/Su6inLRrujhAE3vro iJuw== X-Forwarded-Encrypted: i=1; AJvYcCXCqRyc17703+clsNYuPogLLjAJ7/Kfdlhzp0nbn56QuZ4AA5X/hz0ELPuQikPdYUzlyeNnnPb5Pw==@kvack.org X-Gm-Message-State: AOJu0YzQTuVirtYI/6Vt2/fZgie9USdb8Pg9LReGm+7Vg37r5Tc+uUmF wq8fhWVJt/dISdB0bvStZ5J8syDkxSdZootpoJvuMmx3iU7/Wj+xLJ+X7QeRolg= X-Google-Smtp-Source: AGHT+IEuFmVYZ1jy/33kF2WStN/il8EUrK9psJOjehX1WLShpLPHHGb9nhbp4djvoS4ETi059uFvTw== X-Received: by 2002:a05:6a00:4610:b0:71e:7a56:3eaf with SMTP id d2e1a72fcca58-7206309376bmr19849204b3a.24.1730245505859; Tue, 29 Oct 2024 16:45:05 -0700 (PDT) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id d2e1a72fcca58-72057921863sm8157643b3a.33.2024.10.29.16.45.03 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 29 Oct 2024 16:45:05 -0700 (PDT) From: Deepak Gupta Date: Tue, 29 Oct 2024 16:44:19 -0700 Subject: [PATCH v7 19/32] riscv: Implements arch agnostic indirect branch tracking prctls MIME-Version: 1.0 Message-Id: <20241029-v5_user_cfi_series-v7-19-2727ce9936cb@rivosinc.com> References: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@rivosinc.com> In-Reply-To: <20241029-v5_user_cfi_series-v7-0-2727ce9936cb@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 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-Rspamd-Server: rspam03 X-Rspam-User: X-Rspamd-Queue-Id: A3EDBC0014 X-Stat-Signature: zrh77bdpeugsqpykgenabt7dh9dk33wc X-HE-Tag: 1730245481-45772 X-HE-Meta: U2FsdGVkX18Y/wmIW2BI4RkuHn80hBZSHSNSNGhgkvjK0gUPwAUapjFnQdrt2R7sBXJ7OomsOLOdYrx2JDmbUizrVIaXeQvhzi/GYVvVuxdnn3s63IAHpXwmTlpksKHYltHeFw3kVDPyq+KgUZB7clgO7oNJmoUHwr09jHJgzDSb8AeZGxclWW4g0IKyn3G97OlxTZMLo2gxuSWe8lX90skNhYQbAKEOm6xee9Tc/cLfwcchVuvLYfsQAMwzKWocIBBgQNSdp+ddiTsK7oZamvcKT/wZC3bNr1SQuhHBpVIxaFt/Pr6YiaXvAfpyq4miyLje8Dkx17mLKBBerWICIpv6sOBtFIoKRfoFCy/QA1I7+1o1odNkIRxnQ1Lv1IU54exfSRdY7eloCL+gB1HYilqElw//bY/uybCNAnh7pDEMxiIhLK4nufm8TuZHI+MKAKRHp2VXJOLhV/8QxhCA9I32lAYgUGgzOoB/qvdef/l4vBTScEZq0s7z3DRbjR74HaesElNrIEY3VsRI+fGsMSHhcwfFg0OpqGetU/Y2VRe+HjjbWMyzAXWt6C1XVoyqwk2R9GSJSJYA07sj7SVZsg+NfBL7jtiVaD8JXUe/Wa+Q/thKiBrYLLCSJm3BFqL90mTN7py4rhDDXVBErpgvNmGJOSMopIU4hg8EvTGg9KLJX2OM3KomDh95OxkWG5Aq8gjpTw+zcsbjNmIwIyO3ohZ2w1GooiHkwAsQjT9Q0nVOpMOxo517d74DWJQKeHBknv6HmaN5YLV0adLJ79n1Ru3Y7CSqiLJCyZ5Fjix1BrnD5sFO3ffjDVLLAkXdMZj4lG0iShjcTemdbabHy00y60agLdgcEXz9vKoOaYoyfs57pKi1tyCX5QmXZCzO+rmt4KcnQgDwvxDBCqP2MLTsCu3EwMBw6Da/ZCGyKl2SGSDrsqMztj0hOYUBdyMAoj5hwOEx/CcXT45v9iiHvsi 5kFE/hNs lw1DDDlrU3F2RhgKtJyHm4E58DZytCKrT60vmZ6JC0MMVORUAMw/j+1KYrvMN5yWqxP9BdwW+Z5vBDb8jzrY5SBjqvWnCXbl2Vcz7I9IHlkNH8yHQCfOQYWLgqFat36zrldRnKFt51lYzGDZUIYncbBOewo9URzl7ZaYSaszSBREcFzdwNqnuUYjxcV0vNRLbzU5ClUAUeGUf32dmlesRMvQ+PsdPYuRNktn0JO2n2mp8vlFOB8wQU3UfJ6+mEsMr2kb90J3xF8RLidHDWy+eNvh/yEMprjeXgMlEsQaLmYD/6cjiuXvHogQ4E42nIgoXoBkNkb0Hhi93Gg97mdgyYadMNBN+hLKKhzWYB3/BV50fM9Qld/jd5z8rjy5tUXaWmR3NY/dnvShmRSu87wMNecSaUXuz9mD1sbRbe+GmU0N44g/MrLwjctjcEwpYFF2tjnX3y2jcq7R4gXA= 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. On trap entry, ELP state is recorded in sstatus image on stack and SR_ELP in CSR_STATUS is cleared. Signed-off-by: Deepak Gupta --- arch/riscv/include/asm/usercfi.h | 16 ++++++++- arch/riscv/kernel/entry.S | 2 +- arch/riscv/kernel/process.c | 5 +++ arch/riscv/kernel/usercfi.c | 76 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 97 insertions(+), 2 deletions(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 0b3aff008c85..19ee8e7e23ee 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) @@ -58,6 +64,14 @@ void set_shstk_status(struct task_struct *task, bool enable); #define set_shstk_status(task, enable) +#define is_indir_lp_enabled(task) false + +#define is_indir_lp_locked(task) false + +#define set_indir_lp_status(task, enable) + +#define set_indir_lp_lock(task) + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/entry.S b/arch/riscv/kernel/entry.S index 8f7f477517e3..a1f258fd7bbc 100644 --- a/arch/riscv/kernel/entry.S +++ b/arch/riscv/kernel/entry.S @@ -143,7 +143,7 @@ SYM_CODE_START(handle_exception) * Disable the FPU/Vector to detect illegal usage of floating point * or vector in kernel space. */ - li t0, SR_SUM | SR_FS_VS + li t0, SR_SUM | SR_FS_VS | SR_ELP REG_L s0, TASK_TI_USER_SP(tp) csrrc s1, CSR_STATUS, t0 diff --git a/arch/riscv/kernel/process.c b/arch/riscv/kernel/process.c index 850128427936..013bc3805afd 100644 --- a/arch/riscv/kernel/process.c +++ b/arch/riscv/kernel/process.c @@ -159,6 +159,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 141ef70b08a3..c5fa90711089 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.envcfg |= ENVCFG_LPE; + else + task->thread.envcfg &= ~ENVCFG_LPE; + + csr_write(CSR_ENVCFG, task->thread.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) || (arg != 0)) + return -EINVAL; + + set_indir_lp_lock(task); + + return 0; +}