Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp2732162pxj; Mon, 31 May 2021 09:19:55 -0700 (PDT) X-Google-Smtp-Source: ABdhPJzgqklZPtL/DmfUDwU3EsNYb7uBSWXrWdpsQOVGAJDUvorNfVTVUsT+ozzJeVgusvljz3h8 X-Received: by 2002:a17:906:3a8e:: with SMTP id y14mr23829125ejd.153.1622477995010; Mon, 31 May 2021 09:19:55 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1622477995; cv=none; d=google.com; s=arc-20160816; b=o67x2kU0sEMw5NxQllF4pIAUvRXyR84EBlTAKvxMdVvn2CEBE1IJU0h2LpbfWSxy8Z e1PMq8rrMSchTPFz5H7YdQcNOJtwG5J7jxIbL3yeJOUn2kNutrLjO+hmpimkucfDCYRP Fo5n9rsnfF69nG2uUqrW9DGueVm3qFTa/LlsrIe40dttWg4wsKhVotxPLcrjOvjx65T7 j//OkO7AET3iQ6N4s96ww+YFxA6z4S2NKCSJwA1ErSzVIkatq5zC8dbxnrJr/eOfwQ4h 573oxgPqak4dbI+ED33957+YQcOXmjSBf3niKfV4XvqiVb03HDnm8BJ/LafmZmzv8ipH BxfA== 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=Pmz7chD4sOEOnh/6s9h0eLJeLmb1guQAsSYSZBGAl9Q=; b=uVWFyu0z0FTobmoeMDg8G9SFgA7dkC8Ay7rw6Rhno2cRqSRQQhzqDzyW+cHuzorkfJ GStI2K20LamlyHsa4Cn62ZaL2we6NjP1/ag+MVicn9+4ducQX4M29w4Yy59O4IFVhsE5 uoNVi958N1dvPk0esOC6iIzjJzSqQR9gfy+OiUKIHh63tsPMEjKcyeXyVjnW6Qxnf8k0 vY9m//BhkozV93R6XA9dE6fxV+wn8G8LNCxeJA5/sSWmh30OlX/9XNJenKN8MU/4RCAI kTJdPTE3YrgJ/nYaS/jh6h5BaNKYp7DU6+xlT8bfU6LI9YfddWOjUwV6r3ZH7j76V5x4 ZRyw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uTwkp2Ko; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 vger.kernel.org (vger.kernel.org. [23.128.96.18]) by mx.google.com with ESMTP id rh9si14400590ejb.589.2021.05.31.09.19.32; Mon, 31 May 2021 09:19:55 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 as permitted sender) client-ip=23.128.96.18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=uTwkp2Ko; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 23.128.96.18 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 S234773AbhEaQTx (ORCPT + 99 others); Mon, 31 May 2021 12:19:53 -0400 Received: from mail.kernel.org ([198.145.29.99]:37024 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S232106AbhEaOmK (ORCPT ); Mon, 31 May 2021 10:42:10 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 1751E61C71; Mon, 31 May 2021 13:53:59 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1622469240; bh=oP517+rIBNt+5HX5RsjOP/NTxSLiEzw8RyYNpIdx35M=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uTwkp2Ko84sa7m8bXZaQEpAAd7K5R+/lrhMOFm0R3udf9JuWt55LFEdy8me0v9djM 5v+c83rRoU72T13JiQBpn4O/1vY7jGUx0hluEBs7bfe7df6nCBP7ZVOvulcv5Y2jFS rvBr/2QE9AQ4BxoE8AWLZelG55LHlgag5OGrLvTE= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Dima Chumak , Vlad Buslov , Saeed Mahameed Subject: [PATCH 5.12 122/296] net/mlx5e: Fix nullptr in mlx5e_tc_add_fdb_flow() Date: Mon, 31 May 2021 15:12:57 +0200 Message-Id: <20210531130708.010566858@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210531130703.762129381@linuxfoundation.org> References: <20210531130703.762129381@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org 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: Greg Kroah-Hartman --- drivers/net/ethernet/mellanox/mlx5/core/en_tc.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) --- a/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c +++ b/drivers/net/ethernet/mellanox/mlx5/core/en_tc.c @@ -1276,10 +1276,10 @@ mlx5e_tc_add_fdb_flow(struct mlx5e_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; bool vf_tun = false, encap_valid = true; + struct net_device *encap_dev = NULL; struct mlx5_esw_flow_attr *esw_attr; struct mlx5_fc *counter = NULL; struct mlx5e_rep_priv *rpriv; @@ -1325,16 +1325,22 @@ mlx5e_tc_add_fdb_flow(struct mlx5e_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; + goto err_out; + } err = mlx5e_attach_encap(priv, flow, out_dev, out_index, extack, &encap_dev, &encap_valid); + dev_put(out_dev); if (err) goto err_out;