From nobody Wed May 1 22:39:48 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=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1629800915; cv=none; d=zohomail.com; s=zohoarc; b=FEIYagG30gQ4cimUpL2QYc8a/d07s9xKxzeconYHdpLtfHpsxv12QnMeRVH89hqmSL4lojcPLysHJ3OeTV9wH8v2s5aR8chNWgRhHbZfrhK4CrrtoZ0IxtImJw2UHpKvefuSSqd49AJHoCgIYDpwoN930xnqFbt++Vhr3gJAmhE= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1629800915; h=Content-Transfer-Encoding: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=IbajY+E/j5QwN8ylzKx9q6TBPEhkc8yuc65ahR0PtvI=; b=nKh7ZRR9Qv4W2gwgZYlqxszcEvDTlR1sIfVXKkOICi69ywwZuPbYacqgBa32F+aXCJ9ojoFKwHVnq/u7sO+Bq8cDVrFBCbwiRmptBZWFu+KNORIyVfJ+1+IO8FsL75/f8bdahErwCC+MJC9urtoTUsnpxpPg+tfS/93F5SVY2RU= 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1629800915511753.9782225226026; Tue, 24 Aug 2021 03:28:35 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.170856.311834 (Exim 4.92) (envelope-from ) id 1mITfJ-0003bt-Fc; Tue, 24 Aug 2021 10:28:21 +0000 Received: by outflank-mailman (output) from mailman id 170856.311834; Tue, 24 Aug 2021 10:28:21 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mITfJ-0003bh-BL; Tue, 24 Aug 2021 10:28:21 +0000 Received: by outflank-mailman (input) for mailman id 170856; Tue, 24 Aug 2021 10:28:20 +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 1mITfI-00031f-7P for xen-devel@lists.xenproject.org; Tue, 24 Aug 2021 10:28:20 +0000 Received: from smtp-out2.suse.de (unknown [195.135.220.29]) by us1-amaz-eas2.inumbo.com (Halon) with ESMTPS id fcdf2e9c-04c5-11ec-a8c4-12813bfff9fa; Tue, 24 Aug 2021 10:28:14 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id A1B482004F; Tue, 24 Aug 2021 10:28:13 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 57BEF136DD; Tue, 24 Aug 2021 10:28:13 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id SPvuE73JJGG8DwAAGKfGzw (envelope-from ); Tue, 24 Aug 2021 10:28:13 +0000 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: fcdf2e9c-04c5-11ec-a8c4-12813bfff9fa DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1629800893; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=IbajY+E/j5QwN8ylzKx9q6TBPEhkc8yuc65ahR0PtvI=; b=mtDdJM9krc0OORygAchXMQ1LUti7fTdnE0i5Bpye5LlNYakl02v56oaP3Wr3xyP9+Rr+gZ HYespA8yyUahVvKBf97VhQx96d+Ic56qZ/wmmuRcW28YMSIMSD76oguqKSoZxcM4pQD4B8 2+uI+Ll40TQO7J+fbs6O3W9G6E+VGtc= From: Juergen Gross To: xen-devel@lists.xenproject.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini , "David S. Miller" , Jakub Kicinski , Jan Beulich Subject: [PATCH v2 1/4] xen/netfront: read response from backend only once Date: Tue, 24 Aug 2021 12:28:06 +0200 Message-Id: <20210824102809.26370-2-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210824102809.26370-1-jgross@suse.com> References: <20210824102809.26370-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1629800917687100001 Content-Type: text/plain; charset="utf-8" In order to avoid problems in case the backend is modifying a response on the ring page while the frontend has already seen it, just read the response into a local buffer in one go and then operate on that buffer only. Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich --- V2: - use direct structure assignment instead of memcpy() (Jan Beulich) --- drivers/net/xen-netfront.c | 38 +++++++++++++++++++------------------- 1 file changed, 19 insertions(+), 19 deletions(-) diff --git a/drivers/net/xen-netfront.c b/drivers/net/xen-netfront.c index 44275908d61a..003cdf2ffc92 100644 --- a/drivers/net/xen-netfront.c +++ b/drivers/net/xen-netfront.c @@ -399,13 +399,13 @@ static void xennet_tx_buf_gc(struct netfront_queue *q= ueue) rmb(); /* Ensure we see responses up to 'rp'. */ =20 for (cons =3D queue->tx.rsp_cons; cons !=3D prod; cons++) { - struct xen_netif_tx_response *txrsp; + struct xen_netif_tx_response txrsp; =20 - txrsp =3D RING_GET_RESPONSE(&queue->tx, cons); - if (txrsp->status =3D=3D XEN_NETIF_RSP_NULL) + RING_COPY_RESPONSE(&queue->tx, cons, &txrsp); + if (txrsp.status =3D=3D XEN_NETIF_RSP_NULL) continue; =20 - id =3D txrsp->id; + id =3D txrsp.id; skb =3D queue->tx_skbs[id].skb; if (unlikely(gnttab_query_foreign_access( queue->grant_tx_ref[id]) !=3D 0)) { @@ -814,7 +814,7 @@ static int xennet_get_extras(struct netfront_queue *que= ue, RING_IDX rp) =20 { - struct xen_netif_extra_info *extra; + struct xen_netif_extra_info extra; struct device *dev =3D &queue->info->netdev->dev; RING_IDX cons =3D queue->rx.rsp_cons; int err =3D 0; @@ -830,24 +830,22 @@ static int xennet_get_extras(struct netfront_queue *q= ueue, break; } =20 - extra =3D (struct xen_netif_extra_info *) - RING_GET_RESPONSE(&queue->rx, ++cons); + RING_COPY_RESPONSE(&queue->rx, ++cons, &extra); =20 - if (unlikely(!extra->type || - extra->type >=3D XEN_NETIF_EXTRA_TYPE_MAX)) { + if (unlikely(!extra.type || + extra.type >=3D XEN_NETIF_EXTRA_TYPE_MAX)) { if (net_ratelimit()) dev_warn(dev, "Invalid extra type: %d\n", - extra->type); + extra.type); err =3D -EINVAL; } else { - memcpy(&extras[extra->type - 1], extra, - sizeof(*extra)); + extras[extra.type - 1] =3D extra; } =20 skb =3D xennet_get_rx_skb(queue, cons); ref =3D xennet_get_rx_ref(queue, cons); xennet_move_rx_slot(queue, skb, ref); - } while (extra->flags & XEN_NETIF_EXTRA_FLAG_MORE); + } while (extra.flags & XEN_NETIF_EXTRA_FLAG_MORE); =20 queue->rx.rsp_cons =3D cons; return err; @@ -905,7 +903,7 @@ static int xennet_get_responses(struct netfront_queue *= queue, struct sk_buff_head *list, bool *need_xdp_flush) { - struct xen_netif_rx_response *rx =3D &rinfo->rx; + struct xen_netif_rx_response *rx =3D &rinfo->rx, rx_local; int max =3D XEN_NETIF_NR_SLOTS_MIN + (rx->status <=3D RX_COPY_THRESHOLD); RING_IDX cons =3D queue->rx.rsp_cons; struct sk_buff *skb =3D xennet_get_rx_skb(queue, cons); @@ -989,7 +987,8 @@ static int xennet_get_responses(struct netfront_queue *= queue, break; } =20 - rx =3D RING_GET_RESPONSE(&queue->rx, cons + slots); + RING_COPY_RESPONSE(&queue->rx, cons + slots, &rx_local); + rx =3D &rx_local; skb =3D xennet_get_rx_skb(queue, cons + slots); ref =3D xennet_get_rx_ref(queue, cons + slots); slots++; @@ -1044,10 +1043,11 @@ static int xennet_fill_frags(struct netfront_queue = *queue, struct sk_buff *nskb; =20 while ((nskb =3D __skb_dequeue(list))) { - struct xen_netif_rx_response *rx =3D - RING_GET_RESPONSE(&queue->rx, ++cons); + struct xen_netif_rx_response rx; skb_frag_t *nfrag =3D &skb_shinfo(nskb)->frags[0]; =20 + RING_COPY_RESPONSE(&queue->rx, ++cons, &rx); + if (skb_shinfo(skb)->nr_frags =3D=3D MAX_SKB_FRAGS) { unsigned int pull_to =3D NETFRONT_SKB_CB(skb)->pull_to; =20 @@ -1062,7 +1062,7 @@ static int xennet_fill_frags(struct netfront_queue *q= ueue, =20 skb_add_rx_frag(skb, skb_shinfo(skb)->nr_frags, skb_frag_page(nfrag), - rx->offset, rx->status, PAGE_SIZE); + rx.offset, rx.status, PAGE_SIZE); =20 skb_shinfo(nskb)->nr_frags =3D 0; kfree_skb(nskb); @@ -1161,7 +1161,7 @@ static int xennet_poll(struct napi_struct *napi, int = budget) i =3D queue->rx.rsp_cons; work_done =3D 0; while ((i !=3D rp) && (work_done < budget)) { - memcpy(rx, RING_GET_RESPONSE(&queue->rx, i), sizeof(*rx)); + RING_COPY_RESPONSE(&queue->rx, i, rx); memset(extras, 0, sizeof(rinfo.extras)); =20 err =3D xennet_get_responses(queue, &rinfo, rp, &tmpq, --=20 2.26.2 From nobody Wed May 1 22:39:48 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=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1629800915; cv=none; d=zohomail.com; s=zohoarc; b=as8nltuvZHoUqgwSLp1Cz+nOILaC2lnS9diSGhyEoGcmTxGqfB+bgt/2U6odj7yM6hJHC5MScSmfdlx+zPa8GRlH5VR0ekeC30QeYB0B1vqf5yto46xNTRbS/T4s7cC4DYzybfAN6VHgHoj/thAT1ygCoS+ZXMU5t27lAelIo6U= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1629800915; h=Content-Transfer-Encoding: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=xzRnNXEpYNK6Fr5aUl3tWJGBJDdvhYuHIaqCmKzznYU=; b=BhZjG/27Y3fDCI0XKyhyokClTy54l/Tof4b+KEZE/kCIGNnLyv8y1xOGo/YOQAAaACfH81L4WcDs1lNkIRxqbp303Pc5Zr4AdQg5IEtP6sEGlp3i9dx/5ODsqCtdQ+6GY9kLVsWC5ayxg8DvkaeaO6m46VoivZRjGZvaVdgRb5g= 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1629800915554243.89129987070203; Tue, 24 Aug 2021 03:28:35 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.170855.311823 (Exim 4.92) (envelope-from ) id 1mITfG-0003IY-6S; Tue, 24 Aug 2021 10:28:18 +0000 Received: by outflank-mailman (output) from mailman id 170855.311823; Tue, 24 Aug 2021 10:28:18 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mITfG-0003IN-1I; Tue, 24 Aug 2021 10:28:18 +0000 Received: by outflank-mailman (input) for mailman id 170855; Tue, 24 Aug 2021 10:28:16 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mITfE-00031l-4Y for xen-devel@lists.xenproject.org; Tue, 24 Aug 2021 10:28:16 +0000 Received: from smtp-out1.suse.de (unknown [195.135.220.28]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 44d69b1b-7280-4e2d-a16d-190dcef9212e; Tue, 24 Aug 2021 10:28:14 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out1.suse.de (Postfix) with ESMTPS id EF470220AC; Tue, 24 Aug 2021 10:28:13 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap1.suse-dmz.suse.de (Postfix) with ESMTPS id AAFBB136DD; Tue, 24 Aug 2021 10:28:13 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id 6BJGKL3JJGG8DwAAGKfGzw (envelope-from ); Tue, 24 Aug 2021 10:28:13 +0000 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: 44d69b1b-7280-4e2d-a16d-190dcef9212e DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1629800893; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=xzRnNXEpYNK6Fr5aUl3tWJGBJDdvhYuHIaqCmKzznYU=; b=Q+8gdbEVImSGds7mmPaM4UehS+aTvl6Jy+7iX7DxXXhxkZgPpS4IOcdZd25ZzMRAmjW9kb n32wpEB0gsPm6SzdwQNp6wxpOyyxsoznmynuAgqxBufivgjhWLcd+aRn5E+MizuAVJZgcV QPa5WwxRjK/QKCMDzUCCm08y/G8KCEU= From: Juergen Gross To: xen-devel@lists.xenproject.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini , "David S. Miller" , Jakub Kicinski Subject: [PATCH v2 2/4] xen/netfront: don't read data from request on the ring page Date: Tue, 24 Aug 2021 12:28:07 +0200 Message-Id: <20210824102809.26370-3-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210824102809.26370-1-jgross@suse.com> References: <20210824102809.26370-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1629800917851100003 Content-Type: text/plain; charset="utf-8" In order to avoid a malicious backend being able to influence the local processing of a request build the request locally first and then copy it to the ring page. Any reading from the request influencing the processing in the frontend needs to be done on the local instance. Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich --- V2: - drop local tx variable (Jan Beulich) - fix oversight of reading value from ring page (Jan Beulich) --- drivers/net/xen-netfront.c | 86 +++++++++++++++++++------------------- 1 file changed, 42 insertions(+), 44 deletions(-) diff --git a/drivers/net/xen-netfront.c b/drivers/net/xen-netfront.c index 003cdf2ffc92..714fe9d2c534 100644 --- a/drivers/net/xen-netfront.c +++ b/drivers/net/xen-netfront.c @@ -435,7 +435,8 @@ struct xennet_gnttab_make_txreq { struct netfront_queue *queue; struct sk_buff *skb; struct page *page; - struct xen_netif_tx_request *tx; /* Last request */ + struct xen_netif_tx_request *tx; /* Last request on ring page */ + struct xen_netif_tx_request tx_local; /* Last request local copy*/ unsigned int size; }; =20 @@ -463,30 +464,27 @@ static void xennet_tx_setup_grant(unsigned long gfn, = unsigned int offset, queue->grant_tx_page[id] =3D page; queue->grant_tx_ref[id] =3D ref; =20 - tx->id =3D id; - tx->gref =3D ref; - tx->offset =3D offset; - tx->size =3D len; - tx->flags =3D 0; + info->tx_local.id =3D id; + info->tx_local.gref =3D ref; + info->tx_local.offset =3D offset; + info->tx_local.size =3D len; + info->tx_local.flags =3D 0; + + *tx =3D info->tx_local; =20 info->tx =3D tx; - info->size +=3D tx->size; + info->size +=3D info->tx_local.size; } =20 static struct xen_netif_tx_request *xennet_make_first_txreq( - struct netfront_queue *queue, struct sk_buff *skb, - struct page *page, unsigned int offset, unsigned int len) + struct xennet_gnttab_make_txreq *info, + unsigned int offset, unsigned int len) { - struct xennet_gnttab_make_txreq info =3D { - .queue =3D queue, - .skb =3D skb, - .page =3D page, - .size =3D 0, - }; + info->size =3D 0; =20 - gnttab_for_one_grant(page, offset, len, xennet_tx_setup_grant, &info); + gnttab_for_one_grant(info->page, offset, len, xennet_tx_setup_grant, info= ); =20 - return info.tx; + return info->tx; } =20 static void xennet_make_one_txreq(unsigned long gfn, unsigned int offset, @@ -499,35 +497,27 @@ static void xennet_make_one_txreq(unsigned long gfn, = unsigned int offset, xennet_tx_setup_grant(gfn, offset, len, data); } =20 -static struct xen_netif_tx_request *xennet_make_txreqs( - struct netfront_queue *queue, struct xen_netif_tx_request *tx, - struct sk_buff *skb, struct page *page, +static void xennet_make_txreqs( + struct xennet_gnttab_make_txreq *info, + struct page *page, unsigned int offset, unsigned int len) { - struct xennet_gnttab_make_txreq info =3D { - .queue =3D queue, - .skb =3D skb, - .tx =3D tx, - }; - /* Skip unused frames from start of page */ page +=3D offset >> PAGE_SHIFT; offset &=3D ~PAGE_MASK; =20 while (len) { - info.page =3D page; - info.size =3D 0; + info->page =3D page; + info->size =3D 0; =20 gnttab_foreach_grant_in_range(page, offset, len, xennet_make_one_txreq, - &info); + info); =20 page++; offset =3D 0; - len -=3D info.size; + len -=3D info->size; } - - return info.tx; } =20 /* @@ -580,10 +570,14 @@ static int xennet_xdp_xmit_one(struct net_device *dev, { struct netfront_info *np =3D netdev_priv(dev); struct netfront_stats *tx_stats =3D this_cpu_ptr(np->tx_stats); + struct xennet_gnttab_make_txreq info =3D { + .queue =3D queue, + .skb =3D NULL, + .page =3D virt_to_page(xdpf->data), + }; int notify; =20 - xennet_make_first_txreq(queue, NULL, - virt_to_page(xdpf->data), + xennet_make_first_txreq(&info, offset_in_page(xdpf->data), xdpf->len); =20 @@ -638,7 +632,7 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *sk= b, struct net_device *dev { struct netfront_info *np =3D netdev_priv(dev); struct netfront_stats *tx_stats =3D this_cpu_ptr(np->tx_stats); - struct xen_netif_tx_request *tx, *first_tx; + struct xen_netif_tx_request *first_tx; unsigned int i; int notify; int slots; @@ -647,6 +641,7 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *sk= b, struct net_device *dev unsigned int len; unsigned long flags; struct netfront_queue *queue =3D NULL; + struct xennet_gnttab_make_txreq info =3D { }; unsigned int num_queues =3D dev->real_num_tx_queues; u16 queue_index; struct sk_buff *nskb; @@ -704,21 +699,24 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *= skb, struct net_device *dev } =20 /* First request for the linear area. */ - first_tx =3D tx =3D xennet_make_first_txreq(queue, skb, - page, offset, len); - offset +=3D tx->size; + info.queue =3D queue; + info.skb =3D skb; + info.page =3D page; + first_tx =3D xennet_make_first_txreq(&info, offset, len); + offset +=3D info.tx_local.size; if (offset =3D=3D PAGE_SIZE) { page++; offset =3D 0; } - len -=3D tx->size; + len -=3D info.tx_local.size; =20 if (skb->ip_summed =3D=3D CHECKSUM_PARTIAL) /* local packet? */ - tx->flags |=3D XEN_NETTXF_csum_blank | XEN_NETTXF_data_validated; + first_tx->flags |=3D XEN_NETTXF_csum_blank | + XEN_NETTXF_data_validated; else if (skb->ip_summed =3D=3D CHECKSUM_UNNECESSARY) /* remote but checksummed. */ - tx->flags |=3D XEN_NETTXF_data_validated; + first_tx->flags |=3D XEN_NETTXF_data_validated; =20 /* Optional extra info after the first request. */ if (skb_shinfo(skb)->gso_size) { @@ -727,7 +725,7 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *sk= b, struct net_device *dev gso =3D (struct xen_netif_extra_info *) RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++); =20 - tx->flags |=3D XEN_NETTXF_extra_info; + first_tx->flags |=3D XEN_NETTXF_extra_info; =20 gso->u.gso.size =3D skb_shinfo(skb)->gso_size; gso->u.gso.type =3D (skb_shinfo(skb)->gso_type & SKB_GSO_TCPV6) ? @@ -741,12 +739,12 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *= skb, struct net_device *dev } =20 /* Requests for the rest of the linear area. */ - tx =3D xennet_make_txreqs(queue, tx, skb, page, offset, len); + xennet_make_txreqs(&info, page, offset, len); =20 /* Requests for all the frags. */ for (i =3D 0; i < skb_shinfo(skb)->nr_frags; i++) { skb_frag_t *frag =3D &skb_shinfo(skb)->frags[i]; - tx =3D xennet_make_txreqs(queue, tx, skb, skb_frag_page(frag), + xennet_make_txreqs(&info, skb_frag_page(frag), skb_frag_off(frag), skb_frag_size(frag)); } --=20 2.26.2 From nobody Wed May 1 22:39:48 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=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1629800922; cv=none; d=zohomail.com; s=zohoarc; b=HiPV9toBDBaaE8mgPVHxceYWVmHwIBef5wbjqcPbeNgk47l0vd6ncaep876/Ati+gufKcnQAA3ngeM1sELh06drfE8eQ/BNQDh8klpj/xE/V8ls0XE9ZeJKbaztoaYKEZwlSIllxaRi3akrDth7zG/qFcDInO1uDYsJ5vwm8WLM= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1629800922; h=Content-Transfer-Encoding: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=dRfHDlbGvJX0Rh3aH0nd0i0HxQzYtne2EnV2YC+Etxk=; b=dprVTkWsvpjz8XkbLClHcutCrMZj/LTpIrJ8jFKr0YXOu2TQ6FGgm9L0IQLAU9PCpb4IqhpIR+6U2ZaqIRL8fLwAiFKTPo+EHGv5iFW+sfamttIAOgivONAM4tKN/e9BkY+/P1RXIWl3wf0ot8AZ6AFqbwd4RzCzxKXltIe5UuA= 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 1629800922225342.6021907568895; Tue, 24 Aug 2021 03:28:42 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.170858.311856 (Exim 4.92) (envelope-from ) id 1mITfQ-0004L3-7N; Tue, 24 Aug 2021 10:28:28 +0000 Received: by outflank-mailman (output) from mailman id 170858.311856; Tue, 24 Aug 2021 10:28:28 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mITfQ-0004Ku-2u; Tue, 24 Aug 2021 10:28:28 +0000 Received: by outflank-mailman (input) for mailman id 170858; Tue, 24 Aug 2021 10:28:26 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mITfO-00031l-3n for xen-devel@lists.xenproject.org; Tue, 24 Aug 2021 10:28:26 +0000 Received: from smtp-out2.suse.de (unknown [195.135.220.29]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 84f74615-da8a-4f51-8b77-82161ad5c47f; Tue, 24 Aug 2021 10:28:15 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 4AF9E20050; Tue, 24 Aug 2021 10:28:14 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 048EE136DD; Tue, 24 Aug 2021 10:28:13 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id aKYuO73JJGG8DwAAGKfGzw (envelope-from ); Tue, 24 Aug 2021 10:28:13 +0000 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: 84f74615-da8a-4f51-8b77-82161ad5c47f DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1629800894; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=dRfHDlbGvJX0Rh3aH0nd0i0HxQzYtne2EnV2YC+Etxk=; b=WA3W65tQDCGvZ6HWHIGUJZAQZYk/ziLqp7BG4E0U2Kn9UPepZzumKpmbZ0uW+XGqsGRDGm hc3woFMVOqUqQY8GKTWR9k337NFfSSOWEWki45zkGpCXg4Tdq40AaS6+pew3yTMN51P8xQ BAUavhaDsivS2qpG2Z9jGgMWI7k70rk= From: Juergen Gross To: xen-devel@lists.xenproject.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini , "David S. Miller" , Jakub Kicinski Subject: [PATCH v2 3/4] xen/netfront: disentangle tx_skb_freelist Date: Tue, 24 Aug 2021 12:28:08 +0200 Message-Id: <20210824102809.26370-4-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210824102809.26370-1-jgross@suse.com> References: <20210824102809.26370-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1629800924249100002 Content-Type: text/plain; charset="utf-8" The tx_skb_freelist elements are in a single linked list with the request id used as link reference. The per element link field is in a union with the skb pointer of an in use request. Move the link reference out of the union in order to enable a later reuse of it for requests which need a populated skb pointer. Rename add_id_to_freelist() and get_id_from_freelist() to add_id_to_list() and get_id_from_list() in order to prepare using those for other lists as well. Define ~0 as value to indicate the end of a list and place that value into the link for a request not being on the list. When freeing a skb zero the skb pointer in the request. Use a NULL value of the skb pointer instead of skb_entry_is_link() for deciding whether a request has a skb linked to it. Remove skb_entry_set_link() and open code it instead as it is really trivial now. Signed-off-by: Juergen Gross --- V2: - new patch --- drivers/net/xen-netfront.c | 61 ++++++++++++++++---------------------- 1 file changed, 25 insertions(+), 36 deletions(-) diff --git a/drivers/net/xen-netfront.c b/drivers/net/xen-netfront.c index 714fe9d2c534..956e1266bd1a 100644 --- a/drivers/net/xen-netfront.c +++ b/drivers/net/xen-netfront.c @@ -126,17 +126,11 @@ struct netfront_queue { =20 /* * {tx,rx}_skbs store outstanding skbuffs. Free tx_skb entries - * are linked from tx_skb_freelist through skb_entry.link. - * - * NB. Freelist index entries are always going to be less than - * PAGE_OFFSET, whereas pointers to skbs will always be equal or - * greater than PAGE_OFFSET: we use this property to distinguish - * them. + * are linked from tx_skb_freelist through tx_link. */ - union skb_entry { - struct sk_buff *skb; - unsigned long link; - } tx_skbs[NET_TX_RING_SIZE]; + struct sk_buff *tx_skbs[NET_TX_RING_SIZE]; + unsigned short tx_link[NET_TX_RING_SIZE]; +#define TX_LINK_NONE 0xffff grant_ref_t gref_tx_head; grant_ref_t grant_tx_ref[NET_TX_RING_SIZE]; struct page *grant_tx_page[NET_TX_RING_SIZE]; @@ -181,33 +175,25 @@ struct netfront_rx_info { struct xen_netif_extra_info extras[XEN_NETIF_EXTRA_TYPE_MAX - 1]; }; =20 -static void skb_entry_set_link(union skb_entry *list, unsigned short id) -{ - list->link =3D id; -} - -static int skb_entry_is_link(const union skb_entry *list) -{ - BUILD_BUG_ON(sizeof(list->skb) !=3D sizeof(list->link)); - return (unsigned long)list->skb < PAGE_OFFSET; -} - /* * Access macros for acquiring freeing slots in tx_skbs[]. */ =20 -static void add_id_to_freelist(unsigned *head, union skb_entry *list, - unsigned short id) +static void add_id_to_list(unsigned *head, unsigned short *list, + unsigned short id) { - skb_entry_set_link(&list[id], *head); + list[id] =3D *head; *head =3D id; } =20 -static unsigned short get_id_from_freelist(unsigned *head, - union skb_entry *list) +static unsigned short get_id_from_list(unsigned *head, unsigned short *lis= t) { unsigned int id =3D *head; - *head =3D list[id].link; + + if (id !=3D TX_LINK_NONE) { + *head =3D list[id]; + list[id] =3D TX_LINK_NONE; + } return id; } =20 @@ -406,7 +392,8 @@ static void xennet_tx_buf_gc(struct netfront_queue *que= ue) continue; =20 id =3D txrsp.id; - skb =3D queue->tx_skbs[id].skb; + skb =3D queue->tx_skbs[id]; + queue->tx_skbs[id] =3D NULL; if (unlikely(gnttab_query_foreign_access( queue->grant_tx_ref[id]) !=3D 0)) { pr_alert("%s: warning -- grant still in use by backend domain\n", @@ -419,7 +406,7 @@ static void xennet_tx_buf_gc(struct netfront_queue *que= ue) &queue->gref_tx_head, queue->grant_tx_ref[id]); queue->grant_tx_ref[id] =3D GRANT_INVALID_REF; queue->grant_tx_page[id] =3D NULL; - add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, id); + add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, id); dev_kfree_skb_irq(skb); } =20 @@ -452,7 +439,7 @@ static void xennet_tx_setup_grant(unsigned long gfn, un= signed int offset, struct netfront_queue *queue =3D info->queue; struct sk_buff *skb =3D info->skb; =20 - id =3D get_id_from_freelist(&queue->tx_skb_freelist, queue->tx_skbs); + id =3D get_id_from_list(&queue->tx_skb_freelist, queue->tx_link); tx =3D RING_GET_REQUEST(&queue->tx, queue->tx.req_prod_pvt++); ref =3D gnttab_claim_grant_reference(&queue->gref_tx_head); WARN_ON_ONCE(IS_ERR_VALUE((unsigned long)(int)ref)); @@ -460,7 +447,7 @@ static void xennet_tx_setup_grant(unsigned long gfn, un= signed int offset, gnttab_grant_foreign_access_ref(ref, queue->info->xbdev->otherend_id, gfn, GNTMAP_readonly); =20 - queue->tx_skbs[id].skb =3D skb; + queue->tx_skbs[id] =3D skb; queue->grant_tx_page[id] =3D page; queue->grant_tx_ref[id] =3D ref; =20 @@ -1284,17 +1271,18 @@ static void xennet_release_tx_bufs(struct netfront_= queue *queue) =20 for (i =3D 0; i < NET_TX_RING_SIZE; i++) { /* Skip over entries which are actually freelist references */ - if (skb_entry_is_link(&queue->tx_skbs[i])) + if (!queue->tx_skbs[i]) continue; =20 - skb =3D queue->tx_skbs[i].skb; + skb =3D queue->tx_skbs[i]; + queue->tx_skbs[i] =3D NULL; get_page(queue->grant_tx_page[i]); gnttab_end_foreign_access(queue->grant_tx_ref[i], GNTMAP_readonly, (unsigned long)page_address(queue->grant_tx_page[i])); queue->grant_tx_page[i] =3D NULL; queue->grant_tx_ref[i] =3D GRANT_INVALID_REF; - add_id_to_freelist(&queue->tx_skb_freelist, queue->tx_skbs, i); + add_id_to_list(&queue->tx_skb_freelist, queue->tx_link, i); dev_kfree_skb_irq(skb); } } @@ -1851,13 +1839,14 @@ static int xennet_init_queue(struct netfront_queue = *queue) snprintf(queue->name, sizeof(queue->name), "vif%s-q%u", devid, queue->id); =20 - /* Initialise tx_skbs as a free chain containing every entry. */ + /* Initialise tx_skb_freelist as a free chain containing every entry. */ queue->tx_skb_freelist =3D 0; for (i =3D 0; i < NET_TX_RING_SIZE; i++) { - skb_entry_set_link(&queue->tx_skbs[i], i+1); + queue->tx_link[i] =3D i + 1; queue->grant_tx_ref[i] =3D GRANT_INVALID_REF; queue->grant_tx_page[i] =3D NULL; } + queue->tx_link[NET_TX_RING_SIZE - 1] =3D TX_LINK_NONE; =20 /* Clear out rx_skbs */ for (i =3D 0; i < NET_RX_RING_SIZE; i++) { --=20 2.26.2 From nobody Wed May 1 22:39:49 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=quarantine dis=none) header.from=suse.com ARC-Seal: i=1; a=rsa-sha256; t=1629800919; cv=none; d=zohomail.com; s=zohoarc; b=UPU7RvUinyN0oq3YSEOO33J2Iy5csggMPlJCAyFkzlMx4s1OhA6xA4mVJBj7goqLdaTXYegzCh3qBO3l7SNDuUIQEB+oeOcPWAt2bxlF4D/5wJ21EQnqY/hxg82993UqDVGlKjAKy2sJpTidLd1hZnNkDAZn0qzLYJsmF5jXJbY= ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=zohomail.com; s=zohoarc; t=1629800919; h=Content-Transfer-Encoding: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=nfhixkZoKYt7ltZuGvoMSJv4YyU9Ov7I0dmOxqpdsKE=; b=mK/uELl6sgblwb5AfrkMyxpMIRnRnedhIMn4v0MjKR6iSqess/TCQco2KwU+fZ77JjhNuguIDhwGXTZS+7llTxo9xcaSTwv9VEaOnEkhlyfO1jWMa/IdWXPMeSg2/+kCtWmYZtcNT1yLHZHFYtECUs/LdGKPmn035F1vU1rpqBc= 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=quarantine dis=none) Return-Path: Received: from lists.xenproject.org (lists.xenproject.org [192.237.175.120]) by mx.zohomail.com with SMTPS id 162980091945570.34596849410411; Tue, 24 Aug 2021 03:28:39 -0700 (PDT) Received: from list by lists.xenproject.org with outflank-mailman.170857.311845 (Exim 4.92) (envelope-from ) id 1mITfK-0003uT-Tt; Tue, 24 Aug 2021 10:28:22 +0000 Received: by outflank-mailman (output) from mailman id 170857.311845; Tue, 24 Aug 2021 10:28:22 +0000 Received: from localhost ([127.0.0.1] helo=lists.xenproject.org) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mITfK-0003uG-Q8; Tue, 24 Aug 2021 10:28:22 +0000 Received: by outflank-mailman (input) for mailman id 170857; Tue, 24 Aug 2021 10:28:21 +0000 Received: from us1-rack-iad1.inumbo.com ([172.99.69.81]) by lists.xenproject.org with esmtp (Exim 4.92) (envelope-from ) id 1mITfJ-00031l-3a for xen-devel@lists.xenproject.org; Tue, 24 Aug 2021 10:28:21 +0000 Received: from smtp-out2.suse.de (unknown [195.135.220.29]) by us1-rack-iad1.inumbo.com (Halon) with ESMTPS id 74f56f79-319b-4012-a878-2fb3c7833876; Tue, 24 Aug 2021 10:28:15 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by smtp-out2.suse.de (Postfix) with ESMTPS id 969A920051; Tue, 24 Aug 2021 10:28:14 +0000 (UTC) Received: from imap1.suse-dmz.suse.de (imap1.suse-dmz.suse.de [192.168.254.73]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature ECDSA (P-521) server-digest SHA512) (No client certificate requested) by imap1.suse-dmz.suse.de (Postfix) with ESMTPS id 52545136DD; Tue, 24 Aug 2021 10:28:14 +0000 (UTC) Received: from dovecot-director2.suse.de ([192.168.254.65]) by imap1.suse-dmz.suse.de with ESMTPSA id yDeiEr7JJGG8DwAAGKfGzw (envelope-from ); Tue, 24 Aug 2021 10:28:14 +0000 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: 74f56f79-319b-4012-a878-2fb3c7833876 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=suse.com; s=susede1; t=1629800894; h=from:from:reply-to:date:date:message-id:message-id:to:to:cc:cc: mime-version:mime-version: content-transfer-encoding:content-transfer-encoding: in-reply-to:in-reply-to:references:references; bh=nfhixkZoKYt7ltZuGvoMSJv4YyU9Ov7I0dmOxqpdsKE=; b=DcR+51AtjNOu/RurWnkc4/qcw2RfDI2dJrDj/kJHKxCouGLQNJhFK50ZaZBZasJVpbTU7Q iqFhF5ojLAljx7+nVh/UOfGJnk12i3tue4uY5AJkjdNeDxN1IDCzpRXe0jh3onS3kfaPuX sIsCN8m06Dkp46Gon3fUJYQTn0DLNLo= From: Juergen Gross To: xen-devel@lists.xenproject.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org Cc: Juergen Gross , Boris Ostrovsky , Stefano Stabellini , "David S. Miller" , Jakub Kicinski Subject: [PATCH v2 4/4] xen/netfront: don't trust the backend response data blindly Date: Tue, 24 Aug 2021 12:28:09 +0200 Message-Id: <20210824102809.26370-5-jgross@suse.com> X-Mailer: git-send-email 2.26.2 In-Reply-To: <20210824102809.26370-1-jgross@suse.com> References: <20210824102809.26370-1-jgross@suse.com> MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-ZohoMail-DKIM: pass (identity @suse.com) X-ZM-MESSAGEID: 1629800920061100001 Content-Type: text/plain; charset="utf-8" Today netfront will trust the backend to send only sane response data. In order to avoid privilege escalations or crashes in case of malicious backends verify the data to be within expected limits. Especially make sure that the response always references an outstanding request. Note that only the tx queue needs special id handling, as for the rx queue the id is equal to the index in the ring page. Introduce a new indicator for the device whether it is broken and let the device stop working when it is set. Set this indicator in case the backend sets any weird data. Signed-off-by: Juergen Gross Reviewed-by: Jan Beulich --- V2: - set the pending flag only just before sending the request (Jan Beulich) - reset broken indicator during connect (Jan Beulich) --- drivers/net/xen-netfront.c | 89 +++++++++++++++++++++++++++++++++++--- 1 file changed, 84 insertions(+), 5 deletions(-) diff --git a/drivers/net/xen-netfront.c b/drivers/net/xen-netfront.c index 956e1266bd1a..e31b98403f31 100644 --- a/drivers/net/xen-netfront.c +++ b/drivers/net/xen-netfront.c @@ -131,10 +131,12 @@ struct netfront_queue { struct sk_buff *tx_skbs[NET_TX_RING_SIZE]; unsigned short tx_link[NET_TX_RING_SIZE]; #define TX_LINK_NONE 0xffff +#define TX_PENDING 0xfffe grant_ref_t gref_tx_head; grant_ref_t grant_tx_ref[NET_TX_RING_SIZE]; struct page *grant_tx_page[NET_TX_RING_SIZE]; unsigned tx_skb_freelist; + unsigned int tx_pend_queue; =20 spinlock_t rx_lock ____cacheline_aligned_in_smp; struct xen_netif_rx_front_ring rx; @@ -167,6 +169,9 @@ struct netfront_info { bool netback_has_xdp_headroom; bool netfront_xdp_enabled; =20 + /* Is device behaving sane? */ + bool broken; + atomic_t rx_gso_checksum_fixup; }; =20 @@ -349,7 +354,7 @@ static int xennet_open(struct net_device *dev) unsigned int i =3D 0; struct netfront_queue *queue =3D NULL; =20 - if (!np->queues) + if (!np->queues || np->broken) return -ENODEV; =20 for (i =3D 0; i < num_queues; ++i) { @@ -377,11 +382,17 @@ static void xennet_tx_buf_gc(struct netfront_queue *q= ueue) unsigned short id; struct sk_buff *skb; bool more_to_do; + const struct device *dev =3D &queue->info->netdev->dev; =20 BUG_ON(!netif_carrier_ok(queue->info->netdev)); =20 do { prod =3D queue->tx.sring->rsp_prod; + if (RING_RESPONSE_PROD_OVERFLOW(&queue->tx, prod)) { + dev_alert(dev, "Illegal number of responses %u\n", + prod - queue->tx.rsp_cons); + goto err; + } rmb(); /* Ensure we see responses up to 'rp'. */ =20 for (cons =3D queue->tx.rsp_cons; cons !=3D prod; cons++) { @@ -391,14 +402,27 @@ static void xennet_tx_buf_gc(struct netfront_queue *q= ueue) if (txrsp.status =3D=3D XEN_NETIF_RSP_NULL) continue; =20 - id =3D txrsp.id; + id =3D txrsp.id; + if (id >=3D RING_SIZE(&queue->tx)) { + dev_alert(dev, + "Response has incorrect id (%u)\n", + id); + goto err; + } + if (queue->tx_link[id] !=3D TX_PENDING) { + dev_alert(dev, + "Response for inactive request\n"); + goto err; + } + + queue->tx_link[id] =3D TX_LINK_NONE; skb =3D queue->tx_skbs[id]; queue->tx_skbs[id] =3D NULL; if (unlikely(gnttab_query_foreign_access( queue->grant_tx_ref[id]) !=3D 0)) { - pr_alert("%s: warning -- grant still in use by backend domain\n", - __func__); - BUG(); + dev_alert(dev, + "Grant still in use by backend domain\n"); + goto err; } gnttab_end_foreign_access_ref( queue->grant_tx_ref[id], GNTMAP_readonly); @@ -416,6 +440,12 @@ static void xennet_tx_buf_gc(struct netfront_queue *qu= eue) } while (more_to_do); =20 xennet_maybe_wake_tx(queue); + + return; + + err: + queue->info->broken =3D true; + dev_alert(dev, "Disabled for further use\n"); } =20 struct xennet_gnttab_make_txreq { @@ -459,6 +489,12 @@ static void xennet_tx_setup_grant(unsigned long gfn, u= nsigned int offset, =20 *tx =3D info->tx_local; =20 + /* + * Put the request in the pending queue, it will be set to be pending + * when the producer index is about to be raised. + */ + add_id_to_list(&queue->tx_pend_queue, queue->tx_link, id); + info->tx =3D tx; info->size +=3D info->tx_local.size; } @@ -551,6 +587,15 @@ static u16 xennet_select_queue(struct net_device *dev,= struct sk_buff *skb, return queue_idx; } =20 +static void xennet_mark_tx_pending(struct netfront_queue *queue) +{ + unsigned int i; + + while ((i =3D get_id_from_list(&queue->tx_pend_queue, queue->tx_link)) != =3D + TX_LINK_NONE) + queue->tx_link[i] =3D TX_PENDING; +} + static int xennet_xdp_xmit_one(struct net_device *dev, struct netfront_queue *queue, struct xdp_frame *xdpf) @@ -568,6 +613,8 @@ static int xennet_xdp_xmit_one(struct net_device *dev, offset_in_page(xdpf->data), xdpf->len); =20 + xennet_mark_tx_pending(queue); + RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify); if (notify) notify_remote_via_irq(queue->tx_irq); @@ -592,6 +639,8 @@ static int xennet_xdp_xmit(struct net_device *dev, int = n, int nxmit =3D 0; int i; =20 + if (unlikely(np->broken)) + return -ENODEV; if (unlikely(flags & ~XDP_XMIT_FLAGS_MASK)) return -EINVAL; =20 @@ -636,6 +685,8 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *sk= b, struct net_device *dev /* Drop the packet if no queues are set up */ if (num_queues < 1) goto drop; + if (unlikely(np->broken)) + goto drop; /* Determine which queue to transmit this SKB on */ queue_index =3D skb_get_queue_mapping(skb); queue =3D &np->queues[queue_index]; @@ -742,6 +793,8 @@ static netdev_tx_t xennet_start_xmit(struct sk_buff *sk= b, struct net_device *dev /* timestamp packet in software */ skb_tx_timestamp(skb); =20 + xennet_mark_tx_pending(queue); + RING_PUSH_REQUESTS_AND_CHECK_NOTIFY(&queue->tx, notify); if (notify) notify_remote_via_irq(queue->tx_irq); @@ -1141,6 +1194,13 @@ static int xennet_poll(struct napi_struct *napi, int= budget) skb_queue_head_init(&tmpq); =20 rp =3D queue->rx.sring->rsp_prod; + if (RING_RESPONSE_PROD_OVERFLOW(&queue->rx, rp)) { + dev_alert(&dev->dev, "Illegal number of responses %u\n", + rp - queue->rx.rsp_cons); + queue->info->broken =3D true; + spin_unlock(&queue->rx_lock); + return 0; + } rmb(); /* Ensure we see queued responses up to 'rp'. */ =20 i =3D queue->rx.rsp_cons; @@ -1362,6 +1422,9 @@ static irqreturn_t xennet_tx_interrupt(int irq, void = *dev_id) struct netfront_queue *queue =3D dev_id; unsigned long flags; =20 + if (queue->info->broken) + return IRQ_HANDLED; + spin_lock_irqsave(&queue->tx_lock, flags); xennet_tx_buf_gc(queue); spin_unlock_irqrestore(&queue->tx_lock, flags); @@ -1374,6 +1437,9 @@ static irqreturn_t xennet_rx_interrupt(int irq, void = *dev_id) struct netfront_queue *queue =3D dev_id; struct net_device *dev =3D queue->info->netdev; =20 + if (queue->info->broken) + return IRQ_HANDLED; + if (likely(netif_carrier_ok(dev) && RING_HAS_UNCONSUMED_RESPONSES(&queue->rx))) napi_schedule(&queue->napi); @@ -1395,6 +1461,10 @@ static void xennet_poll_controller(struct net_device= *dev) struct netfront_info *info =3D netdev_priv(dev); unsigned int num_queues =3D dev->real_num_tx_queues; unsigned int i; + + if (info->broken) + return; + for (i =3D 0; i < num_queues; ++i) xennet_interrupt(0, &info->queues[i]); } @@ -1466,6 +1536,11 @@ static int xennet_xdp_set(struct net_device *dev, st= ruct bpf_prog *prog, =20 static int xennet_xdp(struct net_device *dev, struct netdev_bpf *xdp) { + struct netfront_info *np =3D netdev_priv(dev); + + if (np->broken) + return -ENODEV; + switch (xdp->command) { case XDP_SETUP_PROG: return xennet_xdp_set(dev, xdp->prog, xdp->extack); @@ -1841,6 +1916,7 @@ static int xennet_init_queue(struct netfront_queue *q= ueue) =20 /* Initialise tx_skb_freelist as a free chain containing every entry. */ queue->tx_skb_freelist =3D 0; + queue->tx_pend_queue =3D TX_LINK_NONE; for (i =3D 0; i < NET_TX_RING_SIZE; i++) { queue->tx_link[i] =3D i + 1; queue->grant_tx_ref[i] =3D GRANT_INVALID_REF; @@ -2115,6 +2191,9 @@ static int talk_to_netback(struct xenbus_device *dev, if (info->queues) xennet_destroy_queues(info); =20 + /* For the case of a reconnect reset the "broken" indicator. */ + info->broken =3D false; + err =3D xennet_create_queues(info, &num_queues); if (err < 0) { xenbus_dev_fatal(dev, err, "creating queues"); --=20 2.26.2