Received: by 2002:ac0:da4c:0:0:0:0:0 with SMTP id a12csp2589889imi; Mon, 25 Jul 2022 02:50:02 -0700 (PDT) X-Google-Smtp-Source: AGRyM1sg8+hLbQU7HrWGxM7oRV3U4arBvVQgqhhNBPlBvd3Sq75IRQaNEfEN/5q3ZLQizNRozo9X X-Received: by 2002:a17:906:9c82:b0:6e1:1d6c:914c with SMTP id fj2-20020a1709069c8200b006e11d6c914cmr9216247ejc.769.1658742602482; Mon, 25 Jul 2022 02:50:02 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1658742602; cv=none; d=google.com; s=arc-20160816; b=aVComhHEowTdQaS0htWDT+BEOiAjodat0g4B/WN1mpYMoZV9XX1il5RqwYdnIE4ZtP z5O8SgVS1xxXr3S1ODdjm1Zu466p9QZlSswl64LAvLgITbJZus4p/mPILPmNJnpfHuew PAeLipnCj656FuUlnVgwcaR5h+MF3FrG+Y2qC5fuCGQLgr4af8Re5o4FJZPWUr+agDmF HpQxmyxh05As6f1vIAWnafIfJmEvUr3B6d1398fpZawa+RAkDleV0sdf9KWhCEcdWz/B ly+RNyCVl9cXQQjxROmIdVIVHeAWXLv8bXSybTmuGWH2G25mDYKX8ticK74wk2MtMB+I 3CXQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:in-reply-to:references:cc:to:from:date:subject :message-id; bh=u1wVDW+lEZzTVF7adYZBfOqkgq7chcPWXYHYR8MFcZo=; b=H4AMHa4lrTrSfkSd2AkUpjh8ov2IAfucePsrvCxduLr6jNkSBaKV+AZEOnhdjfrcX8 He0Bp9WaSo27iv32Q/e2EXw3XIjM6hl+jQIwrzKYrnhTnc/TGKBa0km1yrZaQ7+Ax8AV EAuRS6/+632lS0NQxh/nzzc8rGXaUfSvLY289lq+8Na5Gn/Rs8g3wstBWj9WK8Pfd9/E xW78sSIdjTGPyKMPBV/Onl0dc5hr4kPNEcViPF2NhpzJE3+aMWe4xUyJx3o6pUTzakiK 7GpTCYaYkHjQP/17hME5ehWD0iFOfI5ayuMSQqTDdgzhUitOSs6R5zHEMCdHpijjlWj9 K9Cg== ARC-Authentication-Results: i=1; mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id j8-20020aa7c0c8000000b0043be98925a2si3149350edp.499.2022.07.25.02.49.38; Mon, 25 Jul 2022 02:50:02 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=fail (p=NONE sp=NONE dis=NONE) header.from=alibaba.com Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S234196AbiGYJjR (ORCPT + 99 others); Mon, 25 Jul 2022 05:39:17 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44114 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S234192AbiGYJjQ (ORCPT ); Mon, 25 Jul 2022 05:39:16 -0400 Received: from out30-56.freemail.mail.aliyun.com (out30-56.freemail.mail.aliyun.com [115.124.30.56]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id E9058DF18; Mon, 25 Jul 2022 02:39:13 -0700 (PDT) X-Alimail-AntiSpam: AC=PASS;BC=-1|-1;BR=01201311R621e4;CH=green;DM=||false|;DS=||;FP=0|-1|-1|-1|0|-1|-1|-1;HT=ay29a033018046051;MF=xuanzhuo@linux.alibaba.com;NM=1;PH=DS;RN=9;SR=0;TI=SMTPD_---0VKLhpcP_1658741950; Received: from localhost(mailfrom:xuanzhuo@linux.alibaba.com fp:SMTPD_---0VKLhpcP_1658741950) by smtp.aliyun-inc.com; Mon, 25 Jul 2022 17:39:10 +0800 Message-ID: <1658741940.4607933-1-xuanzhuo@linux.alibaba.com> Subject: Re: [PATCH V6] virtio-net: fix the race between refill work and close Date: Mon, 25 Jul 2022 17:39:00 +0800 From: Xuan Zhuo To: Jason Wang Cc: virtualization@lists.linux-foundation.org, netdev@vger.kernel.org, linux-kernel@vger.kernel.org, Jason Wang , mst@redhat.com, davem@davemloft.net, edumazet@google.com, kuba@kernel.org, pabeni@redhat.com References: <20220725072159.3577-1-jasowang@redhat.com> In-Reply-To: <20220725072159.3577-1-jasowang@redhat.com> X-Spam-Status: No, score=-9.9 required=5.0 tests=BAYES_00, ENV_AND_HDR_SPF_MATCH,RCVD_IN_DNSWL_NONE,RCVD_IN_MSPIKE_H2, SPF_HELO_NONE,SPF_PASS,UNPARSEABLE_RELAY,USER_IN_DEF_SPF_WL autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, 25 Jul 2022 15:21:59 +0800, Jason Wang wrote: > We try using cancel_delayed_work_sync() to prevent the work from > enabling NAPI. This is insufficient since we don't disable the source > of the refill work scheduling. This means an NAPI poll callback after > cancel_delayed_work_sync() can schedule the refill work then can > re-enable the NAPI that leads to use-after-free [1]. > > Since the work can enable NAPI, we can't simply disable NAPI before > calling cancel_delayed_work_sync(). So fix this by introducing a > dedicated boolean to control whether or not the work could be > scheduled from NAPI. > > [1] > ================================================================== > BUG: KASAN: use-after-free in refill_work+0x43/0xd4 > Read of size 2 at addr ffff88810562c92e by task kworker/2:1/42 > > CPU: 2 PID: 42 Comm: kworker/2:1 Not tainted 5.19.0-rc1+ #480 > Hardware name: QEMU Standard PC (Q35 + ICH9, 2009), BIOS rel-1.16.0-0-gd239552ce722-prebuilt.qemu.org 04/01/2014 > Workqueue: events refill_work > Call Trace: > > dump_stack_lvl+0x34/0x44 > print_report.cold+0xbb/0x6ac > ? _printk+0xad/0xde > ? refill_work+0x43/0xd4 > kasan_report+0xa8/0x130 > ? refill_work+0x43/0xd4 > refill_work+0x43/0xd4 > process_one_work+0x43d/0x780 > worker_thread+0x2a0/0x6f0 > ? process_one_work+0x780/0x780 > kthread+0x167/0x1a0 > ? kthread_exit+0x50/0x50 > ret_from_fork+0x22/0x30 > > ... > > Fixes: b2baed69e605c ("virtio_net: set/cancel work on ndo_open/ndo_stop") > Signed-off-by: Jason Wang Reviewed-by: Xuan Zhuo > --- > drivers/net/virtio_net.c | 37 ++++++++++++++++++++++++++++++++++--- > 1 file changed, 34 insertions(+), 3 deletions(-) > > diff --git a/drivers/net/virtio_net.c b/drivers/net/virtio_net.c > index 356cf8dd4164..ec8e1b3108c3 100644 > --- a/drivers/net/virtio_net.c > +++ b/drivers/net/virtio_net.c > @@ -242,9 +242,15 @@ struct virtnet_info { > /* Packet virtio header size */ > u8 hdr_len; > > - /* Work struct for refilling if we run low on memory. */ > + /* Work struct for delayed refilling if we run low on memory. */ > struct delayed_work refill; > > + /* Is delayed refill enabled? */ > + bool refill_enabled; > + > + /* The lock to synchronize the access to refill_enabled */ > + spinlock_t refill_lock; > + > /* Work struct for config space updates */ > struct work_struct config_work; > > @@ -348,6 +354,20 @@ static struct page *get_a_page(struct receive_queue *rq, gfp_t gfp_mask) > return p; > } > > +static void enable_delayed_refill(struct virtnet_info *vi) > +{ > + spin_lock_bh(&vi->refill_lock); > + vi->refill_enabled = true; > + spin_unlock_bh(&vi->refill_lock); > +} > + > +static void disable_delayed_refill(struct virtnet_info *vi) > +{ > + spin_lock_bh(&vi->refill_lock); > + vi->refill_enabled = false; > + spin_unlock_bh(&vi->refill_lock); > +} > + > static void virtqueue_napi_schedule(struct napi_struct *napi, > struct virtqueue *vq) > { > @@ -1527,8 +1547,12 @@ static int virtnet_receive(struct receive_queue *rq, int budget, > } > > if (rq->vq->num_free > min((unsigned int)budget, virtqueue_get_vring_size(rq->vq)) / 2) { > - if (!try_fill_recv(vi, rq, GFP_ATOMIC)) > - schedule_delayed_work(&vi->refill, 0); > + if (!try_fill_recv(vi, rq, GFP_ATOMIC)) { > + spin_lock(&vi->refill_lock); > + if (vi->refill_enabled) > + schedule_delayed_work(&vi->refill, 0); > + spin_unlock(&vi->refill_lock); > + } > } > > u64_stats_update_begin(&rq->stats.syncp); > @@ -1651,6 +1675,8 @@ static int virtnet_open(struct net_device *dev) > struct virtnet_info *vi = netdev_priv(dev); > int i, err; > > + enable_delayed_refill(vi); > + > for (i = 0; i < vi->max_queue_pairs; i++) { > if (i < vi->curr_queue_pairs) > /* Make sure we have some buffers: if oom use wq. */ > @@ -2033,6 +2059,8 @@ static int virtnet_close(struct net_device *dev) > struct virtnet_info *vi = netdev_priv(dev); > int i; > > + /* Make sure NAPI doesn't schedule refill work */ > + disable_delayed_refill(vi); > /* Make sure refill_work doesn't re-enable napi! */ > cancel_delayed_work_sync(&vi->refill); > > @@ -2792,6 +2820,8 @@ static int virtnet_restore_up(struct virtio_device *vdev) > > virtio_device_ready(vdev); > > + enable_delayed_refill(vi); > + > if (netif_running(vi->dev)) { > err = virtnet_open(vi->dev); > if (err) > @@ -3535,6 +3565,7 @@ static int virtnet_probe(struct virtio_device *vdev) > vdev->priv = vi; > > INIT_WORK(&vi->config_work, virtnet_config_changed_work); > + spin_lock_init(&vi->refill_lock); > > /* If we can receive ANY GSO packets, we must allocate large ones. */ > if (virtio_has_feature(vdev, VIRTIO_NET_F_GUEST_TSO4) || > -- > 2.25.1 >