Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp523414pxx; Wed, 28 Oct 2020 10:14:59 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxoI7VkYjTLXxtIKpYr/7OxTz9mkgYuK1RiIvshsaC+l/yaQVLvTg4cY974gV8QEh8Qp9lF X-Received: by 2002:a17:906:444:: with SMTP id e4mr107828eja.218.1603905298755; Wed, 28 Oct 2020 10:14:58 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603905298; cv=none; d=google.com; s=arc-20160816; b=iDBrAOeCTEctcVWauRmm3wcYvy8AD/TNg1MNRbIcnX685W1snBBRR1Xb0Q3HRWmd+0 /fbdsp5b5hehzAtLHAktRW6AoZFOdFKjBh+PMILmRQToU/xpbool0d2FpMoBN+dB84oi Ksv96ievomXYOQ364AXVbyUXgNerRXHlh5CpgwlrkR5Zvid0ZbJroGn1Sr2F0YZDVX3P 4zjxE6XTYOAoMYW98D0gM3tvJkCB6pkLL9IIMaj0m2Yz6A0hNAnugcKxpgd67xx8JdsU Ed3GsuGpUfZ1Y4zReamoJ5R/9rtuVZT54LsBpEkwixRnsrYa+YjdNkZSLIlBRpAYc0p3 0kvg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=t1I9jX+FUaR7hSqYoJ2Enc4gHM7bpRaR/vs0zIDOAtg=; b=B8kO6V3rQsnqBfjPb8GB7rjN5qawwxbrx2vcSPQ3Kj6kF9tYNBVyi7G/Ua/H3lzyd+ 23yPHzxNExbKUm+BOWCYZ9fvegARSN/TcV+CqzqNaWmDxJv+j7zOsp2yLAE61aoP6Cz/ YGZs0BRRZlJcunqF9TkxkFaf7p7AwNcNOMS4tufVycDMDzRj1qEWaBu9BCWZSLPZi07Q wB+3mnGder4T29ZxSleS1GQXmRyWL58ehi7oM9cNOzI7Q5PSlHaGl9usBVyHnZbWA5/C X7uauySCDEcwK7q8K0hGSYkh3OdwppK17xkRDpUY+PQpmKZUyylha8oxGGzKGmuN32Ei t26g== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KA0wqJh4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id gb38si1650347ejc.555.2020.10.28.10.14.35; Wed, 28 Oct 2020 10:14:58 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=KA0wqJh4; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1820620AbgJ0Rgn (ORCPT + 99 others); Tue, 27 Oct 2020 13:36:43 -0400 Received: from mail.kernel.org ([198.145.29.99]:34918 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1760613AbgJ0OgH (ORCPT ); Tue, 27 Oct 2020 10:36:07 -0400 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 707E1207BB; Tue, 27 Oct 2020 14:36:06 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603809367; bh=GW3xx2deSf781ZDoKaAayCq/tBd+PxR3Kl1erP6he/g=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=KA0wqJh4fmO8bFtcJSYfU5LlvfM+xYomXPouxMT7VmzekOIAwz0esp2FMVR64s0Yl 6ZfvoJvFS38i6EdEznfcfQg3QklXHU1wFW9XzQ6T584Mx2YG2iaAYv1mLOeHxH8amJ JMFbDBEMNnpTVvnRJYkPTLd8eQAkxNepq8EJ/m/M= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Srinivas Kandagatla , Vadym Kochan , Sasha Levin Subject: [PATCH 5.4 170/408] nvmem: core: fix possibly memleak when use nvmem_cell_info_to_nvmem_cell() Date: Tue, 27 Oct 2020 14:51:48 +0100 Message-Id: <20201027135502.985783875@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027135455.027547757@linuxfoundation.org> References: <20201027135455.027547757@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Vadym Kochan [ Upstream commit fc9eec4d643597cf4cb2fef17d48110e677610da ] Fix missing 'kfree_const(cell->name)' when call to nvmem_cell_info_to_nvmem_cell() in several places: * after nvmem_cell_info_to_nvmem_cell() failed during nvmem_add_cells() * during nvmem_device_cell_{read,write} when cell->name is kstrdup'ed() without calling kfree_const() at the end, but really there is no reason to do that 'dup, because the cell instance is allocated on the stack for some short period to be read/write without exposing it to the caller. So the new nvmem_cell_info_to_nvmem_cell_nodup() helper is introduced which is used to convert cell_info -> cell without name duplication as a lighweight version of nvmem_cell_info_to_nvmem_cell(). Fixes: e2a5402ec7c6 ("nvmem: Add nvmem_device based consumer apis.") Reviewed-by: Srinivas Kandagatla Acked-by: Srinivas Kandagatla Signed-off-by: Vadym Kochan Link: https://lore.kernel.org/r/20200923204456.14032-1-vadym.kochan@plvision.eu Signed-off-by: Greg Kroah-Hartman Signed-off-by: Sasha Levin --- drivers/nvmem/core.c | 33 ++++++++++++++++++++++++--------- 1 file changed, 24 insertions(+), 9 deletions(-) diff --git a/drivers/nvmem/core.c b/drivers/nvmem/core.c index 960542dea5adb..84f4078216a36 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -130,16 +130,14 @@ static void nvmem_cell_add(struct nvmem_cell *cell) blocking_notifier_call_chain(&nvmem_notifier, NVMEM_CELL_ADD, cell); } -static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, - const struct nvmem_cell_info *info, - struct nvmem_cell *cell) +static int nvmem_cell_info_to_nvmem_cell_nodup(struct nvmem_device *nvmem, + const struct nvmem_cell_info *info, + struct nvmem_cell *cell) { cell->nvmem = nvmem; cell->offset = info->offset; cell->bytes = info->bytes; - cell->name = kstrdup_const(info->name, GFP_KERNEL); - if (!cell->name) - return -ENOMEM; + cell->name = info->name; cell->bit_offset = info->bit_offset; cell->nbits = info->nbits; @@ -151,13 +149,30 @@ static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, if (!IS_ALIGNED(cell->offset, nvmem->stride)) { dev_err(&nvmem->dev, "cell %s unaligned to nvmem stride %d\n", - cell->name, nvmem->stride); + cell->name ?: "", nvmem->stride); return -EINVAL; } return 0; } +static int nvmem_cell_info_to_nvmem_cell(struct nvmem_device *nvmem, + const struct nvmem_cell_info *info, + struct nvmem_cell *cell) +{ + int err; + + err = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, cell); + if (err) + return err; + + cell->name = kstrdup_const(info->name, GFP_KERNEL); + if (!cell->name) + return -ENOMEM; + + return 0; +} + /** * nvmem_add_cells() - Add cell information to an nvmem device * @@ -1174,7 +1189,7 @@ ssize_t nvmem_device_cell_read(struct nvmem_device *nvmem, if (!nvmem) return -EINVAL; - rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); + rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell); if (rc) return rc; @@ -1204,7 +1219,7 @@ int nvmem_device_cell_write(struct nvmem_device *nvmem, if (!nvmem) return -EINVAL; - rc = nvmem_cell_info_to_nvmem_cell(nvmem, info, &cell); + rc = nvmem_cell_info_to_nvmem_cell_nodup(nvmem, info, &cell); if (rc) return rc; -- 2.25.1