From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-oi1-f197.google.com (mail-oi1-f197.google.com [209.85.167.197]) (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 A13E74E06F6 for ; Fri, 25 Sep 2026 16:25:04 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.167.197 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790353506; cv=none; b=qpxzFJ2HUqn2dMvK2Q993ucikw5tCedd1NnSPBQDrxjgc6SZGco89hUPUi0B/zpthwrSUejkR+HGKrYRbSNqWi7tJqAqeP/QplrNqVxpwk79mhl64jmATkFmyWzYQbhYDj68jmUNM5kgZatnSForetoT/ie/taJ9VFifRplBKoc= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1790353506; c=relaxed/simple; bh=D0xYUozz0eKaRjqfdaOEvfYWvBHmjoShCH+XjEeHxWg=; h=Date:In-Reply-To:Mime-Version:References:Message-ID:Subject:From: To:Cc:Content-Type; b=WDK45297OsPwXBGtBuwoDmgNmvB77NtopR6TCCakOYi4gqkr2pft1Zl022qQaAmchhki3/eM6AowZLcE5V3HwKaYxg06fR7+27028jGOH8HLq5Xfb2RTtdkJQHhTLaEHFbveu+XcCxFSXwKqtyiJZMyWhrVLEwH2bgMZWBb0A3M= 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=qSx0l9On; arc=none smtp.client-ip=209.85.167.197 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="qSx0l9On" Received: by mail-oi1-f197.google.com with SMTP id 5614622812f47-4be498aca30so1395195b6e.3 for ; Fri, 25 Sep 2026 09:25:04 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=20251104; t=1790353503; x=1790958303; 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=wVFocSfR4Lgc319cRBvL1V/1tH2jGEIsWXRjOMDStP4=; b=qSx0l9OnTFm6fnCSpt+b2Y80pk9AyQPUu98FyjBwaLIBzFG3lOn3aITOhjDpazkyf7 VWxAjCxLPQ9qfSiSWJ975By490DKL0ogdCHZoUes2j4rFb9OTTwlR9/JiRCsXzDfCAxF 7IQbHMm1mAnLkB8dPIc376QRkmF/cjjd02l28h0kW4QypOejrSrFe2ddCcnxrSi/PrOU dPicsmOHKXISAZAqVRLLc+ulaS2IQTpVxMFswF9IJv7nbMATatwJTZVV2y6y+tk8zVhO SqRX2qLJzjZcGv5kTLAn6iRg8fNYtmUqlvI95kDlHKyJxFv0cyPY3GL2Y8nFwbJSgcV5 KrIA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20260707; t=1790353503; x=1790958303; 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=wVFocSfR4Lgc319cRBvL1V/1tH2jGEIsWXRjOMDStP4=; b=AjGWc5C+e+5HSBwRTR2KIZW/v7szGMlmfXZt8CoiRP5nxJO1wdVGWsvEUMkKc3HUz9 cSq1jt4/6odevoj2Wm0yiSsfYMmBgzzLP1GlXswN2sCF+y7LhWDoxblseG7hwNWmc/Ty BJR2Z5+psDcz8f8l5lNPS2sGtCKs+/5BuPkmJULg2AaYtDRpHSA/oLCTLoFEiwjC/AFl uoqygHUaaCi0X8lnkR46rI+4/M0tnQFe/e78WLrUbxKrIZ1fJp3jj00JIVLET/HU1Jid lwR3k1gU8x/ryqxwCvKSNbmmq4kptaAECfhlqVVEig9OgNl8n2FgMUujPLKNmbvU5o9v 0IQg== X-Forwarded-Encrypted: i=1; AKwUvBwnoc9QI0ePT6W40vOAHTlvPN6WSa2yK3VfL0+xaYcCWAPQH1vR7qlYuhy0iPdNKH0zhKsI@lists.linux.dev X-Gm-Message-State: AFuF++k5CymYPjtR4b3VmYKAbIQaDPAb42FpVMgEa3a6wm92NqT6h2M7 JCn+m6UXHXiIdxOoEqBX7s/ZxpWsaloy0iaimzb7dDrVGaQy14TX2dho5qmCynSpVR6CGBjj/H6 UT0TMxQ== X-Received: from ioed30.prod.google.com ([2002:a05:6602:281e:b0:9c0:11e6:874e]) (user=avagin job=prod-delivery.src-stubby-dispatcher) by 2002:a05:6808:3a09:b0:4cb:818f:d572 with SMTP id 5614622812f47-4d72962202dmr7031617b6e.26.1790353502872; Fri, 25 Sep 2026 09:25:02 -0700 (PDT) Date: Fri, 25 Sep 2026 16:24:51 +0000 In-Reply-To: <20260925162454.1403405-1-avagin@google.com> Precedence: bulk X-Mailing-List: criu@lists.linux.dev List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 References: <20260925162454.1403405-1-avagin@google.com> X-Mailer: git-send-email 2.56.0.rc1.315.gc6ed9934b7-goog Message-ID: <20260925162454.1403405-6-avagin@google.com> Subject: [PATCH 5/7] x86/fpu: Fix potential underflow in xstate_calculate_size() From: Andrei Vagin To: Borislav Petkov , "Chang S. Bae" Cc: linux-kernel@vger.kernel.org, criu@lists.linux.dev, Thomas Gleixner , Ingo Molnar , 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 97cfd4fb6cc0..efa7879d2f97 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.56.0.rc1.315.gc6ed9934b7-goog