logging: allow max_len=0 to disable log rollover

Currently setting max_len=0 causes virtlogd to spin in a busy loop. It
is natural to allow this to disable log rollover which can be useful for
developers debugging things.

Note disabling rollover exposes the host to denial of service from a
malicious guest, so must be used with care.

Closes https://gitlab.com/libvirt/libvirt/-/issues/85
Reviewed-by: Peter Krempa <pkrempa@redhat.com>
Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrangé 2020-10-13 10:20:19 +01:00
parent 71a6522e4f
commit 6938cd8830
2 changed files with 29 additions and 21 deletions

View File

@ -87,6 +87,10 @@
# Maximum file size before rolling over. Defaults to 2 MB
#
# Setting max_size to zero will disable rollover entirely.
# NOTE: disabling rollover exposes the host filesystem to
# denial of service from a malicious guest.
#
# Beware that a logrotate config file might be installed too,
# to handle cases where virtlogd is disabled. To ensure that
# the logrotate config is a no-op when virtlogd is running,

View File

@ -225,7 +225,8 @@ virRotatingFileWriterDelete(virRotatingFileWriterPtr file)
*
* The files will never exceed @maxlen bytes in size,
* but may be rolled over before they reach this size
* in order to avoid splitting lines
* in order to avoid splitting lines. If @maxlen is
* zero then no rollover will be performed.
*/
virRotatingFileWriterPtr
virRotatingFileWriterNew(const char *path,
@ -430,6 +431,7 @@ virRotatingFileWriterAppend(virRotatingFileWriterPtr file,
size_t towrite = len;
bool forceRollover = false;
if (file->maxlen != 0) {
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.*/
@ -452,6 +454,7 @@ virRotatingFileWriterAppend(virRotatingFileWriterPtr file,
}
}
}
}
if (towrite) {
if (safewrite(file->entry->fd, buf, towrite) != towrite) {
@ -468,8 +471,9 @@ virRotatingFileWriterAppend(virRotatingFileWriterPtr file,
file->entry->len += towrite;
}
if ((file->entry->pos == file->maxlen && len) ||
forceRollover) {
if (file->maxlen != 0 &&
((file->entry->pos == file->maxlen && len) ||
forceRollover)) {
virRotatingFileWriterEntryPtr tmp;
VIR_DEBUG("Hit max size %zu on %s (force=%d)",
file->maxlen, file->basepath, forceRollover);