Received: by 2002:a05:6358:4e97:b0:b3:742d:4702 with SMTP id ce23csp2882977rwb; Mon, 15 Aug 2022 13:15:12 -0700 (PDT) X-Google-Smtp-Source: AA6agR5A95WOAP9yuGJp1jIiK+uBQ1xqvu+FlbcuMPNW6Pbaog48AZXWBzfk+MNrVyhjRzvGs462 X-Received: by 2002:a05:6402:110a:b0:443:225c:6822 with SMTP id u10-20020a056402110a00b00443225c6822mr15420898edv.81.1660594511832; Mon, 15 Aug 2022 13:15:11 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1660594511; cv=none; d=google.com; s=arc-20160816; b=L0KEH2xh4lND+cTt0o+Mor9qwk9yrrzakXRA0mFofZz+PPKmiyKwsjLOPEiv1m79B7 xnJG8/m0qmp8pplIZK0450UmYD5vfRcjzdEyqnrKE+1RVNfqFT61ieBstsetCyRwyzia tHltG9ecGj49Hqg369Sxov6EPjI239iucXXVRQXmVEPGHZ6adSLgB25xsgU9BbNzlv38 T2mpXuH4AWQ2Z71wGCRlHHgs/VsUJRUbQwsE0t+BYq91gyDFRodkQN4dw8B2ewmVgQPD qvjYp4z8Z3J6qS5t0K9eIv1+jV8pT3A2rqIvh2uueag55So+n2lKAz7e2bsVWKwM8gJ+ YENg== 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=PgPUzZxt/Xw5y02fM39kdhvEM8hgeCZbmlWm6WsYXjevfnUGEbRZFyhLhmrHoQGTbi fJgouvdlEKkacdR2tbgt+icDlqy2arU4A8Fbh7F9jg625Pfp7FtfSnXPFESVP65Qx+Zc EzA4AhSDqkqoU0N/+EloD8irD3kzoEYlkebCgW5SrsilIjsUQzp8+snaLJPkG+344kd2 vrdg3CoKucSVKwccE7JfxX/KPeDIAhL28I5THlzhgXZjg6ndV+lfj4zRq0u9R5NnQ7pz tOBJaEDlRyh0n0IZm8Lnf6RcYHfnKbT34WZvepG/F5akoinM1S5s9jxa0n8FVaXI8sFb lVSg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=S8fd+EWz; 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 qb23-20020a1709077e9700b0073155c0e7cfsi8120346ejc.695.2022.08.15.13.14.45; Mon, 15 Aug 2022 13:15:11 -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=S8fd+EWz; 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 S242626AbiHOTEb (ORCPT + 99 others); Mon, 15 Aug 2022 15:04:31 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:49190 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244631AbiHOTAG (ORCPT ); Mon, 15 Aug 2022 15:00:06 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [IPv6:2604:1380:4601:e00::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 0F6DF4AD78; Mon, 15 Aug 2022 11:32:42 -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 4CEFCB8105D; Mon, 15 Aug 2022 18:32:40 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 82E83C433C1; Mon, 15 Aug 2022 18:32:38 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1660588359; bh=2rlkvR+VSCNZxJ9MpCjFN9T0LkYJDu75Wd2nel5TUv8=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=S8fd+EWz5Aw7OUn1moW7nQ1R8S+ldrIwjPMtmmqQjca6JbpKWp0dWqgTs80jBaWsN hAaF15dLJpFUOLNopEXyJNkWS+Y7aDk3siccVZqaufpH6196CxK2KuxpkbrD27s9Hl ouCHDnlhE++TSK4xCz1z5Lz/rBENsZAEhdHEWTBE= 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.15 377/779] netdevsim: fib: Fix reference count leak on route deletion failure Date: Mon, 15 Aug 2022 20:00:21 +0200 Message-Id: <20220815180353.384201774@linuxfoundation.org> X-Mailer: git-send-email 2.37.2 In-Reply-To: <20220815180337.130757997@linuxfoundation.org> References: <20220815180337.130757997@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