From patchwork Fri Jun 16 19:16:54 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: 109323 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp1565356vqr; Fri, 16 Jun 2023 12:23:14 -0700 (PDT) X-Google-Smtp-Source: ACHHUZ5gSrwZzNpofWLzoA2u1MNqejX4lmWaOnKFmBMGI1CMeeQezgF1hJyAjw3CWZxnM9t2IhzH X-Received: by 2002:a17:90a:1cb:b0:25e:a5b2:846d with SMTP id 11-20020a17090a01cb00b0025ea5b2846dmr2180207pjd.27.1686943393638; Fri, 16 Jun 2023 12:23:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1686943393; cv=none; d=google.com; s=arc-20160816; b=tEiFYKyyBehPaUm1bGrRblcYyYcivDhrT899fbUHtq0/c+hP3kEoKBdBRfYaOR6FBj 0sYh/UDD7IVHZL0J6ntb5B9sH+/RlQa3RI02xFvApv7aEv0jpz2URykfQ9+C9cm11i8O TYKenAqlefNPUIXDl0zDYbpTTOZtJYuUqttX6yOLX/7CCXmJjFG9cjsdLFwO4nYiz5AD 5TjGFvoEHiXXZibY7/jjoA+T2j6TGth0gEALQU9AxRko5SSPE6U+6w+45LoYVrtoJoM3 RqZLXdP0sb6E3+LAqFlB/tm23JIAlXVCGYqumma4xVRPQR6cwMkLJ4ljlupTL6yCWkW8 sR3A== 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=zO2B8V07am/QeVtBa6uKJ5WNKhTuPbw3pt8o70zvrPw=; b=icaBja9l0SgXP2G8EyLiAvWlXpp142dzdbQdA6CGyT5hAmxC0WRphngHIu9P9i1eWe HQzIOvG4M1LoOrZxVCU8LLP+yfG7XJVDnDzr7MMlpwM4ByqhsubJwD5VkOAwUGiI9Owe EkXH204J0skNhrVpN5RnPFj2lSDV89jg02B54CtTbMZfbWhHiDO5YR3FKgql3fu4cRdK RMFSwgSPkoIFTw92zg776ibxTMWGgtl0H2JLpZTFQARwZZ7fNAqDL3wxcfUYCo8Qf89A GiELc1TyY9gqlUenfGosbIT6x5OOmywvbjWlZGb44urO0YWAEr2oG2Kf39RecCXFthgh TeSA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linutronix.de header.s=2020 header.b=ZQVVc88l; 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 i10-20020a639d0a000000b00534849cb1afsi15667689pgd.593.2023.06.16.12.23.01; Fri, 16 Jun 2023 12:23:13 -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=ZQVVc88l; 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 S1346068AbjFPTRd (ORCPT + 99 others); Fri, 16 Jun 2023 15:17:33 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47962 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345778AbjFPTQ6 (ORCPT ); Fri, 16 Jun 2023 15:16:58 -0400 Received: from galois.linutronix.de (Galois.linutronix.de [193.142.43.55]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 964E535B0; Fri, 16 Jun 2023 12:16:56 -0700 (PDT) Date: Fri, 16 Jun 2023 19:16:54 -0000 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020; t=1686943015; 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=zO2B8V07am/QeVtBa6uKJ5WNKhTuPbw3pt8o70zvrPw=; b=ZQVVc88ls6j3xXWn9BTZvQXVaEM2ltYvjS5abtetrf09wfsEotIB18d3Z8pO4WvnK6D7Wh OMYGvIOsxUmi8s2LJj8sHP6ILJBexrzuW3BMhuAkSF4T3nnXuK6PaYxqyirUweWb1KUpjX thApL7vlIUtarf2bkg+r+z2NItTqL9RBt8tLWjQviO5UJihqBDvChO9+yjLhq01TBafBm3 c+ktgye4S5yJHUr8EAatL9dG7qzMCBHdVaU7VSkNqgMJy+BaTnj/NSPop7POElD9II5yZ+ InJE/+NWqJBLVCm+dFtCGqOjKkpdGnNpz8TM4LJva9DK0dt8pOYxPqsJqtC3qQ== DKIM-Signature: v=1; a=ed25519-sha256; c=relaxed/relaxed; d=linutronix.de; s=2020e; t=1686943015; 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=zO2B8V07am/QeVtBa6uKJ5WNKhTuPbw3pt8o70zvrPw=; b=R08ZrSroiTmbdHOuQc+yIzXjTrX18aYlfZqFIN0m1FEw8tIO+S1+OxcdyrgIA8+dsWrHNu FfP1KA6WhlYbRsDw== 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: <168694301460.404.1265547288806195648.tip-bot2@tip-bot2> Robot-ID: Robot-Unsubscribe: Contact to get blacklisted from these emails X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1768888356028409983?= X-GMAIL-MSGID: =?utf-8?q?1768888356028409983?= The following commit has been merged into the x86/shstk branch of tip: Commit-ID: 1858e1bc9f6236c4d92e1a11caba6e67ab7b5163 Gitweb: https://git.kernel.org/tip/1858e1bc9f6236c4d92e1a11caba6e67ab7b5163 Author: Rick Edgecombe AuthorDate: Mon, 12 Jun 2023 17:10:59 -07:00 Committer: Dave Hansen CommitterDate: Thu, 15 Jun 2023 16:31:34 -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)