From patchwork Mon Dec 26 18:09:50 2022 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Nikita Zhandarovich X-Patchwork-Id: 36682 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:4e01:0:0:0:0:0 with SMTP id p1csp1036758wrt; Mon, 26 Dec 2022 10:19:17 -0800 (PST) X-Google-Smtp-Source: AMrXdXuPfUOVO33Twk8/+XcCwELPKAasQaz6enAlhYhKxLRKPFZCPvJ9KDo4X3oX8Q0hRI8BaNdG X-Received: by 2002:a17:906:39d8:b0:847:410:ecff with SMTP id i24-20020a17090639d800b008470410ecffmr11128802eje.16.1672078757712; Mon, 26 Dec 2022 10:19:17 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1672078757; cv=none; d=google.com; s=arc-20160816; b=xkfoIc9TjzXnRMMiaXzBNTmmkDEJO8JIGwlgWxeu9x3jC7n9POkvUB6HN8wdOK95lw dQJNRXZ8pvpvnx8JyFIrdQdNfuRETgGtKe1Y5QlsOKNU/KBHqvmvWURHkUxFhVANNnNt /1hEmlhBm1/JDN9O5R4a86B8uYJXUzxjU/hjX3J6qcTgwIQcaEBUg+Y+Jhk23IGXm0yH QOdLWbOL0KdzeqtgbZwHhOCv6tb+58N9YUX5xpNCP0zq7ZfaLZbMTLPSdNSdMgTWAlPi UETrxyk5mZfLKl8l/ZadJbkGgbC1hc/oWEZQtgfnQbwDJHinaT0AiKuaLvHo4SaUuFa2 3Rcw== 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 :references:in-reply-to:message-id:date:subject:cc:to:from; bh=ai0PSODzP37kyeS2u57bGSt/JIJrBeGfIEwNKwxuz+U=; b=x9U7Lnv2rjdSuvNEhGnv4lgNNKEFXsb8xdOZu6WmVDB0lNfJcOE3VfDps6sqIBAjSM LacqCAXwOfvK9ywQxmxxYijnsA1Irc3P8FoKMbUBVLeHLHqNm/RumO7C4I1EF0HVyNv8 MPJ1r4mTEH7G/UpoHm1Yfl7eaUsPUPjvc0IBljb2X14+t61WuS1bL3VAJZe/WOauDlDQ MVMHe9j8KZ23yJrz8oWkicrKYDj2z4sfdH1hBooUNUqbZrsdKZbShB0zlSz6BFltuRiZ 2HtyF6lu066AZjjkWNur6oyDnJEm8+6Tb7asQ2Ny2fxfIBuCm1ur4K0AKRw/CyaDlB6K jwTQ== 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 Received: from out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id x5-20020a056402414500b00484e95ad48bsi2890916eda.115.2022.12.26.10.18.45; Mon, 26 Dec 2022 10:19:17 -0800 (PST) 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 Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S231531AbiLZSLQ (ORCPT + 99 others); Mon, 26 Dec 2022 13:11:16 -0500 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:35334 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S229614AbiLZSLO (ORCPT ); Mon, 26 Dec 2022 13:11:14 -0500 X-Greylist: delayed 63 seconds by postgrey-1.37 at lindbergh.monkeyblade.net; Mon, 26 Dec 2022 10:11:10 PST Received: from exchange.fintech.ru (e10edge.fintech.ru [195.54.195.159]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 308C0333; Mon, 26 Dec 2022 10:11:10 -0800 (PST) Received: from Ex16-01.fintech.ru (10.0.10.18) by exchange.fintech.ru (195.54.195.169) with Microsoft SMTP Server (TLS) id 14.3.498.0; Mon, 26 Dec 2022 21:10:01 +0300 Received: from localhost (10.0.253.157) by Ex16-01.fintech.ru (10.0.10.18) with Microsoft SMTP Server (version=TLS1_2, cipher=TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256) id 15.1.2242.4; Mon, 26 Dec 2022 21:10:01 +0300 From: Nikita Zhandarovich To: , Greg Kroah-Hartman CC: Nikita Zhandarovich , =?utf-8?b?RGltYcK3Q2h1?= =?utf-8?b?bWFrwrc=?= , Saeed Mahameed , Leon Romanovsky , "David S. Miller" , Jakub Kicinski , , , , , Vlad Buslov Subject: [PATCH 5.10 1/1] net/mlx5e: Fix nullptr in mlx5e_tc_add_fdb_flow() Date: Mon, 26 Dec 2022 10:09:50 -0800 Message-ID: <20221226180950.86129-2-n.zhandarovich@fintech.ru> X-Mailer: git-send-email 2.25.1 In-Reply-To: <20221226180950.86129-1-n.zhandarovich@fintech.ru> References: <20221226180950.86129-1-n.zhandarovich@fintech.ru> MIME-Version: 1.0 X-Originating-IP: [10.0.253.157] X-ClientProxiedBy: Ex16-01.fintech.ru (10.0.10.18) To Ex16-01.fintech.ru (10.0.10.18) X-Spam-Status: No, score=-1.9 required=5.0 tests=BAYES_00,SPF_HELO_NONE, SPF_PASS 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 X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1753301655190808645?= X-GMAIL-MSGID: =?utf-8?q?1753301655190808645?= From: Dima Chumak commit fe7738eb3ca3631a75844e790f6cb576c0fe7b00 upstream. The result of __dev_get_by_index() is not checked for NULL, which then passed to mlx5e_attach_encap() and gets dereferenced. Also, in case of a successful lookup, the net_device reference count is not incremented, which may result in net_device pointer becoming invalid at any time during mlx5e_attach_encap() execution. Fix by using dev_get_by_index(), which does proper reference counting on the net_device pointer. Also, handle nullptr return value when mirred device is not found. It's safe to call dev_put() on the mirred net_device pointer, right after mlx5e_attach_encap() call, because it's not being saved/copied down the call chain. Fixes: 3c37745ec614 ("net/mlx5e: Properly deal with encap flows add/del under neigh update") Addresses-Coverity: ("Dereference null return value") Signed-off-by: Dima Chumak Reviewed-by: Vlad Buslov Signed-off-by: Saeed Mahameed Signed-off-by: Nikita Zhandarovich --- drivers/net/ethernet/mellanox/mlx5/core/en_tc.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c b/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c index ce710f22b1ff..fbfce2b3baeb 100644 --- a/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c @@ -1307,9 +1307,9 @@ mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv, struct netlink_ext_ack *extack) { struct mlx5_eswitch *esw = priv->mdev->priv.eswitch; - struct net_device *out_dev, *encap_dev = NULL; struct mlx5e_tc_flow_parse_attr *parse_attr; struct mlx5_flow_attr *attr = flow->attr; + struct net_device *encap_dev = NULL; struct mlx5_esw_flow_attr *esw_attr; struct mlx5_fc *counter = NULL; struct mlx5e_rep_priv *rpriv; @@ -1354,16 +1354,22 @@ mlx5e_tc_add_fdb_flow(struct mlx5e_priv *priv, esw_attr = attr->esw_attr; for (out_index = 0; out_index < MLX5_MAX_FLOW_FWD_VPORTS; out_index++) { + struct net_device *out_dev; int mirred_ifindex; if (!(esw_attr->dests[out_index].flags & MLX5_ESW_DEST_ENCAP)) continue; mirred_ifindex = parse_attr->mirred_ifindex[out_index]; - out_dev = __dev_get_by_index(dev_net(priv->netdev), - mirred_ifindex); + out_dev = dev_get_by_index(dev_net(priv->netdev), mirred_ifindex); + if (!out_dev) { + NL_SET_ERR_MSG_MOD(extack, "Requested mirred device not found"); + err = -ENODEV; + return err; + } err = mlx5e_attach_encap(priv, flow, out_dev, out_index, extack, &encap_dev, &encap_valid); + dev_put(out_dev); if (err) return err;