logo

oasis

Own branch of Oasis Linux (upstream: <https://git.sr.ht/~mcf/oasis/>) git clone https://anongit.hacktivis.me/git/oasis.git

0007-fix-incorrect-access-to-tzname-by-strptime-Z-convers.patch (3586B)


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