Received: by 2002:a05:6a10:16a7:0:0:0:0 with SMTP id gp39csp3875134pxb; Tue, 17 Nov 2020 05:59:57 -0800 (PST) X-Google-Smtp-Source: ABdhPJylKsJ74CJOTWJaaFo/xIT5vA7XyshEZyqzo1uqVAVg6nWZY0N3gEgrh3EW4cc52qhAnW4e X-Received: by 2002:a17:906:4a02:: with SMTP id w2mr1719125eju.267.1605621596990; Tue, 17 Nov 2020 05:59:56 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1605621596; cv=none; d=google.com; s=arc-20160816; b=jCVt2fU5coa7NwXhwF5Jw14/beFnpQMk2kuDUtLTreAQUo0T7Tw8I3Ih99rapV/R8E O2S3M/A4ovk1PSuBFmtRH8KPA4bdPaiFRIG8WgMu7EwRgY5KUFF0vp4Lqjq9OnD2EY/E p0YANUR5Fei5GDYeBlIwGExhfgQ/5/C5qqd/wLCoVGSOoxdLl9poVgOWl9QVHcK548XM lDelYahJBASEVOqoyM0BbpLEU4m9bMsQFOZw/cK8/jylIvzWd11ymYg97lmgC5LsO3/V boTvktwDUeCiez51fxX9dzm6BP/U/TMAeUdbGtw0IAR95Mdm5unSp2xbP0A2pjb3ik81 UpPg== 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=w3QckGE/0tesl93mDnBZI9KKwooTMb717UqCdGPWO00=; b=bg1YkCVPiDNYjKJgOn4j/f+eOE7YPDYa5EjNyR4319mA8hPicx/g0ejXJ/LejkKavO Hu8drZbuHaY276K6JL0l/rfzaiTAeKlAunZfRAS7ZnWp2hBiQpwbQwji0bC/dwDKRbhw xiXvIJPt2WkrdpMcXunBrotCs/hIrOPg5fpS938ygaZJrbymrohZeWBph2kf9zb1YLGJ 8uHssfokGLw7JlthOzdv2823PxBCn2v9EvvVw3/yukjJtsfN37X7XIZNBkPatXizgR1Z aDzuQwOm505D3XZ3HdVmYbdL5HsqJSIBtRUMybewAnqGSimwNC862uoJ2jNQItxYAjs1 vpzw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@kernel.org header.s=default header.b=COdd6PQT; 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 wr19si13351233ejb.218.2020.11.17.05.59.34; Tue, 17 Nov 2020 05:59:56 -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=COdd6PQT; 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 S1729779AbgKQNTc (ORCPT + 99 others); Tue, 17 Nov 2020 08:19:32 -0500 Received: from mail.kernel.org ([198.145.29.99]:52256 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1730382AbgKQNTR (ORCPT ); Tue, 17 Nov 2020 08:19:17 -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 A04D7206D5; Tue, 17 Nov 2020 13:19:14 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=kernel.org; s=default; t=1605619155; bh=MZk2mRD1qjys7a2I8jirLXucMTlVvT1r3XsMOo29Jjc=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=COdd6PQTSrmQnPaAKS4tIvLtWC75pWMAnNXklR+FAci4+gdALVDoUkRSIwD54Hv3/ bjNsYrlUSvAi2do52cgwZUM9m3CstKU6fG3UwR48K4web5rN2+S4Xpo8fKWEgcyn6Y 3rIPV5+SaHQ72KlnynGEr9GoRKRLEGltzqKUBva4= 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.19 016/101] 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: <20201117122113.885320075@linuxfoundation.org> X-Mailer: git-send-email 2.29.2 In-Reply-To: <20201117122113.128215851@linuxfoundation.org> References: <20201117122113.128215851@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 1545f2b299d06..be532eb64baa2 100644 --- a/drivers/net/can/dev.c +++ b/drivers/net/can/dev.c @@ -520,7 +520,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