From nobody Fri May 3 10:06:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1625570656; cv=none; d=zohomail.com; s=zohoarc; b=i/httvK90hRlPdWpR5WkDouIKL+tBfJ3fN/LpL908PI+Z0vfENpYMxMcbhcitGuv/lamgj/k+uCKogsOcKuhJ+O9hal6uv1qM+b7XG4TO8NYRB6JmejZcOi17DEPEFeaIJaovFp/ADwOrMn96Cnl/3FI0bsAzYPyyhPlmY6nZ/8= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625570656; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=XetfZYGAMtUUZ+TLI3v2YPc28PP5LrMlGpSi7tq+kDk=; b=RCJcWpzHq937jY/hMwvvN6fZdow2wbeWqS63MPjF6buXf9RR+OJYN7kJwJJh8TYFzzPfr32J8wfduF8jjSkp6AoowZ1L8hcPJG8UWL3BKFY2VgVgM2b5MtbSnEYI+qZcFFh8WMj2Zu0NfidYNRcm9O+Qkbtj12tRP3eI2+VJjGc= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1625570656661614.7586507951711; Tue, 6 Jul 2021 04:24:16 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.151119.279325 (Exim 4.92) (envelope-from ) id 1m0jB3-0005H1-IM; Tue, 06 Jul 2021 11:23:45 +0000 Received: by outflank-mailman (output) from mailman id 151119.279325; Tue, 06 Jul 2021 11:23:45 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m0jB3-0005Gu-FB; Tue, 06 Jul 2021 11:23:45 +0000 Received: by outflank-mailman (input) for mailman id 151119; Tue, 06 Jul 2021 11:23:43 +0000 Received: from all-amaz-eas1.inumbo.com ([34.197.232.57] helo=us1-amaz-eas2.inumbo.com) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m0jB1-00050S-ST for xen-devel@lists.xenproject.org; Tue, 06 Jul 2021 11:23:43 +0000 Received: from esa5.hc3370-68.iphmx.com (unknown [216.71.155.168]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id 9e879cf2-de4c-11eb-848b-12813bfff9fa; Tue, 06 Jul 2021 11:23:42 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: 9e879cf2-de4c-11eb-848b-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1625570622; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=mG6RSUJd/P4kPROIxzI66p2WXgDfEpS2fP5XlHKOE1U=; b=BizdhoPSfN2h+IGrDCvyCiV6TLdXC63kSkwLUlggWM169g6BBl3zJZdg RXvYJgxKEXti4cenSsHVEdwUCT0Y4hYII0er7oO88yIwxq0wn1Hm80fUf nXj7+y8xXvV7/rL7bsqCzDIULJvuZI09inuEESmRL9rt096g13HWY+1xf A=; Authentication-Results: esa5.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: cJG1nD6OlySdVDO7OU4ZcDYtinLX1HmYx2Sdn+YbdnWe6ytqItuFdPSiiC1oXplhmHHUsfIT+J nEQXt27PAMAXSnIyXAPeif3PyZ17bLnm4G+33DbO7HJD7Q7A0z2qEpcOXKH4e6Wj7ejmGIVje0 FIi3tIrTdS02SeDmwAfPaYbSKXoXHL0OvMBcRue3GIXjRCPWise7o0RgyPEFRHpRyrWyx9xk9S 211G5EjGjgm4CotTN3GM+Z0g0ckgFyNr32Ei69yX6Oj1QmVKUpQVusBDavHp2tYxw/KGiFoeQt KbY= X-SBRS: 5.1 X-MesageID: 47339752 X-Ironport-Server: esa5.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED IronPort-HdrOrdr: A9a23:lf5To6jEoRhZK+xUoMKfhC8VmXBQXt4ji2hC6mlwRA09TySZ// rOoB17726NtN9/YgBCpTntAsa9qDbnhPpICOoqTM6ftWvdyQmVxehZhOOIqVCNJ8S9zJ876U 4JSdkZNDSaNzhHZKjBjjVQa+xQpeW6zA== X-IronPort-AV: E=Sophos;i="5.83,328,1616472000"; d="scan'208";a="47339752" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , Ian Jackson , Wei Liu , Juergen Gross , Jan Beulich , Olaf Hering Subject: [PATCH 1/2] tools/migration: Fix iovec handling in send_checkpoint_dirty_pfn_list() Date: Tue, 6 Jul 2021 12:23:31 +0100 Message-ID: <20210706112332.31753-2-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210706112332.31753-1-andrew.cooper3@citrix.com> References: <20210706112332.31753-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1625570657634100009 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" We shouldn't be using two struct iovec's to write half of 'rec' each, and there is no need to malloc() for two struct iovec's at all. Simplify down to just two - one covering the whole of 'rec', and one coveri= ng the pfns array. No functional change. Signed-off-by: Andrew Cooper Acked-by: Olaf Hering Reviewed-by: Jan Beulich --- CC: Ian Jackson CC: Wei Liu CC: Juergen Gross CC: Jan Beulich CC: Olaf Hering --- tools/libs/guest/xg_sr_restore.c | 25 ++++++------------------- 1 file changed, 6 insertions(+), 19 deletions(-) diff --git a/tools/libs/guest/xg_sr_restore.c b/tools/libs/guest/xg_sr_rest= ore.c index aa4113d7f679..07c9e291610b 100644 --- a/tools/libs/guest/xg_sr_restore.c +++ b/tools/libs/guest/xg_sr_restore.c @@ -425,11 +425,13 @@ static int send_checkpoint_dirty_pfn_list(struct xc_s= r_context *ctx) int rc =3D -1; unsigned int count, written; uint64_t i, *pfns =3D NULL; - struct iovec *iov =3D NULL; xc_shadow_op_stats_t stats =3D { 0, ctx->restore.p2m_size }; struct xc_sr_record rec =3D { .type =3D REC_TYPE_CHECKPOINT_DIRTY_PFN_LIST, }; + struct iovec iov[2] =3D { + { &rec, sizeof(rec) }, + }; DECLARE_HYPERCALL_BUFFER_SHADOW(unsigned long, dirty_bitmap, &ctx->restore.dirty_bitmap_hbuf); =20 @@ -471,26 +473,12 @@ static int send_checkpoint_dirty_pfn_list(struct xc_s= r_context *ctx) pfns[written++] =3D i; } =20 - /* iovec[] for writev(). */ - iov =3D malloc(3 * sizeof(*iov)); - if ( !iov ) - { - ERROR("Unable to allocate memory for sending dirty bitmap"); - goto err; - } - rec.length =3D count * sizeof(*pfns); =20 - iov[0].iov_base =3D &rec.type; - iov[0].iov_len =3D sizeof(rec.type); - - iov[1].iov_base =3D &rec.length; - iov[1].iov_len =3D sizeof(rec.length); - - iov[2].iov_base =3D pfns; - iov[2].iov_len =3D count * sizeof(*pfns); + iov[1].iov_base =3D pfns; + iov[1].iov_len =3D rec.length; =20 - if ( writev_exact(ctx->restore.send_back_fd, iov, 3) ) + if ( writev_exact(ctx->restore.send_back_fd, iov, ARRAY_SIZE(iov)) ) { PERROR("Failed to write dirty bitmap to stream"); goto err; @@ -499,7 +487,6 @@ static int send_checkpoint_dirty_pfn_list(struct xc_sr_= context *ctx) rc =3D 0; err: free(pfns); - free(iov); return rc; } =20 --=20 2.11.0 From nobody Fri May 3 10:06:04 2024 Delivered-To: importer@patchew.org Received-SPF: pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) client-ip=192.237.175.120; envelope-from=xen-devel-bounces@lists.xenproject.org; helo=lists.xenproject.org; Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass(p=reject dis=none) header.from=citrix.com ARC-Seal: i=1; a=rsa-sha256; t=1625570657; cv=none; d=zohomail.com; s=zohoarc; b=MB/Mj5lvmHLq9kKFAT7BK+UH81dglV/VIZxf234AKxE2V/1z9sWHVaKa1h99PG0X/Y8FXgfhTX2rSbVPTuucpY83lSnhFIKTajifCT13kZEPn12w1jnFvjJiugFZMAMCGvEvcT5iGGWxdPoz3bST2C87Wr6uRWiGeg+LKFdrchg= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1625570657; h=Content-Type:Cc:Date:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:To; bh=fXWQMNVQtFnr6WDNJRiBArwTxI8o8/X1leO4EsOLGVo=; b=gBOr6Zho1N1MyrPQy/sfsU4pd9zRlqTy2b50YrAyOpvLRDsp4YoA8+pdIJCeUKv6KbpQ2wJhFV3rsMQc8R2tM4J9KvrT88t6tQYaeYTYAGraxdXpUMIpHeH0lXPAwOqjEGf84wDoPQNLTW4ZWNOrmefBRPGjWjF5TrlDZjQ2kDs= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of lists.xenproject.org designates 192.237.175.120 as permitted sender) smtp.mailfrom=xen-devel-bounces@lists.xenproject.org; dmarc=pass header.from= (p=reject dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1625570657706185.0058491161002; Tue, 6 Jul 2021 04:24:17 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.151120.279336 (Exim 4.92) (envelope-from ) id 1m0jB6-0005aE-S3; Tue, 06 Jul 2021 11:23:48 +0000 Received: by outflank-mailman (output) from mailman id 151120.279336; Tue, 06 Jul 2021 11:23:48 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m0jB6-0005a7-Ob; Tue, 06 Jul 2021 11:23:48 +0000 Received: by outflank-mailman (input) for mailman id 151120; Tue, 06 Jul 2021 11:23:47 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1m0jB5-00050M-95 for xen-devel@lists.xenproject.org; Tue, 06 Jul 2021 11:23:47 +0000 Received: from esa5.hc3370-68.iphmx.com (unknown [216.71.155.168]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id a6698324-0e09-41de-abe0-cad6e4d3afe9; Tue, 06 Jul 2021 11:23:42 +0000 (UTC) X-Outflank-Mailman: Message body and most headers restored to incoming version X-BeenThere: xen-devel@lists.xenproject.org List-Id: Xen developer discussion List-Unsubscribe: , List-Post: List-Help: List-Subscribe: , Errors-To: xen-devel-bounces@lists.xenproject.org Precedence: list Sender: "Xen-devel" X-Inumbo-ID: a6698324-0e09-41de-abe0-cad6e4d3afe9 DKIM-Signature: v=1; a=rsa-sha256; c=simple/simple; d=citrix.com; s=securemail; t=1625570622; h=from:to:cc:subject:date:message-id:in-reply-to: references:mime-version; bh=VXrAugVYE4b5MTycMGEmas33a/+GWPcEalVzYy+9Ju0=; b=IS0bAE4agv/t1sERC6R0tCqV+mNjnNTYSIOhc2Ni42+lp0CRIbmP8T+S OJQxKXJcNsc0P9ACmf8KHyVabUrcEQUJd7kk9YrgfEBor8MG7jEEnW9yO sm6QHAWfbZcOYVzNasm1QEubXMp7DVgjx91KpiyW9lJRTGRlqNNUuXMus c=; Authentication-Results: esa5.hc3370-68.iphmx.com; dkim=none (message not signed) header.i=none IronPort-SDR: ufRwhmBCrEF+p4kvtglXYkUtTbqLaCCRIjtqEQ3NLKubM2ugge1q00RVxagjDaRrp5KpYoTIMn 5wnNXejwuYMEdJYkpLpNm3uiZpMh6JszJA6blA4tBxip+3N6/+vHsqmtN9dZyOdagKJhWCANaK ygEOxuvrlJhac/bxL0vxmdR+c/DKmJ7/H2Kf1lnyOCP2S/9jT/60qk0X+/96BzHRHsdcPmAOY1 /LHr+iu4Yqitp1vE0znugHbpguuOoaT1buLiDIRKEUETzZhX/VEZLXHaLsX4Yihko9kO3ptx6u QLo= X-SBRS: 5.1 X-MesageID: 47339751 X-Ironport-Server: esa5.hc3370-68.iphmx.com X-Remote-IP: 162.221.158.21 X-Policy: $RELAYED IronPort-HdrOrdr: A9a23:B63c1a2vu+EnVe8PZUbf4wqjBLAkLtp133Aq2lEZdPRUGvb3qy nOpoVj6faaslYssR0b9exofZPwJE80lqQFh7X5X43SPzUO0VHAROoJgLcKgQeQfxEWntQtsp uIGJIeNDSfNzdHZL7BkWuFL+o= X-IronPort-AV: E=Sophos;i="5.83,328,1616472000"; d="scan'208";a="47339751" From: Andrew Cooper To: Xen-devel CC: Andrew Cooper , Ian Jackson , Wei Liu , Juergen Gross , Jan Beulich , Olaf Hering Subject: [PATCH 2/2] tools/migration: Fix potential overflow in send_checkpoint_dirty_pfn_list() Date: Tue, 6 Jul 2021 12:23:32 +0100 Message-ID: <20210706112332.31753-3-andrew.cooper3@citrix.com> X-Mailer: git-send-email 2.11.0 In-Reply-To: <20210706112332.31753-1-andrew.cooper3@citrix.com> References: <20210706112332.31753-1-andrew.cooper3@citrix.com> MIME-Version: 1.0 X-ZohoMail-DKIM: pass (identity @citrix.com) X-ZM-MESSAGEID: 1625570658819100012 Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="utf-8" 'count * sizeof(*pfns)' can in principle overflow, but is implausible in practice as the time between checkpoints is typically sub-second. Nevertheless, simplify the code and remove the risk. There is no need to loop over the bitmap to calculate count. The number of set bits is returned in xc_shadow_op_stats_t which is already collected (and ignored). Bounds check the count against what will fit in REC_LENGTH_MAX. At the time of writing, this allows up to 0xffffff pfns. Rearrange the pfns loop to ch= eck for errors both ways, not simply that there were more pfns than expected. Reported-by: Jan Beulich Signed-off-by: Andrew Cooper --- CC: Ian Jackson CC: Wei Liu CC: Juergen Gross CC: Jan Beulich CC: Olaf Hering --- tools/libs/guest/xg_sr_restore.c | 33 ++++++++++++++++----------------- 1 file changed, 16 insertions(+), 17 deletions(-) diff --git a/tools/libs/guest/xg_sr_restore.c b/tools/libs/guest/xg_sr_rest= ore.c index 07c9e291610b..bda04ee42e3f 100644 --- a/tools/libs/guest/xg_sr_restore.c +++ b/tools/libs/guest/xg_sr_restore.c @@ -425,7 +425,7 @@ static int send_checkpoint_dirty_pfn_list(struct xc_sr_= context *ctx) int rc =3D -1; unsigned int count, written; uint64_t i, *pfns =3D NULL; - xc_shadow_op_stats_t stats =3D { 0, ctx->restore.p2m_size }; + xc_shadow_op_stats_t stats; struct xc_sr_record rec =3D { .type =3D REC_TYPE_CHECKPOINT_DIRTY_PFN_LIST, }; @@ -444,14 +444,17 @@ static int send_checkpoint_dirty_pfn_list(struct xc_s= r_context *ctx) goto err; } =20 - for ( i =3D 0, count =3D 0; i < ctx->restore.p2m_size; i++ ) + count =3D stats.dirty_count; + + if ( ((REC_LENGTH_MAX - sizeof(rec)) / sizeof(*pfns)) < count ) { - if ( test_bit(i, dirty_bitmap) ) - count++; + ERROR("Too many PFNs (%u) to fit in record (limit %zu)", count, + ((REC_LENGTH_MAX - sizeof(rec)) / sizeof(*pfns))); + goto err; } =20 - - pfns =3D malloc(count * sizeof(*pfns)); + iov[1].iov_len =3D rec.length =3D count * sizeof(*pfns); + iov[1].iov_base =3D pfns =3D malloc(rec.length); if ( !pfns ) { ERROR("Unable to allocate %zu bytes of memory for dirty pfn list", @@ -459,24 +462,20 @@ static int send_checkpoint_dirty_pfn_list(struct xc_s= r_context *ctx) goto err; } =20 - for ( i =3D 0, written =3D 0; i < ctx->restore.p2m_size; ++i ) + for ( i =3D 0, written =3D 0; count && i < ctx->restore.p2m_size; ++i,= --count ) { if ( !test_bit(i, dirty_bitmap) ) continue; =20 - if ( written > count ) - { - ERROR("Dirty pfn list exceed"); - goto err; - } - pfns[written++] =3D i; } =20 - rec.length =3D count * sizeof(*pfns); - - iov[1].iov_base =3D pfns; - iov[1].iov_len =3D rec.length; + if ( written !=3D stats.dirty_count ) + { + ERROR("Mismatch between dirty bitmap bits (%u), and dirty_count (%= u)", + written, stats.dirty_count); + goto err; + } =20 if ( writev_exact(ctx->restore.send_back_fd, iov, ARRAY_SIZE(iov)) ) { --=20 2.11.0