Received: by 2002:a05:6a10:9848:0:0:0:0 with SMTP id x8csp3319809pxf; Mon, 15 Mar 2021 07:04:49 -0700 (PDT) X-Google-Smtp-Source: ABdhPJwFiSSPzg7maENotLYfuVx6xVY0iF8PImnZ2TFfsyIqoSm9CCa9xJa+E93vT+ABa27Eeqw+ X-Received: by 2002:aa7:dd05:: with SMTP id i5mr30084139edv.300.1615817089697; Mon, 15 Mar 2021 07:04:49 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1615817089; cv=none; d=google.com; s=arc-20160816; b=GMOAkAcT5MFwpW5rwQ98lwDA7L103lj6aeU85eM+YWcwHIjLmLqqjlkFcUJ0sfuNbR GJ6nkOSfyGNlL0Pd46IsluvOwKBuee1+WFiEo58S23IBmp+lOGe6vRjOPloV8dQEBljm jKxtEbieZAfDU3iSGBj/ShdNxWnPxhvNFvbbCDGpi4K3cOkXX/UsMVd2jSYgCk+xKn0d qj8EWg7SUQ59hpkZ5+pR7LvIo8aR5qcuB+cFndvfWRDuHsRUWzS1xyO2e/NSyOEGT6Bc DdRHYiTMS3wUw6QRre6uO4VGFCfi6HMnQsM2oMdmIdQdqLSvQqXiw8EnIp99oiYjSYL0 UG2g== 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=ZZrt3YSkSC6Bx9bfiFmudfSJWb+RLD2DwBHi27Y7rpI=; b=kf5xdcac2U5YFrq1tT8ymkz6Jy3B+2QxmR4SunoCNz8JoikEaF2BtB0F3nHThsCaH4 kgoE8eI+8dCsRgEkeO2bWfg9O9fG5Abb6gUbTe6ZEx0KaEbQHMFK15hCl6XhvsaSAKNZ lghfk4GslPzMHszK+/AUWgAAQ4vFjplFvXN75D8B7xDdJiW4dXl9Uam+IbGhiPFxMTvw SXKcownEBWIchpZ9nu8i1c1dKi6iJmYV6GnAJcO2A1QXqLWDHvPkx+Jj36lsIpTMhLKg WyQC8ZVtoHouobPfuFRUcRA3D2+SeZZSFusF1A52Z9Npexb+6oPux5zo+cdI4SffUcH6 e/PQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=04AVRmD+; 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 x14si10216018edd.207.2021.03.15.07.04.25; Mon, 15 Mar 2021 07:04:49 -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=04AVRmD+; 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 S234143AbhCOOC7 (ORCPT + 99 others); Mon, 15 Mar 2021 10:02:59 -0400 Received: from mail.kernel.org ([198.145.29.99]:34198 "EHLO mail.kernel.org" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S230142AbhCON5D (ORCPT ); Mon, 15 Mar 2021 09:57:03 -0400 Received: by mail.kernel.org (Postfix) with ESMTPSA id 9FA2064DAD; Mon, 15 Mar 2021 13:57:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1615816622; bh=XaLBq42/E+mlwlGI2vk6zUz1rdgx0t7/XCw3YyfywR4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=04AVRmD+4gClfEcOGbzG4i2AikK8jqdHxX6nN1ZPeM1Q7k4k25yYed9eeVaZlo6aL Wb+6mDi1ofimx3AW3qEUMNiT3D7JBLrvou/EtAkBFO+f0WmU8mBk3JWP39h6wMH9B1 Ad9gkxT9w4T2/pv4tBWFDlQz027cBemw4AM0LsKk= From: gregkh@linuxfoundation.org To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Oliver Hartkopp , Andre Naujoks , Eric Dumazet , Oleksij Rempel , Marc Kleine-Budde Subject: [PATCH 5.4 009/168] can: skb: can_skb_set_owner(): fix ref counting if socket was closed before setting skb ownership Date: Mon, 15 Mar 2021 14:54:01 +0100 Message-Id: <20210315135550.653222790@linuxfoundation.org> X-Mailer: git-send-email 2.30.2 In-Reply-To: <20210315135550.333963635@linuxfoundation.org> References: <20210315135550.333963635@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: Greg Kroah-Hartman From: Oleksij Rempel commit e940e0895a82c6fbaa259f2615eb52b57ee91a7e upstream. There are two ref count variables controlling the free()ing of a socket: - struct sock::sk_refcnt - which is changed by sock_hold()/sock_put() - struct sock::sk_wmem_alloc - which accounts the memory allocated by the skbs in the send path. In case there are still TX skbs on the fly and the socket() is closed, the struct sock::sk_refcnt reaches 0. In the TX-path the CAN stack clones an "echo" skb, calls sock_hold() on the original socket and references it. This produces the following back trace: | WARNING: CPU: 0 PID: 280 at lib/refcount.c:25 refcount_warn_saturate+0x114/0x134 | refcount_t: addition on 0; use-after-free. | Modules linked in: coda_vpu(E) v4l2_jpeg(E) videobuf2_vmalloc(E) imx_vdoa(E) | CPU: 0 PID: 280 Comm: test_can.sh Tainted: G E 5.11.0-04577-gf8ff6603c617 #203 | Hardware name: Freescale i.MX6 Quad/DualLite (Device Tree) | Backtrace: | [<80bafea4>] (dump_backtrace) from [<80bb0280>] (show_stack+0x20/0x24) r7:00000000 r6:600f0113 r5:00000000 r4:81441220 | [<80bb0260>] (show_stack) from [<80bb593c>] (dump_stack+0xa0/0xc8) | [<80bb589c>] (dump_stack) from [<8012b268>] (__warn+0xd4/0x114) r9:00000019 r8:80f4a8c2 r7:83e4150c r6:00000000 r5:00000009 r4:80528f90 | [<8012b194>] (__warn) from [<80bb09c4>] (warn_slowpath_fmt+0x88/0xc8) r9:83f26400 r8:80f4a8d1 r7:00000009 r6:80528f90 r5:00000019 r4:80f4a8c2 | [<80bb0940>] (warn_slowpath_fmt) from [<80528f90>] (refcount_warn_saturate+0x114/0x134) r8:00000000 r7:00000000 r6:82b44000 r5:834e5600 r4:83f4d540 | [<80528e7c>] (refcount_warn_saturate) from [<8079a4c8>] (__refcount_add.constprop.0+0x4c/0x50) | [<8079a47c>] (__refcount_add.constprop.0) from [<8079a57c>] (can_put_echo_skb+0xb0/0x13c) | [<8079a4cc>] (can_put_echo_skb) from [<8079ba98>] (flexcan_start_xmit+0x1c4/0x230) r9:00000010 r8:83f48610 r7:0fdc0000 r6:0c080000 r5:82b44000 r4:834e5600 | [<8079b8d4>] (flexcan_start_xmit) from [<80969078>] (netdev_start_xmit+0x44/0x70) r9:814c0ba0 r8:80c8790c r7:00000000 r6:834e5600 r5:82b44000 r4:82ab1f00 | [<80969034>] (netdev_start_xmit) from [<809725a4>] (dev_hard_start_xmit+0x19c/0x318) r9:814c0ba0 r8:00000000 r7:82ab1f00 r6:82b44000 r5:00000000 r4:834e5600 | [<80972408>] (dev_hard_start_xmit) from [<809c6584>] (sch_direct_xmit+0xcc/0x264) r10:834e5600 r9:00000000 r8:00000000 r7:82b44000 r6:82ab1f00 r5:834e5600 r4:83f27400 | [<809c64b8>] (sch_direct_xmit) from [<809c6c0c>] (__qdisc_run+0x4f0/0x534) To fix this problem, only set skb ownership to sockets which have still a ref count > 0. Fixes: 0ae89beb283a ("can: add destructor for self generated skbs") Cc: Oliver Hartkopp Cc: Andre Naujoks Link: https://lore.kernel.org/r/20210226092456.27126-1-o.rempel@pengutronix.de Suggested-by: Eric Dumazet Signed-off-by: Oleksij Rempel Reviewed-by: Oliver Hartkopp Signed-off-by: Marc Kleine-Budde Signed-off-by: Greg Kroah-Hartman --- include/linux/can/skb.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) --- a/include/linux/can/skb.h +++ b/include/linux/can/skb.h @@ -49,8 +49,12 @@ static inline void can_skb_reserve(struc static inline void can_skb_set_owner(struct sk_buff *skb, struct sock *sk) { - if (sk) { - sock_hold(sk); + /* If the socket has already been closed by user space, the + * refcount may already be 0 (and the socket will be freed + * after the last TX skb has been freed). So only increase + * socket refcount if the refcount is > 0. + */ + if (sk && refcount_inc_not_zero(&sk->sk_refcnt)) { skb->destructor = sock_efree; skb->sk = sk; }