Skip to content

Commit a93e7b3

Browse files
Hamish Martingregkh
Hamish Martin
authored andcommittedMay 14, 2018
uio: Prevent device destruction while fds are open
Prevent destruction of a uio_device while user space apps hold open file descriptors to that device. Further, access to the 'info' member of the struct uio_device is protected by spinlock. This is to ensure stale pointers to data not under control of the UIO subsystem are not dereferenced. Signed-off-by: Hamish Martin <[email protected]> Reviewed-by: Chris Packham <[email protected]> Signed-off-by: Greg Kroah-Hartman <[email protected]>
1 parent 81daa40 commit a93e7b3

File tree

2 files changed

+75
-27
lines changed

2 files changed

+75
-27
lines changed
 

‎drivers/uio/uio.c

+72-26
Original file line numberDiff line numberDiff line change
@@ -270,7 +270,7 @@ static int uio_dev_add_attributes(struct uio_device *idev)
270270
if (!map_found) {
271271
map_found = 1;
272272
idev->map_dir = kobject_create_and_add("maps",
273-
&idev->dev->kobj);
273+
&idev->dev.kobj);
274274
if (!idev->map_dir) {
275275
ret = -ENOMEM;
276276
goto err_map;
@@ -299,7 +299,7 @@ static int uio_dev_add_attributes(struct uio_device *idev)
299299
if (!portio_found) {
300300
portio_found = 1;
301301
idev->portio_dir = kobject_create_and_add("portio",
302-
&idev->dev->kobj);
302+
&idev->dev.kobj);
303303
if (!idev->portio_dir) {
304304
ret = -ENOMEM;
305305
goto err_portio;
@@ -342,7 +342,7 @@ static int uio_dev_add_attributes(struct uio_device *idev)
342342
kobject_put(&map->kobj);
343343
}
344344
kobject_put(idev->map_dir);
345-
dev_err(idev->dev, "error creating sysfs files (%d)\n", ret);
345+
dev_err(&idev->dev, "error creating sysfs files (%d)\n", ret);
346346
return ret;
347347
}
348348

@@ -379,7 +379,7 @@ static int uio_get_minor(struct uio_device *idev)
379379
idev->minor = retval;
380380
retval = 0;
381381
} else if (retval == -ENOSPC) {
382-
dev_err(idev->dev, "too many uio devices\n");
382+
dev_err(&idev->dev, "too many uio devices\n");
383383
retval = -EINVAL;
384384
}
385385
mutex_unlock(&minor_lock);
@@ -433,6 +433,7 @@ static int uio_open(struct inode *inode, struct file *filep)
433433
struct uio_device *idev;
434434
struct uio_listener *listener;
435435
int ret = 0;
436+
unsigned long flags;
436437

437438
mutex_lock(&minor_lock);
438439
idev = idr_find(&uio_idr, iminor(inode));
@@ -442,9 +443,11 @@ static int uio_open(struct inode *inode, struct file *filep)
442443
goto out;
443444
}
444445

446+
get_device(&idev->dev);
447+
445448
if (!try_module_get(idev->owner)) {
446449
ret = -ENODEV;
447-
goto out;
450+
goto err_module_get;
448451
}
449452

450453
listener = kmalloc(sizeof(*listener), GFP_KERNEL);
@@ -457,11 +460,13 @@ static int uio_open(struct inode *inode, struct file *filep)
457460
listener->event_count = atomic_read(&idev->event);
458461
filep->private_data = listener;
459462

460-
if (idev->info->open) {
463+
spin_lock_irqsave(&idev->info_lock, flags);
464+
if (idev->info && idev->info->open)
461465
ret = idev->info->open(idev->info, inode);
462-
if (ret)
463-
goto err_infoopen;
464-
}
466+
spin_unlock_irqrestore(&idev->info_lock, flags);
467+
if (ret)
468+
goto err_infoopen;
469+
465470
return 0;
466471

467472
err_infoopen:
@@ -470,6 +475,9 @@ static int uio_open(struct inode *inode, struct file *filep)
470475
err_alloc_listener:
471476
module_put(idev->owner);
472477

478+
err_module_get:
479+
put_device(&idev->dev);
480+
473481
out:
474482
return ret;
475483
}
@@ -487,22 +495,33 @@ static int uio_release(struct inode *inode, struct file *filep)
487495
int ret = 0;
488496
struct uio_listener *listener = filep->private_data;
489497
struct uio_device *idev = listener->dev;
498+
unsigned long flags;
490499

491-
if (idev->info->release)
500+
spin_lock_irqsave(&idev->info_lock, flags);
501+
if (idev->info && idev->info->release)
492502
ret = idev->info->release(idev->info, inode);
503+
spin_unlock_irqrestore(&idev->info_lock, flags);
493504

494505
module_put(idev->owner);
495506
kfree(listener);
507+
put_device(&idev->dev);
496508
return ret;
497509
}
498510

499511
static __poll_t uio_poll(struct file *filep, poll_table *wait)
500512
{
501513
struct uio_listener *listener = filep->private_data;
502514
struct uio_device *idev = listener->dev;
515+
__poll_t ret = 0;
516+
unsigned long flags;
503517

504-
if (!idev->info->irq)
505-
return -EIO;
518+
spin_lock_irqsave(&idev->info_lock, flags);
519+
if (!idev->info || !idev->info->irq)
520+
ret = -EIO;
521+
spin_unlock_irqrestore(&idev->info_lock, flags);
522+
523+
if (ret)
524+
return ret;
506525

507526
poll_wait(filep, &idev->wait, wait);
508527
if (listener->event_count != atomic_read(&idev->event))
@@ -516,11 +535,17 @@ static ssize_t uio_read(struct file *filep, char __user *buf,
516535
struct uio_listener *listener = filep->private_data;
517536
struct uio_device *idev = listener->dev;
518537
DECLARE_WAITQUEUE(wait, current);
519-
ssize_t retval;
538+
ssize_t retval = 0;
520539
s32 event_count;
540+
unsigned long flags;
521541

522-
if (!idev->info->irq)
523-
return -EIO;
542+
spin_lock_irqsave(&idev->info_lock, flags);
543+
if (!idev->info || !idev->info->irq)
544+
retval = -EIO;
545+
spin_unlock_irqrestore(&idev->info_lock, flags);
546+
547+
if (retval)
548+
return retval;
524549

525550
if (count != sizeof(s32))
526551
return -EINVAL;
@@ -567,8 +592,10 @@ static ssize_t uio_write(struct file *filep, const char __user *buf,
567592
struct uio_device *idev = listener->dev;
568593
ssize_t retval;
569594
s32 irq_on;
595+
unsigned long flags;
570596

571-
if (!idev->info->irq) {
597+
spin_lock_irqsave(&idev->info_lock, flags);
598+
if (!idev->info || !idev->info->irq) {
572599
retval = -EIO;
573600
goto out;
574601
}
@@ -591,6 +618,7 @@ static ssize_t uio_write(struct file *filep, const char __user *buf,
591618
retval = idev->info->irqcontrol(idev->info, irq_on);
592619

593620
out:
621+
spin_unlock_irqrestore(&idev->info_lock, flags);
594622
return retval ? retval : sizeof(s32);
595623
}
596624

@@ -803,6 +831,13 @@ static void release_uio_class(void)
803831
uio_major_cleanup();
804832
}
805833

834+
static void uio_device_release(struct device *dev)
835+
{
836+
struct uio_device *idev = dev_get_drvdata(dev);
837+
838+
kfree(idev);
839+
}
840+
806841
/**
807842
* uio_register_device - register a new userspace IO device
808843
* @owner: module that creates the new device
@@ -823,28 +858,34 @@ int __uio_register_device(struct module *owner,
823858

824859
info->uio_dev = NULL;
825860

826-
idev = devm_kzalloc(parent, sizeof(*idev), GFP_KERNEL);
861+
idev = kzalloc(sizeof(*idev), GFP_KERNEL);
827862
if (!idev) {
828863
return -ENOMEM;
829864
}
830865

831866
idev->owner = owner;
832867
idev->info = info;
868+
spin_lock_init(&idev->info_lock);
833869
init_waitqueue_head(&idev->wait);
834870
atomic_set(&idev->event, 0);
835871

836872
ret = uio_get_minor(idev);
837873
if (ret)
838874
return ret;
839875

840-
idev->dev = device_create(&uio_class, parent,
841-
MKDEV(uio_major, idev->minor), idev,
842-
"uio%d", idev->minor);
843-
if (IS_ERR(idev->dev)) {
844-
printk(KERN_ERR "UIO: device register failed\n");
845-
ret = PTR_ERR(idev->dev);
876+
idev->dev.devt = MKDEV(uio_major, idev->minor);
877+
idev->dev.class = &uio_class;
878+
idev->dev.parent = parent;
879+
idev->dev.release = uio_device_release;
880+
dev_set_drvdata(&idev->dev, idev);
881+
882+
ret = dev_set_name(&idev->dev, "uio%d", idev->minor);
883+
if (ret)
884+
goto err_device_create;
885+
886+
ret = device_register(&idev->dev);
887+
if (ret)
846888
goto err_device_create;
847-
}
848889

849890
ret = uio_dev_add_attributes(idev);
850891
if (ret)
@@ -872,7 +913,7 @@ int __uio_register_device(struct module *owner,
872913
err_request_irq:
873914
uio_dev_del_attributes(idev);
874915
err_uio_dev_add_attributes:
875-
device_destroy(&uio_class, MKDEV(uio_major, idev->minor));
916+
device_unregister(&idev->dev);
876917
err_device_create:
877918
uio_free_minor(idev);
878919
return ret;
@@ -887,6 +928,7 @@ EXPORT_SYMBOL_GPL(__uio_register_device);
887928
void uio_unregister_device(struct uio_info *info)
888929
{
889930
struct uio_device *idev;
931+
unsigned long flags;
890932

891933
if (!info || !info->uio_dev)
892934
return;
@@ -900,7 +942,11 @@ void uio_unregister_device(struct uio_info *info)
900942
if (info->irq && info->irq != UIO_IRQ_CUSTOM)
901943
free_irq(info->irq, idev);
902944

903-
device_destroy(&uio_class, MKDEV(uio_major, idev->minor));
945+
spin_lock_irqsave(&idev->info_lock, flags);
946+
idev->info = NULL;
947+
spin_unlock_irqrestore(&idev->info_lock, flags);
948+
949+
device_unregister(&idev->dev);
904950

905951
return;
906952
}

‎include/linux/uio_driver.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
#ifndef _UIO_DRIVER_H_
1515
#define _UIO_DRIVER_H_
1616

17+
#include <linux/device.h>
1718
#include <linux/fs.h>
1819
#include <linux/interrupt.h>
1920

@@ -68,12 +69,13 @@ struct uio_port {
6869

6970
struct uio_device {
7071
struct module *owner;
71-
struct device *dev;
72+
struct device dev;
7273
int minor;
7374
atomic_t event;
7475
struct fasync_struct *async_queue;
7576
wait_queue_head_t wait;
7677
struct uio_info *info;
78+
spinlock_t info_lock;
7779
struct kobject *map_dir;
7880
struct kobject *portio_dir;
7981
};

0 commit comments

Comments
 (0)
Please sign in to comment.