From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.0 (2014-02-07) on aws-us-west-2-korg-lkml-1.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-4.0 required=3.0 tests=HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,SIGNED_OFF_BY,SPF_PASS,URIBL_BLOCKED autolearn=ham autolearn_force=no version=3.4.0 Received: from mail.kernel.org (mail.kernel.org [198.145.29.99]) by smtp.lore.kernel.org (Postfix) with ESMTP id E6E19C43387 for ; Fri, 18 Jan 2019 01:57:17 +0000 (UTC) Received: from vger.kernel.org (vger.kernel.org [209.132.180.67]) by mail.kernel.org (Postfix) with ESMTP id BCBF520657 for ; Fri, 18 Jan 2019 01:57:17 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1726999AbfARB5Q (ORCPT ); Thu, 17 Jan 2019 20:57:16 -0500 Received: from tama50.ecl.ntt.co.jp ([129.60.39.147]:54999 "EHLO tama50.ecl.ntt.co.jp" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1726329AbfARB5Q (ORCPT ); Thu, 17 Jan 2019 20:57:16 -0500 Received: from vc2.ecl.ntt.co.jp (vc2.ecl.ntt.co.jp [129.60.86.154]) by tama50.ecl.ntt.co.jp (8.13.8/8.13.8) with ESMTP id x0I1vAcD000530; Fri, 18 Jan 2019 10:57:10 +0900 Received: from vc2.ecl.ntt.co.jp (localhost [127.0.0.1]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id 03764638616; Fri, 18 Jan 2019 10:57:10 +0900 (JST) Received: from jcms-pop21.ecl.ntt.co.jp (jcms-pop21.ecl.ntt.co.jp [129.60.87.134]) by vc2.ecl.ntt.co.jp (Postfix) with ESMTP id DD0716385E2; Fri, 18 Jan 2019 10:57:09 +0900 (JST) Received: from [IPv6:::1] (unknown [129.60.241.182]) by jcms-pop21.ecl.ntt.co.jp (Postfix) with ESMTPSA id CF24F400D1A; Fri, 18 Jan 2019 10:57:09 +0900 (JST) Subject: Re: [PATCH net 5/7] virtio_net: Don't process redirected XDP frames when XDP is disabled References: <1547724045-2726-1-git-send-email-makita.toshiaki@lab.ntt.co.jp> <1547724045-2726-6-git-send-email-makita.toshiaki@lab.ntt.co.jp> <24359f65-fe45-46d1-5344-f0cc4705009b@redhat.com> From: Toshiaki Makita Message-ID: Date: Fri, 18 Jan 2019 10:56:27 +0900 User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:60.0) Gecko/20100101 Thunderbird/60.4.0 MIME-Version: 1.0 In-Reply-To: <24359f65-fe45-46d1-5344-f0cc4705009b@redhat.com> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 8bit X-CC-Mail-RelayStamp: 1 To: Jason Wang , "David S. Miller" , "Michael S. Tsirkin" Cc: netdev@vger.kernel.org, virtualization@lists.linux-foundation.org, Jesper Dangaard Brouer X-TM-AS-MML: disable Sender: netdev-owner@vger.kernel.org Precedence: bulk List-ID: X-Mailing-List: netdev@vger.kernel.org On 2019/01/17 22:05, Jason Wang wrote: > On 2019/1/17 下午8:53, Jason Wang wrote: >> On 2019/1/17 下午7:20, Toshiaki Makita wrote: >>> Commit 8dcc5b0ab0ec ("virtio_net: fix ndo_xdp_xmit crash towards dev not >>> ready for XDP") tried to avoid access to unexpected sq while XDP is >>> disabled, but was not complete. >>> >>> There was a small window which causes out of bounds sq access in >>> virtnet_xdp_xmit() while disabling XDP. >>> >>> An example case of >>>   - curr_queue_pairs = 6 (2 for SKB and 4 for XDP) >>>   - online_cpu_num = xdp_queue_paris = 4 >>> when XDP is enabled: >>> >>> CPU 0                         CPU 1 >>> (Disabling XDP)               (Processing redirected XDP frames) >>> >>>                                virtnet_xdp_xmit() >>> virtnet_xdp_set() >>>   _virtnet_set_queues() >>>    set curr_queue_pairs (2) >>>                                 check if rq->xdp_prog is not NULL >>>                                 virtnet_xdp_sq(vi) >>>                                  qp = curr_queue_pairs - >>>                                       xdp_queue_pairs + >>>                                       smp_processor_id() >>>                                     = 2 - 4 + 1 = -1 >>>                                  sq = &vi->sq[qp] // out of bounds >>> access >>>    set xdp_queue_pairs (0) >>>    rq->xdp_prog = NULL >>> >>> Basically we should not change curr_queue_pairs and xdp_queue_pairs >>> while someone can read the values. Thus, when disabling XDP, assign NULL >>> to rq->xdp_prog first, and wait for RCU grace period, then change >>> xxx_queue_pairs. >>> Note that we need to keep the current order when enabling XDP though. >>> >>> Fixes: 186b3c998c50 ("virtio-net: support XDP_REDIRECT") >>> Signed-off-by: Toshiaki Makita >> >> >> I wonder whether or not we could simply do: >> >> >> if (prog) { > > > Should be !prog > > >> >>     rcu_assign_pointer() >> >>     synchronize_net() >> >> } >> >> set queues >> >> if (!prog) { > > > Should be prog. Either would work. With your suggestion the code will look like: --- if (!prog) { for (...) { rcu_assign_pointer(); ... } synchronize_net(); } virtnet_set_queues(); netif_set_real_num_rx_queues(); vi->xdp_queue_pairs = xdp_qp; if (prog) { for (...) { rcu_assign_pointer(); ... } } --- But strictly speaking, virtnet_set_queues() should not be necessary if (prog != NULL && old_prog != NULL). If you prefer this, I can modify it accordingly. -- Toshiaki Makita