From mboxrd@z Thu Jan 1 00:00:00 1970 Received: from mail-wm2-f4.google.com (mail-wm2-f4.google.com [74.125.225.132]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 77C8341D112 for ; Tue, 18 Aug 2026 08:11:30 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=74.125.225.132 ARC-Seal:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787040692; cv=none; b=Oak11Qv6y0er38p9k2Xre+bscDOuhHhRkFjTlafM4sqwHusT3VPfQEBCXN6akJ/w3/ts2O8B//JKVZe2nCvGJeC305JqQ9oWaCh+wNNa6UTRqSHsy2Mgp7tTv9Q8UMcYcKL2G44j371jzzF2MWbojo4NRuv2wJnXbgErwcEEv2I= ARC-Message-Signature:i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1787040692; c=relaxed/simple; bh=gf6eXb6yvLsmDAdin0XQusCflWSPPesgkp75WkmRxzw=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=MjtcdiYETJH6UxogH4FXTJFRfckoXFU57amTuHzJ3k6vW+1/fnqMF1SzROgmpRuLJbcJ11C8QgFGpFrHSoWUnf5WcfDq3cw0iYcm+Exp2daiYlYuUGySB/WoVcP94xaKeIJ/n08woAsQNZhlB3EiZgocLmBCZbddhhftZydPUvE= ARC-Authentication-Results:i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com; spf=pass smtp.mailfrom=gmail.com; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b=YsfnF5Qp; arc=none smtp.client-ip=74.125.225.132 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=gmail.com Authentication-Results: smtp.subspace.kernel.org; dkim=pass (2048-bit key) header.d=gmail.com header.i=@gmail.com header.b="YsfnF5Qp" Received: by mail-wm2-f4.google.com with SMTP id 5b1f17b1804b1-4955f00e593so11037565e9.0 for ; Tue, 18 Aug 2026 01:11:30 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20251104; t=1787040689; x=1787645489; darn=vger.kernel.org; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:from:to:cc:subject:date:message-id:reply-to:content-type; bh=DcPr6p2BuA1D1TqcoH29NRqImIBRYTBu7SBRwCO9aWs=; b=YsfnF5QpZMY/76Iv+SvxbhRjzkkhpmebhz9KW+7Se9hLVmNxKn1X+qoyd3VNVIlQbS HoP6X22WlVwfUidRStB6N7v71TjNO00pteQ/7PArmpNOtszUQJ1+2977n/5nUpWH6RwO Y45EynugjfmDwtPSnfFd1flXAmPTJYgiG5BYBVRCN9QOgrC44mOMRISvMBWCus4+Pwhz 80ujAZ9n27Z58kJDJapHrcRI00xNvygky7A4zMq7IPcqrqn/nfSOqc/IVgfCGVvRAxll njYGpFhupsjruo26jiVx45dWBq3E3ml4D1QyxOzD14XrEYt0tOmC0zehSivsFr9YQSrm EG7g== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20251104; t=1787040689; x=1787645489; h=content-transfer-encoding:mime-version:message-id:date:subject:cc :to:from:x-gm-gg:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to:content-type; bh=DcPr6p2BuA1D1TqcoH29NRqImIBRYTBu7SBRwCO9aWs=; b=ZSVUUWpef2qlFGnfI4gbsCU70FxBvFzQQ2h3GM7oKzjMDGtDygME4V8uG/o/qpgi7S SVD7FjEty3ARv64wlJ3rqSGvaxkoJmqdEhqByahe0F0NqNoM4dI6F7wQj8OPHIpoNRjG Prhn6Tts+lc3uk4Y03CN/HpekcP8tMtDXdlCKlfGCzyGLNrnAehXcmQFmiRczrG209Mf CecP28p280e4eouc/kuAG9NqM0CWRrTHDXIECeBHq63mTwcgoOFPFW1xhQ9sgLW7c4b/ X3fhlodcsql5QF5Na1seQt+jgLxOhBp+G9BBBzbOAA9J0Y1wdBwsusY3hcLREmDsDNDk japQ== X-Forwarded-Encrypted: i=1; AHgh+RrLP6/pyMhM42xrreQa972bnEab4n5ebexQ/D2sX502P5HX4DjMHNCCXH/syL3dmnyqp6utCabh1HlMv90=@vger.kernel.org X-Gm-Message-State: AOJu0YzTxDnpt33Ig4+loaWtylfTvGs+ghOYNCguAl2Dq186OY1KLCi1 gZouEaTj5UP1sUIp2QmctzHFmql34IKJpv0sHgTchkjAcLJnaVUTPLC9 X-Gm-Gg: AR+sD125o5VD40++RZTOcSdYIp8VE4TShOM69kqaqPrxlyuoyABIxJ0LldkhFUmuXcs cBX7CpstZphtM+IF3Wu6N3x1l/kguPlDC59PVWFrrf69L0cDAEefWWkjvVc7+q72HOYK+iN7lRZ rE9VIZy7vnI2PhVf+EgTZDzRCPFrPZGUCdKlRxqZ8PBRg4PgobtZjMSFJFPPIJKvoqrqxdEgZOv CHqTkpyWu1hkORwV+H4SHJHaGIJS6OcKXU/yuG2d/bQHTXXwTQYluhS1UUka1CD1ldAnNGa2OWJ MGThDJ9Fuj81Fznybi9ppbaaMoryinmR0MWkLOZ9X4tgfKyAaZIP66Z/sn9VY6aRuLDSrCF+BIo 3ipu8nDvwRlZzk6dmKA0AbE2X/8zsLLKbmDfkZ7Kbyq3Epdbx8HzzdiatGIjHX/YQT/YANanCRy nhti7nxRjX9xmxyCH4F1IUmzGfb1aeR9yp0FTbqZRNdUhM/1qwY/5uF9eQarI7l758dkJ8+1pwu N/Dtc18sBsao1E= X-Received: by 2002:a05:600c:848e:b0:499:8156:cd3f with SMTP id 5b1f17b1804b1-4999fb3f78bmr105368795e9.8.1787040688315; Tue, 18 Aug 2026 01:11:28 -0700 (PDT) Received: from serhat-ubuntu.home ([212.253.216.238]) by smtp.gmail.com with ESMTPSA id 5b1f17b1804b1-4999d10ba73sm115165235e9.13.2026.08.18.01.11.26 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 18 Aug 2026 01:11:27 -0700 (PDT) From: Serhat Kumral To: Jason Gunthorpe , Leon Romanovsky Cc: Parav Pandit , linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org, Serhat Kumral Subject: [PATCH] RDMA/core: Fix use-after-free when netns exit races compat dev removal Date: Tue, 18 Aug 2026 11:09:20 +0300 Message-ID: <20260818080920.13555-1-serhatkumral1@gmail.com> X-Mailer: git-send-email 2.53.0 Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: 8bit A compat device is removed from two places: disable_device() during the ib device unregistration, and rdma_dev_exit_net() when the netns it belongs to dies. remove_one_compat_dev() lets the xa_erase() decide which of the two performs the removal, but drops compat_devs_mutex before device_del(), so the caller that finds nothing to erase returns without waiting for the removal the other one is running. When that caller is rdma_dev_exit_net(), cleanup_net() carries on while the compat device is still alive. Its kobject is tagged with the dying net, so the device_del() still in progress dereferences net->uevent_sock after uevent_net_exit() has freed it: CPU0 (ib-unreg-wq) CPU1 (netns wq) ------------------ --------------- __ib_unregister_device() disable_device() remove_compat_devs() remove_one_compat_dev() xa_erase() device_del(cdev) ... cleanup_net() rdma_dev_exit_net() remove_one_compat_dev() xa_erase() // returns without waiting uevent_net_exit() kfree(net->uevent_sock) kobject_uevent_env() net->uevent_sock->sk // UAF BUG: KASAN: slab-use-after-free in kobject_uevent_env+0xb6f/0xc80 Read of size 8 at addr ffff888103806490 by task kworker/u16:1/41 Workqueue: ib-unreg-wq ib_unregister_work Call Trace: kobject_uevent_env+0xb6f/0xc80 device_del+0x737/0xc10 disable_device+0x1ad/0x230 __ib_unregister_device+0x229/0x3f0 Freed by task 12: kfree+0x1b3/0x550 ops_undo_list+0x273/0x8c0 cleanup_net+0x3b3/0x720 Fix by holding compat_devs_mutex across the whole removal, so that once remove_one_compat_dev() returns the compat device is gone no matter which caller removed it. Fixes: 4e0f7b907072 ("RDMA/core: Implement compat device/sysfs tree in net namespace") Assisted-by: Claude:claude-opus-5 Signed-off-by: Serhat Kumral --- With this patch applied the report no longer shows up. drivers/infiniband/core/device.c | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/drivers/infiniband/core/device.c b/drivers/infiniband/core/device.c index d954eda63134..a80ac69ef986 100644 --- a/drivers/infiniband/core/device.c +++ b/drivers/infiniband/core/device.c @@ -1005,14 +1005,20 @@ static void remove_one_compat_dev(struct ib_device *device, u32 id) { struct ib_core_device *cdev; + /* + * Hold the lock across device_del(): the other remover may have won + * the xa_erase() and still be inside device_del(), and the netns exit + * path has to wait for it instead of letting cleanup_net() free the + * netns state the compat device is still tagged with. + */ mutex_lock(&device->compat_devs_mutex); cdev = xa_erase(&device->compat_devs, id); - mutex_unlock(&device->compat_devs_mutex); if (cdev) { ib_free_port_attrs(cdev); device_del(&cdev->dev); put_device(&cdev->dev); } + mutex_unlock(&device->compat_devs_mutex); } static void remove_compat_devs(struct ib_device *device) -- 2.53.0