Root Cause Analysis
The bug is a reference-counting / premature publication race in net/bluetooth/rfcomm/tty.c between device creation in rfcomm_dev_add() (via RFCOMMCREATEDEV ioctl) and concurrent TTY open/close in rfcomm_tty_install() / rfcomm_tty_cleanup().
Involved Functions, Structs, and Fields
Triggering Sequence and Lifecycle Violation
-
Creation and premature publication with only 1 reference:
rfcomm_dev_ioctl() (RFCOMMCREATEDEV) calls rfcomm_create_dev() -> __rfcomm_create_dev() -> rfcomm_dev_add().
Inside rfcomm_dev_add() (net/bluetooth/rfcomm/tty.c:322), __rfcomm_dev_add() allocates struct rfcomm_dev *dev (kzalloc_obj), initializes dev->port via tty_port_init(&dev->port) (initial kref = 1), copies RFCOMM_RELEASE_ONHUP into dev->flags, inserts dev into rfcomm_dev_list (net/bluetooth/rfcomm/tty.c:260), unlocks rfcomm_dev_lock, and returns dev.
Crucially, rfcomm_dev_add() does not hold its own extra reference on dev->port before dev becomes reachable in rfcomm_dev_list, and rfcomm_ioctl_mutex does not synchronize against TTY open/install/close paths (which only acquire rfcomm_dev_lock via rfcomm_dev_get()).
-
Concurrent TTY install takes ownership of the sole tty_port reference:
While rfcomm_dev_add() is executing tty_port_register_device(&dev->port, rfcomm_tty_driver, dev->id, NULL) (net/bluetooth/rfcomm/tty.c:328-329), or immediately after tty_port_register_device() returns before line 335, a concurrent task opens the RFCOMM TTY device (/dev/rfcomm<id>).
rfcomm_tty_install() (net/bluetooth/rfcomm/tty.c:703) calls rfcomm_dev_get(tty->index), which finds dev in rfcomm_dev_list and increments dev->port refcount from 1 to 2.
After tty_port_install(), lines 727-729 check:
if (test_bit(RFCOMM_RELEASE_ONHUP, &dev->flags) &&
!test_and_set_bit(RFCOMM_TTY_OWNED, &dev->status))
tty_port_put(&dev->port);
This drops dev->port refcount from 2 back to 1, transferring the sole remaining reference to the tty_struct (to be released on rfcomm_tty_cleanup()).
-
TTY release frees dev while rfcomm_dev_add() is still initializing it:
When the concurrent task closes the TTY, release_one_tty() (drivers/tty/tty_io.c:1497) runs on a workqueue and invokes rfcomm_tty_cleanup(tty) -> tty_port_put(&dev->port) (net/bluetooth/rfcomm/tty.c:690).
This drops dev->port's refcount from 1 to 0, invoking rfcomm_dev_destruct(&dev->port), which removes dev from rfcomm_dev_list and frees dev via kfree(dev) at net/bluetooth/rfcomm/tty.c:99. Note that because dev->tty_dev is still NULL at this point, if (dev->tty_dev) tty_unregister_device(...) at lines 92-93 is skipped.
-
Use-after-free in rfcomm_dev_add():
rfcomm_dev_add() resumes at net/bluetooth/rfcomm/tty.c:335:
dev->tty_dev = tty;
This writes 8 bytes (tty) to dev->tty_dev at offset 992 inside the already-freed kmalloc-2k struct rfcomm_dev object, triggering the KASAN slab-use-after-free write report. rfcomm_dev_add() subsequently performs additional use-after-free accesses in rfcomm_reparent_device(dev) (line 336), dev_set_drvdata(dev->tty_dev, dev) (line 337), device_create_file() (lines 339-343), and return dev->id (line 345). (Additionally, on the IS_ERR(tty) error path at line 331, tty_port_put(&dev->port) would similarly operate on a potentially freed dev.)
Fix Direction
rfcomm_dev_add() (or __rfcomm_dev_add()) must hold an extra tty_port_get(&dev->port) reference across tty_port_register_device() and the remaining initialization (dev->tty_dev = tty, rfcomm_reparent_device(dev), dev_set_drvdata(), device_create_file(), reading dev->id)βor defer inserting dev into rfcomm_dev_list / initialize dev->tty_dev appropriately before releasing the creation reference with tty_port_put(&dev->port). (If dev can already be torn down while rfcomm_dev_add() holds that reference, rfcomm_dev_add() / rfcomm_dev_destruct() must also ensure tty_unregister_device() is properly paired with tty_port_register_device().)