From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f198.google.com (mail-oi1-f198.google.com [209.85.167.198]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 61DC24DA540 for ; Thu, 24 Sep 2026 21:01:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.198 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790283694; cv=none; b=eRePgs4wOFVJCqrcCI3ZpzeyyepXuSofAyDhCddmNR2AztszzOrx6Cf/87fgIE2EKL3aPJ6+8JV/byWWedrSkgJ/KdMCkctf/WUaHQYdf3batyBo9aucm1MtaugFKO5jh1fuArJRrVJB9wgdGUDq3nZOZnbf0I1pS94SeYumK0I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790283694; c=relaxed/simple; bh=eWdNXYkzpEGoLypYsfkXKKnEba4CgKEPV8FCoFIGb5o=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=fhZjHGjB9ndsHisHtZqJStUy4SCJ6GoTa1FpXhwp9PDXxdQI9e2xzVOzARrb7HvuO4g4VWHaLYhZZR7sJH4PL3raWtSZWpiGcwez3G0OTzi6CXV5vl037WjUuKU6lCNw9Nuw+wd5AOhj/0WffwsPt56VrlHzh2mmruNg87htJqQ= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com; spf=pass smtp.mailfrom=flex--avagin.bounces.google.com; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b=XDV0r7cq; arc=none smtp.client-ip=209.85.167.198 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=reject dis=none) header.from=google.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=flex--avagin.bounces.google.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=google.com header.i=@google.com header.b="XDV0r7cq" Received: by mail-oi1-f198.google.com with SMTP id 5614622812f47-4c307941971so573531b6e.1 for ; Thu, 24 Sep 2026 14:01:31 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790283690; x=1790888490; darn=lists.linux.dev; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:from:to:cc:subject:date:message-id:reply-to :content-type; bh=dwQiHsyrLWUzsxzNSVYlkwXiHkF2i4EcK7GZMOAbO5Y=; b=XDV0r7cqGD0JfpZUw7/ZwUQ3+aFDVtf80dUNk7lRxw77yPFz6qVGJ7YxZKd0R2oh/Y IAeNIqVwcv4kdP32oR4wAVaNc90j1AlucC2nF9y6/vVUyZot+EpNu6BP3wtyTXO+vNM+ 1IyYrawnchg3Qqdhkajn2wMQvxNvL1WCe3kza4QBieA4fb2jj7DvfGGxY0D0k/YFQ4Q3 s4U9GgEOqfynh8/fND/Q4Ge7NKdBSlG4no7CA6qvkEyYFNbuwhxHUwJRX2kjwhd/upT2 txHhoHorlX70Qksr8NCqjmxEg2Pke38xVZ0uo9LMhXt+XQK6isuRJe+JKN9cqAz8qxOG Lfbg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790283690; x=1790888490; h=content-type:cc:to:from:subject:message-id:references:mime-version :in-reply-to:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=dwQiHsyrLWUzsxzNSVYlkwXiHkF2i4EcK7GZMOAbO5Y=; b=J3aqO0ay3DY8n9ZyewkyKLdrsO1Lu8ByvcmIBGE6bahwEPH7I4qpQnRfAIMm9deXPJ 4f9GgeZ/gpWnwuiO4jWpnDixpiC3sRzdUGnC9DORnEZ5jDKaLS5/hR8NEgqnBBSuxmgQ D7g6a4wfHXRUn2nYF7Z2NsAkYMLqfRgCrhngrnfjeEFURB7do/5VPb5sFr13DoDvKrHP Rf8OBgMJTbh5J+1eXvqQ0lK4Ow6ccMXHyY1WSg37pUbmAFO2mmceqfddY1ZqMLs8G571 Fx1wXtkF96qDJG6/hcSD8lQ6AsOZl7/TzJToEwziekT9HBrY4Vt7YBSJ9H8WrxZD6mkE SYhw== X-Forwarded-Encrypted: i=1; AKwUvBwzPzIC3Q84HgnKTIg/Mt++eARPAaai++b17MuvPKlfjoLpOVB/elDmHN7MVeto86CN4wHP@lists.linux.dev X-Gm-Message-State: AFuF++mAaEQ5gn/pgM1APt7ZY2MOXqwOhocBBnKaW8t9dt9u0mXtIurD y3AfjeAy/CmtfosWp23lJ5hhr2M7oi/6lEN7xXajGGeR4UKvyI/O8gl34obwErgR9rTGOdzTYRA lp+Evvw== X-Received: from iobdo19.prod.google.com ([2002:a05:6602:4613:b0:9c3:8c1f:2a18]) (user=avagin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:10c7:b0:4d6:48c4:a819 with SMTP id 5614622812f47-4d72cb41616mr3405681b6e.29.1790283689932; Thu, 24 Sep 2026 14:01:29 -0700 (PDT) Date: Thu, 24 Sep 2026 21:01:19 +0000 In-Reply-To: <20260924210121.87032-1-avagin@google.com> Precedence: bulk X-Mailing-List: criu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260924210121.87032-1-avagin@google.com> X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <20260924210121.87032-6-avagin@google.com> Subject: [PATCH 5/7] x86/fpu: Fix potential underflow in xstate_calculate_size() From: Andrei Vagin To: Thomas Gleixner , Ingo Molnar , Borislav Petkov , "Chang S. Bae" Cc: linux-kernel@vger.kernel.org, criu@lists.linux.dev, Dave Hansen , x86@kernel.org, Andrei Vagin , Alexander Mikhalitsyn , "H. Peter Anvin" Content-Type: text/plain; charset="UTF-8" xstate_calculate_size() calculates the size required for a given set of xfeatures. It determines the topmost feature by finding the most significant bit in xfeatures using fls64(xfeatures) - 1. If xfeatures is 0, fls64(0) returns 0, and topmost becomes -1. Previously, topmost was unsigned int, so -1 underflowed to UINT_MAX. This caused the subsequent check `topmost <= XFEATURE_SSE` to fail, and the code proceeded to access xstate arrays using topmost (UINT_MAX) as an index, leading to an out-of-bounds access. Fix this by checking if xfeatures only contains legacy features (FP/SSE) or is empty (!(xfeatures & ~XFEATURE_MASK_FPSSE)) before calculating topmost. Fixes: d6d6d50f1e80 ("x86/fpu/xstate: Consolidate size calculations") Reviewed-by: Alexander Mikhalitsyn Reviewed-by: Chang S. Bae Signed-off-by: Andrei Vagin --- arch/x86/kernel/fpu/xstate.c | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/arch/x86/kernel/fpu/xstate.c b/arch/x86/kernel/fpu/xstate.c index 3e7f5fb5bfaf..362df13a88cf 100644 --- a/arch/x86/kernel/fpu/xstate.c +++ b/arch/x86/kernel/fpu/xstate.c @@ -589,12 +589,13 @@ static bool __init check_xstate_against_struct(int nr) unsigned int xstate_calculate_size(u64 xfeatures, bool compacted) { - unsigned int topmost = fls64(xfeatures) - 1; - unsigned int offset, i; + unsigned int topmost, offset, i; - if (topmost <= XFEATURE_SSE) + if (!(xfeatures & ~XFEATURE_MASK_FPSSE)) return sizeof(struct xregs_state); + topmost = fls64(xfeatures) - 1; + if (compacted) { offset = xfeature_get_offset(xfeatures, topmost); } else { -- 2.56.0.rc1.315.gc6ed9934b7-goog