From nobody Tue Dec 16 19:46:25 2025 Received: from out-181.mta0.migadu.com (out-181.mta0.migadu.com [91.218.175.181]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by smtp.subspace.kernel.org (Postfix) with ESMTPS id 105542AD0F for ; Fri, 21 Feb 2025 02:06:23 +0000 (UTC) Authentication-Results: smtp.subspace.kernel.org; arc=none smtp.client-ip=91.218.175.181 ARC-Seal: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740103586; cv=none; b=i37SDMLlyCaDYc396mMsK+ydQ//lIqeiVB4TKK5/X1ltAGOq3piW3M77uhJJ/MNQJYbwGl5MQn9uzylh4K9ctutgRTcyvRwG3Wtkl0r9nUaYX3LKLnUiCzcGD3+wLLkZfzP+Hasb+LRp57s7eJLPgqfjhwCIyrz1f5GcqL/ylpg= ARC-Message-Signature: i=1; a=rsa-sha256; d=subspace.kernel.org; s=arc-20240116; t=1740103586; c=relaxed/simple; bh=7bUYJkVR4LhZ5MDpF+WhZIC+c5Z7P28KhdMyqRYAle4=; h=From:To:Cc:Subject:Date:Message-ID:MIME-Version; b=s9x5KmFeGoo7KaVtR23bN9j8H937Gea86tR+n77tuoeoqZMuNpZcUal2+Gj7GVfITMT5Pj/6US7fTmsQMt7+kIX/VRcADtgMkfLsmq6tw44TZ2DM6MkIlxvhMPXgY0nld5YiLMMicUkHK5Rda34snh2p8FDSbg/qMUsSoiro4mY= ARC-Authentication-Results: i=1; smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev; spf=pass smtp.mailfrom=linux.dev; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b=BGD2xc4s; arc=none smtp.client-ip=91.218.175.181 Authentication-Results: smtp.subspace.kernel.org; dmarc=pass (p=none dis=none) header.from=linux.dev Authentication-Results: smtp.subspace.kernel.org; spf=pass smtp.mailfrom=linux.dev Authentication-Results: smtp.subspace.kernel.org; dkim=pass (1024-bit key) header.d=linux.dev header.i=@linux.dev header.b="BGD2xc4s" X-Report-Abuse: Please report any abuse attempt to abuse@migadu.com and include these headers. DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=linux.dev; s=key1; t=1740103569; h=from:from:reply-to:subject:subject:date:date:message-id:message-id: to:to:cc:cc:mime-version:mime-version: content-transfer-encoding:content-transfer-encoding; bh=ZV49fxUaLuaAIe94fEcao8tPpI/oFMeLfe5oAC8i2bM=; b=BGD2xc4sfo+EqSf0zxzex/zxxMANPBaXiq2p7+KZWEykfR1QHb5gVtFWk0yLdr0rUw3Iyh NwOKqlTnAa2zDXoHBdNsc44d7nMwJdmhzFeROxu6XVIJ0cXz4a7UhYK86bPqRzh+JZ9gri WNK/cIvXMSSoyYUlttZ6IYocwG8tOWw= From: Roman Gushchin To: Jason Gunthorpe Cc: Roman Gushchin , Leon Romanovsky , Maher Sanalla , Parav Pandit , linux-rdma@vger.kernel.org, linux-kernel@vger.kernel.org Subject: [PATCH] RDMA/core: fix a NULL-pointer dereference in hw_stat_device_show() Date: Fri, 21 Feb 2025 02:05:55 +0000 Message-ID: <20250221020555.4090014-1-roman.gushchin@linux.dev> Precedence: bulk X-Mailing-List: linux-kernel@vger.kernel.org List-Id: List-Subscribe: List-Unsubscribe: MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Migadu-Flow: FLOW_OUT Content-Type: text/plain; charset="utf-8" Commit 54747231150f ("RDMA: Introduce and use rdma_device_to_ibdev()") introduced rdma_device_to_ibdev() helper which has to be used to obtain an ib_device pointer from a device pointer. hw_stat_device_show() and hw_stat_device_store() were missed. It causes a NULL pointer dereference panic on an attempt to read hw counters from a namespace, when the device structure is not embedded into the ib_device structure. In this case casting the device pointer into the ib_device pointer using container_of() is wrong. Instead, rdma_device_to_ibdev() should be used, which uses the back-reference (container_of(device, struct ib_core_device, dev))->owner. [42021.807566] BUG: kernel NULL pointer dereference, address: 0000000000000= 028 [42021.814463] #PF: supervisor read access in kernel mode [42021.819549] #PF: error_code(0x0000) - not-present page [42021.824636] PGD 0 P4D 0 [42021.827145] Oops: 0000 [#1] SMP PTI [42021.830598] CPU: 82 PID: 2843922 Comm: switchto-defaul Kdump: loaded Tai= nted: G S W I XXX [42021.841697] Hardware name: XXX [42021.849619] RIP: 0010:hw_stat_device_show+0x1e/0x40 [ib_core] [42021.855362] Code: 90 90 90 90 90 90 90 90 90 90 90 90 f3 0f 1e fa 0f 1f = 44 00 00 49 89 d0 4c 8b 5e 20 48 8b 8f b8 04 00 00 48 81 c7 f0 fa ff ff <48= > 8b 41 28 48 29 ce 48 83 c6 d0 48 c1 ee 04 69 d6 ab aa aa aa 48 [42021.873931] RSP: 0018:ffff97fe90f03da0 EFLAGS: 00010287 [42021.879108] RAX: ffff9406988a8c60 RBX: ffff940e1072d438 RCX: 00000000000= 00000 [42021.886169] RDX: ffff94085f1aa000 RSI: ffff93c6cbbdbcb0 RDI: ffff940c751= 7aef0 [42021.893230] RBP: ffff97fe90f03e70 R08: ffff94085f1aa000 R09: 00000000000= 00000 [42021.900294] R10: ffff94085f1aa000 R11: ffffffffc0775680 R12: ffffffff87c= a2530 [42021.907355] R13: ffff940651602840 R14: ffff93c6cbbdbcb0 R15: ffff94085f1= aa000 [42021.914418] FS: 00007fda1a3b9700(0000) GS:ffff94453fb80000(0000) knlGS:= 0000000000000000 [42021.922423] CS: 0010 DS: 0000 ES: 0000 CR0: 0000000080050033 [42021.928130] CR2: 0000000000000028 CR3: 00000042dcfb8003 CR4: 00000000003= 726f0 [42021.935194] DR0: 0000000000000000 DR1: 0000000000000000 DR2: 00000000000= 00000 [42021.942257] DR3: 0000000000000000 DR6: 00000000fffe0ff0 DR7: 00000000000= 00400 [42021.949324] Call Trace: [42021.951756] [42021.953842] [] ? show_regs+0x64/0x70 [42021.959030] [] ? __die+0x78/0xc0 [42021.963874] [] ? page_fault_oops+0x2b5/0x3b0 [42021.969749] [] ? exc_page_fault+0x1a2/0x3c0 [42021.975549] [] ? asm_exc_page_fault+0x26/0x30 [42021.981517] [] ? __pfx_show_hw_stats+0x10/0x10 [ib_co= re] [42021.988482] [] ? hw_stat_device_show+0x1e/0x40 [ib_co= re] [42021.995438] [] dev_attr_show+0x1e/0x50 [42022.000803] [] sysfs_kf_seq_show+0x81/0xe0 [42022.006508] [] seq_read_iter+0xf4/0x410 [42022.011954] [] vfs_read+0x16e/0x2f0 [42022.017058] [] ksys_read+0x6e/0xe0 [42022.022073] [] do_syscall_64+0x6a/0xa0 [42022.027441] [] entry_SYSCALL_64_after_hwframe+0x78/0x= e2 Fixes: 54747231150f ("RDMA: Introduce and use rdma_device_to_ibdev()") Signed-off-by: Roman Gushchin Cc: Jason Gunthorpe Cc: Leon Romanovsky Cc: Maher Sanalla Cc: Parav Pandit Cc: linux-rdma@vger.kernel.org Cc: linux-kernel@vger.kernel.org --- drivers/infiniband/core/sysfs.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/drivers/infiniband/core/sysfs.c b/drivers/infiniband/core/sysf= s.c index 7491328ca5e6..0be77b8abeae 100644 --- a/drivers/infiniband/core/sysfs.c +++ b/drivers/infiniband/core/sysfs.c @@ -148,7 +148,7 @@ static ssize_t hw_stat_device_show(struct device *dev, { struct hw_stats_device_attribute *stat_attr =3D container_of(attr, struct hw_stats_device_attribute, attr); - struct ib_device *ibdev =3D container_of(dev, struct ib_device, dev); + struct ib_device *ibdev =3D rdma_device_to_ibdev(dev); =20 return stat_attr->show(ibdev, ibdev->hw_stats_data->stats, stat_attr - ibdev->hw_stats_data->attrs, 0, buf); @@ -160,7 +160,7 @@ static ssize_t hw_stat_device_store(struct device *dev, { struct hw_stats_device_attribute *stat_attr =3D container_of(attr, struct hw_stats_device_attribute, attr); - struct ib_device *ibdev =3D container_of(dev, struct ib_device, dev); + struct ib_device *ibdev =3D rdma_device_to_ibdev(dev); =20 return stat_attr->store(ibdev, ibdev->hw_stats_data->stats, stat_attr - ibdev->hw_stats_data->attrs, 0, buf, --=20 2.48.1.601.g30ceb7b040-goog