Received: from mail-oo1-f69.google.com (mail-oo1-f69.google.com [209.85.161.69]) (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 47FD4370ADC for ; Tue, 8 Sep 2026 04:34:36 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.161.69 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788842083; cv=none; b=BmXCJntHZhO4BihoC02yV91QRC864SfpIgAdrFcjE6HFfQSDSsryI/npO2wDpJIJVLFovym97es3oXKlusTo8wabZGSOVNnVFrzN8Pr3F2XZ+Br3k0U0BkDxa1udMIq/2xs5zh31KNmnhunWSeXjQ5lrOuZAS9afrq9Nm2OYt68= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1788842083; c=relaxed/simple; bh=Yzbz5KsjH5VLNThiyWRSB/Nuu62H2W9/vrT//XuTC8w=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MUGOPmDR1k9UJVIuOlAQFiHIk/hzWQjIdWrRlDQQ3p9eCUQocMRtiI2DIPPWBXlln5P10y0Aa5e4yRjN+X1gZ7BOZIDru+sKYP2EqfqBaYtGkq7rvQWByl8b88ENR1gkluMw8t8Ak9lDrf7Vj2ZN7JuC3oG5sJzE/nHX0otgzj8= 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=kBVmOnk6; arc=none smtp.client-ip=209.85.161.69 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="kBVmOnk6" Received: by mail-oo1-f69.google.com with SMTP id 006d021491bc7-6bcc9eb528bso208908eaf.0 for ; Mon, 07 Sep 2026 21:34:36 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1788842075; x=1789446875; 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=5TNNxc4M/PskCbQkTRvLFwOXh+gojv+fbauKLe1Eeeg=; b=kBVmOnk6sGIAIq2ZUJZq/HY1haGzugZWEGg5FfZmd8n+6OozBzGT2VFa7ywbMnM24m 4Yezw3kB75eaFjPLR6i+XeYPQzcw8g56irzOvHNYVSXNGU7D4pPTRuxvCJ2hBZnjGfM1 QxwBLBdc2yGPBQYXPt1VhpxUbptp1b5J2jELumC1p8c5C35EKrnjLM1kcj0Af5hsPqgJ IHXcy9skV7OiRK2EjQsmEIJ0/diXaaRPot4SZl7/Fcq2UVLEAF5Te6dWiZptVKU54ocR 0c9DwE8DMW1Q6YfpPy10sV7nSkEu+3Or/RTkp6qq9piqS8qfYdQUxFjV4tqazWgj5qYO gNhQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1788842075; x=1789446875; 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=5TNNxc4M/PskCbQkTRvLFwOXh+gojv+fbauKLe1Eeeg=; b=YPVxPjepLRG2nL9laqNTrmYP+GKAQwFvvrWIMH+eLZrtHSUvZhMh6m1bRVBh4Z2eES murJBEbQEvHoVCkA/w5rFGwmgs8lFtEabhA9D7pDsW8NSm9cPU9gfUhJbJjR0uFRRG1B dqmGQPCURsz3EdcKkZEnjzUr5H91ivnR+rA/z1Wh8gjt9bPEgO087aKaEVME7E/4AZFC BxH0QGQx7JVa5e21Sb2Pa/NW6jRP3aWbGHrjjgUqVvu7B0zRmJcIUCh1n2tb4AKK5UvR bjLsFgb+OMiIAip9yLJ5aslqQ/3Cc+lKeBpfJHxd5VGFrjhQ7zbBuu53Y9A2CG1mRE+m tvJg== X-Forwarded-Encrypted: i=1; AKwUvBxD0WO4n5zN1SkP5DYSCs0w7tMpeL0zrZDpw9C8e2x5SkwYHiXE6V7v0Rker16TWlrEF8DJ@lists.linux.dev X-Gm-Message-State: AFuF++lmNfFrhZPGN9aLUdJETodOkcB9aou7rneyE6PC56W/YUlbUaLQ L1N7DN53FHObFDyvvx9JU8dXNoOo+nDwyT6e59Q2Bjq2YXYiXI/10qQKOBH0ofUGUGsWFseIpsd ywwPeIA== X-Received: from iobih9.prod.google.com ([2002:a05:6602:6b09:b0:9a8:51c0:4784]) (user=avagin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:a28c:20b0:6ba:2927:3661 with SMTP id 006d021491bc7-6ba29273f2fmr7920635eaf.18.1788842074915; Mon, 07 Sep 2026 21:34:34 -0700 (PDT) Date: Tue, 8 Sep 2026 04:34:25 +0000 In-Reply-To: <20260908043427.1842515-1-avagin@google.com> Precedence: bulk X-Mailing-List: criu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260908043427.1842515-1-avagin@google.com> X-Mailer: git-send-email 2.55.0.979.g7e5102b832-goog Message-ID: <20260908043427.1842515-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 a7b6524a9dea..fe0a29f599d2 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) static 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.55.0.979.g7e5102b832-goog