iommufd: Reject opens until module initialization succeeds
PATCH 1/1 iommufd: Reject opens until module initialization succeeds
iommufd_init() publishes /dev/iommu before registering the optional /dev/vfio/vfio compatibility node and running the optional self-test initialization. Either later operation can fail. Since file operations can acquire a reference to a COMING module, an already-open file can survive the failed initialization and refer to freed iommufd module memory.
Gate the shared open callback until all fallible initialization has completed. Publish the ready flag with release semantics and read it with acquire semantics so successful opens see the initialized state.
An early rejected open also needs to drop its module-backed operations table. misc_open() holds misc_mtx across the callback, so deregistration waits for it, but the VFS's fops_put() on the failed open happens later. Restore the inode's core character-device operations before returning -ENODEV, avoiding that remaining reference to the failed module.
---
I have a reproducer for this issue and can share via DM
drivers/iommu/iommufd/main.c | 15 +++++++++++++++ 1 file changed, 15 insertions(+)
drivers/iommu/iommufd/main.c +15 -0
@@ -16,6 +16,7 @@#include <linux/module.h> #include <linux/mutex.h> #include <linux/slab.h> +#include <linux/smp.h> #include <uapi/linux/iommufd.h> #include "io_pagetable.h"
@@ -30,6 +31,7 @@ struct iommufd_object_ops { };static const struct iommufd_object_ops iommufd_object_ops[];static struct miscdevice vfio_misc_dev; +static bool iommufd_ready; struct iommufd_object *_iommufd_object_alloc(struct iommufd_ctx *ictx, size_t size,
@@ -316,6 +318,17 @@ static int iommufd_fops_open(struct inode *inode, struct file *filp) {struct iommufd_ctx *ictx; +/* Pairs with the ready publication at the end of iommufd_init(). */ +if (!smp_load_acquire (&iommufd_ready)) { +/* +* misc_deregister() waits for this callback, but not the +* VFS's fops_put() after a failed open. Restore the core +* character-device operations before dropping misc_mtx. +*/ +replace_fops (filp,fops_get (inode->i_fop)); +return -ENODEV ; + } + ictx =kzalloc_obj (*ictx,GFP_KERNEL_ACCOUNT );if (!ictx)return -ENOMEM ;
@@ -799,6 +812,8 @@ static int __init iommufd_init(void) ret =iommufd_test_init ();if (ret) goto err_vfio_misc; +/* Pairs with iommufd_fops_open(); no fallible setup may follow. */ +smp_store_release (&iommufd_ready, true);return 0 ; err_vfio_misc:
base-commit: d89e1d186ec9271bf78e62771779e9fc03bf0fc3
Signature · 1 line
2.56.0
Quoted 6 lines · Kyle Zeng
iommufd_init()publishes/dev/iommubefore registering the optional/dev/vfio/vfiocompatibility node and running the optional self-test initialization. Either later operation can fail. Since file operations can acquire a reference to a COMING module, an already-open file can survive the failed initialization and refer to freed iommufd module memory.
What? This seems like a core bug not iommufd.
files take refcounts on their modules when they open via this:
static const struct file_operations iommufd_fops = {
.owner = THIS_MODULE,
If the module hasn't even loaded yet I would rather expect the core code to not allow the module refcount to be taken, and if it somehow succeeds then it shouldn't allow the module to get yanked out from under it.
It is hard to guess what crashes to be more specific, but at least misc_deregister() is safe while files are open.
Though I wonder how module auto loading works..
Jason
Quoted 20 lines · Jason Gunthorpe
Quoted 6 lines · Kyle Zeng
iommufd_init()publishes/dev/iommubefore registering the optional/dev/vfio/vfiocompatibility node and running the optional self-test initialization. Either later operation can fail. Since file operations can acquire a reference to a COMING module, an already-open file can survive the failed initialization and refer to freed iommufd module memory.What? This seems like a core bug not iommufd.
files take refcounts on their modules when they open via this:
static const struct
file_operationsiommufd_fops= {
.owner =THIS_MODULE,If the module hasn't even loaded yet I would rather expect the core code to not allow the module refcount to be taken, and if it somehow succeeds then it shouldn't allow the module to get yanked out from under it.
Quoted 6 lines
It is hard to guess what crashes to be more specific, but at least
misc_deregister()is safe while files are open.Though I wonder how module auto loading works..
Jason
Hi Jason,
Sorry for the confusion. This is supposed to be a two-patch series and I accidentally dropped the first patch.
I'll resend the patch series. Also, I have a reproducer for this issue and can provide through DM.
Best,
Kyle