From patchwork Thu Oct 20 08:00:48 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Zhang Qilong X-Patchwork-Id: 617043 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 4E33CC433FE for ; Thu, 20 Oct 2022 07:56:18 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S229514AbiJTH4R (ORCPT ); Thu, 20 Oct 2022 03:56:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:46864 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S231139AbiJTH4Q (ORCPT ); Thu, 20 Oct 2022 03:56:16 -0400 Received: from szxga01-in.huawei.com (szxga01-in.huawei.com [45.249.212.187]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 01D58172B5F for ; Thu, 20 Oct 2022 00:56:07 -0700 (PDT) Received: from dggemv711-chm.china.huawei.com (unknown [172.30.72.53]) by szxga01-in.huawei.com (SkyGuard) with ESMTP id 4MtKY31QYVzmVC3; Thu, 20 Oct 2022 15:51:19 +0800 (CST) Received: from kwepemm600014.china.huawei.com (7.193.23.54) by dggemv711-chm.china.huawei.com (10.1.198.66) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 20 Oct 2022 15:55:42 +0800 Received: from huawei.com (10.90.53.225) by kwepemm600014.china.huawei.com (7.193.23.54) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2375.31; Thu, 20 Oct 2022 15:55:42 +0800 From: Zhang Qilong To: , , , CC: Subject: [PATCH] thermal/of: Fix possible memleak in thermal_of_zone_register() Date: Thu, 20 Oct 2022 16:00:48 +0800 Message-ID: <20221020080048.56377-1-zhangqilong3@huawei.com> X-Mailer: git-send-email 2.26.0.106.g9fadedd MIME-Version: 1.0 X-Originating-IP: [10.90.53.225] X-ClientProxiedBy: dggems705-chm.china.huawei.com (10.3.19.182) To kwepemm600014.china.huawei.com (7.193.23.54) X-CFilter-Loop: Reflected Precedence: bulk List-ID: X-Mailing-List: linux-pm@vger.kernel.org In the error path, we forget to free the memory that allocated to of_ops in thermal_of_zone_register(), it can cause memleak when error returns. We fix it by moving kmemdup to the front of using it and freeing it in the later error path. Fixes: 3fd6d6e2b4e8 ("thermal/of: Rework the thermal device tree initialization") Signed-off-by: Zhang Qilong --- drivers/thermal/thermal_of.c | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c index d4b6335ace15..fc8fa27480a1 100644 --- a/drivers/thermal/thermal_of.c +++ b/drivers/thermal/thermal_of.c @@ -596,10 +596,6 @@ struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, int ntrips, mask; int ret; - of_ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL); - if (!of_ops) - return ERR_PTR(-ENOMEM); - np = of_thermal_zone_find(sensor, id); if (IS_ERR(np)) { if (PTR_ERR(np) != -ENODEV) @@ -626,6 +622,12 @@ struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, goto out_kfree_trips; } + of_ops = kmemdup(ops, sizeof(*ops), GFP_KERNEL); + if (!of_ops) { + ret = -ENOMEM; + goto out_kfree_tzp; + } + of_ops->get_trip_type = of_ops->get_trip_type ? : of_thermal_get_trip_type; of_ops->get_trip_temp = of_ops->get_trip_temp ? : of_thermal_get_trip_temp; of_ops->get_trip_hyst = of_ops->get_trip_hyst ? : of_thermal_get_trip_hyst; @@ -656,6 +658,7 @@ struct thermal_zone_device *thermal_of_zone_register(struct device_node *sensor, return tz; out_kfree_tzp: + kfree(of_ops); kfree(tzp); out_kfree_trips: kfree(trips);