Received: by 2002:a05:6358:701b:b0:131:369:b2a3 with SMTP id 27csp1301772rwo; Sat, 22 Jul 2023 10:18:25 -0700 (PDT) X-Google-Smtp-Source: APBJJlEnfzCm4JsGp4NTC2c7s3hZksdu/9dJpZg9prnTfgA8ArFDwxAYtaUrdPzxX6fyH+ETKrQb X-Received: by 2002:a17:907:2718:b0:993:ec0b:1a27 with SMTP id w24-20020a170907271800b00993ec0b1a27mr5009644ejk.24.1690046305615; Sat, 22 Jul 2023 10:18:25 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1690046305; cv=none; d=google.com; s=arc-20160816; b=Co8PxhUJU5F51QKZSsxt7jd7hRY/UqzaCYha3ZNOLe+JzxJyJVgFoqNprGyHdFIVWK b1xTg0ZJurVZ6W815p6kvcWqMdPBIwGWCdT21ziVE4ThvfDwFC+OZnnKemG8+E+DxwEd vBHgo2ZuIXOxrD1Qh8ePuNrzGrt+OW06LEC9NAVmJz3GYFfIAJmXd8PJ/2dm8TbrwtTr yzuQ8rBdecwHFjb/H4fvnf53+kX7OX5obLQdL2wR8nAyFb0/TqDCHRuUbUcXihC/nc7a ngPhd8foDrooTVU1q1JQn9PKv7FxnxlhqFrA45AiQQT8Tw+Yhdk4ytkuG2kIEo+k80uw RCCg== 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 :references:in-reply-to:message-id:subject:cc:to:from:date :dkim-signature; bh=QGUlbApCBXtIJRbFSzjpQ2BZ603E7daTOT9c1hfgHNc=; fh=lfyksRBsrWd7phJOcWfH0OgBd4369d0K50XpkFSmYWs=; b=ypq/AY/o3WmCoE/UYOcfZsi6pVFB6c6eWhwQnUZTPMrabN2cDfusKDpGmbvNaq6MQ0 jwWFhjcl7/yiWmrv33G3FY9y/qLvTS+gragkekG2BPebrbqD6VNvSkgu3qhSEXrLyQ9J 1Bd+JtOWuUVtlblb9Sgg0AV0rjmTtqdJIBrHunYOMCTXgHGagJLxlQnPSGQOH3RdLWAx 070yLBKKIsATApILJQ0RfBHNY8axQ+pcwUl4hds5xKB1wIo7zNX+cz+PBAXT6RJQlpsy ASj+P63SXmHXFVxfJWELAaCb5oY606tiCZ6LCcWN0nBCY7TE3PArULQgwcBpjkDxig6H Iovg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jUAEq07K; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id f21-20020a170906391500b00997bd26a559si3849773eje.881.2023.07.22.10.18.01; Sat, 22 Jul 2023 10:18:25 -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; dkim=pass header.i=@kernel.org header.s=k20201202 header.b=jUAEq07K; 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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229682AbjGVQuT (ORCPT + 99 others); Sat, 22 Jul 2023 12:50:19 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:58354 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229552AbjGVQuS (ORCPT ); Sat, 22 Jul 2023 12:50:18 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 60957A0; Sat, 22 Jul 2023 09:50:17 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (2048 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id E97F960B06; Sat, 22 Jul 2023 16:50:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id CFC23C433C7; Sat, 22 Jul 2023 16:50:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=k20201202; t=1690044616; bh=9XeEJvlgen8BcyqBv9I/W6Fq2iW/Yz6k/D4YZTouxus=; h=Date:From:To:Cc:Subject:In-Reply-To:References:From; b=jUAEq07Klk95vWO9FT5slhqsTh47S6Pom4vQcWI9MDXX9edeZlAaXCISBdfFraCkc 7pE8bA1zxsO3ggyKp3HVErjgMmR/g/X/4kbioVxIUwHHTJ4GB/PLcGDfVIK8EUL5Xu RnsjY84EEVLpmkBoPyopa8oo1DcAsJ9xi7Ru3MfkJrW4IFmaGh5Dwmq80WM1/gDjS1 17RfDriN65MFFsUzSA99R8xDtf2mW2e9enTx/ROatl/cwEbBgHIXhWqmeLlxainqaa Uq21RisUDgzqVCiFfGR6kmNOoUKnw0JCY8Vde2J7wvW11l8UW0nTx6nEnPqc7LLHXo Vq/uW5iU4MoSQ== Date: Sat, 22 Jul 2023 17:50:10 +0100 From: Jonathan Cameron To: Chenyuan Mi Cc: , , Subject: Re: [PATCH] tools: iio: iio_generic_buffer: Fix some integer type and calculation Message-ID: <20230722175010.2e10bc6c@jic23-huawei> In-Reply-To: <20230718081542.2892453-1-michenyuan@huawei.com> References: <20230718081542.2892453-1-michenyuan@huawei.com> X-Mailer: Claws Mail 4.1.1 (GTK 3.24.38; x86_64-pc-linux-gnu) MIME-Version: 1.0 Content-Type: text/plain; charset=US-ASCII Content-Transfer-Encoding: 7bit X-Spam-Status: No, score=-4.4 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_MED, 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 On Tue, 18 Jul 2023 08:15:42 +0000 Chenyuan Mi wrote: Hi, In principle I support hardening code, though in this case we are talking about example code only. We have libiio and similar for anyone who wants to do more than basic tests. > 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. For this one, in practice it's controlled entirely by the kernel drivers and they won't get anywhere near integer overflow. The change is small however and doesn't hurt readability so I guess no harm applying it. > > 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. That would have to be a very large allocation... I suppose it is possible someone might try it... > > Signed-off-by: Chenyuan Mi My first inclination is not to apply this on basis that it adds slight complexity to example code (the aim of which is too illustrate the interface), however on the other side of things the checks don't add significant complexity... So I tried to apply it, but it doesn't go on cleanly and patch is telling me it's malformed. I'm not quite sure why. patching file tools/iio/iio_generic_buffer.c patch: **** malformed patch at line 68: ret = -ENOMEM; Jonathan > --- > 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;