public inbox for linux-bluetooth@vger.kernel.org
 help / color / mirror / Atom feed
* [PATCH v2 0/3] Bluetooth: Fix a few module init/deinit bugs
@ 2024-10-04 23:04 Aaron Thompson
  2024-10-04 23:04 ` [PATCH v2 1/3] Bluetooth: ISO: Fix multiple init when debugfs is disabled Aaron Thompson
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Aaron Thompson @ 2024-10-04 23:04 UTC (permalink / raw)
  To: Johan Hedberg, Luiz Augusto von Dentz, Marcel Holtmann,
	linux-bluetooth, linux-kernel
  Cc: stable, Aaron Thompson

Hi all,

These patches fix a few bugs in init and deinit for the bluetooth
module. I ran into the first one when I started running a kernel with
debugfs disabled on my laptop, and I ran into the next two when I was
working on the fix for the first one.

v2:
  * Move iso_exit() call to bt_exit() (Luiz Augusto von Dentz)

Aaron Thompson (3):
  Bluetooth: ISO: Fix multiple init when debugfs is disabled
  Bluetooth: Call iso_exit() on module unload
  Bluetooth: Remove debugfs directory on module init failure

 net/bluetooth/af_bluetooth.c | 3 +++
 net/bluetooth/iso.c          | 6 +-----
 2 files changed, 4 insertions(+), 5 deletions(-)


base-commit: 9852d85ec9d492ebef56dc5f229416c925758edc
-- 
2.39.5


^ permalink raw reply	[flat|nested] 5+ messages in thread

* [PATCH v2 1/3] Bluetooth: ISO: Fix multiple init when debugfs is disabled
  2024-10-04 23:04 [PATCH v2 0/3] Bluetooth: Fix a few module init/deinit bugs Aaron Thompson
@ 2024-10-04 23:04 ` Aaron Thompson
  2024-10-04 23:04 ` [PATCH v2 2/3] Bluetooth: Call iso_exit() on module unload Aaron Thompson
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Aaron Thompson @ 2024-10-04 23:04 UTC (permalink / raw)
  To: Johan Hedberg, Luiz Augusto von Dentz, Marcel Holtmann,
	linux-bluetooth, linux-kernel
  Cc: stable, Aaron Thompson

From: Aaron Thompson <dev@aaront.org>

If bt_debugfs is not created successfully, which happens if either
CONFIG_DEBUG_FS or CONFIG_DEBUG_FS_ALLOW_ALL is unset, then iso_init()
returns early and does not set iso_inited to true. This means that a
subsequent call to iso_init() will result in duplicate calls to
proto_register(), bt_sock_register(), etc.

With CONFIG_LIST_HARDENED and CONFIG_BUG_ON_DATA_CORRUPTION enabled, the
duplicate call to proto_register() triggers this BUG():

  list_add double add: new=ffffffffc0b280d0, prev=ffffffffbab56250,
    next=ffffffffc0b280d0.
  ------------[ cut here ]------------
  kernel BUG at lib/list_debug.c:35!
  Oops: invalid opcode: 0000 [#1] PREEMPT SMP PTI
  CPU: 2 PID: 887 Comm: bluetoothd Not tainted 6.10.11-1-ao-desktop #1
  RIP: 0010:__list_add_valid_or_report+0x9a/0xa0
  ...
    __list_add_valid_or_report+0x9a/0xa0
    proto_register+0x2b5/0x340
    iso_init+0x23/0x150 [bluetooth]
    set_iso_socket_func+0x68/0x1b0 [bluetooth]
    kmem_cache_free+0x308/0x330
    hci_sock_sendmsg+0x990/0x9e0 [bluetooth]
    __sock_sendmsg+0x7b/0x80
    sock_write_iter+0x9a/0x110
    do_iter_readv_writev+0x11d/0x220
    vfs_writev+0x180/0x3e0
    do_writev+0xca/0x100
  ...

This change removes the early return. The check for iso_debugfs being
NULL was unnecessary, it is always NULL when iso_inited is false.

Cc: stable@vger.kernel.org
Fixes: ccf74f2390d6 ("Bluetooth: Add BTPROTO_ISO socket type")
Signed-off-by: Aaron Thompson <dev@aaront.org>
---
 net/bluetooth/iso.c | 6 +-----
 1 file changed, 1 insertion(+), 5 deletions(-)

diff --git a/net/bluetooth/iso.c b/net/bluetooth/iso.c
index d5e00d0dd1a0..c9eefb43bf47 100644
--- a/net/bluetooth/iso.c
+++ b/net/bluetooth/iso.c
@@ -2301,13 +2301,9 @@ int iso_init(void)
 
 	hci_register_cb(&iso_cb);
 
-	if (IS_ERR_OR_NULL(bt_debugfs))
-		return 0;
-
-	if (!iso_debugfs) {
+	if (!IS_ERR_OR_NULL(bt_debugfs))
 		iso_debugfs = debugfs_create_file("iso", 0444, bt_debugfs,
 						  NULL, &iso_debugfs_fops);
-	}
 
 	iso_inited = true;
 
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 2/3] Bluetooth: Call iso_exit() on module unload
  2024-10-04 23:04 [PATCH v2 0/3] Bluetooth: Fix a few module init/deinit bugs Aaron Thompson
  2024-10-04 23:04 ` [PATCH v2 1/3] Bluetooth: ISO: Fix multiple init when debugfs is disabled Aaron Thompson
@ 2024-10-04 23:04 ` Aaron Thompson
  2024-10-04 23:04 ` [PATCH v2 3/3] Bluetooth: Remove debugfs directory on module init failure Aaron Thompson
  2024-10-07 15:10 ` [PATCH v2 0/3] Bluetooth: Fix a few module init/deinit bugs patchwork-bot+bluetooth
  3 siblings, 0 replies; 5+ messages in thread
From: Aaron Thompson @ 2024-10-04 23:04 UTC (permalink / raw)
  To: Johan Hedberg, Luiz Augusto von Dentz, Marcel Holtmann,
	linux-bluetooth, linux-kernel
  Cc: stable, Aaron Thompson

From: Aaron Thompson <dev@aaront.org>

If iso_init() has been called, iso_exit() must be called on module
unload. Without that, the struct proto that iso_init() registered with
proto_register() becomes invalid, which could cause unpredictable
problems later. In my case, with CONFIG_LIST_HARDENED and
CONFIG_BUG_ON_DATA_CORRUPTION enabled, loading the module again usually
triggers this BUG():

  list_add corruption. next->prev should be prev (ffffffffb5355fd0),
    but was 0000000000000068. (next=ffffffffc0a010d0).
  ------------[ cut here ]------------
  kernel BUG at lib/list_debug.c:29!
  Oops: invalid opcode: 0000 [#1] PREEMPT SMP PTI
  CPU: 1 PID: 4159 Comm: modprobe Not tainted 6.10.11-4+bt2-ao-desktop #1
  RIP: 0010:__list_add_valid_or_report+0x61/0xa0
  ...
    __list_add_valid_or_report+0x61/0xa0
    proto_register+0x299/0x320
    hci_sock_init+0x16/0xc0 [bluetooth]
    bt_init+0x68/0xd0 [bluetooth]
    __pfx_bt_init+0x10/0x10 [bluetooth]
    do_one_initcall+0x80/0x2f0
    do_init_module+0x8b/0x230
    __do_sys_init_module+0x15f/0x190
    do_syscall_64+0x68/0x110
  ...

Cc: stable@vger.kernel.org
Fixes: ccf74f2390d6 ("Bluetooth: Add BTPROTO_ISO socket type")
Signed-off-by: Aaron Thompson <dev@aaront.org>
---
 net/bluetooth/af_bluetooth.c | 2 ++
 1 file changed, 2 insertions(+)

diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
index 67604ccec2f4..9425d0680844 100644
--- a/net/bluetooth/af_bluetooth.c
+++ b/net/bluetooth/af_bluetooth.c
@@ -830,6 +830,8 @@ static int __init bt_init(void)
 
 static void __exit bt_exit(void)
 {
+	iso_exit();
+
 	mgmt_exit();
 
 	sco_exit();
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH v2 3/3] Bluetooth: Remove debugfs directory on module init failure
  2024-10-04 23:04 [PATCH v2 0/3] Bluetooth: Fix a few module init/deinit bugs Aaron Thompson
  2024-10-04 23:04 ` [PATCH v2 1/3] Bluetooth: ISO: Fix multiple init when debugfs is disabled Aaron Thompson
  2024-10-04 23:04 ` [PATCH v2 2/3] Bluetooth: Call iso_exit() on module unload Aaron Thompson
@ 2024-10-04 23:04 ` Aaron Thompson
  2024-10-07 15:10 ` [PATCH v2 0/3] Bluetooth: Fix a few module init/deinit bugs patchwork-bot+bluetooth
  3 siblings, 0 replies; 5+ messages in thread
From: Aaron Thompson @ 2024-10-04 23:04 UTC (permalink / raw)
  To: Johan Hedberg, Luiz Augusto von Dentz, Marcel Holtmann,
	linux-bluetooth, linux-kernel
  Cc: stable, Aaron Thompson

From: Aaron Thompson <dev@aaront.org>

If bt_init() fails, the debugfs directory currently is not removed. If
the module is loaded again after that, the debugfs directory is not set
up properly due to the existing directory.

  # modprobe bluetooth
  # ls -laF /sys/kernel/debug/bluetooth
  total 0
  drwxr-xr-x  2 root root 0 Sep 27 14:26 ./
  drwx------ 31 root root 0 Sep 27 14:25 ../
  -r--r--r--  1 root root 0 Sep 27 14:26 l2cap
  -r--r--r--  1 root root 0 Sep 27 14:26 sco
  # modprobe -r bluetooth
  # ls -laF /sys/kernel/debug/bluetooth
  ls: cannot access '/sys/kernel/debug/bluetooth': No such file or directory
  #

  # modprobe bluetooth
  modprobe: ERROR: could not insert 'bluetooth': Invalid argument
  # dmesg | tail -n 6
  Bluetooth: Core ver 2.22
  NET: Registered PF_BLUETOOTH protocol family
  Bluetooth: HCI device and connection manager initialized
  Bluetooth: HCI socket layer initialized
  Bluetooth: Faking l2cap_init() failure for testing
  NET: Unregistered PF_BLUETOOTH protocol family
  # ls -laF /sys/kernel/debug/bluetooth
  total 0
  drwxr-xr-x  2 root root 0 Sep 27 14:31 ./
  drwx------ 31 root root 0 Sep 27 14:26 ../
  #

  # modprobe bluetooth
  # dmesg | tail -n 7
  Bluetooth: Core ver 2.22
  debugfs: Directory 'bluetooth' with parent '/' already present!
  NET: Registered PF_BLUETOOTH protocol family
  Bluetooth: HCI device and connection manager initialized
  Bluetooth: HCI socket layer initialized
  Bluetooth: L2CAP socket layer initialized
  Bluetooth: SCO socket layer initialized
  # ls -laF /sys/kernel/debug/bluetooth
  total 0
  drwxr-xr-x  2 root root 0 Sep 27 14:31 ./
  drwx------ 31 root root 0 Sep 27 14:26 ../
  #

Cc: stable@vger.kernel.org
Fixes: ffcecac6a738 ("Bluetooth: Create root debugfs directory during module init")
Signed-off-by: Aaron Thompson <dev@aaront.org>
---
 net/bluetooth/af_bluetooth.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/net/bluetooth/af_bluetooth.c b/net/bluetooth/af_bluetooth.c
index 9425d0680844..e39fba5565c5 100644
--- a/net/bluetooth/af_bluetooth.c
+++ b/net/bluetooth/af_bluetooth.c
@@ -825,6 +825,7 @@ static int __init bt_init(void)
 	bt_sysfs_cleanup();
 cleanup_led:
 	bt_leds_cleanup();
+	debugfs_remove_recursive(bt_debugfs);
 	return err;
 }
 
-- 
2.39.5


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH v2 0/3] Bluetooth: Fix a few module init/deinit bugs
  2024-10-04 23:04 [PATCH v2 0/3] Bluetooth: Fix a few module init/deinit bugs Aaron Thompson
                   ` (2 preceding siblings ...)
  2024-10-04 23:04 ` [PATCH v2 3/3] Bluetooth: Remove debugfs directory on module init failure Aaron Thompson
@ 2024-10-07 15:10 ` patchwork-bot+bluetooth
  3 siblings, 0 replies; 5+ messages in thread
From: patchwork-bot+bluetooth @ 2024-10-07 15:10 UTC (permalink / raw)
  To: Aaron Thompson
  Cc: johan.hedberg, luiz.dentz, marcel, linux-bluetooth, linux-kernel,
	stable

Hello:

This series was applied to bluetooth/bluetooth-next.git (master)
by Luiz Augusto von Dentz <luiz.von.dentz@intel.com>:

On Fri,  4 Oct 2024 23:04:07 +0000 you wrote:
> Hi all,
> 
> These patches fix a few bugs in init and deinit for the bluetooth
> module. I ran into the first one when I started running a kernel with
> debugfs disabled on my laptop, and I ran into the next two when I was
> working on the fix for the first one.
> 
> [...]

Here is the summary with links:
  - [v2,1/3] Bluetooth: ISO: Fix multiple init when debugfs is disabled
    https://git.kernel.org/bluetooth/bluetooth-next/c/6259a9d4578d
  - [v2,2/3] Bluetooth: Call iso_exit() on module unload
    https://git.kernel.org/bluetooth/bluetooth-next/c/d18b99bb8795
  - [v2,3/3] Bluetooth: Remove debugfs directory on module init failure
    https://git.kernel.org/bluetooth/bluetooth-next/c/f5a04a514aa2

You are awesome, thank you!
-- 
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html



^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2024-10-07 15:10 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-04 23:04 [PATCH v2 0/3] Bluetooth: Fix a few module init/deinit bugs Aaron Thompson
2024-10-04 23:04 ` [PATCH v2 1/3] Bluetooth: ISO: Fix multiple init when debugfs is disabled Aaron Thompson
2024-10-04 23:04 ` [PATCH v2 2/3] Bluetooth: Call iso_exit() on module unload Aaron Thompson
2024-10-04 23:04 ` [PATCH v2 3/3] Bluetooth: Remove debugfs directory on module init failure Aaron Thompson
2024-10-07 15:10 ` [PATCH v2 0/3] Bluetooth: Fix a few module init/deinit bugs patchwork-bot+bluetooth

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox