From nobody Wed Nov 27 07:56:15 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=1700299638; cv=none; d=zohomail.com; s=zohoarc; b=cFJWvIIs5Wp79v+CWUkYXD81/AizzdRQScBkzjjqvzLiGZPke2O1N7LwVkGhQK4q8xFTWb2A3zR/yq2vCzjaKXfI5PfRBacaUZCLa1DceR+kOW0ahtXmoJ6uENJgTErFohBR8dcoafqGzlKOv86s46jlOqgi+1Fjfgc2EPlDLdI= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1700299638; 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=rS2eseUCEbFcqMJ5u5vWaUCi/cZtheDdVfcMVOjXvDI=; b=Qt4UweRl8/NzlCJKzbop2a+T3EZmB1zqPhXp8SHjd2o1K3Mk/RnJHyKaQlnY4SXnYl1wWrXxHYo3fdVpzRwxfUMQWBWJbtZfIa/Jj/Ss2u9PBdTRvi/O8egoD6WCjJwaTJw3qSTGn2nogZQTQUY5qbOT8qT3JwY0neV5CBJK6CA= 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 170029963891337.728480385555486; Sat, 18 Nov 2023 01:27:18 -0800 (PST) Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1r4Hb9-0006KN-FG; Sat, 18 Nov 2023 04:26:43 -0500 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 1r4Hb7-0006E2-R7 for qemu-devel@nongnu.org; Sat, 18 Nov 2023 04:26:41 -0500 Received: from mail-pj1-x102c.google.com ([2607:f8b0:4864:20::102c]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1r4Hb6-0003it-D4 for qemu-devel@nongnu.org; Sat, 18 Nov 2023 04:26:41 -0500 Received: by mail-pj1-x102c.google.com with SMTP id 98e67ed59e1d1-2809a824bbbso2118403a91.3 for ; Sat, 18 Nov 2023 01:26:40 -0800 (PST) Received: from localhost ([157.82.205.15]) by smtp.gmail.com with UTF8SMTPSA id f22-20020a17090a639600b00285139b71cdsm294193pjj.11.2023.11.18.01.26.37 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Sat, 18 Nov 2023 01:26:38 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=daynix-com.20230601.gappssmtp.com; s=20230601; t=1700299599; x=1700904399; 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=rS2eseUCEbFcqMJ5u5vWaUCi/cZtheDdVfcMVOjXvDI=; b=I8yC3wKnPYRMs5znNBjTROUGl6glVcMByQl84B7tq5t3mbEo0uuJSt6GyCQvyJhrHK 8sBG1kWDHcT+wnDYvBXoB0bDV/IzVSVlYfj7i5L1dsvptWshoa/pmPeYl6dbe6J0ma9U xSqmmdY6JExdUoowIkGN3JheXekUD0D2z5dxijMuncHX75u0sgfdYWztTX6y7taiSyRM dEfo6J9P8xKpj/htd+vRWK35/2DsxqE7ZeX1wQNu/r7dECzr/1bl6lzIeesHPVFo4pt/ sAmSF+19141OpS+CBnLi7Lxzu9Pl2fXoRIwp2Mg4QNjU9z0udHG5CAxCOVoCbCcYFxRa j39g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1700299599; x=1700904399; 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=rS2eseUCEbFcqMJ5u5vWaUCi/cZtheDdVfcMVOjXvDI=; b=POyH3nZiBK8766LUSsTHdTB9f6Adkr6N4KfnxlVh2G4/eKWhR9JMQftBqdfNfkkejY K9TB6QqdJPtLZVQNRyewO5roOdIOQ9eYANu07dMD5snHMfW8MYiWDI1606n8TF2n0fBE vKVFI0uBj6Uz6BPRN/kgzGf9NqA6d9WqZTOWIkw8KrUXQiZA1NCv63gFOWe7fBPyjtuK /t/nE048RZqPRFzkGWQ7hz4GGvlE/S4+sRjvFnVYG+LlDSvuVLbROWHOiU3Q7w2ytIoR L9UCpG+VSHIQ41MlT+VDDg3S8ZGqEHQcc3ADzf0L6ubtbRnN8ugNgdUrWKCS7Lzxddav VoGw== X-Gm-Message-State: AOJu0Yx9Sd0Q3JxuhGLF3FJ5YR7d/Z6Kml9xzSCARqVvJfk9LCGbuK+X MXV+1YqNz6pueyVepR7eTbSpQNjAzR901880ae4= X-Google-Smtp-Source: AGHT+IHEZSuEO+hbJEbKgUyLdo0KifqtaMli9amDx0qe70zw/P1yS1wjGzut6yYjO9/x509vUSupJg== X-Received: by 2002:a17:90b:4b42:b0:280:2438:120 with SMTP id mi2-20020a17090b4b4200b0028024380120mr1842696pjb.45.1700299599088; Sat, 18 Nov 2023 01:26:39 -0800 (PST) From: Akihiko Odaki To: Cc: qemu-devel@nongnu.org, Yuri Benditovich , Andrew Melnychenko , "Michael S . Tsirkin" , Jason Wang , "Zhang, Chen" , Akihiko Odaki Subject: [PATCH RESEND v7 07/19] virtio-net: Copy header only when necessary Date: Sat, 18 Nov 2023 18:25:47 +0900 Message-ID: <20231118092601.7132-8-akihiko.odaki@daynix.com> X-Mailer: git-send-email 2.42.1 In-Reply-To: <20231118092601.7132-1-akihiko.odaki@daynix.com> References: <20231118092601.7132-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::102c; envelope-from=akihiko.odaki@daynix.com; helo=mail-pj1-x102c.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, T_SCC_BODY_TEXT_LINE=-0.01 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: 1700299639599100001 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 e12176acb1..b6223031e1 100644 --- a/hw/net/virtio-net.c +++ b/hw/net/virtio-net.c @@ -359,7 +359,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 @@ -2709,7 +2710,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"); @@ -2717,19 +2718,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.1