From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f201.google.com (mail-oi1-f201.google.com [209.85.167.201]) (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 82CAF3AFAEA for ; Mon, 15 Jun 2026 19:37:33 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781552254; cv=none; b=ra0c9IaQzyG4FI95gqDXwhktYMZoURNCJ46JBHjyNrcIuDmgCgAWxO1g9aCkU1E1KoQRpSGwsc7yAzprdl4QbDSdooUF+XDHk2onmilC1to7VJuLCgCPGd0ziOalDVMleMCUnRY5W6PR776Ew/j/i05YdUkwrcNsP2TBvXJP1t0= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1781552254; c=relaxed/simple; bh=wYoQWE5d9j5J4hPgDSIcXpdkkNH9h+1efcmEowuqdq0=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=OEFEnVJL14VHgyo6tZuZZ4cvIF/fiGVBgOkJ4wmPDRLK62XjM8B0vanAke5jCxc6C3sTenwiNpcXTOhDbjzPhHs7Y8IBUrxkBzT7NjHIRS/UmhOO9X87jPS1tlTBnaw4HpvrQDFZCSJE/huKTuFl6J6BGL9m4IFZrNleqzMiBCg= 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=OPP4MOXD; arc=none smtp.client-ip=209.85.167.201 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="OPP4MOXD" Received: by mail-oi1-f201.google.com with SMTP id 5614622812f47-486db2bf46eso4979205b6e.3 for ; Mon, 15 Jun 2026 12:37:33 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1781552251; x=1782157051; darn=lists.linux.dev; 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=OPP4MOXDKR9O2VsoyaWWZr6eAJzSi5XgyuxpPohNFu4M0O08y9VylKfLRAEoYD1xGs E2qOR5WUqKCOAepbGBZ2PjmQcxcAeSt/zoyaOmPms3IqPElrueMDBvtSsC59D3l7lktW rsjqQgZN12sJfWSimYebv1bgGjjKdGJ02dB0r6Yu2aBjxjx835nTTpbG9dsadCe03wvz QMppwT/8PncAooqq+XbQb5VqyUeg1TceO9eA68ffuNe4taey4+WD2xGXBQc3LKYLeTtk hKSFVBH28AR4iKLV1yBDutgC/qr1WSRt5X7LnUZBOmcdF2L9UHu3NAOtIA0T+eGJzDJA /YWA== 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=NICw6tTYEpuACVvxhe33n4BEOhIXI/o6dT/ZmWARXS8qVOPLc+hSzXKeLnKB8Z3p2n ZzjpFtEhKInUB06tlZCmcSUk9UfZRmLEje1SMiGl8Y0EjM+fOX/SFA4HQwp52he0AhJ9 /jPI3J/jqqEcq/dQ8PY9cv6QqERQifkq3Y27kLBuNAWn15tSgmgR/kozaSpoDnz9uylI S87U3UaCocaWoRfiM6f2+nnhzQhtGGHsypHmGmmvACYGaMAoMWbpOwqC5bjEPMEgF5IZ lpXYKotB182TZOGaPqH5fv6rvj8LZKnlc74PhTK29dZoSnowkZ+xSC87bzVTfK9SIWto xwMw== X-Forwarded-Encrypted: i=1; AFNElJ/UYQqWb5WSH5vjArAILmH7pXjJ9qnNXpFeNukDNAWjqJxBONH/0VwzhrE4fX9nYi4JI+XH@lists.linux.dev X-Gm-Message-State: AOJu0YySElMUFbm9Z0nWJ7N6X/81ZYisauNX24NjIcGoOVB2mnhq80l9 Z+lIIJSLG4WoNgzJfhZb9v+iHvQ3nxW13kaNrBtDE4XjWQiPqR4QiycsPI3pU2BanUlkO8sx9fW rbEmlEA== 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: criu@lists.linux.dev 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