Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3879231pxb; Tue, 17 Nov 2020 06:04:38 -0800 (PST) X-Google-Smtp-Source: ABdhPJykD4WU/cfpNl4LeL+exN2iEWVYMdqui6mfnSqCUQMV6F5ftXOfZ7wSuvj2Rnzzh5thHB0L X-Received: by 2002:a17:906:9458:: with SMTP id z24mr20321255ejx.318.1605621878104; Tue, 17 Nov 2020 06:04:38 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605621878; cv=none; d=google.com; s=arc-20160816; b=kMBuPNfC4OUpgUrV6wyYEhJ/lQKncpSnUmlwEoSxom/2UdmQg8sHblNbAGsLax07+B 1dacdkgGFYTWVV4H+uv4PU0b32PnUnAZa/hwn55cKWO+6RG0lq5O10vkI9rWh+A47My8 oMqJMjstJn+3jX1elwc/jgoh6dX8GrhVRboDpI0AFYbkOBs5LvGbo1nuRTTVt1HpanJ5 r76/zRft4QBRjqAl6mdCNDi4VoXcQ9td2BZ303+j4cAkOckpdYIi3C3yCugBiJbtVZRb Z7Vye/RgwKDS0KMWZPnOjXg6BqQmbCA5hO+RPLJ1StwqsvcN3NHqQfSUx2pokvOo3tEC zXBg== 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=S8lx0WoWV52m+lsuRBGIfS2NL/IMozodBrG/xyrQayk=; b=aRO5j2v+w0wHTukOWIng6atOZBBtbaKrccG3TV68lWX2oKxIT4kqsg6kZ1mGLq8jWc AFWPs9X4C9qcgGIpveOcG1X2tL4xhVjP/7JZpiOOWhpvSz/JGMIAjcXbeDBaW5CFmnB/ swb4c8Zwaa9JycCrs6YJJka3MsGvyMvAQXH8m5we8XJx8ZL7IFJjoUI6nR0tFg7DISPc Dkg0OuZPS+kas68rVluFXL+EEx8JmE0qa4o1Yc956vqMqj/nbZX+1mS+lyoT2947Pk81 ZBF+0KTEosk4J3vBjJ50DTbpB6zFdTnoUympN3V5i8qIB1hvFn34H9+FHXSKICy4oESv Y1/Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=IfUy9VeS; 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=fail (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 x16si14357489eds.110.2020.11.17.06.04.10; Tue, 17 Nov 2020 06:04:38 -0800 (PST) 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=@kernel.org header.s=default header.b=IfUy9VeS; 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=fail (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1729689AbgKQNNd (ORCPT + 99 others); Tue, 17 Nov 2020 08:13:33 -0500 Received: from mail.kernel.org ([198.145.29.99]:43952 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1728643AbgKQNN1 (ORCPT ); Tue, 17 Nov 2020 08:13:27 -0500 Received: from localhost (83-86-74-64.cable.dynamic.v4.ziggo.nl [83.86.74.64]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by mail.kernel.org (Postfix) with ESMTPSA id 086C6246C3; Tue, 17 Nov 2020 13:13:25 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605618806; bh=L5VTr8X2OXy6sxrXyoXMwlZk1hPWPod3JAZrnp0UmKg=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=IfUy9VeSi3xVS6P64jshrfTxAtrma/YYvm0bwaeDOH6YU4z2EDHHM0daU2yBnDB2x t5bUfTGK2XPxYqDW//XRBS5wVJopDv5ZDTElZJSB60yiitQ7fd39MJdED1NpbzZbFA Z9lanvwfl1q9RT81o51NbzgHcYbXVdt+J76ZF+x0= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Vincent Mailhol , Marc Kleine-Budde , Sasha Levin Subject: [PATCH 4.14 14/85] can: dev: can_get_echo_skb(): prevent call to kfree_skb() in hard IRQ context Date: Tue, 17 Nov 2020 14:04:43 +0100 Message-Id: <20201117122111.733175438@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201117122111.018425544@linuxfoundation.org> References: <20201117122111.018425544@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: Vincent Mailhol [ Upstream commit 2283f79b22684d2812e5c76fc2280aae00390365 ] If a driver calls can_get_echo_skb() during a hardware IRQ (which is often, but not always, the case), the 'WARN_ON(in_irq)' in net/core/skbuff.c#skb_release_head_state() might be triggered, under network congestion circumstances, together with the potential risk of a NULL pointer dereference. The root cause of this issue is the call to kfree_skb() instead of dev_kfree_skb_irq() in net/core/dev.c#enqueue_to_backlog(). This patch prevents the skb to be freed within the call to netif_rx() by incrementing its reference count with skb_get(). The skb is finally freed by one of the in-irq-context safe functions: dev_consume_skb_any() or dev_kfree_skb_any(). The "any" version is used because some drivers might call can_get_echo_skb() in a normal context. The reason for this issue to occur is that initially, in the core network stack, loopback skb were not supposed to be received in hardware IRQ context. The CAN stack is an exeption. This bug was previously reported back in 2017 in [1] but the proposed patch never got accepted. While [1] directly modifies net/core/dev.c, we try to propose here a smoother modification local to CAN network stack (the assumption behind is that only CAN devices are affected by this issue). [1] http://lore.kernel.org/r/57a3ffb6-3309-3ad5-5a34-e93c3fe3614d@cetitec.com Signed-off-by: Vincent Mailhol Link: https://lore.kernel.org/r/20201002154219.4887-2-mailhol.vincent@wanadoo.fr Fixes: 39549eef3587 ("can: CAN Network device driver and Netlink interface") Signed-off-by: Marc Kleine-Budde Signed-off-by: Sasha Levin --- drivers/net/can/dev.c | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/drivers/net/can/dev.c b/drivers/net/can/dev.c index 05ad5ed145a3a..926d663eed37a 100644 --- a/drivers/net/can/dev.c +++ b/drivers/net/can/dev.c @@ -519,7 +519,11 @@ unsigned int can_get_echo_skb(struct net_device *dev, unsigned int idx) if (!skb) return 0; - netif_rx(skb); + skb_get(skb); + if (netif_rx(skb) == NET_RX_SUCCESS) + dev_consume_skb_any(skb); + else + dev_kfree_skb_any(skb); return len; } -- 2.27.0