From: Christian Marangi <ansuelsmth@gmail.com>
To: Simon Glass <sjg@chromium.org>, Tom Rini <trini@konsulko.com>,
Mattijs Korpershoek <mkorpershoek@kernel.org>,
Marek Vasut <marek.vasut+renesas@mailbox.org>,
Heiko Schocher <hs@nabladev.com>,
Alexey Charkov <alchark@flipper.net>,
Rasmus Villemoes <ravi@prevas.dk>,
"Lucien.Jheng" <lucienzx159@gmail.com>,
"Markus Schneider-Pargmann (TI.com)" <msp@baylibre.com>,
Christian Marangi <ansuelsmth@gmail.com>,
Neil Armstrong <neil.armstrong@linaro.org>,
Daniel Golle <daniel@makrotopia.org>,
Mehmet Fide <mehmet.fide@screeningeagle.com>,
Casey Connolly <casey.connolly@linaro.org>,
Michal Simek <michal.simek@amd.com>,
Aristo Chen <aristo.chen@canonical.com>,
Quentin Schulz <quentin.schulz@cherry.de>,
Johan Jonker <jbx6244@gmail.com>, Anshul Dalal <anshuld@ti.com>,
Marcello Sylvester Bauer <marcello.bauer@9elements.com>,
Mateus Lima Alves <mateuslima.ti@gmail.com>,
Patrice Chotard <patrice.chotard@foss.st.com>,
Neha Malcom Francis <n-francis@ti.com>, Yao Zi <me@ziyao.cc>,
Peng Fan <peng.fan@nxp.com>, Martin Schwan <m.schwan@phytec.de>,
Heinrich Schuchardt <xypron.glpk@gmx.de>,
u-boot@lists.u-boot-project.org
Subject: [PATCH v11 4/8] misc: fs_loader: validate requested offset on reading file
Date: Tue, 27 Oct 2026 13:38:59 +0100 [thread overview]
Message-ID: <20261027123905.839-5-ansuelsmth@gmail.com> (raw)
In-Reply-To: <20261027123905.839-1-ansuelsmth@gmail.com>
Make sure we are not requesting an offset for the requesting file beyond
the size of the file.
If it's such the case, log an error and return -EINVAL.
Code is reworked and split in various subfunction to permit re-preparing
the filesystem after a fs_close (called by fs_size) and reduce patch delta
of upcoming support for reading ONLY the size of the requested firmware.
Dedicated mount function are created to mount relevant filesystem only once
and follow the same if-else logic pattern used by the prepare functions.
Signed-off-by: Christian Marangi <ansuelsmth@gmail.com>
---
drivers/misc/fw_loader/fs_loader.c | 97 ++++++++++++++++++++++++------
1 file changed, 77 insertions(+), 20 deletions(-)
diff --git a/drivers/misc/fw_loader/fs_loader.c b/drivers/misc/fw_loader/fs_loader.c
index b1eb0311dd2b..cc1bbd2f0db9 100644
--- a/drivers/misc/fw_loader/fs_loader.c
+++ b/drivers/misc/fw_loader/fs_loader.c
@@ -49,6 +49,16 @@ static int mount_ubifs(char *mtdpart, char *ubivol)
}
#endif
+static int mount_fs_dev(struct fw_loader_plat *plat)
+{
+ if (plat->phandlepart.phandle)
+ return 0;
+ else if (plat->mtdpart && plat->ubivol)
+ return mount_ubifs(plat->mtdpart, plat->ubivol);
+ else
+ return 0;
+}
+
static int select_fs_dev(struct fw_loader_plat *plat)
{
int ret;
@@ -63,10 +73,6 @@ static int select_fs_dev(struct fw_loader_plat *plat)
ret = fs_set_blk_dev_with_part(desc,
plat->phandlepart.partition);
} else if (plat->mtdpart && plat->ubivol) {
- ret = mount_ubifs(plat->mtdpart, plat->ubivol);
- if (ret)
- return ret;
-
ret = fs_set_blk_dev("ubi", NULL, FS_TYPE_UBIFS);
} else {
debug("Error: unsupported storage device.\n");
@@ -79,18 +85,26 @@ static int select_fs_dev(struct fw_loader_plat *plat)
return ret;
}
-/**
- * fw_get_filesystem_firmware - load firmware into an allocated buffer.
- * @dev: An instance of a driver.
- *
- * Return: Size of total read, negative value when error.
- */
-static int fw_get_filesystem_firmware(struct udevice *dev)
+static int fw_get_filesystem_mount(struct udevice *dev)
+{
+ struct fw_loader_plat *plat = dev_get_uclass_plat(dev);
+ struct fw_loader_env_info info = { };
+
+ generic_fw_loader_env_info(&info);
+
+ if (info.storage_interface && info.dev_part)
+ return 0;
+ else if (info.storage_interface && info.ubi_mtdpart &&
+ info.ubi_volume)
+ return mount_ubifs(info.ubi_mtdpart, info.ubi_volume);
+ else
+ return mount_fs_dev(plat);
+}
+
+static int fw_get_filesystem_prepare(struct udevice *dev)
{
- struct firmware *upriv = dev_get_uclass_priv(dev);
struct fw_loader_plat *plat = dev_get_uclass_plat(dev);
struct fw_loader_env_info info = { };
- loff_t actread = 0;
int ret;
generic_fw_loader_env_info(&info);
@@ -100,10 +114,6 @@ static int fw_get_filesystem_firmware(struct udevice *dev)
info.dev_part, FS_TYPE_ANY);
} else if (info.storage_interface && info.ubi_mtdpart &&
info.ubi_volume) {
- ret = mount_ubifs(info.ubi_mtdpart, info.ubi_volume);
- if (ret)
- return ret;
-
if (!strcmp("ubi", info.storage_interface))
ret = fs_set_blk_dev(info.storage_interface, NULL,
FS_TYPE_UBIFS);
@@ -113,6 +123,55 @@ static int fw_get_filesystem_firmware(struct udevice *dev)
ret = select_fs_dev(plat);
}
+ return ret;
+}
+
+static void fw_get_filesystem_release(struct udevice *dev)
+{
+#ifdef CONFIG_CMD_UBIFS
+ umount_ubifs();
+#endif
+}
+
+static int __fw_get_filesystem_firmware_size(struct udevice *dev,
+ const char *name, loff_t *size)
+{
+ int ret;
+
+ ret = fw_get_filesystem_prepare(dev);
+ if (ret)
+ return ret;
+
+ return fs_size(name, size);
+}
+
+/**
+ * fw_get_filesystem_firmware - load firmware into an allocated buffer.
+ * @dev: An instance of a driver.
+ *
+ * Return: Size of total read, negative value when error.
+ */
+static int fw_get_filesystem_firmware(struct udevice *dev)
+{
+ struct firmware *upriv = dev_get_uclass_priv(dev);
+ loff_t size, actread = 0;
+ int ret;
+
+ ret = fw_get_filesystem_mount(dev);
+ if (ret)
+ goto out;
+
+ ret = __fw_get_filesystem_firmware_size(dev, upriv->name, &size);
+ if (ret)
+ goto out;
+
+ if (upriv->offset > size) {
+ log_err("Invalid offset. Entry too small\n");
+ ret = -EINVAL;
+ goto out;
+ }
+
+ ret = fw_get_filesystem_prepare(dev);
if (ret)
goto out;
@@ -127,9 +186,7 @@ static int fw_get_filesystem_firmware(struct udevice *dev)
}
out:
-#ifdef CONFIG_CMD_UBIFS
- umount_ubifs();
-#endif
+ fw_get_filesystem_release(dev);
return ret;
}
--
2.55.0
next prev parent reply other threads:[~2026-10-27 12:39 UTC|newest]
Thread overview: 13+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-10-27 12:38 [PATCH v11 0/8] misc: fs_loader: reorg and split to FS and FW loader + FIP loader Christian Marangi
2026-10-27 12:38 ` [PATCH v11 1/8] misc: fs_loader: fix ubifs not unmounted on dev_get_priv error Christian Marangi
2026-10-27 12:38 ` [PATCH v11 2/8] misc: fs_loader: reorganize and split to FS loader and FW UCLASS Christian Marangi
2026-10-27 12:38 ` [PATCH v11 3/8] misc: fw_loader: implement generic get_fw_loader_from_node() Christian Marangi
2026-10-27 12:38 ` Christian Marangi [this message]
2026-10-27 14:36 ` [PATCH v11 4/8] misc: fs_loader: validate requested offset on reading file Simon Glass
2026-10-27 14:41 ` Christian Marangi (Ansuel)
2026-10-27 12:39 ` [PATCH v11 5/8] misc: fw_loader: implement request_firmware_size() OP Christian Marangi
2026-10-27 12:39 ` [PATCH v11 6/8] doc: dtbinding: Update documentation for Generic Firmware loader Christian Marangi
2026-10-27 12:39 ` [PATCH v11 7/8] misc: fw_loader: introduce FIP loader driver Christian Marangi
2026-10-27 12:39 ` [PATCH v11 8/8] test: dm: Add test for FW Loader feature Christian Marangi
2026-10-27 14:36 ` Simon Glass
2026-10-27 14:36 ` [PATCH v11 0/8] misc: fs_loader: reorg and split to FS and FW loader + FIP loader Simon Glass
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=20261027123905.839-5-ansuelsmth@gmail.com \
--to=ansuelsmth@gmail.com \
--cc=alchark@flipper.net \
--cc=anshuld@ti.com \
--cc=aristo.chen@canonical.com \
--cc=casey.connolly@linaro.org \
--cc=daniel@makrotopia.org \
--cc=hs@nabladev.com \
--cc=jbx6244@gmail.com \
--cc=lucienzx159@gmail.com \
--cc=m.schwan@phytec.de \
--cc=marcello.bauer@9elements.com \
--cc=marek.vasut+renesas@mailbox.org \
--cc=mateuslima.ti@gmail.com \
--cc=me@ziyao.cc \
--cc=mehmet.fide@screeningeagle.com \
--cc=michal.simek@amd.com \
--cc=mkorpershoek@kernel.org \
--cc=msp@baylibre.com \
--cc=n-francis@ti.com \
--cc=neil.armstrong@linaro.org \
--cc=patrice.chotard@foss.st.com \
--cc=peng.fan@nxp.com \
--cc=quentin.schulz@cherry.de \
--cc=ravi@prevas.dk \
--cc=sjg@chromium.org \
--cc=trini@konsulko.com \
--cc=u-boot@lists.u-boot-project.org \
--cc=xypron.glpk@gmx.de \
/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