From mboxrd@z Thu Jan 1 00:00:00 1970 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 6BEC85474E for ; Mon, 17 Aug 2026 04:21:03 +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=1786940466; cv=none; b=uebUaIkwojP6XXR5xxXkEAz9EfHx6/jxx324hVlpJCDd+ZOmE8MWrTC8qyB6+C/tXvx4out+cE79XtOFMywOjgCnLE3flC2n4JOMolcgDScDgefqu8d5pBdPP19RGBGgNWOSvpbl0bAzrKFmyDeECakjlmff9Xl6tyrbdMGHE7s= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786940466; c=relaxed/simple; bh=OHRR405LoruzjVQ7I+gV2w9ZETAswaFWPTaGunXzhfQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=XCaHlOrQrhnerCkyGO4ZQHl6XgBFJ7gZSxnS/YcnKaxBd0GZUVywYmnpQLFHpeaHoJG4pbjHM1lOMjfZaftZ/Hhh8bV1CSH6l8VDDE2yjyBIKW4enCPV+4EyUANf4Yg11XI//pAs/hsfHSXi0TrUIahHcpTE3gMFu+c2QgGpV1E= 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=insoOdzq; 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="insoOdzq" Received: by mail-oo1-f69.google.com with SMTP id 006d021491bc7-6a3da2369aaso1962331eaf.1 for ; Sun, 16 Aug 2026 21:21:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1786940462; x=1787545262; darn=vger.kernel.org; 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=akqPyoKu72swjeR5uaSFSTBEl/QkWuBKcd50F31FwJY=; b=insoOdzq5v1h4ICmckwZnsDC098zzN3DyUuB0OcrCdZ6KNAYVgLV8hwNVRTnXYi0PV plh8ZsYR/fI9wenimjKhXpH7raUB46dGiOqBld8VrBJLQvDuXg7Xb4f3kzmlvP8g8yt0 JwWscLgBn2BqSkTB8j2qJyXsceBba49JdlHJrQ6kqcCntH3LHY0UrW2b6lTWT4tOE3Op X69KPcU5CSveJgrwqO4F8r94Ho7Rk5gGUDFq4OOq/3QPJNjg9p/itDPGEjw9P+AepRG4 Rfy7m1yqq23gqvtUf4wd6B3GWNKzf22P5ltqWYpTG1Y1jV1Gs4tx1Y6NwZd3kWHeiEwj Bn/Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1786940462; x=1787545262; 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=akqPyoKu72swjeR5uaSFSTBEl/QkWuBKcd50F31FwJY=; b=Velfc6Ju+Z/vuQyxT9nMahL0Caa8p80ctkwrKdIyMJ1gEdRUefMLNyGfiNIa7dtzr9 z8I6DsCtfwQSsUG1eUXi+bNOHVOY83tAPoYIaAFa38M7nxPz+QH7eb0+qZaN3PLrVigp /KPjoOitQErn8gh7CqLa0iTvVafdw/2XCoOcQcrOPEjiotS5BDy/5mXfZJesm8nFVZ7q YRNpH4+JWewBpZOTa6x4bccJIOCAIthUpRu/Gt9hLXbZixThT1ddMrLCf4BfKvvPJnDS z8xrlkWtANNeKHFCl3xPbuqg+D39EhZaFv7uUF/hiR8lvhXwhZteMzBPyc5pHzdux0pI IJFQ== X-Gm-Message-State: AOJu0YwdswSfUPXgSA99lmeha8sQjyNzRYdy41vGsRpfZqMJBlL4/aPq brh4hQbOvsQSrbiE8O1DPtbO/9Qn9OiCER/9uUrXxVfajRhqaQm82qYJ/UXP/HZY8f6lT4ZXvsi DKf62PQ== X-Received: from jabjx3.prod.google.com ([2002:a05:6638:a283:b0:5e9:17a8:a8c9]) (user=avagin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6820:f02f:b0:6ac:b3b1:a5a2 with SMTP id 006d021491bc7-6b0d5db38f0mr18372936eaf.0.1786940462110; Sun, 16 Aug 2026 21:21:02 -0700 (PDT) Date: Mon, 17 Aug 2026 04:20:46 +0000 In-Reply-To: <20260817042048.1579415-1-avagin@google.com> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260817042048.1579415-1-avagin@google.com> X-Mailer: git-send-email 2.55.0.691.gc56d675ccc-goog Message-ID: <20260817042048.1579415-7-avagin@google.com> Subject: [PATCH 6/8] 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 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..ed39d7051d0d 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.691.gc56d675ccc-goog