From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-ot1-f69.google.com (mail-ot1-f69.google.com [209.85.210.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 5F537314B66 for ; Mon, 17 Aug 2026 04:21:03 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.210.69 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786940468; cv=none; b=fKh0ok/ZabnBQZnPsUAMu0ayoIzzo9DMbRODWEsP1+5llOB4TJoSkxBA1yLFr8fL+Vqd6pU8RHeAh83WTfEBMAtVo5zJwCPAcAYGmh/sBloRQpOlpLFVgQl1zklwJuFjc+MEYtLsIEko+CV4gRD80ghrcPc//ZGYlV6+Vv3WHII= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1786940468; c=relaxed/simple; bh=OHRR405LoruzjVQ7I+gV2w9ZETAswaFWPTaGunXzhfQ=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=MEjQ5i9FY2+njXGE2y2VzydfRNvCB8RE6rrNlNGhNc1m4sTbOnUmn8rvBXokBvnNTWbUVHMXp3AgZQ1aXJ+OKOvKHOdtmg68ScF9g5AtBWGDgQJ5DXLBIcNRkENlRvOX2EfCsTJxZFhPQi2tmHWqciE3+pJ/8CgRbYMUSAW8R3E= 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=OtgsSWaE; arc=none smtp.client-ip=209.85.210.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="OtgsSWaE" Received: by mail-ot1-f69.google.com with SMTP id 46e09a7af769-7e6b73b3920so2275367a34.0 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=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=akqPyoKu72swjeR5uaSFSTBEl/QkWuBKcd50F31FwJY=; b=OtgsSWaEdwFblci6jL1ODWQD23HPqw5l2tZ75AB0ZaRS8OyCxyZIbVdvCa8CaCm7pM iXyL7hlrMxUD6QldTiSOUZ4UChL2hBurRVnW/9ZIM53lEZBS9D0sF/u3fYVmEGgmStqW 42+Y2iOKicGvasFLQFF6yrJk+1w4uG8QwhlpvJ5pIHfHp5lUBtI8WQhA/8xZ1A83viBQ Sl36ZuXWhzQwRlwlNv5VehkKxvIp7S5aD1loy8L/QbAbM28gg3FHZdeEK7rQiBcn23Wn ZF/Q9L7Vl58Knd6Mp2s5TgU+oBYz0isjy20abaw/eePXP1iAOffbere5trQSpOBdoR4h O5gA== 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=pJC8kvQZw8jFHvmLwpC+S4UjjZdevUzeP3bKm4aaZfi2B5pmi5QuVFB1OmChhmg+ph LJnhh8dh2HDzGPUUAAOFo+WfZ6fOBBz56yJKr5q4GnnbqNtktLEtCzLsKhKn555xi5g/ useGrIHj18AAjHUCMGviFvKNODiqufLTbkvb0SwnRg9ph6LUYOWES7f+4ae76ADR/PCf 8qY4oO8HivExnAmyOhoJNzfJaUzJt69kDPg4vzThnls0JF8o4L/pt5DZi7COuaBblalO Mdl4LQ4R6S14fypC02hMV0YOqYiy2ZL7L2Lwhi+fbFVv74VZFEHwLllFh+qhGXkv/sga qbIw== X-Forwarded-Encrypted: i=1; AHgh+Rod22+FwThQK0Lno9LE5JTkCVs0+GlNM5qZSCQZE+6JjrsNAdllrbRj0lqdlkhHGQ0wKfWt@lists.linux.dev X-Gm-Message-State: AOJu0Yyp1qjeUjLyk9Sw9NCtplPUAtxUpPUfJQ10lk3n9JdZzIqG9ist LaBpYE9L9RRP+W8mcw/d8eZr0eU62R+2ZhMyLYzcW3nOiQYWFXQBCoc5DD6BIWli+fRMlCquuD8 MndJMRg== 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: criu@lists.linux.dev 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