mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] always reset DST rules during tzset
@ 2018-09-14 22:46 Benjamin Peterson
  2018-09-15  1:16 ` Rich Felker
  0 siblings, 1 reply; 4+ messages in thread
From: Benjamin Peterson @ 2018-09-14 22:46 UTC (permalink / raw)
  To: musl

do_tzset() did't always reset the DST transition rules r0 and r1. That means the
rules from older TZ settings could leak into newer ones.

The following program demonstrates this bug. It should print out the same
timezone twice but doesn't due to the leaky state.

int main() {
	time_t t = 0;
	struct tm p;
	setenv("TZ", "STD-1DST", 1);
	localtime_r(&t, &p);
	printf("%s\n", p.tm_zone);
	setenv("TZ", "STD-1DST,M3.2.0,M11.1.0", 1);
	tzset();
	setenv("TZ", "STD-1DST", 1);
	localtime_r(&t, &p);
	printf("%s\n", p.tm_zone);
	return 0;
}
---
 src/time/__tz.c | 4 +++-
 1 file changed, 3 insertions(+), 1 deletion(-)

diff --git a/src/time/__tz.c b/src/time/__tz.c
index 51e66514..3ccfdd5d 100644
--- a/src/time/__tz.c
+++ b/src/time/__tz.c
@@ -130,6 +130,9 @@ static void do_tzset()
 
 	if (old_tz && !strcmp(s, old_tz)) return;
 
+	memset(r0, 0, sizeof r0);
+	memset(r1, 0, sizeof r1);
+
 	if (zi) __munmap((void *)zi, map_size);
 
 	/* Cache the old value of TZ to check if it has changed. Avoid
@@ -194,7 +197,6 @@ static void do_tzset()
 			const unsigned char *p;
 			__tzname[0] = __tzname[1] = 0;
 			__daylight = __timezone = dst_off = 0;
-			for (i=0; i<5; i++) r0[i] = r1[i] = 0;
 			for (p=types; p<abbrevs; p+=6) {
 				if (!p[4] && !__tzname[0]) {
 					__tzname[0] = (char *)abbrevs + p[5];
-- 
2.17.1



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

* Re: [PATCH] always reset DST rules during tzset
  2018-09-14 22:46 [PATCH] always reset DST rules during tzset Benjamin Peterson
@ 2018-09-15  1:16 ` Rich Felker
       [not found]   ` <20180915170524.18964-1-benjamin@python.org>
  2018-09-15 17:20   ` [PATCH] " Benjamin Peterson
  0 siblings, 2 replies; 4+ messages in thread
From: Rich Felker @ 2018-09-15  1:16 UTC (permalink / raw)
  To: musl

On Fri, Sep 14, 2018 at 03:46:55PM -0700, Benjamin Peterson wrote:
> do_tzset() did't always reset the DST transition rules r0 and r1. That means the
> rules from older TZ settings could leak into newer ones.
> 
> The following program demonstrates this bug. It should print out the same
> timezone twice but doesn't due to the leaky state.
> 
> int main() {
> 	time_t t = 0;
> 	struct tm p;
> 	setenv("TZ", "STD-1DST", 1);
> 	localtime_r(&t, &p);
> 	printf("%s\n", p.tm_zone);
> 	setenv("TZ", "STD-1DST,M3.2.0,M11.1.0", 1);
> 	tzset();
> 	setenv("TZ", "STD-1DST", 1);
> 	localtime_r(&t, &p);
> 	printf("%s\n", p.tm_zone);
> 	return 0;
> }
> ---
>  src/time/__tz.c | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/src/time/__tz.c b/src/time/__tz.c
> index 51e66514..3ccfdd5d 100644
> --- a/src/time/__tz.c
> +++ b/src/time/__tz.c
> @@ -130,6 +130,9 @@ static void do_tzset()
>  
>  	if (old_tz && !strcmp(s, old_tz)) return;
>  
> +	memset(r0, 0, sizeof r0);
> +	memset(r1, 0, sizeof r1);
> +
>  	if (zi) __munmap((void *)zi, map_size);
>  
>  	/* Cache the old value of TZ to check if it has changed. Avoid
> @@ -194,7 +197,6 @@ static void do_tzset()
>  			const unsigned char *p;
>  			__tzname[0] = __tzname[1] = 0;
>  			__daylight = __timezone = dst_off = 0;
> -			for (i=0; i<5; i++) r0[i] = r1[i] = 0;
>  			for (p=types; p<abbrevs; p+=6) {
>  				if (!p[4] && !__tzname[0]) {
>  					__tzname[0] = (char *)abbrevs + p[5];
> -- 
> 2.17.1

This looks right, but is there a reason you swapped the for loop out
for memsets? Either should work, but I probably preferred the loop in
the original since, due to -ffreestanding which is necessary when
building a libc, the compiler can't inline the memset automatically
with a builtin. I might however use the new header wrapper framework
to make it so (dependent on __GNUC__) memcpy and memset get redirected
to the __builtin_* versions except in the source files that define
them, in which case this kind of thing wouldn't matter.

Rich


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

* [PATCH v2] always reset DST rules during tzset
       [not found]   ` <20180915170524.18964-1-benjamin@python.org>
@ 2018-09-15 17:05     ` Benjamin Peterson
  0 siblings, 0 replies; 4+ messages in thread
From: Benjamin Peterson @ 2018-09-15 17:05 UTC (permalink / raw)
  To: musl

do_tzset() did't always reset the DST transition rules r0 and r1. That means the
rules from older TZ settings could leak into newer ones.

The following program demonstrates this bug. It should print out the same
timezone twice but doesn't due to the leaky state.

int main() {
	time_t t = 0;
	struct tm p;
	setenv("TZ", "STD-1DST", 1);
	localtime_r(&t, &p);
	printf("%s\n", p.tm_zone);
	setenv("TZ", "STD-1DST,M3.2.0,M11.1.0", 1);
	tzset();
	setenv("TZ", "STD-1DST", 1);
	localtime_r(&t, &p);
	printf("%s\n", p.tm_zone);
	return 0;
}
---
 src/time/__tz.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/time/__tz.c b/src/time/__tz.c
index 51e66514..185642e8 100644
--- a/src/time/__tz.c
+++ b/src/time/__tz.c
@@ -130,6 +130,8 @@ static void do_tzset()
 
 	if (old_tz && !strcmp(s, old_tz)) return;
 
+	for (i=0; i<5; i++) r0[i] = r1[i] = 0;
+
 	if (zi) __munmap((void *)zi, map_size);
 
 	/* Cache the old value of TZ to check if it has changed. Avoid
@@ -194,7 +196,6 @@ static void do_tzset()
 			const unsigned char *p;
 			__tzname[0] = __tzname[1] = 0;
 			__daylight = __timezone = dst_off = 0;
-			for (i=0; i<5; i++) r0[i] = r1[i] = 0;
 			for (p=types; p<abbrevs; p+=6) {
 				if (!p[4] && !__tzname[0]) {
 					__tzname[0] = (char *)abbrevs + p[5];
-- 
2.17.1



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

* Re: [PATCH] always reset DST rules during tzset
  2018-09-15  1:16 ` Rich Felker
       [not found]   ` <20180915170524.18964-1-benjamin@python.org>
@ 2018-09-15 17:20   ` Benjamin Peterson
  1 sibling, 0 replies; 4+ messages in thread
From: Benjamin Peterson @ 2018-09-15 17:20 UTC (permalink / raw)
  To: Rich Felker; +Cc: musl

> This looks right, but is there a reason you swapped the for loop out
> for memsets?

I thought it might generate better code due to store merging. But I definitely
forgot about -ffreestanding.

My latest patch switches back to the loop.



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

end of thread, other threads:[~2018-09-15 17:20 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-09-14 22:46 [PATCH] always reset DST rules during tzset Benjamin Peterson
2018-09-15  1:16 ` Rich Felker
     [not found]   ` <20180915170524.18964-1-benjamin@python.org>
2018-09-15 17:05     ` [PATCH v2] " Benjamin Peterson
2018-09-15 17:20   ` [PATCH] " Benjamin Peterson

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