From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from smtp.kernel.org (aws-us-west-2-korg-mail-1.web.codeaurora.org [10.30.226.201]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id B1A9845C0B for ; Sun, 8 Mar 2026 19:55:20 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=10.30.226.201 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772999720; cv=none; b=pP0wwg7V94LERsYa4MAP/roQxTL0z7Qu/fcDJbAOIuurbcKEMqDuqsv4Xp5EPzMTfiSQVKSwWPhSgRY2h+4iZI5sqwFI/WBvFnmslj30Mj1ffF5pNh0BLNnK+vQ5UvepfJSiG5edtp3pYOXkyam1ctCISc9RJzlrwR4g8SzpM+g= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1772999720; c=relaxed/simple; bh=5TWtnPyZtO8v3Bc59q7m1Z/F8qr02d0lMzPlKYIS2yc=; h=Date:From:To:Cc:Subject:Message-Id:In-Reply-To:References: Mime-Version:Content-Type; b=ptb9fPquSnfwVK2XRTyeEcaouFm+slP2Yrgk8KAw+WdVERg5G+XFSAYzjaAVUxkNEwLpDwDhSbUpf09ZJ7TQrfoKBSmJYo8XCDItFWmn2ceLxOE2hsBjYOGjBEHgG8nUuDtrMNBDL7/fexW8BOURlCfwkIw0M5u7Q71GF/u/zMg= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b=Iv8afn5U; arc=none smtp.client-ip=10.30.226.201 Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux-foundation.org header.i=@linux-foundation.org header.b="Iv8afn5U" Received: by smtp.kernel.org (Postfix) with ESMTPSA id 229D2C116C6; Sun, 8 Mar 2026 19:55:20 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linux-foundation.org; s=korg; t=1772999720; bh=5TWtnPyZtO8v3Bc59q7m1Z/F8qr02d0lMzPlKYIS2yc=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=Iv8afn5UT1jU6ZMaOSm+dPyfsC1IU4+qbz4lgMh967eyCq0zncTp8EBUHzUoVPJZH 88M4VlUcxb/LOgVoixnokNvGB9tv2wM3sLMlw33R7vSaBnse8Dc3t38zDqvZ3aLxpT O+fdt9ZSt+cqk5utoJhpdka+pIy5kZ9Ab2OH96s0= Date: Sun, 8 Mar 2026 12:55:19 -0700 From: Andrew Morton To: Josh Law Cc: linux-kernel@vger.kernel.org, Josh Law Subject: Re: [PATCH v2 1/2] lib/ts_bm: fix integer overflow in pattern length calculation Message-Id: <20260308125519.dc842722209726977a8e14cc@linux-foundation.org> In-Reply-To: <20260308181054.2884026-1-objecting@objecting.org> References: <20260308181054.2884026-1-objecting@objecting.org> X-Mailer: Sylpheed 3.8.0beta1 (GTK+ 2.24.33; x86_64-pc-linux-gnu) Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: Mime-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit On Sun, 8 Mar 2026 18:10:53 +0000 Josh Law wrote: > From: Josh Law > > The ts_bm 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. > > ... > > --- a/lib/ts_bm.c > +++ b/lib/ts_bm.c > @@ -166,6 +166,9 @@ static struct ts_config *bm_init(const void *pattern, unsigned int len, > unsigned int prefix_tbl_len = len * sizeof(unsigned int); > size_t priv_size = sizeof(*bm) + len + prefix_tbl_len; The above description is referring to this expression? I think the uints will be promoted to size_t before the addition occurs? If you're referring to the prefix_tbl_len initialization then yes, overflow could happen. > + if (unlikely(len == 0 || len > (UINT_MAX - sizeof(*bm)) / (sizeof(unsigned int) + 1))) > + return ERR_PTR(-EINVAL); Seems odd to perform these (uncommented!) checks after having performed the problematic operations. Something like: unsigned int prefix_tbl_len; size_t priv_size; /* Explanatory comment goes here */ /* Can this actually happen? */ if (unlikely(len == 0)) return ERR_PTR(-EINVAL); /* Explanatory comment goes here */ if (unlikely(len > (UINT_MAX - sizeof(*bm)) / (sizeof(unsigned int) + 1)) return ERR_PTR(-EINVAL); prefix_tbl_len = len * sizeof(unsigned int); priv_size = sizeof(*bm) + len + prefix_tbl_len; Also, please check the various helpers in overflow.h.