char: xillybus: Fix trivial bug with mutex
authorEli Billauer <eli.billauer@gmail.com>
Thu, 17 Nov 2022 07:18:25 +0000 (09:18 +0200)
committerGreg Kroah-Hartman <gregkh@linuxfoundation.org>
Wed, 23 Nov 2022 18:44:04 +0000 (19:44 +0100)
@unit_mutex protects @unit from being freed, so obviously it should be
released after @unit is used, and not before.

This is a follow-up to commit 282a4b71816b ("char: xillybus: Prevent
use-after-free due to race condition") which ensures, among others, the
protection of @private_data after @unit_mutex has been released.

Reported-by: Hyunwoo Kim <imv4bel@gmail.com>
Signed-off-by: Eli Billauer <eli.billauer@gmail.com>
Link: https://lore.kernel.org/r/20221117071825.3942-1-eli.billauer@gmail.com
Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
drivers/char/xillybus/xillybus_class.c

index 0f23864..e9a288e 100644 (file)
@@ -227,14 +227,15 @@ int xillybus_find_inode(struct inode *inode,
                        break;
                }
 
-       mutex_unlock(&unit_mutex);
-
-       if (!unit)
+       if (!unit) {
+               mutex_unlock(&unit_mutex);
                return -ENODEV;
+       }
 
        *private_data = unit->private_data;
        *index = minor - unit->lowest_minor;
 
+       mutex_unlock(&unit_mutex);
        return 0;
 }
 EXPORT_SYMBOL(xillybus_find_inode);