** Description changed:

  [ Impact ]
  
  WirePlumber sometimes crashes when the Bluetooth adapter goes away, for
  example when bluetoothd restarts. All audio devices disappear until systemd
  restarts it. On my laptop it happens in about 3% of bluetoothd restarts
  (18 of 600). No Bluetooth device needs to be connected.
  
  The journal shows one of:
  
    wireplumber: corrupted size vs. prev_size
    wireplumber: malloc(): unaligned tcache chunk detected
    kernel: wireplumber[...]: segfault at 9 ip ... in libglib-2.0.so.0
  
  The cause is in the BLE MIDI code of libspa-0.2-bluetooth. Three GDBusProxy
  subclasses override finalize and do not call the parent finalize, so GLib
  keeps a weak pointer into the freed proxy. When BlueZ removes the adapter,
  GLib writes NULL through that pointer into freed memory.
  
  Fix proposed upstream:
  https://gitlab.freedesktop.org/pipewire/pipewire/-/merge_requests/3129
  
  I found this whilst verifying LP: #2160089. That fix is unrelated; the
  crash rate is the same with and without it.
  
  [ Test Plan ]
  
- On real hardware with a Bluetooth adapter, logged in to a desktop
- session:
+ The crash is rare (about 3% of restarts) and does not show in a virtual
+ machine, so the test looks for the invalid write itself with valgrind. It
+ needs no Bluetooth hardware.
  
- 1. Note the WirePlumber PID:
-      pgrep -x wireplumber
- 2. Restart bluetoothd 150 times:
-      for i in $(seq 150); do sudo systemctl restart bluetooth; sleep 5; done
- 3. Check the PID again and the journal:
-      pgrep -x wireplumber
-      journalctl --user -u wireplumber -b | grep -E 
"corrupted|malloc\(\)|dumped"
+ 1. In a VM of the series, install the packages and a virtual adapter:
+      sudo apt install pipewire wireplumber libspa-0.2-bluetooth bluez 
bluez-test-tools valgrind
+      sudo modprobe hci_vhci
+      sudo systemd-run --unit=btvirt btvirt -l1
+      sudo systemctl restart bluetooth
+      bluetoothctl list
+    The last command must list one controller.
+ 2. There is no graphical session, so let the Bluetooth monitor run without
+    a seat. Put this in
+    ~/.config/wireplumber/wireplumber.conf.d/98-test-noseat.conf:
+      wireplumber.profiles = {
+        main = {
+          monitor.bluez.seat-monitoring = disabled
+        }
+      }
+ 3. Run WirePlumber under valgrind. Put this in
+    ~/.config/systemd/user/wireplumber.service.d/vg.conf:
+      [Service]
+      ExecStart=
+      ExecStart=/usr/bin/valgrind --log-file=/tmp/vg-wp.log --num-callers=30 
/usr/bin/wireplumber
+      MemoryDenyWriteExecute=no
+      SystemCallFilter=
+      LockPersonality=no
+      TimeoutStopSec=60
+    then:
+      systemctl --user daemon-reload
+      systemctl --user restart wireplumber
+      sleep 60
+ 4. Restart bluetoothd three times and stop WirePlumber:
+      for i in 1 2 3; do sudo systemctl restart bluetooth; sleep 40; done
+      systemctl --user stop wireplumber
+      grep -A3 "Invalid write" /tmp/vg-wp.log
  
- Without the fix: the PID changes and the journal shows crashes (4 to 7 per
- 150 restarts here).
- With the fix: same PID, nothing in the journal.
+ Without the fix:
+   Invalid write of size 8
+      at g_nullify_pointer (in libglib-2.0.so.0)
+ With the fix: grep prints nothing.
  
- Also check that Bluetooth audio still works: connect a headset, play audio,
- switch between A2DP and HFP.
+ Also check, on a machine with a real adapter, that Bluetooth audio still
+ works: connect a headset, play audio, switch between A2DP and HFP.
  
  [ Where problems could occur ]
  
  The change only touches the BLE MIDI proxies in the Bluetooth plugin
  (midi-enum.c, midi-server.c). The parent finalize now runs where it did not
  before, so any mistake would show up as a crash or a GLib warning from
  WirePlumber when a BLE MIDI device or the adapter is removed, or at
  WirePlumber shutdown. Bluetooth audio does not use this code.
  
  I have no BLE MIDI device, so BLE MIDI itself is untested.
  
  [ Other Info ]
  
  Confirmed with AddressSanitizer builds of libspa-0.2-bluetooth and GLib on
  resolute (pipewire 1.6.2-1ubuntu1.3): heap-use-after-free write in
  g_nullify_pointer on the first bluetoothd restart. With the patch, 150
  restarts give no report, and 150 restarts on a normal build give no crash.
  
  The same code is in 1.6.8 (stonking, tenacious) and 1.6.11
- (tenacious-proposed). I have only reproduced it on resolute.
+ (tenacious-proposed). On stonking (1.6.8-1ubuntu4) valgrind shows the same
+ invalid write in a VM. I have only seen the crash itself on resolute.
  
  Workaround: disable the monitor in
  ~/.config/wireplumber/wireplumber.conf.d/no-bluez-midi.conf
  
    wireplumber.profiles = {
      main = {
        monitor.bluez-midi = disabled
      }
    }
  
  Possibly the same crash upstream:
  https://gitlab.freedesktop.org/pipewire/wireplumber/-/work_items/1003
  https://gitlab.freedesktop.org/pipewire/wireplumber/-/work_items/951

-- 
You received this bug notification because you are a member of Ubuntu
Bugs, which is subscribed to Ubuntu.
https://bugs.launchpad.net/bugs/2174961

Title:
  [SRU] WirePlumber crashes with heap corruption when bluetoothd
  restarts

To manage notifications about this bug go to:
https://bugs.launchpad.net/ubuntu/+source/pipewire/+bug/2174961/+subscriptions


-- 
ubuntu-bugs mailing list
[email protected]
https://lists.ubuntu.com/mailman/listinfo/ubuntu-bugs

Reply via email to