mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Ahmad Fatoum <>
Cc: Ahmad Fatoum <>
Subject: [PATCH master] blspec: fix use-after-free of firmware search path
Date: Mon, 13 Sep 2021 10:29:57 +0200	[thread overview]
Message-ID: <> (raw)

firmware_set_searchpath() is used to temporarily extend firmware search
path when parsing boot spec files. It does so by first freeing the
original firmware pointer and then storing a pointer to a copy of the
new search path.

firmware_get_searchpath() returns this pointer without copying meaning
that following sequence causes a use-after-free:

	old_fws = firmware_get_searchpath();
	firmware_set_searchpath(fws); /* calls free(old_fws) */

Fix this by keeping around a copy of the search path.

Fixes: dfebbb0a5944 ("blspec: Set firmware searchpath")
Signed-off-by: Ahmad Fatoum <>
 common/blspec.c    | 4 ++--
 common/firmware.c  | 4 ++--
 include/firmware.h | 4 ++--
 3 files changed, 6 insertions(+), 6 deletions(-)

diff --git a/common/blspec.c b/common/blspec.c
index 6cb1fea9e876..158fd1e9a28c 100644
--- a/common/blspec.c
+++ b/common/blspec.c
@@ -75,8 +75,7 @@ static int blspec_boot(struct bootentry *be, int verbose, int dryrun)
 	const char *abspath, *devicetree, *options, *initrd, *linuximage;
 	const char *overlays;
 	const char *appendroot;
-	const char *old_fws;
-	char *fws;
+	char *old_fws, *fws;
 	struct bootm_data data = {
 		.dryrun = dryrun,
@@ -158,6 +157,7 @@ static int blspec_boot(struct bootentry *be, int verbose, int dryrun)
+	free(old_fws);
 	free((char *)data.oftree_file);
diff --git a/common/firmware.c b/common/firmware.c
index b33acff77ff8..b87d7da38fa7 100644
--- a/common/firmware.c
+++ b/common/firmware.c
@@ -224,9 +224,9 @@ out:
 static char *firmware_path;
-const char *firmware_get_searchpath(void)
+char *firmware_get_searchpath(void)
-	return firmware_path;
+	return strdup(firmware_path);
 void firmware_set_searchpath(const char *path)
diff --git a/include/firmware.h b/include/firmware.h
index 0ffea5284086..2583342230ae 100644
--- a/include/firmware.h
+++ b/include/firmware.h
@@ -31,7 +31,7 @@ struct firmware_mgr *firmwaremgr_find(const char *);
 struct firmware_mgr *firmwaremgr_find_by_node(struct device_node *np);
 int firmwaremgr_load_file(struct firmware_mgr *, const char *path);
-const char *firmware_get_searchpath(void);
+char *firmware_get_searchpath(void);
 void firmware_set_searchpath(const char *path);
 static inline struct firmware_mgr *firmwaremgr_find_by_node(struct device_node *np)
@@ -44,7 +44,7 @@ static inline int firmwaremgr_load_file(struct firmware_mgr *mgr, const char *pa
 	return -ENOSYS;
-static inline const char *firmware_get_searchpath(void)
+static inline char *firmware_get_searchpath(void)
 	return NULL;

barebox mailing list

             reply	other threads:[~2021-09-13  8:32 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-09-13  8:29 Ahmad Fatoum [this message]
2021-10-04 10:46 ` 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:

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

  git send-email \ \ \ \

* 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