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: Ahmad Fatoum <a.fatoum@pengutronix.de>
Subject: [PATCH master 1/3] glob: use empty globfree when compiling without CONFIG_GLOB
Date: Fri, 19 Mar 2021 19:26:06 +0100	[thread overview]
Message-ID: <20210319182608.5054-1-a.fatoum@pengutronix.de> (raw)

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


             reply	other threads:[~2021-03-19 18:27 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-03-19 18:26 Ahmad Fatoum [this message]
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

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