From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f197.google.com (mail-oi1-f197.google.com [209.85.167.197]) (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 5E9DE3CFF5A for ; Thu, 24 Sep 2026 04:16:13 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790223378; cv=none; b=NSM2NjW3S7Mqv1kadU0LwbqLXsouNc7A4a2riY2pauME9bJGdqR//y/nt+wSTbeToGuP4AtfLD/qzJgy3zFFNeJRFci4kJ83VhwAtwLmczp8bsdU4aOp7qm50dpg4Ao6yN9dFCu5sWFTdy5xzYzm8tv6WIqCjo3vC2mFnlnIkB8= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790223378; c=relaxed/simple; bh=GW++IQkN3swJonF+Ux3M4S61EH1JXfeg3xTVbZ1j010=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=ph+33zhTvyVj1kGMdhU1wiATtALW9+MBb0B1lE2LgPKB8u8ECmFbIIdqrY47AjR5tAIHFUuqqq8uCebMn7A88GUoic4B+exERZvrwJRobbIkDymP6dsMYzDPHFRluk9Whn2dJ+JoXkvOBBtOKHonHUDB8oWlJkUfLrCtL+JIrUc= 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=sUuZ3kdS; arc=none smtp.client-ip=209.85.167.197 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="sUuZ3kdS" Received: by mail-oi1-f197.google.com with SMTP id 5614622812f47-4a7de733fa9so2569727b6e.3 for ; Wed, 23 Sep 2026 21:16:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790223371; x=1790828171; 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=FLDrAGg7xWWuICM8+UcK0lvwffhpOIQZdOjs40Fvj3s=; b=sUuZ3kdS+fEMr8zXCTipe5ulNoYF1DPMwqwJltDFEiZCoJfKDqTH/E3nat9+t7ixXO z19lxDypH2pAtDSKkKR/G+1fgNXPKhJIKIuWViTpn/tE2WYwLECdDEnhLA2UR+1BJUO9 5Mr5blw/YPd9/nJhoLeWL/bt9lTivzZv/XZy+AcAxL8ro4AQDzj3xFG2ABhw77uA34lv 8aLzShqvkdV3QmapQ1IGn6EiQt0Wry/3D6xweF0IjW6AK1wflJIYxyfafbsjTCdiRB+o 4po5tp0lLHNBfGmxuy18k/CIYmQLWDBnMjdX0313mov0OT+h0UPhl4bjPZsKkhaxdKHE sVAQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790223371; x=1790828171; 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=FLDrAGg7xWWuICM8+UcK0lvwffhpOIQZdOjs40Fvj3s=; b=Br9PB4FEUY/uCd4+BkN1jhEt0q23gXCujOIQOJFsCTnHkr2ddaMhMsonqw7nzX4leH Yartz472lPurdgOUOWaFpC50LKp2zK9JgQdMn6Abo7S5z5u4Ec7JK7jTWQ98r6LjqhUS foxu44FQ2yxqB8wSsDX+b9WD6cG2qSrcFhm5E/EWR0kDnLsU9Jy2wYTiWGfviZ5fiSb6 Qu9ax0XAtKJUgKdcoSWbC+6v5BWGRTqXmtZzxlCAVTdg4kbRLSAXWVokYb/O5uxKo7Oq Q/ERM4HGzeqNwEc6pfWuM7ba6sgPksoG0udoX/gm8Al+xz64yc43Gqec3tjg3CDvyxsX NuHA== X-Forwarded-Encrypted: i=1; AKwUvBwHFtksVz/suR4DtFNcCG5JLb8Ce8+LqrIq+XWKzv8s9sq5iXBjd1nvE6w7yGGz6MV4ozxo@lists.linux.dev X-Gm-Message-State: AFuF++ljnFxYTwN/6isqXenc+DjlO1THrBWav+NDqlVL12Ygfm43A7dT J43Y40iTYEljrTTHcSXc9OQMqWn8wOWpKiAZR5RYPdvKACliGlGHkEtrZfMyY+TVi+va9CT2VDF NZXwvuA== X-Received: from iofh3.prod.google.com ([2002:a05:6602:6fc3:b0:9c4:a4f8:640]) (user=avagin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:250b:b0:4c4:a240:9c3a with SMTP id 5614622812f47-4d72c0429famr1389700b6e.4.1790223371033; Wed, 23 Sep 2026 21:16:11 -0700 (PDT) Date: Thu, 24 Sep 2026 04:16:01 +0000 In-Reply-To: <20260924041604.3209659-1-avagin@google.com> Precedence: bulk X-Mailing-List: criu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260924041604.3209659-1-avagin@google.com> X-Mailer: git-send-email 2.56.0.rc1.310.g51773c2048-goog Message-ID: <20260924041604.3209659-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.310.g51773c2048-goog