]> xenbits.xensource.com Git - libvirt.git/commitdiff
virlogd: fix crash if log file exists and it's larger the maxlen
authorPavel Hrdina <phrdina@redhat.com>
Sat, 28 Nov 2015 08:03:40 +0000 (09:03 +0100)
committerPavel Hrdina <phrdina@redhat.com>
Mon, 30 Nov 2015 09:45:45 +0000 (10:45 +0100)
If for some reason there is an existing log file, that is larger then
max length of log file, we need to rollover that file immediately.
Trying to figure out how much data we could write will resolve in
overflow of unsigned variable 'towrite' and this leads to segfault.

Signed-off-by: Pavel Hrdina <phrdina@redhat.com>
src/util/virrotatingfile.c
tests/virrotatingfiletest.c

index 12607103016c53b561540c0d340b30ecb297cd44..827b44b6f94856d5c9dfa6aacb29e21ce03e4f90 100644 (file)
@@ -443,7 +443,12 @@ virRotatingFileWriterAppend(virRotatingFileWriterPtr file,
         size_t towrite = len;
         bool forceRollover = false;
 
-        if ((file->entry->pos + towrite) > file->maxlen) {
+        if (file->entry->pos > file->maxlen) {
+            /* If existing file is for some reason larger then max length we
+             * won't write to this file anymore, but we rollover this file.*/
+            forceRollover = true;
+            towrite = 0;
+        } else if ((file->entry->pos + towrite) > file->maxlen) {
             towrite = file->maxlen - file->entry->pos;
 
             /*
index 03e9664347563e97b877eafd8ac5a9617c99c384..44a59cdb17734d4047c755fc9403d83ee73f3d3f 100644 (file)
@@ -515,6 +515,48 @@ static int testRotatingFileWriterRolloverLineBreak(const void *data ATTRIBUTE_UN
 }
 
 
+static int testRotatingFileWriterLargeFile(const void *data ATTRIBUTE_UNUSED)
+{
+    virRotatingFileWriterPtr file;
+    int ret = -1;
+    const char *buf = "The quick brown fox jumps over the lazy dog\n"
+        "The wizard quickly jinxed the gnomes before they vaporized\n";
+
+    if (testRotatingFileInitFiles(200,
+                                  (off_t)-1,
+                                  (off_t)-1) < 0)
+        return -1;
+
+    file = virRotatingFileWriterNew(FILENAME,
+                                    160,
+                                    2,
+                                    false,
+                                    0700);
+    if (!file)
+        goto cleanup;
+
+    if (testRotatingFileWriterAssertFileSizes(200,
+                                              (off_t)-1,
+                                              (off_t)-1) < 0)
+        goto cleanup;
+
+    virRotatingFileWriterAppend(file, buf, strlen(buf));
+
+    if (testRotatingFileWriterAssertFileSizes(103,
+                                              200,
+                                              (off_t)-1) < 0)
+        goto cleanup;
+
+    ret = 0;
+ cleanup:
+    virRotatingFileWriterFree(file);
+    unlink(FILENAME);
+    unlink(FILENAME0);
+    unlink(FILENAME1);
+    return ret;
+}
+
+
 static int testRotatingFileReaderOne(const void *data ATTRIBUTE_UNUSED)
 {
     virRotatingFileReaderPtr file;
@@ -681,6 +723,9 @@ mymain(void)
     if (virtTestRun("Rotating file write rollover line break", testRotatingFileWriterRolloverLineBreak, NULL) < 0)
         ret = -1;
 
+    if (virtTestRun("Rotating file write to file larger then maxlen", testRotatingFileWriterLargeFile, NULL) < 0)
+        ret = -1;
+
     if (virtTestRun("Rotating file read one", testRotatingFileReaderOne, NULL) < 0)
         ret = -1;