mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] env: avoid leaving dangling pointers in __env_map
@ 2016-03-06 17:22 Alexander Monakov
  2016-03-06 19:41 ` [libc-test][PATCH] add putenv double-free regression test Alexander Monakov
  0 siblings, 1 reply; 3+ messages in thread
From: Alexander Monakov @ 2016-03-06 17:22 UTC (permalink / raw)
  To: musl; +Cc: Alexander Monakov

This is the minimal fix for __putenv leaving a pointer to freed heap
storage in __env_map array, which could later on lead to errors such
as double-free.
---
This was discovered by code inspection after Rich asked me to develop
testcases for another environment-related patch in a recent thread.
There's another known issue due to logic errors in surrounding code
(a memory leak due to putenv never freeing storage allocated by preceding
setenv), but that is planned to be addressed with a patch overhauling the
implementation.

Thanks.
Alexander

 src/env/putenv.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/src/env/putenv.c b/src/env/putenv.c
index 4042869..7153042 100644
--- a/src/env/putenv.c
+++ b/src/env/putenv.c
@@ -30,6 +30,7 @@ int __putenv(char *s, int a)
 				}
 			} else {
 				free(__env_map[j]);
+				__env_map[j] = s;
 			}
 		}
 	}
-- 
2.1.3



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

* [libc-test][PATCH] add putenv double-free regression test
  2016-03-06 17:22 [PATCH] env: avoid leaving dangling pointers in __env_map Alexander Monakov
@ 2016-03-06 19:41 ` Alexander Monakov
  2016-03-06 21:13   ` Szabolcs Nagy
  0 siblings, 1 reply; 3+ messages in thread
From: Alexander Monakov @ 2016-03-06 19:41 UTC (permalink / raw)
  To: musl; +Cc: Alexander Monakov

---
I see the musl patch has been committed, so here's the corresponding libc-test
regression test.

It's pretty bare-bones since it just relies on libc's built-in double-free
detection.

Also it's pretty lazy to use _GNU_SOURCE ftm to get putenv; I'm not sure
what's the expected style in libc-test for such things though.
src/functional/env.c also uses _GNU_SOURCE.

 src/regression/putenv-doublefree.c | 16 ++++++++++++++++
 1 file changed, 16 insertions(+)
 create mode 100644 src/regression/putenv-doublefree.c

diff --git a/src/regression/putenv-doublefree.c b/src/regression/putenv-doublefree.c
new file mode 100644
index 0000000..f3a1f6c
--- /dev/null
+++ b/src/regression/putenv-doublefree.c
@@ -0,0 +1,16 @@
+// commit 9543656cc32fda48fc463f332ee20e91eed2b768 2016-03-06
+// __putenv could be confused into freeing storage that does not belong to the implementation
+#define _GNU_SOURCE
+#include <stdlib.h>
+#include <string.h>
+
+int main(void)
+{
+	setenv("A", "1", 1);
+	setenv("A", "2", 1);
+	char *c = strdup("A=3");
+	putenv(c);
+	setenv("A", "4", 1);
+	free(c);
+	return 0;
+}
-- 
2.1.3



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

* Re: [libc-test][PATCH] add putenv double-free regression test
  2016-03-06 19:41 ` [libc-test][PATCH] add putenv double-free regression test Alexander Monakov
@ 2016-03-06 21:13   ` Szabolcs Nagy
  0 siblings, 0 replies; 3+ messages in thread
From: Szabolcs Nagy @ 2016-03-06 21:13 UTC (permalink / raw)
  To: musl; +Cc: Alexander Monakov

* Alexander Monakov <amonakov@ispras.ru> [2016-03-06 22:41:14 +0300]:
> ---
> I see the musl patch has been committed, so here's the corresponding libc-test
> regression test.
> 
> It's pretty bare-bones since it just relies on libc's built-in double-free
> detection.
> 
> Also it's pretty lazy to use _GNU_SOURCE ftm to get putenv; I'm not sure
> what's the expected style in libc-test for such things though.
> src/functional/env.c also uses _GNU_SOURCE.
> 

thanks, applied.

used _XOPEN_SOURCE that is at least a standard.

>  src/regression/putenv-doublefree.c | 16 ++++++++++++++++
>  1 file changed, 16 insertions(+)
>  create mode 100644 src/regression/putenv-doublefree.c
> 
> diff --git a/src/regression/putenv-doublefree.c b/src/regression/putenv-doublefree.c
> new file mode 100644
> index 0000000..f3a1f6c
> --- /dev/null
> +++ b/src/regression/putenv-doublefree.c
> @@ -0,0 +1,16 @@
> +// commit 9543656cc32fda48fc463f332ee20e91eed2b768 2016-03-06
> +// __putenv could be confused into freeing storage that does not belong to the implementation
> +#define _GNU_SOURCE
> +#include <stdlib.h>
> +#include <string.h>
> +
> +int main(void)
> +{
> +	setenv("A", "1", 1);
> +	setenv("A", "2", 1);
> +	char *c = strdup("A=3");
> +	putenv(c);
> +	setenv("A", "4", 1);
> +	free(c);
> +	return 0;
> +}
> -- 
> 2.1.3


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

end of thread, other threads:[~2016-03-06 21:13 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2016-03-06 17:22 [PATCH] env: avoid leaving dangling pointers in __env_map Alexander Monakov
2016-03-06 19:41 ` [libc-test][PATCH] add putenv double-free regression test Alexander Monakov
2016-03-06 21:13   ` Szabolcs Nagy

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).