From patchwork Wed Aug 2 22:19:31 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: tip-bot2 for Thomas Gleixner X-Patchwork-Id: 130202 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9f41:0:b0:3e4:2afc:c1 with SMTP id v1csp781695vqx; Wed, 2 Aug 2023 16:06:19 -0700 (PDT) X-Google-Smtp-Source: APBJJlG/8sLlnL230ZLydrN7KaM4mNc/dvmrxV50S1sUEA+gzRbK/dZ8t30yEXluKvTK6VpGdddU X-Received: by 2002:a17:902:da84:b0:1bb:ac37:384b with SMTP id j4-20020a170902da8400b001bbac37384bmr17812199plx.6.1691017579253; Wed, 02 Aug 2023 16:06:19 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691017579; cv=none; d=google.com; s=arc-20160816; b=Ra/g8+MO0taYz+3YHAkqH2UuhfLFme2fiybstI/8IbC+E2Vj9rj6q4DwDD5UnOwAZZ LMc7fLPjNeSRq1Sty56l1u3D0SHTe3/cVlceN4kcxGQrfUt8l6cFRVNTCDDZlMy8FEN1 Hu5+1ZItK/ujlQQTy5rPCFuv017i/R+F1AlQmOLAlcOfMYdKRtN4/VnT62EbCx/8xJez IpZjYjLuQVVpdsjUPjGCxD4H/O1ALVJ0O/vHNvoJcrkefJptXq7o8tJ5WLNBZ3xAA6b0 zULO7sv7Jd/KlnZ3IXevyeLnjQuJpczFegs9OXoBwKpoBPDUEdQKrXdgkqAp1YvmJjti ohIg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:robot-unsubscribe :robot-id:message-id:mime-version:cc:subject:to:reply-to:sender:from :dkim-signature:dkim-signature:date; bh=IstEpb6mzOZLU9hL9T/45QOiURs2kmDy8oax4r7RTL0=; fh=DErNX+FP1i15oz3qs87i1gR330nFAaK00aIY9hllqPU=; b=CZnihcyClxEkNxFH4Um1yhnzGgYPAyx1XFy4Ff++2n15iORNEL7RKAr7y4bawJQHPS VkTk8tE8mEtmYznrLCWiujYvax6MkckIxAaYbZkNmKGVTpkzYcDvPSVntXl5AzW+zSaD K4y8JmAGrXKo/ibLGx/LIeGYYParHbHSMaR+wxBlKgC/88zKOk7Ymk1gtgbYR++P1i+i YcRn0ggbA3W2//cctxDXQWSTPmoLFrg9EBJZRFTIfx1EDfJV134pVs4WE+wo8H9yVo4B SRBT5qjZ8Gii/iebD4xnJJopO8BS4pQ0D3acMnejwK4iC+tCQHdBeCmxNhXtAoFmX71u gLWw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=uTUYZJJZ; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id ky13-20020a170902f98d00b001bbb33d3c3esi1950884plb.559.2023.08.02.16.06.05; Wed, 02 Aug 2023 16:06:19 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=uTUYZJJZ; dkim=neutral (no key) header.i=@linutronix.de; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=QUARANTINE dis=NONE) header.from=linutronix.de Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S233985AbjHBWUZ (ORCPT + 99 others); Wed, 2 Aug 2023 18:20:25 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:60312 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232855AbjHBWTh (ORCPT ); Wed, 2 Aug 2023 18:19:37 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60325272D; Wed, 2 Aug 2023 15:19:34 -0700 (PDT) Date: Wed, 02 Aug 2023 22:19:31 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1691014772; h=from:from:sender:sender:reply-to: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; bh=IstEpb6mzOZLU9hL9T/45QOiURs2kmDy8oax4r7RTL0=; b=uTUYZJJZ1Aocok0W2Qli8xsKXuoqK7OZJkq2U03aMzgaf9ZhZQICZL+0ZjgEj1MvUha590 AjqymAefyZzXVzM0on5xEq6wdu9wHq6E3+X9hwHIb4FSIgZhMsBEWyGcv13OBws6XAhoKx bzwgLx8RWZeG7j8W0wYS5SzUmyHZv6JO+bUDAU/BAeKv00Bp/fyPjK3zvUIwL9W8MzgNOi TbrktCrvZfWzuaXyFwbang3sAplh3YmggUXwX00XWQtcPd8ixx0cLhfHo7Ro+Hbaj5cF20 uRiLCYLMtEG9yZogQBKv50So+G+ybOUcF/gJbCO4tgnGFr6UcL7cl2Ogb3S5Gg== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1691014772; h=from:from:sender:sender:reply-to: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; bh=IstEpb6mzOZLU9hL9T/45QOiURs2kmDy8oax4r7RTL0=; b=07KvcmKweG5u2W3uqHdQKiK1P2fm82gRdofbdMZG7w7dqNX9oxn5Sb8gxPdkCZh3VEPw/J KM2eNunR1tZhIECg== From: "tip-bot2 for Rick Edgecombe" Sender: tip-bot2@linutronix.de Reply-to: linux-kernel@vger.kernel.org To: linux-tip-commits@vger.kernel.org Subject: [tip: x86/shstk] x86/shstk: Check that signal frame is shadow stack mem Cc: Rick Edgecombe , Dave Hansen , x86@kernel.org, linux-kernel@vger.kernel.org MIME-Version: 1.0 Message-ID: <169101477165.28540.13769364032686646171.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-2.1 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_BLOCKED, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1773160449306028626 X-GMAIL-MSGID: 1773160449306028626 The following commit has been merged into the x86/shstk branch of tip: Commit-ID: 7fad2a432cd35bbf104d2d9d426e74902f22aa95 Gitweb: https://git.kernel.org/tip/7fad2a432cd35bbf104d2d9d426e74902f22aa95 Author: Rick Edgecombe AuthorDate: Mon, 12 Jun 2023 17:10:59 -07:00 Committer: Dave Hansen CommitterDate: Wed, 02 Aug 2023 15:01:50 -07:00 x86/shstk: Check that signal frame is shadow stack mem The shadow stack signal frame is read by the kernel on sigreturn. It relies on shadow stack memory protections to prevent forgeries of this signal frame (which included the pre-signal SSP). This behavior helps userspace protect itself. However, using the INCSSP instruction userspace can adjust the SSP to 8 bytes beyond the end of a shadow stack. INCSSP performs shadow stack reads to make sure it doesn’t increment off of the shadow stack, but on the end position it actually reads 8 bytes below the new SSP. For the shadow stack HW operations, this situation (INCSSP off the end of a shadow stack by 8 bytes) would be fine. If the a RET is executed, the push to the shadow stack would fail to write to the shadow stack. If a CALL is executed, the SSP will be incremented back onto the stack and the return address will be written successfully to the very end. That is expected behavior around shadow stack underflow. However, the kernel doesn’t have a way to read shadow stack memory using shadow stack accesses. WRUSS can write to shadow stack memory with a shadow stack access which ensures the access is to shadow stack memory. But unfortunately for this case, there is no equivalent instruction for shadow stack reads. So when reading the shadow stack signal frames, the kernel currently assumes the SSP is pointing to the shadow stack and uses a normal read. The SSP pointing to shadow stack memory will be true in most cases, but as described above, in can be untrue by 8 bytes. So lookup the VMA of the shadow stack sigframe being read to verify it is shadow stack. Since the SSP can only be beyond the shadow stack by 8 bytes, and shadow stack memory is page aligned, this check only needs to be done when this type of relative position to a page boundary is encountered. So skip the extra work otherwise. Signed-off-by: Rick Edgecombe Signed-off-by: Dave Hansen Link: https://lore.kernel.org/all/20230613001108.3040476-34-rick.p.edgecombe%40intel.com --- arch/x86/kernel/shstk.c | 31 +++++++++++++++++++++++++++++-- 1 file changed, 29 insertions(+), 2 deletions(-) diff --git a/arch/x86/kernel/shstk.c b/arch/x86/kernel/shstk.c index a8705f7..50733a5 100644 --- a/arch/x86/kernel/shstk.c +++ b/arch/x86/kernel/shstk.c @@ -249,15 +249,38 @@ static int shstk_push_sigframe(unsigned long *ssp) static int shstk_pop_sigframe(unsigned long *ssp) { + struct vm_area_struct *vma; unsigned long token_addr; - int err; + bool need_to_check_vma; + int err = 1; + /* + * It is possible for the SSP to be off the end of a shadow stack by 4 + * or 8 bytes. If the shadow stack is at the start of a page or 4 bytes + * before it, it might be this case, so check that the address being + * read is actually shadow stack. + */ if (!IS_ALIGNED(*ssp, 8)) return -EINVAL; + need_to_check_vma = PAGE_ALIGN(*ssp) == *ssp; + + if (need_to_check_vma) + mmap_read_lock_killable(current->mm); + err = get_shstk_data(&token_addr, (unsigned long __user *)*ssp); if (unlikely(err)) - return err; + goto out_err; + + if (need_to_check_vma) { + vma = find_vma(current->mm, *ssp); + if (!vma || !(vma->vm_flags & VM_SHADOW_STACK)) { + err = -EFAULT; + goto out_err; + } + + mmap_read_unlock(current->mm); + } /* Restore SSP aligned? */ if (unlikely(!IS_ALIGNED(token_addr, 8))) @@ -270,6 +293,10 @@ static int shstk_pop_sigframe(unsigned long *ssp) *ssp = token_addr; return 0; +out_err: + if (need_to_check_vma) + mmap_read_unlock(current->mm); + return err; } int setup_signal_shadow_stack(struct ksignal *ksig)