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 Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by smtp.lore.kernel.org (Postfix) with ESMTP id 192CCCCA480 for ; Mon, 20 Jun 2022 10:19:20 +0000 (UTC) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S240910AbiFTKSy (ORCPT ); Mon, 20 Jun 2022 06:18:54 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:48014 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S240811AbiFTKSo (ORCPT ); Mon, 20 Jun 2022 06:18:44 -0400 Received: from us-smtp-delivery-124.mimecast.com (us-smtp-delivery-124.mimecast.com [170.10.129.124]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 4D35C13F68 for ; Mon, 20 Jun 2022 03:18:43 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=redhat.com; s=mimecast20190719; t=1655720322; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version:content-type:content-type: in-reply-to:in-reply-to:references:references; bh=nm+eORp3zH2jrCWgqulUmPpdYYIUAnWdQdIYkQxKCxc=; b=fRVqSSP6RSWpI7sPT+A+u95mOzYqwQe7bQ2zWHI0Wtd6+JRWn7aT50hYK3gYyLsYXoOv8d I7+nyqMn02SfqHCXeiEH2vpF3iKYHiiCrTzmMnFxD45FNvahBQYi/u0Kc0i/7aZKJv0Gmp jVGlAUzeinpP3c233bGPnV5l5ug7yWg= Received: from mail-wr1-f72.google.com (mail-wr1-f72.google.com [209.85.221.72]) by relay.mimecast.com with ESMTP with STARTTLS (version=TLSv1.2, cipher=TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384) id us-mta-133-eZ1tN0CRMlaR_PJlGulp4Q-1; Mon, 20 Jun 2022 06:18:40 -0400 X-MC-Unique: eZ1tN0CRMlaR_PJlGulp4Q-1 Received: by mail-wr1-f72.google.com with SMTP id i16-20020adfa510000000b0021b8e9f7666so701637wrb.19 for ; Mon, 20 Jun 2022 03:18:40 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=nm+eORp3zH2jrCWgqulUmPpdYYIUAnWdQdIYkQxKCxc=; b=Ttw4wc+cIphhR6g14QK7K/YWpiYdBeDyO7XTMfrgyvy8+WAxKNk/crWHP1RkEgeZZ5 jVDUaPzF7KDnWOmvOB0AQXudqRl528H30uGcI9T8boU1YWjGEwlBBlUNPoIvGz5cYhrr E6DxhZjQGyfi1AHUHgedOcXH7yG/fz8yzpJM7UYIAEWrYrA2Npsfh7uEtR+vnDcWzofD V6bUbvXB4+l2XkXTkjqz2Pl+tAUt+6O77rm9akVndXloAZ4QBtTg585SVBDN1Y5NxJn3 XxTXgwLPjIuXLpfQ9wKrWZBi6uSV26tJalUVGxCrgtHU589f69Ow2WPXxLl50ootlUYH V7zg== X-Gm-Message-State: AOAM533U/E6NsyiLlBn+tEhgrKmPt9u8mo6o5GNHUOO9s5lECLxIYTWk dWeajNAodaWjhKjavtZyU/ayRpnT10FDneJsjyHZzwiNlsQq2KBMuZLXvNpoqpPUCvK0GVMtlIh HMyUroLkjJMnWVVOZhJRO2VEz X-Received: by 2002:a7b:c31a:0:b0:39c:4783:385e with SMTP id k26-20020a7bc31a000000b0039c4783385emr35408653wmj.185.1655720319446; Mon, 20 Jun 2022 03:18:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwcakcs2Da84r0qRgyJ2AJNFnTRJfDw0r95PsCWFIAzPqNF7lYCclsTp5XJoBLwTxe6twkMaA== X-Received: by 2002:a7b:c31a:0:b0:39c:4783:385e with SMTP id k26-20020a7bc31a000000b0039c4783385emr35408630wmj.185.1655720319132; Mon, 20 Jun 2022 03:18:39 -0700 (PDT) Received: from redhat.com ([2.52.146.221]) by smtp.gmail.com with ESMTPSA id k190-20020a1ca1c7000000b0039c587342d8sm18156471wme.3.2022.06.20.03.18.37 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 20 Jun 2022 03:18:38 -0700 (PDT) Date: Mon, 20 Jun 2022 06:18:35 -0400 From: "Michael S. Tsirkin" To: Jason Wang Cc: netdev , linux-kernel , davem , Jakub Kicinski , erwan.yvin@stericsson.com Subject: Re: [PATCH 3/3] caif_virtio: fix the race between reset and netdev unregister Message-ID: <20220620061607-mutt-send-email-mst@kernel.org> References: <20220620051115.3142-1-jasowang@redhat.com> <20220620051115.3142-4-jasowang@redhat.com> <20220620050446-mutt-send-email-mst@kernel.org> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org On Mon, Jun 20, 2022 at 05:18:29PM +0800, Jason Wang wrote: > On Mon, Jun 20, 2022 at 5:09 PM Michael S. Tsirkin wrote: > > > > On Mon, Jun 20, 2022 at 01:11:15PM +0800, Jason Wang wrote: > > > We use to do the following steps during .remove(): > > > > We currently do > > > > > > > static void cfv_remove(struct virtio_device *vdev) > > > { > > > struct cfv_info *cfv = vdev->priv; > > > > > > rtnl_lock(); > > > dev_close(cfv->ndev); > > > rtnl_unlock(); > > > > > > tasklet_kill(&cfv->tx_release_tasklet); > > > debugfs_remove_recursive(cfv->debugfs); > > > > > > vringh_kiov_cleanup(&cfv->ctx.riov); > > > virtio_reset_device(vdev); > > > vdev->vringh_config->del_vrhs(cfv->vdev); > > > cfv->vr_rx = NULL; > > > vdev->config->del_vqs(cfv->vdev); > > > unregister_netdev(cfv->ndev); > > > } > > > This is racy since device could be re-opened after dev_close() but > > > before unregister_netdevice(): > > > > > > 1) RX vringh is cleaned before resetting the device, rx callbacks that > > > is called after the vringh_kiov_cleanup() will result a UAF > > > 2) Network stack can still try to use TX virtqueue even if it has been > > > deleted after dev_vqs() > > > > > > Fixing this by unregistering the network device first to make sure not > > > device access from both TX and RX side. > > > > > > Fixes: 0d2e1a2926b18 ("caif_virtio: Introduce caif over virtio") > > > Signed-off-by: Jason Wang > > > --- > > > drivers/net/caif/caif_virtio.c | 6 ++---- > > > 1 file changed, 2 insertions(+), 4 deletions(-) > > > > > > diff --git a/drivers/net/caif/caif_virtio.c b/drivers/net/caif/caif_virtio.c > > > index 66375bea2fcd..a29f9b2df5b1 100644 > > > --- a/drivers/net/caif/caif_virtio.c > > > +++ b/drivers/net/caif/caif_virtio.c > > > @@ -752,9 +752,8 @@ static void cfv_remove(struct virtio_device *vdev) > > > { > > > struct cfv_info *cfv = vdev->priv; > > > > > > - rtnl_lock(); > > > - dev_close(cfv->ndev); > > > - rtnl_unlock(); > > > + /* Make sure NAPI/TX won't try to access the device */ > > > + unregister_netdev(cfv->ndev); > > > > > > tasklet_kill(&cfv->tx_release_tasklet); > > > debugfs_remove_recursive(cfv->debugfs); > > > @@ -764,7 +763,6 @@ static void cfv_remove(struct virtio_device *vdev) > > > vdev->vringh_config->del_vrhs(cfv->vdev); > > > cfv->vr_rx = NULL; > > > vdev->config->del_vqs(cfv->vdev); > > > - unregister_netdev(cfv->ndev); > > > } > > > > > > This gives me pause, callbacks can now trigger after device > > has been unregistered. Are we sure this is safe? > > It looks safe, for RX NAPI is disabled. For TX, tasklet is disabled > after tasklet_kill(). I can add a comment to explain this. that waits for outstanding tasklets but does it really prevent future ones? > > Won't it be safer to just keep the rtnl_lock around > > the whole process? > > It looks to me we rtnl_lock can't help in synchronizing with the > callbacks, anything I miss? > > Thanks good point. > > > > > static struct virtio_device_id id_table[] = { > > > -- > > > 2.25.1 > >