From nobody Wed Nov 27 20:30:21 2024 Delivered-To: importer@patchew.org Authentication-Results: mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org ARC-Seal: i=1; a=rsa-sha256; t=1697378938; cv=none; d=zohomail.com; s=zohoarc; b=Jhe7SBXfgmtOb9qSmI6+5MQpPZEeIcOQXX2pY7Rf7tEPLqWRIBGKZkHs25mCCd+b8fCihgsIkIlljnAXEzAMaYksKLBzGFnAupOqkCHLFuAOYzRXLHwaMh0ZvoQiajN1v4cIeq4cQl/6RUT8WEGVcHNZSRo8CMaJPUv9MHVyxgU= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1697378938; h=Content-Transfer-Encoding:Cc:Cc:Date:Date:From:From:In-Reply-To:List-Subscribe:List-Post:List-Id:List-Archive:List-Help:List-Unsubscribe:MIME-Version:Message-ID:References:Sender:Subject:Subject:Message-Id:Reply-To:To; bh=GwJY41Y5Ssd2yodd2ldhaOgM4YyGsLPHPwk9ht/SrrY=; b=VB+jg1RCyVrGydY3adV13RfydHXLPgmV5NrXF9FgSQi1ZJV2J7CU0qdAY6htkhz//SiZVgUAGqa/zH2hsQDpFuwLnonwhZ6yEe11vtcYsINe/0OqK30p/aG691JD92wknXYJkLH2MHtNRERVUGCluGEP+8IXltl0/pmzdXIv2DI= ARC-Authentication-Results: i=1; mx.zohomail.com; dkim=pass; spf=pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) smtp.mailfrom=qemu-devel-bounces+importer=patchew.org@nongnu.org Return-Path: Received: from lists.gnu.org (lists.gnu.org [209.51.188.17]) by mx.zohomail.com with SMTPS id 1697378938244800.906518640186; Sun, 15 Oct 2023 07:08:58 -0700 (PDT) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qs1iV-0001Sd-NM; Sun, 15 Oct 2023 10:03:39 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1qs1iU-0001SN-Fx for qemu-devel@nongnu.org; Sun, 15 Oct 2023 10:03:38 -0400 Received: from mail-pl1-x62c.google.com ([2607:f8b0:4864:20::62c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qs1iT-0007l3-01 for qemu-devel@nongnu.org; Sun, 15 Oct 2023 10:03:38 -0400 Received: by mail-pl1-x62c.google.com with SMTP id d9443c01a7336-1c88b46710bso23202495ad.1 for ; Sun, 15 Oct 2023 07:03:36 -0700 (PDT) Received: from localhost ([2400:4050:a840:1e00:78d2:b862:10a7:d486]) by smtp.gmail.com with UTF8SMTPSA id l18-20020a170902eb1200b001c5fc291ef9sm6861775plb.209.2023.10.15.07.03.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sun, 15 Oct 2023 07:03:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1697378615; x=1697983415; darn=nongnu.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=GwJY41Y5Ssd2yodd2ldhaOgM4YyGsLPHPwk9ht/SrrY=; b=czJ7fMGArjgTp0CLkMQ9uCYlVLkhHXU6ZJ7EGpwBCKEw3K7B2RvYM0E4PBJnxb3TRs upx7lO/o+Q/7bshTHr1Tr4+dH6tdtuJaDvdq72N2SUjYMV8fzt8vCK+HQoMOLkX6G/5b tmPuwwJhiEcFwVwlLYa5GohRN17wg1GC8IS6Dt14U062v+j73dsUiqgknpCXD62sccAP 4RLlKc10/mZCn1A+hz6o5xcN87WG6j1EhF+WQii/vOlYHarWj2Vf6loMpFxXjdd0E5B7 EkA1PoV1EnJuErG4Asv9kykrLQ77G5O+ah5B0B9pYzEfVjE4c6x2yuvU9QUUakf3FlWs xv0Q== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1697378615; x=1697983415; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=GwJY41Y5Ssd2yodd2ldhaOgM4YyGsLPHPwk9ht/SrrY=; b=RMgKklQwGLV9riQ+WPO7/DIIS7lROx6O0HaLsSScDeK+virII0obuQo3TUo2SE0nY6 9iN63IllL2rZ3uXLHYu5fCw5qAOmruAPEFV7YWb+ycxrBljbFu/7uvxPc2x+rWHlWdZG //LUjp0fBsUdYWzj4gTrQqe/q6ZKjSDFac0YhkQMCcm3oIuXUKxi63rXjhMxQyzaqbqO JJCYnY3et1699gl4ZikIMkFjlf1g6tYuE7grn+HoeLXMqSrZn5lyznLSh5CwzsOZjFux j3RKHm6J9XaXqE8NkkkBNIVzyVK1gF+NUjJxu9BdPxdz9+7DCfiGyNi28jy+zgey3IYD 1RSQ== X-Gm-Message-State: AOJu0YyH7xRtrmPyOwfAsrVa/OAjwtCTqC14BCBqms2izznESGWgLB/R tEIenuY5UNjFRe/eI7/4/rk2Pm2QfasKQDpY6of0XA== X-Google-Smtp-Source: AGHT+IHu5LpqjY8urx2INBkOMqnDvUvXYRd/B9rTgpKsvH0jdccmv6hPCik04pZ6K4/hVkOGbzFJaw== X-Received: by 2002:a17:902:904c:b0:1c6:2ae1:dc28 with SMTP id w12-20020a170902904c00b001c62ae1dc28mr24747159plz.36.1697378615585; Sun, 15 Oct 2023 07:03:35 -0700 (PDT) From: Akihiko Odaki To: Cc: qemu-devel@nongnu.org, Yuri Benditovich , Andrew Melnychenko , "Michael S . Tsirkin" , Jason Wang , Akihiko Odaki Subject: [PATCH v4 08/20] virtio-net: Copy header only when necessary Date: Sun, 15 Oct 2023 23:02:40 +0900 Message-ID: <20231015140259.259434-9-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.0 In-Reply-To: <20231015140259.259434-1-akihiko.odaki@daynix.com> References: <20231015140259.259434-1-akihiko.odaki@daynix.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable Received-SPF: pass (zohomail.com: domain of gnu.org designates 209.51.188.17 as permitted sender) client-ip=209.51.188.17; envelope-from=qemu-devel-bounces+importer=patchew.org@nongnu.org; helo=lists.gnu.org; Received-SPF: none client-ip=2607:f8b0:4864:20::62c; envelope-from=akihiko.odaki@daynix.com; helo=mail-pl1-x62c.google.com X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_NONE=0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: qemu-devel-bounces+importer=patchew.org@nongnu.org Sender: qemu-devel-bounces+importer=patchew.org@nongnu.org X-ZohoMail-DKIM: pass (identity @daynix-com.20230601.gappssmtp.com) X-ZM-MESSAGEID: 1697378939058100001 Content-Type: text/plain; charset="utf-8" It is necessary to copy the header only for byte swapping. Worse, when byte swapping is not needed, the header can be larger than the buffer due to VIRTIO_NET_F_HASH_REPORT, which results in buffer overflow. Copy the header only when byte swapping is needed. Fixes: e22f0603fb ("virtio-net: reference implementation of hash report") Signed-off-by: Akihiko Odaki --- hw/net/virtio-net.c | 26 ++++++++++++-------------- 1 file changed, 12 insertions(+), 14 deletions(-) diff --git a/hw/net/virtio-net.c b/hw/net/virtio-net.c index cab79f19cb..b1cc74881a 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -361,7 +361,8 @@ static void virtio_net_vnet_endian_status(VirtIONet *n,= uint8_t status) * can't do it, we fallback onto fixing the headers in the core * virtio-net code. */ - n->needs_vnet_hdr_swap =3D virtio_net_set_vnet_endian(vdev, n->nic= ->ncs, + n->needs_vnet_hdr_swap =3D n->has_vnet_hdr && + virtio_net_set_vnet_endian(vdev, n->nic->= ncs, queue_pairs, t= rue); } else if (virtio_net_started(n, vdev->status)) { /* After using the device, we need to reset the network backend to @@ -2690,7 +2691,7 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *q) return -EINVAL; } =20 - if (n->has_vnet_hdr) { + if (n->needs_vnet_hdr_swap) { if (iov_to_buf(out_sg, out_num, 0, &mhdr, n->guest_hdr_len) < n->guest_hdr_len) { virtio_error(vdev, "virtio-net header incorrect"); @@ -2698,19 +2699,16 @@ static int32_t virtio_net_flush_tx(VirtIONetQueue *= q) g_free(elem); return -EINVAL; } - if (n->needs_vnet_hdr_swap) { - virtio_net_hdr_swap(vdev, (void *) &mhdr); - sg2[0].iov_base =3D &mhdr; - sg2[0].iov_len =3D n->guest_hdr_len; - out_num =3D iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, - out_sg, out_num, - n->guest_hdr_len, -1); - if (out_num =3D=3D VIRTQUEUE_MAX_SIZE) { - goto drop; - } - out_num +=3D 1; - out_sg =3D sg2; + virtio_net_hdr_swap(vdev, (void *) &mhdr); + sg2[0].iov_base =3D &mhdr; + sg2[0].iov_len =3D n->guest_hdr_len; + out_num =3D iov_copy(&sg2[1], ARRAY_SIZE(sg2) - 1, out_sg, out= _num, + n->guest_hdr_len, -1); + if (out_num =3D=3D VIRTQUEUE_MAX_SIZE) { + goto drop; } + out_num +=3D 1; + out_sg =3D sg2; } /* * If host wants to see the guest header as is, we can --=20 2.42.0