mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH master 1/3] glob: use empty globfree when compiling without CONFIG_GLOB
@ 2021-03-19 18:26 Ahmad Fatoum
  2021-03-19 18:26 ` [PATCH master 2/3] common: boot: select dependency CONFIG_GLOB Ahmad Fatoum
                   ` (2 more replies)
  0 siblings, 3 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2021-03-19 18:26 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

We already return an error code unconditionally when building with
!CONFIG_GLOB. We need to do the same for globfree. Otherwise,
we run risk of corrupting memory.

This issue exists since the code was first added, but it became
more acute with 90cde3b9ff46 ("startup: Execute init scripts in
alphabetical order"), which added a globfree into the shell init.
Configuration without CONFIG_GLOB would from then on experience
memory corruption during startup.

Reported-by: Antony Pavlov <antonynpavlov@gmail.com>
Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 include/glob.h |  6 +++++-
 lib/glob.c     | 24 ++++++++++++------------
 2 files changed, 17 insertions(+), 13 deletions(-)

diff --git a/include/glob.h b/include/glob.h
index 5f532e6652c3..ec0ac66f8765 100644
--- a/include/glob.h
+++ b/include/glob.h
@@ -177,6 +177,7 @@ extern int glob __P ((__const char *__restrict __pattern, int __flags,
 		      int (*__errfunc) (__const char *, int),
 		      glob_t *__restrict __pglob));
 
+extern void globfree __P ((glob_t *__pglob));
 #else
 static inline int glob __P ((__const char *__restrict __pattern, int __flags,
 		      int (*__errfunc) (__const char *, int),
@@ -184,9 +185,12 @@ static inline int glob __P ((__const char *__restrict __pattern, int __flags,
 {
 	return GLOB_ABORTED;
 }
+
+static inline void globfree __P ((glob_t *__pglob))
+{
+}
 #endif
 /* Free storage allocated in PGLOB by a previous `glob' call.  */
-extern void globfree __P ((glob_t *__pglob));
 #else
 extern int glob __P ((__const char *__restrict __pattern, int __flags,
 		      int (*__errfunc) (__const char *, int),
diff --git a/lib/glob.c b/lib/glob.c
index 32f7afdce81b..8523bad9a7ef 100644
--- a/lib/glob.c
+++ b/lib/glob.c
@@ -406,6 +406,18 @@ static int glob_in_dir(const char *pattern, const char *directory,
 	}
 	return nfound == 0 ? GLOB_NOMATCH : 0;
 }
+
+/* Free storage allocated in PGLOB by a previous `glob' call.  */
+void globfree(glob_t *pglob)
+{
+	if (pglob->gl_pathv != NULL) {
+		int i = pglob->gl_flags & GLOB_DOOFFS ? pglob->gl_offs : 0;
+		for (; i < pglob->gl_pathc; ++i)
+			if (pglob->gl_pathv[i] != NULL)
+				free((__ptr_t) pglob->gl_pathv[i]);
+		free((__ptr_t) pglob->gl_pathv);
+	}
+}
 #endif /* CONFIG_GLOB */
 
 #ifdef CONFIG_FAKE_GLOB
@@ -443,15 +455,3 @@ glob_t *pglob;
 	return 0;
 }
 #endif /* CONFIG_FAKE_GLOB */
-
-/* Free storage allocated in PGLOB by a previous `glob' call.  */
-void globfree(glob_t *pglob)
-{
-	if (pglob->gl_pathv != NULL) {
-		int i = pglob->gl_flags & GLOB_DOOFFS ? pglob->gl_offs : 0;
-		for (; i < pglob->gl_pathc; ++i)
-			if (pglob->gl_pathv[i] != NULL)
-				free((__ptr_t) pglob->gl_pathv[i]);
-		free((__ptr_t) pglob->gl_pathv);
-	}
-}
-- 
2.29.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2021-03-22  4:48 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-19 18:26 [PATCH master 1/3] glob: use empty globfree when compiling without CONFIG_GLOB Ahmad Fatoum
2021-03-19 18:26 ` [PATCH master 2/3] common: boot: select dependency CONFIG_GLOB Ahmad Fatoum
2021-03-19 18:26 ` [PATCH master 3/3] common: shell: select dependency GLOB for SHELL_SIMPLE Ahmad Fatoum
2021-03-22  4:47 ` [PATCH master 1/3] glob: use empty globfree when compiling without CONFIG_GLOB Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox