In the Linux kernel, the following vulnerability has been resolved:
gpiolib: cdev: Fix use after free in lineinfochangednotify
The use-after-free issue occurs as follows: when the GPIO chip device file is being closed by invoking gpiochrdevrelease(), watchedlines is freed by bitmapfree(), but the unregistration of lineinfochangednb notifier chain failed due to waiting write rwsem. Additionally, one of the GPIO chip's lines is also in the release process and holds the notifier chain's read rwsem. Consequently, a race condition leads to the use-after-free of watched_lines.
Here is the typical stack when issue happened:
[free] gpiochrdevrelease() --> bitmapfree(cdev->watchedlines) <-- freed --> blockingnotifierchainunregister() --> downwrite(&nh->rwsem) <-- waiting rwsem --> _downwritecommon() --> rwsemdownwriteslowpath() --> schedulepreemptdisabled() --> schedule()
[use] st54spigpiodevrelease() --> gpiofree() --> gpiodfree() --> gpiodfreecommit() --> gpiodlinestatenotify() --> blockingnotifiercallchain() --> downread(&nh->rwsem); <-- held rwsem --> notifiercallchain() --> lineinfochangednotify() --> testbit(xxxx, cdev->watchedlines) <-- use after free
The side effect of the use-after-free issue is that a GPIO line event is being generated for userspace where it shouldn't. However, since the chrdev is being closed, userspace won't have the chance to read that event anyway.
To fix the issue, call the bitmapfree() function after the unregistration of lineinfochanged_nb notifier chain.