From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f202.google.com (mail-oi1-f202.google.com [209.85.167.202]) (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 964313B14AB for ; Mon, 15 Jun 2026 19:37:32 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.202 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781552256; cv=none; b=hUEInUd8RNKj3EOIKDrLT6eRT2fBzcVYxV19XTGZQqhanW2fsNefllnB7iWerV7cA2eKJ/cURDDWiPTbSZLXEMj/o9UoMGF5z/aWuY005zo2Wswl0rtD/jpTVrVBacto44zIEdtNmbCbS7CHGiHsUiKGcyESn4nLFQhDFRDslSs= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781552256; c=relaxed/simple; bh=wYoQWE5d9j5J4hPgDSIcXpdkkNH9h+1efcmEowuqdq0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=JpWe7yDDRSB3ENTYZLJCfnxyvZbeReU695N5yFWTUaN/kxUyPgBIwA6C/rSqnqryZRfPgQ5gtsnlKQoLCCA1zK9iQh+1IW8QZSuUMXtsLbyDGt2gxGTrLtoxgrRs7SzNMsitEZxEsUbL0KuDAaXeWfv0CfbC0kzflelzRWUNHiE= 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=Re1Uu+mH; arc=none smtp.client-ip=209.85.167.202 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="Re1Uu+mH" Received: by mail-oi1-f202.google.com with SMTP id 5614622812f47-486db2bf46eso4979200b6e.3 for ; Mon, 15 Jun 2026 12:37:32 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781552251; x=1782157051; darn=vger.kernel.org; h=cc:to:from:subject:message-id:references:mime-version:in-reply-to :date:from:to:cc:subject:date:message-id:reply-to; bh=NZ5mMyEOm0dY6i0ikXfFXUA/TA5uItGvTRL3OYslBFg=; b=Re1Uu+mH4lhVyy/r2vYByxqGFpS9HwYRUAByCaJg23mjhRHmWIUG4C/G6knCpq8klp sx9EpKnqM9OaZxtGMmE3WveXEpHMxY0PYXxpgqMYGH+86XlgZnDNNy0qakDsOwTWK3k9 4fi4aWJfgBKl7KEdVyH8sUnNCcFVThy08MKF4SoAY24vQAl08Stb0V1/E2EJcpv08NvO VTl0DlZv03DfCEeF+BUj3OcSW8VrakAuufpoHM1uxoYn+DHGHhNbUgd36vIXJJvvGCsH jt95MtdD41nMo8MhkJTmScBgmDoxT6DXRe3gMdeN1w33YeaQA0MZ+LjijVQFPJIuvix9 S0LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1781552251; x=1782157051; h=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; bh=NZ5mMyEOm0dY6i0ikXfFXUA/TA5uItGvTRL3OYslBFg=; b=fJZRSP+LrdUeVwOCz1VknFuzoQjvqFyDnMR0VDYZWvJQgyYsW6TJBJ4eCDaSgF+n7O Ym0M8vZgHM+xCKgkrIAHHCfhjtCAY+8u/RNddEsPgQM6WmRxCfT2fWbdFAF04+1mY3cU 7EQdrQQFtLOioSS+/P9mu2RS/9n6DXI3A6v9xkOYh9GzNCv8gXI2Lfr49aOnLJHV84ME ljj6YzW8UXTTK1RGMcpmtsCJAfGGcpz1cC+X/Ile/4hFSfNWFYxHl8E44mbl3t/9spmi NcCBjJWi3q8neKakrEMlm7Goxyoe/F8mkdRVpWosLk/xB4aM1JOPPaLaUPhePLIGXuJM bSpg== X-Gm-Message-State: AOJu0YzZZQ7xEiGLH3YXNKemBYDCM66EOuJYIUR4CUEO4Xt4fa12qkhc BW7IdbU56JcXhykTPFkcbPWLwbaC3jEiaV3Fv4hXhEZuPD+BOKIGbttdKn7Bdkgbpqz38RuOkCj HOJghog== X-Received: from ilki7.prod.google.com ([2002:a05:6e02:ec7:b0:502:125b:8168]) (user=avagin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:15a9:b0:479:f7e7:4a81 with SMTP id 5614622812f47-487416dc1d4mr7431004b6e.0.1781552251042; Mon, 15 Jun 2026 12:37:31 -0700 (PDT) Date: Mon, 15 Jun 2026 19:37:11 +0000 In-Reply-To: <20260615193716.1843340-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: <20260615193716.1843340-1-avagin@google.com> X-Mailer: git-send-email 2.54.0.1189.g8c84645362-goog Message-ID: <20260615193716.1843340-6-avagin@google.com> Subject: [PATCH 05/10] 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 , "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") 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.54.0.1189.g8c84645362-goog