CVE-2024-56631: scsi: sg: Fix slab-use-after-free read in sg_release()
In the Linux kernel, the following vulnerability has been resolved:
scsi: sg: Fix slab-use-after-free read in sgrelease()
Fix a use-after-free bug in sgrelease(), detected by syzbot with KASAN:
BUG: KASAN: slab-use-after-free in lockrelease+0x151/0xa30 kernel/locking/lockdep.c:5838 mutexunlockslowpath+0xe2/0x750 kernel/locking/mutex.c:912 sgrelease+0x1f4/0x2e0 drivers/scsi/sg.c:407
In sgrelease(), the function krefput(&sfp->fref, sgremovesfp) is called before releasing the openrellock mutex. The krefput() call may decrement the reference count of sfp to zero, triggering its cleanup through sgremovesfp(). This cleanup includes scheduling deferred work via sgremovesfpusercontext(), which ultimately frees sfp.
After krefput(), sgrelease() continues to unlock openrellock and may reference sfp or sdp. If sfp has already been freed, this results in a slab-use-after-free error.
Move the krefput(&sfp->fref, sgremovesfp) call after unlocking the openrellock mutex. This ensures:
- No references to sfp or sdp occur after the reference count is decremented.
- Cleanup functions such as sgremovesfp() and sgremovesfpusercontext() can safely execute without impacting the mutex handling in sgrelease().
The fix has been tested and validated by syzbot. This patch closes the bug reported at the following syzkaller link and ensures proper sequencing of resource cleanup and mutex operations, eliminating the risk of use-after-free errors in sgrelease().
Other sources
This CVE was automatically created from a reference found in an email or other text. If you are reading this, then this CVE entry is probably erroneous, since this text should be replaced by the official CVE description automatically.
— Launchpad
Affected Software
Remediation
Recommended actions to resolve this vulnerability, in priority order.
- Upgrade
Upgrade
debian/linuxto a version that resolves this vulnerability.Fixed in 5.10.234-1Fixed in 6.1.129-1Fixed in 6.1.135-1Fixed in 6.12.27-1 - Upgrade
Upgrade
debian/linux-6.1to a version that resolves this vulnerability.Fixed in 6.1.129-1~deb11u1 - Configuration
In drivers/scsi/sg.c in sg_release(), move the call to kref_put(&sfp->f_ref, sg_remove_sfp) so it occurs after unlocking the open_rel_lock mutex. This ensures sg_remove_sfp_usercontext() cleanup can run without any use-after-free references to sfp/sdp after the reference count is decremented.
Linux kernel (scsi: sg) sg_release() cleanup sequencing = kref_put after unlocking open_rel_lock
Event History
Frequently Asked Questions
What is the severity of CVE-2024-56631?
CVE-2024-56631 has a medium severity rating due to its potential to cause a use-after-free condition in the Linux kernel.
How do I fix CVE-2024-56631?
To fix CVE-2024-56631, upgrade your Linux kernel to a version that includes the patch for this vulnerability.
Which Linux kernel versions are affected by CVE-2024-56631?
CVE-2024-56631 affects Linux kernel versions from 3.16.85 to 6.6.66 and versions between 6.7 and 6.12.5.
What causes the CVE-2024-56631 vulnerability?
CVE-2024-56631 is caused by a use-after-free bug in the sg_release() function in the Linux kernel.
Is CVE-2024-56631 detectable by security tools?
Yes, CVE-2024-56631 can be detected by security tools that utilize Kernel Address Sanitizer (KASAN) during their testing processes.