Received: by 2002:a05:6358:11c7:b0:104:8066:f915 with SMTP id i7csp2542038rwl; Mon, 27 Mar 2023 01:27:49 -0700 (PDT) X-Google-Smtp-Source: AKy350b46DSHknr4ITkj+2nM46UYRTBAP5PQXDpct+bQHDr8Hv3a7zbXBXY5/HIlHVBfAsUToHx2 X-Received: by 2002:a05:6402:4c5:b0:502:4182:648a with SMTP id n5-20020a05640204c500b005024182648amr3335290edw.9.1679905669635; Mon, 27 Mar 2023 01:27:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1679905669; cv=none; d=google.com; s=arc-20160816; b=V33diCjFplL1GaiQh0mYMrX0S8/iHXFCn0g06goGQHuo7ehPvuXqF0XbxWkaZqcI9n ODi5x2Zz5fEVpd7ORcbujNLn86Ksaf4H6GRHYrx3ibHB4b3f0ZMhfRrVWEEE3At0/6QX ut8GI//9H9nnj8hF3rOWac8JDSGN5/gRGoFcuWs9kFxjDEvPSaLEpgYA170zciXcrls9 ZuQgTP8QMxsvufntTTVUVPEQYrN52AdSPZEwB6DOpRypVgbEGMFZfDT2BJmPKOerGlKF nSOuH0Biy/sdXg5GGmTtGel1KgPc0x8GG3jQ6e5LlnTIenNlIgamy5Drb3feOa1yAZSb d7tQ== 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 :dkim-signature; bh=6ryoaO52SQacc1V2FLptfa9uuyFKNPljQyyaT3VTYMw=; b=MR4oF7JlOzEZdFuLlekJV8vo65qXRXESgzwrLNebeIQEKuALYnSuXTSlTUQaveKWPO 6npnPvhPDZ+Un2BjGJLDyENfhHFARwwqkoeK6d5VLHnuhuc0Gego5F2eAlfSgM0ZrGx/ Lr3y+PlsX+9L0A6U26cA1RpsSJi4pOA9rnoCoE49I4Mo4bRUP0iBcU3yKA+OrGb/q81p Z+pLiIbIc533C8cYQd6TaKpuhO18m2COAHiAJ3bW65E1KQlK9WU5JrYIfyhVRorSRsIy iX4dACYez/Fh+TqoYTjjB88myei7GRTRnq2ja+VCS+5M1roTU9WR2ermXqgutQKx1SOa Tz4g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MhuTPWr+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id be1-20020a0564021a2100b00502246e6728si8064931edb.398.2023.03.27.01.27.24; Mon, 27 Mar 2023 01:27:49 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=MhuTPWr+; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=intel.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S232440AbjC0I0c (ORCPT + 99 others); Mon, 27 Mar 2023 04:26:32 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33064 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233170AbjC0IY7 (ORCPT ); Mon, 27 Mar 2023 04:24:59 -0400 Received: from mga04.intel.com (mga04.intel.com [192.55.52.120]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id A5A0E5B8B; Mon, 27 Mar 2023 01:24:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1679905482; x=1711441482; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5IBl0XY5xGj0Ul5jZJTZVlHtGnpVju3gJ8HZ07Stwk0=; b=MhuTPWr+7THHdZI9zsPhgiWedqMcjcaQhkNoXGXGj0csVvzYKn5FctUZ xucUkg9DV50r8wdzcuaX9RtMWXnJQW88pr2TvfZrvakhGnbFEc+QxOcQQ isfKyrFd+u+P4tHFa+V6vwLHkB6gNjYSXEGER+J89Y0biKCaQDHlKiZ1h F4k+z4TIxsV9UuR/ILYbT5Tjm6C+4Wf+X1c/Fh32ZfS4GQ8kXUKZE19T4 SKpGzYY8gVCtbnjKRZPhNBXUcSMXxwvbQ+1xgWaYGnU91pagRG222x6tF yfyZ44gBCwbGUDDJ9P5j+yIw4tcM1CVKs3Bqb5O+iC+b+sdU1PQ36M8ik g==; X-IronPort-AV: E=McAfee;i="6600,9927,10661"; a="338930391" X-IronPort-AV: E=Sophos;i="5.98,294,1673942400"; d="scan'208";a="338930391" Received: from orsmga008.jf.intel.com ([10.7.209.65]) by fmsmga104.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 27 Mar 2023 01:24:40 -0700 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6600,9927,10661"; a="713787142" X-IronPort-AV: E=Sophos;i="5.98,294,1673942400"; d="scan'208";a="713787142" Received: from unknown (HELO fred..) ([172.25.112.68]) by orsmga008.jf.intel.com with ESMTP; 27 Mar 2023 01:24:40 -0700 From: Xin Li To: linux-kernel@vger.kernel.org, x86@kernel.org, kvm@vger.kernel.org Cc: tglx@linutronix.de, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, hpa@zytor.com, peterz@infradead.org, andrew.cooper3@citrix.com, seanjc@google.com, pbonzini@redhat.com, ravi.v.shankar@intel.com, jiangshanlai@gmail.com, shan.kang@intel.com Subject: [PATCH v6 28/33] x86/ia32: do not modify the DPL bits for a null selector Date: Mon, 27 Mar 2023 00:58:33 -0700 Message-Id: <20230327075838.5403-29-xin3.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230327075838.5403-1-xin3.li@intel.com> References: <20230327075838.5403-1-xin3.li@intel.com> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.5 required=5.0 tests=DKIMWL_WL_HIGH,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED,SPF_HELO_NONE, SPF_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org When a null selector is to be loaded into a segment register, reload_segments() sets its DPL bits to 3. Later when the IRET instruction loads it, it zeros the segment register. The two operations offset each other to actually effect a nop. Unlike IRET, ERETU does not make any of DS, ES, FS, or GS null if it is found to have DPL < 3. It is expected that a FRED-enabled operating system will return to ring 3 (in compatibility mode) only when those segments all have DPL = 3. Thus when FRED is enabled, we end up with having 3 in a segment register even when it is initially set to 0. Fix it by not modifying the DPL bits for a null selector. Tested-by: Shan Kang Signed-off-by: Xin Li --- arch/x86/kernel/signal_32.c | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/arch/x86/kernel/signal_32.c b/arch/x86/kernel/signal_32.c index 9027fc088f97..7796cf84fca2 100644 --- a/arch/x86/kernel/signal_32.c +++ b/arch/x86/kernel/signal_32.c @@ -36,22 +36,27 @@ #ifdef CONFIG_IA32_EMULATION #include +static inline u16 usrseg(u16 sel) +{ + return sel <= 3 ? sel : sel | 3; +} + static inline void reload_segments(struct sigcontext_32 *sc) { unsigned int cur; savesegment(gs, cur); - if ((sc->gs | 0x03) != cur) - load_gs_index(sc->gs | 0x03); + if (usrseg(sc->gs) != cur) + load_gs_index(usrseg(sc->gs)); savesegment(fs, cur); - if ((sc->fs | 0x03) != cur) - loadsegment(fs, sc->fs | 0x03); + if (usrseg(sc->fs) != cur) + loadsegment(fs, usrseg(sc->fs)); savesegment(ds, cur); - if ((sc->ds | 0x03) != cur) - loadsegment(ds, sc->ds | 0x03); + if (usrseg(sc->ds) != cur) + loadsegment(ds, usrseg(sc->ds)); savesegment(es, cur); - if ((sc->es | 0x03) != cur) - loadsegment(es, sc->es | 0x03); + if (usrseg(sc->es) != cur) + loadsegment(es, usrseg(sc->es)); } #define sigset32_t compat_sigset_t -- 2.34.1