From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1752511AbdLMJ5C (ORCPT ); Wed, 13 Dec 2017 04:57:02 -0500 Received: from mail-ot0-f193.google.com ([74.125.82.193]:34187 "EHLO mail-ot0-f193.google.com" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1751523AbdLMJ47 (ORCPT ); Wed, 13 Dec 2017 04:56:59 -0500 X-Google-Smtp-Source: ACJfBoszyC0qN++PANzlcd5bbC5/M2M+StzujPbUSgBzaUGn+jQA798iahVGze2P+rQpprWdKOaYY9N4GB/EzJSNK0E= MIME-Version: 1.0 In-Reply-To: References: <20171213092812.26472-1-brgl@bgdev.pl> <20171213092812.26472-4-brgl@bgdev.pl> From: Bartosz Golaszewski Date: Wed, 13 Dec 2017 10:56:58 +0100 Message-ID: Subject: Re: [PATCH 3/3] regmap: duplicate the name string stored in regmap To: Lars-Peter Clausen Cc: Mark Brown , Greg Kroah-Hartman , Linux Kernel Mailing List , Andy Shevchenko Content-Type: text/plain; charset="UTF-8" Sender: linux-kernel-owner@vger.kernel.org List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 2017-12-13 10:32 GMT+01:00 Lars-Peter Clausen : > On 12/13/2017 10:28 AM, Bartosz Golaszewski wrote: >> Currently we just copy over the pointer passed to regmap_init() in >> the regmap config struct. To be on the safe side: duplicate the string >> so that if an unaware user passes an address to a stack-allocated >> buffer, we won't crash. >> >> Signed-off-by: Bartosz Golaszewski >> --- >> drivers/base/regmap/regmap.c | 10 +++++++++- >> 1 file changed, 9 insertions(+), 1 deletion(-) >> >> diff --git a/drivers/base/regmap/regmap.c b/drivers/base/regmap/regmap.c >> index a2a02ce58824..3952e5d7638a 100644 >> --- a/drivers/base/regmap/regmap.c >> +++ b/drivers/base/regmap/regmap.c >> @@ -672,6 +672,14 @@ struct regmap *__regmap_init(struct device *dev, >> goto err; >> } >> >> + if (config->name) { >> + map->name = kstrdup(config->name, GFP_KERNEL); > > To get the best of both worlds: kstrdup_const(), this will not make a copy > when it is in a read-only section (like most strings will be). Needs to be > matched with kfree_const(). > Nice! Another useful kernel interface I wasn't aware of. I'll wait some more for any potential comments from others and include that in v2. Thanks, Bartosz >> + if (!map->name) { >> + ret = -ENOMEM; >> + goto err_map; >> + } >> + } >> + >> if (config->disable_locking) { >> map->locking_disabled = true; >> map->lock = map->unlock = regmap_lock_unlock_none; >> @@ -763,7 +771,6 @@ struct regmap *__regmap_init(struct device *dev, >> map->volatile_reg = config->volatile_reg; >> map->precious_reg = config->precious_reg; >> map->cache_type = config->cache_type; >> - map->name = config->name; >> >> spin_lock_init(&map->async_lock); >> INIT_LIST_HEAD(&map->async_list); >> @@ -1308,6 +1315,7 @@ void regmap_exit(struct regmap *map) >> } >> if (map->hwlock) >> hwspin_lock_free(map->hwlock); >> + kfree(map->name); >> kfree(map); >> } >> EXPORT_SYMBOL_GPL(regmap_exit); >> >