Received: by 2002:a6b:fb09:0:0:0:0:0 with SMTP id h9csp592420iog; Mon, 13 Jun 2022 08:45:57 -0700 (PDT) X-Google-Smtp-Source: AGRyM1uzGqvSEFW7Og8Vg5S87JvDuy3fy57vngfJTY70p8d1osjxLwwnRoJOlZJ/jcYtnyuD2Efe X-Received: by 2002:a17:902:e885:b0:167:5d51:f391 with SMTP id w5-20020a170902e88500b001675d51f391mr255730plg.131.1655135156729; Mon, 13 Jun 2022 08:45:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1655135156; cv=none; d=google.com; s=arc-20160816; b=HzSKIWrGqfAnZXhtICLhY+ib6iLXWG3PqWlyCuFttzKsIkSUeHYWfxeGwfZ7fkc92f V6s8wuw+jzxnMUepvJy5GnlPOeVqfT+6sDMXRhTKwVZqk6+YB//Sgoagu4JMp9iT4EZS WQGLLYXd9wQd07ARPiXaohRUr+p4cbbKV34NdCpcFMRDsNThUp5DUM8P589MNC7ChIjX M3YGToH7z17O9Ly/IUOL8aH79e5F4DRR5016ZuVJLjwf0NVOPaB0GcaGWa7fVhIQVxTd +9b4mJuR6UXWwPjEBTEHiCB5dpiJPLhrGeg3rVrtpySkzetItYqpUILTxn8/Lwcrr5So sDjw== 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 :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=kGgQ7dAimJtoJnOpC4/tMeY67ex4fZfAPK6Uc5Zi/Fk=; b=PRrMfYIn9jSWy6XLiwR/h6NXYaXPTKS3lfUqeAWBFy6elfpBPoheZeVa3o7Vn7vamp IY7ZC73fCV0ENKHMZSTl6riCAQmCHRpojzCRF/9ShlrfYd8Cp8v2jSMbCyKcKkkDxlPr yEkRiMeKtA1sgvhedbYxd7lZYoqBiIbRvKMnvEBai9ZR5gZgv2KDBDYGpoEMIEXWsNaZ 4q6OlSM6eRNO6uhR69y2OskoAkFYwtLeeWm1ZqHlsWFvJtQuWd64Lk1YGIffuKUCJizP +AMSz+PD1b7QYE1t9Gxz9HWYqJSAzlqA2HceDcbVdfzQaspndclVRhWFuvmHx8kaFTju ItJA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=MC2dYi8O; 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=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id w14-20020a170902e88e00b00167863f828dsi12220197plg.119.2022.06.13.08.45.42; Mon, 13 Jun 2022 08:45:56 -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=@linuxfoundation.org header.s=korg header.b=MC2dYi8O; 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=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1344678AbiFMKbu (ORCPT + 99 others); Mon, 13 Jun 2022 06:31:50 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:33512 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244492AbiFMK0k (ORCPT ); Mon, 13 Jun 2022 06:26:40 -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 04F26245BC; Mon, 13 Jun 2022 03:19:47 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by dfw.source.kernel.org (Postfix) with ESMTPS id 8631B6066C; Mon, 13 Jun 2022 10:19:46 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 92720C34114; Mon, 13 Jun 2022 10:19:45 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1655115585; bh=LauyIngqKhHgUdFrZjftbaUlMZvP3vHjbGSVAmxtyFI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=MC2dYi8OE+2G8gAeEsCjUJOt/tCEJNIkjTGFjmTLig7igsD8Er9YtVJriMkE5lGT/ BygtkYS5IJZdubakmteLeZUsJeh+Z+rllTVT0czjhW+CAt7ZeH9g+8v6Vw26h8x8TE rkIeeoMrgcMND6XFdo3wilO3z1PYNmJCK1iN3JYc= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xiaoke Wang , Jonathan Cameron , Sasha Levin Subject: [PATCH 4.9 138/167] iio: dummy: iio_simple_dummy: check the return value of kstrdup() Date: Mon, 13 Jun 2022 12:10:12 +0200 Message-Id: <20220613094913.234307301@linuxfoundation.org> X-Mailer: git-send-email 2.36.1 In-Reply-To: <20220613094840.720778945@linuxfoundation.org> References: <20220613094840.720778945@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.3 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, 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 From: Xiaoke Wang [ Upstream commit ba93642188a6fed754bf7447f638bc410e05a929 ] kstrdup() is also a memory allocation-related function, it returns NULL when some memory errors happen. So it is better to check the return value of it so to catch the memory error in time. Besides, there should have a kfree() to clear up the allocation if we get a failure later in this function to prevent memory leak. Signed-off-by: Xiaoke Wang Link: https://lore.kernel.org/r/tencent_C920CFCC33B9CC1C63141FE1334A39FF8508@qq.com Signed-off-by: Jonathan Cameron Signed-off-by: Sasha Levin --- drivers/iio/dummy/iio_simple_dummy.c | 20 ++++++++++++-------- 1 file changed, 12 insertions(+), 8 deletions(-) diff --git a/drivers/iio/dummy/iio_simple_dummy.c b/drivers/iio/dummy/iio_simple_dummy.c index ad3410e528b6..7fef76f0b5c7 100644 --- a/drivers/iio/dummy/iio_simple_dummy.c +++ b/drivers/iio/dummy/iio_simple_dummy.c @@ -572,10 +572,9 @@ static struct iio_sw_device *iio_dummy_probe(const char *name) struct iio_sw_device *swd; swd = kzalloc(sizeof(*swd), GFP_KERNEL); - if (!swd) { - ret = -ENOMEM; - goto error_kzalloc; - } + if (!swd) + return ERR_PTR(-ENOMEM); + /* * Allocate an IIO device. * @@ -587,7 +586,7 @@ static struct iio_sw_device *iio_dummy_probe(const char *name) indio_dev = iio_device_alloc(sizeof(*st)); if (!indio_dev) { ret = -ENOMEM; - goto error_ret; + goto error_free_swd; } st = iio_priv(indio_dev); @@ -618,6 +617,10 @@ static struct iio_sw_device *iio_dummy_probe(const char *name) * indio_dev->name = spi_get_device_id(spi)->name; */ indio_dev->name = kstrdup(name, GFP_KERNEL); + if (!indio_dev->name) { + ret = -ENOMEM; + goto error_free_device; + } /* Provide description of available channels */ indio_dev->channels = iio_dummy_channels; @@ -634,7 +637,7 @@ static struct iio_sw_device *iio_dummy_probe(const char *name) ret = iio_simple_dummy_events_register(indio_dev); if (ret < 0) - goto error_free_device; + goto error_free_name; ret = iio_simple_dummy_configure_buffer(indio_dev); if (ret < 0) @@ -651,11 +654,12 @@ static struct iio_sw_device *iio_dummy_probe(const char *name) iio_simple_dummy_unconfigure_buffer(indio_dev); error_unregister_events: iio_simple_dummy_events_unregister(indio_dev); +error_free_name: + kfree(indio_dev->name); error_free_device: iio_device_free(indio_dev); -error_ret: +error_free_swd: kfree(swd); -error_kzalloc: return ERR_PTR(ret); } -- 2.35.1