Received: by 2002:a05:6a10:a0d1:0:0:0:0 with SMTP id j17csp2531364pxa; Mon, 24 Aug 2020 17:34:05 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzZJRAMbd4icimrlE/w+zoL2WbQQiGdHoF42jLH9xTKB0EdpOVEPj03bH4GuokKZM7CVF5v X-Received: by 2002:a17:906:c34a:: with SMTP id ci10mr7915754ejb.356.1598315644925; Mon, 24 Aug 2020 17:34:04 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1598315644; cv=none; d=google.com; s=arc-20160816; b=zN/goq+qj0y+YTgdzBYksK7lt8j50zLnU1S1CS/3BN/oUvhh2UVB4Cab/2qnEoGH/T y6zJdgppNAyiAbQxxGSPpbqgQgmjLfuTnuvcX0N7MmQ2pycHbqxwYHEOCUxQyI85ypQR CBuRnmtTuvyJQQueVR3/QAO1v5h3mkZt+IeSrT7On+tl0YIEN93JYxTjzlNzIOWOccfT ZDZ96rNzRmu34YzHlU+XF1b3JqPqsyVf02RCOqvwj0BDHbBZKZbeGh5gBr3pB7pUr7o1 6Pfgbz5pNdxaLOKLvdljxFTdbp3b9AeHBC4BvUOpvplyN6uAtHNUvSsIbySNTMgfXlI+ SvuA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :ironport-sdr:ironport-sdr; bh=YxRrsSRertmcl5OMdijtwpDClR+43ZKQAvVY7qhJ9GY=; b=Eg8H3PDgBbkGO0k7I1di/HnIWbOFiLw2khC+MtfAPY1MoQvAGc6JWuQ3nIdZ7RikuZ WPnHBS+DSbGIC6H14GyfAEKRwUWdi9I4EJ3tI4tzSviYZWjbIcYuRfkk1woqKrVkd0FD lISte1iwbTDzxQnc+fB4C3oPe8qGo7LmBVH64AS5bB9u4oNxkCvfCkhw3g2UifFxMEx9 7NjhL35IasCzz90jMyszGkP3YHTm8AgNmRDnEvYKffFydmjFEPouAyBZimGEPNKxxPHJ zQQUBtv/bCxL3U4la6oRXdzonOlAFm4nPs2EqvNK8VUP1vcU4mCF3X05pcljRofpvm7r f4wQ== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id d4si1746767edz.16.2020.08.24.17.33.41; Mon, 24 Aug 2020 17:34:04 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728539AbgHYAa6 (ORCPT + 99 others); Mon, 24 Aug 2020 20:30:58 -0400 Received: from mga01.intel.com ([192.55.52.88]:28753 "EHLO mga01.intel.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728238AbgHYAai (ORCPT ); Mon, 24 Aug 2020 20:30:38 -0400 IronPort-SDR: i72NIM+bu4h6W69P2xI2mCKRUfca3lOH2fGeochdyo/eDPa7dxLxEEwBJ6QJy87YQ6zuzaflLo hrcoJI0GQKuA== X-IronPort-AV: E=McAfee;i="6000,8403,9723"; a="174053264" X-IronPort-AV: E=Sophos;i="5.76,350,1592895600"; d="scan'208";a="174053264" X-Amp-Result: SKIPPED(no attachment in message) X-Amp-File-Uploaded: False Received: from orsmga004.jf.intel.com ([10.7.209.38]) by fmsmga101.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Aug 2020 17:30:35 -0700 IronPort-SDR: V4+8RAz4lr57YJf6ppaQdnCN2Y+4ePc8I0Bx49RHfkv6ZLb0zEP9kczEe8WNzqeCpfswk8DtAh lrHmlnLDKi3g== X-IronPort-AV: E=Sophos;i="5.76,350,1592895600"; d="scan'208";a="443429307" Received: from yyu32-desk.sc.intel.com ([143.183.136.146]) by orsmga004-auth.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 24 Aug 2020 17:30:34 -0700 From: Yu-cheng Yu To: x86@kernel.org, "H. Peter Anvin" , Thomas Gleixner , Ingo Molnar , linux-kernel@vger.kernel.org, linux-doc@vger.kernel.org, linux-mm@kvack.org, linux-arch@vger.kernel.org, linux-api@vger.kernel.org, Arnd Bergmann , Andy Lutomirski , Balbir Singh , Borislav Petkov , Cyrill Gorcunov , Dave Hansen , Eugene Syromiatnikov , Florian Weimer , "H.J. Lu" , Jann Horn , Jonathan Corbet , Kees Cook , Mike Kravetz , Nadav Amit , Oleg Nesterov , Pavel Machek , Peter Zijlstra , Randy Dunlap , "Ravi V. Shankar" , Vedvyas Shanbhogue , Dave Martin , Weijiang Yang Cc: Yu-cheng Yu Subject: [PATCH v11 3/9] x86/cet/ibt: Handle signals for Indirect Branch Tracking Date: Mon, 24 Aug 2020 17:26:38 -0700 Message-Id: <20200825002645.3658-4-yu-cheng.yu@intel.com> X-Mailer: git-send-email 2.21.0 In-Reply-To: <20200825002645.3658-1-yu-cheng.yu@intel.com> References: <20200825002645.3658-1-yu-cheng.yu@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org An indirect CALL/JMP moves the indirect branch tracking (IBT) state machine to WAIT_ENDBR status until the instruction reaches an ENDBR opcode. If the CALL/JMP does not reach an ENDBR opcode, the processor raises a control- protection fault. WAIT_ENDBR status can be read from MSR_IA32_U_CET. WAIT_ENDBR is cleared for signal handling, and restored for sigreturn. IBT state machine is described in Intel SDM Vol. 1, Sec. 18.3. Signed-off-by: Yu-cheng Yu --- v9: - Fix missing WAIT_ENDBR in signal handling. arch/x86/kernel/cet.c | 27 +++++++++++++++++++++++++-- arch/x86/kernel/fpu/signal.c | 8 +++++--- 2 files changed, 30 insertions(+), 5 deletions(-) diff --git a/arch/x86/kernel/cet.c b/arch/x86/kernel/cet.c index b1c122a5aef4..f783229460b6 100644 --- a/arch/x86/kernel/cet.c +++ b/arch/x86/kernel/cet.c @@ -309,6 +309,13 @@ void cet_restore_signal(struct sc_ext *sc_ext) msr_val |= CET_SHSTK_EN; } + if (cet->ibt_enabled) { + msr_val |= (CET_ENDBR_EN | CET_NO_TRACK_EN); + + if (sc_ext->wait_endbr) + msr_val |= CET_WAIT_ENDBR; + } + if (test_thread_flag(TIF_NEED_FPU_LOAD)) cet_user_state->user_cet = msr_val; else @@ -349,9 +356,25 @@ int cet_setup_signal(bool ia32, unsigned long rstor_addr, struct sc_ext *sc_ext) sc_ext->ssp = new_ssp; } - if (ssp) { + if (ssp || cet->ibt_enabled) { + start_update_msrs(); - wrmsrl(MSR_IA32_PL3_SSP, ssp); + + if (ssp) + wrmsrl(MSR_IA32_PL3_SSP, ssp); + + if (cet->ibt_enabled) { + u64 r; + + rdmsrl(MSR_IA32_U_CET, r); + + if (r & CET_WAIT_ENDBR) { + sc_ext->wait_endbr = 1; + r &= ~CET_WAIT_ENDBR; + wrmsrl(MSR_IA32_U_CET, r); + } + } + end_update_msrs(); } diff --git a/arch/x86/kernel/fpu/signal.c b/arch/x86/kernel/fpu/signal.c index d02ea8c11128..a4d66fa69c1c 100644 --- a/arch/x86/kernel/fpu/signal.c +++ b/arch/x86/kernel/fpu/signal.c @@ -57,7 +57,8 @@ int save_cet_to_sigframe(int ia32, void __user *fp, unsigned long restorer) { int err = 0; - if (!current->thread.cet.shstk_size) + if (!current->thread.cet.shstk_size && + !current->thread.cet.ibt_enabled) return 0; if (fp) { @@ -89,7 +90,8 @@ static int get_cet_from_sigframe(int ia32, void __user *fp, struct sc_ext *ext) memset(ext, 0, sizeof(*ext)); - if (!current->thread.cet.shstk_size) + if (!current->thread.cet.shstk_size && + !current->thread.cet.ibt_enabled) return 0; if (fp) { @@ -577,7 +579,7 @@ static unsigned long fpu__alloc_sigcontext_ext(unsigned long sp) * sigcontext_ext is at: fpu + fpu_user_xstate_size + * FP_XSTATE_MAGIC2_SIZE, then aligned to 8. */ - if (cet->shstk_size) + if (cet->shstk_size || cet->ibt_enabled) sp -= (sizeof(struct sc_ext) + 8); return sp; -- 2.21.0