From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 99A16EEB57C for ; Sat, 9 Sep 2023 00:41:29 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346028AbjIIAlb (ORCPT ); Fri, 8 Sep 2023 20:41:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:43402 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1343985AbjIIAka (ORCPT ); Fri, 8 Sep 2023 20:40:30 -0400 Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0059230CB; Fri, 8 Sep 2023 17:39:28 -0700 (PDT) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 27A57C4166B; Sat, 9 Sep 2023 00:39:26 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1694219966; bh=3FdsGKdIimLySiH95S3fd9cn34fEnJcqoSPUbeK7X3o=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=i0B9s+MoFEYumJs0iUpr9mggUEdscR4HfeDuLLHFWWA/T9+JftmJgTFLyK/zwnY8M rudQrJ65e+hqB5UTFAeZBde8y6YIlUPXYnuLDJjihiT1HcMMGnmc/8XU5l27HZY6oZ a63RzMM6Svs1bGqhDCeNc4oshJELAzRjj6Tgy9RWEJtdviecrkPXrDBIDK4Wr2o7VU EKl2evIsFCGNGWe6x3r7ek40mzsntPXNL/TFU1idMUADpFqJ10yMA8UIQZvcaWGbHF qTa9Op4CTbwGKPrGrqk4UzDqk3xf6p2aC4S7KJN/lzi0RlBdTzVEoB07Vy2NVm/xNY IPxXRnr5xoZSw== From: Sasha Levin To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Chenyuan Mi , Jonathan Cameron , Sasha Levin , jic23@kernel.org, linux-iio@vger.kernel.org Subject: [PATCH AUTOSEL 5.15 12/19] tools: iio: iio_generic_buffer: Fix some integer type and calculation Date: Fri, 8 Sep 2023 20:38:56 -0400 Message-Id: <20230909003903.3580394-12-sashal@kernel.org> X-Mailer: git-send-email 2.40.1 In-Reply-To: <20230909003903.3580394-1-sashal@kernel.org> References: <20230909003903.3580394-1-sashal@kernel.org> MIME-Version: 1.0 X-stable: review X-Patchwork-Hint: Ignore X-stable-base: Linux 5.15.131 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: stable@vger.kernel.org From: Chenyuan Mi [ Upstream commit 49d736313d0975ddeb156f4f59801da833f78b30 ] In function size_from_channelarray(), the return value 'bytes' is defined as int type. However, the calcution of 'bytes' in this function is designed to use the unsigned int type. So it is necessary to change 'bytes' type to unsigned int to avoid integer overflow. The size_from_channelarray() is called in main() function, its return value is directly multipled by 'buf_len' and then used as the malloc() parameter. The 'buf_len' is completely controllable by user, thus a multiplication overflow may occur here. This could allocate an unexpected small area. Signed-off-by: Chenyuan Mi Link: https://lore.kernel.org/r/20230725092407.62545-1-michenyuan@huawei.com Signed-off-by: Jonathan Cameron Signed-off-by: Sasha Levin --- tools/iio/iio_generic_buffer.c | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/tools/iio/iio_generic_buffer.c b/tools/iio/iio_generic_buffer.c index f8deae4e26a15..44bbf80f0cfdd 100644 --- a/tools/iio/iio_generic_buffer.c +++ b/tools/iio/iio_generic_buffer.c @@ -51,9 +51,9 @@ enum autochan { * Has the side effect of filling the channels[i].location values used * in processing the buffer output. **/ -static int size_from_channelarray(struct iio_channel_info *channels, int num_channels) +static unsigned int size_from_channelarray(struct iio_channel_info *channels, int num_channels) { - int bytes = 0; + unsigned int bytes = 0; int i = 0; while (i < num_channels) { @@ -348,7 +348,7 @@ int main(int argc, char **argv) ssize_t read_size; int dev_num = -1, trig_num = -1; char *buffer_access = NULL; - int scan_size; + unsigned int scan_size; int noevents = 0; int notrigger = 0; char *dummy; @@ -674,7 +674,16 @@ int main(int argc, char **argv) } scan_size = size_from_channelarray(channels, num_channels); - data = malloc(scan_size * buf_len); + + size_t total_buf_len = scan_size * buf_len; + + if (scan_size > 0 && total_buf_len / scan_size != buf_len) { + ret = -EFAULT; + perror("Integer overflow happened when calculate scan_size * buf_len"); + goto error; + } + + data = malloc(total_buf_len); if (!data) { ret = -ENOMEM; goto error; -- 2.40.1