]> git.ipfire.org Git - thirdparty/strongswan.git/commitdiff
asn1: Make sure the first argument to sscanf() is null-terminated
authorTobias Brunner <tobias@strongswan.org>
Tue, 23 May 2017 10:19:48 +0000 (12:19 +0200)
committerTobias Brunner <tobias@strongswan.org>
Tue, 23 May 2017 16:29:12 +0000 (18:29 +0200)
src/libstrongswan/asn1/asn1.c

index 5ce84032575e377b569008d12ead95ea7cc1e201..8b9dc1c4877acb6a3174c32eed4cff07ab0feb43 100644 (file)
@@ -350,13 +350,15 @@ time_t asn1_to_time(const chunk_t *utctime, asn1_t type)
        int tm_leap_4, tm_leap_100, tm_leap_400, tm_leap;
        int tz_hour, tz_min, tz_offset;
        time_t tm_days, tm_secs;
-       u_char *eot = NULL;
+       char buf[BUF_LEN], *eot = NULL;
 
-       if ((eot = memchr(utctime->ptr, 'Z', utctime->len)) != NULL)
+       snprintf(buf, sizeof(buf), "%.*s", (int)utctime->len, utctime->ptr);
+
+       if ((eot = strchr(buf, 'Z')) != NULL)
        {
                tz_offset = 0; /* Zulu time with a zero time zone offset */
        }
-       else if ((eot = memchr(utctime->ptr, '+', utctime->len)) != NULL)
+       else if ((eot = strchr(buf, '+')) != NULL)
        {
                if (sscanf(eot+1, "%2d%2d", &tz_hour, &tz_min) != 2)
                {
@@ -364,7 +366,7 @@ time_t asn1_to_time(const chunk_t *utctime, asn1_t type)
                }
                tz_offset = 3600*tz_hour + 60*tz_min;  /* positive time zone offset */
        }
-       else if ((eot = memchr(utctime->ptr, '-', utctime->len)) != NULL)
+       else if ((eot = strchr(buf, '-')) != NULL)
        {
                if (sscanf(eot+1, "%2d%2d", &tz_hour, &tz_min) != 2)
                {
@@ -382,15 +384,15 @@ time_t asn1_to_time(const chunk_t *utctime, asn1_t type)
                const char* format = (type == ASN1_UTCTIME)? "%2d%2d%2d%2d%2d":
                                                                                                         "%4d%2d%2d%2d%2d";
 
-               if (sscanf(utctime->ptr, format, &tm_year, &tm_mon, &tm_day,
-                                                                                &tm_hour, &tm_min) != 5)
+               if (sscanf(buf, format, &tm_year, &tm_mon, &tm_day,
+                                                               &tm_hour, &tm_min) != 5)
                {
                        return 0; /* error in [yy]yymmddhhmm time format */
                }
        }
 
        /* is there a seconds field? */
-       if ((eot - utctime->ptr) == ((type == ASN1_UTCTIME)?12:14))
+       if ((eot - buf) == ((type == ASN1_UTCTIME)?12:14))
        {
                if (sscanf(eot-2, "%2d", &tm_sec) != 1)
                {