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

* [PATCH master 2/3] common: boot: select dependency CONFIG_GLOB
  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 ` 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
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2021-03-19 18:26 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

bootscript_scan_path calls glob to collect the bootscripts.  This won't
work without CONFIG_GLOB, so select it to ensure it's available.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/Kconfig | 1 +
 1 file changed, 1 insertion(+)

diff --git a/common/Kconfig b/common/Kconfig
index 37204fb40a44..8abd47a7569a 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -121,6 +121,7 @@ config USBGADGET_START
 	default y
 
 config BOOT
+	select GLOB
 	bool
 
 config FASTBOOT_BASE
-- 
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

* [PATCH master 3/3] common: shell: select dependency GLOB for SHELL_SIMPLE
  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 ` Ahmad Fatoum
  2021-03-22  4:47 ` [PATCH master 1/3] glob: use empty globfree when compiling without CONFIG_GLOB Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Ahmad Fatoum @ 2021-03-19 18:26 UTC (permalink / raw)
  To: barebox; +Cc: Ahmad Fatoum

Both CONFIG_SHELL_HUSH and CONFIG_SHELL_SIMPLE will call run_init, which
already uses glob to collect the init scripts to run. select GLOB to
make sure that these scripts are executed. If there is a use case for
a shell configuration without glob support to save on size, this should
be realized by a new Kconfig symbol to disable the init scripts.

Signed-off-by: Ahmad Fatoum <a.fatoum@pengutronix.de>
---
 common/Kconfig | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/common/Kconfig b/common/Kconfig
index 8abd47a7569a..342817bbcbb4 100644
--- a/common/Kconfig
+++ b/common/Kconfig
@@ -413,6 +413,7 @@ choice
 		select PARAMETER
 		select BINFMT
 		select STDDEV
+		select GLOB
 		help
 		  Enable hush support. This is the most advanced shell available
 		  for barebox.
@@ -424,6 +425,7 @@ choice
 		select PARAMETER
 		select STDDEV
 		select CMD_SETENV
+		select GLOB
 		help
 		  simple shell. No if/then, no return values from commands, no loops
 
-- 
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

* Re: [PATCH master 1/3] glob: use empty globfree when compiling without CONFIG_GLOB
  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 ` Sascha Hauer
  2 siblings, 0 replies; 4+ messages in thread
From: Sascha Hauer @ 2021-03-22  4:47 UTC (permalink / raw)
  To: Ahmad Fatoum; +Cc: barebox

On Fri, Mar 19, 2021 at 07:26:06PM +0100, Ahmad Fatoum wrote:
> 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>
> ---

Applied, thanks

Sascha

>  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
> 

-- 
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
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