Skip to contentSkip to conversation

st.c: possible memory use after free after MTSETBLK ioctl

PATCH David Jeffery 2 participants 2 messages Sep 28, 2009 1 message not in the archive

Subsystems (2)
lore
Share & tools
lore

A message that is not in the archive (sent off-list or not yet received): 511326308.574361254160109240.JavaMail.root@zmail02.collab.prod.int.phx2.redhat.com

#1 lore raw
Quoted 11 lines · Kai Makisara

Having thought this a little more, it would probably be best to remove the
call to normalize_buffer() instead of trying to cope with it failing. This
just means that some memory may stay allocated longer. In the usage scenarios coming into my mind, the buffer is either small or it will be
reallocated.

Quoted 4 lines

Would you like to make the patch?

Thanks,
Kai

How does this look? Compiles and passes initial tests. I'd left the normalize_buffer() in originally thinking there must be a reason for it, but you're right it just seems to make buffer handling problematic. This patch removes the normalize_buffer() call and the specialness of changing from a 0 to non-zero blocksize to fix the possible use of memory after it has been freed by the st driver.

drivers/scsi/st.c +0 -3
@@ -2859,11 +2859,8 @@
 			ioctl_result = st_int_ioctl(STp, MTBSF, 1);
 
 		if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
-			int old_block_size = STp->block_size;
 			STp->block_size = arg & MT_ST_BLKSIZE_MASK;
 			if (STp->block_size != 0) {
-				if (old_block_size == 0)
-					normalize_buffer(STp->buffer);
 				(STp->buffer)->buffer_blocks =
 				    (STp->buffer)->buffer_size / STp->block_size;
 			}
Acked-by
#2 lore raw
Quoted 30 lines · David Jeffery
Quoted 11 lines · Kai Makisara

Having thought this a little more, it would probably be best to remove the
call to normalize_buffer() instead of trying to cope with it failing. This
just means that some memory may stay allocated longer. In the usage scenarios coming into my mind, the buffer is either small or it will be
reallocated.

Quoted 4 lines

Would you like to make the patch?

Thanks,
Kai

How does this look? Compiles and passes initial tests. I'd left the normalize_buffer() in originally thinking there must be a reason for it, but you're right it just seems to make buffer handling problematic. This patch removes the normalize_buffer() call and the specialness of changing from a 0 to non-zero blocksize to fix the possible use of memory after it has been freed by the st driver.

drivers/scsi/st.c +0 -0
@@ -2859,11 +2859,8 @@
 			ioctl_result = st_int_ioctl(STp, MTBSF, 1);

drivers/scsi/st.c line 2866

Quoted 10 lines
drivers/scsi/st.c +0 -3
 
 		if (cmd_in == MTSETBLK || cmd_in == SET_DENS_AND_BLK) {
-			int old_block_size = STp->block_size;
 			STp->block_size = arg & MT_ST_BLKSIZE_MASK;
 			if (STp->block_size != 0) {
-				if (old_block_size == 0)
-					normalize_buffer(STp->buffer);
 				(STp->buffer)->buffer_blocks =
 				    (STp->buffer)->buffer_size / STp->block_size;
 			}

Looks good to me. There were reasons when it was inserted, but many things have changed since then: memory was expensive, I/O always used the buffer, normalize_buffer() did not free the last segment, etc.

Thanks,
Kai