All of lore.kernel.org
 help / color / mirror / Atom feed
From: Sasha Levin <sashal@kernel.org>
To: linux-kernel@vger.kernel.org, stable@vger.kernel.org
Cc: Duoming Zhou <duoming@zju.edu.cn>,
	"David S . Miller" <davem@davemloft.net>,
	Sasha Levin <sashal@kernel.org>,
	jreuter@yaina.de, kuba@kernel.org, linux-hams@vger.kernel.org,
	netdev@vger.kernel.org
Subject: [PATCH AUTOSEL 5.15 15/36] ax25: improve the incomplete fix to avoid UAF and NPD bugs
Date: Wed,  9 Feb 2022 13:37:38 -0500	[thread overview]
Message-ID: <20220209183759.47134-15-sashal@kernel.org> (raw)
In-Reply-To: <20220209183759.47134-1-sashal@kernel.org>

From: Duoming Zhou <duoming@zju.edu.cn>

[ 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 <duoming@zju.edu.cn>
Signed-off-by: David S. Miller <davem@davemloft.net>
Signed-off-by: Sasha Levin <sashal@kernel.org>
---
 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 7473e0cc6d469..ea3431ac46a14 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


  parent reply	other threads:[~2022-02-09 18:37 UTC|newest]

Thread overview: 46+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-02-09 18:37 [PATCH AUTOSEL 5.15 01/36] platform/x86: touchscreen_dmi: Add info for the RWC NANOTE P8 AY07J 2-in-1 Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 02/36] platform/x86: ISST: Fix possible circular locking dependency detected Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 03/36] kunit: tool: Import missing importlib.abc Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 04/36] selftests: rtc: Increase test timeout so that all tests run Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 05/36] kselftest: signal all child processes Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 06/36] selftests: netfilter: check stateless nat udp checksum fixup Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 07/36] net: ieee802154: at86rf230: Stop leaking skb's Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 08/36] selftests/zram: Skip max_comp_streams interface on newer kernel Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 09/36] selftests/zram01.sh: Fix compression ratio calculation Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 10/36] selftests/zram: Adapt the situation that /dev/zram0 is being used Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 11/36] selftests: openat2: Print also errno in failure messages Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 12/36] selftests: openat2: Add missing dependency in Makefile Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 13/36] selftests: openat2: Skip testcases that fail with EOPNOTSUPP Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 14/36] selftests: skip mincore.check_file_mmap when fs lacks needed support Sasha Levin
2022-02-09 18:37 ` Sasha Levin [this message]
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 16/36] pinctrl: bcm63xx: fix unmet dependency on REGMAP for GPIO_REGMAP Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 17/36] vfs: make freeze_super abort when sync_filesystem returns error Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 18/36] quota: make dquot_quota_sync return errors from ->sync_fs Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 19/36] iommu: Fix potential use-after-free during probe Sasha Levin
2022-02-09 18:37   ` Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 20/36] scsi: pm80xx: Fix double completion for SATA devices Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 21/36] kselftest: Fix vdso_test_abi return status Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 22/36] scsi: core: Reallocate device's budget map on queue depth change Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 23/36] scsi: pm8001: Fix use-after-free for aborted TMF sas_task Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 24/36] scsi: pm8001: Fix use-after-free for aborted SSP/STP sas_task Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 25/36] drm/amd: Warn users about potential s0ix problems Sasha Levin
2022-02-09 18:37   ` Sasha Levin
2022-02-09 18:37   ` Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 26/36] nvme: fix a possible use-after-free in controller reset during load Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 27/36] nvme-tcp: fix possible use-after-free in transport error_recovery work Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 28/36] nvme-rdma: " Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 29/36] net: sparx5: do not refer to skb after passing it on Sasha Levin
2022-02-09 18:37   ` Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 30/36] drm/amd: add support to check whether the system is set to s3 Sasha Levin
2022-02-09 18:37   ` Sasha Levin
2022-02-09 18:37   ` Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 31/36] drm/amd: Only run s3 or s0ix if system is configured properly Sasha Levin
2022-02-09 18:37   ` Sasha Levin
2022-02-09 18:37   ` Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 32/36] drm/amdgpu: fix logic inversion in check Sasha Levin
2022-02-09 18:37   ` Sasha Levin
2022-02-09 18:37   ` Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 33/36] x86/Xen: streamline (and fix) PV CPU enumeration Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 34/36] Revert "module, async: async_synchronize_full() on module init iff async is used" Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 35/36] gcc-plugins/stackleak: Use noinstr in favor of notrace Sasha Levin
2022-02-09 18:37 ` [PATCH AUTOSEL 5.15 36/36] random: wake up /dev/random writers after zap Sasha Levin

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20220209183759.47134-15-sashal@kernel.org \
    --to=sashal@kernel.org \
    --cc=davem@davemloft.net \
    --cc=duoming@zju.edu.cn \
    --cc=jreuter@yaina.de \
    --cc=kuba@kernel.org \
    --cc=linux-hams@vger.kernel.org \
    --cc=linux-kernel@vger.kernel.org \
    --cc=netdev@vger.kernel.org \
    --cc=stable@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.