Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2832386pxv; Mon, 12 Jul 2021 03:03:44 -0700 (PDT) X-Google-Smtp-Source: ABdhPJyLeE4A7g357XSE4CqVUafNNZyp4MpmrNsHjZr08GaseChx6ULRZRwUGaIBVPUqPPdTKgDz X-Received: by 2002:a05:6402:51c9:: with SMTP id r9mr62951431edd.326.1626084224427; Mon, 12 Jul 2021 03:03:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626084224; cv=none; d=google.com; s=arc-20160816; b=G6r56NG3EJtP+JLjlSLCG6MFiv16ARVrmN3r0HpdunQzmjWY6sYC5Dm8MAKH5R445Y P2SwmGr9saOoJtyEoZfM+zRuijwDEE2d/GEr/LShL6y4jYRRVAMgQEd7Obk0731g1z7Y hkFckCTY9w6L9xWuTAZ5EJNkqkXSeWbCmZf17TCbRgKlRbcHlAVXX0sm6qvVIzALAaAi IK2aGGAkaHW9AeZYxywwGxE414qDFGDe5kotNG8i5B/+hxRpKvyNK11/wuneilaBwuPP VWkxMuPdOqTqwAiNxcDXgwUcIrn9C3cHHVjfJGkS8ygubonuc0IpvQmeSemFOEMGcAKB Solg== 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=bEEwr3DT/8O+1kvR2/Cv5gEdttITJ2XsWcFl2yQ13KI=; b=nZLnfUmK0Nn5QnggzWm2p/iYw3XR9qWqtfJFCjklA/jpl2bTHNSt+lgdFeOtmwlMeL AurS5K5SW6a6dW6m1NcwiFJn2reQ6grjf2fPiJhXjqCKPN8/KM7v5dpzMPm6S4sm3al+ 4GVm8SEeO23BHbHSJmuw66BkyAa7bfQVhzwAr5+Ft1uM7WE9tFIxfXBaAON2U4bvs7vF oUKGTjg0YdviLC5rvk29yoUeFR7otmHeAqjHnge4HObSUcc6NPNl9rEnoQ5U4nBacJeV 73wEbB3Bj//Ei3eq0+ENkxXUDkj/wMswxdBNCkZ5nrO5G9MyG6w3nT7vwvZtOXopewjP zEqA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=xCasC8rT; 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 p20si16108624ejr.572.2021.07.12.03.03.21; Mon, 12 Jul 2021 03:03:44 -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=xCasC8rT; 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 S244676AbhGLHSe (ORCPT + 99 others); Mon, 12 Jul 2021 03:18:34 -0400 Received: from mail.kernel.org ([198.145.29.99]:53144 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240670AbhGLGyD (ORCPT ); Mon, 12 Jul 2021 02:54:03 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id DAE5B60233; Mon, 12 Jul 2021 06:51:09 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626072670; bh=92RQ7ovfVPIika+3PGDd5//JInAuNVNk4o1IcirZSxM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=xCasC8rTT5uPB7H6CbKxumLMXyLVA7OGR0rYRZn137PMfe0LfbyWxROGZJmu69/mm DI0jmdmv0kbd18lg+7v/1xqsWm+3PVp+K91FuouL3utfgEgOP8auFA7u8tcVmkjd6b k8YlbIm/YZ95MwF544WLxLiEtNz1CHHFa9DRr28o= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dave Hansen , Thomas Gleixner , "Aneesh Kumar K.V" , Ram Pai , Sandipan Das , Florian Weimer , "Desnes A. Nunes do Rosario" , Ingo Molnar , Thiago Jung Bauermann , Michael Ellerman , Michal Hocko , Michal Suchanek , Shuah Khan , Andrew Morton , Linus Torvalds , Sasha Levin Subject: [PATCH 5.10 576/593] selftests/vm/pkeys: refill shadow register after implicit kernel write Date: Mon, 12 Jul 2021 08:12:16 +0200 Message-Id: <20210712060958.420043834@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060843.180606720@linuxfoundation.org> References: <20210712060843.180606720@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: Dave Hansen [ Upstream commit 6039ca254979694c5362dfebadd105e286c397bb ] The pkey test code keeps a "shadow" of the pkey register around. This ensures that any bugs which might write to the register can be caught more quickly. Generally, userspace has a good idea when the kernel is going to write to the register. For instance, alloc_pkey() is passed a permission mask. The caller of alloc_pkey() can update the shadow based on the return value and the mask. But, the kernel can also modify the pkey register in a more sneaky way. For mprotect(PROT_EXEC) mappings, the kernel will allocate a pkey and write the pkey register to create an execute-only mapping. The kernel never tells userspace what key it uses for this. This can cause the test to fail with messages like: protection_keys_64.2: pkey-helpers.h:132: _read_pkey_reg: Assertion `pkey_reg == shadow_pkey_reg' failed. because the shadow was not updated with the new kernel-set value. Forcibly update the shadow value immediately after an mprotect(). Link: https://lkml.kernel.org/r/20210611164200.EF76AB73@viggo.jf.intel.com Fixes: 6af17cf89e99 ("x86/pkeys/selftests: Add PROT_EXEC test") Signed-off-by: Dave Hansen Signed-off-by: Thomas Gleixner Tested-by: Aneesh Kumar K.V Cc: Ram Pai Cc: Sandipan Das Cc: Florian Weimer Cc: "Desnes A. Nunes do Rosario" Cc: Ingo Molnar Cc: Thiago Jung Bauermann Cc: Michael Ellerman Cc: Michal Hocko Cc: Michal Suchanek Cc: Shuah Khan Signed-off-by: Andrew Morton Signed-off-by: Linus Torvalds Signed-off-by: Sasha Levin --- tools/testing/selftests/vm/protection_keys.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/tools/testing/selftests/vm/protection_keys.c b/tools/testing/selftests/vm/protection_keys.c index 356d62fca27f..87eecd5ba577 100644 --- a/tools/testing/selftests/vm/protection_keys.c +++ b/tools/testing/selftests/vm/protection_keys.c @@ -1448,6 +1448,13 @@ void test_implicit_mprotect_exec_only_memory(int *ptr, u16 pkey) ret = mprotect(p1, PAGE_SIZE, PROT_EXEC); pkey_assert(!ret); + /* + * Reset the shadow, assuming that the above mprotect() + * correctly changed PKRU, but to an unknown value since + * the actual alllocated pkey is unknown. + */ + shadow_pkey_reg = __read_pkey_reg(); + dprintf2("pkey_reg: %016llx\n", read_pkey_reg()); /* Make sure this is an *instruction* fault */ -- 2.30.2