[PATCH master] blspec: fix use-after-free of firmware search path

Ahmad Fatoum ahmad at a3f.at
Mon Sep 13 01:29:57 PDT 2021


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) */
	firmware_set_searchpath(old_fws);

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

Fixes: dfebbb0a5944 ("blspec: Set firmware searchpath")
Signed-off-by: Ahmad Fatoum <ahmad at a3f.at>
---
 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)
 
 	of_overlay_set_basedir("/");
 	firmware_set_searchpath(old_fws);
+	free(old_fws);
 
 err_out:
 	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 *);
 #ifdef CONFIG_FIRMWARE
 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);
 #else
 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;
 }
-- 
2.33.0




More information about the barebox mailing list