Bug 13401 - calling localtime() or mktime() with a time far in the past corrupts cached timezone info
Summary: calling localtime() or mktime() with a time far in the past corrupts cached t...
Status: RESOLVED INVALID
Alias: None
Product: glibc
Classification: Unclassified
Component: libc (show other bugs)
Version: 2.12
: P2 normal
Target Milestone: ---
Assignee: Ulrich Drepper
URL:
Keywords:
Depends on:
Blocks:
 
Reported: 2011-11-11 00:08 UTC by Antoine Pitrou
Modified: 2014-06-27 11:37 UTC (History)
1 user (show)

See Also:
Host:
Target:
Build:
Last reconfirmed:
fweimer: security-


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Antoine Pitrou 2011-11-11 00:08:17 UTC
See the following snippet:

#include <time.h>
#include <stdlib.h>

int main() {
    time_t t;
    struct tm tmp;
    char str[200];

    t = time(NULL);
    tmp = *gmtime(&t);
    tmp.tm_gmtoff = 0;
    tmp.tm_zone = NULL;

    strftime(str, sizeof(str), "%Z", &tmp);
    puts(str);

    t = -2461446500;
    localtime(&t);

    t = time(NULL);
    tmp = *gmtime(&t);
    tmp.tm_gmtoff = 0;
    tmp.tm_zone = NULL;

    strftime(str, sizeof(str), "%Z", &tmp);
    puts(str);

    return 0;
}


Output here is:
CET
PMT

After a bit of digging, it seems the external variable "tzname" gets (wrongly) mutated by the localtime() call. The "%Z" format specifier just reuses that info.

This issue got reproduced on several distributions, see http://bugs.python.org/issue13309
Comment 1 Andreas Schwab 2011-11-21 13:09:49 UTC
By overwriting tm_gmtoff and tm_zone the struct tm is no longer valid.
Comment 2 Antoine Pitrou 2011-11-21 15:00:13 UTC
These fields are "overwritten" by hand in the example for ease of reproduction.
The actual use case is to fill the members of a struct tm by hand from user data. Since tm_gmtoff and tm_zone are not specified by POSIX, they are left alone, zero-filled (the struct is previously initialized with memset()). There's no portable way that I know of to initialize these fields.

While the most basic use of strftime() indeed consists in formatting a struct tm returned by a libc function (such as gmtime()), it seems reasonable to use strftime() to format user-defined time data. Nowhere does POSIX seem to say that the struct tm *has* to come from gmtime() or similar functions.