From patchwork Tue Sep 19 13:33:00 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Evgeny Yakovlev X-Patchwork-Id: 9959001 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 2E5EA60568 for ; Tue, 19 Sep 2017 13:41:29 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id 0AF3328F9C for ; Tue, 19 Sep 2017 13:41:29 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id 0970828EB1; Tue, 19 Sep 2017 13:41:29 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-7.0 required=2.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, RCVD_IN_DNSWL_HI autolearn=ham version=3.3.1 Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id CAF1F28ED2 for ; Tue, 19 Sep 2017 13:40:58 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1750974AbdISNk5 (ORCPT ); Tue, 19 Sep 2017 09:40:57 -0400 Received: from forwardcorp1o.cmail.yandex.net ([37.9.109.47]:50823 "EHLO forwardcorp1o.cmail.yandex.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1750822AbdISNk4 (ORCPT ); Tue, 19 Sep 2017 09:40:56 -0400 X-Greylist: delayed 468 seconds by postgrey-1.27 at vger.kernel.org; Tue, 19 Sep 2017 09:40:55 EDT Received: from smtpcorp1o.mail.yandex.net (smtpcorp1o.mail.yandex.net [IPv6:2a02:6b8:0:1a2d::30]) by forwardcorp1o.cmail.yandex.net (Yandex) with ESMTP id 59DB420EAB for ; Tue, 19 Sep 2017 16:33:06 +0300 (MSK) Received: from smtpcorp1o.mail.yandex.net (localhost.localdomain [127.0.0.1]) by smtpcorp1o.mail.yandex.net (Yandex) with ESMTP id 5849E2440D7C for ; Tue, 19 Sep 2017 16:33:06 +0300 (MSK) Received: from unknown (unknown [2a02:6b8:0:40c:34b4:5e0c:be02:4233]) by smtpcorp1o.mail.yandex.net (nwsmtp/Yandex) with ESMTPSA id WqLsilBt45-X6VapDTx; Tue, 19 Sep 2017 16:33:06 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex-team.ru; s=default; t=1505827986; bh=6U06TVuDkkwnwpMYLVPxV65nZhG7ER43+WNJotHJ1Sw=; h=From:To:Subject:Date:Message-Id; b=mIftxB+fIaKIx1pFJ86Iw9zC/3yjRkPQINsyIuxwkMzN5+pPuDZaMrUybiCJANChO Hav0vI8zS+1B+y9b1rpM5YihNLIFnBEO4I1TWVgSq/a5M0mwgmzCn1zVvL6sY1URyK K/XoMFPdG66LGSJYf0S1n+YdrrPQKf4WHqBGXSys= Authentication-Results: smtpcorp1o.mail.yandex.net; dkim=pass header.i=@yandex-team.ru From: Evgeny Yakovlev To: kvm@vger.kernel.org Subject: [kvm-unit-tests PATCH] x86: access: revert PTE changes if CR4.SMEP change failed Date: Tue, 19 Sep 2017 16:33:00 +0300 Message-Id: <1505827980-9351-1-git-send-email-wrfsh@yandex-team.ru> X-Mailer: git-send-email 2.7.4 Sender: kvm-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: kvm@vger.kernel.org X-Virus-Scanned: ClamAV using ClamSMTP When calling set_cr4_smep(1) to enable SMEP implementation will first drop user access bit in ptl2 and then attempt to change actual cr4 value. In case emulated CPU does not support setting CR4.SMEP this will generate a GP which we expect. However, in that case we should also revert user access bit change. Othervise supervisor access sticks and later faults the test binary. Signed-off-by: Evgeny Yakovlev --- x86/access.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/x86/access.c b/x86/access.c index a0c19dc..ccdaefc 100644 --- a/x86/access.c +++ b/x86/access.c @@ -195,6 +195,7 @@ unsigned set_cr4_smep(int smep) { unsigned long cr4 = read_cr4(); unsigned long old_cr4 = cr4; + unsigned long ptl2_access; extern u64 ptl2[]; unsigned r; @@ -204,9 +205,15 @@ unsigned set_cr4_smep(int smep) if (old_cr4 == cr4) return 0; + ptl2_access = ptl2[2]; if (smep) ptl2[2] &= ~PT_USER_MASK; r = write_cr4_checking(cr4); + if (cr4 != read_cr4()) { + if (smep) + ptl2[2] = ptl2_access; + return r; + } if (!smep) ptl2[2] |= PT_USER_MASK; return r;