From nobody Mon Feb 9 12:43:09 2026 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) client-ip=216.205.24.124; envelope-from=libvir-list-bounces@redhat.com; helo=us-smtp-delivery-124.mimecast.com; Authentication-Results: mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com ARC-Seal: i=1; a=rsa-sha256; t=1612862435; cv=none; d=zohomail.com; s=zohoarc; b=Hy7vZmp4Obx6Quy2LI3gGOetcB2EekK11j3gjcKxlQbKtUhxAfIU3Ab0sybE76+lmtxv/mhiEpehuay9kuIpOwyeCtNLMG74ke6FxMMO7w78OjCxSTWiSc56ph+m7XZ7ldrxScW6Rtnbq75+RbRKjE1Phr319g5hUBhMWGEi8Zc= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612862435; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=m/7HGyr+yLhKzAsPfwi/5k6EZKJOv2LSuT2u3MA5lwE=; b=VDvk/OD2NYohE7G/iikNLi+umlzW/CZ2I0Fq55Ipu13LPY2Y3H2bGUEnsYW+rXBqI11RLhhiK6MC26RGIOkBQZ6Bb44q7lshBWdzUOkYXkTP5CMpEMHQUK55Ka9YOpqhnGhVqyZVmHzUJ2T1T1q3R+Lfpb0sDUwihx5PlZPvyGI= ARC-Authentication-Results: i=1; mx.zohomail.com; spf=pass (zohomail.com: domain of redhat.com designates 216.205.24.124 as permitted sender) smtp.mailfrom=libvir-list-bounces@redhat.com Return-Path: Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [216.205.24.124]) by mx.zohomail.com with SMTPS id 1612862435195427.86014823690823; Tue, 9 Feb 2021 01:20:35 -0800 (PST) Received: from mimecast-mx01.redhat.com (mimecast-mx01.redhat.com [209.132.183.4]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-37-i9QVsJfhPWC--77EPCoMjA-1; Tue, 09 Feb 2021 04:20:30 -0500 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 mimecast-mx01.redhat.com (Postfix) with ESMTPS id 78CF379EC0; Tue, 9 Feb 2021 09:20:24 +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 6BEBF60C05; Tue, 9 Feb 2021 09:20:23 +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 C95024EE76; Tue, 9 Feb 2021 09:20:17 +0000 (UTC) Received: from smtp.corp.redhat.com (int-mx04.intmail.prod.int.rdu2.redhat.com [10.11.54.4]) by lists01.pubmisc.prod.ext.phx2.redhat.com (8.13.8/8.13.8) with ESMTP id 1199KFWD023842 for ; Tue, 9 Feb 2021 04:20:15 -0500 Received: by smtp.corp.redhat.com (Postfix) id 747772026D16; Tue, 9 Feb 2021 09:20:15 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast04.extmail.prod.ext.rdu2.redhat.com [10.11.55.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 6BDFB2026D49 for ; Tue, 9 Feb 2021 09:20:13 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [207.211.31.120]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-SHA384 (256/256 bits)) (No client certificate requested) by mimecast-mx02.redhat.com (Postfix) with ESMTPS id 793B2101A53B for ; Tue, 9 Feb 2021 09:20:13 +0000 (UTC) Received: from szxga04-in.huawei.com (szxga04-in.huawei.com [45.249.212.190]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-21-zw9yMaLKPJ-jkXx-h1GmVg-1; Tue, 09 Feb 2021 04:20:10 -0500 Received: from DGGEMS407-HUB.china.huawei.com (unknown [172.30.72.58]) by szxga04-in.huawei.com (SkyGuard) with ESMTP id 4DZcl552Ktz165hq for ; Tue, 9 Feb 2021 17:18:41 +0800 (CST) Received: from Y00470486.china.huawei.com (10.174.187.147) by DGGEMS407-HUB.china.huawei.com (10.3.19.207) with Microsoft SMTP Server id 14.3.498.0; Tue, 9 Feb 2021 17:19:57 +0800 X-MC-Unique: i9QVsJfhPWC--77EPCoMjA-1 X-MC-Unique: zw9yMaLKPJ-jkXx-h1GmVg-1 From: BiaoxiangYe To: Subject: [PATCH v2 1/2] rpc: avoid crash when system time jump back Date: Tue, 9 Feb 2021 09:19:47 +0000 Message-ID: <1612862388-12192-2-git-send-email-yebiaoxiang@huawei.com> In-Reply-To: <1612862388-12192-1-git-send-email-yebiaoxiang@huawei.com> References: <1612862388-12192-1-git-send-email-yebiaoxiang@huawei.com> MIME-Version: 1.0 X-Originating-IP: [10.174.187.147] X-CFilter-Loop: Reflected X-Mimecast-Impersonation-Protect: Policy=CLT - Impersonation Protection Definition; Similar Internal Domain=false; Similar Monitored External Domain=false; Custom External Domain=false; Mimecast External Domain=false; Newly Observed Domain=false; Internal User Name=false; Custom Display Name List=false; Reply-to Address Mismatch=false; Targeted Threat Dictionary=false; Mimecast Threat Dictionary=false; Custom Threat Dictionary=false X-Scanned-By: MIMEDefang 2.78 on 10.11.54.4 X-loop: libvir-list@redhat.com Cc: xiexiangyou@huawei.com 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: , Sender: libvir-list-bounces@redhat.com Errors-To: libvir-list-bounces@redhat.com X-Scanned-By: MIMEDefang 2.79 on 10.5.11.12 Authentication-Results: relay.mimecast.com; auth=pass smtp.auth=CUSA124A263 smtp.mailfrom=libvir-list-bounces@redhat.com X-Mimecast-Spam-Score: 0 X-Mimecast-Originator: redhat.com Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" From: BiaoXiang Ye Setting the system time backward would lead to a multiplication overflow in function virKeepAliveStart. The function virKeepAliveTimerInternal got the same bug too. Backtrace below: #0 0x0000ffffae898470 in raise () from /usr/lib64/libc.so.6 #1 0x0000ffffae89981c in abort () from /usr/lib64/libc.so.6 #2 0x0000ffffaf9a36a8 in __mulvsi3 () from /usr/lib64/libvirt.so.0 #3 0x0000ffffaf8fd9e8 in virKeepAliveStart (ka=3D0xaaaaf954ce10, interval= =3Dinterval entry=3D0, count=3Dcount entry=3D0) at ../../src/rpc/virkeepalive.c:283 #4 0x0000ffffaf908560 in virNetServerClientStartKeepAlive (client=3D0xaaa= af954cbe0) at ../../src/rpc/virnetserverclient.c:1628 #5 0x0000aaaac57eb6dc in remoteDispatchConnectSupportsFeature (server=3D0= xaaaaf95309d0, msg=3D0xaaaaf9549d90, ret=3D0xffff8c007fc0, args=3D0xffff8c002e70, rer= r=3D0xffff9ea054a0, client=3D0xaaaaf954cbe0) at ../../src/remote/remote_daemon_dispatch.c:= 5063 #6 remoteDispatchConnectSupportsFeatureHelper (server=3D0xaaaaf95309d0, c= lient=3D0xaaaaf954cbe0, msg=3D0xaaaaf9549d90, rerr=3D0xffff9ea054a0, args=3D0xffff8c002e70, re= t=3D0xffff8c007fc0) at ./remote/remote_daemon_dispatch_stubs.h:3503 #7 0x0000ffffaf9053a4 in virNetServerProgramDispatchCall(msg=3D0xaaaaf954= 9d90, client=3D0xaaaaf954cbe0, server=3D0x0, prog=3D0xaaaaf953a170) at ../../src/rpc/virnetserverprog= ram.c:451 #8 virNetServerProgramDispatch (prog=3D0xaaaaf953a170, server=3D0x0, serv= er entry=3D0xaaaaf95309d0, client=3D0xaaaaf954cbe0, msg=3D0xaaaaf9549d90) at ../../src/rpc/virnet= serverprogram.c:306 #9 0x0000ffffaf90a6bc in virNetServerProcessMsg (msg=3D, p= rog=3D, client=3D, srv=3D0xaaaaf95309d0) at ../../src/rpc/virne= tserver.c:137 #10 virNetServerHandleJob (jobOpaque=3D0xaaaaf950df80, opaque=3D0xaaaaf953= 09d0) at ../../src/rpc/virnetserver.c:154 #11 0x0000ffffaf812e14 in virThreadPoolWorker (opaque=3D) at ../../src/util/virthreadpool.c:163 #12 0x0000ffffaf81237c in virThreadHelper (data=3D) at ../.= ./src/util/virthread.c:246 #13 0x0000ffffaea327ac in ?? () from /usr/lib64/libpthread.so.0 #14 0x0000ffffae93747c in ?? () from /usr/lib64/libc.so.6 (gdb) frame 3 #3 0x0000ffffaf8fd9e8 in virKeepAliveStart (ka=3D0xaaaaf954ce10, interval= =3Dinterval entry=3D0, count=3Dcount entry=3D0) at ../../src/rpc/virkeepalive.c:283 283 timeout =3D ka->interval - delay; (gdb) list 278 now =3D time(NULL); 279 delay =3D now - ka->lastPacketReceived; <=3D'delay' got a negative = value 280 if (delay > ka->interval) 281 timeout =3D 0; 282 else 283 timeout =3D ka->interval - delay; 284 ka->intervalStart =3D now - (ka->interval - timeout); 285 ka->timer =3D virEventAddTimeout(timeout * 1000, virKeepAliveTimer,= <=3D multiplication overflow 286 ka, virObjectFreeCallback); 287 if (ka->timer < 0) (gdb) p now $2 =3D 18288001 (gdb) p ka->lastPacketReceived $3 =3D 1609430405 Signed-off-by: BiaoXiang Ye Reviewed-by: Jonathon Jongsma --- src/rpc/virkeepalive.c | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/src/rpc/virkeepalive.c b/src/rpc/virkeepalive.c index 860b91b6b1..786f9038ef 100644 --- a/src/rpc/virkeepalive.c +++ b/src/rpc/virkeepalive.c @@ -31,7 +31,7 @@ #include "virprobe.h" =20 #define VIR_FROM_THIS VIR_FROM_RPC - +#define MICROSEC_PER_SEC (1000 * 1000) VIR_LOG_INIT("rpc.keepalive"); =20 struct _virKeepAlive { @@ -40,8 +40,8 @@ struct _virKeepAlive { int interval; unsigned int count; unsigned int countToDeath; - time_t lastPacketReceived; - time_t intervalStart; + gint64 lastPacketReceived; + gint64 intervalStart; int timer; =20 virKeepAliveSendFunc sendCB; @@ -113,7 +113,7 @@ static bool virKeepAliveTimerInternal(virKeepAlivePtr ka, virNetMessagePtr *msg) { - time_t now =3D time(NULL); + gint64 now =3D g_get_monotonic_time() / MICROSEC_PER_SEC; int timeval; =20 if (ka->interval <=3D 0 || ka->intervalStart =3D=3D 0) @@ -231,9 +231,9 @@ virKeepAliveStart(virKeepAlivePtr ka, unsigned int count) { int ret =3D -1; - time_t delay; + gint64 delay; int timeout; - time_t now; + gint64 now; =20 virObjectLock(ka); =20 @@ -270,7 +270,7 @@ virKeepAliveStart(virKeepAlivePtr ka, "ka=3D%p client=3D%p interval=3D%d count=3D%u", ka, ka->client, interval, count); =20 - now =3D time(NULL); + now =3D g_get_monotonic_time() / MICROSEC_PER_SEC; delay =3D now - ka->lastPacketReceived; if (delay > ka->interval) timeout =3D 0; @@ -375,7 +375,8 @@ virKeepAliveCheckMessage(virKeepAlivePtr ka, virObjectLock(ka); =20 ka->countToDeath =3D ka->count; - ka->lastPacketReceived =3D ka->intervalStart =3D time(NULL); + ka->intervalStart =3D g_get_monotonic_time() / MICROSEC_PER_SEC; + ka->lastPacketReceived =3D ka->intervalStart; =20 if (msg->header.prog =3D=3D KEEPALIVE_PROGRAM && msg->header.vers =3D=3D KEEPALIVE_PROTOCOL_VERSION && --=20 2.23.0