Received: by 2002:a05:6602:2086:0:0:0:0 with SMTP id a6csp4465120ioa; Wed, 27 Apr 2022 04:35:39 -0700 (PDT) X-Google-Smtp-Source: ABdhPJz2RIsF9nwimbZEgie+OqdRLq0XuAJA2cXtkHNeMUZOcPWkQqFy4lDwUH3HwQQvenufgRJ3 X-Received: by 2002:a17:902:b698:b0:158:faee:442f with SMTP id c24-20020a170902b69800b00158faee442fmr28816813pls.75.1651059339097; Wed, 27 Apr 2022 04:35:39 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1651059339; cv=none; d=google.com; s=arc-20160816; b=O2EzIl78+5RLqytTfcel66DESMa8z56Xp01WgzFWZlmdxhwdySAQyn7EVoKGhOhlv6 XXLqrHjUhvZ047UQRvRs/jqYHcdRA4tOf3umAh9U5SPqbRnTD9nQbMm99m+dWCCwsAwn ALZsDTdyoFTM+vrjy233G9NdoXEvPDAGx+OjGeRo6yH+Qi8L510f/WZDw6tNW3wK/Rwn zfdQi+wpHQbJMgNJg+osGJvtmfSlzHmdcPuR6TUNeXwB734wRjbGteCg4GUJT8EBPBgN +GIKseV0Mg9gMc9fVYDZMjqJ1uIaM6WZOJTMoRJtIntc0texAlHI3s7Kpk8779m83+cn QWXA== 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=C+uckDV3r2AWGw1GgNNdxIDagyIh7fFYOVYcrbp/TCU=; b=D4wHuqwHKWGMmsrTqaI5jVLtBJK/RMKn19vmquaq4sT2IcflIwSKc3CQGATUORSm2M V+xqYytfJmPRKN36rTO1wm32/Hw2RYullcyEEev94qM+7RjWWLHDK7aAD3o7FJ0ImUUq 52b1MT8/WYGw53tC6i2tEYyQinQXztfDxkIIvNd/bYGcjlEZwFr8jukwKh4iIdG3yL+d 3FtjPICcKz07prKY+Dt+fw/BJA3VmrRvg8qRgeQvRfN5y89v2fhOkgkZc69tFea/d3K2 5IOfMnxBGYB0gNKkaGIwnw+SEyLkL9fnDu68C9XXr3iYSvSe3EAdQS0akFjRxDZRwboN 8j2w== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=J5CpA9P0; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [23.128.96.19]) by mx.google.com with ESMTPS id e6-20020a17090301c600b0015d56a3ccc8si1525750plh.71.2022.04.27.04.35.38 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 27 Apr 2022 04:35:39 -0700 (PDT) Received-SPF: softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) client-ip=23.128.96.19; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=J5CpA9P0; spf=softfail (google.com: domain of transitioning linux-kernel-owner@vger.kernel.org does not designate 23.128.96.19 as permitted sender) smtp.mailfrom=linux-kernel-owner@vger.kernel.org; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=linuxfoundation.org Received: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id B7D36434028; Wed, 27 Apr 2022 03:25:07 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1346091AbiDZIj6 (ORCPT + 99 others); Tue, 26 Apr 2022 04:39:58 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:36234 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1345675AbiDZIe5 (ORCPT ); Tue, 26 Apr 2022 04:34:57 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id C98BE7A9BD; Tue, 26 Apr 2022 01:28:03 -0700 (PDT) Received: from smtp.kernel.org (relay.kernel.org [52.25.139.140]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by ams.source.kernel.org (Postfix) with ESMTPS id 5A6BAB81CAF; Tue, 26 Apr 2022 08:28:02 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id BDD61C385A4; Tue, 26 Apr 2022 08:28:00 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650961681; bh=RUvmbQXRPebHo8224VQKMTezWYzBBTmR1kWFAlwCiA4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=J5CpA9P0N57aoK02auBdPZrHv12Kem77ePFDTxthkIMzsE6cpMNstRFdiCCkvVsjM ff8hS0VnmalsqstcT2TNrXqvgMKtyQag0YYAfXXbIA7crUK9IfXWKItYihNMAm54fV m8DgeB9Tzb6QSTIN7MVH9MGjvzELQpNqg+NUv1TM= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org, stable@vger.kernel.org Cc: Greg Kroah-Hartman , Thomas Osterried , Duoming Zhou , "David S. Miller" , Ovidiu Panait Subject: [PATCH 4.19 48/53] ax25: Fix refcount leaks caused by ax25_cb_del() Date: Tue, 26 Apr 2022 10:21:28 +0200 Message-Id: <20220426081737.060499628@linuxfoundation.org> X-Mailer: git-send-email 2.36.0 In-Reply-To: <20220426081735.651926456@linuxfoundation.org> References: <20220426081735.651926456@linuxfoundation.org> User-Agent: quilt/0.66 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-2.5 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,HEADER_FROM_DIFFERENT_DOMAINS, MAILING_LIST_MULTI,RDNS_NONE,SPF_HELO_NONE autolearn=unavailable autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on lindbergh.monkeyblade.net Precedence: bulk List-ID: X-Mailing-List: linux-kernel@vger.kernel.org From: Duoming Zhou commit 9fd75b66b8f68498454d685dc4ba13192ae069b0 upstream. The previous commit d01ffb9eee4a ("ax25: add refcount in ax25_dev to avoid UAF bugs") and commit feef318c855a ("ax25: fix UAF bugs of net_device caused by rebinding operation") increase the refcounts of ax25_dev and net_device in ax25_bind() and decrease the matching refcounts in ax25_kill_by_device() in order to prevent UAF bugs, but there are reference count leaks. The root cause of refcount leaks is shown below: (Thread 1) | (Thread 2) ax25_bind() | ... | ax25_addr_ax25dev() | ax25_dev_hold() //(1) | ... | dev_hold_track() //(2) | ... | ax25_destroy_socket() | ax25_cb_del() | ... | hlist_del_init() //(3) | | (Thread 3) | ax25_kill_by_device() | ... | ax25_for_each(s, &ax25_list) { | if (s->ax25_dev == ax25_dev) //(4) | ... | Firstly, we use ax25_bind() to increase the refcount of ax25_dev in position (1) and increase the refcount of net_device in position (2). Then, we use ax25_cb_del() invoked by ax25_destroy_socket() to delete ax25_cb in hlist in position (3) before calling ax25_kill_by_device(). Finally, the decrements of refcounts in ax25_kill_by_device() will not be executed, because no s->ax25_dev equals to ax25_dev in position (4). This patch adds decrements of refcounts in ax25_release() and use lock_sock() to do synchronization. If refcounts decrease in ax25_release(), the decrements of refcounts in ax25_kill_by_device() will not be executed and vice versa. Fixes: d01ffb9eee4a ("ax25: add refcount in ax25_dev to avoid UAF bugs") Fixes: 87563a043cef ("ax25: fix reference count leaks of ax25_dev") Fixes: feef318c855a ("ax25: fix UAF bugs of net_device caused by rebinding operation") Reported-by: Thomas Osterried Signed-off-by: Duoming Zhou Signed-off-by: David S. Miller [OP: backport to 4.19: adjust dev_put_track()->dev_put()] Signed-off-by: Ovidiu Panait Signed-off-by: Greg Kroah-Hartman --- net/ax25/af_ax25.c | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) --- a/net/ax25/af_ax25.c +++ b/net/ax25/af_ax25.c @@ -101,8 +101,10 @@ again: spin_unlock_bh(&ax25_list_lock); lock_sock(sk); s->ax25_dev = NULL; - dev_put(ax25_dev->dev); - ax25_dev_put(ax25_dev); + if (sk->sk_socket) { + dev_put(ax25_dev->dev); + ax25_dev_put(ax25_dev); + } release_sock(sk); ax25_disconnect(s, ENETUNREACH); spin_lock_bh(&ax25_list_lock); @@ -981,14 +983,20 @@ static int ax25_release(struct socket *s { struct sock *sk = sock->sk; ax25_cb *ax25; + ax25_dev *ax25_dev; if (sk == NULL) return 0; sock_hold(sk); - sock_orphan(sk); lock_sock(sk); + sock_orphan(sk); ax25 = sk_to_ax25(sk); + ax25_dev = ax25->ax25_dev; + if (ax25_dev) { + dev_put(ax25_dev->dev); + ax25_dev_put(ax25_dev); + } if (sk->sk_type == SOCK_SEQPACKET) { switch (ax25->state) {