Received: by 2002:a05:6a10:6d10:0:0:0:0 with SMTP id gq16csp2918032pxb; Mon, 18 Apr 2022 11:01:35 -0700 (PDT) X-Google-Smtp-Source: ABdhPJw18mJr5+HgU7OZipqQSFXVgY7BtzqLeOK3SQRPC7UQ2STurVWuC7staB+UJcfO+ed8bt4R X-Received: by 2002:a05:6402:4499:b0:41d:7e83:8565 with SMTP id er25-20020a056402449900b0041d7e838565mr13418779edb.332.1650304895029; Mon, 18 Apr 2022 11:01:35 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1650304894; cv=none; d=google.com; s=arc-20160816; b=A07zIuvAHKxOmpQxrSRaD8zPFZkCY8HyhsmkDM9wE9a4ETfbLZQOJEOBKgV5TfNfv8 t538zEcjRX/KgKcvnLn84WHs4TF4CTyMPk84BoCeGD3HVm3PBJqIZ4hDvqwRlL9YlD/W EatTwAt33OpLCPugvoP6Iz9D95xJBOQIDC5T9cQH/t7LCl6dTwlwo1ehlLKyM6GxZOTt A8S0WLF9OJIzqD4bpRjkUjnzbc/q4iY+YqeqNFtsXMcHyGa0xQ3YrK1tbTzPW9tx1IL6 7PJm6xlEddGx9N1WIhdJZ+bFf35xgtVJfERnZlYKAzCtdg0hvHXr1o2UsjgGFovnw2FE j/3w== 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=EFj4aZfF2y/e2TC4/HH7EPbbiGfHWVGG+3iQnQNLMXo=; b=ckXsjEHJ3eCut6Muhd/fGFg9Oq8rDoBXrGIZlHlru8vwNjiNeWas5GSlD3V1Ptj0yI Cmg4TyuTRU/7M86o0sya7/LE+XLgA5Hh6qLNeJxbZPTBY2+LGfHpfUr3ST6/5akng3ps rwJZ8+j6uKfiKHhUtxsGlbGZad/zH3v0ksVxRWHGeauzX5mgx9itj2Ck3mmg27xV4ecg LidPO/DNt0Ti+QCclHtq8tcB2IhyQeViYUCCBjbyG34Tb17skES7oECnRttimT5rVUF3 U4zSIUO+/63fMOmpbXtPVInRvUJOhHfkmLPiao+TGs/vpI3+WUEq94lXP6/l4sMD//nV S+nA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WnsinLxN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 out1.vger.email (out1.vger.email. [2620:137:e000::1:20]) by mx.google.com with ESMTP id z18-20020a05640240d200b00423e88cfbe1si2388514edb.588.2022.04.18.11.01.10; Mon, 18 Apr 2022 11:01:34 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 as permitted sender) client-ip=2620:137:e000::1:20; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b=WnsinLxN; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:20 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 S244305AbiDRNur (ORCPT + 99 others); Mon, 18 Apr 2022 09:50:47 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:41180 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S244656AbiDRNaq (ORCPT ); Mon, 18 Apr 2022 09:30:46 -0400 Received: from ams.source.kernel.org (ams.source.kernel.org [145.40.68.75]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id D7839658C; Mon, 18 Apr 2022 05:54:57 -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 5AAA5B80EBA; Mon, 18 Apr 2022 12:54:54 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id ABE8EC385A1; Mon, 18 Apr 2022 12:54:52 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1650286493; bh=raBcB2yEX7qZk3a4wK2LfDY148vi/GI9qOPBsM4Powo=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=WnsinLxNa81s2id6JsM6xdaA0j4VUHQRFsaFM077jkcFBiGdeinLQYFEYqUKLdJ8Y TAKCVU75eHnX8e/CZeXWNdn55wJWJeu9PBkXxcaPsDNX68fcd2ehB9CQZoa978x4p5 RXD4T7F+BHadz3GmqIyMvsgVU6ymx3KuLNtskhoA= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Duoming Zhou , Lin Ma , "David S. Miller" , Sasha Levin Subject: [PATCH 4.14 152/284] net/x25: Fix null-ptr-deref caused by x25_disconnect Date: Mon, 18 Apr 2022 14:12:13 +0200 Message-Id: <20220418121215.991566555@linuxfoundation.org> X-Mailer: git-send-email 2.35.3 In-Reply-To: <20220418121210.689577360@linuxfoundation.org> References: <20220418121210.689577360@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=-7.7 required=5.0 tests=BAYES_00,DKIMWL_WL_HIGH, DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,RCVD_IN_DNSWL_HI, SPF_HELO_NONE,SPF_PASS,T_SCC_BODY_TEXT_LINE autolearn=ham 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 [ Upstream commit 7781607938c8371d4c2b243527430241c62e39c2 ] When the link layer is terminating, x25->neighbour will be set to NULL in x25_disconnect(). As a result, it could cause null-ptr-deref bugs in x25_sendmsg(),x25_recvmsg() and x25_connect(). One of the bugs is shown below. (Thread 1) | (Thread 2) x25_link_terminated() | x25_recvmsg() x25_kill_by_neigh() | ... x25_disconnect() | lock_sock(sk) ... | ... x25->neighbour = NULL //(1) | ... | x25->neighbour->extended //(2) The code sets NULL to x25->neighbour in position (1) and dereferences x25->neighbour in position (2), which could cause null-ptr-deref bug. This patch adds lock_sock() in x25_kill_by_neigh() in order to synchronize with x25_sendmsg(), x25_recvmsg() and x25_connect(). What`s more, the sock held by lock_sock() is not NULL, because it is extracted from x25_list and uses x25_list_lock to synchronize. Fixes: 4becb7ee5b3d ("net/x25: Fix x25_neigh refcnt leak when x25 disconnect") Signed-off-by: Duoming Zhou Reviewed-by: Lin Ma Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/x25/af_x25.c | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/net/x25/af_x25.c b/net/x25/af_x25.c index fd0a6c6c77b6..e103ec39759f 100644 --- a/net/x25/af_x25.c +++ b/net/x25/af_x25.c @@ -1796,10 +1796,15 @@ void x25_kill_by_neigh(struct x25_neigh *nb) write_lock_bh(&x25_list_lock); - sk_for_each(s, &x25_list) - if (x25_sk(s)->neighbour == nb) + sk_for_each(s, &x25_list) { + if (x25_sk(s)->neighbour == nb) { + write_unlock_bh(&x25_list_lock); + lock_sock(s); x25_disconnect(s, ENETUNREACH, 0, 0); - + release_sock(s); + write_lock_bh(&x25_list_lock); + } + } write_unlock_bh(&x25_list_lock); /* Remove any related forwards */ -- 2.34.1