linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/3] Bluetooth: Fix a few module init/deinit bugs
@ 2024-10-04  0:30 Aaron Thompson
  2024-10-04  0:30 ` [PATCH 1/3] Bluetooth: ISO: Fix multiple init when debugfs is disabled Aaron Thompson
                   ` (2 more replies)
  0 siblings, 3 replies; 6+ messages in thread
From: Aaron Thompson @ 2024-10-04  0:30 UTC (permalink / raw)
  To: Johan Hedberg, Luiz Augusto von Dentz, Marcel Holtmann,
	linux-bluetooth, linux-kernel
  Cc: 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.

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 | 1 +
 net/bluetooth/iso.c          | 6 +-----
 net/bluetooth/mgmt.c         | 1 +
 3 files changed, 3 insertions(+), 5 deletions(-)


base-commit: 9852d85ec9d492ebef56dc5f229416c925758edc
-- 
2.39.5


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

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

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] 6+ messages in thread

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

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/mgmt.c | 1 +
 1 file changed, 1 insertion(+)

diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
index e4f564d6f6fb..78a164fab3e1 100644
--- a/net/bluetooth/mgmt.c
+++ b/net/bluetooth/mgmt.c
@@ -10487,6 +10487,7 @@ int mgmt_init(void)
 
 void mgmt_exit(void)
 {
+	iso_exit();
 	hci_mgmt_chan_unregister(&chan);
 }
 
-- 
2.39.5


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

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

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 67604ccec2f4..0d4b171b939a 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] 6+ messages in thread

* Re: [PATCH 2/3] Bluetooth: Call iso_exit() on module unload
  2024-10-04  0:30 ` [PATCH 2/3] Bluetooth: Call iso_exit() on module unload Aaron Thompson
@ 2024-10-04 19:30   ` Luiz Augusto von Dentz
  2024-10-04 23:01     ` Aaron Thompson
  0 siblings, 1 reply; 6+ messages in thread
From: Luiz Augusto von Dentz @ 2024-10-04 19:30 UTC (permalink / raw)
  To: Aaron Thompson
  Cc: Johan Hedberg, Marcel Holtmann, linux-bluetooth, linux-kernel,
	stable

Hi Aaron,

On Thu, Oct 3, 2024 at 8:31 PM Aaron Thompson <dev@aaront.org> wrote:
>
> 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/mgmt.c | 1 +
>  1 file changed, 1 insertion(+)
>
> diff --git a/net/bluetooth/mgmt.c b/net/bluetooth/mgmt.c
> index e4f564d6f6fb..78a164fab3e1 100644
> --- a/net/bluetooth/mgmt.c
> +++ b/net/bluetooth/mgmt.c
> @@ -10487,6 +10487,7 @@ int mgmt_init(void)
>
>  void mgmt_exit(void)
>  {
> +       iso_exit();
>         hci_mgmt_chan_unregister(&chan);
>  }
>
> --
> 2.39.5

I had it under bt_exit with the rest of the exit functions so that we
don't have to move it once ISO sockets become stable.


-- 
Luiz Augusto von Dentz

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

* Re: [PATCH 2/3] Bluetooth: Call iso_exit() on module unload
  2024-10-04 19:30   ` Luiz Augusto von Dentz
@ 2024-10-04 23:01     ` Aaron Thompson
  0 siblings, 0 replies; 6+ messages in thread
From: Aaron Thompson @ 2024-10-04 23:01 UTC (permalink / raw)
  To: Luiz Augusto von Dentz
  Cc: Johan Hedberg, Marcel Holtmann, linux-bluetooth, linux-kernel,
	stable

Hi Luiz,

On 10/4/24 12:30, Luiz Augusto von Dentz wrote:
> 
> I had it under bt_exit with the rest of the exit functions so that we
> don't have to move it once ISO sockets become stable.
> 

Sure thing. I was just guessing based on where the call to iso_init() 
is. Thanks for the review!

-- Aaron


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

end of thread, other threads:[~2024-10-04 23:01 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2024-10-04  0:30 [PATCH 0/3] Bluetooth: Fix a few module init/deinit bugs Aaron Thompson
2024-10-04  0:30 ` [PATCH 1/3] Bluetooth: ISO: Fix multiple init when debugfs is disabled Aaron Thompson
2024-10-04  0:30 ` [PATCH 2/3] Bluetooth: Call iso_exit() on module unload Aaron Thompson
2024-10-04 19:30   ` Luiz Augusto von Dentz
2024-10-04 23:01     ` Aaron Thompson
2024-10-04  0:30 ` [PATCH 3/3] Bluetooth: Remove debugfs directory on module init failure Aaron Thompson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).