Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

Bluetooth: hci_{ldisc,serdev}: check percpu_init_rwsem() failure

syzbot is reporting NULL pointer dereference at hci_uart_tty_close() [1],
for rcu_sync_enter() is called without rcu_sync_init() due to
hci_uart_tty_open() ignoring percpu_init_rwsem() failure.

While we are at it, fix that hci_uart_register_device() ignores
percpu_init_rwsem() failure and hci_uart_unregister_device() does not
call percpu_free_rwsem().

Link: https://syzkaller.appspot.com/bug?extid=576dfca25381fb6fbc5f [1]
Reported-by: syzbot <syzbot+576dfca25381fb6fbc5f@syzkaller.appspotmail.com>
Signed-off-by: Tetsuo Handa <penguin-kernel@I-love.SAKURA.ne.jp>
Fixes: 67d2f8781b9f00d1 ("Bluetooth: hci_ldisc: Allow sleeping while proto locks are held.")
Fixes: d73e172816652772 ("Bluetooth: hci_serdev: Init hci_uart proto_lock to avoid oops")
Signed-off-by: Luiz Augusto von Dentz <luiz.von.dentz@intel.com>

authored by

Tetsuo Handa and committed by
Luiz Augusto von Dentz
3124d320 deee93d1

+12 -5
+5 -2
drivers/bluetooth/hci_ldisc.c
··· 493 493 BT_ERR("Can't allocate control structure"); 494 494 return -ENFILE; 495 495 } 496 + if (percpu_init_rwsem(&hu->proto_lock)) { 497 + BT_ERR("Can't allocate semaphore structure"); 498 + kfree(hu); 499 + return -ENOMEM; 500 + } 496 501 497 502 tty->disc_data = hu; 498 503 hu->tty = tty; ··· 509 504 510 505 INIT_WORK(&hu->init_ready, hci_uart_init_work); 511 506 INIT_WORK(&hu->write_work, hci_uart_write_work); 512 - 513 - percpu_init_rwsem(&hu->proto_lock); 514 507 515 508 /* Flush any pending characters in the driver */ 516 509 tty_driver_flush_buffer(tty);
+7 -3
drivers/bluetooth/hci_serdev.c
··· 310 310 311 311 serdev_device_set_client_ops(hu->serdev, &hci_serdev_client_ops); 312 312 313 + if (percpu_init_rwsem(&hu->proto_lock)) 314 + return -ENOMEM; 315 + 313 316 err = serdev_device_open(hu->serdev); 314 317 if (err) 315 - return err; 316 - 317 - percpu_init_rwsem(&hu->proto_lock); 318 + goto err_rwsem; 318 319 319 320 err = p->open(hu); 320 321 if (err) ··· 390 389 p->close(hu); 391 390 err_open: 392 391 serdev_device_close(hu->serdev); 392 + err_rwsem: 393 + percpu_free_rwsem(&hu->proto_lock); 393 394 return err; 394 395 } 395 396 EXPORT_SYMBOL_GPL(hci_uart_register_device); ··· 413 410 clear_bit(HCI_UART_PROTO_READY, &hu->flags); 414 411 serdev_device_close(hu->serdev); 415 412 } 413 + percpu_free_rwsem(&hu->proto_lock); 416 414 } 417 415 EXPORT_SYMBOL_GPL(hci_uart_unregister_device);