blspec: fix use-after-free of firmware search path
PATCH 1/1 blspec: fix use-after-free of firmware search path
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.
--- common/blspec.c | 4 ++-- common/firmware.c | 4 ++-- include/firmware.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
common/blspec.c +2 -2
@@ -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);
common/firmware.c +2 -2
@@ -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)
include/firmware.h +2 -2
@@ -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 *pareturn -ENOSYS ; } -static inline const char *firmware_get_searchpath (void ) +static inline char *firmware_get_searchpath (void ) {return NULL ; }
Signature · 7 lines
2.33.0 _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox
Sascha Hauer
AppliedQuoted 14 lines · Ahmad Fatoum
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.
Quoted 7 lines
Fixes dfebbb0a5944 ("blspec: Set firmware searchpath")Signed-off-by Ahmad Fatoum <[email protected]>--- common/blspec.c | 4 ++-- common/firmware.c | 4 ++-- include/firmware.h | 4 ++-- 3 files changed, 6 insertions(+), 6 deletions(-)
Applied, thanks
Sascha
Signature · 9 lines
Pengutronix e.K. | | Steuerwalder Str. 21 | http://www.pengutronix.de/ | 31137 Hildesheim, Germany | Phone: +49-5121-206917-0 | Amtsgericht Hildesheim, HRA 2686 | Fax: +49-5121-206917-5555 | _______________________________________________ barebox mailing list [email protected] http://lists.infradead.org/mailman/listinfo/barebox