From patchwork Thu Mar 2 05:25:06 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Li, Xin3" X-Patchwork-Id: 63243 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp4070364wrd; Wed, 1 Mar 2023 22:12:57 -0800 (PST) X-Google-Smtp-Source: AK7set+4wiHIudYgjBEAQIh05zQvE4U1BTupWvvZ00Pb+7O7+dueymMymo1M6nWnuLVfRP4bdjZc X-Received: by 2002:a05:6a20:3d91:b0:cd:3f04:6452 with SMTP id s17-20020a056a203d9100b000cd3f046452mr11541016pzi.49.1677737576994; Wed, 01 Mar 2023 22:12:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1677737576; cv=none; d=google.com; s=arc-20160816; b=HVFOVrK+OXv5l/gVPMwr62oc/M/CoURZ7gVTe5HgRA875IEMOU26EO7x5GTY7t+IEn 19vXUXgY8inKs4ZEDVASY9mRcaxOOWXTvxqGykVjLV4Z7U4PGLMPwsECBikpBEL400Bx 33QR6NqeRS3FN9x1aYgPDRsV4RG2AQ600reh25SRMtTDarV27npdbMS3137ioq/AfTOs XaSfZDvgJhpEPUkGjFTq2W/CzhE5zqpANauxjrcHMEvjrJ9kOK0+vNnDufnBKVkmgrCP G/N765pBP65rUyQ2Na5hSNMaT8ohO9sv23cP50sul1P8xTtMQfvoBdPVh+usu8L3kr7E fMYQ== 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=Hs5MvgAGMV354K13+qXGyQwGDUtw8Q3VERme+EJeNGUFc59wk5OkixT2CO+GS7t5O8 bQ+XE19r5dcdMXEaMZIr+aUeXLzQfj6eHOBxhkn7F+0Mkarb/kBnKtxJTUqYAssQD4QP 165HMyMTlms7GQiuRb6N6ienmrN2X3gqVZ1R0oSozeTCXUwiP8icKhV2FAQ76r7JWGmu FhnPPQuOXNLBI3urIpStt0Z1ULL9xA4bUYpaQZ32k7yxC6eSw5IK4uibs7L1zhW2p291 OaaO2MGKcAdHPTI7ZwK+xibox+ysMozhp1ubJIbP7F5T/WKdvxUgbW6DIRiJ1fGcGarh Mv7Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@intel.com header.s=Intel header.b=XrxBh89F; 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: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id 84-20020a630557000000b004dfe1a4ff03si13663811pgf.106.2023.03.01.22.12.43; Wed, 01 Mar 2023 22:12:56 -0800 (PST) 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=XrxBh89F; 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 S230176AbjCBFxB (ORCPT + 99 others); Thu, 2 Mar 2023 00:53:01 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:47654 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230057AbjCBFwa (ORCPT ); Thu, 2 Mar 2023 00:52:30 -0500 Received: from mga05.intel.com (mga05.intel.com [192.55.52.43]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 4EFC555074; Wed, 1 Mar 2023 21:51:32 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1677736292; x=1709272292; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=5IBl0XY5xGj0Ul5jZJTZVlHtGnpVju3gJ8HZ07Stwk0=; b=XrxBh89F5efoaqUKFckWHq+4kQ1aFc9XuAfUEvt6r9jAq/0HEHPXwOzt tsAfVpsx1F/raEybyyBIOurjdMojuTbnubI9sFnMNkwMCSxK0aNOQuHV8 yoVsiAzGP1KGq2YBvBqf/GTBG6fT9PXxIUZ+ChhqsArl32ZtdCr3FQm2z bEDA2v4GvdVpUqhXy2Wgm4VbN+DByPhq23xTbNW3YIuAqRWXOkGw5v69x A/MS1xAvBM3jpDmevBQ0L769HZ0ZV6RjrPsymoP8zGnDlT6scS87PwZvC XGgrSjmBoGha0eergh2v+lssqtswVEDPNh5zCLsn4gJi2+g9+CakswHnj A==; X-IronPort-AV: E=McAfee;i="6500,9779,10636"; a="420887297" X-IronPort-AV: E=Sophos;i="5.98,226,1673942400"; d="scan'208";a="420887297" Received: from fmsmga006.fm.intel.com ([10.253.24.20]) by fmsmga105.fm.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 01 Mar 2023 21:50:52 -0800 X-ExtLoop1: 1 X-IronPort-AV: E=McAfee;i="6500,9779,10636"; a="920530970" X-IronPort-AV: E=Sophos;i="5.98,226,1673942400"; d="scan'208";a="920530970" Received: from unknown (HELO fred..) ([172.25.112.68]) by fmsmga006.fm.intel.com with ESMTP; 01 Mar 2023 21:50:51 -0800 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 Subject: [PATCH v4 29/34] x86/ia32: do not modify the DPL bits for a null selector Date: Wed, 1 Mar 2023 21:25:06 -0800 Message-Id: <20230302052511.1918-30-xin3.li@intel.com> X-Mailer: git-send-email 2.34.1 In-Reply-To: <20230302052511.1918-1-xin3.li@intel.com> References: <20230302052511.1918-1-xin3.li@intel.com> MIME-Version: 1.0 X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, SPF_HELO_NONE,SPF_NONE autolearn=ham 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1759235357881153624?= X-GMAIL-MSGID: =?utf-8?q?1759235357881153624?= 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