]> granicus.if.org Git - procps-ng/commitdiff
0124-vmstat: Check return values of localtime() and strftime().
authorQualys Security Advisory <qsa@qualys.com>
Thu, 1 Jan 1970 00:00:00 +0000 (00:00 +0000)
committerCraig Small <csmall@enc.com.au>
Sat, 23 Jun 2018 11:59:14 +0000 (21:59 +1000)
Otherwise it leads to NULL-pointer dereferences (in case of localtime()
errors) and indeterminate contents of timebuf (in case of strftime()
errors).

Signed-off-by: Craig Small <csmall@enc.com.au>
vmstat.c

index c08349b987cb4dfd0329277a36ef8e8467fe8fa8..6eaf73660a51eea3ddd8ef3fbf7b7aa6a8523467 100644 (file)
--- a/vmstat.c
+++ b/vmstat.c
@@ -302,7 +302,7 @@ static void new_header(void)
     if (t_option) {
         (void) time( &the_time );
         tm_ptr = localtime( &the_time );
-        if (strftime(timebuf, sizeof(timebuf), "%Z", tm_ptr)) {
+               if (tm_ptr && strftime(timebuf, sizeof(timebuf), "%Z", tm_ptr)) {
             timebuf[strlen(timestamp_header) - 1] = '\0';
         } else {
             timebuf[0] = '\0';
@@ -365,7 +365,11 @@ static void new_format(void)
     if (t_option) {
         (void) time( &the_time );
         tm_ptr = localtime( &the_time );
-        strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_ptr);
+               if (tm_ptr && strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_ptr)) {
+                       ;
+               } else {
+                       timebuf[0] = '\0';
+               }
     }
     /* Do the initial fill */
     if (!(stat_stack = procps_stat_select(stat_info, First_stat_items, MAX_stat)))
@@ -440,7 +444,11 @@ static void new_format(void)
         if (t_option) {
             (void) time( &the_time );
             tm_ptr = localtime( &the_time );
-            strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_ptr);
+                       if (tm_ptr && strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_ptr)) {
+                               ;
+                       } else {
+                               timebuf[0] = '\0';
+                       }
         }
 
         /* idle can run backwards for a moment -- kernel "feature" */
@@ -608,7 +616,7 @@ static void diskheader(void)
     if (t_option) {
         (void) time( &the_time );
         tm_ptr = localtime( &the_time );
-        if (strftime(timebuf, sizeof(timebuf), "%Z", tm_ptr)) {
+               if (tm_ptr && strftime(timebuf, sizeof(timebuf), "%Z", tm_ptr)) {
             timebuf[strlen(timestamp_header) - 1] = '\0';
         } else {
             timebuf[0] = '\0';
@@ -643,7 +651,11 @@ static void diskformat(void)
         if (t_option) {
             (void) time( &the_time );
             tm_ptr = localtime( &the_time );
-            strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_ptr);
+                       if (tm_ptr && strftime(timebuf, sizeof(timebuf), "%Y-%m-%d %H:%M:%S", tm_ptr)) {
+                               ;
+                       } else {
+                               timebuf[0] = '\0';
+                       }
         }
         for (j = 0; j < reap->total; j++) {
             if (diskVAL(disk_TYPE, s_int) != DISKSTATS_TYPE_DISK)