kernel_samsung_a53x/drivers/usb/misc/chaoskey.c
Edward Adam Davis 366f1818f3 USB: chaoskey: Fix possible deadlock chaoskey_list_lock
[ Upstream commit d73dc7b182be4238b75278bfae16afb4c5564a58 ]

[Syzbot reported two possible deadlocks]
The first possible deadlock is:
WARNING: possible recursive locking detected
6.12.0-rc1-syzkaller-00027-g4a9fe2a8ac53 #0 Not tainted
--------------------------------------------
syz-executor363/2651 is trying to acquire lock:
ffffffff89b120e8 (chaoskey_list_lock){+.+.}-{3:3}, at: chaoskey_release+0x15d/0x2c0 drivers/usb/misc/chaoskey.c:322

but task is already holding lock:
ffffffff89b120e8 (chaoskey_list_lock){+.+.}-{3:3}, at: chaoskey_release+0x7f/0x2c0 drivers/usb/misc/chaoskey.c:299

other info that might help us debug this:
 Possible unsafe locking scenario:

       CPU0
       ----
  lock(chaoskey_list_lock);
  lock(chaoskey_list_lock);

 *** DEADLOCK ***

The second possible deadlock is:
WARNING: possible circular locking dependency detected
6.12.0-rc1-syzkaller-00027-g4a9fe2a8ac53 #0 Not tainted
------------------------------------------------------
kworker/0:2/804 is trying to acquire lock:
ffffffff899dadb0 (minor_rwsem){++++}-{3:3}, at: usb_deregister_dev+0x7c/0x1e0 drivers/usb/core/file.c:186

but task is already holding lock:
ffffffff89b120e8 (chaoskey_list_lock){+.+.}-{3:3}, at: chaoskey_disconnect+0xa8/0x2a0 drivers/usb/misc/chaoskey.c:235

which lock already depends on the new lock.

the existing dependency chain (in reverse order) is:

-> #1 (chaoskey_list_lock){+.+.}-{3:3}:
       __mutex_lock_common kernel/locking/mutex.c:608 [inline]
       __mutex_lock+0x175/0x9c0 kernel/locking/mutex.c:752
       chaoskey_open+0xdd/0x220 drivers/usb/misc/chaoskey.c:274
       usb_open+0x186/0x220 drivers/usb/core/file.c:47
       chrdev_open+0x237/0x6a0 fs/char_dev.c:414
       do_dentry_open+0x6cb/0x1390 fs/open.c:958
       vfs_open+0x82/0x3f0 fs/open.c:1088
       do_open fs/namei.c:3774 [inline]
       path_openat+0x1e6a/0x2d60 fs/namei.c:3933
       do_filp_open+0x1dc/0x430 fs/namei.c:3960
       do_sys_openat2+0x17a/0x1e0 fs/open.c:1415
       do_sys_open fs/open.c:1430 [inline]
       __do_sys_openat fs/open.c:1446 [inline]
       __se_sys_openat fs/open.c:1441 [inline]
       __x64_sys_openat+0x175/0x210 fs/open.c:1441
       do_syscall_x64 arch/x86/entry/common.c:52 [inline]
       do_syscall_64+0xcd/0x250 arch/x86/entry/common.c:83
       entry_SYSCALL_64_after_hwframe+0x77/0x7f

-> #0 (minor_rwsem){++++}-{3:3}:
       check_prev_add kernel/locking/lockdep.c:3161 [inline]
       check_prevs_add kernel/locking/lockdep.c:3280 [inline]
       validate_chain kernel/locking/lockdep.c:3904 [inline]
       __lock_acquire+0x250b/0x3ce0 kernel/locking/lockdep.c:5202
       lock_acquire.part.0+0x11b/0x380 kernel/locking/lockdep.c:5825
       down_write+0x93/0x200 kernel/locking/rwsem.c:1577
       usb_deregister_dev+0x7c/0x1e0 drivers/usb/core/file.c:186
       chaoskey_disconnect+0xb7/0x2a0 drivers/usb/misc/chaoskey.c:236
       usb_unbind_interface+0x1e8/0x970 drivers/usb/core/driver.c:461
       device_remove drivers/base/dd.c:569 [inline]
       device_remove+0x122/0x170 drivers/base/dd.c:561
       __device_release_driver drivers/base/dd.c:1273 [inline]
       device_release_driver_internal+0x44a/0x610 drivers/base/dd.c:1296
       bus_remove_device+0x22f/0x420 drivers/base/bus.c:576
       device_del+0x396/0x9f0 drivers/base/core.c:3864
       usb_disable_device+0x36c/0x7f0 drivers/usb/core/message.c:1418
       usb_disconnect+0x2e1/0x920 drivers/usb/core/hub.c:2304
       hub_port_connect drivers/usb/core/hub.c:5361 [inline]
       hub_port_connect_change drivers/usb/core/hub.c:5661 [inline]
       port_event drivers/usb/core/hub.c:5821 [inline]
       hub_event+0x1bed/0x4f40 drivers/usb/core/hub.c:5903
       process_one_work+0x9c5/0x1ba0 kernel/workqueue.c:3229
       process_scheduled_works kernel/workqueue.c:3310 [inline]
       worker_thread+0x6c8/0xf00 kernel/workqueue.c:3391
       kthread+0x2c1/0x3a0 kernel/kthread.c:389
       ret_from_fork+0x45/0x80 arch/x86/kernel/process.c:147
       ret_from_fork_asm+0x1a/0x30 arch/x86/entry/entry_64.S:244

other info that might help us debug this:

 Possible unsafe locking scenario:

       CPU0                    CPU1
       ----                    ----
  lock(chaoskey_list_lock);
                               lock(minor_rwsem);
                               lock(chaoskey_list_lock);
  lock(minor_rwsem);

 *** DEADLOCK ***
[Analysis]
The first is AA lock, it because wrong logic, it need a unlock.
The second is AB lock, it needs to rearrange the order of lock usage.

Fixes: 422dc0a4d12d ("USB: chaoskey: fail open after removal")
Reported-by: syzbot+685e14d04fe35692d3bc@syzkaller.appspotmail.com
Reported-by: syzbot+1f8ca5ee82576ec01f12@syzkaller.appspotmail.com
Closes: https://syzkaller.appspot.com/bug?extid=685e14d04fe35692d3bc
Signed-off-by: Edward Adam Davis <eadavis@qq.com>
Tested-by: syzbot+685e14d04fe35692d3bc@syzkaller.appspotmail.com
Reported-by: syzbot+5f1ce62e956b7b19610e@syzkaller.appspotmail.com
Tested-by: syzbot+5f1ce62e956b7b19610e@syzkaller.appspotmail.com
Tested-by: syzbot+1f8ca5ee82576ec01f12@syzkaller.appspotmail.com
Link: https://lore.kernel.org/r/tencent_84EB865C89862EC22EE94CB3A7C706C59206@qq.com
Cc: Oliver Neukum <oneukum@suse.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
Signed-off-by: Sasha Levin <sashal@kernel.org>
2024-12-17 13:24:16 +01:00

597 lines
14 KiB
C
Executable file

// SPDX-License-Identifier: GPL-2.0
/*
* chaoskey - driver for ChaosKey device from Altus Metrum.
*
* This device provides true random numbers using a noise source based
* on a reverse-biased p-n junction in avalanche breakdown. More
* details can be found at http://chaoskey.org
*
* The driver connects to the kernel hardware RNG interface to provide
* entropy for /dev/random and other kernel activities. It also offers
* a separate /dev/ entry to allow for direct access to the random
* bit stream.
*
* Copyright © 2015 Keith Packard <keithp@keithp.com>
*/
#include <linux/module.h>
#include <linux/slab.h>
#include <linux/usb.h>
#include <linux/wait.h>
#include <linux/hw_random.h>
#include <linux/mutex.h>
#include <linux/uaccess.h>
static struct usb_driver chaoskey_driver;
static struct usb_class_driver chaoskey_class;
static int chaoskey_rng_read(struct hwrng *rng, void *data,
size_t max, bool wait);
static DEFINE_MUTEX(chaoskey_list_lock);
#define usb_dbg(usb_if, format, arg...) \
dev_dbg(&(usb_if)->dev, format, ## arg)
#define usb_err(usb_if, format, arg...) \
dev_err(&(usb_if)->dev, format, ## arg)
/* Version Information */
#define DRIVER_AUTHOR "Keith Packard, keithp@keithp.com"
#define DRIVER_DESC "Altus Metrum ChaosKey driver"
#define DRIVER_SHORT "chaoskey"
MODULE_AUTHOR(DRIVER_AUTHOR);
MODULE_DESCRIPTION(DRIVER_DESC);
MODULE_LICENSE("GPL");
#define CHAOSKEY_VENDOR_ID 0x1d50 /* OpenMoko */
#define CHAOSKEY_PRODUCT_ID 0x60c6 /* ChaosKey */
#define ALEA_VENDOR_ID 0x12d8 /* Araneus */
#define ALEA_PRODUCT_ID 0x0001 /* Alea I */
#define CHAOSKEY_BUF_LEN 64 /* max size of USB full speed packet */
#define NAK_TIMEOUT (HZ) /* normal stall/wait timeout */
#define ALEA_FIRST_TIMEOUT (HZ*3) /* first stall/wait timeout for Alea */
#ifdef CONFIG_USB_DYNAMIC_MINORS
#define USB_CHAOSKEY_MINOR_BASE 0
#else
/* IOWARRIOR_MINOR_BASE + 16, not official yet */
#define USB_CHAOSKEY_MINOR_BASE 224
#endif
static const struct usb_device_id chaoskey_table[] = {
{ USB_DEVICE(CHAOSKEY_VENDOR_ID, CHAOSKEY_PRODUCT_ID) },
{ USB_DEVICE(ALEA_VENDOR_ID, ALEA_PRODUCT_ID) },
{ },
};
MODULE_DEVICE_TABLE(usb, chaoskey_table);
static void chaos_read_callback(struct urb *urb);
/* Driver-local specific stuff */
struct chaoskey {
struct usb_interface *interface;
char in_ep;
struct mutex lock;
struct mutex rng_lock;
int open; /* open count */
bool present; /* device not disconnected */
bool reading; /* ongoing IO */
bool reads_started; /* track first read for Alea */
int size; /* size of buf */
int valid; /* bytes of buf read */
int used; /* bytes of buf consumed */
char *name; /* product + serial */
struct hwrng hwrng; /* Embedded struct for hwrng */
int hwrng_registered; /* registered with hwrng API */
wait_queue_head_t wait_q; /* for timeouts */
struct urb *urb; /* for performing IO */
char *buf;
};
static void chaoskey_free(struct chaoskey *dev)
{
if (dev) {
usb_dbg(dev->interface, "free");
usb_free_urb(dev->urb);
kfree(dev->name);
kfree(dev->buf);
usb_put_intf(dev->interface);
kfree(dev);
}
}
static int chaoskey_probe(struct usb_interface *interface,
const struct usb_device_id *id)
{
struct usb_device *udev = interface_to_usbdev(interface);
struct usb_host_interface *altsetting = interface->cur_altsetting;
struct usb_endpoint_descriptor *epd;
int in_ep;
struct chaoskey *dev;
int result = -ENOMEM;
int size;
int res;
usb_dbg(interface, "probe %s-%s", udev->product, udev->serial);
/* Find the first bulk IN endpoint and its packet size */
res = usb_find_bulk_in_endpoint(altsetting, &epd);
if (res) {
usb_dbg(interface, "no IN endpoint found");
return res;
}
in_ep = usb_endpoint_num(epd);
size = usb_endpoint_maxp(epd);
/* Validate endpoint and size */
if (size <= 0) {
usb_dbg(interface, "invalid size (%d)", size);
return -ENODEV;
}
if (size > CHAOSKEY_BUF_LEN) {
usb_dbg(interface, "size reduced from %d to %d\n",
size, CHAOSKEY_BUF_LEN);
size = CHAOSKEY_BUF_LEN;
}
/* Looks good, allocate and initialize */
dev = kzalloc(sizeof(struct chaoskey), GFP_KERNEL);
if (dev == NULL)
goto out;
dev->interface = usb_get_intf(interface);
dev->buf = kmalloc(size, GFP_KERNEL);
if (dev->buf == NULL)
goto out;
dev->urb = usb_alloc_urb(0, GFP_KERNEL);
if (!dev->urb)
goto out;
usb_fill_bulk_urb(dev->urb,
udev,
usb_rcvbulkpipe(udev, in_ep),
dev->buf,
size,
chaos_read_callback,
dev);
/* Construct a name using the product and serial values. Each
* device needs a unique name for the hwrng code
*/
if (udev->product && udev->serial) {
dev->name = kasprintf(GFP_KERNEL, "%s-%s", udev->product,
udev->serial);
if (dev->name == NULL)
goto out;
}
dev->in_ep = in_ep;
if (le16_to_cpu(udev->descriptor.idVendor) != ALEA_VENDOR_ID)
dev->reads_started = true;
dev->size = size;
dev->present = true;
init_waitqueue_head(&dev->wait_q);
mutex_init(&dev->lock);
mutex_init(&dev->rng_lock);
usb_set_intfdata(interface, dev);
result = usb_register_dev(interface, &chaoskey_class);
if (result) {
usb_err(interface, "Unable to allocate minor number.");
goto out;
}
dev->hwrng.name = dev->name ? dev->name : chaoskey_driver.name;
dev->hwrng.read = chaoskey_rng_read;
dev->hwrng.quality = 1024;
dev->hwrng_registered = (hwrng_register(&dev->hwrng) == 0);
if (!dev->hwrng_registered)
usb_err(interface, "Unable to register with hwrng");
usb_enable_autosuspend(udev);
usb_dbg(interface, "chaoskey probe success, size %d", dev->size);
return 0;
out:
usb_set_intfdata(interface, NULL);
chaoskey_free(dev);
return result;
}
static void chaoskey_disconnect(struct usb_interface *interface)
{
struct chaoskey *dev;
usb_dbg(interface, "disconnect");
dev = usb_get_intfdata(interface);
if (!dev) {
usb_dbg(interface, "disconnect failed - no dev");
return;
}
if (dev->hwrng_registered)
hwrng_unregister(&dev->hwrng);
usb_deregister_dev(interface, &chaoskey_class);
usb_set_intfdata(interface, NULL);
mutex_lock(&chaoskey_list_lock);
mutex_lock(&dev->lock);
dev->present = false;
usb_poison_urb(dev->urb);
if (!dev->open) {
mutex_unlock(&dev->lock);
chaoskey_free(dev);
} else
mutex_unlock(&dev->lock);
mutex_unlock(&chaoskey_list_lock);
usb_dbg(interface, "disconnect done");
}
static int chaoskey_open(struct inode *inode, struct file *file)
{
struct chaoskey *dev;
struct usb_interface *interface;
int rv = 0;
/* get the interface from minor number and driver information */
interface = usb_find_interface(&chaoskey_driver, iminor(inode));
if (!interface)
return -ENODEV;
usb_dbg(interface, "open");
dev = usb_get_intfdata(interface);
if (!dev) {
usb_dbg(interface, "open (dev)");
return -ENODEV;
}
file->private_data = dev;
mutex_lock(&chaoskey_list_lock);
mutex_lock(&dev->lock);
if (dev->present)
++dev->open;
else
rv = -ENODEV;
mutex_unlock(&dev->lock);
mutex_unlock(&chaoskey_list_lock);
return rv;
}
static int chaoskey_release(struct inode *inode, struct file *file)
{
struct chaoskey *dev = file->private_data;
struct usb_interface *interface;
int rv = 0;
if (dev == NULL)
return -ENODEV;
interface = dev->interface;
usb_dbg(interface, "release");
mutex_lock(&chaoskey_list_lock);
mutex_lock(&dev->lock);
usb_dbg(interface, "open count at release is %d", dev->open);
if (dev->open <= 0) {
usb_dbg(interface, "invalid open count (%d)", dev->open);
rv = -ENODEV;
goto bail;
}
--dev->open;
if (!dev->present) {
if (dev->open == 0) {
mutex_unlock(&dev->lock);
chaoskey_free(dev);
goto destruction;
}
}
bail:
mutex_unlock(&dev->lock);
destruction:
mutex_unlock(&chaoskey_list_lock);
usb_dbg(interface, "release success");
return rv;
}
static void chaos_read_callback(struct urb *urb)
{
struct chaoskey *dev = urb->context;
int status = urb->status;
usb_dbg(dev->interface, "callback status (%d)", status);
if (status == 0)
dev->valid = urb->actual_length;
else
dev->valid = 0;
dev->used = 0;
/* must be seen first before validity is announced */
smp_wmb();
dev->reading = false;
wake_up(&dev->wait_q);
}
/* Fill the buffer. Called with dev->lock held
*/
static int _chaoskey_fill(struct chaoskey *dev)
{
DEFINE_WAIT(wait);
int result;
bool started;
usb_dbg(dev->interface, "fill");
/* Return immediately if someone called before the buffer was
* empty */
if (dev->valid != dev->used) {
usb_dbg(dev->interface, "not empty yet (valid %d used %d)",
dev->valid, dev->used);
return 0;
}
/* Bail if the device has been removed */
if (!dev->present) {
usb_dbg(dev->interface, "device not present");
return -ENODEV;
}
/* Make sure the device is awake */
result = usb_autopm_get_interface(dev->interface);
if (result) {
usb_dbg(dev->interface, "wakeup failed (result %d)", result);
return result;
}
dev->reading = true;
result = usb_submit_urb(dev->urb, GFP_KERNEL);
if (result < 0) {
result = usb_translate_errors(result);
dev->reading = false;
goto out;
}
/* The first read on the Alea takes a little under 2 seconds.
* Reads after the first read take only a few microseconds
* though. Presumably the entropy-generating circuit needs
* time to ramp up. So, we wait longer on the first read.
*/
started = dev->reads_started;
dev->reads_started = true;
result = wait_event_interruptible_timeout(
dev->wait_q,
!dev->reading,
(started ? NAK_TIMEOUT : ALEA_FIRST_TIMEOUT) );
if (result < 0) {
usb_kill_urb(dev->urb);
goto out;
}
if (result == 0) {
result = -ETIMEDOUT;
usb_kill_urb(dev->urb);
} else {
result = dev->valid;
}
out:
/* Let the device go back to sleep eventually */
usb_autopm_put_interface(dev->interface);
usb_dbg(dev->interface, "read %d bytes", dev->valid);
return result;
}
static ssize_t chaoskey_read(struct file *file,
char __user *buffer,
size_t count,
loff_t *ppos)
{
struct chaoskey *dev;
ssize_t read_count = 0;
int this_time;
int result = 0;
unsigned long remain;
dev = file->private_data;
if (dev == NULL || !dev->present)
return -ENODEV;
usb_dbg(dev->interface, "read %zu", count);
while (count > 0) {
/* Grab the rng_lock briefly to ensure that the hwrng interface
* gets priority over other user access
*/
result = mutex_lock_interruptible(&dev->rng_lock);
if (result)
goto bail;
mutex_unlock(&dev->rng_lock);
result = mutex_lock_interruptible(&dev->lock);
if (result)
goto bail;
if (dev->valid == dev->used) {
result = _chaoskey_fill(dev);
if (result < 0) {
mutex_unlock(&dev->lock);
goto bail;
}
}
this_time = dev->valid - dev->used;
if (this_time > count)
this_time = count;
remain = copy_to_user(buffer, dev->buf + dev->used, this_time);
if (remain) {
result = -EFAULT;
/* Consume the bytes that were copied so we don't leak
* data to user space
*/
dev->used += this_time - remain;
mutex_unlock(&dev->lock);
goto bail;
}
count -= this_time;
read_count += this_time;
buffer += this_time;
dev->used += this_time;
mutex_unlock(&dev->lock);
}
bail:
if (read_count) {
usb_dbg(dev->interface, "read %zu bytes", read_count);
return read_count;
}
usb_dbg(dev->interface, "empty read, result %d", result);
if (result == -ETIMEDOUT)
result = -EAGAIN;
return result;
}
static int chaoskey_rng_read(struct hwrng *rng, void *data,
size_t max, bool wait)
{
struct chaoskey *dev = container_of(rng, struct chaoskey, hwrng);
int this_time;
usb_dbg(dev->interface, "rng_read max %zu wait %d", max, wait);
if (!dev->present) {
usb_dbg(dev->interface, "device not present");
return 0;
}
/* Hold the rng_lock until we acquire the device lock so that
* this operation gets priority over other user access to the
* device
*/
mutex_lock(&dev->rng_lock);
mutex_lock(&dev->lock);
mutex_unlock(&dev->rng_lock);
/* Try to fill the buffer if empty. It doesn't actually matter
* if _chaoskey_fill works; we'll just return zero bytes as
* the buffer will still be empty
*/
if (dev->valid == dev->used)
(void) _chaoskey_fill(dev);
this_time = dev->valid - dev->used;
if (this_time > max)
this_time = max;
memcpy(data, dev->buf + dev->used, this_time);
dev->used += this_time;
mutex_unlock(&dev->lock);
usb_dbg(dev->interface, "rng_read this_time %d\n", this_time);
return this_time;
}
#ifdef CONFIG_PM
static int chaoskey_suspend(struct usb_interface *interface,
pm_message_t message)
{
usb_dbg(interface, "suspend");
return 0;
}
static int chaoskey_resume(struct usb_interface *interface)
{
struct chaoskey *dev;
struct usb_device *udev = interface_to_usbdev(interface);
usb_dbg(interface, "resume");
dev = usb_get_intfdata(interface);
/*
* We may have lost power.
* In that case the device that needs a long time
* for the first requests needs an extended timeout
* again
*/
if (le16_to_cpu(udev->descriptor.idVendor) == ALEA_VENDOR_ID)
dev->reads_started = false;
return 0;
}
#else
#define chaoskey_suspend NULL
#define chaoskey_resume NULL
#endif
/* file operation pointers */
static const struct file_operations chaoskey_fops = {
.owner = THIS_MODULE,
.read = chaoskey_read,
.open = chaoskey_open,
.release = chaoskey_release,
.llseek = default_llseek,
};
/* class driver information */
static struct usb_class_driver chaoskey_class = {
.name = "chaoskey%d",
.fops = &chaoskey_fops,
.minor_base = USB_CHAOSKEY_MINOR_BASE,
};
/* usb specific object needed to register this driver with the usb subsystem */
static struct usb_driver chaoskey_driver = {
.name = DRIVER_SHORT,
.probe = chaoskey_probe,
.disconnect = chaoskey_disconnect,
.suspend = chaoskey_suspend,
.resume = chaoskey_resume,
.reset_resume = chaoskey_resume,
.id_table = chaoskey_table,
.supports_autosuspend = 1,
};
module_usb_driver(chaoskey_driver);