mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <a.fatoum@pengutronix.de>
To: barebox@lists.infradead.org
Cc: "Daniel Brát" <danek.brat@gmail.com>,
	"Ahmad Fatoum" <a.fatoum@pengutronix.de>
Subject: [PATCH v3] barebox: Fix excessive loading of FIT images
Date: Fri, 19 May 2023 14:10:28 +0200	[thread overview]
Message-ID: <20230519121028.2475832-1-a.fatoum@pengutronix.de> (raw)

From: Christian Melki <christian.melki@t2data.com>

Barebox doesn't use the FIT image size from the header
when loading FIT images. It bluntly assumes that the FIT image
is equal to the file size. Which would be true if the
FIT image is a file. But if it's situated on a raw device,
then barebox proceeds to load the entire contents of that
raw device, only to conclude that it only needed parts of it.
Fix it.

Cc: Daniel Brát <danek.brat@gmail.com>
Signed-off-by: Christian Melki <christian.melki@t2data.com>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
v3 was here: https://lore.barebox.org/barebox/20220729205441.9512-1-danek.brat@gmail.com/
v2 -> v3:
 - restrict change to bootm_open_fit
 - use cached data in struct image_data
v1 -> v2:
 - use fdt32_to_cpu to read the totalsize from header
---
 common/bootm.c      | 9 ++++++++-
 common/image-fit.c  | 7 ++++---
 include/image-fit.h | 2 +-
 3 files changed, 13 insertions(+), 5 deletions(-)

diff --git a/common/bootm.c b/common/bootm.c
index fb1ed36a26dc..91a6e1688674 100644
--- a/common/bootm.c
+++ b/common/bootm.c
@@ -522,13 +522,20 @@ static int bootm_open_os_uimage(struct image_data *data)
 static int bootm_open_fit(struct image_data *data)
 {
 	struct fit_handle *fit;
+	struct fdt_header *header;
 	static const char *kernel_img = "kernel";
+	size_t flen, hlen;
 	int ret;
 
 	if (!IS_ENABLED(CONFIG_FITIMAGE))
 		return 0;
 
-	fit = fit_open(data->os_file, data->verbose, data->verify);
+	header = (struct fdt_header *)data->os_header;
+	flen = bootm_get_os_size(data);
+	hlen = fdt32_to_cpu(header->totalsize);
+
+	fit = fit_open(data->os_file, data->verbose, data->verify,
+		       min(flen, hlen));
 	if (IS_ERR(fit)) {
 		pr_err("Loading FIT image %s failed with: %pe\n", data->os_file, fit);
 		return PTR_ERR(fit);
diff --git a/common/image-fit.c b/common/image-fit.c
index 3e6e7fbd6d12..9bea62bb34a0 100644
--- a/common/image-fit.c
+++ b/common/image-fit.c
@@ -827,6 +827,7 @@ struct fit_handle *fit_open_buf(const void *buf, size_t size, bool verbose,
  * @filename:	The filename of the FIT image
  * @verbose:	If true, be more verbose
  * @verify:	The verify mode
+ * @max_size:	maximum length to read from file
  *
  * This opens a FIT image found in @filename. The returned handle is used as
  * context for the other FIT functions.
@@ -834,7 +835,7 @@ struct fit_handle *fit_open_buf(const void *buf, size_t size, bool verbose,
  * Return: A handle to a FIT image or a ERR_PTR
  */
 struct fit_handle *fit_open(const char *filename, bool verbose,
-			    enum bootm_verify verify)
+			    enum bootm_verify verify, loff_t max_size)
 {
 	struct fit_handle *handle;
 	int ret;
@@ -845,8 +846,8 @@ struct fit_handle *fit_open(const char *filename, bool verbose,
 	handle->verify = verify;
 
 	ret = read_file_2(filename, &handle->size, &handle->fit_alloc,
-			  FILESIZE_MAX);
-	if (ret) {
+			  max_size);
+	if (ret && ret != -EFBIG) {
 		pr_err("unable to read %s: %s\n", filename, strerror(-ret));
 		return ERR_PTR(ret);
 	}
diff --git a/include/image-fit.h b/include/image-fit.h
index f21545988e16..0b8e94bf4635 100644
--- a/include/image-fit.h
+++ b/include/image-fit.h
@@ -23,7 +23,7 @@ struct fit_handle {
 };
 
 struct fit_handle *fit_open(const char *filename, bool verbose,
-			    enum bootm_verify verify);
+			    enum bootm_verify verify, loff_t max_size);
 struct fit_handle *fit_open_buf(const void *buf, size_t len, bool verbose,
 				enum bootm_verify verify);
 void *fit_open_configuration(struct fit_handle *handle, const char *name);
-- 
2.39.2




             reply	other threads:[~2023-05-19 12:11 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2023-05-19 12:10 Ahmad Fatoum [this message]
2023-05-23  7:29 ` Sascha Hauer

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=20230519121028.2475832-1-a.fatoum@pengutronix.de \
    --to=a.fatoum@pengutronix.de \
    --cc=barebox@lists.infradead.org \
    --cc=danek.brat@gmail.com \
    /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