virlogd: fix crash if log file exists and it's larger the maxlen

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>
This commit is contained in:
Pavel Hrdina 2015-11-28 09:03:40 +01:00
parent 163a781e63
commit cb97426208
2 changed files with 51 additions and 1 deletions

View File

@ -443,7 +443,12 @@ virRotatingFileWriterAppend(virRotatingFileWriterPtr file,
size_t towrite = len; size_t towrite = len;
bool forceRollover = false; 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; towrite = file->maxlen - file->entry->pos;
/* /*

View 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) static int testRotatingFileReaderOne(const void *data ATTRIBUTE_UNUSED)
{ {
virRotatingFileReaderPtr file; virRotatingFileReaderPtr file;
@ -681,6 +723,9 @@ mymain(void)
if (virtTestRun("Rotating file write rollover line break", testRotatingFileWriterRolloverLineBreak, NULL) < 0) if (virtTestRun("Rotating file write rollover line break", testRotatingFileWriterRolloverLineBreak, NULL) < 0)
ret = -1; 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) if (virtTestRun("Rotating file read one", testRotatingFileReaderOne, NULL) < 0)
ret = -1; ret = -1;