Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp3023656rwb; Mon, 15 Aug 2022 16:21:14 -0700 (PDT) X-Google-Smtp-Source: AA6agR4x+AF/3QPJJgQwqNvU6X4/M8kvSfXaPRL/Hc/cpj5g1a9A/xHIvjmD4Be0HX7sVnlTuet6 X-Received: by 2002:a17:906:478d:b0:730:a5ed:1bf3 with SMTP id cw13-20020a170906478d00b00730a5ed1bf3mr11438955ejc.255.1660605674408; Mon, 15 Aug 2022 16:21:14 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660605674; cv=none; d=google.com; s=arc-20160816; b=TIwCTzy/+REoVv/xM8RoG1wopNW0Gxt9h3Ye2WxVACWCIXSNIi+vYFV5CoeLCHk2M+ FynrGbDjxybp8HT/NRq4Nf3p646PMvAE7j5/NDGa2HNeUKLJpLFAd5Z9zWSFra1AVX+y y7mFZQbTG2It7nkcJp4a7bBkruiJ+svqdmYeEv5aVdcIgKwOfBB9+WomUB57Lky5VtOr TeFjDiUQ2Uj1YfJGVHG9enyoEE808H2w2myxrrycGm8FPV6Gz1Md/chhkuvd28ksID5Q SHjqDrPv4SaskHb1hQEDLIOU5TyJdCrJ2k1E/greETMqzJoMjqI23cc4lpLshw7hu8Lp GhqA== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=list-id:precedence:content-transfer-encoding:mime-version :user-agent:references:in-reply-to:message-id:date:subject:cc:to :from:dkim-signature; bh=+4JlI4JeC8DL+fu2jnH/mn3ePpqyEulOJul8eVGMIsQ=; b=RnYhwhyZWoK2OcowUQU61GpWhIDPRS6hzdayLfy9AAeWlfdR+7a7CocRUMGKhU6YQt ZjMsAauLJnph5NtwDA+kr4HnSxD2WBSsgUANpI4lTtnXQ0oPg9i7cSmhEFz+LF0AavKa m1eAF4kxVJXoGo1blAFlFFyuWNXjWAtYd4gSGtzEfAtnHB9WSTsxukd1jMatoaynbMFM YqW/+ga/ErjPpR5TS4vaPgs73EVDKzrnC6aPiN2df2i0ckLXnmGZkvODTHG2071gbitA B/Kfz1P3I51IO7y4KPVH8DtgEjSkEa/wZgd7VZCrhwtjNUluP/ohpdJvWrtBsC2M9SDG sp8Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=oW+RQWsu; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Return-Path: Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z21-20020a05640240d500b0043d9c8dbfd9si10772745edb.31.2022.08.15.16.20.49; Mon, 15 Aug 2022 16:21:14 -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; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=oW+RQWsu; 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=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346441AbiHOVY3 (ORCPT + 99 others); Mon, 15 Aug 2022 17:24:29 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:37452 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1346297AbiHOVSD (ORCPT ); Mon, 15 Aug 2022 17:18:03 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 7EDAE5A147; Mon, 15 Aug 2022 12:21:20 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 0FC10B810C6; Mon, 15 Aug 2022 19:21:19 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 4926AC433D6; Mon, 15 Aug 2022 19:21:17 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660591277; bh=2rlkvR+VSCNZxJ9MpCjFN9T0LkYJDu75Wd2nel5TUv8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=oW+RQWsuB0XOvYx9LhU4t0EYhtaMghyJKnhLHZyCk4wr8Yj/nvkoBpFrtnMyVbWTx S11V4B0Davo6lovVCOLlUbac+362atI+FZ2+Hkl195PPmAHy/ECVF5ybnF+NBuHUDT 1lLRRXsPXvAcVeTgSijjUEnOY+fdhpW5jyG4YAus= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Ido Schimmel , Amit Cohen , David Ahern , "David S. Miller" , Sasha Levin Subject: [PATCH 5.18 0527/1095] netdevsim: fib: Fix reference count leak on route deletion failure Date: Mon, 15 Aug 2022 19:58:46 +0200 Message-Id: <20220815180451.357535785@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180429.240518113@linuxfoundation.org> References: <20220815180429.240518113@linuxfoundation.org> User-Agent: quilt/0.67 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-7.1 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE 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 From: Ido Schimmel [ Upstream commit 180a6a3ee60a7cb69ed1232388460644f6a21f00 ] As part of FIB offload simulation, netdevsim stores IPv4 and IPv6 routes and holds a reference on FIB info structures that in turn hold a reference on the associated nexthop device(s). In the unlikely case where we are unable to allocate memory to process a route deletion request, netdevsim will not release the reference from the associated FIB info structure, thereby preventing the associated nexthop device(s) from ever being removed [1]. Fix this by scheduling a work item that will flush netdevsim's FIB table upon route deletion failure. This will cause netdevsim to release its reference from all the FIB info structures in its table. Reported by Lucas Leong of Trend Micro Zero Day Initiative. Fixes: 0ae3eb7b4611 ("netdevsim: fib: Perform the route programming in a non-atomic context") Signed-off-by: Ido Schimmel Reviewed-by: Amit Cohen Reviewed-by: David Ahern Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- drivers/net/netdevsim/fib.c | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/drivers/net/netdevsim/fib.c b/drivers/net/netdevsim/fib.c index 378ee779061c..14787d17f703 100644 --- a/drivers/net/netdevsim/fib.c +++ b/drivers/net/netdevsim/fib.c @@ -53,6 +53,7 @@ struct nsim_fib_data { struct rhashtable nexthop_ht; struct devlink *devlink; struct work_struct fib_event_work; + struct work_struct fib_flush_work; struct list_head fib_event_queue; spinlock_t fib_event_queue_lock; /* Protects fib event queue list */ struct mutex nh_lock; /* Protects NH HT */ @@ -977,7 +978,7 @@ static int nsim_fib_event_schedule_work(struct nsim_fib_data *data, fib_event = kzalloc(sizeof(*fib_event), GFP_ATOMIC); if (!fib_event) - return NOTIFY_BAD; + goto err_fib_event_alloc; fib_event->data = data; fib_event->event = event; @@ -1005,6 +1006,9 @@ static int nsim_fib_event_schedule_work(struct nsim_fib_data *data, err_fib_prepare_event: kfree(fib_event); +err_fib_event_alloc: + if (event == FIB_EVENT_ENTRY_DEL) + schedule_work(&data->fib_flush_work); return NOTIFY_BAD; } @@ -1482,6 +1486,24 @@ static void nsim_fib_event_work(struct work_struct *work) mutex_unlock(&data->fib_lock); } +static void nsim_fib_flush_work(struct work_struct *work) +{ + struct nsim_fib_data *data = container_of(work, struct nsim_fib_data, + fib_flush_work); + struct nsim_fib_rt *fib_rt, *fib_rt_tmp; + + /* Process pending work. */ + flush_work(&data->fib_event_work); + + mutex_lock(&data->fib_lock); + list_for_each_entry_safe(fib_rt, fib_rt_tmp, &data->fib_rt_list, list) { + rhashtable_remove_fast(&data->fib_rt_ht, &fib_rt->ht_node, + nsim_fib_rt_ht_params); + nsim_fib_rt_free(fib_rt, data); + } + mutex_unlock(&data->fib_lock); +} + static int nsim_fib_debugfs_init(struct nsim_fib_data *data, struct nsim_dev *nsim_dev) { @@ -1540,6 +1562,7 @@ struct nsim_fib_data *nsim_fib_create(struct devlink *devlink, goto err_rhashtable_nexthop_destroy; INIT_WORK(&data->fib_event_work, nsim_fib_event_work); + INIT_WORK(&data->fib_flush_work, nsim_fib_flush_work); INIT_LIST_HEAD(&data->fib_event_queue); spin_lock_init(&data->fib_event_queue_lock); @@ -1586,6 +1609,7 @@ struct nsim_fib_data *nsim_fib_create(struct devlink *devlink, err_nexthop_nb_unregister: unregister_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb); err_rhashtable_fib_destroy: + cancel_work_sync(&data->fib_flush_work); flush_work(&data->fib_event_work); rhashtable_free_and_destroy(&data->fib_rt_ht, nsim_fib_rt_free, data); @@ -1615,6 +1639,7 @@ void nsim_fib_destroy(struct devlink *devlink, struct nsim_fib_data *data) NSIM_RESOURCE_IPV4_FIB); unregister_fib_notifier(devlink_net(devlink), &data->fib_nb); unregister_nexthop_notifier(devlink_net(devlink), &data->nexthop_nb); + cancel_work_sync(&data->fib_flush_work); flush_work(&data->fib_event_work); rhashtable_free_and_destroy(&data->fib_rt_ht, nsim_fib_rt_free, data); -- 2.35.1