002-0572555dab1d1e10b5f7351a005ec588cab41e25.patch (3582B)
1 From 0572555dab1d1e10b5f7351a005ec588cab41e25 Mon Sep 17 00:00:00 2001 2 From: Rich Felker <dalias@aerifal.cx> 3 Date: Sun, 22 Mar 2026 21:32:35 -0400 4 Subject: fix incorrect access to tzname[] by strptime %Z conversion specifier 5 6 there are three issues here: 7 8 1. if tzset has not been called (explicitly or implicitly), the 9 tzname[] array will contain null pointers, and the dereference to 10 compare against them has undefined behavior (and will fault). 11 12 2. access to tzname[] was performed without the timezone lock held. 13 this resulted in a data race if the timezone is concurrently changed 14 from another thread. 15 16 3. due to unintended signedness of the types, the open-coded isalpha 17 in the non-matching case was wrong and would continue past null 18 termination. 19 20 to fix the first two issues, the body of the %Z conversion is moved to 21 __tz.c where it has access to locking, and null checks are added. 22 23 there is probably an argument to be made that the equivalent of tzset 24 should happen here, but POSIX does not specify that to happen, so in 25 the absence of an interpretation adding such an allowance or 26 requirement, it is not done. 27 28 the third issue is fixed just by using the existing isalpha macro. 29 --- 30 src/time/__tz.c | 19 +++++++++++++++++++ 31 src/time/strptime.c | 13 +++---------- 32 src/time/time_impl.h | 1 + 33 3 files changed, 23 insertions(+), 10 deletions(-) 34 35 diff --git a/src/time/__tz.c b/src/time/__tz.c 36 index 54ed4cf6..cfce268e 100644 37 --- a/src/time/__tz.c 38 +++ b/src/time/__tz.c 39 @@ -436,3 +436,22 @@ const char *__tm_to_tzname(const struct tm *tm) 40 UNLOCK(lock); 41 return p; 42 } 43 + 44 +int __tzname_to_isdst(const char *restrict *s) 45 +{ 46 + size_t len; 47 + int isdst = -1; 48 + LOCK(lock); 49 + if (tzname[0] && !strncmp(*s, tzname[0], len = strlen(tzname[0]))) { 50 + isdst = 0; 51 + *s += len; 52 + } else if (tzname[1] && !strncmp(*s, tzname[1], len=strlen(tzname[1]))) { 53 + isdst = 1; 54 + *s += len; 55 + } else { 56 + /* FIXME: is this supposed to be an error? */ 57 + while (isalpha(**s)) ++*s; 58 + } 59 + UNLOCK(lock); 60 + return isdst; 61 +} 62 diff --git a/src/time/strptime.c b/src/time/strptime.c 63 index b1147242..40bb37af 100644 64 --- a/src/time/strptime.c 65 +++ b/src/time/strptime.c 66 @@ -5,6 +5,7 @@ 67 #include <stddef.h> 68 #include <string.h> 69 #include <strings.h> 70 +#include "time_impl.h" 71 72 char *strptime(const char *restrict s, const char *restrict f, struct tm *restrict tm) 73 { 74 @@ -207,16 +208,8 @@ char *strptime(const char *restrict s, const char *restrict f, struct tm *restri 75 s += 5; 76 break; 77 case 'Z': 78 - if (!strncmp(s, tzname[0], len = strlen(tzname[0]))) { 79 - tm->tm_isdst = 0; 80 - s += len; 81 - } else if (!strncmp(s, tzname[1], len=strlen(tzname[1]))) { 82 - tm->tm_isdst = 1; 83 - s += len; 84 - } else { 85 - /* FIXME: is this supposed to be an error? */ 86 - while ((*s|32)-'a' <= 'z'-'a') s++; 87 - } 88 + i = __tzname_to_isdst(&s); 89 + if (i>=0) tm->tm_isdst = i; 90 break; 91 case '%': 92 if (*s++ != '%') return 0; 93 diff --git a/src/time/time_impl.h b/src/time/time_impl.h 94 index f26d8005..ffe5050b 100644 95 --- a/src/time/time_impl.h 96 +++ b/src/time/time_impl.h 97 @@ -5,6 +5,7 @@ hidden int __month_to_secs(int, int); 98 hidden long long __year_to_secs(long long, int *); 99 hidden long long __tm_to_secs(const struct tm *); 100 hidden const char *__tm_to_tzname(const struct tm *); 101 +hidden int __tzname_to_isdst(const char *restrict *); 102 hidden int __secs_to_tm(long long, struct tm *); 103 hidden void __secs_to_zone(long long, int, int *, long *, long *, const char **); 104 hidden const char *__strftime_fmt_1(char (*)[100], size_t *, int, const struct tm *, locale_t, int); 105 -- 106 cgit v1.2.1 107