]> git.pld-linux.org Git - packages/php.git/blame_incremental - php-systzdata.patch
Up to 8.0.12; fixes CVE-2021-21703
[packages/php.git] / php-systzdata.patch
... / ...
CommitLineData
1# License: MIT
2# http://opensource.org/licenses/MIT
3
4Add support for use of the system timezone database, rather
5than embedding a copy. Discussed upstream but was not desired.
6
7History:
8r19: adapt for timelib 2020.02 (in 8.0.0beta2)
9r18: adapt for autotool change in 7.3.3RC1
10r17: adapt for timelib 2018.01 (in 7.3.2RC1)
11r16: adapt for timelib 2017.06 (in 7.2.3RC1)
12r15: adapt for timelib 2017.05beta7 (in 7.2.0RC1)
13r14: improve check for valid tz file
14r13: adapt for upstream changes to use PHP allocator
15r12: adapt for upstream changes for new zic
16r11: use canonical names to avoid more case sensitivity issues
17 round lat/long from zone.tab towards zero per builtin db
18r10: make timezone case insensitive
19r9: fix another compile error without --with-system-tzdata configured (Michael Heimpold)
20r8: fix compile error without --with-system-tzdata configured
21r7: improve check for valid timezone id to exclude directories
22r6: fix fd leak in r5, fix country code/BC flag use in
23 timezone_identifiers_list() using system db,
24 fix use of PECL timezonedb to override system db,
25r5: reverts addition of "System/Localtime" fake tzname.
26 updated for 5.3.0, parses zone.tab to pick up mapping between
27 timezone name, country code and long/lat coords
28r4: added "System/Localtime" tzname which uses /etc/localtime
29r3: fix a crash if /usr/share/zoneinfo doesn't exist (Raphael Geissert)
30r2: add filesystem trawl to set up name alias index
31r1: initial revision
32
33diff -up php-8.0.0beta3/ext/date/config0.m4.systzdata php-8.0.0beta3/ext/date/config0.m4
34--- php-8.0.0beta3/ext/date/config0.m4.systzdata 2020-09-01 19:13:26.000000000 +0200
35+++ php-8.0.0beta3/ext/date/config0.m4 2020-09-02 08:07:51.039979873 +0200
36@@ -4,6 +4,19 @@ AC_CHECK_HEADERS([io.h])
37 dnl Check for strtoll, atoll
38 AC_CHECK_FUNCS(strtoll atoll)
39
40+PHP_ARG_WITH(system-tzdata, for use of system timezone data,
41+[ --with-system-tzdata[=DIR] to specify use of system timezone data],
42+no, no)
43+
44+if test "$PHP_SYSTEM_TZDATA" != "no"; then
45+ AC_DEFINE(HAVE_SYSTEM_TZDATA, 1, [Define if system timezone data is used])
46+
47+ if test "$PHP_SYSTEM_TZDATA" != "yes"; then
48+ AC_DEFINE_UNQUOTED(HAVE_SYSTEM_TZDATA_PREFIX, "$PHP_SYSTEM_TZDATA",
49+ [Define for location of system timezone data])
50+ fi
51+fi
52+
53 PHP_DATE_CFLAGS="-I@ext_builddir@/lib -DZEND_ENABLE_STATIC_TSRMLS_CACHE=1 -DHAVE_TIMELIB_CONFIG_H=1"
54 timelib_sources="lib/astro.c lib/dow.c lib/parse_date.c lib/parse_tz.c
55 lib/timelib.c lib/tm2unixtime.c lib/unixtime2tm.c lib/parse_iso_intervals.c lib/interval.c"
56diff -up php-8.0.0beta3/ext/date/lib/parse_tz.c.systzdata php-8.0.0beta3/ext/date/lib/parse_tz.c
57--- php-8.0.0beta3/ext/date/lib/parse_tz.c.systzdata 2020-09-01 19:13:26.000000000 +0200
58+++ php-8.0.0beta3/ext/date/lib/parse_tz.c 2020-09-02 08:07:51.039979873 +0200
59@@ -26,8 +26,21 @@
60 #include "timelib.h"
61 #include "timelib_private.h"
62
63+#ifdef HAVE_SYSTEM_TZDATA
64+#include <sys/mman.h>
65+#include <sys/stat.h>
66+#include <limits.h>
67+#include <fcntl.h>
68+#include <unistd.h>
69+
70+#include "php_scandir.h"
71+
72+#else
73 #define TIMELIB_SUPPORTS_V2DATA
74 #include "timezonedb.h"
75+#endif
76+
77+#include <ctype.h>
78
79 #if (defined(__APPLE__) || defined(__APPLE_CC__)) && (defined(__BIG_ENDIAN__) || defined(__LITTLE_ENDIAN__))
80 # if defined(__LITTLE_ENDIAN__)
81@@ -94,6 +107,11 @@ static int read_php_preamble(const unsig
82 {
83 uint32_t version;
84
85+ if (memcmp(*tzf, "TZif", 4) == 0) {
86+ *tzf += 20;
87+ return 0;
88+ }
89+
90 /* read ID */
91 version = (*tzf)[3] - '0';
92 *tzf += 4;
93@@ -418,7 +436,429 @@ void timelib_dump_tzinfo(timelib_tzinfo
94 }
95 }
96
97-static int seek_to_tz_position(const unsigned char **tzf, const char *timezone, const timelib_tzdb *tzdb)
98+#ifdef HAVE_SYSTEM_TZDATA
99+
100+#ifdef HAVE_SYSTEM_TZDATA_PREFIX
101+#define ZONEINFO_PREFIX HAVE_SYSTEM_TZDATA_PREFIX
102+#else
103+#define ZONEINFO_PREFIX "/usr/share/zoneinfo"
104+#endif
105+
106+/* System timezone database pointer. */
107+static const timelib_tzdb *timezonedb_system;
108+
109+/* Hash table entry for the cache of the zone.tab mapping table. */
110+struct location_info {
111+ char code[2];
112+ double latitude, longitude;
113+ char name[64];
114+ char *comment;
115+ struct location_info *next;
116+};
117+
118+/* Cache of zone.tab. */
119+static struct location_info **system_location_table;
120+
121+/* Size of the zone.tab hash table; a random-ish prime big enough to
122+ * prevent too many collisions. */
123+#define LOCINFO_HASH_SIZE (1021)
124+
125+/* Compute a case insensitive hash of str */
126+static uint32_t tz_hash(const char *str)
127+{
128+ const unsigned char *p = (const unsigned char *)str;
129+ uint32_t hash = 5381;
130+ int c;
131+
132+ while ((c = tolower(*p++)) != '\0') {
133+ hash = (hash << 5) ^ hash ^ c;
134+ }
135+
136+ return hash % LOCINFO_HASH_SIZE;
137+}
138+
139+/* Parse an ISO-6709 date as used in zone.tab. Returns end of the
140+ * parsed string on success, or NULL on parse error. On success,
141+ * writes the parsed number to *result. */
142+static char *parse_iso6709(char *p, double *result)
143+{
144+ double v, sign;
145+ char *pend;
146+ size_t len;
147+
148+ if (*p == '+')
149+ sign = 1.0;
150+ else if (*p == '-')
151+ sign = -1.0;
152+ else
153+ return NULL;
154+
155+ p++;
156+ for (pend = p; *pend >= '0' && *pend <= '9'; pend++)
157+ ;;
158+
159+ /* Annoying encoding used by zone.tab has no decimal point, so use
160+ * the length to determine the format:
161+ *
162+ * 4 = DDMM
163+ * 5 = DDDMM
164+ * 6 = DDMMSS
165+ * 7 = DDDMMSS
166+ */
167+ len = pend - p;
168+ if (len < 4 || len > 7) {
169+ return NULL;
170+ }
171+
172+ /* p => [D]DD */
173+ v = (p[0] - '0') * 10.0 + (p[1] - '0');
174+ p += 2;
175+ if (len == 5 || len == 7)
176+ v = v * 10.0 + (*p++ - '0');
177+ /* p => MM[SS] */
178+ v += (10.0 * (p[0] - '0')
179+ + p[1] - '0') / 60.0;
180+ p += 2;
181+ /* p => [SS] */
182+ if (len > 5) {
183+ v += (10.0 * (p[0] - '0')
184+ + p[1] - '0') / 3600.0;
185+ p += 2;
186+ }
187+
188+ /* Round to five decimal place, not because it's a good idea,
189+ * but, because the builtin data uses rounded data, so, match
190+ * that. */
191+ *result = trunc(v * sign * 100000.0) / 100000.0;
192+
193+ return p;
194+}
195+
196+/* This function parses the zone.tab file to build up the mapping of
197+ * timezone to country code and geographic location, and returns a
198+ * hash table. The hash table is indexed by the function:
199+ *
200+ * tz_hash(timezone-name)
201+ */
202+static struct location_info **create_location_table(void)
203+{
204+ struct location_info **li, *i;
205+ char zone_tab[PATH_MAX];
206+ char line[512];
207+ FILE *fp;
208+
209+ strncpy(zone_tab, ZONEINFO_PREFIX "/zone.tab", sizeof zone_tab);
210+
211+ fp = fopen(zone_tab, "r");
212+ if (!fp) {
213+ return NULL;
214+ }
215+
216+ li = calloc(LOCINFO_HASH_SIZE, sizeof *li);
217+
218+ while (fgets(line, sizeof line, fp)) {
219+ char *p = line, *code, *name, *comment;
220+ uint32_t hash;
221+ double latitude, longitude;
222+
223+ while (isspace(*p))
224+ p++;
225+
226+ if (*p == '#' || *p == '\0' || *p == '\n')
227+ continue;
228+
229+ if (!isalpha(p[0]) || !isalpha(p[1]) || p[2] != '\t')
230+ continue;
231+
232+ /* code => AA */
233+ code = p;
234+ p[2] = 0;
235+ p += 3;
236+
237+ /* coords => [+-][D]DDMM[SS][+-][D]DDMM[SS] */
238+ p = parse_iso6709(p, &latitude);
239+ if (!p) {
240+ continue;
241+ }
242+ p = parse_iso6709(p, &longitude);
243+ if (!p) {
244+ continue;
245+ }
246+
247+ if (!p || *p != '\t') {
248+ continue;
249+ }
250+
251+ /* name = string */
252+ name = ++p;
253+ while (*p != '\t' && *p && *p != '\n')
254+ p++;
255+
256+ *p++ = '\0';
257+
258+ /* comment = string */
259+ comment = p;
260+ while (*p != '\t' && *p && *p != '\n')
261+ p++;
262+
263+ if (*p == '\n' || *p == '\t')
264+ *p = '\0';
265+
266+ hash = tz_hash(name);
267+ i = malloc(sizeof *i);
268+ memcpy(i->code, code, 2);
269+ strncpy(i->name, name, sizeof i->name);
270+ i->comment = strdup(comment);
271+ i->longitude = longitude;
272+ i->latitude = latitude;
273+ i->next = li[hash];
274+ li[hash] = i;
275+ /* printf("%s [%u, %f, %f]\n", name, hash, latitude, longitude); */
276+ }
277+
278+ fclose(fp);
279+
280+ return li;
281+}
282+
283+/* Return location info from hash table, using given timezone name.
284+ * Returns NULL if the name could not be found. */
285+const struct location_info *find_zone_info(struct location_info **li,
286+ const char *name)
287+{
288+ uint32_t hash = tz_hash(name);
289+ const struct location_info *l;
290+
291+ if (!li) {
292+ return NULL;
293+ }
294+
295+ for (l = li[hash]; l; l = l->next) {
296+ if (timelib_strcasecmp(l->name, name) == 0)
297+ return l;
298+ }
299+
300+ return NULL;
301+}
302+
303+/* Filter out some non-tzdata files and the posix/right databases, if
304+ * present. */
305+static int index_filter(const struct dirent *ent)
306+{
307+ return strcmp(ent->d_name, ".") != 0
308+ && strcmp(ent->d_name, "..") != 0
309+ && strcmp(ent->d_name, "posix") != 0
310+ && strcmp(ent->d_name, "posixrules") != 0
311+ && strcmp(ent->d_name, "right") != 0
312+ && strstr(ent->d_name, ".list") == NULL
313+ && strstr(ent->d_name, ".tab") == NULL;
314+}
315+
316+static int sysdbcmp(const void *first, const void *second)
317+{
318+ const timelib_tzdb_index_entry *alpha = first, *beta = second;
319+
320+ return timelib_strcasecmp(alpha->id, beta->id);
321+}
322+
323+
324+/* Create the zone identifier index by trawling the filesystem. */
325+static void create_zone_index(timelib_tzdb *db)
326+{
327+ size_t dirstack_size, dirstack_top;
328+ size_t index_size, index_next;
329+ timelib_tzdb_index_entry *db_index;
330+ char **dirstack;
331+
332+ /* LIFO stack to hold directory entries to scan; each slot is a
333+ * directory name relative to the zoneinfo prefix. */
334+ dirstack_size = 32;
335+ dirstack = malloc(dirstack_size * sizeof *dirstack);
336+ dirstack_top = 1;
337+ dirstack[0] = strdup("");
338+
339+ /* Index array. */
340+ index_size = 64;
341+ db_index = malloc(index_size * sizeof *db_index);
342+ index_next = 0;
343+
344+ do {
345+ struct dirent **ents;
346+ char name[PATH_MAX], *top;
347+ int count;
348+
349+ /* Pop the top stack entry, and iterate through its contents. */
350+ top = dirstack[--dirstack_top];
351+ snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s", top);
352+
353+ count = php_scandir(name, &ents, index_filter, php_alphasort);
354+
355+ while (count > 0) {
356+ struct stat st;
357+ const char *leaf = ents[count - 1]->d_name;
358+
359+ snprintf(name, sizeof name, ZONEINFO_PREFIX "/%s/%s",
360+ top, leaf);
361+
362+ if (strlen(name) && stat(name, &st) == 0) {
363+ /* Name, relative to the zoneinfo prefix. */
364+ const char *root = top;
365+
366+ if (root[0] == '/') root++;
367+
368+ snprintf(name, sizeof name, "%s%s%s", root,
369+ *root ? "/": "", leaf);
370+
371+ if (S_ISDIR(st.st_mode)) {
372+ if (dirstack_top == dirstack_size) {
373+ dirstack_size *= 2;
374+ dirstack = realloc(dirstack,
375+ dirstack_size * sizeof *dirstack);
376+ }
377+ dirstack[dirstack_top++] = strdup(name);
378+ }
379+ else {
380+ if (index_next == index_size) {
381+ index_size *= 2;
382+ db_index = realloc(db_index,
383+ index_size * sizeof *db_index);
384+ }
385+
386+ db_index[index_next++].id = strdup(name);
387+ }
388+ }
389+
390+ free(ents[--count]);
391+ }
392+
393+ if (count != -1) free(ents);
394+ free(top);
395+ } while (dirstack_top);
396+
397+ qsort(db_index, index_next, sizeof *db_index, sysdbcmp);
398+
399+ db->index = db_index;
400+ db->index_size = index_next;
401+
402+ free(dirstack);
403+}
404+
405+#define FAKE_HEADER "1234\0??\1??"
406+#define FAKE_UTC_POS (7 - 4)
407+
408+/* Create a fake data segment for database 'sysdb'. */
409+static void fake_data_segment(timelib_tzdb *sysdb,
410+ struct location_info **info)
411+{
412+ size_t n;
413+ char *data, *p;
414+
415+ data = malloc(3 * sysdb->index_size + 7);
416+
417+ p = mempcpy(data, FAKE_HEADER, sizeof(FAKE_HEADER) - 1);
418+
419+ for (n = 0; n < sysdb->index_size; n++) {
420+ const struct location_info *li;
421+ timelib_tzdb_index_entry *ent;
422+
423+ ent = (timelib_tzdb_index_entry *)&sysdb->index[n];
424+
425+ /* Lookup the timezone name in the hash table. */
426+ if (strcmp(ent->id, "UTC") == 0) {
427+ ent->pos = FAKE_UTC_POS;
428+ continue;
429+ }
430+
431+ li = find_zone_info(info, ent->id);
432+ if (li) {
433+ /* If found, append the BC byte and the
434+ * country code; set the position for this
435+ * section of timezone data. */
436+ ent->pos = (p - data) - 4;
437+ *p++ = '\1';
438+ *p++ = li->code[0];
439+ *p++ = li->code[1];
440+ }
441+ else {
442+ /* If not found, the timezone data can
443+ * point at the header. */
444+ ent->pos = 0;
445+ }
446+ }
447+
448+ sysdb->data = (unsigned char *)data;
449+}
450+
451+/* Returns true if the passed-in stat structure describes a
452+ * probably-valid timezone file. */
453+static int is_valid_tzfile(const struct stat *st, int fd)
454+{
455+ if (fd) {
456+ char buf[20];
457+ if (read(fd, buf, 20)!=20) {
458+ return 0;
459+ }
460+ lseek(fd, SEEK_SET, 0);
461+ if (memcmp(buf, "TZif", 4)) {
462+ return 0;
463+ }
464+ }
465+ return S_ISREG(st->st_mode) && st->st_size > 20;
466+}
467+
468+/* To allow timezone names to be used case-insensitively, find the
469+ * canonical name for this timezone, if possible. */
470+static const char *canonical_tzname(const char *timezone)
471+{
472+ if (timezonedb_system) {
473+ timelib_tzdb_index_entry *ent, lookup;
474+
475+ lookup.id = (char *)timezone;
476+
477+ ent = bsearch(&lookup, timezonedb_system->index,
478+ timezonedb_system->index_size, sizeof lookup,
479+ sysdbcmp);
480+ if (ent) {
481+ return ent->id;
482+ }
483+ }
484+
485+ return timezone;
486+}
487+
488+/* Return the mmap()ed tzfile if found, else NULL. On success, the
489+ * length of the mapped data is placed in *length. */
490+static char *map_tzfile(const char *timezone, size_t *length)
491+{
492+ char fname[PATH_MAX];
493+ struct stat st;
494+ char *p;
495+ int fd;
496+
497+ if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
498+ return NULL;
499+ }
500+
501+ snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
502+
503+ fd = open(fname, O_RDONLY);
504+ if (fd == -1) {
505+ return NULL;
506+ } else if (fstat(fd, &st) != 0 || !is_valid_tzfile(&st, fd)) {
507+ close(fd);
508+ return NULL;
509+ }
510+
511+ *length = st.st_size;
512+ p = mmap(NULL, st.st_size, PROT_READ, MAP_SHARED, fd, 0);
513+ close(fd);
514+
515+ return p != MAP_FAILED ? p : NULL;
516+}
517+
518+#endif
519+
520+static int inmem_seek_to_tz_position(const unsigned char **tzf, const char *timezone, const timelib_tzdb *tzdb)
521 {
522 int left = 0, right = tzdb->index_size - 1;
523
524@@ -444,9 +884,48 @@ static int seek_to_tz_position(const uns
525 return 0;
526 }
527
528+static int seek_to_tz_position(const unsigned char **tzf, const char *timezone,
529+ char **map, size_t *maplen,
530+ const timelib_tzdb *tzdb)
531+{
532+#ifdef HAVE_SYSTEM_TZDATA
533+ if (tzdb == timezonedb_system) {
534+ char *orig;
535+
536+ orig = map_tzfile(timezone, maplen);
537+ if (orig == NULL) {
538+ return 0;
539+ }
540+
541+ (*tzf) = (unsigned char *)orig;
542+ *map = orig;
543+ return 1;
544+ }
545+ else
546+#endif
547+ {
548+ return inmem_seek_to_tz_position(tzf, timezone, tzdb);
549+ }
550+}
551+
552 const timelib_tzdb *timelib_builtin_db(void)
553 {
554+#ifdef HAVE_SYSTEM_TZDATA
555+ if (timezonedb_system == NULL) {
556+ timelib_tzdb *tmp = malloc(sizeof *tmp);
557+
558+ tmp->version = "0.system";
559+ tmp->data = NULL;
560+ create_zone_index(tmp);
561+ system_location_table = create_location_table();
562+ fake_data_segment(tmp, system_location_table);
563+ timezonedb_system = tmp;
564+ }
565+
566+ return timezonedb_system;
567+#else
568 return &timezonedb_builtin;
569+#endif
570 }
571
572 const timelib_tzdb_index_entry *timelib_timezone_identifiers_list(const timelib_tzdb *tzdb, int *count)
573@@ -458,7 +937,30 @@ const timelib_tzdb_index_entry *timelib_
574 int timelib_timezone_id_is_valid(const char *timezone, const timelib_tzdb *tzdb)
575 {
576 const unsigned char *tzf;
577- return (seek_to_tz_position(&tzf, timezone, tzdb));
578+
579+#ifdef HAVE_SYSTEM_TZDATA
580+ if (tzdb == timezonedb_system) {
581+ char fname[PATH_MAX];
582+ struct stat st;
583+
584+ if (timezone[0] == '\0' || strstr(timezone, "..") != NULL) {
585+ return 0;
586+ }
587+
588+ if (system_location_table) {
589+ if (find_zone_info(system_location_table, timezone) != NULL) {
590+ /* found in cache */
591+ return 1;
592+ }
593+ }
594+
595+ snprintf(fname, sizeof fname, ZONEINFO_PREFIX "/%s", canonical_tzname(timezone));
596+
597+ return stat(fname, &st) == 0 && is_valid_tzfile(&st, 0);
598+ }
599+#endif
600+
601+ return (inmem_seek_to_tz_position(&tzf, timezone, tzdb));
602 }
603
604 static int skip_64bit_preamble(const unsigned char **tzf, timelib_tzinfo *tz)
605@@ -500,12 +1002,14 @@ static timelib_tzinfo* timelib_tzinfo_ct
606 timelib_tzinfo *timelib_parse_tzfile(const char *timezone, const timelib_tzdb *tzdb, int *error_code)
607 {
608 const unsigned char *tzf;
609+ char *memmap = NULL;
610+ size_t maplen;
611 timelib_tzinfo *tmp;
612 int version;
613 int transitions_result, types_result;
614 unsigned int type; /* TIMELIB_TZINFO_PHP or TIMELIB_TZINFO_ZONEINFO */
615
616- if (seek_to_tz_position(&tzf, timezone, tzdb)) {
617+ if (seek_to_tz_position(&tzf, timezone, &memmap, &maplen, tzdb)) {
618 tmp = timelib_tzinfo_ctor(timezone);
619
620 version = read_preamble(&tzf, tmp, &type);
621@@ -540,11 +1044,36 @@ timelib_tzinfo *timelib_parse_tzfile(con
622 }
623 skip_posix_string(&tzf, tmp);
624
625+#ifdef HAVE_SYSTEM_TZDATA
626+ if (memmap) {
627+ const struct location_info *li;
628+
629+ /* TZif-style - grok the location info from the system database,
630+ * if possible. */
631+
632+ if ((li = find_zone_info(system_location_table, timezone)) != NULL) {
633+ tmp->location.comments = timelib_strdup(li->comment);
634+ strncpy(tmp->location.country_code, li->code, 2);
635+ tmp->location.longitude = li->longitude;
636+ tmp->location.latitude = li->latitude;
637+ tmp->bc = 1;
638+ }
639+ else {
640+ set_default_location_and_comments(&tzf, tmp);
641+ }
642+
643+ /* Now done with the mmap segment - discard it. */
644+ munmap(memmap, maplen);
645+ } else {
646+#endif
647 if (type == TIMELIB_TZINFO_PHP) {
648 read_location(&tzf, tmp);
649 } else {
650 set_default_location_and_comments(&tzf, tmp);
651 }
652+#ifdef HAVE_SYSTEM_TZDATA
653+ }
654+#endif
655 } else {
656 *error_code = TIMELIB_ERROR_NO_SUCH_TIMEZONE;
657 tmp = NULL;
This page took 0.069613 seconds and 4 git commands to generate.