Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp4200930rwo; Tue, 25 Jul 2023 02:10:50 -0700 (PDT) X-Google-Smtp-Source: APBJJlEgsHX7tvsvzQQ+0t4YH3zYJihy81ITO9aKQ+dA7B0hNtSLrRDVnbFhyYFJnH+JPayXmfzz X-Received: by 2002:aa7:c0cb:0:b0:522:21a1:4245 with SMTP id j11-20020aa7c0cb000000b0052221a14245mr6486487edp.27.1690276250030; Tue, 25 Jul 2023 02:10:50 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690276250; cv=none; d=google.com; s=arc-20160816; b=LLRGEDgbnyUsvhhzLR2AfSx8+975vJzk038g0uvlWRBZNCiznrL4JrJ3T23VpnXSWu qYLj1lIc1VjxhoVA5lGv6UQBSgzB698rU7NII6Xlij1a5d5x5QqnjFdVUlzVP1OF2KEu atYkqZetkgN7CHWm6FV/nqtkDtnS/rjd4WAlWgwx2K3cXgUFy+T6ux9vn9JZUO5ZzWpc fLleqEUQak1vMO/e+3VNwBm9NVqkOYWS4jugWN7l6x5bWr5MKZuzv/fGz8G+DKBQvDli dy3p2sE5KPnOWiBnI0MeFE2ziueSElLKalhk+HLMX3oCyHK+C0pqB5L4molweuTTo/ha Q4Gg== 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=+CNPWaEHdN1Kw9D19d7etzOozFMLY2jCHPP6RpN18AE=; fh=NDHS63Sbahsnp/TLgi6tnM57k/Y5v8EzMfZQPh+tBp0=; b=VBcybnLRVKFQvvBeSuBReMN8salIUPJwfok7DZVcTPhbAyB/Z9Yip+4OWwhcl+vC3a 9LJIAJIjZAQ2zeySDHKDimGh8eftPvTJJo1FZ5u7ltYdtwQDan8+aOgyPOMZgkMT+CBS /m5BwsnJI6SZgwkB4WTHKsYPblYIABgVIR811xR7o0L3WwzZqfk2LX0g6GAWkhc0RwSq NxV86A+Vv23R/xEBwUKcndXdNokwuc713vosQ7Fpi9Geo/ss3I22FM5bKYT5kpB1LzmW UJAuJbjSppsRc9UKiByXzOyfH9D+PlFHeSV9BWDVvBgE3dYVqIsNPzTO8pXUE4BwMKD5 LXrw== 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 c9-20020aa7c749000000b0051a324db9bcsi7753129eds.304.2023.07.25.02.10.24; Tue, 25 Jul 2023 02:10:50 -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 S232786AbjGYJED (ORCPT + 99 others); Tue, 25 Jul 2023 05:04:03 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48668 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S233006AbjGYJDt (ORCPT ); Tue, 25 Jul 2023 05:03:49 -0400 Received: from szxga03-in.huawei.com (szxga03-in.huawei.com [45.249.212.189]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B5B8B1BFD; Tue, 25 Jul 2023 02:03:45 -0700 (PDT) Received: from dggpeml500019.china.huawei.com (unknown [172.30.72.54]) by szxga03-in.huawei.com (SkyGuard) with ESMTP id 4R99xK2tSRzLnwX; Tue, 25 Jul 2023 17:01:09 +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, 25 Jul 2023 17:03:43 +0800 From: Chenyuan Mi To: CC: , , Subject: [PATCH V2] tools: iio: iio_generic_buffer: Fix some integer type and calculation Date: Tue, 25 Jul 2023 09:24:07 +0000 Message-ID: <20230725092407.62545-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: dggems701-chm.china.huawei.com (10.3.19.178) To dggpeml500019.china.huawei.com (7.185.36.137) X-CFilter-Loop: Reflected X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00, RCVD_IN_DNSWL_BLOCKED,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 | 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 f8deae4e26a1..44bbf80f0cfd 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.25.1