Received: by 2002:a05:6a10:f3d0:0:0:0:0 with SMTP id a16csp2862073pxv; Mon, 12 Jul 2021 03:51:28 -0700 (PDT) X-Google-Smtp-Source: ABdhPJykcb88nFdzNzOD/QG9zmTtsER0s1P8DdhFHbPlPyxk02NOki46+5frx2YQ5TDPYfQhE+yQ X-Received: by 2002:a6b:e40d:: with SMTP id u13mr28249056iog.100.1626087088533; Mon, 12 Jul 2021 03:51:28 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1626087088; cv=none; d=google.com; s=arc-20160816; b=HtBFjMHXZw2qheq5Be6AcGoF0nkvRzGGq5aqQYjGMhbrT+9X/Y/d5At2V8RxhBPMVO TyA5vmZg2T5pnZK7k4AtWqStdItHTWcjGb+Ls+MH9AZPmGgHjq7H8VytjAszgTpmIsGG xBoM7mYOZt0UzmarvFlyv3grijPnE6eoJrj/gi50ggMZNr1WCfQ7/mvYQTyxcYq7jGfb J+MHdPFePj49JNowkvz+GAru9AZWGfykZlZ9EYl+XlAfis+tF6F6096IYjyjgUfeQzU5 Dr+mLxaddexrwhmkPtoLxha/M2J5i0gqWglT9jUpcG49ptd4wmKkikvh42y79jLiqr8e laoQ== 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=olr465FZVfmwphl2A08mUEuvEECOGkIkM4RzApX0f/wvEsbwgYHadmeXULXH0x4irR ywbWQXBDvf4CSVMgW4EIv20kHWFDWy3ixRCd1vj8Tw0lnS8aD2nKUmiQONRknu4gMUlk mHJ6CVUgk8jLZSqotjx1I6vTYmV8y3i+5PXcpcdml8X1fQRynqeRgB8YG+yY1I2JEHS7 OwoHgSerRE+9D2B+qPfn82ve6XQZfYl3LXTdPB3w2lvLpLSva0dQScoX9nBh0r5Mvevj Ri5Rq90cZgKdMgbf62fJ94PZL6cKzKFMh8ieVqrz8pDV+CLB0sRTISz8rYam/gyvFiV6 eQVw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=z+Jca37D; 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 k25si7785882jan.106.2021.07.12.03.51.17; Mon, 12 Jul 2021 03:51:28 -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=z+Jca37D; 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 S1355832AbhGLIPw (ORCPT + 99 others); Mon, 12 Jul 2021 04:15:52 -0400 Received: from mail.kernel.org ([198.145.29.99]:43440 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345493AbhGLH3y (ORCPT ); Mon, 12 Jul 2021 03:29:54 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 218A96052B; Mon, 12 Jul 2021 07:26:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1626074812; bh=92RQ7ovfVPIika+3PGDd5//JInAuNVNk4o1IcirZSxM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=z+Jca37D1yyWRQoQaBJd2rcuiDzsEskM6fL1lBc67H4HniKYKvzhLEOkLfxZEpyG1 RYdsSCZi62EzsE8gwG1kUbRoa7MuCSevGyrrXnA12jWNK6FR/goL8CQ2xwvEc7Eaop pAsKDXi6ZlhCU9hYlN+KQCteYMEqdkGPFu8dB2O0= 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.12 677/700] selftests/vm/pkeys: refill shadow register after implicit kernel write Date: Mon, 12 Jul 2021 08:12:40 +0200 Message-Id: <20210712061047.897155351@linuxfoundation.org> X-Mailer: git-send-email 2.32.0 In-Reply-To: <20210712060924.797321836@linuxfoundation.org> References: <20210712060924.797321836@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