Skip to content

Commit e32c9e6

Browse files
Vaughan CaoJames Bottomley
Vaughan Cao
authored and
James Bottomley
committed
[SCSI] sg: checking sdp->detached isn't protected when open
@detached is set under the protection of sg_index_lock. Without getting the lock, new sfp will be added during sg removal and there is no chance for it to be picked out. So check with sg_index_lock held in sg_add_sfp(). Signed-off-by: Vaughan Cao <[email protected]> Acked-by: Douglas Gilbert <[email protected]> Signed-off-by: James Bottomley <[email protected]>
1 parent 00b2d9d commit e32c9e6

File tree

1 file changed

+9
-8
lines changed

1 file changed

+9
-8
lines changed

drivers/scsi/sg.c

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -295,23 +295,20 @@ sg_open(struct inode *inode, struct file *filp)
295295
if (flags & O_EXCL)
296296
sdp->exclude = 1; /* used by release lock */
297297

298-
if (sdp->detached) {
299-
retval = -ENODEV;
300-
goto sem_out;
301-
}
302298
if (sfds_list_empty(sdp)) { /* no existing opens on this device */
303299
sdp->sgdebug = 0;
304300
q = sdp->device->request_queue;
305301
sdp->sg_tablesize = queue_max_segments(q);
306302
}
307-
if ((sfp = sg_add_sfp(sdp, dev)))
303+
sfp = sg_add_sfp(sdp, dev);
304+
if (!IS_ERR(sfp))
308305
filp->private_data = sfp;
309306
/* retval is already provably zero at this point because of the
310307
* check after retval = scsi_autopm_get_device(sdp->device))
311308
*/
312309
else {
313-
retval = -ENOMEM;
314-
sem_out:
310+
retval = PTR_ERR(sfp);
311+
315312
if (flags & O_EXCL) {
316313
sdp->exclude = 0; /* undo if error */
317314
up_write(&sdp->o_sem);
@@ -2045,7 +2042,7 @@ sg_add_sfp(Sg_device * sdp, int dev)
20452042

20462043
sfp = kzalloc(sizeof(*sfp), GFP_ATOMIC | __GFP_NOWARN);
20472044
if (!sfp)
2048-
return NULL;
2045+
return ERR_PTR(-ENOMEM);
20492046

20502047
init_waitqueue_head(&sfp->read_wait);
20512048
rwlock_init(&sfp->rq_list_lock);
@@ -2060,6 +2057,10 @@ sg_add_sfp(Sg_device * sdp, int dev)
20602057
sfp->keep_orphan = SG_DEF_KEEP_ORPHAN;
20612058
sfp->parentdp = sdp;
20622059
write_lock_irqsave(&sg_index_lock, iflags);
2060+
if (sdp->detached) {
2061+
write_unlock_irqrestore(&sg_index_lock, iflags);
2062+
return ERR_PTR(-ENODEV);
2063+
}
20632064
list_add_tail(&sfp->sfd_siblings, &sdp->sfds);
20642065
write_unlock_irqrestore(&sg_index_lock, iflags);
20652066
SCSI_LOG_TIMEOUT(3, printk("sg_add_sfp: sfp=0x%p\n", sfp));

0 commit comments

Comments
 (0)