linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Luis Chamberlain <mcgrof@kernel.org>,
	Greg Kroah-Hartman <gregkh@linuxfoundation.org>,
	Sasha Levin <sashal@kernel.org>
Subject: [PATCH AUTOSEL 4.19 11/21] firmware_loader: fix pre-allocated buf built-in firmware use
Date: Tue,  9 Nov 2021 17:23:00 -0500	[thread overview]
Message-ID: <20211109222311.1235686-11-sashal@kernel.org> (raw)
In-Reply-To: <20211109222311.1235686-1-sashal@kernel.org>

From: Luis Chamberlain <mcgrof@kernel.org>

[ Upstream commit f7a07f7b96033df7709042ff38e998720a3f7119 ]

The firmware_loader can be used with a pre-allocated buffer
through the use of the API calls:

  o request_firmware_into_buf()
  o request_partial_firmware_into_buf()

If the firmware was built-in and present, our current check
for if the built-in firmware fits into the pre-allocated buffer
does not return any errors, and we proceed to tell the caller
that everything worked fine. It's a lie and no firmware would
end up being copied into the pre-allocated buffer. So if the
caller trust the result it may end up writing a bunch of 0's
to a device!

Fix this by making the function that checks for the pre-allocated
buffer return non-void. Since the typical use case is when no
pre-allocated buffer is provided make this return successfully
for that case. If the built-in firmware does *not* fit into the
pre-allocated buffer size return a failure as we should have
been doing before.

I'm not aware of users of the built-in firmware using the API
calls with a pre-allocated buffer, as such I doubt this fixes
any real life issue. But you never know... perhaps some oddball
private tree might use it.

In so far as upstream is concerned this just fixes our code for
correctness.

Signed-off-by: Luis Chamberlain <mcgrof@kernel.org>
Link: https://lore.kernel.org/r/20210917182226.3532898-2-mcgrof@kernel.org
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 drivers/base/firmware_loader/main.c | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/drivers/base/firmware_loader/main.c b/drivers/base/firmware_loader/main.c
index 24410a0d6df07..cfa5e598a0dc8 100644
--- a/drivers/base/firmware_loader/main.c
+++ b/drivers/base/firmware_loader/main.c
@@ -97,12 +97,15 @@ static struct firmware_cache fw_cache;
 extern struct builtin_fw __start_builtin_fw[];
 extern struct builtin_fw __end_builtin_fw[];
 
-static void fw_copy_to_prealloc_buf(struct firmware *fw,
+static bool fw_copy_to_prealloc_buf(struct firmware *fw,
 				    void *buf, size_t size)
 {
-	if (!buf || size < fw->size)
-		return;
+	if (!buf)
+		return true;
+	if (size < fw->size)
+		return false;
 	memcpy(buf, fw->data, fw->size);
+	return true;
 }
 
 static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
@@ -114,9 +117,7 @@ static bool fw_get_builtin_firmware(struct firmware *fw, const char *name,
 		if (strcmp(name, b_fw->name) == 0) {
 			fw->size = b_fw->size;
 			fw->data = b_fw->data;
-			fw_copy_to_prealloc_buf(fw, buf, size);
-
-			return true;
+			return fw_copy_to_prealloc_buf(fw, buf, size);
 		}
 	}
 
-- 
2.33.0


  parent reply	other threads:[~2021-11-09 22:39 UTC|newest]

Thread overview: 21+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-11-09 22:22 [PATCH AUTOSEL 4.19 01/21] arm64: zynqmp: Do not duplicate flash partition label property Sasha Levin
2021-11-09 22:22 ` [PATCH AUTOSEL 4.19 02/21] arm64: zynqmp: Fix serial compatible string Sasha Levin
2021-11-09 22:22 ` [PATCH AUTOSEL 4.19 03/21] scsi: lpfc: Fix list_add() corruption in lpfc_drain_txq() Sasha Levin
2021-11-09 22:22 ` [PATCH AUTOSEL 4.19 04/21] arm64: dts: hisilicon: fix arm,sp805 compatible string Sasha Levin
2021-11-09 22:22 ` [PATCH AUTOSEL 4.19 05/21] arm64: dts: rockchip: add Coresight debug range for RK3399 Sasha Levin
2021-11-09 22:22 ` [PATCH AUTOSEL 4.19 06/21] usb: musb: tusb6010: check return value after calling platform_get_resource() Sasha Levin
2021-11-09 22:22 ` [PATCH AUTOSEL 4.19 07/21] usb: typec: tipd: Remove WARN_ON in tps6598x_block_read Sasha Levin
2021-11-09 22:22 ` [PATCH AUTOSEL 4.19 08/21] arm64: dts: freescale: fix arm,sp805 compatible string Sasha Levin
2021-11-09 22:22 ` [PATCH AUTOSEL 4.19 09/21] ASoC: nau8824: Add DMI quirk mechanism for active-high jack-detect Sasha Levin
2021-11-09 22:22 ` [PATCH AUTOSEL 4.19 10/21] scsi: advansys: Fix kernel pointer leak Sasha Levin
2021-11-09 22:23 ` Sasha Levin [this message]
2021-11-09 22:23 ` [PATCH AUTOSEL 4.19 12/21] ARM: dts: omap: fix gpmc,mux-add-data type Sasha Levin
2021-11-09 22:23 ` [PATCH AUTOSEL 4.19 13/21] usb: host: ohci-tmio: check return value after calling platform_get_resource() Sasha Levin
2021-11-09 22:23 ` [PATCH AUTOSEL 4.19 14/21] ALSA: ISA: not for M68K Sasha Levin
2021-11-09 22:23 ` [PATCH AUTOSEL 4.19 15/21] tty: tty_buffer: Fix the softlockup issue in flush_to_ldisc Sasha Levin
2021-11-09 22:23 ` [PATCH AUTOSEL 4.19 16/21] MIPS: sni: Fix the build Sasha Levin
2021-11-09 22:23 ` [PATCH AUTOSEL 4.19 17/21] scsi: target: Fix ordered tag handling Sasha Levin
2021-11-09 22:23 ` [PATCH AUTOSEL 4.19 18/21] scsi: target: Fix alua_tg_pt_gps_count tracking Sasha Levin
2021-11-09 22:23 ` [PATCH AUTOSEL 4.19 19/21] powerpc/5200: dts: fix memory node unit name Sasha Levin
2021-11-09 22:23 ` [PATCH AUTOSEL 4.19 20/21] ALSA: gus: fix null pointer dereference on pointer block Sasha Levin
2021-11-09 22:23 ` [PATCH AUTOSEL 4.19 21/21] powerpc/dcr: Use cmplwi instead of 3-argument cmpli Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20211109222311.1235686-11-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=gregkh@linuxfoundation.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=mcgrof@kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).