Received: by 2002:a05:6a10:9e8c:0:0:0:0 with SMTP id y12csp261670pxx; Wed, 28 Oct 2020 04:24:12 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzdaKgfRulc4eT8YzQkptIRUmcfP3kXQHNWTYX5JISQ2EEM854AvuhyWFaq5IEQQNLkF3Cd X-Received: by 2002:a50:ff10:: with SMTP id a16mr7124010edu.83.1603884251935; Wed, 28 Oct 2020 04:24:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1603884251; cv=none; d=google.com; s=arc-20160816; b=T69vldU14Bk9ZU7WizZQhKLh4d37iykAVGz7BXa/rTr1bT0asxhpMCFbiX9heDZBGj +AIBI73gGCqxEjf6C73f6Dm7bMw8GB2rTOGrhPc/wC2wqChO+9Y6gCvOt4L4fr9/Gikl YLiJXjhljZ0UmDLSMV72gfb9kw0y0smFi4JOBVG+MQWLzbRSnIocIv6BhJ/P00DCH92v wtXmJqQlNCL0Hx75Kk7Xikyf2je4ND3/nIbihQJ3iMPbLdDkxn0058wYQ+RHzxnWzTYY Q9Hn8/H50kiOrGjm1e96Im61W03o8ywuUHJ6uvIJiCyEszvigZVKd339OwIFFR4zr9KP 4rDA== 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=Wo1nLO9Tx4mmQre5X7mqc0qTV7r4SoawYQhdEVCCuUI=; b=fD4f9woocSSICkkgf+3Npq1RUiD/Y+UH7NL1OsH5l0NbFfOBlH561VYmCcxNjeyWb/ yxQKtbUNiTLUIkkyt5V7Ggi4C30CukLLan6vSAXNoNfqWOcibsjmtO+2JEFvRkrdYsAv 6BZ+p1WywltFWXCBDEpvOMbFkEA2WAi5y0icjhGjqd5g066sWSJIFcp3JMcWDlO3CZxE Ov0SDgpr8uP9VXKBIfwnW1lpipGv0Ki8cUXN5+xb9iUOqlq0YBsAz8pN/4ZErteA9vMl YJiRco3ee3EIHGeNfjeSgoWHZdnjdHtzzzHgF63uWUZfF8xmvIZtQ4TmbVRkUi/7/HWW 8vQg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=nwg+Myc8; 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 q18si2524555edb.466.2020.10.28.04.23.49; Wed, 28 Oct 2020 04:24:11 -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=nwg+Myc8; 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 S1789866AbgJ0PDS (ORCPT + 99 others); Tue, 27 Oct 2020 11:03:18 -0400 Received: from mail.kernel.org ([198.145.29.99]:33662 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1788582AbgJ0PA3 (ORCPT ); Tue, 27 Oct 2020 11:00:29 -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 7EFDE20714; Tue, 27 Oct 2020 15:00:28 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1603810829; bh=X29lZydw6kWSNcMmU6M4f+oKfY11QqVF0S/Svtg6fok=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=nwg+Myc8SwoNJE2Bnbaisygf1RFd3xZvQgsGYRrlmuCO/EEROqDlcEWf+1LhCgA5b GoVE878UsxsUjZgepnt1Flz4bCoWN05h1Hy9CuZWJZUZ2qTI9Oh+AlXB6PhOERkYvi A15XIOzR1NUyZjZIq38FBoI9eUohpEuF0DZHfXbc= 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.8 276/633] nvmem: core: fix possibly memleak when use nvmem_cell_info_to_nvmem_cell() Date: Tue, 27 Oct 2020 14:50:19 +0100 Message-Id: <20201027135535.616771401@linuxfoundation.org> X-Mailer: git-send-email 2.29.1 In-Reply-To: <20201027135522.655719020@linuxfoundation.org> References: <20201027135522.655719020@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 394e75dede725..4aca5b4a87d75 100644 --- a/drivers/nvmem/core.c +++ b/drivers/nvmem/core.c @@ -355,16 +355,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; @@ -376,13 +374,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 * @@ -1436,7 +1451,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; @@ -1466,7 +1481,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