Skip to content

Commit f41ced8

Browse files
Laurent Pincharttorvalds
authored andcommitted
Check fops_get() return value
Several subsystem open handlers dereference the fops_get() return value without checking it for nullness. This opens a race condition between the open handler and module unloading. A module can be marked as being unloaded (MODULE_STATE_GOING) before its exit function is called and gets the chance to unregister the driver. During that window open handlers can still be called, and fops_get() will fail in try_module_get() and return a NULL pointer. This change checks the fops_get() return value and returns -ENODEV if NULL. Reported-by: Alan Jenkins <alan-jenkins@tuffmail.co.uk> Signed-off-by: Laurent Pinchart <laurent.pinchart@skynet.be> Acked-by: Takashi Iwai <tiwai@suse.de> Cc: Al Viro <viro@zeniv.linux.org.uk> Cc: Dave Airlie <airlied@linux.ie> Acked-by: Mauro Carvalho Chehab <mchehab@infradead.org> Signed-off-by: Andrew Morton <akpm@linux-foundation.org> Signed-off-by: Linus Torvalds <torvalds@linux-foundation.org>
1 parent bdbeed7 commit f41ced8

3 files changed

Lines changed: 13 additions & 0 deletions

File tree

‎drivers/gpu/drm/drm_fops.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ int drm_stub_open(struct inode *inode, struct file *filp)
183183

184184
old_fops = filp->f_op;
185185
filp->f_op = fops_get(&dev->driver->fops);
186+
if (filp->f_op == NULL) {
187+
filp->f_op = old_fops;
188+
goto out;
189+
}
186190
if (filp->f_op->open && (err = filp->f_op->open(inode, filp))) {
187191
fops_put(filp->f_op);
188192
filp->f_op = fops_get(old_fops);

‎drivers/media/dvb/dvb-core/dvbdev.c‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,10 @@ static int dvb_device_open(struct inode *inode, struct file *file)
7979
file->private_data = dvbdev;
8080
old_fops = file->f_op;
8181
file->f_op = fops_get(dvbdev->fops);
82+
if (file->f_op == NULL) {
83+
file->f_op = old_fops;
84+
goto fail;
85+
}
8286
if(file->f_op->open)
8387
err = file->f_op->open(inode,file);
8488
if (err) {
@@ -90,6 +94,7 @@ static int dvb_device_open(struct inode *inode, struct file *file)
9094
unlock_kernel();
9195
return err;
9296
}
97+
fail:
9398
up_read(&minor_rwsem);
9499
unlock_kernel();
95100
return -ENODEV;

‎sound/core/sound.c‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,10 @@ static int __snd_open(struct inode *inode, struct file *file)
152152
}
153153
old_fops = file->f_op;
154154
file->f_op = fops_get(mptr->f_ops);
155+
if (file->f_op == NULL) {
156+
file->f_op = old_fops;
157+
return -ENODEV;
158+
}
155159
if (file->f_op->open)
156160
err = file->f_op->open(inode, file);
157161
if (err) {

0 commit comments

Comments
 (0)