Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp8157468ybi; Thu, 6 Jun 2019 07:38:06 -0700 (PDT) X-Google-Smtp-Source: APXvYqwNH/Ug7DLppNjQc0oun1WuP+g8nmErIM1zPiY7KHeDUbztTZZztxMIm/17tmic2J4L97y1 X-Received: by 2002:a17:902:148:: with SMTP id 66mr47652389plb.143.1559831886325; Thu, 06 Jun 2019 07:38:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559831886; cv=none; d=google.com; s=arc-20160816; b=ZflUAgykJJl6QIgMbUUfYuNwhdonGuqhWEf4ZBtTKs7criFV1RGmNvKxfbjNA3K/xj 7GujWEiZmkmZLgRr7zJ4kJ8zGL86noJo80VzdqXFW2uTt4IB8PGlXmV/IkTsUKZNNefA C33WHyGLXLY8ay8+Dci2yc0DuBoi+QvfD5T0E7St+Xt2Zav19bAf2oY85mdwdj1PcvzU YGRvuMdbuJT11GHh3EMfFfxWtNlPUop/zKATz3p/kf4hNI7cj2wRI7MKwfaDL7aRBJPz xRwMflDpW7pETh8IJGpD7eZpzUbJvb0TQFP6Pz1XQHbuxw7O5qrSwF1KrCcRt+e/dNeB SiGA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:sender:user-agent:in-reply-to :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:dkim-signature; bh=VLzEF5AzOpV6fnQ8fVRLQhxhBcwCON1BJjsm+1F9fTg=; b=evcFDGuHksHqSlLL3ovMus/cD5lj7Te6cy+JrCRh5wwNsMizVlN4/lIaLf2612NnPT 9BpklHivoSHSZhMQPidaQqfy+krlC2/c/Up3H7E5b2t1kf/1IectWqhwH4T4O+M8Zpx2 o0VIpKpvzoeLPXn8dChAv4Ax8I7WPdmgiD47BUBX8BZVMUCDzMKaxByqk6RBgQKJ1my6 Z+8Kd5RNcvj+Egykc2g3OyaZlvly0EItmVuQdlmq+7b2dMqxnDMF99EgYfqA6bwEvhwR yU/VgMXG0JL4hvpJ3l7zWgqBKh2JNaXM+i7xfymzaWGw5boS9DneXeC/LunltcHKD8XD 5Ogg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=aGtubtOB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id m19si2171986pgl.241.2019.06.06.07.37.48; Thu, 06 Jun 2019 07:38:06 -0700 (PDT) Received-SPF: pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) client-ip=209.132.180.67; Authentication-Results: mx.google.com; dkim=pass header.i=@amazon.com header.s=amazon201209 header.b=aGtubtOB; spf=pass (google.com: best guess record for domain of linux-kernel-owner@vger.kernel.org designates 209.132.180.67 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=QUARANTINE sp=QUARANTINE dis=NONE) header.from=amazon.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729141AbfFFOgX (ORCPT + 99 others); Thu, 6 Jun 2019 10:36:23 -0400 Received: from smtp-fw-4101.amazon.com ([72.21.198.25]:27975 "EHLO smtp-fw-4101.amazon.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728508AbfFFOgW (ORCPT ); Thu, 6 Jun 2019 10:36:22 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=amazon.com; i=@amazon.com; q=dns/txt; s=amazon201209; t=1559831781; x=1591367781; h=date:from:to:cc:subject:message-id:references: mime-version:in-reply-to; bh=VLzEF5AzOpV6fnQ8fVRLQhxhBcwCON1BJjsm+1F9fTg=; b=aGtubtOBSSfCd/Us9r1WMlpAy/LpeUgv1w8AaVCuKWEx4CgPct1tf4Yw U9HFmiC0tTq7b+e/ihMQiNzx2LfoobZ2NZsOSN+8p/K/RnneuQ1an0HNQ 34D2UinKkwBxQdkrm4hZSLYpoJe8lSxuY2g8s5IjFPrfMvUjqLkZ5NbXI g=; X-IronPort-AV: E=Sophos;i="5.60,559,1549929600"; d="scan'208";a="769272317" Received: from iad6-co-svc-p1-lb1-vlan3.amazon.com (HELO email-inbound-relay-2c-168cbb73.us-west-2.amazon.com) ([10.124.125.6]) by smtp-border-fw-out-4101.iad4.amazon.com with ESMTP; 06 Jun 2019 14:36:19 +0000 Received: from EX13MTAUWC001.ant.amazon.com (pdx1-ws-svc-p6-lb9-vlan3.pdx.amazon.com [10.236.137.198]) by email-inbound-relay-2c-168cbb73.us-west-2.amazon.com (Postfix) with ESMTPS id 804B7A07C3; Thu, 6 Jun 2019 14:36:19 +0000 (UTC) Received: from EX13D05UWC003.ant.amazon.com (10.43.162.226) by EX13MTAUWC001.ant.amazon.com (10.43.162.135) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 6 Jun 2019 14:36:19 +0000 Received: from EX13MTAUEA001.ant.amazon.com (10.43.61.82) by EX13D05UWC003.ant.amazon.com (10.43.162.226) with Microsoft SMTP Server (TLS) id 15.0.1367.3; Thu, 6 Jun 2019 14:36:19 +0000 Received: from localhost (10.95.251.103) by mail-relay.amazon.com (10.43.61.243) with Microsoft SMTP Server id 15.0.1367.3 via Frontend Transport; Thu, 6 Jun 2019 14:36:17 +0000 Date: Thu, 6 Jun 2019 07:35:44 -0700 From: Eduardo Valentin To: Guenter Roeck CC: Eduardo Valentin , Jean Delvare , , Subject: Re: [PATCHv2 2/2] hwmon: core: fix potential memory leak in *hwmon_device_register* Message-ID: <20190606143509.GF1534@u40b0340c692b58f6553c.ant.amazon.com> References: <20190530025605.3698-1-eduval@amazon.com> <20190530025605.3698-3-eduval@amazon.com> <20190605203837.GA30238@roeck-us.net> MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Disposition: inline In-Reply-To: <20190605203837.GA30238@roeck-us.net> User-Agent: Mutt/1.5.24 (2015-08-30) Sender: linux-kernel-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Wed, Jun 05, 2019 at 01:38:38PM -0700, Guenter Roeck wrote: > On Wed, May 29, 2019 at 07:56:05PM -0700, Eduardo Valentin wrote: > > When registering a hwmon device with HWMON_C_REGISTER_TZ flag > > in place, the hwmon subsystem will attempt to register the device > > also with the thermal subsystem. When the of-thermal registration > > fails, __hwmon_device_register jumps to ida_remove, leaving > > the locally allocated hwdev pointer. > > > > This patch fixes the leak by jumping to a new label that > > will first unregister hdev and then fall into the kfree of hwdev > > to finally remove the idas and propagate the error code. > > > > Hah, actually this is wrong. hwdev is freed indirectly with the > device_unregister() call. See commit 74e3512731bd ("hwmon: (core) > Fix double-free in __hwmon_device_register()"). heh.. I see it now. Well, it is not a straight catch though. > > It may make sense to add a respective comment to the code, though. > I agree. Or a simple comment saying "dont worry about freeing hwdev because hwmon_dev_release() takes care of it". Are you patching it ? > Guenter > > > Cc: Jean Delvare > > Cc: Guenter Roeck > > Cc: linux-hwmon@vger.kernel.org > > Cc: linux-kernel@vger.kernel.org > > Signed-off-by: Eduardo Valentin > > --- > > V1->V2: removed the device_unregister() before jumping > > into the new label, as suggested in the first review round. > > > > drivers/hwmon/hwmon.c | 8 ++++---- > > 1 file changed, 4 insertions(+), 4 deletions(-) > > > > diff --git a/drivers/hwmon/hwmon.c b/drivers/hwmon/hwmon.c > > index 429784edd5ff..620f05fc412a 100644 > > --- a/drivers/hwmon/hwmon.c > > +++ b/drivers/hwmon/hwmon.c > > @@ -652,10 +652,8 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, > > if (info[i]->config[j] & HWMON_T_INPUT) { > > err = hwmon_thermal_add_sensor(dev, > > hwdev, j); > > - if (err) { > > - device_unregister(hdev); > > - goto ida_remove; > > - } > > + if (err) > > + goto device_unregister; > > } > > } > > } > > @@ -663,6 +661,8 @@ __hwmon_device_register(struct device *dev, const char *name, void *drvdata, > > > > return hdev; > > > > +device_unregister: > > + device_unregister(hdev); > > free_hwmon: > > kfree(hwdev); > > ida_remove: -- All the best, Eduardo Valentin