Received: by 2002:a25:868d:0:0:0:0:0 with SMTP id z13csp549500ybk; Sat, 9 May 2020 10:39:16 -0700 (PDT) X-Google-Smtp-Source: APiQypKbCwgG8lDLUwDdTbR2OrT3vasymEHfT5/VWczeKOg4o4YRxoMpESnRl+Y9+zwgBNxV5JOW X-Received: by 2002:a17:906:3605:: with SMTP id q5mr7036407ejb.363.1589045956322; Sat, 09 May 2020 10:39:16 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1589045956; cv=none; d=google.com; s=arc-20160816; b=riZcMmpjgIu1E7XNyJSNDkBC+gfpqFU7ctYpSlPS2B9v5rlS3lvj9cfeDxIhbGS6kr 3zS+j3sYT97z5rPFslhROrTz+n/yV1AiYX0wXQlPbNtq7LfmZ2LXNbxh0tUtY+n46PM4 3+q1utgLSw0nQbPEZrU04F0Z3QSUgOmegvi8M/pj+DCEZsD7NECq6QlEOsd0CR4Z8cGp CopTmQPqfyjchYhoZfYwJNRL5zDHKuJvet+VYAUCKigr/PNZbMyUdPuQ6ihW2Y7tvzH3 SbFg3GjgIyMZ9vVVwTsoUt/H1pXau1y5ZCMMr9wX8F4M6FUvyVvr+FB/Z7/c60xM2Jrk xNXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:content-transfer-encoding:mime-version :references:in-reply-to:message-id:date:subject:cc:to:from :dkim-signature; bh=Zv+QTIqLsbMrtGqxs+Q7G8vlwSjwI47ByKLTbdbINDg=; b=VpdV1yjLoY9skW+deu06M8kcdTC5TdijoyryMCebYNHtmVfOpkDocaliSN4sI81Y44 TWXR5CYZFSCFlsUNiwe74qzwDGG96DHLnQQC/nXgtJNnfk71X43c7wDQ3INdL7cH3fGo CqRevnWU8fEK0O9I477KzcMANYkbB7fl6nw85E8edJsGepnX2oHwpgUKP6cYBhvc4Lqi DEt9ds7ZKEpnRCJ5+JcRjDQAAVwZpcpoE7T6IQt2zMENyS6mFm4keCmjs14IFQfiU0Tj Sp4b7E2OjuBAz/rskSv+oCrVYYXflxHOieO+fDuObdBMbiC2KDNhbXiIWdii4aE8E0T7 1rog== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=n94VuA8A; 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=kernel.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id i20si3157148edx.314.2020.05.09.10.38.53; Sat, 09 May 2020 10:39:16 -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=@kernel.org header.s=default header.b=n94VuA8A; 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=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1728642AbgEIRhW (ORCPT + 99 others); Sat, 9 May 2020 13:37:22 -0400 Received: from mail.kernel.org ([198.145.29.99]:54656 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728595AbgEIRhS (ORCPT ); Sat, 9 May 2020 13:37:18 -0400 Received: from sasha-vm.mshome.net (c-73-47-72-35.hsd1.nh.comcast.net [73.47.72.35]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 046B824975; Sat, 9 May 2020 17:37:16 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1589045838; bh=q0R70uyPyClzQN46UcwhEYpcP3Otr2K5sbFabZXwPkM=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=n94VuA8AX3L3p5UCKZiqr8kkkk5JyoqQQ5JjA/RqhkEFZse3fTcSDZlwn8XivQVb+ nR15R5NB6x8x9yYa/2IrXN+ybdi2FUjwHF+ikw1hzbE7unwL8EjeeoVxdPEbGajzk0 nyGStYKuTV4XlMtj96AztzuOCTb6dCKJKsAn9NNw= From: Sasha Levin To: linux-kernel@vger.kernel.org, tglx@linutronix.de, bp@alien8.de, luto@kernel.org Cc: hpa@zytor.com, dave.hansen@intel.com, tony.luck@intel.com, ak@linux.intel.com, ravi.v.shankar@intel.com, chang.seok.bae@intel.com, Sasha Levin Subject: [PATCH v11 13/18] x86/fsgsbase/64: Use FSGSBASE instructions on thread copy and ptrace Date: Sat, 9 May 2020 13:36:50 -0400 Message-Id: <20200509173655.13977-14-sashal@kernel.org> X-Mailer: git-send-email 2.20.1 In-Reply-To: <20200509173655.13977-1-sashal@kernel.org> References: <20200509173655.13977-1-sashal@kernel.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: "Chang S. Bae" When FSGSBASE is enabled, copying threads and reading FS/GS base using ptrace must read the actual values. When copying a thread, use fsgs_save() and copy the saved values. For ptrace, the bases must be read from memory regardless of the selector if FSGSBASE is enabled. Suggested-by: Andy Lutomirski Signed-off-by: Chang S. Bae Signed-off-by: Sasha Levin Reviewed-by: Tony Luck Cc: Thomas Gleixner Cc: Borislav Petkov Cc: Andy Lutomirski Cc: H. Peter Anvin Cc: Dave Hansen Cc: Tony Luck Cc: Andi Kleen --- arch/x86/kernel/process.c | 10 ++++++---- arch/x86/kernel/process_64.c | 6 ++++-- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/arch/x86/kernel/process.c b/arch/x86/kernel/process.c index 9da70b279dad8..3ebb56cc2cfee 100644 --- a/arch/x86/kernel/process.c +++ b/arch/x86/kernel/process.c @@ -127,6 +127,7 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp, struct inactive_task_frame *frame; struct fork_frame *fork_frame; struct pt_regs *childregs; + struct task_struct *me = current; int ret = 0; childregs = task_pt_regs(p); @@ -140,10 +141,11 @@ int copy_thread_tls(unsigned long clone_flags, unsigned long sp, memset(p->thread.ptrace_bps, 0, sizeof(p->thread.ptrace_bps)); #ifdef CONFIG_X86_64 - savesegment(gs, p->thread.gsindex); - p->thread.gsbase = p->thread.gsindex ? 0 : current->thread.gsbase; - savesegment(fs, p->thread.fsindex); - p->thread.fsbase = p->thread.fsindex ? 0 : current->thread.fsbase; + save_fsgs(me); + p->thread.fsindex = me->thread.fsindex; + p->thread.fsbase = me->thread.fsbase; + p->thread.gsindex = me->thread.gsindex; + p->thread.gsbase = me->thread.gsbase; savesegment(es, p->thread.es); savesegment(ds, p->thread.ds); #else diff --git a/arch/x86/kernel/process_64.c b/arch/x86/kernel/process_64.c index 4be88124d81ea..57cdbbb0381ac 100644 --- a/arch/x86/kernel/process_64.c +++ b/arch/x86/kernel/process_64.c @@ -346,7 +346,8 @@ unsigned long x86_fsbase_read_task(struct task_struct *task) if (task == current) fsbase = x86_fsbase_read_cpu(); - else if (task->thread.fsindex == 0) + else if (static_cpu_has(X86_FEATURE_FSGSBASE) || + (task->thread.fsindex == 0)) fsbase = task->thread.fsbase; else fsbase = x86_fsgsbase_read_task(task, task->thread.fsindex); @@ -360,7 +361,8 @@ unsigned long x86_gsbase_read_task(struct task_struct *task) if (task == current) gsbase = x86_gsbase_read_cpu_inactive(); - else if (task->thread.gsindex == 0) + else if (static_cpu_has(X86_FEATURE_FSGSBASE) || + (task->thread.gsindex == 0)) gsbase = task->thread.gsbase; else gsbase = x86_fsgsbase_read_task(task, task->thread.gsindex); -- 2.20.1