Received: by 2002:a05:6358:3188:b0:123:57c1:9b43 with SMTP id q8csp37281087rwd; Tue, 11 Jul 2023 11:59:39 -0700 (PDT) X-Google-Smtp-Source: APBJJlE1LrVJc+BHQq09QBQb0kQWwCRhH4MTUdZ3315wFvoltuguT7oN45TtjZpVUSLDAFMa3itf X-Received: by 2002:a05:6a20:146:b0:12e:b02c:99e3 with SMTP id 6-20020a056a20014600b0012eb02c99e3mr15557175pzs.50.1689101978749; Tue, 11 Jul 2023 11:59:38 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1689101978; cv=none; d=google.com; s=arc-20160816; b=ArreLPpaD/v4Ow/ud5bHX9/0HzIjqoclP+3nFaWJphesNkGTJjpHCbZUx0/nnNZ9eH fgJ7aDbtl+MOaof7VCX1MdxZax0RbTuR5SQKXLOA1F91cj1RprjAitWq5RmUsjhqhdLt V1TQZz+hwPoX4aZaKFqc51pfUD8Q4IFs+Q8mUiIBD55MatXbp4H2p6sz6roK/y5MHG1c N4CcfPeJXOtzgWs52z3tGaYOzHetf63pf+mfZ74Dtvr1p4EIaFp8MzkZC6doTQZ2KDQ6 nM4MS87As4r1y6NsWHqOTIVMQIUxa72jaJoF2XNMiFIPBs/O7BdDBNSZCPz5ENrb7cXg aFJA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:cc:to:subject :message-id:date:from:in-reply-to:references:mime-version; bh=XoGTdwFW7gZvHQ5oH92AXgCdeMFJfD1AvO5dNwDF5X8=; fh=IS6n6JXkSnax0io64QUNfh/7ZDc8rCm7Cv277sFzMpA=; b=ALajZccWbF/LnkwGGjlJ+IhKKug1WXVHgZqUychsxsVAigzqPge38r01ip13I9X0R4 gTQdTz2/C5PLeagzRsdod8jHNm+pd0Q+k356wksBxHz4bFbMYofih+IJQPpEDI7Umn9m QfbE+c6MU+1fMzMmvNi47nUgnXuBdBcMioJ9qnkncg1pvM+KxwAEGk3w3jZFyDR58Hxy zexB2tujQIsDlPocuztnbwxyv7vq0EmAYGI3fU7feskql156eBKN4tSgyxg+QeBIIQZR RFiEUBJ8ue9BUElFO6Ny+bsABFsC2msmW+KL0y4MMSkueOl4RZnpuhxTxzVwxVVPcoUq 535Q== 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=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 fh17-20020a056a00391100b006778caabbd5si1907466pfb.11.2023.07.11.11.59.26; Tue, 11 Jul 2023 11:59:38 -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=pass (p=NONE sp=NONE dis=NONE) header.from=kernel.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231208AbjGKSFk convert rfc822-to-8bit (ORCPT + 99 others); Tue, 11 Jul 2023 14:05:40 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:55604 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229660AbjGKSFi (ORCPT ); Tue, 11 Jul 2023 14:05:38 -0400 Received: from mail-wr1-f51.google.com (mail-wr1-f51.google.com [209.85.221.51]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B325B10D2; Tue, 11 Jul 2023 11:05:37 -0700 (PDT) Received: by mail-wr1-f51.google.com with SMTP id ffacd0b85a97d-3145fcecef6so1286629f8f.0; Tue, 11 Jul 2023 11:05:37 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1689098736; x=1689703536; h=content-transfer-encoding:cc:to:subject:message-id:date:from :in-reply-to:references:mime-version:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=jN4aAUtnCM1PMkPeyFKL9GEE0yIi4I315hV3adntXQQ=; b=cQTtPWtuAXSc5Sb7Dezk6Q70atT8nC4y+lcDiFvlIKMqCiL+lx96IxiyV8yCXPBitE CmdGWGHJl/YvB9v1TY+dCI6cqs23YXbqNrOEydd/CBdljHjwcKCpoCNbEw9qopbQYDCC O7Kt6G1w8uidbOmb0f/M2aMKaLkxwsDWyqZNCPkXm19e69PZpXykls29sdL5EwSL1AyR 7ZOV4lQNA42rZgDz7O5/A3gcYlsMLv1mAld5KysFO4ga/wzsDObwQwJbv+cyn6xnXeSq /G4siGauTKZeAtE72yMkYwQUT2TnY8liCXCzzfXbHdI/Uel4PQtygRVqNxt/Sk0BfNBj OSkg== X-Gm-Message-State: ABy/qLZzDU4eUrdNzqe2jBDHYGJ6mOCDFOwQJYffb6QUV7DjEE6XEet5 uU175wbWxd9nYDqc6yPfku/iGWD42mnq/01lSSQ= X-Received: by 2002:adf:cd86:0:b0:314:1af1:4ea6 with SMTP id q6-20020adfcd86000000b003141af14ea6mr15669150wrj.5.1689098735993; Tue, 11 Jul 2023 11:05:35 -0700 (PDT) MIME-Version: 1.0 References: <20230708112720.2897484-1-a.fatoum@pengutronix.de> <20230708112720.2897484-2-a.fatoum@pengutronix.de> In-Reply-To: <20230708112720.2897484-2-a.fatoum@pengutronix.de> From: "Rafael J. Wysocki" Date: Tue, 11 Jul 2023 20:05:24 +0200 Message-ID: Subject: Re: [PATCH 2/2] thermal: of: fix double-free on unregistration To: Ahmad Fatoum , Daniel Lezcano Cc: "Rafael J. Wysocki" , Amit Kucheria , Zhang Rui , kernel@pengutronix.de, linux-pm@vger.kernel.org, linux-kernel@vger.kernel.org Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: 8BIT X-Spam-Status: No, score=-1.4 required=5.0 tests=BAYES_00, FREEMAIL_FORGED_FROMDOMAIN,FREEMAIL_FROM,HEADER_FROM_DIFFERENT_DOMAINS, RCVD_IN_DNSWL_BLOCKED,RCVD_IN_MSPIKE_H2,SPF_HELO_NONE,SPF_PASS, T_SCC_BODY_TEXT_LINE autolearn=no 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 Sat, Jul 8, 2023 at 1:27 PM Ahmad Fatoum wrote: > > Since commit 3d439b1a2ad3 ("thermal/core: Alloc-copy-free the thermal > zone parameters structure"), thermal_zone_device_register() allocates > a copy of the tzp argument and frees it when unregistering, so > thermal_of_zone_register() now ends up leaking its original tzp and > double-freeing the tzp copy. Fix this by locating tzp on stack instead. > > Fixes: 3d439b1a2ad3 ("thermal/core: Alloc-copy-free the thermal zone parameters structure") > Signed-off-by: Ahmad Fatoum Daniel, this looks like a genuine fix to me, so I'm inclined to pick it up directly. Any objections? > --- > drivers/thermal/thermal_of.c | 27 ++++++--------------------- > 1 file changed, 6 insertions(+), 21 deletions(-) > > diff --git a/drivers/thermal/thermal_of.c b/drivers/thermal/thermal_of.c > index 6fb14e521197..bc07ae1c284c 100644 > --- a/drivers/thermal/thermal_of.c > +++ b/drivers/thermal/thermal_of.c > @@ -238,17 +238,13 @@ static int thermal_of_monitor_init(struct device_node *np, int *delay, int *pdel > return 0; > } > > -static struct thermal_zone_params *thermal_of_parameters_init(struct device_node *np) > +static void thermal_of_parameters_init(struct device_node *np, > + struct thermal_zone_params *tzp) > { > - struct thermal_zone_params *tzp; > int coef[2]; > int ncoef = ARRAY_SIZE(coef); > int prop, ret; > > - tzp = kzalloc(sizeof(*tzp), GFP_KERNEL); > - if (!tzp) > - return ERR_PTR(-ENOMEM); > - > tzp->no_hwmon = true; > > if (!of_property_read_u32(np, "sustainable-power", &prop)) > @@ -267,8 +263,6 @@ static struct thermal_zone_params *thermal_of_parameters_init(struct device_node > > tzp->slope = coef[0]; > tzp->offset = coef[1]; > - > - return tzp; > } > > static struct device_node *thermal_of_zone_get_by_name(struct thermal_zone_device *tz) > @@ -442,13 +436,11 @@ static int thermal_of_unbind(struct thermal_zone_device *tz, > static void thermal_of_zone_unregister(struct thermal_zone_device *tz) > { > struct thermal_trip *trips = tz->trips; > - struct thermal_zone_params *tzp = tz->tzp; > struct thermal_zone_device_ops *ops = tz->ops; > > thermal_zone_device_disable(tz); > thermal_zone_device_unregister(tz); > kfree(trips); > - kfree(tzp); > kfree(ops); > } > > @@ -477,7 +469,7 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node * > { > struct thermal_zone_device *tz; > struct thermal_trip *trips; > - struct thermal_zone_params *tzp; > + struct thermal_zone_params tzp = {}; > struct thermal_zone_device_ops *of_ops; > struct device_node *np; > int delay, pdelay; > @@ -509,12 +501,7 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node * > goto out_kfree_trips; > } > > - tzp = thermal_of_parameters_init(np); > - if (IS_ERR(tzp)) { > - ret = PTR_ERR(tzp); > - pr_err("Failed to initialize parameter from %pOFn: %d\n", np, ret); > - goto out_kfree_trips; > - } > + thermal_of_parameters_init(np, &tzp); > > of_ops->bind = thermal_of_bind; > of_ops->unbind = thermal_of_unbind; > @@ -522,12 +509,12 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node * > mask = GENMASK_ULL((ntrips) - 1, 0); > > tz = thermal_zone_device_register_with_trips(np->name, trips, ntrips, > - mask, data, of_ops, tzp, > + mask, data, of_ops, &tzp, > pdelay, delay); > if (IS_ERR(tz)) { > ret = PTR_ERR(tz); > pr_err("Failed to register thermal zone %pOFn: %d\n", np, ret); > - goto out_kfree_tzp; > + goto out_kfree_trips; > } > > ret = thermal_zone_device_enable(tz); > @@ -540,8 +527,6 @@ static struct thermal_zone_device *thermal_of_zone_register(struct device_node * > > return tz; > > -out_kfree_tzp: > - kfree(tzp); > out_kfree_trips: > kfree(trips); > out_kfree_of_ops: > --