Received: by 2002:a25:ab43:0:0:0:0:0 with SMTP id u61csp7234742ybi; Wed, 5 Jun 2019 13:41:18 -0700 (PDT) X-Google-Smtp-Source: APXvYqwLk2V68LzaIwB6cKsOAgHZc670AvZSyk0EHgv0bhv7FspCb/CaN7Q33bqfXpda8Vs6tyWr X-Received: by 2002:aa7:90ce:: with SMTP id k14mr49289256pfk.239.1559767278418; Wed, 05 Jun 2019 13:41:18 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1559767278; cv=none; d=google.com; s=arc-20160816; b=xlNEkTLqwvTpwUACoPZUi6PL3TBdsnvY8ARFfsStI/LAn6ii/fmj40jznuF+zF+R7n 4Gaf5El7osuI+GSFM4i/ZZRADDzT5h2UpYimyYYcKOeySR0gGF2Y+vgrWqnVOX1UePvu Ktr8zEshOVfb275exN0hqAiiYjDVMUsSu2cydglbz/fJJ96PBOnb+SKut9qGmAONlMNO P5mFK/lzq/jsztTHvVou7869+6rpQ6gOwVcAfW9uXp2RInedZwHxsg7Lag6QaEqKfqpa Tml15SHvXcqWlEI8leKM+P8WYyih+pI3uTagTqzzELAnaq2w/vHKPRo9j74ffIS/C5pU A3xw== 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=Kyi91WMR4AvwDRIy5DxgM0o6bNXmSrWa07y0x+D4CAE=; b=qIu4BhuxL3uuk9MjkFwHqpdHesUjbrfZLqEXZ0YqW9hgNTPBuIn3HPCj8P2fUk+lmz GTYp4Qwmvg1+3oe2BhcdgrBP81Af1v1JYWmPRQWrNo53TpD8LNJ5lKf83OeUX1H1t4pq pZPikaWXTrLAThCYeEQ+u7TUTHzTTEfHp25Ka1ZbBwXNy8mfohCHDQNxhzi0DCZ7Z2uY S1M2k2tIu4C9fD/v4O6Wl1fkirGw28kGX+dHdeH6cBiHfD7yxtcPVr/cUKVpYzOpXsHT DktSbfjn1ChpgmJ2Exu1S6WUGs2Y8bAH4q2bbJEyi/ycMyvUqt/Gy0hxjstGzctvUH+u 3U9g== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@gmail.com header.s=20161025 header.b="H2rPos/u"; 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 Return-Path: Received: from vger.kernel.org (vger.kernel.org. [209.132.180.67]) by mx.google.com with ESMTP id a16si20593829pgl.242.2019.06.05.13.41.00; Wed, 05 Jun 2019 13:41:18 -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=fail header.i=@gmail.com header.s=20161025 header.b="H2rPos/u"; 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726642AbfFEUik (ORCPT + 99 others); Wed, 5 Jun 2019 16:38:40 -0400 Received: from mail-pg1-f196.google.com ([209.85.215.196]:45726 "EHLO mail-pg1-f196.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726477AbfFEUik (ORCPT ); Wed, 5 Jun 2019 16:38:40 -0400 Received: by mail-pg1-f196.google.com with SMTP id w34so13021786pga.12; Wed, 05 Jun 2019 13:38:39 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=sender:date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to:user-agent; bh=Kyi91WMR4AvwDRIy5DxgM0o6bNXmSrWa07y0x+D4CAE=; b=H2rPos/u564e/RMxlV0kDV5IpWYrSjoJZs3LwzOA5spNEigtkKwhdm+sVNo+rle9on iHahXa4VDiKz3mdzMYnqMj5rDggFiHfMUtBPEuqJmHKl3S4cWVl5An67zYqOeuuZhJbd n548Nb8/qTkZKWXMqIRLgC015pk7VKJa4LnukLtgyNDR+UBRpSLsqDQ5ofuZkfyFoair n+fs2oAiOYcv3+yh98+B3sVNiWC8lhqBB/q8R3yJEiJGsD5jY5u3Ywi/UeP7EkNgMwN5 QwfYU1xxIU8OaS8tNjtTckhKY1CO3+RpcbwwY4UAlSdJCxlCNEZZuDCPhVYYeI4zlP6N +xpQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:sender:date:from:to:cc:subject:message-id :references:mime-version:content-disposition:in-reply-to:user-agent; bh=Kyi91WMR4AvwDRIy5DxgM0o6bNXmSrWa07y0x+D4CAE=; b=Ww5y2q1GIn6V0PHlFv6WVJfX4ixqpfJ4K5d5c6VGnzprUuph7wdUu77StcvkS0Ixer VZMwWdHkg7RJ+bgH6Me42RBUUbl5OueNjBmk5t0r7ytSyI6k1kCGnt9N2F5Dw9CJ0oyA FwvG36M9Nmk/EvIqNLjeaJ0N0ReEhH3b/wECZbOD/8q1VVVuY/wYxoZApviIqJ/F66jV Ohcsr95Es2dmbBTmyRuOLRFyiz/xgUSE7dFuLlrtOhA41kn9rF+NHnG6k4jPZL88fBtg PXYbWmqLv/+SfWWbgr6K5mtuxoYRauJGHE3YVB4XbEIRfZ+w1Q7cVGGWCtyNEBqdBvIH /1ww== X-Gm-Message-State: APjAAAWVmUhOEPZCGH7T5IwbafE8uy2zF3vjLBshIy0ozVzxUXfGY67D tZ0/fiq/INFtB0dbAr6tm8s= X-Received: by 2002:a63:91c4:: with SMTP id l187mr771892pge.95.1559767119687; Wed, 05 Jun 2019 13:38:39 -0700 (PDT) Received: from localhost ([2600:1700:e321:62f0:329c:23ff:fee3:9d7c]) by smtp.gmail.com with ESMTPSA id n13sm20788638pff.59.2019.06.05.13.38.38 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 05 Jun 2019 13:38:38 -0700 (PDT) Date: Wed, 5 Jun 2019 13:38:38 -0700 From: Guenter Roeck To: Eduardo Valentin Cc: Jean Delvare , linux-hwmon@vger.kernel.org, linux-kernel@vger.kernel.org Subject: Re: [PATCHv2 2/2] hwmon: core: fix potential memory leak in *hwmon_device_register* Message-ID: <20190605203837.GA30238@roeck-us.net> References: <20190530025605.3698-1-eduval@amazon.com> <20190530025605.3698-3-eduval@amazon.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20190530025605.3698-3-eduval@amazon.com> 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, 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()"). It may make sense to add a respective comment to the code, though. 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: