Received: by 2002:a05:6358:7058:b0:131:369:b2a3 with SMTP id 24csp6499283rwp; Tue, 18 Jul 2023 01:00:11 -0700 (PDT) X-Google-Smtp-Source: APBJJlElSsrVV+38oFk5MB3+xcEfhsixV3eHBhyhgEOMK7vL9LlDpgtaQroK5n61JaZardx5xrwH X-Received: by 2002:a17:906:77db:b0:989:450:e56a with SMTP id m27-20020a17090677db00b009890450e56amr1307323ejn.76.1689667211544; Tue, 18 Jul 2023 01:00:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689667211; cv=none; d=google.com; s=arc-20160816; b=cAhcs+UvPI6S7fBuUnmDN1eKhLPcTDNiHyO4voushB65Y+3CH7uS1aA0oHxjJry+a/ LqxSMDKy27AuMqN4pnJHlQsyJdoi++UsPGIRCVThuaEBqNlVo0K2h3AWpUNMFeXG9jYb QHKR4JE48OBVRj6SMCgtFrlKm8yfsTjacpgUf6L5Cg3T6jnW1Fwx7t4dmPCsSMBXSdup VQW2lWFGvOlSNXuoqTTYjQWhBtcicEKYMgTFvDBouqWw009ZshmcTOgRj2o6dxe9jwF0 7OZT0UteG6lvUXa9p1Q45/hjegavc2/nfysx1kFg/XirK4GVRrfpADWxrMS83zWTp35G jxTQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :message-id:date:subject:cc:to:from; bh=1vVXND76iKL820oDMV4FjGZNkC67bHRr8mUzKgtc7oU=; fh=NDHS63Sbahsnp/TLgi6tnM57k/Y5v8EzMfZQPh+tBp0=; b=dnKdHQI6bj+rzYarAfQfdvYhTfGoPRUbKn9594n7cGYtNVZNZ35eoJw6mv4R7oOTXZ p/iXeGANro3D+15lx6YKsjlOGBpjwezQ5cl6TuXrPcmNahB+BzDfDyfr1NjShTExybCL hxMQRYf0Mwgx1YmMYsW9PQWJxbCjK4tmc36rfj8R9MD70odFBZ1nqcSXbvofeHFOnV7B NLUM7R4rXCu/clmto8oOB53qRJ/RpZRIFpkLZwkSkAIflW7NX8bpYziXKwJ8aNOy6wit Q5iFOPwS2/5FRNUNN6FAgg0/KpdBfJDuiD5Yf++d87QZR/JuV4Kt9G22HzOQATKESxJC eUzg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id l12-20020a170906230c00b00991d54db2a6si791576eja.693.2023.07.18.00.59.47; Tue, 18 Jul 2023 01:00:11 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=huawei.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S230040AbjGRHzM (ORCPT + 99 others); Tue, 18 Jul 2023 03:55:12 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:57782 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229579AbjGRHzL (ORCPT ); Tue, 18 Jul 2023 03:55:11 -0400 Received: from szxga02-in.huawei.com (szxga02-in.huawei.com [45.249.212.188]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 74D408E; Tue, 18 Jul 2023 00:55:09 -0700 (PDT) Received: from dggpeml500019.china.huawei.com (unknown [172.30.72.53]) by szxga02-in.huawei.com (SkyGuard) with ESMTP id 4R4rkT0rt8zNmPG; Tue, 18 Jul 2023 15:51:45 +0800 (CST) Received: from localhost.localdomain (10.90.76.33) by dggpeml500019.china.huawei.com (7.185.36.137) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2507.27; Tue, 18 Jul 2023 15:55:04 +0800 From: Chenyuan Mi To: CC: , , Subject: [PATCH] tools: iio: iio_generic_buffer: Fix some integer type and calculation Date: Tue, 18 Jul 2023 08:15:42 +0000 Message-ID: <20230718081542.2892453-1-michenyuan@huawei.com> X-Mailer: git-send-email 2.25.1 MIME-Version: 1.0 Content-Transfer-Encoding: 7BIT Content-Type: text/plain; charset=US-ASCII X-Originating-IP: [10.90.76.33] X-ClientProxiedBy: dggems706-chm.china.huawei.com (10.3.19.183) To dggpeml500019.china.huawei.com (7.185.36.137) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-4.2 required=5.0 tests=BAYES_00,RCVD_IN_DNSWL_MED, RCVD_IN_MSPIKE_H5,RCVD_IN_MSPIKE_WL,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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 --- tools/iio/iio_generic_buffer.c | 15 +++++++++++---- 1 file changed, 11 insertions(+), 4 deletions(-) diff --git a/tools/iio/iio_generic_buffer.c b/tools/iio/iio_generic_buffer.c index f8deae4e26a1..dc7d19c179ca 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,14 @@ 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.25.1