Received: by 2002:a05:7412:5112:b0:fa:6e18:a558 with SMTP id fm18csp1574208rdb; Wed, 24 Jan 2024 22:35:53 -0800 (PST) X-Google-Smtp-Source: AGHT+IEoS9R45LDJmYfe0hdUxhTfizmpZm04d5XxBjGXNipCvKUUHqj5dDxvYWPhNUKWlbNJlyKo X-Received: by 2002:a05:6512:3248:b0:50f:f9bd:e354 with SMTP id c8-20020a056512324800b0050ff9bde354mr220271lfr.23.1706164553676; Wed, 24 Jan 2024 22:35:53 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1706164553; cv=pass; d=google.com; s=arc-20160816; b=J9gzUFBAsfBlOJvqKdvkf4Fmg/wWJGLJFVWWc5MYllBmETSZRIAWDMR3nql7CslNHo xCVo20p7tIG9iU4w9zfcSB+g2JylLYpYqoxttD9xmdPGMgr9E1zZVVYsL5sa/afCSrtN E44pbEby0+Zd3320X+ytDgaMBpkC4bcTsEtG+c74z3tFaEa2Tw9diBPhTAu7Y0QKSiw8 Uvliuva9M/jOqLB+4NGSIaKw7NT55kF7Kgyry4JxsW/TWtXe31ig1BDJVsO0iqavE6/O RRDyIfQUwYSjZWEFfN65BcVge376w4iTJIcDyv3qLkK6frI8gDprXgUw55U2LhNuUrkB ernw== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=content-transfer-encoding:mime-version:list-unsubscribe :list-subscribe:list-id:precedence:references:in-reply-to:message-id :date:subject:cc:to:from:dkim-signature; bh=aZQZJuD25Dv4YRroK4zFhzWBRez2vp1MS493EB8S7EM=; fh=oy7V1wWqiaug32hsWRXm98kKNp5NFPTWbY7PiGr4deM=; b=elZVg7M5OkfDcx3PCNftFebb3qzPCxB35yHKikvuowmyqKaJKy8bxCSb0ywrqzX9IY 3cJn3XN/WnE8lKap3ckEyet2cZ7h1lzfIkTfjdSnv6JyoucMfwdC1fl+p3Qv+LsxqzZ1 UoppPfsiRoiZIOIIBC+tbGPh7KRPHj2aMfOsF5yRrUHgdfvCPgGktph5j+OgGBHwt/mp C7R0YKsvdkTX+ANeuwf7F5pVjZQUgycWEKLI8bB33G9teqlNK/zqrDFVNH1XbbCaaYcm UJE9kf0ufHzmhpdV3DDI8n90ORG0Hn/QhO+SD5TQlEKTtxo8LwU9mwQvDqh2qovltDem hUfQ== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="b+/kfNVz"; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-38028-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38028-linux.lists.archive=gmail.com@vger.kernel.org" Return-Path: Received: from am.mirrors.kernel.org (am.mirrors.kernel.org. [2604:1380:4601:e00::3]) by mx.google.com with ESMTPS id x9-20020a1709064a8900b00a3109845476si677246eju.874.2024.01.24.22.35.53 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:35:53 -0800 (PST) Received-SPF: pass (google.com: domain of linux-kernel+bounces-38028-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) client-ip=2604:1380:4601:e00::3; Authentication-Results: mx.google.com; dkim=pass header.i=@rivosinc-com.20230601.gappssmtp.com header.s=20230601 header.b="b+/kfNVz"; arc=pass (i=1 spf=pass spfdomain=rivosinc.com dkim=pass dkdomain=rivosinc-com.20230601.gappssmtp.com); spf=pass (google.com: domain of linux-kernel+bounces-38028-linux.lists.archive=gmail.com@vger.kernel.org designates 2604:1380:4601:e00::3 as permitted sender) smtp.mailfrom="linux-kernel+bounces-38028-linux.lists.archive=gmail.com@vger.kernel.org" Received: from smtp.subspace.kernel.org (wormhole.subspace.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by am.mirrors.kernel.org (Postfix) with ESMTPS id 470C01F26758 for ; Thu, 25 Jan 2024 06:35:53 +0000 (UTC) Received: from localhost.localdomain (localhost.localdomain [127.0.0.1]) by smtp.subspace.kernel.org (Postfix) with ESMTP id 22AFB1BDE1; Thu, 25 Jan 2024 06:30:14 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b="b+/kfNVz" Received: from mail-oi1-f173.google.com (mail-oi1-f173.google.com [209.85.167.173]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A26B210A2B for ; Thu, 25 Jan 2024 06:30:11 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.173 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706164213; cv=none; b=F0xQh7gpQVVbmHUQ++gnjZxR5qvL5uqdmooTYv5kPiqRZnpp2L/cU7mq8tCAR+PvX2sFydKmY4z3xst00aUlVbiSPyaakcoKnKeJMRpzjSkrrSQt2jq4cH6l/rDRCIvRef9n53v2SeQTpImt6qOEqPdblFTkxLy0zoYwtkf2oCw= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1706164213; c=relaxed/simple; bh=EeYE+liconztX3vH/QbRUB4lYtHeeSm6ruQQjkWo+Bw=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=VWVPZQxMCjW7orLeHmDP7bp9Lpycb2k0MGYZ9fyGNsvtw/5m+8H4mfBUR7lZnYDXVlrUXVqT9i/2NxFEtZqJpJDpTT2xz5oCn+rMzi/GpJmuhtvBRuttC2DIph2DTZt/cGzATXqNdIkDkbBe13NExJQOouDKd1mQIJeMDlkrNiM= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com; spf=pass smtp.mailfrom=rivosinc.com; dkim=pass (2048-bit key) header.d=rivosinc-com.20230601.gappssmtp.com header.i=@rivosinc-com.20230601.gappssmtp.com header.b=b+/kfNVz; arc=none smtp.client-ip=209.85.167.173 Authentication-Results: smtp.subspace.kernel.org; dmarc=none (p=none dis=none) header.from=rivosinc.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=rivosinc.com Received: by mail-oi1-f173.google.com with SMTP id 5614622812f47-3bdd7fae400so348245b6e.0 for ; Wed, 24 Jan 2024 22:30:11 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=rivosinc-com.20230601.gappssmtp.com; s=20230601; t=1706164211; x=1706769011; darn=vger.kernel.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=aZQZJuD25Dv4YRroK4zFhzWBRez2vp1MS493EB8S7EM=; b=b+/kfNVzZDe+CNWAISINy/6oNPq8qq7qEHSqLdYSyW01d/aIajuR8YQkfqfBM5ckoG DmFiAKEuQungjoP+s92MvTukPPB73lpddfJuYeixz783oIoRvjcsOAprpXcy02M2+HiJ goWJJFdEvw7GlzvvnAwpcjfL27zhS8T4VTmSjZansh3FLTmiGufMqm1f23AyJdzoJr9l uyVQOXfS2UGZMsSHdm/L+firkJFcMUl+i8zp8/pFo4QB52oAgTEXAVcXfUkEpXN0U6Fj gotriAFPdlugq8vQv7S/WLIfPkreebU7CK0mKHCIfN8PzuWkSxdl4ui+PZp2BXtHGIwv R7HA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1706164211; x=1706769011; 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=aZQZJuD25Dv4YRroK4zFhzWBRez2vp1MS493EB8S7EM=; b=DBiRxmC/MRoOjU0L8TnAk8uVTfe91CfDQ+vEo1QD9s/3XQKr5uVBSKF8ToZLFFUiYW p4rHxoKQx9ntwfBmCahE3idPHbzVYm4UBhXmFqDX5dia21Xd2vropxYV0BhwPrwE31Wl +0YR1dbUnjMRlZ9AkC+dwU78d1x1eE5oLfos0kbZ06UVwBKubeIUYJT1zwlxF6dbGPpV lwetKR+plAcOvj7NYi02TO/S8AI6bnCICyiK7orogqOWeKUIGwwHHVI6Ww7abGPRq6Pa CQBTqjUc5wsIQKcZuewgV1IdHPcNfrUBtB2RjhmxrwXrKB7R7dKX+fA2JEGUX9zu1LW7 k7Gg== X-Gm-Message-State: AOJu0YzTgrP3FLWzQ2XfBaWVrlHyNTZ2d0d9Phvnsi/l4vp0Q0h4yd3E SP97FHEYqUZGzmRK4Qy1Q5HMcJjzdBz4sYnCJT01uQC7Xx2+2BZkM09jH/M8YPI= X-Received: by 2002:a05:6808:d51:b0:3bd:bf47:d635 with SMTP id w17-20020a0568080d5100b003bdbf47d635mr590089oik.41.1706164210775; Wed, 24 Jan 2024 22:30:10 -0800 (PST) Received: from debug.ba.rivosinc.com ([64.71.180.162]) by smtp.gmail.com with ESMTPSA id t19-20020a056a00139300b006dd870b51b8sm3201139pfg.126.2024.01.24.22.30.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 24 Jan 2024 22:30:10 -0800 (PST) From: debug@rivosinc.com To: rick.p.edgecombe@intel.com, broonie@kernel.org, Szabolcs.Nagy@arm.com, kito.cheng@sifive.com, keescook@chromium.org, ajones@ventanamicro.com, paul.walmsley@sifive.com, palmer@dabbelt.com, conor.dooley@microchip.com, cleger@rivosinc.com, atishp@atishpatra.org, alex@ghiti.fr, bjorn@rivosinc.com, alexghiti@rivosinc.com Cc: corbet@lwn.net, aou@eecs.berkeley.edu, oleg@redhat.com, akpm@linux-foundation.org, arnd@arndb.de, ebiederm@xmission.com, shuah@kernel.org, brauner@kernel.org, debug@rivosinc.com, guoren@kernel.org, samitolvanen@google.com, evan@rivosinc.com, xiao.w.wang@intel.com, apatel@ventanamicro.com, mchitale@ventanamicro.com, waylingii@gmail.com, greentime.hu@sifive.com, heiko@sntech.de, jszhang@kernel.org, shikemeng@huaweicloud.com, david@redhat.com, charlie@rivosinc.com, panqinglin2020@iscas.ac.cn, willy@infradead.org, vincent.chen@sifive.com, andy.chiu@sifive.com, gerg@kernel.org, jeeheng.sia@starfivetech.com, mason.huo@starfivetech.com, ancientmodern4@gmail.com, mathis.salmen@matsal.de, cuiyunhui@bytedance.com, bhe@redhat.com, chenjiahao16@huawei.com, ruscur@russell.cc, bgray@linux.ibm.com, alx@kernel.org, baruch@tkos.co.il, zhangqing@loongson.cn, catalin.marinas@arm.com, revest@chromium.org, josh@joshtriplett.org, joey.gouly@arm.com, shr@devkernel.io, omosnace@redhat.com, ojeda@kernel.org, jhubbard@nvidia.com, linux-doc@vger.kernel.org, linux-riscv@lists.infradead.org, linux-kernel@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-kselftest@vger.kernel.org Subject: [RFC PATCH v1 20/28] riscv: Implements arch argnostic indirect branch tracking prctls Date: Wed, 24 Jan 2024 22:21:45 -0800 Message-ID: <20240125062739.1339782-21-debug@rivosinc.com> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20240125062739.1339782-1-debug@rivosinc.com> References: <20240125062739.1339782-1-debug@rivosinc.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Deepak Gupta 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 | 17 +++++++- arch/riscv/kernel/usercfi.c | 74 ++++++++++++++++++++++++++++++++ 2 files changed, 90 insertions(+), 1 deletion(-) diff --git a/arch/riscv/include/asm/usercfi.h b/arch/riscv/include/asm/usercfi.h index 72bcfa773752..4bd10dcd48aa 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 */ @@ -29,6 +31,8 @@ void set_shstk_base(struct task_struct *task, unsigned long shstk_addr, unsigned 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); +bool is_indir_lp_enabled(struct task_struct *task); +bool is_indir_lp_locked(struct task_struct *task); #define PR_SHADOW_STACK_SUPPORTED_STATUS_MASK (PR_SHADOW_STACK_ENABLE) @@ -66,6 +70,17 @@ static inline bool is_shstk_locked(struct task_struct *task) return false; } +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; +} + #endif /* CONFIG_RISCV_USER_CFI */ #endif /* __ASSEMBLY__ */ diff --git a/arch/riscv/kernel/usercfi.c b/arch/riscv/kernel/usercfi.c index be3a071272d8..af8cc8f4616c 100644 --- a/arch/riscv/kernel/usercfi.c +++ b/arch/riscv/kernel/usercfi.c @@ -67,6 +67,30 @@ 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; +} + +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 @@ -374,3 +398,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; +} -- 2.43.0