Received: by 2002:a05:6a10:206:0:0:0:0 with SMTP id 6csp4738239pxj; Wed, 12 May 2021 12:05:11 -0700 (PDT) X-Google-Smtp-Source: ABdhPJxweFRNtWX7z+yIVjDQ3VJz2CQSxZOmjr3hzOLKQltsAhcA64MoKZvG1IddB4bOdXib1pK0 X-Received: by 2002:a7b:c34a:: with SMTP id l10mr40796476wmj.46.1620846224976; Wed, 12 May 2021 12:03:44 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1620846224; cv=none; d=google.com; s=arc-20160816; b=bFLmU8z3P8AzXcfy3mhEW5T94bogirPSg6R66DLuV/jEZG8VcGULGory2H8F5+1cNs FzMmFh6tfElKV9RWt5V0i5EPtUAlr3es0ZqWhfQyBEmDL4rHd9Nkz+YJTRboHPoTIpG9 pktJLppT7XoqqUB7cM3WhXRaR1S7vrNfx0xzXj3dpURpzGUOLrItC7fLr98J8BSI5kiv UWOyclBeOHVJnmLO0asYmzy6+pMUi/5IY6yuZmocASa9d19J7kfgjv8eeZmwDmNGflx2 W4MWvLuOrClJu+MN0e/Mf2RqA8WyX+z0kfuRmjWFvxUx2gc1LeWnEzH8N6KORk8+0d5A yTnw== 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=djFxOgCXkHBuHA5UlJXhhWXqvJKIcgNPHJYoX4OY/nM=; b=cPLQkwozKBZwAxDsnN7bl3BWhAf2hRYzMOQ5bl8rZzO2qs3wYfsPaOh0yavGYEOBTK CStzm/2cfrAjgNAIIJu0UKOIs8YlrBjTHydvDorI2s4hdP2z7D/QUNpKdiESrii9XFy2 WUbpc9C1H9/BbakNeoQOW+2BFBu4M+oB1LJ3atEtxAYzCEba/cyzi9FktmC41DZPmxrn bUf+6IsSj9Rz2gQCj21vEGLyjt1FolUhChfMEb+ZPp8bRnPKfRXyCWbZXru9jxgrnBQS oxdYm2P5MtKcMukqBIMukzuxyQXPNGXdSCfw+ogwCDilucz0+EtzSiQTPSqNTXJBNtId Hzhw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=kd0p4dUz; 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 kz18si698265ejc.598.2021.05.12.12.03.16; Wed, 12 May 2021 12:03:44 -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=kd0p4dUz; 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 S1359517AbhELSx3 (ORCPT + 99 others); Wed, 12 May 2021 14:53:29 -0400 Received: from mail.kernel.org ([198.145.29.99]:35836 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S243956AbhELQmS (ORCPT ); Wed, 12 May 2021 12:42:18 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 64D8861C5D; Wed, 12 May 2021 16:08:36 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1620835717; bh=XuKxvpfkqIgUoV+gZWVDgkzoJ8SU7lzLm6T6bv+7Fps=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=kd0p4dUzKVcqLFaZq8uWFSEEOR+R5lFTjbRCP0vvOYrBED1opSYBkEm+770OL5DJ+ +WQ8QOxzJp4Nz9kJv5UXqBnmB8rK3UmZizP22Hn+UnbX0kkC27uCp5SqXzNMGOUF41 s7IsVTvl6F7PdGMD71i3HUaVk4qxcoc/hSbGctZ4= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Xie He , Martin Schiller , "David S. Miller" , Sasha Levin Subject: [PATCH 5.12 449/677] net: lapbether: Prevent racing when checking whether the netif is running Date: Wed, 12 May 2021 16:48:15 +0200 Message-Id: <20210512144852.283038374@linuxfoundation.org> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20210512144837.204217980@linuxfoundation.org> References: <20210512144837.204217980@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: Xie He [ Upstream commit 5acd0cfbfbb5a688da1bfb1a2152b0c855115a35 ] There are two "netif_running" checks in this driver. One is in "lapbeth_xmit" and the other is in "lapbeth_rcv". They serve to make sure that the LAPB APIs called in these functions are called before "lapb_unregister" is called by the "ndo_stop" function. However, these "netif_running" checks are unreliable, because it's possible that immediately after "netif_running" returns true, "ndo_stop" is called (which causes "lapb_unregister" to be called). This patch adds locking to make sure "lapbeth_xmit" and "lapbeth_rcv" can reliably check and ensure the netif is running while doing their work. Fixes: 1da177e4c3f4 ("Linux-2.6.12-rc2") Signed-off-by: Xie He Acked-by: Martin Schiller Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- drivers/net/wan/lapbether.c | 32 +++++++++++++++++++++++++------- 1 file changed, 25 insertions(+), 7 deletions(-) diff --git a/drivers/net/wan/lapbether.c b/drivers/net/wan/lapbether.c index c3372498f4f1..8fda0446ff71 100644 --- a/drivers/net/wan/lapbether.c +++ b/drivers/net/wan/lapbether.c @@ -51,6 +51,8 @@ struct lapbethdev { struct list_head node; struct net_device *ethdev; /* link to ethernet device */ struct net_device *axdev; /* lapbeth device (lapb#) */ + bool up; + spinlock_t up_lock; /* Protects "up" */ }; static LIST_HEAD(lapbeth_devices); @@ -101,8 +103,9 @@ static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packe rcu_read_lock(); lapbeth = lapbeth_get_x25_dev(dev); if (!lapbeth) - goto drop_unlock; - if (!netif_running(lapbeth->axdev)) + goto drop_unlock_rcu; + spin_lock_bh(&lapbeth->up_lock); + if (!lapbeth->up) goto drop_unlock; len = skb->data[0] + skb->data[1] * 256; @@ -117,11 +120,14 @@ static int lapbeth_rcv(struct sk_buff *skb, struct net_device *dev, struct packe goto drop_unlock; } out: + spin_unlock_bh(&lapbeth->up_lock); rcu_read_unlock(); return 0; drop_unlock: kfree_skb(skb); goto out; +drop_unlock_rcu: + rcu_read_unlock(); drop: kfree_skb(skb); return 0; @@ -151,13 +157,11 @@ static int lapbeth_data_indication(struct net_device *dev, struct sk_buff *skb) static netdev_tx_t lapbeth_xmit(struct sk_buff *skb, struct net_device *dev) { + struct lapbethdev *lapbeth = netdev_priv(dev); int err; - /* - * Just to be *really* sure not to send anything if the interface - * is down, the ethernet device may have gone. - */ - if (!netif_running(dev)) + spin_lock_bh(&lapbeth->up_lock); + if (!lapbeth->up) goto drop; /* There should be a pseudo header of 1 byte added by upper layers. @@ -194,6 +198,7 @@ static netdev_tx_t lapbeth_xmit(struct sk_buff *skb, goto drop; } out: + spin_unlock_bh(&lapbeth->up_lock); return NETDEV_TX_OK; drop: kfree_skb(skb); @@ -285,6 +290,7 @@ static const struct lapb_register_struct lapbeth_callbacks = { */ static int lapbeth_open(struct net_device *dev) { + struct lapbethdev *lapbeth = netdev_priv(dev); int err; if ((err = lapb_register(dev, &lapbeth_callbacks)) != LAPB_OK) { @@ -292,13 +298,22 @@ static int lapbeth_open(struct net_device *dev) return -ENODEV; } + spin_lock_bh(&lapbeth->up_lock); + lapbeth->up = true; + spin_unlock_bh(&lapbeth->up_lock); + return 0; } static int lapbeth_close(struct net_device *dev) { + struct lapbethdev *lapbeth = netdev_priv(dev); int err; + spin_lock_bh(&lapbeth->up_lock); + lapbeth->up = false; + spin_unlock_bh(&lapbeth->up_lock); + if ((err = lapb_unregister(dev)) != LAPB_OK) pr_err("lapb_unregister error: %d\n", err); @@ -356,6 +371,9 @@ static int lapbeth_new_device(struct net_device *dev) dev_hold(dev); lapbeth->ethdev = dev; + lapbeth->up = false; + spin_lock_init(&lapbeth->up_lock); + rc = -EIO; if (register_netdevice(ndev)) goto fail; -- 2.30.2