Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp3920078pxj; Mon, 21 Jun 2021 09:23:09 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzhI6NXye+GpyaQE1czQBv2Nx5phVE47x7TVBl3uby4A+Y0jx2T6W9XNh4FkAvM29wxhnxp X-Received: by 2002:a05:6402:49:: with SMTP id f9mr22572938edu.178.1624292589105; Mon, 21 Jun 2021 09:23:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1624292589; cv=none; d=google.com; s=arc-20160816; b=HWI/3G/0I8HOO+Rb4paXC2nPbhPlt0CLLGzUB+haVXbNAG+PFaPMRJUECuf6bmFkfx Cp/PRApFZsnD7tbHiBvCYRpqWqHiqFItfIVG9lE9e/Pzpg4g5+PxChYT7+gqqKptOp0c FOokKPjk7dFvLlDuy+chmTD6xIBW7YRY2GklfQMWAYGllKUgAajb+RKU2TI79cFXcLuS kWMb5UGFAM5cUoyqumXr+yJixjqa/1gjVMcsnH/bJS1O4Bo9RmOzORsSYVv6tAnfeWO5 RbvX1jfwucRGiOtPn6sjkS4++6ZpvVjgqsSlvFU0RReCo7muQvjVBONeX4aPTGIgB4aK Q2WA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=f2766aFIfIsPnMGElgmG/tYIb/IhlKhXsyMEpcaYO7E=; b=rgGForj5+S+Y+/PfBix0lqutp7d+p77lbGXBgW83IZ17VP7hJsoKJ+Fh/LGegXyjj7 Xb5v1ftXclgGuOI4cCwjMRS8gYMIkjWqP33dZzdrLO0R6a8NPtjiPCCNQEN8A2knmhVZ tO+dPSiu5+k6GGScx+MBUHjqV86aLVmBBAuEcOdn+4+i4l49VGOXVyjPENvGkgcpbyO6 JFsXQDDicP3wCvmBsyRgqCTiNxkKDM28VQZ+ZvGi6O0jE7Lu/t0OHc55ZsRTH4KLE++J 1sIPaTW0PWf6TAVM5HfmjeQXsGjfoQJvu2UQD5w5fZLuFY/mZF8HcAnX9GAquW7vJKWt MUhg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BA9Y8i0J; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id ml14si1839855ejb.205.2021.06.21.09.22.46; Mon, 21 Jun 2021 09:23:09 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=BA9Y8i0J; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231992AbhFUQXQ (ORCPT + 99 others); Mon, 21 Jun 2021 12:23:16 -0400 Received: from mail.kernel.org ([198.145.29.99]:41900 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231834AbhFUQV7 (ORCPT ); Mon, 21 Jun 2021 12:21:59 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 39B0161370; Mon, 21 Jun 2021 16:19:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1624292376; bh=RoLcPgWCvjSIo1JSDqIbHOHp+GAykiVT3pMa/3u0SCE=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=BA9Y8i0JnI13SCIj9e4jLxFieNHlOY63aKAEL7i2g1x/EQ7VCPd2vmqIJcSFoAtmx M6MHTYzfXl7SwEhRjuVymv9f2vA/Pc+mOqSoqLaMsT2IPCv7F37uf9ca+7Z1TxMAja /Wv1oRlg8inr2kB3jKr8iUGfj0GFDW+B9A8VhyTY= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Thomas Gleixner , Borislav Petkov , Dave Hansen , Rik van Riel , Babu Moger Subject: [PATCH 5.4 67/90] x86/pkru: Write hardware init value to PKRU when xstate is init Date: Mon, 21 Jun 2021 18:15:42 +0200 Message-Id: <20210621154906.431457997@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210621154904.159672728@linuxfoundation.org> References: <20210621154904.159672728@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Thomas Gleixner commit 510b80a6a0f1a0d114c6e33bcea64747d127973c upstream. When user space brings PKRU into init state, then the kernel handling is broken: T1 user space xsave(state) state.header.xfeatures &= ~XFEATURE_MASK_PKRU; xrstor(state) T1 -> kernel schedule() XSAVE(S) -> T1->xsave.header.xfeatures[PKRU] == 0 T1->flags |= TIF_NEED_FPU_LOAD; wrpkru(); schedule() ... pk = get_xsave_addr(&T1->fpu->state.xsave, XFEATURE_PKRU); if (pk) wrpkru(pk->pkru); else wrpkru(DEFAULT_PKRU); Because the xfeatures bit is 0 and therefore the value in the xsave storage is not valid, get_xsave_addr() returns NULL and switch_to() writes the default PKRU. -> FAIL #1! So that wrecks any copy_to/from_user() on the way back to user space which hits memory which is protected by the default PKRU value. Assumed that this does not fail (pure luck) then T1 goes back to user space and because TIF_NEED_FPU_LOAD is set it ends up in switch_fpu_return() __fpregs_load_activate() if (!fpregs_state_valid()) { load_XSTATE_from_task(); } But if nothing touched the FPU between T1 scheduling out and back in, then the fpregs_state is still valid which means switch_fpu_return() does nothing and just clears TIF_NEED_FPU_LOAD. Back to user space with DEFAULT_PKRU loaded. -> FAIL #2! The fix is simple: if get_xsave_addr() returns NULL then set the PKRU value to 0 instead of the restrictive default PKRU value in init_pkru_value. [ bp: Massage in minor nitpicks from folks. ] Fixes: 0cecca9d03c9 ("x86/fpu: Eager switch PKRU state") Signed-off-by: Thomas Gleixner Signed-off-by: Borislav Petkov Acked-by: Dave Hansen Acked-by: Rik van Riel Tested-by: Babu Moger Cc: stable@vger.kernel.org Link: https://lkml.kernel.org/r/20210608144346.045616965@linutronix.de Signed-off-by: Greg Kroah-Hartman --- arch/x86/include/asm/fpu/internal.h | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) --- a/arch/x86/include/asm/fpu/internal.h +++ b/arch/x86/include/asm/fpu/internal.h @@ -608,9 +608,16 @@ static inline void switch_fpu_finish(str * return to userland e.g. for a copy_to_user() operation. */ if (!(current->flags & PF_KTHREAD)) { + /* + * If the PKRU bit in xsave.header.xfeatures is not set, + * then the PKRU component was in init state, which means + * XRSTOR will set PKRU to 0. If the bit is not set then + * get_xsave_addr() will return NULL because the PKRU value + * in memory is not valid. This means pkru_val has to be + * set to 0 and not to init_pkru_value. + */ pk = get_xsave_addr(&new_fpu->state.xsave, XFEATURE_PKRU); - if (pk) - pkru_val = pk->pkru; + pkru_val = pk ? pk->pkru : 0; } __write_pkru(pkru_val); }