Received: by 2002:a05:6a10:af89:0:0:0:0 with SMTP id iu9csp1244742pxb; Fri, 21 Jan 2022 13:13:31 -0800 (PST) X-Google-Smtp-Source: ABdhPJzWQ41MamKD6nDxOHCV4HVw4duQowtyu7tizucmM5JOy7v7BGUeGABSc2iMMwGCGfgrsKSf X-Received: by 2002:a17:902:c950:b0:14a:59cd:b0bf with SMTP id i16-20020a170902c95000b0014a59cdb0bfmr5301073pla.103.1642799611131; Fri, 21 Jan 2022 13:13:31 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1642799611; cv=none; d=google.com; s=arc-20160816; b=TsBtlVe4uSRlBchJN+VxzoteFQ805drtWMStn30SJOpQc4gZe4LGYQoIe+VwW0+Czz kpgVluFAGhHcb9+wSiL/L4ZiLkEjIpHjxwW8qAfGhnsEK08Sg6BG5wPlFaptnb5z4UMN QKPRvRk+tISU/56glWjrY2tY71gGUbp+P7aSQG7cd2gN3IlB/grJSi8Qitd11ESlKkgz Hqyj6Dmu22J2l4TgZGXTT1x4SnRQGKjhk74q+sYBCN0Z4vtNQhkdj2+dw7XLwujR/FyS ku85D/2x7/wSvs5BeWTZFmZGVBoyiHXEl88XFWlaSW2CAYIg3BsfFEhSu/N0X+OLtjah 0E0g== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=I2Ok6M+vXFYtd4Xg7ija+0a0tOq+N9sn5OM4kss3fnk=; b=Ai4iWPtLEKKU8nr+lZ9z0p2DlfloYiJ+/+8b8PKWB/GJh7PtM9Zbnm+ZgkK7w/0o0Z 1uqw6dIWplyie9xt8qxpIY38ctfhieCUdVyUioVTmrDWD45cEt2MtuOFRvchs6e8zXXZ 4syiQ7MQz+UKVxfMj4AewA/ldl3101qjsS8Du2Jt/o5+WPN3gRGo0dh7om4FhTiL/Hsw bJ27PlLoZNvvViqyLyg0ucmGLU+sBP8AXQfYt7gsYtVxrqDpOTi/UkSOzjlW1rhR6iTU TOgobLQzRDgeo5hvlPWS8XUdr33TxWp4a9JUr13aIU+RvbX1NgFUe/sKmuwI6pVVTZbR ZnCQ== ARC-Authentication-Results: i=1; mx.google.com; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id y65si3279330pgd.598.2022.01.21.13.13.19; Fri, 21 Jan 2022 13:13:31 -0800 (PST) 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; 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=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=antgroup.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359414AbiATJdi (ORCPT + 99 others); Thu, 20 Jan 2022 04:33:38 -0500 Received: from out0-136.mail.aliyun.com ([140.205.0.136]:50954 "EHLO out0-136.mail.aliyun.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1358140AbiATJdf (ORCPT ); Thu, 20 Jan 2022 04:33:35 -0500 X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R181e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018047203;MF=houwenlong.hwl@antgroup.com;NM=1;PH=DS;RN=14;SR=0;TI=SMTPD_---.MfqJchL_1642671212; Received: from localhost(mailfrom:houwenlong.hwl@antgroup.com fp:SMTPD_---.MfqJchL_1642671212) by smtp.aliyun-inc.com(127.0.0.1); Thu, 20 Jan 2022 17:33:32 +0800 From: "Hou Wenlong" To: kvm@vger.kernel.org Cc: Paolo Bonzini , Sean Christopherson , Vitaly Kuznetsov , Wanpeng Li , Jim Mattson , Joerg Roedel , Thomas Gleixner , Ingo Molnar , Borislav Petkov , Dave Hansen , x86@kernel.org, "H. Peter Anvin" , linux-kernel@vger.kernel.org Subject: [PATCH 2/2] KVM: x86: Fix wrong privilege check for code segment in __load_segment_descriptor() Date: Thu, 20 Jan 2022 17:33:30 +0800 Message-Id: X-Mailer: git-send-email 2.31.1 In-Reply-To: References: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org Code segment descriptor can be loaded by jmp/call/ret, iret and int. The privilege checks are different between those instructions above realmode. Although, the emulator has use x86_transfer_type enumerate to differentiate them, but it is not really used in __load_segment_descriptor(). Note, far jump/call to call gate, task gate or task state segment are not implemented in emulator. As for far jump/call to code segment, if DPL > CPL for conforming code or (RPL > CPL or DPL != CPL) for non-conforming code, it should trigger #GP. The current checks are ok. As for far return, if RPL < CPL or DPL > RPL for conforming code or DPL != RPL for non-conforming code, it should trigger #GP. Outer level return is not implemented above virtual-8086 mode in emulator. So it implies that RPL <= CPL, but the current checks wouldn't trigger #GP if RPL < CPL. As for code segment loading in task switch, if DPL > RPL for conforming code or DPL != RPL for non-conforming code, it should trigger #TS. Since segment selector is loaded before segment descriptor when load state from tss, it implies that RPL = CPL, so the current checks are ok. The only problem in current implementation is mssing RPL < CPL check for far return. However, change code to follow the manual is better. Signed-off-by: Hou Wenlong --- arch/x86/kvm/emulate.c | 30 ++++++++++++++++++++++-------- 1 file changed, 22 insertions(+), 8 deletions(-) diff --git a/arch/x86/kvm/emulate.c b/arch/x86/kvm/emulate.c index 864db6fbe8db..b7ce2a85e58e 100644 --- a/arch/x86/kvm/emulate.c +++ b/arch/x86/kvm/emulate.c @@ -1631,14 +1631,28 @@ static int __load_segment_descriptor(struct x86_emulate_ctxt *ctxt, if (!(seg_desc.type & 8)) goto exception; - if (seg_desc.type & 4) { - /* conforming */ - if (dpl > cpl) - goto exception; - } else { - /* nonconforming */ - if (rpl > cpl || dpl != cpl) - goto exception; + if (transfer == X86_TRANSFER_RET && rpl < cpl) + goto exception; + if (transfer == X86_TRANSFER_RET || X86_TRANSFER_TASK_SWITCH) { + if (seg_desc.type & 4) { + /* conforming */ + if (dpl > rpl) + goto exception; + } else { + /* nonconforming */ + if (dpl != rpl) + goto exception; + } + } else { /* X86_TRANSFER_CALL_JMP */ + if (seg_desc.type & 4) { + /* conforming */ + if (dpl > cpl) + goto exception; + } else { + /* nonconforming */ + if (rpl > cpl || dpl != cpl) + goto exception; + } } /* in long-mode d/b must be clear if l is set */ if (seg_desc.d && seg_desc.l) { -- 2.31.1