From nobody Sat May 4 18:21:47 2024 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=1612936731; cv=none; d=zohomail.com; s=zohoarc; b=FMa65GDeFvZvuLildSumEYCpqqAliIJRfMCKBVojg7Lt2aU40aDlCFMrAaoDAbzc6Vekc6VBwHSVX+aHEbN1WGxbFDYP6ZMb3JnM+C1Mc4PvIEqKq1kinBvi9vECmpgSsyED5/HYpPFZhOOXNTm3IwAu29xZMkTa7jgqGjsUE34= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1612936731; 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=od4B6nBjjDtLgPnWrI2WhTHrFwbGGinkL1wmuyNNAYs=; b=JBYJIf8a7vVsSYf2o+rN2e9RPjHb04bkYPGAlU+tAq6oKBVXcoaFUV3mfLHHEBQE3jmq05ucWpS8qzUeZ+gVVTJF/4S3hBdEsqRl1KJUFYNvNHClWv7prfCggLNfEKG2rixx8tFKLMK9hFaAQJvmG0tM3rd4vp3NjMfGLOu+phg= 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 1612936731500479.74722980331364; Tue, 9 Feb 2021 21:58:51 -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-489-09KES_1pN_q17W6btIrtew-1; Wed, 10 Feb 2021 00:58:47 -0500 Received: from smtp.corp.redhat.com (int-mx07.intmail.prod.int.phx2.redhat.com [10.5.11.22]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mimecast-mx01.redhat.com (Postfix) with ESMTPS id 2D68B1846092; Wed, 10 Feb 2021 05:58:41 +0000 (UTC) Received: from colo-mx.corp.redhat.com (colo-mx01.intmail.prod.int.phx2.redhat.com [10.5.11.20]) by smtp.corp.redhat.com (Postfix) with ESMTPS id 13FA710016F9; Wed, 10 Feb 2021 05:58:40 +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 242D718095C9; Wed, 10 Feb 2021 05:58:35 +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 11A5wXgx016104 for ; Wed, 10 Feb 2021 00:58:33 -0500 Received: by smtp.corp.redhat.com (Postfix) id ED6D42026D46; Wed, 10 Feb 2021 05:58:32 +0000 (UTC) Received: from mimecast-mx02.redhat.com (mimecast03.extmail.prod.ext.rdu2.redhat.com [10.11.55.19]) by smtp.corp.redhat.com (Postfix) with ESMTPS id A85942026D11 for ; Wed, 10 Feb 2021 05:58:30 +0000 (UTC) Received: from us-smtp-1.mimecast.com (us-smtp-delivery-1.mimecast.com [205.139.110.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 6E60E811E87 for ; Wed, 10 Feb 2021 05:58:30 +0000 (UTC) Received: from szxga05-in.huawei.com (szxga05-in.huawei.com [45.249.212.191]) (Using TLS) by relay.mimecast.com with ESMTP id us-mta-488-sJXW8ZYIOpa7qbrtcFvdQA-1; Wed, 10 Feb 2021 00:58:27 -0500 Received: from DGGEMS401-HUB.china.huawei.com (unknown [172.30.72.60]) by szxga05-in.huawei.com (SkyGuard) with ESMTP id 4Db8Cv1KCgzjLj3 for ; Wed, 10 Feb 2021 13:56:59 +0800 (CST) Received: from Y00470486.china.huawei.com (10.174.187.147) by DGGEMS401-HUB.china.huawei.com (10.3.19.201) with Microsoft SMTP Server id 14.3.498.0; Wed, 10 Feb 2021 13:58:13 +0800 X-MC-Unique: 09KES_1pN_q17W6btIrtew-1 X-MC-Unique: sJXW8ZYIOpa7qbrtcFvdQA-1 From: BiaoxiangYe To: Subject: [PATCH v3 1/1] rpc: avoid crash when system time jump back Date: Wed, 10 Feb 2021 05:58:05 +0000 Message-ID: <1612936685-17664-2-git-send-email-yebiaoxiang@huawei.com> In-Reply-To: <1612936685-17664-1-git-send-email-yebiaoxiang@huawei.com> References: <1612936685-17664-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.84 on 10.5.11.22 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: J=C3=A1n Tomko --- src/rpc/virkeepalive.c | 15 ++++++++------- 1 file changed, 8 insertions(+), 7 deletions(-) diff --git a/src/rpc/virkeepalive.c b/src/rpc/virkeepalive.c index 860b91b6b1..119427eb03 100644 --- a/src/rpc/virkeepalive.c +++ b/src/rpc/virkeepalive.c @@ -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() / G_USEC_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() / G_USEC_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() / G_USEC_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