From nobody Fri May 3 11:45:31 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) client-ip=209.132.183.28; envelope-from=libvir-list-bounces@redhat.com; helo=mx1.redhat.com; Authentication-Results: mx.zohomail.com; spf=pass (zoho.com: domain of redhat.com designates 209.132.183.28 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com; dmarc=fail(p=none dis=none) header.from=linux.ibm.com Return-Path: Received: from mx1.redhat.com (mx1.redhat.com [209.132.183.28]) by mx.zohomail.com with SMTPS id 1534841386540916.5870494726531; Tue, 21 Aug 2018 01:49:46 -0700 (PDT) Received: from smtp.corp.redhat.com (int-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.12]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id EDA6C81DE1; Tue, 21 Aug 2018 08:49:43 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx02.intmail.prod.int.phx2.redhat.com [10.5.11.21]) by smtp.corp.redhat.com (Postfix) with ESMTPS id B8F2C77375; Tue, 21 Aug 2018 08:49:42 +0000 (UTC) Received: from lists01.pubmisc.prod.ext.phx2.redhat.com (lists01.pubmisc.prod.ext.phx2.redhat.com [10.5.19.33]) by colo-mx.corp.redhat.com (Postfix) with ESMTP id 236CF4BB75; Tue, 21 Aug 2018 08:49:40 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx05.intmail.prod.int.phx2.redhat.com [10.5.11.15]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id w7L8ndNQ015445 for ; Tue, 21 Aug 2018 04:49:39 -0400 Received: by smtp.corp.redhat.com (Postfix) id 1864580EE; Tue, 21 Aug 2018 08:49:39 +0000 (UTC) Received: from mx1.redhat.com (ext-mx07.extmail.prod.ext.phx2.redhat.com [10.5.110.31]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 0F1295D6B3 for ; Tue, 21 Aug 2018 08:49:39 +0000 (UTC) Received: from mx0a-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 04B6EC049D65 for ; Tue, 21 Aug 2018 08:49:38 +0000 (UTC) Received: from pps.filterd (m0098421.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.16.0.22/8.16.0.22) with SMTP id w7L8nPRk145476 for ; Tue, 21 Aug 2018 04:49:37 -0400 Received: from e06smtp07.uk.ibm.com (e06smtp07.uk.ibm.com [195.75.94.103]) by mx0a-001b2d01.pphosted.com with ESMTP id 2m0e2pknvx-1 (version=TLSv1.2 cipher=AES256-GCM-SHA384 bits=256 verify=NOT) for ; Tue, 21 Aug 2018 04:49:37 -0400 Received: from localhost by e06smtp07.uk.ibm.com with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted for from ; Tue, 21 Aug 2018 09:49:35 +0100 Received: from b06cxnps3074.portsmouth.uk.ibm.com (9.149.109.194) by e06smtp07.uk.ibm.com (192.168.101.137) with IBM ESMTP SMTP Gateway: Authorized Use Only! Violators will be prosecuted; (version=TLSv1/SSLv3 cipher=AES256-GCM-SHA384 bits=256/256) Tue, 21 Aug 2018 09:49:33 +0100 Received: from d06av26.portsmouth.uk.ibm.com (d06av26.portsmouth.uk.ibm.com [9.149.105.62]) by b06cxnps3074.portsmouth.uk.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id w7L8nWFT37290042 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=FAIL) for ; Tue, 21 Aug 2018 08:49:32 GMT Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 6E47FAE051 for ; Tue, 21 Aug 2018 11:49:11 +0100 (BST) Received: from d06av26.portsmouth.uk.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 541CFAE056 for ; Tue, 21 Aug 2018 11:49:11 +0100 (BST) Received: from marc-ibm.boeblingen.de.ibm.com (unknown [9.152.224.50]) by d06av26.portsmouth.uk.ibm.com (Postfix) with ESMTP for ; Tue, 21 Aug 2018 11:49:11 +0100 (BST) From: Marc Hartmayer To: Date: Tue, 21 Aug 2018 10:49:28 +0200 X-TM-AS-GCONF: 00 x-cbid: 18082108-0028-0000-0000-000002ED8D7F X-IBM-AV-DETECTION: SAVI=unused REMOTE=unused XFE=unused x-cbparentid: 18082108-0029-0000-0000-000023A6C9DC Message-Id: <20180821084928.31315-1-mhartmay@linux.ibm.com> X-Proofpoint-Virus-Version: vendor=fsecure engine=2.50.10434:, , definitions=2018-08-21_04:, , signatures=0 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 priorityscore=1501 malwarescore=0 suspectscore=1 phishscore=0 bulkscore=0 spamscore=0 clxscore=1015 lowpriorityscore=0 mlxscore=0 impostorscore=0 mlxlogscore=931 adultscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.0.1-1807170000 definitions=main-1808210096 X-Greylist: Sender passed SPF test, Sender IP whitelisted by DNSRBL, ACL 212 matched, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 21 Aug 2018 08:49:38 +0000 (UTC) X-Greylist: inspected by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.31]); Tue, 21 Aug 2018 08:49:38 +0000 (UTC) for IP:'148.163.158.5' DOMAIN:'mx0b-001b2d01.pphosted.com' HELO:'mx0a-001b2d01.pphosted.com' FROM:'mhartmay@linux.ibm.com' RCPT:'' X-RedHat-Spam-Score: -0.701 (RCVD_IN_DNSWL_LOW, SPF_PASS) 148.163.158.5 mx0b-001b2d01.pphosted.com 148.163.158.5 mx0b-001b2d01.pphosted.com X-Scanned-By: MIMEDefang 2.78 on 10.5.110.31 X-Scanned-By: MIMEDefang 2.79 on 10.5.11.15 X-loop: libvir-list@redhat.com Subject: [libvirt] [PATCH] util: Don't delete the original file for truncation X-BeenThere: libvir-list@redhat.com X-Mailman-Version: 2.1.12 Precedence: junk List-Id: Development discussions about the libvirt library & tools List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 21 Aug 2018 08:49:44 +0000 (UTC) X-ZohoMail: RDMRC_1 RSF_0 Z_629925259 SPT_0 Content-Type: text/plain; charset="utf-8" Truncate means that if a file exists it's length will be truncated to 0, but the mode and the owner shall be unchanged. The current behavior is that the original file is deleted and a new file is created. Let's fix this by using O_TRUNC. The function virRotatingFileWriterDelete is now unused but may be used in the future and is therefore still defined. Signed-off-by: Marc Hartmayer Reviewed-by: Boris Fiuczynski --- Note: This change has the (potentially unwanted) security effect that the owner/group of the log file does not change. Before this patch the old log file was deleted and the newly created log file was owned by the virtlogd user. Now, if a user has created the log file before, he can read the logs. If we don't wanna have this effect we can either adjust/add a virtlogd API or do a chown within the calling driver (e.g. QEMU driver). Side note: the original behavior has changed with the patch series "qemu: use FD passing for chardev UNIX sockets". Example where it the behavior has changed: --- src/util/virrotatingfile.c | 49 ++++++++++++++++++++++++++++++++----------= ---- 1 file changed, 34 insertions(+), 15 deletions(-) diff --git a/src/util/virrotatingfile.c b/src/util/virrotatingfile.c index ca62a8e02641..d6ab3780aae3 100644 --- a/src/util/virrotatingfile.c +++ b/src/util/virrotatingfile.c @@ -98,17 +98,25 @@ virRotatingFileReaderEntryFree(virRotatingFileReaderEnt= ryPtr entry) =20 static virRotatingFileWriterEntryPtr virRotatingFileWriterEntryNew(const char *path, - mode_t mode) + mode_t mode, + bool truncate) { virRotatingFileWriterEntryPtr entry; struct stat sb; + /* O_APPEND is also useful in combination with O_TRUNC since it + * guarantees the atomicity of a write operation (at least for + * POSIX systems) */ + int oflag =3D O_CREAT|O_APPEND|O_WRONLY|O_CLOEXEC; =20 VIR_DEBUG("Opening %s mode=3D0%02o", path, mode); =20 if (VIR_ALLOC(entry) < 0) return NULL; =20 - if ((entry->fd =3D open(path, O_CREAT|O_APPEND|O_WRONLY|O_CLOEXEC, mod= e)) < 0) { + if (truncate) + oflag |=3D O_TRUNC; + + if ((entry->fd =3D open(path, oflag, mode)) < 0) { virReportSystemError(errno, _("Unable to open file: %s"), path); goto error; @@ -182,18 +190,10 @@ virRotatingFileReaderEntryNew(const char *path) =20 =20 static int -virRotatingFileWriterDelete(virRotatingFileWriterPtr file) +virRotatingFileWriterDeleteBackup(virRotatingFileWriterPtr file) { size_t i; =20 - if (unlink(file->basepath) < 0 && - errno !=3D ENOENT) { - virReportSystemError(errno, - _("Unable to delete file %s"), - file->basepath); - return -1; - } - for (i =3D 0; i < file->maxbackup; i++) { char *oldpath; if (virAsprintf(&oldpath, "%s.%zu", file->basepath, i) < 0) @@ -214,6 +214,24 @@ virRotatingFileWriterDelete(virRotatingFileWriterPtr f= ile) } =20 =20 +static int ATTRIBUTE_UNUSED +virRotatingFileWriterDelete(virRotatingFileWriterPtr file) +{ + if (unlink(file->basepath) < 0 && + errno !=3D ENOENT) { + virReportSystemError(errno, + _("Unable to delete file %s"), + file->basepath); + return -1; + } + + if (virRotatingFileWriterDeleteBackup(file) < 0) + return -1; + + return 0; +} + + /** * virRotatingFileWriterNew * @path: the base path for files @@ -257,12 +275,12 @@ virRotatingFileWriterNew(const char *path, file->maxbackup =3D maxbackup; file->maxlen =3D maxlen; =20 - if (trunc && - virRotatingFileWriterDelete(file) < 0) + if (trunc && virRotatingFileWriterDeleteBackup(file) < 0) goto error; =20 if (!(file->entry =3D virRotatingFileWriterEntryNew(file->basepath, - mode))) + mode, + trunc))) goto error; =20 return file; @@ -491,7 +509,8 @@ virRotatingFileWriterAppend(virRotatingFileWriterPtr fi= le, return -1; =20 if (!(tmp =3D virRotatingFileWriterEntryNew(file->basepath, - file->mode))) + file->mode, + false))) return -1; =20 virRotatingFileWriterEntryFree(file->entry); --=20 2.13.4 -- libvir-list mailing list libvir-list@redhat.com https://www.redhat.com/mailman/listinfo/libvir-list