[PATCH 2/2] uio: avoid module unloding of in module created UIO devices

From: Benedikt Spranger
Date: Thu Dec 06 2012 - 07:47:27 EST


A kernel module can create a uio device. Get a reference to the module, if the
UIO device is in use. Otherwise the device can be removed and a uio write or an
access to am mmaped memory can cause a kernel Oops or other strange effects.

Signed-off-by: Benedikt Spranger <b.spranger@xxxxxxxxxxxxx>
---
drivers/uio/uio.c | 8 ++++++++
include/linux/uio_driver.h | 1 +
2 files changed, 9 insertions(+)

diff --git a/drivers/uio/uio.c b/drivers/uio/uio.c
index b96499a..3b62da0 100644
--- a/drivers/uio/uio.c
+++ b/drivers/uio/uio.c
@@ -446,6 +446,7 @@ static void uio_release_listener(struct kref *kref)
idev->info->release(idev->info);

module_put(idev->owner);
+ module_put(idev->info->owner);
kfree(listener);
}

@@ -468,6 +469,11 @@ static int uio_open(struct inode *inode, struct file *filep)
goto out;
}

+ if (!try_module_get(idev->info->owner)) {
+ ret = -ENODEV;
+ goto err_get_module;
+ }
+
listener = kmalloc(sizeof(*listener), GFP_KERNEL);
if (!listener) {
ret = -ENOMEM;
@@ -493,6 +499,8 @@ err_infoopen:

err_alloc_listener:
module_put(idev->owner);
+err_get_module:
+ module_put(idev->owner);

out:
return ret;
diff --git a/include/linux/uio_driver.h b/include/linux/uio_driver.h
index 1bc6493..2862de23 100644
--- a/include/linux/uio_driver.h
+++ b/include/linux/uio_driver.h
@@ -95,6 +95,7 @@ struct uio_info {
int (*open)(struct uio_info *info);
void (*release)(struct uio_info *info);
int (*irqcontrol)(struct uio_info *info, s32 irq_on);
+ struct module *owner;
};

extern int __must_check
--
1.7.10.4

--
To unsubscribe from this list: send the line "unsubscribe linux-kernel" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at http://vger.kernel.org/majordomo-info.html
Please read the FAQ at http://www.tux.org/lkml/