Received: by 2002:a05:6a10:7420:0:0:0:0 with SMTP id hk32csp4197035pxb; Mon, 21 Feb 2022 14:32:48 -0800 (PST) X-Google-Smtp-Source: ABdhPJxQi8n++kS7ET7uddB14iTNiMuE/7ZEzZLYlkxSaz9lybbut+9Y68UbhqYo1uCDfjXo5Zwv X-Received: by 2002:a05:6402:50c7:b0:410:b745:1a6c with SMTP id h7-20020a05640250c700b00410b7451a6cmr23720893edb.228.1645482768442; Mon, 21 Feb 2022 14:32:48 -0800 (PST) ARC-Seal: i=1; a=rsa-sha256; t=1645482768; cv=none; d=google.com; s=arc-20160816; b=khFj4OmTCyACeQYZHsOn2dt01iNlo9e/nQX0H22d2i/ya6igSxVD3ujQL8Lw+n9nfq +meCK/9BiKkBiFv0R5v2nVdNWK4FMvuBILDX0VpMM7dMECCPAWfSNW4zFQNLcbt7/848 mIQF4VijYS8nKUE1YfdFFQRMsPP6S3pg32Dh9FKMarS3Bdnu06ac4/98jKn/nnhqonOf Lm20K9RYbi1l/DBnLB38c6YA5f0DzLV9YFRegN7goDCUBEFKlNTA/pG7Zaoca9hLIV+x jedXzDI+bAHMUxHZ/47RjJWzu77Q1spR5NcSIxEiCYwVQ8LgQmoBxHgbqoEXnC43zVpV aOmA== 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=TD4wn95EsqR9rdT8RsLpnAOb17HIISY1E6YHAi/vtfA=; b=YeslFKIQ5l0Oj4cbCrjGrSNM0XZ5B0sMFgWS1jWsWPyeQZ29DrH2iSHm2RMh+5j0i5 yxiKTeDYmna/U8dy86XGCp3movhn3w7Yj2laRHzJFmqtCPM1IcMQ5YuqcGqrLgAUBfUN TL+FpyeYq2uYIgg2yAbKZeNqm88S4DR3lBMhaOrinRF1fLcmbLp3Kbc+cNlJURg/Q/Lb Wgt2LYmIHHHC4hBinO9g64pkODvgYl95nQPZrOJLhirTB11BMfrhqm33hw1PlOcSICO9 LWZHJ7zQS1U3a5JC5+kox8wZNCd/ujOfzqThkRXI5nnJBrH2g4K+yrPF58O7FWYIj6/n +tqg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@linuxfoundation.org header.s=korg header.b="ylf/YSyt"; 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 hw8si10452128ejc.598.2022.02.21.14.32.25; Mon, 21 Feb 2022 14:32:48 -0800 (PST) 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="ylf/YSyt"; 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 S1347757AbiBUJJp (ORCPT + 99 others); Mon, 21 Feb 2022 04:09:45 -0500 Received: from mxb-00190b01.gslb.pphosted.com ([23.128.96.19]:42800 "EHLO lindbergh.monkeyblade.net" rhost-flags-OK-OK-OK-OK) by vger.kernel.org with ESMTP id S1347550AbiBUJFw (ORCPT ); Mon, 21 Feb 2022 04:05:52 -0500 Received: from dfw.source.kernel.org (dfw.source.kernel.org [IPv6:2604:1380:4641:c500::1]) by lindbergh.monkeyblade.net (Postfix) with ESMTPS id B922C2FFD3; Mon, 21 Feb 2022 00:59:16 -0800 (PST) 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 5690D61202; Mon, 21 Feb 2022 08:59:16 +0000 (UTC) Received: by smtp.kernel.org (Postfix) with ESMTPSA id 3DD2FC340E9; Mon, 21 Feb 2022 08:59:15 +0000 (UTC) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=linuxfoundation.org; s=korg; t=1645433955; bh=gD0Sn3OaPOUOkHnhQNdnZwVetF5O3MEMg+qMvhTqXvI=; h=From:To:Cc:Subject:Date:In-Reply-To:References:From; b=ylf/YSytXVi270jdFDnsmE0ryIq/lHKonO2qSbF9tmi/MC3rTHkO17CS1iBBm8Eqn 58+wlZu+8WQTZrN0ZIYI8eeWdYMNl55X8t5yIBavvORWIPZuiWSBAdEmUdVpPEYZNn W54Yu6yp+Mykortwszs8BdfVQpArlxK2WViXFoqQ= From: Greg Kroah-Hartman To: linux-kernel@vger.kernel.org Cc: Greg Kroah-Hartman , stable@vger.kernel.org, Duoming Zhou , "David S. Miller" , Sasha Levin Subject: [PATCH 5.4 15/80] ax25: improve the incomplete fix to avoid UAF and NPD bugs Date: Mon, 21 Feb 2022 09:48:55 +0100 Message-Id: <20220221084916.086495968@linuxfoundation.org> X-Mailer: git-send-email 2.35.1 In-Reply-To: <20220221084915.554151737@linuxfoundation.org> References: <20220221084915.554151737@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.1 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 4e0f718daf97d47cf7dec122da1be970f145c809 ] The previous commit 1ade48d0c27d ("ax25: NPD bug when detaching AX25 device") introduce lock_sock() into ax25_kill_by_device to prevent NPD bug. But the concurrency NPD or UAF bug will occur, when lock_sock() or release_sock() dereferences the ax25_cb->sock. The NULL pointer dereference bug can be shown as below: ax25_kill_by_device() | ax25_release() | ax25_destroy_socket() | ax25_cb_del() ... | ... | ax25->sk=NULL; lock_sock(s->sk); //(1) | s->ax25_dev = NULL; | ... release_sock(s->sk); //(2) | ... | The root cause is that the sock is set to null before dereference site (1) or (2). Therefore, this patch extracts the ax25_cb->sock in advance, and uses ax25_list_lock to protect it, which can synchronize with ax25_cb_del() and ensure the value of sock is not null before dereference sites. The concurrency UAF bug can be shown as below: ax25_kill_by_device() | ax25_release() | ax25_destroy_socket() ... | ... | sock_put(sk); //FREE lock_sock(s->sk); //(1) | s->ax25_dev = NULL; | ... release_sock(s->sk); //(2) | ... | The root cause is that the sock is released before dereference site (1) or (2). Therefore, this patch uses sock_hold() to increase the refcount of sock and uses ax25_list_lock to protect it, which can synchronize with ax25_cb_del() in ax25_destroy_socket() and ensure the sock wil not be released before dereference sites. Signed-off-by: Duoming Zhou Signed-off-by: David S. Miller Signed-off-by: Sasha Levin --- net/ax25/af_ax25.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/net/ax25/af_ax25.c b/net/ax25/af_ax25.c index 1f84d41e22c36..184af6da0defc 100644 --- a/net/ax25/af_ax25.c +++ b/net/ax25/af_ax25.c @@ -77,6 +77,7 @@ static void ax25_kill_by_device(struct net_device *dev) { ax25_dev *ax25_dev; ax25_cb *s; + struct sock *sk; if ((ax25_dev = ax25_dev_ax25dev(dev)) == NULL) return; @@ -85,13 +86,15 @@ static void ax25_kill_by_device(struct net_device *dev) again: ax25_for_each(s, &ax25_list) { if (s->ax25_dev == ax25_dev) { + sk = s->sk; + sock_hold(sk); spin_unlock_bh(&ax25_list_lock); - lock_sock(s->sk); + lock_sock(sk); s->ax25_dev = NULL; - release_sock(s->sk); + release_sock(sk); ax25_disconnect(s, ENETUNREACH); spin_lock_bh(&ax25_list_lock); - + sock_put(sk); /* The entry could have been deleted from the * list meanwhile and thus the next pointer is * no longer valid. Play it safe and restart -- 2.34.1