Received: by 2002:a05:6a10:2726:0:0:0:0 with SMTP id ib38csp728284pxb; Tue, 5 Apr 2022 20:38:08 -0700 (PDT) X-Google-Smtp-Source: ABdhPJx4oLridBWLirwZlJJQYXTYFM31iTaG1GcXsSEdGJtyDyJdQaIxhqFhB4pDpm5SujYgICxZ X-Received: by 2002:a05:6a00:2304:b0:4fa:dff2:88f5 with SMTP id h4-20020a056a00230400b004fadff288f5mr6878480pfh.11.1649216288313; Tue, 05 Apr 2022 20:38:08 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1649216288; cv=none; d=google.com; s=arc-20160816; b=coXsDBs/DitV4zTJ7W7Snv5VDop4tFO0QzUVSy6subEGlLgm7Bgbg8nkwOwv/djU2N 7fuYgS/M/1+AZzqzKHWvtUfTROUkw4vMrmA0ZXy1ETZR2PoAB+QBHdcSSNfSe8vJ/Ebh hJXvuEXl8JJ7xiv7+rqOj1kHq9GoSRPGKbUrqObvihiGQdBOj+tuYSOAQUqbHBU7PqUZ jE+/FHHXUAQpJn54AWvloTzdyz0z/U76VqgR8QdkZNpHP/3L622Iownqpn8MKlp7yrNI 8qBAmoOUjyS7HMH1MD1PZhoeyReMhiqjb31InyEvdRsiGyyIU2e6lr3rI3hJpC+PcN8c 45yg== 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=WrOoRJvBp6Zav9Sh3al8B0cUxwyOelaWSGXa6xbyJSk=; b=fqtGiWvN43wEHsPeSLSVcuD7FDW6IO1dEENuRwsGcKCQueISCiFpubuYPb22hVxdrK vJIO/R7QoOo+RpVGCirUbdKhCfzQbbTMOm8YI4b48DhKN+9yxlLUex2BYvn8HAtZwcUM EUnnen+9hIuFfMseSqoxFmFkxzo6su3ku9UQ/9gxkU2Y1tVt0uJUfq+PhT8DEI9LjNkX vboKSp6l94XuEcjzF+7cwuY5u49waCI10tVOHJqqO0f99IMqqgmSOZF/JPmoPQr6RWIP rorJqCDSnwbuJ9DbmQTzEl4ZKks8g+i8hSYcWW2DxoYJsunJ6p9uBwCqtKY2d+IliGkt vNUw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="uM/doR/A"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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 lindbergh.monkeyblade.net (lindbergh.monkeyblade.net. [2620:137:e000::1:18]) by mx.google.com with ESMTPS id u9-20020a170902e80900b00156db89bdf7si2369488plg.526.2022.04.05.20.38.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 05 Apr 2022 20:38:08 -0700 (PDT) Received-SPF: pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1:18 as permitted sender) client-ip=2620:137:e000::1:18; Authentication-Results: mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="uM/doR/A"; spf=pass (google.com: domain of linux-kernel-owner@vger.kernel.org designates 2620:137:e000::1: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: from vger.kernel.org (vger.kernel.org [23.128.96.18]) by lindbergh.monkeyblade.net (Postfix) with ESMTP id 62C2632600D; Tue, 5 Apr 2022 20:05:52 -0700 (PDT) Received: (majordomo@vger.kernel.org) by vger.kernel.org via listexpand id S1359257AbiDENKL (ORCPT + 99 others); Tue, 5 Apr 2022 09:10:11 -0400 Received: from lindbergh.monkeyblade.net ([23.128.96.19]:44274 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1344192AbiDEJSh (ORCPT ); Tue, 5 Apr 2022 05:18:37 -0400 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id 643024091C; Tue, 5 Apr 2022 02:05:09 -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 dfw.source.kernel.org (Postfix) with ESMTPS id 03B3C61003; Tue, 5 Apr 2022 09:05:09 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 147CCC385A0; Tue, 5 Apr 2022 09:05:07 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1649149508; bh=BKFbN78+A/5yk54tSfiWqE/xHZibpiB0gV7vX3QvTg4=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=uM/doR/AvMWwFz4AYcp38bRg5N+zb2j9hlCH8m8mGK5RKeUFB9dZH5g/w6j5hphv8 WwelXwT9yJw/noene5xqOPmGF0lStca/uXLWSocdbS5+yvx6czlGGMzQlI2urZmnTK 80ijXll0RIstwjLBpnis/qslda2NGVJJC8w0jD4A= 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 5.16 0740/1017] net/x25: Fix null-ptr-deref caused by x25_disconnect Date: Tue, 5 Apr 2022 09:27:33 +0200 Message-Id: <20220405070416.229821462@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220405070354.155796697@linuxfoundation.org> References: <20220405070354.155796697@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.0 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,T_SCC_BODY_TEXT_LINE autolearn=no 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 3583354a7d7f..3a171828638b 100644 --- a/net/x25/af_x25.c +++ b/net/x25/af_x25.c @@ -1765,10 +1765,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