pw_bluetooth_proxy: Hold channels lock while using L2capChannel

Fixes use-after-free and data race issues that can happen when accessing
an L2capChannel* that may already have been removed and deleted.

Return a locked wrapper when looking up a channel that should be held
while accessing.

Fix the resulting deadlocks via two approaches:

- Introduce Locked variants so code that needs to access channel list
  further down the stack can do so without taking the lock.

- Defer queue drain via a flag so channels that need to send during
  their receive processing (for instance, to replenish credits) can
  signal that. Then for each receieved packet we check the flag, as well
  as on any external writes.

Bug: 390511432
Change-Id: Ia319c227a02e2d4b52113d3c2dbf091e4dde256e
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/270418
Lint: Lint 🤖 <android-build-ayeaye@system.gserviceaccount.com>
Commit-Queue: Auto-Submit <auto-submit@pigweed-service-accounts.iam.gserviceaccount.com>
Docs-Not-Needed: Austin Foxley <afoxley@google.com>
Presubmit-Verified: CQ Bot Account <pigweed-scoped@luci-project-accounts.iam.gserviceaccount.com>
Reviewed-by: David Rees <drees@google.com>
9 files changed
tree: 4c1d8cdf44b140e2b0ebabda47d91933ca7b47ea
  1. .allstar/
  2. .github/
  3. .vscode/
  4. build_overrides/
  5. docker/
  6. docs/
  7. kudzu/
  8. pw_alignment/
  9. pw_allocator/
  10. pw_analog/
  11. pw_android_toolchain/
  12. pw_arduino_build/
  13. pw_assert/
  14. pw_assert_basic/
  15. pw_assert_fuchsia/
  16. pw_assert_log/
  17. pw_assert_tokenized/
  18. pw_assert_trap/
  19. pw_assert_zephyr/
  20. pw_async/
  21. pw_async2/
  22. pw_async2_basic/
  23. pw_async2_epoll/
  24. pw_async_basic/
  25. pw_async_fuchsia/
  26. pw_atomic/
  27. pw_base64/
  28. pw_bloat/
  29. pw_blob_store/
  30. pw_bluetooth/
  31. pw_bluetooth_hci/
  32. pw_bluetooth_profiles/
  33. pw_bluetooth_proxy/
  34. pw_bluetooth_sapphire/
  35. pw_boot/
  36. pw_boot_cortex_m/
  37. pw_build/
  38. pw_build_android/
  39. pw_build_info/
  40. pw_build_mcuxpresso/
  41. pw_bytes/
  42. pw_channel/
  43. pw_checksum/
  44. pw_chre/
  45. pw_chrono/
  46. pw_chrono_embos/
  47. pw_chrono_freertos/
  48. pw_chrono_rp2040/
  49. pw_chrono_stl/
  50. pw_chrono_threadx/
  51. pw_chrono_zephyr/
  52. pw_cli/
  53. pw_cli_analytics/
  54. pw_clock_tree/
  55. pw_clock_tree_mcuxpresso/
  56. pw_compilation_testing/
  57. pw_config_loader/
  58. pw_console/
  59. pw_containers/
  60. pw_cpu_exception/
  61. pw_cpu_exception_cortex_m/
  62. pw_cpu_exception_risc_v/
  63. pw_crypto/
  64. pw_digital_io/
  65. pw_digital_io_linux/
  66. pw_digital_io_mcuxpresso/
  67. pw_digital_io_rp2040/
  68. pw_display/
  69. pw_dma_mcuxpresso/
  70. pw_docgen/
  71. pw_doctor/
  72. pw_elf/
  73. pw_emu/
  74. pw_env_setup/
  75. pw_env_setup_zephyr/
  76. pw_file/
  77. pw_format/
  78. pw_function/
  79. pw_fuzzer/
  80. pw_grpc/
  81. pw_hdlc/
  82. pw_hex_dump/
  83. pw_i2c/
  84. pw_i2c_linux/
  85. pw_i2c_mcuxpresso/
  86. pw_i2c_rp2040/
  87. pw_ide/
  88. pw_interrupt/
  89. pw_interrupt_cortex_m/
  90. pw_interrupt_freertos/
  91. pw_interrupt_xtensa/
  92. pw_interrupt_zephyr/
  93. pw_intrusive_ptr/
  94. pw_json/
  95. pw_kernel/
  96. pw_kvs/
  97. pw_libc/
  98. pw_libcxx/
  99. pw_log/
  100. pw_log_android/
  101. pw_log_basic/
  102. pw_log_fuchsia/
  103. pw_log_null/
  104. pw_log_rpc/
  105. pw_log_string/
  106. pw_log_tokenized/
  107. pw_log_zephyr/
  108. pw_malloc/
  109. pw_malloc_freelist/
  110. pw_malloc_freertos/
  111. pw_metric/
  112. pw_minimal_cpp_stdlib/
  113. pw_module/
  114. pw_multibuf/
  115. pw_multisink/
  116. pw_numeric/
  117. pw_package/
  118. pw_perf_test/
  119. pw_persistent_ram/
  120. pw_polyfill/
  121. pw_preprocessor/
  122. pw_presubmit/
  123. pw_protobuf/
  124. pw_protobuf_compiler/
  125. pw_random/
  126. pw_random_fuchsia/
  127. pw_result/
  128. pw_ring_buffer/
  129. pw_router/
  130. pw_rpc/
  131. pw_rpc_transport/
  132. pw_rust/
  133. pw_sensor/
  134. pw_snapshot/
  135. pw_software_update/
  136. pw_span/
  137. pw_spi/
  138. pw_spi_linux/
  139. pw_spi_mcuxpresso/
  140. pw_spi_rp2040/
  141. pw_status/
  142. pw_stm32cube_build/
  143. pw_stream/
  144. pw_stream_shmem_mcuxpresso/
  145. pw_stream_uart_linux/
  146. pw_stream_uart_mcuxpresso/
  147. pw_string/
  148. pw_symbolizer/
  149. pw_sync/
  150. pw_sync_baremetal/
  151. pw_sync_embos/
  152. pw_sync_freertos/
  153. pw_sync_stl/
  154. pw_sync_threadx/
  155. pw_sync_zephyr/
  156. pw_sys_io/
  157. pw_sys_io_ambiq_sdk/
  158. pw_sys_io_arduino/
  159. pw_sys_io_baremetal_lm3s6965evb/
  160. pw_sys_io_baremetal_stm32f429/
  161. pw_sys_io_emcraft_sf2/
  162. pw_sys_io_mcuxpresso/
  163. pw_sys_io_rp2040/
  164. pw_sys_io_stdio/
  165. pw_sys_io_stm32cube/
  166. pw_sys_io_zephyr/
  167. pw_system/
  168. pw_target_runner/
  169. pw_thread/
  170. pw_thread_embos/
  171. pw_thread_freertos/
  172. pw_thread_stl/
  173. pw_thread_threadx/
  174. pw_thread_zephyr/
  175. pw_tls_client/
  176. pw_tls_client_boringssl/
  177. pw_tls_client_mbedtls/
  178. pw_tokenizer/
  179. pw_toolchain/
  180. pw_trace/
  181. pw_trace_tokenized/
  182. pw_transfer/
  183. pw_uart/
  184. pw_uart_mcuxpresso/
  185. pw_unit_test/
  186. pw_unit_test_zephyr/
  187. pw_varint/
  188. pw_watch/
  189. pw_web/
  190. pw_work_queue/
  191. seed/
  192. targets/
  193. third_party/
  194. ts/
  195. zephyr/
  196. .bazelignore
  197. .bazelrc
  198. .bazelversion
  199. .black.toml
  200. .clang-format
  201. .clang-tidy
  202. .eslintrc.cjs
  203. .git-blame-ignore-revs
  204. .gitattributes
  205. .gitignore
  206. .gn
  207. .mypy.ini
  208. .prettierignore
  209. .prettierrc.cjs
  210. .pw_ide.yaml
  211. .pylintrc
  212. .ruff.toml
  213. activate.bat
  214. Android.bp
  215. AUTHORS
  216. bootstrap.bat
  217. bootstrap.fish
  218. bootstrap.sh
  219. BUILD.bazel
  220. BUILD.gn
  221. BUILDCONFIG.gn
  222. CMakeLists.txt
  223. go.mod
  224. go.sum
  225. jest.config.ts
  226. Kconfig.zephyr
  227. LICENSE
  228. MODULE.bazel
  229. MODULE.bazel.lock
  230. modules.gni
  231. OWNERS
  232. package-lock.json
  233. package.json
  234. pigweed.json
  235. PIGWEED_MODULES
  236. pnpm-lock.yaml
  237. README.md
  238. REPO.bazel
  239. rollup-protos.config.js
  240. rollup.config.js
  241. tsconfig.json
  242. WORKSPACE
  243. WORKSPACE_OWNERS
README.md

Pigweed

Pigweed is an open source collection of embedded-targeted libraries–or as we like to call them, modules. These modules are building blocks and infrastructure that enable faster and more reliable development on small-footprint MMU-less 32-bit microcontrollers like the STMicroelectronics STM32L452 or the Nordic nRF52832.

For more information please see our website: https://pigweed.dev/.

Links