From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-qv1-f47.google.com (mail-qv1-f47.google.com [209.85.219.47]) (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 0441839524B for ; Sun, 8 Mar 2026 18:10:58 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=209.85.219.47 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772993460; cv=none; b=gg702NH6NnmDXLYCT6VNddb3+oq79kd8YreX5gcKDVy10QP6h4/DjG5dzOyIM9GY7xID2jXk58IhRDULdgLzMByzHBBbb0BWW1zP+6doQHeU8JvqvhVtY6oe+Yz+hCMd7HWXHgytjTS9RM4tzarEt4SVCR/QpbrIohMR8jkRbCM= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772993460; c=relaxed/simple; bh=IwfQEwT5INKzw3puHvNTQKi8T7p74fMA9ZIKgGKSlPU=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version; b=nQMV1AxarmmO0Zz5g1ZVHJxGIAVf3IiuPjYgGV1neKtn+rtvQOC/nRVSapSCEpJU6l27hUEEa4bg1DC+KUPtS9cVme8ZI/F/2QZ9xDYi7gwu6ROgp+idTPtat5+Rj7CpEopnT/RWVQvAsYkq1yXie2TzH5rH9CUrON6zFnSEmOU= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=l+FwrpiT; arc=none smtp.client-ip=209.85.219.47 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="l+FwrpiT" Received: by mail-qv1-f47.google.com with SMTP id 6a1803df08f44-899fac9caabso106017876d6.1 for ; Sun, 08 Mar 2026 11:10:58 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1772993458; x=1773598258; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=F3267KyolD+3iYhh1Tt+JnE3J++7p+i3QqFJsRb9DQA=; b=l+FwrpiTPNRoeI8X/0d+mCZXul6LP17Zov3G6NhYm4Nbv/9PmemNeOL5x4UxO+IHra yuxawXx0cBvWZxQH4XhqnaYuwG0WPro/mElaMWZ6lqnw6DeYRB7EhnBecv2/7zXKRRPc ztYgfPiQkWAQc95ZPgw80cEc5CehLXOq+qxaaPjJbidFWFPoHbyUa2YFxzKiV7jnq0ir g7NiGQVM/Dcpuex88mqTG5YzAn2OcZax+UuWSuuK7LpjanoD7D5vddlMH+0VuDfo3chO W+nN9R29dIwDzXo/uWovQua56Cn5XDkEC2gwY3fw2/Pqgnr74xPkQUNSCmuACDCRTLB3 xwlw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1772993458; x=1773598258; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-gg:x-gm-message-state:from :to:cc:subject:date:message-id:reply-to; bh=F3267KyolD+3iYhh1Tt+JnE3J++7p+i3QqFJsRb9DQA=; b=mtlLv91PjKHBBIovKh/PgrumA9WpaEFG5pIMV/Jkx8Jxl46/i4ygls11t48XV5xro0 O+QaJ6NpLgJC6eZHqOfmkn01yUBI/OVT4alxbV8oSRKykMHSn+yaJnAh5lj7S/KN8OFI t0FowcdjDQQLfNhJX3YvP9ne5/NzUaHLWcDGPgnKQPcROsCLoHr/fQdKkdzdMacmUw2z 8OPnLUBHS6Puab6Hki3UhEJbj4EJp6/i3+boterjezSOkPZ08DXk7ggh/YhkBjf2AZ1Z /9QyUEUqFqb1rZbttDQOMUSHbB3/U4pAuouI6b7KYZkHGlLRAd44TlraBHfTFmGasPU4 /WXQ== X-Gm-Message-State: AOJu0YwUBwEwHvSRA7BdqCKMeTOy6VGCvcz5ETbkhlZczc+wIIGTjRes tXUzJ0sSc/Mv6vGcwrFzjwFDkjciICkaT7uo9e0lpsoB/LaNOL/CmQi29juSelFgvWg= X-Gm-Gg: ATEYQzyMF1C4ASfshBTd9icF5q939VhPCU93IIly5Hol2OUZGN+SMRp24h99T2aUSUu hAtRiIz1g5uttbJVU3k8LBwV0BVo7kqnV9evTujtCRPjqv6z86ddyqyu6i1Vk+0XW9sLgl39duv FzcHUOk+Hzo1oZjRDR6Alq8pktJf9IPR0dqk/wDOTsPwtMaF+2llDkTVeFSsjTLCwfOIVbiXBtJ zQDJLivFT0MKr44Dpq0HDPe23i7CzOLGDX7+OJmNCX9iA5so2Ayy40koM0PDCIfDxV+jQ2D2v2x N57TcgO4XiVBFLvSMfo3EtDXR63lUhWyu/Wp5tZyPbG+dkYrW/Mu3cO47T4TRwHLU1veha8zBho ZuDQVcXt+mJ6c5lJkjaKrl+EzhN116rgxg827/7R1ZSHpqn6fkgmXAbrZSipCqWUs7Zs5wcXrvq WZZv9XULUvzKezEMUDSZ2kAFnjktG2qsTUWkNRoJoA/jeezqexqZ+XvL3UH966NcpMEnLpu5y4G +rE X-Received: by 2002:a05:6214:f21:b0:89a:145f:47df with SMTP id 6a1803df08f44-89a30a1e7ffmr127097386d6.3.1772993457979; Sun, 08 Mar 2026 11:10:57 -0700 (PDT) Received: from instance-20260207-1316.vcn12250046.oraclevcn.com ([150.136.248.187]) by smtp.gmail.com with ESMTPSA id 6a1803df08f44-89a314645fcsm56199166d6.22.2026.03.08.11.10.56 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 08 Mar 2026 11:10:56 -0700 (PDT) From: Josh Law X-Google-Original-From: Josh Law To: Andrew Morton Cc: linux-kernel@vger.kernel.org, Josh Law Subject: [PATCH v2 2/2] lib/ts_kmp: fix integer overflow in pattern length calculation Date: Sun, 8 Mar 2026 18:10:54 +0000 Message-ID: <20260308181054.2884026-2-objecting@objecting.org> X-Mailer: git-send-email 2.43.0 In-Reply-To: <20260308181054.2884026-1-objecting@objecting.org> References: <20260308181054.2884026-1-objecting@objecting.org> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit From: Josh Law The ts_kmp algorithm computes the required allocation size by multiplying the pattern length by the size of an integer. If the pattern length is sufficiently large, this can overflow the 32-bit unsigned int before it is widened to size_t. This could result in an undersized allocation and a subsequent heap buffer overflow when copying the pattern. Fix this by explicitly checking that the length does not exceed the maximum safe threshold before calculating the buffer sizes. Signed-off-by: Josh Law --- lib/ts_kmp.c | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/ts_kmp.c b/lib/ts_kmp.c index 5520dc28255a..e07f5e80d076 100644 --- a/lib/ts_kmp.c +++ b/lib/ts_kmp.c @@ -97,6 +97,9 @@ static struct ts_config *kmp_init(const void *pattern, unsigned int len, unsigned int prefix_tbl_len = len * sizeof(unsigned int); size_t priv_size = sizeof(*kmp) + len + prefix_tbl_len; + if (unlikely(len == 0 || len > (UINT_MAX - sizeof(*kmp)) / (sizeof(unsigned int) + 1))) + return ERR_PTR(-EINVAL); + conf = alloc_ts_config(priv_size, gfp_mask); if (IS_ERR(conf)) return conf; -- 2.43.0