From mboxrd@z Thu Jan 1 00:00:00 1970 Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.remote-ip=198.175.65.19 ARC-Seal: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; cv=none; t=1796073558; b=F9qoUj4WblPEVefUIWrTh9vzKfiYCncF5W5Bp8GceldgebUpAQowQSmwd/BCAWkWP7Ac hpSm8zOkWT6hKkzCniaDUfMKjtfbgL9IuKUMe7PelUz9V3mKSExPlTXMO1NDV6j7AAT+2 7QWm5+tAdKnTbgMQq5WEg3o8shm3acccXGVL/hh8TUnQuES0h32d8eVoGo/Ke2OU0jOxz sKjwJWHyhBsvDAnV/INAB382xNdzDvom3HBGPQXroMsm5JU07ZxXlY6UqqS+3vJbyPcrP CpZHl1nGzY5baNC339aUtopOQy6JE9Jve+HYoKKrZLyvX6L+i2PeXTqScxo89601zHw== ARC-Message-Signature: i=1; d=subspace.kernel.org; s=arc20261015; a=rsa-sha256; c=relaxed/relaxed; t=1796073558; h=DMARC-Filter:DKIM-Signature:X-CSE-ConnectionGUID:X-CSE-MsgGUID: X-IronPort-AV:X-IronPort-AV:Received:X-CSE-ConnectionGUID: X-CSE-MsgGUID:X-ExtLoop1:X-IronPort-AV:Received:From:To:Cc:Subject: Date:Message-ID:X-Mailer:In-Reply-To:References:MIME-Version: Content-Type:Content-Transfer-Encoding; bh=43DzXf7EJ5XV22GQLJ7TyuBPzJld1gCEfNzX5v5RszY=; b=TyQHErz2P2zDgCwIadispVaHyddCKNsT755MOrETa36+OTjjq6xrlSl/wVXz3HOSED+k M2Ca22FKRW6VUCEMFBwg5o1SU43zl9xNjQd+gbL+oKty6jqwY2HvJQEgmUgV+2CXyCQ2S X5UDaRsWcbzlFEZejk+oDpuYI6RWjYjm7gmdlczZxNEGQTgj8HqYjO1s2GbNd9B/JHxfP b4HeEeDaaaJRyevD+rHVlVSuIboqrKzrlTq8SIU6URLYidoploBc6eBDBDeaMH/tPgtZp ZVKFhO/7kQY6zw3CDb7fQAlgUZkV66ihSZqG0YVPDHK3xfUTcv+JwcjQkmUHBesl21Q== ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass header.from=intel.com; spf=pass smtp.mailfrom=intel.com; dkim=pass header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Vmv4LwzP; arc=none smtp.remote-ip=198.175.65.19 Received: from mgamail.intel.com (mgamail.intel.com [198.175.65.19]) (using TLSv1.2 with cipher ECDHE-ECDSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id A1EFB606A32C; Mon, 30 Nov 2026 21:19:17 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.4.2 smtp.subspace.kernel.org A1EFB606A32C Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=intel.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=intel.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=intel.com header.i=@intel.com header.a=rsa-sha256 header.s=Intel header.b=Vmv4LwzP DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=intel.com; i=@intel.com; q=dns/txt; s=Intel; t=1796073558; x=1827609558; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version:content-transfer-encoding; bh=43DzXf7EJ5XV22GQLJ7TyuBPzJld1gCEfNzX5v5RszY=; b=Vmv4LwzPi/fU71kHM8sZLbwqYcgGDZaD8Fn1NoPzDxG5nhTsDQvVNWhN UzAl3cF7eNM8ZZ1+rAOvadwWCURtww2GC+Vr7LXRhJjy2xjcg58eSTKcx tdc+jfnx0XqEFtP5CHDIL5+7W2UJ5Ss+VlCPS7T486Ay8LP+5qhEpoyKk 3LlH1HSMGx+g7GgNyMM1HOZpBwh8gCt1WpNW48GXOxX2bR7V19ZDo8+CX 2H+6225MrbmfaRRsCYkcb8Rp10N15vkN3D7XZhslZ0p16/EXnZ3uiPuVS aQqXHeokdjSNbQX803zakcn2cowt0gSozhzAbz3dk6KYUVJzm01UYoN7X g==; X-CSE-ConnectionGUID: K2IwvBkASfS8so4l/hqxRw== X-CSE-MsgGUID: D/eF6dhFSsqz/MBefdzpSg== X-IronPort-AV: E=McAfee;i="6800,10657,11982"; a="4989761" X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="4989761" Received: from fmviesa005.fm.intel.com ([10.60.135.145]) by orvoesa111.jf.intel.com with ESMTP/TLS/ECDHE-RSA-AES256-GCM-SHA384; 30 Nov 2026 13:19:16 -0800 X-CSE-ConnectionGUID: 3odXhIc6QU+GUgHSJs8yDQ== X-CSE-MsgGUID: oPTjZI3iQEiP5wtOYjE72Q== X-ExtLoop1: 1 X-IronPort-AV: E=Sophos;i="6.27,243,1787036400"; d="scan'208";a="13166759" Received: from chang-linux-3.sc.intel.com (HELO chang-linux-3) ([172.25.66.174]) by fmviesa005.fm.intel.com with ESMTP; 30 Nov 2026 13:19:15 -0800 From: "Chang S. Bae" To: linux-kernel@vger.kernel.org Cc: x86@kernel.org, tglx@kernel.org, mingo@redhat.com, bp@alien8.de, dave.hansen@linux.intel.com, weiguixiong@bytedance.com, omermetekaya0@gmail.com, chang.seok.bae@intel.com, syzbot+58cbd7974a103b8b8916@syzkaller.appspotmail.com, syzbot+b07ec23d35be305855be@syzkaller.appspotmail.com, stable@vger.kernel.org Subject: [PATCH 3/6] x86/fpu: Reset the fpstate on exec() under fpregs_lock() Date: Mon, 30 Nov 2026 20:51:39 +0000 Message-ID: <20261130205143.1258369-4-chang.seok.bae@intel.com> X-Mailer: git-send-email 2.53.0 In-Reply-To: <20261130205143.1258369-1-chang.seok.bae@intel.com> References: <20261130205143.1258369-1-chang.seok.bae@intel.com> X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Post: List-Help: List-Subscribe: List-Unsubscribe: List-Owner: Precedence: list MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit fpregs_lock() should be held when either FPU registers or storage is updated. The fpstate metadata is also relevant so fpstate_reset() has to be under the lock. Currently fpu_flush_thread() has fpstate_reset() without the lock. A couple of sysbot reports [1,2] both indicated a race condition to a task that previously loaded dynamic states. To address this issue, first prepare the helper by renaming to fpu_reset_fpstate() and providing the storage reset option to accommodate the fpu_clone() path. Then, move fpstate_reset() under the lock. Fixes: e61d6310a0f8 ("x86/fpu: Reset permission and fpstate on exec()") Closes: https://syzkaller.appspot.com/bug?extid=58cbd7974a103b8b8916 [1] Closes: https://syzkaller.appspot.com/bug?extid=b07ec23d35be305855be [2] Reported-by: syzbot+58cbd7974a103b8b8916@syzkaller.appspotmail.com Reported-by: syzbot+b07ec23d35be305855be@syzkaller.appspotmail.com Suggested-by: Dave Hansen Signed-off-by: Chang S. Bae Cc: Ă–mer Mete Kaya Cc: stable@vger.kernel.org --- arch/x86/kernel/fpu/core.c | 36 +++++++++++++++++++++++++++--------- 1 file changed, 27 insertions(+), 9 deletions(-) diff --git a/arch/x86/kernel/fpu/core.c b/arch/x86/kernel/fpu/core.c index 80847a08ff3bb..de746eedfe397 100644 --- a/arch/x86/kernel/fpu/core.c +++ b/arch/x86/kernel/fpu/core.c @@ -613,6 +613,9 @@ static void __fpstate_reset(struct fpstate *fpstate) fpstate->user_xfeatures = fpu_user_cfg.default_features; } +/* + * fpregs_lock() must be held when fpstate belongs to the current task + */ void fpstate_reset(struct fpu *fpu) { /* Set the fpstate pointer to the default fpstate */ @@ -674,15 +677,21 @@ static int update_fpu_shstk(struct task_struct *dst, unsigned long ssp) } /* - * Reset FPU register storage to the init values from: + * Reset fpstate metadata and register storage to the init values from: * - fpu_clone() for new task's FPU state * - fpu_flush_thread() for current */ -static void fpu_reset_fpstate_regs(struct task_struct *tsk, struct fpu *fpu) +static void fpu_reset_fpstate(struct task_struct *tsk, struct fpu *fpu, bool reset_regs) { + /* Current must get its register storage reset. */ + WARN_ON_ONCE(tsk == current && !reset_regs); + if (tsk == current) fpregs_lock(); + /* Reset the fpstate metadata: */ + fpstate_reset(fpu); + /* * This does not change the actual hardware registers. It just * resets the memory image. Do not use fpstate_init() here. Just copy @@ -691,8 +700,17 @@ static void fpu_reset_fpstate_regs(struct task_struct *tsk, struct fpu *fpu) * PKRU handling does not rely on the xstate when restoring for * user space as PKRU is eagerly written in switch_to() and * flush_thread(). + * + * When skipping the reset, from fpu_clone(), it means the new task + * inherits the current register context instead. + */ + if (reset_regs) + memcpy(&fpu->fpstate->regs, &init_fpstate.regs, init_fpstate_copy_size()); + + /* + * Even if !reset_regs, new task's FPU state cannot be valid in the + * hardware */ - memcpy(&fpu->fpstate->regs, &init_fpstate.regs, init_fpstate_copy_size()); __fpu_invalidate_fpregs_state(fpu); /* @@ -720,10 +738,11 @@ int fpu_clone(struct task_struct *dst, u64 clone_flags, bool minimal, BUILD_BUG_ON(sizeof(*dst) % SMP_CACHE_BYTES != 0); - fpstate_reset(dst_fpu); - - /* Fill the new task FPU storage with init-state */ - fpu_reset_fpstate_regs(dst, dst_fpu); + /* + * Reset the fpstate metadata and fill the new task FPU storage + * with init-state when needed. + */ + fpu_reset_fpstate(dst, dst_fpu, minimal); /* * No FPU state inheritance for kernel threads and IO @@ -874,8 +893,7 @@ void fpu__clear_user_states(struct fpu *fpu) void fpu_flush_thread(void) { - fpstate_reset(x86_task_fpu(current)); - fpu_reset_fpstate_regs(current, x86_task_fpu(current)); + fpu_reset_fpstate(current, x86_task_fpu(current), true); } /* * Load FPU context before returning to userspace. -- 2.53.0